blob: 6e15a55df6364d7b7555cbe4bd8132fd7e7c30d1 [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);
558static void f_eventhandler(typval_T *argvars, typval_T *rettv);
559static void f_executable(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79815f12016-07-09 17:07:29 +0200560static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static 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);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200596#if defined(FEAT_CMDL_COMPL)
597static void f_getcompletion(typval_T *argvars, typval_T *rettv);
598#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100599static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
600static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
601static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
602static void f_getcwd(typval_T *argvars, typval_T *rettv);
603static void f_getfontname(typval_T *argvars, typval_T *rettv);
604static void f_getfperm(typval_T *argvars, typval_T *rettv);
605static void f_getfsize(typval_T *argvars, typval_T *rettv);
606static void f_getftime(typval_T *argvars, typval_T *rettv);
607static void f_getftype(typval_T *argvars, typval_T *rettv);
608static void f_getline(typval_T *argvars, typval_T *rettv);
609static void f_getmatches(typval_T *argvars, typval_T *rettv);
610static void f_getpid(typval_T *argvars, typval_T *rettv);
611static void f_getcurpos(typval_T *argvars, typval_T *rettv);
612static void f_getpos(typval_T *argvars, typval_T *rettv);
613static void f_getqflist(typval_T *argvars, typval_T *rettv);
614static void f_getreg(typval_T *argvars, typval_T *rettv);
615static void f_getregtype(typval_T *argvars, typval_T *rettv);
616static void f_gettabvar(typval_T *argvars, typval_T *rettv);
617static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
618static void f_getwinposx(typval_T *argvars, typval_T *rettv);
619static void f_getwinposy(typval_T *argvars, typval_T *rettv);
620static void f_getwinvar(typval_T *argvars, typval_T *rettv);
621static void f_glob(typval_T *argvars, typval_T *rettv);
622static void f_globpath(typval_T *argvars, typval_T *rettv);
623static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
624static void f_has(typval_T *argvars, typval_T *rettv);
625static void f_has_key(typval_T *argvars, typval_T *rettv);
626static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
627static void f_hasmapto(typval_T *argvars, typval_T *rettv);
628static void f_histadd(typval_T *argvars, typval_T *rettv);
629static void f_histdel(typval_T *argvars, typval_T *rettv);
630static void f_histget(typval_T *argvars, typval_T *rettv);
631static void f_histnr(typval_T *argvars, typval_T *rettv);
632static void f_hlID(typval_T *argvars, typval_T *rettv);
633static void f_hlexists(typval_T *argvars, typval_T *rettv);
634static void f_hostname(typval_T *argvars, typval_T *rettv);
635static void f_iconv(typval_T *argvars, typval_T *rettv);
636static void f_indent(typval_T *argvars, typval_T *rettv);
637static void f_index(typval_T *argvars, typval_T *rettv);
638static void f_input(typval_T *argvars, typval_T *rettv);
639static void f_inputdialog(typval_T *argvars, typval_T *rettv);
640static void f_inputlist(typval_T *argvars, typval_T *rettv);
641static void f_inputrestore(typval_T *argvars, typval_T *rettv);
642static void f_inputsave(typval_T *argvars, typval_T *rettv);
643static void f_inputsecret(typval_T *argvars, typval_T *rettv);
644static void f_insert(typval_T *argvars, typval_T *rettv);
645static void f_invert(typval_T *argvars, typval_T *rettv);
646static void f_isdirectory(typval_T *argvars, typval_T *rettv);
647static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100648#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
649static void f_isnan(typval_T *argvars, typval_T *rettv);
650#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100652#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100653static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100654static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100655static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100656static void f_job_start(typval_T *argvars, typval_T *rettv);
657static void f_job_stop(typval_T *argvars, typval_T *rettv);
658static void f_job_status(typval_T *argvars, typval_T *rettv);
659#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100661static void f_js_decode(typval_T *argvars, typval_T *rettv);
662static void f_js_encode(typval_T *argvars, typval_T *rettv);
663static void f_json_decode(typval_T *argvars, typval_T *rettv);
664static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_keys(typval_T *argvars, typval_T *rettv);
666static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
667static void f_len(typval_T *argvars, typval_T *rettv);
668static void f_libcall(typval_T *argvars, typval_T *rettv);
669static void f_libcallnr(typval_T *argvars, typval_T *rettv);
670static void f_line(typval_T *argvars, typval_T *rettv);
671static void f_line2byte(typval_T *argvars, typval_T *rettv);
672static void f_lispindent(typval_T *argvars, typval_T *rettv);
673static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000674#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_log(typval_T *argvars, typval_T *rettv);
676static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000677#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200678#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_map(typval_T *argvars, typval_T *rettv);
682static void f_maparg(typval_T *argvars, typval_T *rettv);
683static void f_mapcheck(typval_T *argvars, typval_T *rettv);
684static void f_match(typval_T *argvars, typval_T *rettv);
685static void f_matchadd(typval_T *argvars, typval_T *rettv);
686static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
687static void f_matcharg(typval_T *argvars, typval_T *rettv);
688static void f_matchdelete(typval_T *argvars, typval_T *rettv);
689static void f_matchend(typval_T *argvars, typval_T *rettv);
690static void f_matchlist(typval_T *argvars, typval_T *rettv);
691static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200692static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_max(typval_T *argvars, typval_T *rettv);
694static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000695#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000697#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100699#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100701#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
703static void f_nr2char(typval_T *argvars, typval_T *rettv);
704static void f_or(typval_T *argvars, typval_T *rettv);
705static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100706#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100708#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
713static void f_printf(typval_T *argvars, typval_T *rettv);
714static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200715#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200717#endif
718#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_range(typval_T *argvars, typval_T *rettv);
722static void f_readfile(typval_T *argvars, typval_T *rettv);
723static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100724#ifdef FEAT_FLOAT
725static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
726#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_reltimestr(typval_T *argvars, typval_T *rettv);
728static void f_remote_expr(typval_T *argvars, typval_T *rettv);
729static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
730static void f_remote_peek(typval_T *argvars, typval_T *rettv);
731static void f_remote_read(typval_T *argvars, typval_T *rettv);
732static void f_remote_send(typval_T *argvars, typval_T *rettv);
733static void f_remove(typval_T *argvars, typval_T *rettv);
734static void f_rename(typval_T *argvars, typval_T *rettv);
735static void f_repeat(typval_T *argvars, typval_T *rettv);
736static void f_resolve(typval_T *argvars, typval_T *rettv);
737static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000740#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_screenattr(typval_T *argvars, typval_T *rettv);
742static void f_screenchar(typval_T *argvars, typval_T *rettv);
743static void f_screencol(typval_T *argvars, typval_T *rettv);
744static void f_screenrow(typval_T *argvars, typval_T *rettv);
745static void f_search(typval_T *argvars, typval_T *rettv);
746static void f_searchdecl(typval_T *argvars, typval_T *rettv);
747static void f_searchpair(typval_T *argvars, typval_T *rettv);
748static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
749static void f_searchpos(typval_T *argvars, typval_T *rettv);
750static void f_server2client(typval_T *argvars, typval_T *rettv);
751static void f_serverlist(typval_T *argvars, typval_T *rettv);
752static void f_setbufvar(typval_T *argvars, typval_T *rettv);
753static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
754static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100755static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_setline(typval_T *argvars, typval_T *rettv);
757static void f_setloclist(typval_T *argvars, typval_T *rettv);
758static void f_setmatches(typval_T *argvars, typval_T *rettv);
759static void f_setpos(typval_T *argvars, typval_T *rettv);
760static void f_setqflist(typval_T *argvars, typval_T *rettv);
761static void f_setreg(typval_T *argvars, typval_T *rettv);
762static void f_settabvar(typval_T *argvars, typval_T *rettv);
763static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
764static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100765#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100767#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_shellescape(typval_T *argvars, typval_T *rettv);
769static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
770static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_sin(typval_T *argvars, typval_T *rettv);
773static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000774#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_sort(typval_T *argvars, typval_T *rettv);
776static void f_soundfold(typval_T *argvars, typval_T *rettv);
777static void f_spellbadword(typval_T *argvars, typval_T *rettv);
778static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
779static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000780#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_sqrt(typval_T *argvars, typval_T *rettv);
782static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000783#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_str2nr(typval_T *argvars, typval_T *rettv);
785static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000788#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200789static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_stridx(typval_T *argvars, typval_T *rettv);
791static void f_string(typval_T *argvars, typval_T *rettv);
792static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200793static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_strpart(typval_T *argvars, typval_T *rettv);
795static void f_strridx(typval_T *argvars, typval_T *rettv);
796static void f_strtrans(typval_T *argvars, typval_T *rettv);
797static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
798static void f_strwidth(typval_T *argvars, typval_T *rettv);
799static void f_submatch(typval_T *argvars, typval_T *rettv);
800static void f_substitute(typval_T *argvars, typval_T *rettv);
801static void f_synID(typval_T *argvars, typval_T *rettv);
802static void f_synIDattr(typval_T *argvars, typval_T *rettv);
803static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
804static void f_synstack(typval_T *argvars, typval_T *rettv);
805static void f_synconcealed(typval_T *argvars, typval_T *rettv);
806static void f_system(typval_T *argvars, typval_T *rettv);
807static void f_systemlist(typval_T *argvars, typval_T *rettv);
808static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
809static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
810static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
811static void f_taglist(typval_T *argvars, typval_T *rettv);
812static void f_tagfiles(typval_T *argvars, typval_T *rettv);
813static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200814static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
815static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200816static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
817#ifdef FEAT_JOB_CHANNEL
818static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
819#endif
820static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
821#ifdef FEAT_JOB_CHANNEL
822static void f_test_null_job(typval_T *argvars, typval_T *rettv);
823#endif
824static void f_test_null_list(typval_T *argvars, typval_T *rettv);
825static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
826static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200827static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200828#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100829static void f_tan(typval_T *argvars, typval_T *rettv);
830static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200831#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100832#ifdef FEAT_TIMERS
833static void f_timer_start(typval_T *argvars, typval_T *rettv);
834static void f_timer_stop(typval_T *argvars, typval_T *rettv);
835#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static void f_tolower(typval_T *argvars, typval_T *rettv);
837static void f_toupper(typval_T *argvars, typval_T *rettv);
838static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000839#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100840static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000841#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100842static void f_type(typval_T *argvars, typval_T *rettv);
843static void f_undofile(typval_T *argvars, typval_T *rettv);
844static void f_undotree(typval_T *argvars, typval_T *rettv);
845static void f_uniq(typval_T *argvars, typval_T *rettv);
846static void f_values(typval_T *argvars, typval_T *rettv);
847static void f_virtcol(typval_T *argvars, typval_T *rettv);
848static void f_visualmode(typval_T *argvars, typval_T *rettv);
849static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100850static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100851static void f_win_getid(typval_T *argvars, typval_T *rettv);
852static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
853static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
854static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100855static void f_winbufnr(typval_T *argvars, typval_T *rettv);
856static void f_wincol(typval_T *argvars, typval_T *rettv);
857static void f_winheight(typval_T *argvars, typval_T *rettv);
858static void f_winline(typval_T *argvars, typval_T *rettv);
859static void f_winnr(typval_T *argvars, typval_T *rettv);
860static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
861static void f_winrestview(typval_T *argvars, typval_T *rettv);
862static void f_winsaveview(typval_T *argvars, typval_T *rettv);
863static void f_winwidth(typval_T *argvars, typval_T *rettv);
864static void f_writefile(typval_T *argvars, typval_T *rettv);
865static void f_wordcount(typval_T *argvars, typval_T *rettv);
866static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000867
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
869static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
870static int get_env_len(char_u **arg);
871static int get_id_len(char_u **arg);
872static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
873static 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 +0000874#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
875#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
876 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100877static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
878static int eval_isnamec(int c);
879static int eval_isnamec1(int c);
880static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
881static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100882static typval_T *alloc_string_tv(char_u *string);
883static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100884#ifdef FEAT_FLOAT
885static float_T get_tv_float(typval_T *varp);
886#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100887static linenr_T get_tv_lnum(typval_T *argvars);
888static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
890static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
891static hashtab_T *find_var_ht(char_u *name, char_u **varname);
892static funccall_T *get_funccal(void);
893static void vars_clear_ext(hashtab_T *ht, int free_val);
894static void delete_var(hashtab_T *ht, hashitem_T *hi);
895static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
896static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
897static void set_var(char_u *name, typval_T *varp, int copy);
898static int var_check_ro(int flags, char_u *name, int use_gettext);
899static int var_check_fixed(int flags, char_u *name, int use_gettext);
900static int var_check_func_name(char_u *name, int new_var);
901static int valid_varname(char_u *varname);
902static int tv_check_lock(int lock, char_u *name, int use_gettext);
903static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
904static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100905static 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 +0100906static int eval_fname_script(char_u *p);
907static int eval_fname_sid(char_u *p);
908static void list_func_head(ufunc_T *fp, int indent);
909static ufunc_T *find_func(char_u *name);
910static int function_exists(char_u *name);
911static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000912#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100913static void func_do_profile(ufunc_T *fp);
914static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
915static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000916static int
917# ifdef __BORLANDC__
918 _RTLENTRYF
919# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100920 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000921static int
922# ifdef __BORLANDC__
923 _RTLENTRYF
924# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100925 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000926#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100927static int script_autoload(char_u *name, int reload);
928static char_u *autoload_name(char_u *name);
929static void cat_func_name(char_u *buf, ufunc_T *fp);
930static void func_free(ufunc_T *fp);
931static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
932static int can_free_funccal(funccall_T *fc, int copyID) ;
933static void free_funccal(funccall_T *fc, int free_val);
934static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
935static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
936static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
937static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
938static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
939static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
940static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
941static int write_list(FILE *fd, list_T *list, int binary);
942static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200944
945#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100946static int compare_func_name(const void *s1, const void *s2);
947static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200948#endif
949
Bram Moolenaar33570922005-01-25 22:26:29 +0000950/*
951 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000952 */
953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100954eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000955{
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 int i;
957 struct vimvar *p;
958
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200959 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
960 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200961 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000962 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000963 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000964
965 for (i = 0; i < VV_LEN; ++i)
966 {
967 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200968 if (STRLEN(p->vv_name) > 16)
969 {
970 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
971 getout(1);
972 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000973 STRCPY(p->vv_di.di_key, p->vv_name);
974 if (p->vv_flags & VV_RO)
975 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
976 else if (p->vv_flags & VV_RO_SBX)
977 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
978 else
979 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000980
981 /* add to v: scope dict, unless the value is not always available */
982 if (p->vv_type != VAR_UNKNOWN)
983 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000984 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000985 /* add to compat scope dict */
986 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000987 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100988 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
989
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000990 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100991 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200992 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100993 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100994
995 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
996 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
997 set_vim_var_nr(VV_NONE, VVAL_NONE);
998 set_vim_var_nr(VV_NULL, VVAL_NULL);
999
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001000 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001001
1002#ifdef EBCDIC
1003 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +01001004 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001005 */
1006 sortFunctions();
1007#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001008}
1009
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010#if defined(EXITFREE) || defined(PROTO)
1011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001012eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001013{
1014 int i;
1015 struct vimvar *p;
1016
1017 for (i = 0; i < VV_LEN; ++i)
1018 {
1019 p = &vimvars[i];
1020 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001021 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001022 vim_free(p->vv_str);
1023 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001024 }
1025 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1026 {
1027 list_unref(p->vv_list);
1028 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001029 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030 }
1031 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001032 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001033 hash_clear(&compat_hashtab);
1034
Bram Moolenaard9fba312005-06-26 22:34:35 +00001035 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001036# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001037 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001038# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001039
1040 /* global variables */
1041 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001042
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001043 /* autoloaded script names */
1044 ga_clear_strings(&ga_loaded);
1045
Bram Moolenaarcca74132013-09-25 21:00:28 +02001046 /* Script-local variables. First clear all the variables and in a second
1047 * loop free the scriptvar_T, because a variable in one script might hold
1048 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001049 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001050 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001051 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001052 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001053 ga_clear(&ga_scripts);
1054
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001055 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001056 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001057
1058 /* functions */
1059 free_all_functions();
1060 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001061}
1062#endif
1063
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 * Return the name of the executed function.
1066 */
1067 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001068func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Return the address holding the next breakpoint line for a funccall cookie.
1075 */
1076 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001077func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/*
1083 * Return the address holding the debug tick for a funccall cookie.
1084 */
1085 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001086func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar33570922005-01-25 22:26:29 +00001088 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089}
1090
1091/*
1092 * Return the nesting level for a funccall cookie.
1093 */
1094 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001095func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096{
Bram Moolenaar33570922005-01-25 22:26:29 +00001097 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098}
1099
1100/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001101funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001103/* pointer to list of previously used funccal, still around because some
1104 * item in it is still being used. */
1105funccall_T *previous_funccal = NULL;
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107/*
1108 * Return TRUE when a function was ended by a ":return" command.
1109 */
1110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001111current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113 return current_funccal->returned;
1114}
1115
1116
1117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 * Set an internal variable to a string value. Creates the variable if it does
1119 * not already exist.
1120 */
1121 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001122set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001124 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001125 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
1127 val = vim_strsave(value);
1128 if (val != NULL)
1129 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001131 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001133 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001134 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
1136 }
1137}
1138
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001140#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001141static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142static char_u *redir_endp = NULL;
1143static char_u *redir_varname = NULL;
1144
1145/*
1146 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001147 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001148 * Returns OK if successfully completed the setup. FAIL otherwise.
1149 */
1150 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001151var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152{
1153 int save_emsg;
1154 int err;
1155 typval_T tv;
1156
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001158 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 {
1160 EMSG(_(e_invarg));
1161 return FAIL;
1162 }
1163
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001164 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 redir_varname = vim_strsave(name);
1166 if (redir_varname == NULL)
1167 return FAIL;
1168
1169 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1170 if (redir_lval == NULL)
1171 {
1172 var_redir_stop();
1173 return FAIL;
1174 }
1175
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001176 /* The output is stored in growarray "redir_ga" until redirection ends. */
1177 ga_init2(&redir_ga, (int)sizeof(char), 500);
1178
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001180 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001181 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1183 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001184 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 if (redir_endp != NULL && *redir_endp != NUL)
1186 /* Trailing characters are present after the variable name */
1187 EMSG(_(e_trailing));
1188 else
1189 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001190 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191 var_redir_stop();
1192 return FAIL;
1193 }
1194
1195 /* check if we can write to the variable: set it to or append an empty
1196 * string */
1197 save_emsg = did_emsg;
1198 did_emsg = FALSE;
1199 tv.v_type = VAR_STRING;
1200 tv.vval.v_string = (char_u *)"";
1201 if (append)
1202 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1203 else
1204 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001205 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001207 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 if (err)
1209 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001210 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211 var_redir_stop();
1212 return FAIL;
1213 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214
1215 return OK;
1216}
1217
1218/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001219 * Append "value[value_len]" to the variable set by var_redir_start().
1220 * The actual appending is postponed until redirection ends, because the value
1221 * appended may in fact be the string we write to, changing it may cause freed
1222 * memory to be used:
1223 * :redir => foo
1224 * :let foo
1225 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001226 */
1227 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001228var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001230 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001231
1232 if (redir_lval == NULL)
1233 return;
1234
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001235 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001236 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001238 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001239
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001240 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001241 {
1242 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001243 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001244 }
1245 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001246 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247}
1248
1249/*
1250 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001251 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001252 */
1253 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001254var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001255{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001256 typval_T tv;
1257
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001258 if (EVALCMD_BUSY)
1259 {
1260 redir_lval = NULL;
1261 return;
1262 }
1263
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001264 if (redir_lval != NULL)
1265 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001266 /* If there was no error: assign the text to the variable. */
1267 if (redir_endp != NULL)
1268 {
1269 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1270 tv.v_type = VAR_STRING;
1271 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001272 /* Call get_lval() again, if it's inside a Dict or List it may
1273 * have changed. */
1274 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001275 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001276 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1277 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1278 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001279 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001280
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001281 /* free the collected output */
1282 vim_free(redir_ga.ga_data);
1283 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001284
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001285 vim_free(redir_lval);
1286 redir_lval = NULL;
1287 }
1288 vim_free(redir_varname);
1289 redir_varname = NULL;
1290}
1291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292# if defined(FEAT_MBYTE) || defined(PROTO)
1293 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001294eval_charconvert(
1295 char_u *enc_from,
1296 char_u *enc_to,
1297 char_u *fname_from,
1298 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 int err = FALSE;
1301
1302 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1303 set_vim_var_string(VV_CC_TO, enc_to, -1);
1304 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1305 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1306 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1307 err = TRUE;
1308 set_vim_var_string(VV_CC_FROM, NULL, -1);
1309 set_vim_var_string(VV_CC_TO, NULL, -1);
1310 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1311 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1312
1313 if (err)
1314 return FAIL;
1315 return OK;
1316}
1317# endif
1318
1319# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001321eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 int err = FALSE;
1324
1325 set_vim_var_string(VV_FNAME_IN, fname, -1);
1326 set_vim_var_string(VV_CMDARG, args, -1);
1327 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1328 err = TRUE;
1329 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1330 set_vim_var_string(VV_CMDARG, NULL, -1);
1331
1332 if (err)
1333 {
1334 mch_remove(fname);
1335 return FAIL;
1336 }
1337 return OK;
1338}
1339# endif
1340
1341# if defined(FEAT_DIFF) || defined(PROTO)
1342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001343eval_diff(
1344 char_u *origfile,
1345 char_u *newfile,
1346 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
1348 int err = FALSE;
1349
1350 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1351 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1352 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1353 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1354 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1355 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1356 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1357}
1358
1359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001360eval_patch(
1361 char_u *origfile,
1362 char_u *difffile,
1363 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 int err;
1366
1367 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1368 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1369 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1370 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1371 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1372 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1373 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1374}
1375# endif
1376
1377/*
1378 * Top level evaluation function, returning a boolean.
1379 * Sets "error" to TRUE if there was an error.
1380 * Return TRUE or FALSE.
1381 */
1382 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001383eval_to_bool(
1384 char_u *arg,
1385 int *error,
1386 char_u **nextcmd,
1387 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
Bram Moolenaar33570922005-01-25 22:26:29 +00001389 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001390 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 if (skip)
1393 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 else
1397 {
1398 *error = FALSE;
1399 if (!skip)
1400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001401 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404 }
1405 if (skip)
1406 --emsg_skip;
1407
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001408 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409}
1410
1411/*
1412 * Top level evaluation function, returning a string. If "skip" is TRUE,
1413 * only parsing to "nextcmd" is done, without reporting errors. Return
1414 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1415 */
1416 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001417eval_to_string_skip(
1418 char_u *arg,
1419 char_u **nextcmd,
1420 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 char_u *retval;
1424
1425 if (skip)
1426 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 retval = NULL;
1429 else
1430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 retval = vim_strsave(get_tv_string(&tv));
1432 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
1434 if (skip)
1435 --emsg_skip;
1436
1437 return retval;
1438}
1439
1440/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001441 * Skip over an expression at "*pp".
1442 * Return FAIL for an error, OK otherwise.
1443 */
1444 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001445skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001446{
Bram Moolenaar33570922005-01-25 22:26:29 +00001447 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001448
1449 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001450 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001451}
1452
1453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001455 * When "convert" is TRUE convert a List into a sequence of lines and convert
1456 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 * Return pointer to allocated memory, or NULL for failure.
1458 */
1459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001460eval_to_string(
1461 char_u *arg,
1462 char_u **nextcmd,
1463 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaar33570922005-01-25 22:26:29 +00001465 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001467 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001468#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001469 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 retval = NULL;
1474 else
1475 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001476 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001477 {
1478 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001479 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001480 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001481 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001482 if (tv.vval.v_list->lv_len > 0)
1483 ga_append(&ga, NL);
1484 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001485 ga_append(&ga, NUL);
1486 retval = (char_u *)ga.ga_data;
1487 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001488#ifdef FEAT_FLOAT
1489 else if (convert && tv.v_type == VAR_FLOAT)
1490 {
1491 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1492 retval = vim_strsave(numbuf);
1493 }
1494#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001495 else
1496 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499
1500 return retval;
1501}
1502
1503/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001504 * Call eval_to_string() without using current local variables and using
1505 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 */
1507 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508eval_to_string_safe(
1509 char_u *arg,
1510 char_u **nextcmd,
1511 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 char_u *retval;
1514 void *save_funccalp;
1515
1516 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001517 if (use_sandbox)
1518 ++sandbox;
1519 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001520 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001521 if (use_sandbox)
1522 --sandbox;
1523 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 restore_funccal(save_funccalp);
1525 return retval;
1526}
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528/*
1529 * Top level evaluation function, returning a number.
1530 * Evaluates "expr" silently.
1531 * Returns -1 for an error.
1532 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001533 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001534eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
Bram Moolenaar33570922005-01-25 22:26:29 +00001536 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001537 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001538 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
1540 ++emsg_off;
1541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001542 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 retval = -1;
1544 else
1545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001546 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 }
1549 --emsg_off;
1550
1551 return retval;
1552}
1553
Bram Moolenaara40058a2005-07-11 22:42:07 +00001554/*
1555 * Prepare v: variable "idx" to be used.
1556 * Save the current typeval in "save_tv".
1557 * When not used yet add the variable to the v: hashtable.
1558 */
1559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001560prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001561{
1562 *save_tv = vimvars[idx].vv_tv;
1563 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1564 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1565}
1566
1567/*
1568 * Restore v: variable "idx" to typeval "save_tv".
1569 * When no longer defined, remove the variable from the v: hashtable.
1570 */
1571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001573{
1574 hashitem_T *hi;
1575
Bram Moolenaara40058a2005-07-11 22:42:07 +00001576 vimvars[idx].vv_tv = *save_tv;
1577 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1578 {
1579 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1580 if (HASHITEM_EMPTY(hi))
1581 EMSG2(_(e_intern2), "restore_vimvar()");
1582 else
1583 hash_remove(&vimvarht, hi);
1584 }
1585}
1586
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001587#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001588/*
1589 * Evaluate an expression to a list with suggestions.
1590 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001591 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001592 */
1593 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001595{
1596 typval_T save_val;
1597 typval_T rettv;
1598 list_T *list = NULL;
1599 char_u *p = skipwhite(expr);
1600
1601 /* Set "v:val" to the bad word. */
1602 prepare_vimvar(VV_VAL, &save_val);
1603 vimvars[VV_VAL].vv_type = VAR_STRING;
1604 vimvars[VV_VAL].vv_str = badword;
1605 if (p_verbose == 0)
1606 ++emsg_off;
1607
1608 if (eval1(&p, &rettv, TRUE) == OK)
1609 {
1610 if (rettv.v_type != VAR_LIST)
1611 clear_tv(&rettv);
1612 else
1613 list = rettv.vval.v_list;
1614 }
1615
1616 if (p_verbose == 0)
1617 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001618 restore_vimvar(VV_VAL, &save_val);
1619
1620 return list;
1621}
1622
1623/*
1624 * "list" is supposed to contain two items: a word and a number. Return the
1625 * word in "pp" and the number as the return value.
1626 * Return -1 if anything isn't right.
1627 * Used to get the good word and score from the eval_spell_expr() result.
1628 */
1629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001630get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001631{
1632 listitem_T *li;
1633
1634 li = list->lv_first;
1635 if (li == NULL)
1636 return -1;
1637 *pp = get_tv_string(&li->li_tv);
1638
1639 li = li->li_next;
1640 if (li == NULL)
1641 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001642 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001643}
1644#endif
1645
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001646/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001647 * Top level evaluation function.
1648 * Returns an allocated typval_T with the result.
1649 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001650 */
1651 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001652eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001653{
1654 typval_T *tv;
1655
1656 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001657 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001658 {
1659 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001660 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001661 }
1662
1663 return tv;
1664}
1665
1666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001669 * Uses argv[argc] for the function arguments. Only Number and String
1670 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001674call_vim_function(
1675 char_u *func,
1676 int argc,
1677 char_u **argv,
1678 int safe, /* use the sandbox */
1679 int str_arg_only, /* all arguments are strings */
1680 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
Bram Moolenaar33570922005-01-25 22:26:29 +00001682 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001683 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 int len;
1685 int i;
1686 int doesrange;
1687 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001690 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
1694 for (i = 0; i < argc; i++)
1695 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001696 /* Pass a NULL or empty argument as an empty string */
1697 if (argv[i] == NULL || *argv[i] == NUL)
1698 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001699 argvars[i].v_type = VAR_STRING;
1700 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001701 continue;
1702 }
1703
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001704 if (str_arg_only)
1705 len = 0;
1706 else
1707 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001708 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 if (len != 0 && len == (int)STRLEN(argv[i]))
1710 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001711 argvars[i].v_type = VAR_NUMBER;
1712 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 else
1715 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001716 argvars[i].v_type = VAR_STRING;
1717 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719 }
1720
1721 if (safe)
1722 {
1723 save_funccalp = save_funccal();
1724 ++sandbox;
1725 }
1726
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1728 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001730 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (safe)
1732 {
1733 --sandbox;
1734 restore_funccal(save_funccalp);
1735 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 vim_free(argvars);
1737
1738 if (ret == FAIL)
1739 clear_tv(rettv);
1740
1741 return ret;
1742}
1743
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001744/*
1745 * Call vimL function "func" and return the result as a number.
1746 * Returns -1 when calling the function fails.
1747 * Uses argv[argc] for the function arguments.
1748 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001749 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750call_func_retnr(
1751 char_u *func,
1752 int argc,
1753 char_u **argv,
1754 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001755{
1756 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001757 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001758
1759 /* All arguments are passed as strings, no conversion to number. */
1760 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1761 return -1;
1762
1763 retval = get_tv_number_chk(&rettv, NULL);
1764 clear_tv(&rettv);
1765 return retval;
1766}
1767
1768#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1769 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1770
Bram Moolenaar4f688582007-07-24 12:34:30 +00001771# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001773 * Call vimL function "func" and return the result as a string.
1774 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001775 * Uses argv[argc] for the function arguments.
1776 */
1777 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778call_func_retstr(
1779 char_u *func,
1780 int argc,
1781 char_u **argv,
1782 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783{
1784 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001785 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001786
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001787 /* All arguments are passed as strings, no conversion to number. */
1788 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001789 return NULL;
1790
1791 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001792 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 return retval;
1794}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001795# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001796
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001797/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001798 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001799 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001800 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001801 */
1802 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803call_func_retlist(
1804 char_u *func,
1805 int argc,
1806 char_u **argv,
1807 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001808{
1809 typval_T rettv;
1810
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001811 /* All arguments are passed as strings, no conversion to number. */
1812 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001813 return NULL;
1814
1815 if (rettv.v_type != VAR_LIST)
1816 {
1817 clear_tv(&rettv);
1818 return NULL;
1819 }
1820
1821 return rettv.vval.v_list;
1822}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823#endif
1824
1825/*
1826 * Save the current function call pointer, and set it to NULL.
1827 * Used when executing autocommands and for ":source".
1828 */
1829 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001830save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001832 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 current_funccal = NULL;
1835 return (void *)fc;
1836}
1837
1838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001839restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001841 funccall_T *fc = (funccall_T *)vfc;
1842
1843 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844}
1845
Bram Moolenaar05159a02005-02-26 23:04:13 +00001846#if defined(FEAT_PROFILE) || defined(PROTO)
1847/*
1848 * Prepare profiling for entering a child or something else that is not
1849 * counted for the script/function itself.
1850 * Should always be called in pair with prof_child_exit().
1851 */
1852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001853prof_child_enter(
1854 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001855{
1856 funccall_T *fc = current_funccal;
1857
1858 if (fc != NULL && fc->func->uf_profiling)
1859 profile_start(&fc->prof_child);
1860 script_prof_save(tm);
1861}
1862
1863/*
1864 * Take care of time spent in a child.
1865 * Should always be called after prof_child_enter().
1866 */
1867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001868prof_child_exit(
1869 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001870{
1871 funccall_T *fc = current_funccal;
1872
1873 if (fc != NULL && fc->func->uf_profiling)
1874 {
1875 profile_end(&fc->prof_child);
1876 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1877 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1878 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1879 }
1880 script_prof_restore(tm);
1881}
1882#endif
1883
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885#ifdef FEAT_FOLDING
1886/*
1887 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1888 * it in "*cp". Doesn't give error messages.
1889 */
1890 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001891eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
Bram Moolenaar33570922005-01-25 22:26:29 +00001893 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001894 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001896 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1897 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
1899 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001900 if (use_sandbox)
1901 ++sandbox;
1902 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 retval = 0;
1906 else
1907 {
1908 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 if (tv.v_type == VAR_NUMBER)
1910 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001911 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 retval = 0;
1913 else
1914 {
1915 /* If the result is a string, check if there is a non-digit before
1916 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (!VIM_ISDIGIT(*s) && *s != '-')
1919 *cp = *s++;
1920 retval = atol((char *)s);
1921 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 }
1924 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001925 if (use_sandbox)
1926 --sandbox;
1927 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001929 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930}
1931#endif
1932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 * ":let" list all variable values
1935 * ":let var1 var2" list variable values
1936 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 * ":let var += expr" assignment command.
1938 * ":let var -= expr" assignment command.
1939 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 */
1942 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001943ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944{
1945 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949 int var_count = 0;
1950 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001952 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001953 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
Bram Moolenaardb552d602006-03-23 22:59:57 +00001955 argend = skip_var_list(arg, &var_count, &semicolon);
1956 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001958 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1959 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001960 expr = skipwhite(argend);
1961 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1962 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001964 /*
1965 * ":let" without "=": list variables
1966 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 if (*arg == '[')
1968 EMSG(_(e_invarg));
1969 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001971 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001975 list_glob_vars(&first);
1976 list_buf_vars(&first);
1977 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001978#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001979 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001980#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001981 list_script_vars(&first);
1982 list_func_vars(&first);
1983 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 eap->nextcmd = check_nextcmd(arg);
1986 }
1987 else
1988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 op[0] = '=';
1990 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001991 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001993 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1994 op[0] = *expr; /* +=, -= or .= */
1995 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001997 else
1998 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (eap->skip)
2001 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (eap->skip)
2004 {
2005 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 --emsg_skip;
2008 }
2009 else if (i != FAIL)
2010 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002012 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 }
2016}
2017
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018/*
2019 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2020 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 * When "nextchars" is not NULL it points to a string with characters that
2022 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2023 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 * Returns OK or FAIL;
2025 */
2026 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002027ex_let_vars(
2028 char_u *arg_start,
2029 typval_T *tv,
2030 int copy, /* copy values from "tv", don't move */
2031 int semicolon, /* from skip_var_list() */
2032 int var_count, /* from skip_var_list() */
2033 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034{
2035 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002036 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002038 listitem_T *item;
2039 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040
2041 if (*arg != '[')
2042 {
2043 /*
2044 * ":let var = expr" or ":for var in list"
2045 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 return FAIL;
2048 return OK;
2049 }
2050
2051 /*
2052 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2053 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002054 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002055 {
2056 EMSG(_(e_listreq));
2057 return FAIL;
2058 }
2059
2060 i = list_len(l);
2061 if (semicolon == 0 && var_count < i)
2062 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002063 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 return FAIL;
2065 }
2066 if (var_count - semicolon > i)
2067 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002068 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069 return FAIL;
2070 }
2071
2072 item = l->lv_first;
2073 while (*arg != ']')
2074 {
2075 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002076 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 item = item->li_next;
2078 if (arg == NULL)
2079 return FAIL;
2080
2081 arg = skipwhite(arg);
2082 if (*arg == ';')
2083 {
2084 /* Put the rest of the list (may be empty) in the var after ';'.
2085 * Create a new list for this. */
2086 l = list_alloc();
2087 if (l == NULL)
2088 return FAIL;
2089 while (item != NULL)
2090 {
2091 list_append_tv(l, &item->li_tv);
2092 item = item->li_next;
2093 }
2094
2095 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002096 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002097 ltv.vval.v_list = l;
2098 l->lv_refcount = 1;
2099
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002100 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2101 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102 clear_tv(&ltv);
2103 if (arg == NULL)
2104 return FAIL;
2105 break;
2106 }
2107 else if (*arg != ',' && *arg != ']')
2108 {
2109 EMSG2(_(e_intern2), "ex_let_vars()");
2110 return FAIL;
2111 }
2112 }
2113
2114 return OK;
2115}
2116
2117/*
2118 * Skip over assignable variable "var" or list of variables "[var, var]".
2119 * Used for ":let varvar = expr" and ":for varvar in expr".
2120 * For "[var, var]" increment "*var_count" for each variable.
2121 * for "[var, var; var]" set "semicolon".
2122 * Return NULL for an error.
2123 */
2124 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002125skip_var_list(
2126 char_u *arg,
2127 int *var_count,
2128 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129{
2130 char_u *p, *s;
2131
2132 if (*arg == '[')
2133 {
2134 /* "[var, var]": find the matching ']'. */
2135 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002136 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002137 {
2138 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2139 s = skip_var_one(p);
2140 if (s == p)
2141 {
2142 EMSG2(_(e_invarg2), p);
2143 return NULL;
2144 }
2145 ++*var_count;
2146
2147 p = skipwhite(s);
2148 if (*p == ']')
2149 break;
2150 else if (*p == ';')
2151 {
2152 if (*semicolon == 1)
2153 {
2154 EMSG(_("Double ; in list of variables"));
2155 return NULL;
2156 }
2157 *semicolon = 1;
2158 }
2159 else if (*p != ',')
2160 {
2161 EMSG2(_(e_invarg2), p);
2162 return NULL;
2163 }
2164 }
2165 return p + 1;
2166 }
2167 else
2168 return skip_var_one(arg);
2169}
2170
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002171/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002172 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002173 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002175 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002177{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002178 if (*arg == '@' && arg[1] != NUL)
2179 return arg + 2;
2180 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2181 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002182}
2183
Bram Moolenaara7043832005-01-21 11:56:39 +00002184/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002185 * List variables for hashtab "ht" with prefix "prefix".
2186 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002187 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189list_hashtable_vars(
2190 hashtab_T *ht,
2191 char_u *prefix,
2192 int empty,
2193 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002194{
Bram Moolenaar33570922005-01-25 22:26:29 +00002195 hashitem_T *hi;
2196 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002197 int todo;
2198
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002199 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002200 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2201 {
2202 if (!HASHITEM_EMPTY(hi))
2203 {
2204 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002205 di = HI2DI(hi);
2206 if (empty || di->di_tv.v_type != VAR_STRING
2207 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002209 }
2210 }
2211}
2212
2213/*
2214 * List global variables.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002218{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002220}
2221
2222/*
2223 * List buffer variables.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002227{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 char_u numbuf[NUMBUFLEN];
2229
Bram Moolenaar429fa852013-04-15 12:27:36 +02002230 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232
2233 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002234 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2235 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002236}
2237
2238/*
2239 * List window variables.
2240 */
2241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002242list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002243{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002244 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002246}
2247
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002248#ifdef FEAT_WINDOWS
2249/*
2250 * List tab page variables.
2251 */
2252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002253list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002255 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002257}
2258#endif
2259
Bram Moolenaara7043832005-01-21 11:56:39 +00002260/*
2261 * List Vim variables.
2262 */
2263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002264list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002266 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267}
2268
2269/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002270 * List script-local variables, if there is a script.
2271 */
2272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002273list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002274{
2275 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002276 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2277 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002278}
2279
2280/*
2281 * List function variables, if there is a function.
2282 */
2283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002284list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002285{
2286 if (current_funccal != NULL)
2287 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002289}
2290
2291/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 * List variables in "arg".
2293 */
2294 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002295list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296{
2297 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 char_u *name_start;
2301 char_u *arg_subsc;
2302 char_u *tofree;
2303 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304
2305 while (!ends_excmd(*arg) && !got_int)
2306 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002309 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2311 {
2312 emsg_severe = TRUE;
2313 EMSG(_(e_trailing));
2314 break;
2315 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 /* get_name_len() takes care of expanding curly braces */
2320 name_start = name = arg;
2321 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2322 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002324 /* This is mainly to keep test 49 working: when expanding
2325 * curly braces fails overrule the exception error message. */
2326 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 emsg_severe = TRUE;
2329 EMSG2(_(e_invarg2), arg);
2330 break;
2331 }
2332 error = TRUE;
2333 }
2334 else
2335 {
2336 if (tofree != NULL)
2337 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002338 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 else
2341 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342 /* handle d.key, l[idx], f(expr) */
2343 arg_subsc = arg;
2344 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002345 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002348 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002349 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002350 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002351 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002352 case 'g': list_glob_vars(first); break;
2353 case 'b': list_buf_vars(first); break;
2354 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002355#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002356 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002357#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002358 case 'v': list_vim_vars(first); break;
2359 case 's': list_script_vars(first); break;
2360 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002361 default:
2362 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002363 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002364 }
2365 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002366 {
2367 char_u numbuf[NUMBUFLEN];
2368 char_u *tf;
2369 int c;
2370 char_u *s;
2371
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002372 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002373 c = *arg;
2374 *arg = NUL;
2375 list_one_var_a((char_u *)"",
2376 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002377 tv.v_type,
2378 s == NULL ? (char_u *)"" : s,
2379 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002380 *arg = c;
2381 vim_free(tf);
2382 }
2383 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 }
2386 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002387
2388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002390
2391 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 }
2393
2394 return arg;
2395}
2396
2397/*
2398 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2399 * Returns a pointer to the char just after the var name.
2400 * Returns NULL if there is an error.
2401 */
2402 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002403ex_let_one(
2404 char_u *arg, /* points to variable name */
2405 typval_T *tv, /* value to assign to variable */
2406 int copy, /* copy value from "tv" */
2407 char_u *endchars, /* valid chars after variable name or NULL */
2408 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409{
2410 int c1;
2411 char_u *name;
2412 char_u *p;
2413 char_u *arg_end = NULL;
2414 int len;
2415 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417
2418 /*
2419 * ":let $VAR = expr": Set environment variable.
2420 */
2421 if (*arg == '$')
2422 {
2423 /* Find the end of the name. */
2424 ++arg;
2425 name = arg;
2426 len = get_env_len(&arg);
2427 if (len == 0)
2428 EMSG2(_(e_invarg2), name - 1);
2429 else
2430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 if (op != NULL && (*op == '+' || *op == '-'))
2432 EMSG2(_(e_letwrong), op);
2433 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002434 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002436 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 {
2438 c1 = name[len];
2439 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 p = get_tv_string_chk(tv);
2441 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 {
2443 int mustfree = FALSE;
2444 char_u *s = vim_getenv(name, &mustfree);
2445
2446 if (s != NULL)
2447 {
2448 p = tofree = concat_str(s, p);
2449 if (mustfree)
2450 vim_free(s);
2451 }
2452 }
2453 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 if (STRICMP(name, "HOME") == 0)
2457 init_homedir();
2458 else if (didset_vim && STRICMP(name, "VIM") == 0)
2459 didset_vim = FALSE;
2460 else if (didset_vimruntime
2461 && STRICMP(name, "VIMRUNTIME") == 0)
2462 didset_vimruntime = FALSE;
2463 arg_end = arg;
2464 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 }
2468 }
2469 }
2470
2471 /*
2472 * ":let &option = expr": Set option value.
2473 * ":let &l:option = expr": Set local option value.
2474 * ":let &g:option = expr": Set global option value.
2475 */
2476 else if (*arg == '&')
2477 {
2478 /* Find the end of the name. */
2479 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480 if (p == NULL || (endchars != NULL
2481 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 EMSG(_(e_letunexp));
2483 else
2484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 long n;
2486 int opt_type;
2487 long numval;
2488 char_u *stringval = NULL;
2489 char_u *s;
2490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 c1 = *p;
2492 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002494 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 s = get_tv_string_chk(tv); /* != NULL if number or string */
2496 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 {
2498 opt_type = get_option_value(arg, &numval,
2499 &stringval, opt_flags);
2500 if ((opt_type == 1 && *op == '.')
2501 || (opt_type == 0 && *op != '.'))
2502 EMSG2(_(e_letwrong), op);
2503 else
2504 {
2505 if (opt_type == 1) /* number */
2506 {
2507 if (*op == '+')
2508 n = numval + n;
2509 else
2510 n = numval - n;
2511 }
2512 else if (opt_type == 0 && stringval != NULL) /* string */
2513 {
2514 s = concat_str(stringval, s);
2515 vim_free(stringval);
2516 stringval = s;
2517 }
2518 }
2519 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002520 if (s != NULL)
2521 {
2522 set_option_value(arg, n, s, opt_flags);
2523 arg_end = p;
2524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002526 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 }
2528 }
2529
2530 /*
2531 * ":let @r = expr": Set register contents.
2532 */
2533 else if (*arg == '@')
2534 {
2535 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536 if (op != NULL && (*op == '+' || *op == '-'))
2537 EMSG2(_(e_letwrong), op);
2538 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002539 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 EMSG(_(e_letunexp));
2541 else
2542 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002543 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544 char_u *s;
2545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002546 p = get_tv_string_chk(tv);
2547 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002549 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002550 if (s != NULL)
2551 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002552 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002553 vim_free(s);
2554 }
2555 }
2556 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002557 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002558 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002559 arg_end = arg + 1;
2560 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002561 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 }
2563 }
2564
2565 /*
2566 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002569 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002573 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002575 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2577 EMSG(_(e_letunexp));
2578 else
2579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002580 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 arg_end = p;
2582 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 }
2586
2587 else
2588 EMSG2(_(e_invarg2), arg);
2589
2590 return arg_end;
2591}
2592
2593/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2595 */
2596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002597check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598{
2599 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2600 {
2601 EMSG2(_(e_readonlyvar), arg);
2602 return TRUE;
2603 }
2604 return FALSE;
2605}
2606
2607/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 * Get an lval: variable, Dict item or List item that can be assigned a value
2609 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2610 * "name.key", "name.key[expr]" etc.
2611 * Indexing only works if "name" is an existing List or Dictionary.
2612 * "name" points to the start of the name.
2613 * If "rettv" is not NULL it points to the value to be assigned.
2614 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2615 * wrong; must end in space or cmd separator.
2616 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 * flags:
2618 * GLV_QUIET: do not give error messages
2619 * GLV_NO_AUTOLOAD: do not use script autoloading
2620 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002622 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 */
2625 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002626get_lval(
2627 char_u *name,
2628 typval_T *rettv,
2629 lval_T *lp,
2630 int unlet,
2631 int skip,
2632 int flags, /* GLV_ values */
2633 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 char_u *expr_start, *expr_end;
2637 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002638 dictitem_T *v;
2639 typval_T var1;
2640 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002642 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002645 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002646 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002649 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650
2651 if (skip)
2652 {
2653 /* When skipping just find the end of the name. */
2654 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002655 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 }
2657
2658 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002659 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (expr_start != NULL)
2661 {
2662 /* Don't expand the name when we already know there is an error. */
2663 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2664 && *p != '[' && *p != '.')
2665 {
2666 EMSG(_(e_trailing));
2667 return NULL;
2668 }
2669
2670 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2671 if (lp->ll_exp_name == NULL)
2672 {
2673 /* Report an invalid expression in braces, unless the
2674 * expression evaluation has been cancelled due to an
2675 * aborting error, an interrupt, or an exception. */
2676 if (!aborting() && !quiet)
2677 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002678 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 EMSG2(_(e_invarg2), name);
2680 return NULL;
2681 }
2682 }
2683 lp->ll_name = lp->ll_exp_name;
2684 }
2685 else
2686 lp->ll_name = name;
2687
2688 /* Without [idx] or .key we are done. */
2689 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2690 return p;
2691
2692 cc = *p;
2693 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002694 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (v == NULL && !quiet)
2696 EMSG2(_(e_undefvar), lp->ll_name);
2697 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 if (v == NULL)
2699 return NULL;
2700
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 /*
2702 * Loop until no more [idx] or .key is following.
2703 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002704 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2708 && !(lp->ll_tv->v_type == VAR_DICT
2709 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (!quiet)
2712 EMSG(_("E689: Can only index a List or Dictionary"));
2713 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002714 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
2718 EMSG(_("E708: [:] must come last"));
2719 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002720 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002721
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 len = -1;
2723 if (*p == '.')
2724 {
2725 key = p + 1;
2726 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2727 ;
2728 if (len == 0)
2729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (!quiet)
2731 EMSG(_(e_emptykey));
2732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734 p = key + len;
2735 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 else
2737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (*p == ':')
2741 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002742 else
2743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 empty1 = FALSE;
2745 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002747 if (get_tv_string_chk(&var1) == NULL)
2748 {
2749 /* not a number or string */
2750 clear_tv(&var1);
2751 return NULL;
2752 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 }
2754
2755 /* Optionally get the second index [ :expr]. */
2756 if (*p == ':')
2757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002761 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002762 if (!empty1)
2763 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002765 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (rettv != NULL && (rettv->v_type != VAR_LIST
2767 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (!quiet)
2770 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (!empty1)
2772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775 p = skipwhite(p + 1);
2776 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 else
2779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2782 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 if (!empty1)
2784 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002787 if (get_tv_string_chk(&var2) == NULL)
2788 {
2789 /* not a number or string */
2790 if (!empty1)
2791 clear_tv(&var1);
2792 clear_tv(&var2);
2793 return NULL;
2794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002797 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002800
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002802 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 if (!quiet)
2804 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 if (!empty1)
2806 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811
2812 /* Skip to past ']'. */
2813 ++p;
2814 }
2815
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
2818 if (len == -1)
2819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002821 key = get_tv_string_chk(&var1); /* is number or string */
2822 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 }
2827 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002828 lp->ll_list = NULL;
2829 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002830 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002831
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002832 /* When assigning to a scope dictionary check that a function and
2833 * variable name is valid (only variable name unless it is l: or
2834 * g: dictionary). Disallow overwriting a builtin function. */
2835 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002837 int prevval;
2838 int wrong;
2839
2840 if (len != -1)
2841 {
2842 prevval = key[len];
2843 key[len] = NUL;
2844 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002845 else
2846 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002847 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2848 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002849 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002850 || !valid_varname(key);
2851 if (len != -1)
2852 key[len] = prevval;
2853 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002854 return NULL;
2855 }
2856
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002859 /* Can't add "v:" variable. */
2860 if (lp->ll_dict == &vimvardict)
2861 {
2862 EMSG2(_(e_illvar), name);
2863 return NULL;
2864 }
2865
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002866 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002870 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 if (len == -1)
2872 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 }
2875 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 if (len == -1)
2880 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 p = NULL;
2883 break;
2884 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002885 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002886 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002887 return NULL;
2888
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 if (len == -1)
2890 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 }
2893 else
2894 {
2895 /*
2896 * Get the number and item for the only or first index of the List.
2897 */
2898 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002900 else
2901 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002902 lp->ll_n1 = (long)get_tv_number(&var1);
2903 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002904 clear_tv(&var1);
2905 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 lp->ll_list = lp->ll_tv->vval.v_list;
2908 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2909 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002910 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002911 if (lp->ll_n1 < 0)
2912 {
2913 lp->ll_n1 = 0;
2914 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2915 }
2916 }
2917 if (lp->ll_li == NULL)
2918 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002921 if (!quiet)
2922 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002924 }
2925
2926 /*
2927 * May need to find the item or absolute index for the second
2928 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 * When no index given: "lp->ll_empty2" is TRUE.
2930 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002933 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002934 lp->ll_n2 = (long)get_tv_number(&var2);
2935 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002936 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002940 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002941 {
2942 if (!quiet)
2943 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002945 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002947 }
2948
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2950 if (lp->ll_n1 < 0)
2951 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2952 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002953 {
2954 if (!quiet)
2955 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002957 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002958 }
2959
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002961 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002962 }
2963
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 return p;
2965}
2966
2967/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002968 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 */
2970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002971clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972{
2973 vim_free(lp->ll_exp_name);
2974 vim_free(lp->ll_newkey);
2975}
2976
2977/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002978 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002980 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 */
2982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002983set_var_lval(
2984 lval_T *lp,
2985 char_u *endp,
2986 typval_T *rettv,
2987 int copy,
2988 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989{
2990 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002991 listitem_T *ri;
2992 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993
2994 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 cc = *endp;
2999 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 if (op != NULL && *op != '=')
3001 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003004 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003005 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003006 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003007 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003009 if ((di == NULL
3010 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
3011 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
3012 FALSE)))
3013 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003014 set_var(lp->ll_name, &tv, FALSE);
3015 clear_tv(&tv);
3016 }
3017 }
3018 else
3019 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003021 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003023 else if (tv_check_lock(lp->ll_newkey == NULL
3024 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003025 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003026 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 else if (lp->ll_range)
3028 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003029 listitem_T *ll_li = lp->ll_li;
3030 int ll_n1 = lp->ll_n1;
3031
3032 /*
3033 * Check whether any of the list items is locked
3034 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003035 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003036 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003037 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003038 return;
3039 ri = ri->li_next;
3040 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3041 break;
3042 ll_li = ll_li->li_next;
3043 ++ll_n1;
3044 }
3045
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 /*
3047 * Assign the List values to the list items.
3048 */
3049 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003050 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003051 if (op != NULL && *op != '=')
3052 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3053 else
3054 {
3055 clear_tv(&lp->ll_li->li_tv);
3056 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3057 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 ri = ri->li_next;
3059 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3060 break;
3061 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003062 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003064 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003065 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 ri = NULL;
3067 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003068 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003069 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003070 lp->ll_li = lp->ll_li->li_next;
3071 ++lp->ll_n1;
3072 }
3073 if (ri != NULL)
3074 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 else if (lp->ll_empty2
3076 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 : lp->ll_n1 != lp->ll_n2)
3078 EMSG(_("E711: List value has not enough items"));
3079 }
3080 else
3081 {
3082 /*
3083 * Assign to a List or Dictionary item.
3084 */
3085 if (lp->ll_newkey != NULL)
3086 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 if (op != NULL && *op != '=')
3088 {
3089 EMSG2(_(e_letwrong), op);
3090 return;
3091 }
3092
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003093 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003094 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003095 if (di == NULL)
3096 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003097 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3098 {
3099 vim_free(di);
3100 return;
3101 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003102 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003103 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003104 else if (op != NULL && *op != '=')
3105 {
3106 tv_op(lp->ll_tv, rettv, op);
3107 return;
3108 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003109 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003110 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003111
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003112 /*
3113 * Assign the value to the variable or list item.
3114 */
3115 if (copy)
3116 copy_tv(rettv, lp->ll_tv);
3117 else
3118 {
3119 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003120 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003121 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003122 }
3123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003124}
3125
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3128 * Returns OK or FAIL.
3129 */
3130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003131tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003133 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003134 char_u numbuf[NUMBUFLEN];
3135 char_u *s;
3136
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003137 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3138 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3139 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 {
3141 switch (tv1->v_type)
3142 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003143 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003144 case VAR_DICT:
3145 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003146 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003147 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003148 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003149 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003150 break;
3151
3152 case VAR_LIST:
3153 if (*op != '+' || tv2->v_type != VAR_LIST)
3154 break;
3155 /* List += List */
3156 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3157 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3158 return OK;
3159
3160 case VAR_NUMBER:
3161 case VAR_STRING:
3162 if (tv2->v_type == VAR_LIST)
3163 break;
3164 if (*op == '+' || *op == '-')
3165 {
3166 /* nr += nr or nr -= nr*/
3167 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168#ifdef FEAT_FLOAT
3169 if (tv2->v_type == VAR_FLOAT)
3170 {
3171 float_T f = n;
3172
3173 if (*op == '+')
3174 f += tv2->vval.v_float;
3175 else
3176 f -= tv2->vval.v_float;
3177 clear_tv(tv1);
3178 tv1->v_type = VAR_FLOAT;
3179 tv1->vval.v_float = f;
3180 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182#endif
3183 {
3184 if (*op == '+')
3185 n += get_tv_number(tv2);
3186 else
3187 n -= get_tv_number(tv2);
3188 clear_tv(tv1);
3189 tv1->v_type = VAR_NUMBER;
3190 tv1->vval.v_number = n;
3191 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003192 }
3193 else
3194 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003195 if (tv2->v_type == VAR_FLOAT)
3196 break;
3197
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003198 /* str .= str */
3199 s = get_tv_string(tv1);
3200 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3201 clear_tv(tv1);
3202 tv1->v_type = VAR_STRING;
3203 tv1->vval.v_string = s;
3204 }
3205 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003208#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003209 {
3210 float_T f;
3211
3212 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3213 && tv2->v_type != VAR_NUMBER
3214 && tv2->v_type != VAR_STRING))
3215 break;
3216 if (tv2->v_type == VAR_FLOAT)
3217 f = tv2->vval.v_float;
3218 else
3219 f = get_tv_number(tv2);
3220 if (*op == '+')
3221 tv1->vval.v_float += f;
3222 else
3223 tv1->vval.v_float -= f;
3224 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003225#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003226 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003227 }
3228 }
3229
3230 EMSG2(_(e_letwrong), op);
3231 return FAIL;
3232}
3233
3234/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 * Add a watcher to a list.
3236 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239{
3240 lw->lw_next = l->lv_watch;
3241 l->lv_watch = lw;
3242}
3243
3244/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003245 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 * No warning when it isn't found...
3247 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003249list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250{
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252
3253 lwp = &l->lv_watch;
3254 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3255 {
3256 if (lw == lwrem)
3257 {
3258 *lwp = lw->lw_next;
3259 break;
3260 }
3261 lwp = &lw->lw_next;
3262 }
3263}
3264
3265/*
3266 * Just before removing an item from a list: advance watchers to the next
3267 * item.
3268 */
3269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003270list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271{
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273
3274 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3275 if (lw->lw_item == item)
3276 lw->lw_item = item->li_next;
3277}
3278
3279/*
3280 * Evaluate the expression used in a ":for var in expr" command.
3281 * "arg" points to "var".
3282 * Set "*errp" to TRUE for an error, FALSE otherwise;
3283 * Return a pointer that holds the info. Null when there is an error.
3284 */
3285 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003286eval_for_line(
3287 char_u *arg,
3288 int *errp,
3289 char_u **nextcmdp,
3290 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291{
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003294 typval_T tv;
3295 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296
3297 *errp = TRUE; /* default: there is an error */
3298
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 if (fi == NULL)
3301 return NULL;
3302
3303 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3304 if (expr == NULL)
3305 return fi;
3306
3307 expr = skipwhite(expr);
3308 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3309 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003310 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 return fi;
3312 }
3313
3314 if (skip)
3315 ++emsg_skip;
3316 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3317 {
3318 *errp = FALSE;
3319 if (!skip)
3320 {
3321 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003322 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 clear_tv(&tv);
3326 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003327 else if (l == NULL)
3328 {
3329 /* a null list is like an empty list: do nothing */
3330 clear_tv(&tv);
3331 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 else
3333 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003334 /* No need to increment the refcount, it's already set for the
3335 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 fi->fi_list = l;
3337 list_add_watch(l, &fi->fi_lw);
3338 fi->fi_lw.lw_item = l->lv_first;
3339 }
3340 }
3341 }
3342 if (skip)
3343 --emsg_skip;
3344
3345 return fi;
3346}
3347
3348/*
3349 * Use the first item in a ":for" list. Advance to the next.
3350 * Assign the values to the variable (list). "arg" points to the first one.
3351 * Return TRUE when a valid item was found, FALSE when at end of list or
3352 * something wrong.
3353 */
3354 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003355next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003357 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003359 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360
3361 item = fi->fi_lw.lw_item;
3362 if (item == NULL)
3363 result = FALSE;
3364 else
3365 {
3366 fi->fi_lw.lw_item = item->li_next;
3367 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3368 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3369 }
3370 return result;
3371}
3372
3373/*
3374 * Free the structure used to store info used by ":for".
3375 */
3376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003377free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003378{
Bram Moolenaar33570922005-01-25 22:26:29 +00003379 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003380
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003381 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003382 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003384 list_unref(fi->fi_list);
3385 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003386 vim_free(fi);
3387}
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3390
3391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003392set_context_for_expression(
3393 expand_T *xp,
3394 char_u *arg,
3395 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
3397 int got_eq = FALSE;
3398 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003399 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003401 if (cmdidx == CMD_let)
3402 {
3403 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003404 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003405 {
3406 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003407 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003408 {
3409 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003411 if (vim_iswhite(*p))
3412 break;
3413 }
3414 return;
3415 }
3416 }
3417 else
3418 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3419 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 while ((xp->xp_pattern = vim_strpbrk(arg,
3421 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3422 {
3423 c = *xp->xp_pattern;
3424 if (c == '&')
3425 {
3426 c = xp->xp_pattern[1];
3427 if (c == '&')
3428 {
3429 ++xp->xp_pattern;
3430 xp->xp_context = cmdidx != CMD_let || got_eq
3431 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3432 }
3433 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003436 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3437 xp->xp_pattern += 2;
3438
3439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441 else if (c == '$')
3442 {
3443 /* environment variable */
3444 xp->xp_context = EXPAND_ENV_VARS;
3445 }
3446 else if (c == '=')
3447 {
3448 got_eq = TRUE;
3449 xp->xp_context = EXPAND_EXPRESSION;
3450 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003451 else if (c == '#'
3452 && xp->xp_context == EXPAND_EXPRESSION)
3453 {
3454 /* Autoload function/variable contains '#'. */
3455 break;
3456 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003457 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 && xp->xp_context == EXPAND_FUNCTIONS
3459 && vim_strchr(xp->xp_pattern, '(') == NULL)
3460 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003461 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 break;
3463 }
3464 else if (cmdidx != CMD_let || got_eq)
3465 {
3466 if (c == '"') /* string */
3467 {
3468 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3469 if (c == '\\' && xp->xp_pattern[1] != NUL)
3470 ++xp->xp_pattern;
3471 xp->xp_context = EXPAND_NOTHING;
3472 }
3473 else if (c == '\'') /* literal string */
3474 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003475 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3477 /* skip */ ;
3478 xp->xp_context = EXPAND_NOTHING;
3479 }
3480 else if (c == '|')
3481 {
3482 if (xp->xp_pattern[1] == '|')
3483 {
3484 ++xp->xp_pattern;
3485 xp->xp_context = EXPAND_EXPRESSION;
3486 }
3487 else
3488 xp->xp_context = EXPAND_COMMANDS;
3489 }
3490 else
3491 xp->xp_context = EXPAND_EXPRESSION;
3492 }
3493 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003494 /* Doesn't look like something valid, expand as an expression
3495 * anyway. */
3496 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 arg = xp->xp_pattern;
3498 if (*arg != NUL)
3499 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3500 /* skip */ ;
3501 }
3502 xp->xp_pattern = arg;
3503}
3504
3505#endif /* FEAT_CMDL_COMPL */
3506
3507/*
3508 * ":1,25call func(arg1, arg2)" function call.
3509 */
3510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003511ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
3513 char_u *arg = eap->arg;
3514 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003516 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003518 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 linenr_T lnum;
3520 int doesrange;
3521 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003522 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003523 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003525 if (eap->skip)
3526 {
3527 /* trans_function_name() doesn't work well when skipping, use eval0()
3528 * instead to skip to any following command, e.g. for:
3529 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003530 ++emsg_skip;
3531 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3532 clear_tv(&rettv);
3533 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003534 return;
3535 }
3536
Bram Moolenaar65639032016-03-16 21:40:30 +01003537 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003538 if (fudi.fd_newkey != NULL)
3539 {
3540 /* Still need to give an error message for missing key. */
3541 EMSG2(_(e_dictkey), fudi.fd_newkey);
3542 vim_free(fudi.fd_newkey);
3543 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003544 if (tofree == NULL)
3545 return;
3546
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003547 /* Increase refcount on dictionary, it could get deleted when evaluating
3548 * the arguments. */
3549 if (fudi.fd_dict != NULL)
3550 ++fudi.fd_dict->dv_refcount;
3551
Bram Moolenaar65639032016-03-16 21:40:30 +01003552 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3553 * contents. For VAR_PARTIAL get its partial, unless we already have one
3554 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003555 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003556 name = deref_func_name(tofree, &len,
3557 partial != NULL ? NULL : &partial, FALSE);
3558
Bram Moolenaar532c7802005-01-27 14:44:31 +00003559 /* Skip white space to allow ":call func ()". Not good, but required for
3560 * backward compatibility. */
3561 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003562 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 if (*startarg != '(')
3565 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003566 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 goto end;
3568 }
3569
3570 /*
3571 * When skipping, evaluate the function once, to find the end of the
3572 * arguments.
3573 * When the function takes a range, this is discovered after the first
3574 * call, and the loop is broken.
3575 */
3576 if (eap->skip)
3577 {
3578 ++emsg_skip;
3579 lnum = eap->line2; /* do it once, also with an invalid range */
3580 }
3581 else
3582 lnum = eap->line1;
3583 for ( ; lnum <= eap->line2; ++lnum)
3584 {
3585 if (!eap->skip && eap->addr_count > 0)
3586 {
3587 curwin->w_cursor.lnum = lnum;
3588 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003589#ifdef FEAT_VIRTUALEDIT
3590 curwin->w_cursor.coladd = 0;
3591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
3593 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003595 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003596 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598 failed = TRUE;
3599 break;
3600 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003601
3602 /* Handle a function returning a Funcref, Dictionary or List. */
3603 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3604 {
3605 failed = TRUE;
3606 break;
3607 }
3608
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003609 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 if (doesrange || eap->skip)
3611 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003614 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003615 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003616 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 if (aborting())
3618 break;
3619 }
3620 if (eap->skip)
3621 --emsg_skip;
3622
3623 if (!failed)
3624 {
3625 /* Check for trailing illegal characters and a following command. */
3626 if (!ends_excmd(*arg))
3627 {
3628 emsg_severe = TRUE;
3629 EMSG(_(e_trailing));
3630 }
3631 else
3632 eap->nextcmd = check_nextcmd(arg);
3633 }
3634
3635end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003636 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003637 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638}
3639
3640/*
3641 * ":unlet[!] var1 ... " command.
3642 */
3643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003644ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 ex_unletlock(eap, eap->arg, 0);
3647}
3648
3649/*
3650 * ":lockvar" and ":unlockvar" commands
3651 */
3652 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003653ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003654{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656 int deep = 2;
3657
3658 if (eap->forceit)
3659 deep = -1;
3660 else if (vim_isdigit(*arg))
3661 {
3662 deep = getdigits(&arg);
3663 arg = skipwhite(arg);
3664 }
3665
3666 ex_unletlock(eap, arg, deep);
3667}
3668
3669/*
3670 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3671 */
3672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003673ex_unletlock(
3674 exarg_T *eap,
3675 char_u *argstart,
3676 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677{
3678 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003681 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
3683 do
3684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003686 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003687 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 if (lv.ll_name == NULL)
3689 error = TRUE; /* error but continue parsing */
3690 if (name_end == NULL || (!vim_iswhite(*name_end)
3691 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 if (name_end != NULL)
3694 {
3695 emsg_severe = TRUE;
3696 EMSG(_(e_trailing));
3697 }
3698 if (!(eap->skip || error))
3699 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 break;
3701 }
3702
3703 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003704 {
3705 if (eap->cmdidx == CMD_unlet)
3706 {
3707 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3708 error = TRUE;
3709 }
3710 else
3711 {
3712 if (do_lock_var(&lv, name_end, deep,
3713 eap->cmdidx == CMD_lockvar) == FAIL)
3714 error = TRUE;
3715 }
3716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 if (!eap->skip)
3719 clear_lval(&lv);
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 arg = skipwhite(name_end);
3722 } while (!ends_excmd(*arg));
3723
3724 eap->nextcmd = check_nextcmd(arg);
3725}
3726
Bram Moolenaar8c711452005-01-14 21:53:12 +00003727 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003728do_unlet_var(
3729 lval_T *lp,
3730 char_u *name_end,
3731 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003733 int ret = OK;
3734 int cc;
3735
3736 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003738 cc = *name_end;
3739 *name_end = NUL;
3740
3741 /* Normal name or expanded name. */
3742 if (check_changedtick(lp->ll_name))
3743 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003744 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003745 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003746 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003747 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003748 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003749 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003750 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003751 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003752 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003753 else if (lp->ll_range)
3754 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003755 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003756 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003757 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003758
3759 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3760 {
3761 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003762 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003763 return FAIL;
3764 ll_li = li;
3765 ++ll_n1;
3766 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003767
3768 /* Delete a range of List items. */
3769 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3770 {
3771 li = lp->ll_li->li_next;
3772 listitem_remove(lp->ll_list, lp->ll_li);
3773 lp->ll_li = li;
3774 ++lp->ll_n1;
3775 }
3776 }
3777 else
3778 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003779 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003780 /* unlet a List item. */
3781 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003782 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003783 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003784 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003785 }
3786
3787 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003788}
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790/*
3791 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003792 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 */
3794 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003795do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
Bram Moolenaar33570922005-01-25 22:26:29 +00003797 hashtab_T *ht;
3798 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003799 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003800 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003801 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
Bram Moolenaar33570922005-01-25 22:26:29 +00003803 ht = find_var_ht(name, &varname);
3804 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003806 if (ht == &globvarht)
3807 d = &globvardict;
3808 else if (current_funccal != NULL
3809 && ht == &current_funccal->l_vars.dv_hashtab)
3810 d = &current_funccal->l_vars;
3811 else if (ht == &compat_hashtab)
3812 d = &vimvardict;
3813 else
3814 {
3815 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3816 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3817 }
3818 if (d == NULL)
3819 {
3820 EMSG2(_(e_intern2), "do_unlet()");
3821 return FAIL;
3822 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003823 hi = hash_find(ht, varname);
3824 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003825 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003826 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003827 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003828 || var_check_ro(di->di_flags, name, FALSE)
3829 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003830 return FAIL;
3831
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003832 delete_var(ht, hi);
3833 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836 if (forceit)
3837 return OK;
3838 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return FAIL;
3840}
3841
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003842/*
3843 * Lock or unlock variable indicated by "lp".
3844 * "deep" is the levels to go (-1 for unlimited);
3845 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3846 */
3847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003848do_lock_var(
3849 lval_T *lp,
3850 char_u *name_end,
3851 int deep,
3852 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003853{
3854 int ret = OK;
3855 int cc;
3856 dictitem_T *di;
3857
3858 if (deep == 0) /* nothing to do */
3859 return OK;
3860
3861 if (lp->ll_tv == NULL)
3862 {
3863 cc = *name_end;
3864 *name_end = NUL;
3865
3866 /* Normal name or expanded name. */
3867 if (check_changedtick(lp->ll_name))
3868 ret = FAIL;
3869 else
3870 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003871 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003872 if (di == NULL)
3873 ret = FAIL;
3874 else
3875 {
3876 if (lock)
3877 di->di_flags |= DI_FLAGS_LOCK;
3878 else
3879 di->di_flags &= ~DI_FLAGS_LOCK;
3880 item_lock(&di->di_tv, deep, lock);
3881 }
3882 }
3883 *name_end = cc;
3884 }
3885 else if (lp->ll_range)
3886 {
3887 listitem_T *li = lp->ll_li;
3888
3889 /* (un)lock a range of List items. */
3890 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3891 {
3892 item_lock(&li->li_tv, deep, lock);
3893 li = li->li_next;
3894 ++lp->ll_n1;
3895 }
3896 }
3897 else if (lp->ll_list != NULL)
3898 /* (un)lock a List item. */
3899 item_lock(&lp->ll_li->li_tv, deep, lock);
3900 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003901 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003902 item_lock(&lp->ll_di->di_tv, deep, lock);
3903
3904 return ret;
3905}
3906
3907/*
3908 * Lock or unlock an item. "deep" is nr of levels to go.
3909 */
3910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003911item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003912{
3913 static int recurse = 0;
3914 list_T *l;
3915 listitem_T *li;
3916 dict_T *d;
3917 hashitem_T *hi;
3918 int todo;
3919
3920 if (recurse >= DICT_MAXNEST)
3921 {
3922 EMSG(_("E743: variable nested too deep for (un)lock"));
3923 return;
3924 }
3925 if (deep == 0)
3926 return;
3927 ++recurse;
3928
3929 /* lock/unlock the item itself */
3930 if (lock)
3931 tv->v_lock |= VAR_LOCKED;
3932 else
3933 tv->v_lock &= ~VAR_LOCKED;
3934
3935 switch (tv->v_type)
3936 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003937 case VAR_UNKNOWN:
3938 case VAR_NUMBER:
3939 case VAR_STRING:
3940 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003941 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003942 case VAR_FLOAT:
3943 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003944 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003945 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003946 break;
3947
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003948 case VAR_LIST:
3949 if ((l = tv->vval.v_list) != NULL)
3950 {
3951 if (lock)
3952 l->lv_lock |= VAR_LOCKED;
3953 else
3954 l->lv_lock &= ~VAR_LOCKED;
3955 if (deep < 0 || deep > 1)
3956 /* recursive: lock/unlock the items the List contains */
3957 for (li = l->lv_first; li != NULL; li = li->li_next)
3958 item_lock(&li->li_tv, deep - 1, lock);
3959 }
3960 break;
3961 case VAR_DICT:
3962 if ((d = tv->vval.v_dict) != NULL)
3963 {
3964 if (lock)
3965 d->dv_lock |= VAR_LOCKED;
3966 else
3967 d->dv_lock &= ~VAR_LOCKED;
3968 if (deep < 0 || deep > 1)
3969 {
3970 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003971 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003972 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3973 {
3974 if (!HASHITEM_EMPTY(hi))
3975 {
3976 --todo;
3977 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3978 }
3979 }
3980 }
3981 }
3982 }
3983 --recurse;
3984}
3985
Bram Moolenaara40058a2005-07-11 22:42:07 +00003986/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003987 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3988 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003989 */
3990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003991tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003992{
3993 return (tv->v_lock & VAR_LOCKED)
3994 || (tv->v_type == VAR_LIST
3995 && tv->vval.v_list != NULL
3996 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3997 || (tv->v_type == VAR_DICT
3998 && tv->vval.v_dict != NULL
3999 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
4000}
4001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
4003/*
4004 * Delete all "menutrans_" variables.
4005 */
4006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004007del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004010 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaar33570922005-01-25 22:26:29 +00004012 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004013 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004014 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 {
4016 if (!HASHITEM_EMPTY(hi))
4017 {
4018 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004019 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4020 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 }
4022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024}
4025#endif
4026
4027#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4028
4029/*
4030 * Local string buffer for the next two functions to store a variable name
4031 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4032 * get_user_var_name().
4033 */
4034
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004035static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
4037static char_u *varnamebuf = NULL;
4038static int varnamebuflen = 0;
4039
4040/*
4041 * Function to concatenate a prefix and a variable name.
4042 */
4043 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004044cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
4046 int len;
4047
4048 len = (int)STRLEN(name) + 3;
4049 if (len > varnamebuflen)
4050 {
4051 vim_free(varnamebuf);
4052 len += 10; /* some additional space */
4053 varnamebuf = alloc(len);
4054 if (varnamebuf == NULL)
4055 {
4056 varnamebuflen = 0;
4057 return NULL;
4058 }
4059 varnamebuflen = len;
4060 }
4061 *varnamebuf = prefix;
4062 varnamebuf[1] = ':';
4063 STRCPY(varnamebuf + 2, name);
4064 return varnamebuf;
4065}
4066
4067/*
4068 * Function given to ExpandGeneric() to obtain the list of user defined
4069 * (global/buffer/window/built-in) variable names.
4070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004072get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004074 static long_u gdone;
4075 static long_u bdone;
4076 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004077#ifdef FEAT_WINDOWS
4078 static long_u tdone;
4079#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004080 static int vidx;
4081 static hashitem_T *hi;
4082 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004085 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004086 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004087#ifdef FEAT_WINDOWS
4088 tdone = 0;
4089#endif
4090 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004091
4092 /* Global variables */
4093 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004095 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004096 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004097 else
4098 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004099 while (HASHITEM_EMPTY(hi))
4100 ++hi;
4101 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4102 return cat_prefix_varname('g', hi->hi_key);
4103 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004105
4106 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004107 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004110 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004112 else
4113 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004114 while (HASHITEM_EMPTY(hi))
4115 ++hi;
4116 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004120 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 return (char_u *)"b:changedtick";
4122 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004123
4124 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004125 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004126 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004128 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004129 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004130 else
4131 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004132 while (HASHITEM_EMPTY(hi))
4133 ++hi;
4134 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004136
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004137#ifdef FEAT_WINDOWS
4138 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004139 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004140 if (tdone < ht->ht_used)
4141 {
4142 if (tdone++ == 0)
4143 hi = ht->ht_array;
4144 else
4145 ++hi;
4146 while (HASHITEM_EMPTY(hi))
4147 ++hi;
4148 return cat_prefix_varname('t', hi->hi_key);
4149 }
4150#endif
4151
Bram Moolenaar33570922005-01-25 22:26:29 +00004152 /* v: variables */
4153 if (vidx < VV_LEN)
4154 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 vim_free(varnamebuf);
4157 varnamebuf = NULL;
4158 varnamebuflen = 0;
4159 return NULL;
4160}
4161
4162#endif /* FEAT_CMDL_COMPL */
4163
4164/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004165 * Return TRUE if "pat" matches "text".
4166 * Does not use 'cpo' and always uses 'magic'.
4167 */
4168 static int
4169pattern_match(char_u *pat, char_u *text, int ic)
4170{
4171 int matches = FALSE;
4172 char_u *save_cpo;
4173 regmatch_T regmatch;
4174
4175 /* avoid 'l' flag in 'cpoptions' */
4176 save_cpo = p_cpo;
4177 p_cpo = (char_u *)"";
4178 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4179 if (regmatch.regprog != NULL)
4180 {
4181 regmatch.rm_ic = ic;
4182 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4183 vim_regfree(regmatch.regprog);
4184 }
4185 p_cpo = save_cpo;
4186 return matches;
4187}
4188
4189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * types for expressions.
4191 */
4192typedef enum
4193{
4194 TYPE_UNKNOWN = 0
4195 , TYPE_EQUAL /* == */
4196 , TYPE_NEQUAL /* != */
4197 , TYPE_GREATER /* > */
4198 , TYPE_GEQUAL /* >= */
4199 , TYPE_SMALLER /* < */
4200 , TYPE_SEQUAL /* <= */
4201 , TYPE_MATCH /* =~ */
4202 , TYPE_NOMATCH /* !~ */
4203} exptype_T;
4204
4205/*
4206 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4209 */
4210
4211/*
4212 * Handle zero level expression.
4213 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004215 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 * Return OK or FAIL.
4217 */
4218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004219eval0(
4220 char_u *arg,
4221 typval_T *rettv,
4222 char_u **nextcmd,
4223 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
4225 int ret;
4226 char_u *p;
4227
4228 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (ret == FAIL || !ends_excmd(*p))
4231 {
4232 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 /*
4235 * Report the invalid expression unless the expression evaluation has
4236 * been cancelled due to an aborting error, an interrupt, or an
4237 * exception.
4238 */
4239 if (!aborting())
4240 EMSG2(_(e_invexpr2), arg);
4241 ret = FAIL;
4242 }
4243 if (nextcmd != NULL)
4244 *nextcmd = check_nextcmd(p);
4245
4246 return ret;
4247}
4248
4249/*
4250 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004251 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 *
4253 * "arg" must point to the first non-white of the expression.
4254 * "arg" is advanced to the next non-white after the recognized expression.
4255 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004256 * Note: "rettv.v_lock" is not set.
4257 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 * Return OK or FAIL.
4259 */
4260 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004261eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262{
4263 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004264 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
4266 /*
4267 * Get the first variable.
4268 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 return FAIL;
4271
4272 if ((*arg)[0] == '?')
4273 {
4274 result = FALSE;
4275 if (evaluate)
4276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 int error = FALSE;
4278
4279 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 if (error)
4283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285
4286 /*
4287 * Get the second variable.
4288 */
4289 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 return FAIL;
4292
4293 /*
4294 * Check for the ":".
4295 */
4296 if ((*arg)[0] != ':')
4297 {
4298 EMSG(_("E109: Missing ':' after '?'"));
4299 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 return FAIL;
4302 }
4303
4304 /*
4305 * Get the third variable.
4306 */
4307 *arg = skipwhite(*arg + 1);
4308 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4309 {
4310 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313 }
4314 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317
4318 return OK;
4319}
4320
4321/*
4322 * Handle first level expression:
4323 * expr2 || expr2 || expr2 logical OR
4324 *
4325 * "arg" must point to the first non-white of the expression.
4326 * "arg" is advanced to the next non-white after the recognized expression.
4327 *
4328 * Return OK or FAIL.
4329 */
4330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004331eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332{
Bram Moolenaar33570922005-01-25 22:26:29 +00004333 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 long result;
4335 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 /*
4339 * Get the first variable.
4340 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 return FAIL;
4343
4344 /*
4345 * Repeat until there is no following "||".
4346 */
4347 first = TRUE;
4348 result = FALSE;
4349 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4350 {
4351 if (evaluate && first)
4352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004355 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356 if (error)
4357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 first = FALSE;
4359 }
4360
4361 /*
4362 * Get the second variable.
4363 */
4364 *arg = skipwhite(*arg + 2);
4365 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4366 return FAIL;
4367
4368 /*
4369 * Compute the result.
4370 */
4371 if (evaluate && !result)
4372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004373 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004376 if (error)
4377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 if (evaluate)
4380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004381 rettv->v_type = VAR_NUMBER;
4382 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 }
4385
4386 return OK;
4387}
4388
4389/*
4390 * Handle second level expression:
4391 * expr3 && expr3 && expr3 logical AND
4392 *
4393 * "arg" must point to the first non-white of the expression.
4394 * "arg" is advanced to the next non-white after the recognized expression.
4395 *
4396 * Return OK or FAIL.
4397 */
4398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004399eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400{
Bram Moolenaar33570922005-01-25 22:26:29 +00004401 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 long result;
4403 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 /*
4407 * Get the first variable.
4408 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 return FAIL;
4411
4412 /*
4413 * Repeat until there is no following "&&".
4414 */
4415 first = TRUE;
4416 result = TRUE;
4417 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4418 {
4419 if (evaluate && first)
4420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004423 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004424 if (error)
4425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 first = FALSE;
4427 }
4428
4429 /*
4430 * Get the second variable.
4431 */
4432 *arg = skipwhite(*arg + 2);
4433 if (eval4(arg, &var2, evaluate && result) == FAIL)
4434 return FAIL;
4435
4436 /*
4437 * Compute the result.
4438 */
4439 if (evaluate && result)
4440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004444 if (error)
4445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447 if (evaluate)
4448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 rettv->v_type = VAR_NUMBER;
4450 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452 }
4453
4454 return OK;
4455}
4456
4457/*
4458 * Handle third level expression:
4459 * var1 == var2
4460 * var1 =~ var2
4461 * var1 != var2
4462 * var1 !~ var2
4463 * var1 > var2
4464 * var1 >= var2
4465 * var1 < var2
4466 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004467 * var1 is var2
4468 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 *
4470 * "arg" must point to the first non-white of the expression.
4471 * "arg" is advanced to the next non-white after the recognized expression.
4472 *
4473 * Return OK or FAIL.
4474 */
4475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004476eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477{
Bram Moolenaar33570922005-01-25 22:26:29 +00004478 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 char_u *p;
4480 int i;
4481 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004482 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004484 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 char_u *s1, *s2;
4486 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 /*
4490 * Get the first variable.
4491 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004492 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 return FAIL;
4494
4495 p = *arg;
4496 switch (p[0])
4497 {
4498 case '=': if (p[1] == '=')
4499 type = TYPE_EQUAL;
4500 else if (p[1] == '~')
4501 type = TYPE_MATCH;
4502 break;
4503 case '!': if (p[1] == '=')
4504 type = TYPE_NEQUAL;
4505 else if (p[1] == '~')
4506 type = TYPE_NOMATCH;
4507 break;
4508 case '>': if (p[1] != '=')
4509 {
4510 type = TYPE_GREATER;
4511 len = 1;
4512 }
4513 else
4514 type = TYPE_GEQUAL;
4515 break;
4516 case '<': if (p[1] != '=')
4517 {
4518 type = TYPE_SMALLER;
4519 len = 1;
4520 }
4521 else
4522 type = TYPE_SEQUAL;
4523 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004524 case 'i': if (p[1] == 's')
4525 {
4526 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4527 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004528 i = p[len];
4529 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004530 {
4531 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4532 type_is = TRUE;
4533 }
4534 }
4535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 }
4537
4538 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004539 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 */
4541 if (type != TYPE_UNKNOWN)
4542 {
4543 /* extra question mark appended: ignore case */
4544 if (p[len] == '?')
4545 {
4546 ic = TRUE;
4547 ++len;
4548 }
4549 /* extra '#' appended: match case */
4550 else if (p[len] == '#')
4551 {
4552 ic = FALSE;
4553 ++len;
4554 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004555 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 else
4557 ic = p_ic;
4558
4559 /*
4560 * Get the second variable.
4561 */
4562 *arg = skipwhite(p + len);
4563 if (eval5(arg, &var2, evaluate) == FAIL)
4564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 return FAIL;
4567 }
4568
4569 if (evaluate)
4570 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004571 if (type_is && rettv->v_type != var2.v_type)
4572 {
4573 /* For "is" a different type always means FALSE, for "notis"
4574 * it means TRUE. */
4575 n1 = (type == TYPE_NEQUAL);
4576 }
4577 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4578 {
4579 if (type_is)
4580 {
4581 n1 = (rettv->v_type == var2.v_type
4582 && rettv->vval.v_list == var2.vval.v_list);
4583 if (type == TYPE_NEQUAL)
4584 n1 = !n1;
4585 }
4586 else if (rettv->v_type != var2.v_type
4587 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4588 {
4589 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004590 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004591 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004592 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004593 clear_tv(rettv);
4594 clear_tv(&var2);
4595 return FAIL;
4596 }
4597 else
4598 {
4599 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004600 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4601 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004602 if (type == TYPE_NEQUAL)
4603 n1 = !n1;
4604 }
4605 }
4606
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004607 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4608 {
4609 if (type_is)
4610 {
4611 n1 = (rettv->v_type == var2.v_type
4612 && rettv->vval.v_dict == var2.vval.v_dict);
4613 if (type == TYPE_NEQUAL)
4614 n1 = !n1;
4615 }
4616 else if (rettv->v_type != var2.v_type
4617 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4618 {
4619 if (rettv->v_type != var2.v_type)
4620 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4621 else
4622 EMSG(_("E736: Invalid operation for Dictionary"));
4623 clear_tv(rettv);
4624 clear_tv(&var2);
4625 return FAIL;
4626 }
4627 else
4628 {
4629 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004630 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4631 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004632 if (type == TYPE_NEQUAL)
4633 n1 = !n1;
4634 }
4635 }
4636
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004637 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4638 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004639 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004640 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004641 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004642 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004643 clear_tv(rettv);
4644 clear_tv(&var2);
4645 return FAIL;
4646 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004647 if ((rettv->v_type == VAR_PARTIAL
4648 && rettv->vval.v_partial == NULL)
4649 || (var2.v_type == VAR_PARTIAL
4650 && var2.vval.v_partial == NULL))
4651 /* when a partial is NULL assume not equal */
4652 n1 = FALSE;
4653 else if (type_is)
4654 {
4655 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4656 /* strings are considered the same if their value is
4657 * the same */
4658 n1 = tv_equal(rettv, &var2, ic, FALSE);
4659 else if (rettv->v_type == VAR_PARTIAL
4660 && var2.v_type == VAR_PARTIAL)
4661 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4662 else
4663 n1 = FALSE;
4664 }
4665 else
4666 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004667 if (type == TYPE_NEQUAL)
4668 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004669 }
4670
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004671#ifdef FEAT_FLOAT
4672 /*
4673 * If one of the two variables is a float, compare as a float.
4674 * When using "=~" or "!~", always compare as string.
4675 */
4676 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4677 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4678 {
4679 float_T f1, f2;
4680
4681 if (rettv->v_type == VAR_FLOAT)
4682 f1 = rettv->vval.v_float;
4683 else
4684 f1 = get_tv_number(rettv);
4685 if (var2.v_type == VAR_FLOAT)
4686 f2 = var2.vval.v_float;
4687 else
4688 f2 = get_tv_number(&var2);
4689 n1 = FALSE;
4690 switch (type)
4691 {
4692 case TYPE_EQUAL: n1 = (f1 == f2); break;
4693 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4694 case TYPE_GREATER: n1 = (f1 > f2); break;
4695 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4696 case TYPE_SMALLER: n1 = (f1 < f2); break;
4697 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4698 case TYPE_UNKNOWN:
4699 case TYPE_MATCH:
4700 case TYPE_NOMATCH: break; /* avoid gcc warning */
4701 }
4702 }
4703#endif
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 /*
4706 * If one of the two variables is a number, compare as a number.
4707 * When using "=~" or "!~", always compare as string.
4708 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004709 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004712 n1 = get_tv_number(rettv);
4713 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 switch (type)
4715 {
4716 case TYPE_EQUAL: n1 = (n1 == n2); break;
4717 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4718 case TYPE_GREATER: n1 = (n1 > n2); break;
4719 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4720 case TYPE_SMALLER: n1 = (n1 < n2); break;
4721 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4722 case TYPE_UNKNOWN:
4723 case TYPE_MATCH:
4724 case TYPE_NOMATCH: break; /* avoid gcc warning */
4725 }
4726 }
4727 else
4728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004729 s1 = get_tv_string_buf(rettv, buf1);
4730 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4732 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4733 else
4734 i = 0;
4735 n1 = FALSE;
4736 switch (type)
4737 {
4738 case TYPE_EQUAL: n1 = (i == 0); break;
4739 case TYPE_NEQUAL: n1 = (i != 0); break;
4740 case TYPE_GREATER: n1 = (i > 0); break;
4741 case TYPE_GEQUAL: n1 = (i >= 0); break;
4742 case TYPE_SMALLER: n1 = (i < 0); break;
4743 case TYPE_SEQUAL: n1 = (i <= 0); break;
4744
4745 case TYPE_MATCH:
4746 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004747 n1 = pattern_match(s2, s1, ic);
4748 if (type == TYPE_NOMATCH)
4749 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 break;
4751
4752 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4753 }
4754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 clear_tv(rettv);
4756 clear_tv(&var2);
4757 rettv->v_type = VAR_NUMBER;
4758 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760 }
4761
4762 return OK;
4763}
4764
4765/*
4766 * Handle fourth level expression:
4767 * + number addition
4768 * - number subtraction
4769 * . string concatenation
4770 *
4771 * "arg" must point to the first non-white of the expression.
4772 * "arg" is advanced to the next non-white after the recognized expression.
4773 *
4774 * Return OK or FAIL.
4775 */
4776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004777eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
Bram Moolenaar33570922005-01-25 22:26:29 +00004779 typval_T var2;
4780 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004782 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 float_T f1 = 0, f2 = 0;
4785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 char_u *s1, *s2;
4787 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4788 char_u *p;
4789
4790 /*
4791 * Get the first variable.
4792 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004793 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 return FAIL;
4795
4796 /*
4797 * Repeat computing, until no '+', '-' or '.' is following.
4798 */
4799 for (;;)
4800 {
4801 op = **arg;
4802 if (op != '+' && op != '-' && op != '.')
4803 break;
4804
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 if ((op != '+' || rettv->v_type != VAR_LIST)
4806#ifdef FEAT_FLOAT
4807 && (op == '.' || rettv->v_type != VAR_FLOAT)
4808#endif
4809 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 {
4811 /* For "list + ...", an illegal use of the first operand as
4812 * a number cannot be determined before evaluating the 2nd
4813 * operand: if this is also a list, all is ok.
4814 * For "something . ...", "something - ..." or "non-list + ...",
4815 * we know that the first operand needs to be a string or number
4816 * without evaluating the 2nd operand. So check before to avoid
4817 * side effects after an error. */
4818 if (evaluate && get_tv_string_chk(rettv) == NULL)
4819 {
4820 clear_tv(rettv);
4821 return FAIL;
4822 }
4823 }
4824
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 /*
4826 * Get the second variable.
4827 */
4828 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004829 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 return FAIL;
4833 }
4834
4835 if (evaluate)
4836 {
4837 /*
4838 * Compute the result.
4839 */
4840 if (op == '.')
4841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4843 s2 = get_tv_string_buf_chk(&var2, buf2);
4844 if (s2 == NULL) /* type error ? */
4845 {
4846 clear_tv(rettv);
4847 clear_tv(&var2);
4848 return FAIL;
4849 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004850 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(rettv);
4852 rettv->v_type = VAR_STRING;
4853 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004855 else if (op == '+' && rettv->v_type == VAR_LIST
4856 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004857 {
4858 /* concatenate Lists */
4859 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4860 &var3) == FAIL)
4861 {
4862 clear_tv(rettv);
4863 clear_tv(&var2);
4864 return FAIL;
4865 }
4866 clear_tv(rettv);
4867 *rettv = var3;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 else
4870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004871 int error = FALSE;
4872
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873#ifdef FEAT_FLOAT
4874 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 f1 = rettv->vval.v_float;
4877 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 else
4880#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004881 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882 n1 = get_tv_number_chk(rettv, &error);
4883 if (error)
4884 {
4885 /* This can only happen for "list + non-list". For
4886 * "non-list + ..." or "something - ...", we returned
4887 * before evaluating the 2nd operand. */
4888 clear_tv(rettv);
4889 return FAIL;
4890 }
4891#ifdef FEAT_FLOAT
4892 if (var2.v_type == VAR_FLOAT)
4893 f1 = n1;
4894#endif
4895 }
4896#ifdef FEAT_FLOAT
4897 if (var2.v_type == VAR_FLOAT)
4898 {
4899 f2 = var2.vval.v_float;
4900 n2 = 0;
4901 }
4902 else
4903#endif
4904 {
4905 n2 = get_tv_number_chk(&var2, &error);
4906 if (error)
4907 {
4908 clear_tv(rettv);
4909 clear_tv(&var2);
4910 return FAIL;
4911 }
4912#ifdef FEAT_FLOAT
4913 if (rettv->v_type == VAR_FLOAT)
4914 f2 = n2;
4915#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918
4919#ifdef FEAT_FLOAT
4920 /* If there is a float on either side the result is a float. */
4921 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4922 {
4923 if (op == '+')
4924 f1 = f1 + f2;
4925 else
4926 f1 = f1 - f2;
4927 rettv->v_type = VAR_FLOAT;
4928 rettv->vval.v_float = f1;
4929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#endif
4932 {
4933 if (op == '+')
4934 n1 = n1 + n2;
4935 else
4936 n1 = n1 - n2;
4937 rettv->v_type = VAR_NUMBER;
4938 rettv->vval.v_number = n1;
4939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004941 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943 }
4944 return OK;
4945}
4946
4947/*
4948 * Handle fifth level expression:
4949 * * number multiplication
4950 * / number division
4951 * % number modulo
4952 *
4953 * "arg" must point to the first non-white of the expression.
4954 * "arg" is advanced to the next non-white after the recognized expression.
4955 *
4956 * Return OK or FAIL.
4957 */
4958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004959eval6(
4960 char_u **arg,
4961 typval_T *rettv,
4962 int evaluate,
4963 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
Bram Moolenaar33570922005-01-25 22:26:29 +00004965 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004967 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004968#ifdef FEAT_FLOAT
4969 int use_float = FALSE;
4970 float_T f1 = 0, f2;
4971#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004972 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 /*
4975 * Get the first variable.
4976 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004977 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 return FAIL;
4979
4980 /*
4981 * Repeat computing, until no '*', '/' or '%' is following.
4982 */
4983 for (;;)
4984 {
4985 op = **arg;
4986 if (op != '*' && op != '/' && op != '%')
4987 break;
4988
4989 if (evaluate)
4990 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991#ifdef FEAT_FLOAT
4992 if (rettv->v_type == VAR_FLOAT)
4993 {
4994 f1 = rettv->vval.v_float;
4995 use_float = TRUE;
4996 n1 = 0;
4997 }
4998 else
4999#endif
5000 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005002 if (error)
5003 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005 else
5006 n1 = 0;
5007
5008 /*
5009 * Get the second variable.
5010 */
5011 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005012 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 return FAIL;
5014
5015 if (evaluate)
5016 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017#ifdef FEAT_FLOAT
5018 if (var2.v_type == VAR_FLOAT)
5019 {
5020 if (!use_float)
5021 {
5022 f1 = n1;
5023 use_float = TRUE;
5024 }
5025 f2 = var2.vval.v_float;
5026 n2 = 0;
5027 }
5028 else
5029#endif
5030 {
5031 n2 = get_tv_number_chk(&var2, &error);
5032 clear_tv(&var2);
5033 if (error)
5034 return FAIL;
5035#ifdef FEAT_FLOAT
5036 if (use_float)
5037 f2 = n2;
5038#endif
5039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
5041 /*
5042 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005043 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045#ifdef FEAT_FLOAT
5046 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005048 if (op == '*')
5049 f1 = f1 * f2;
5050 else if (op == '/')
5051 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005052# ifdef VMS
5053 /* VMS crashes on divide by zero, work around it */
5054 if (f2 == 0.0)
5055 {
5056 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005057 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005058 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005059 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005060 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005061 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005062 }
5063 else
5064 f1 = f1 / f2;
5065# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005066 /* We rely on the floating point library to handle divide
5067 * by zero to result in "inf" and not a crash. */
5068 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005069# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005072 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005073 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005074 return FAIL;
5075 }
5076 rettv->v_type = VAR_FLOAT;
5077 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 if (op == '*')
5083 n1 = n1 * n2;
5084 else if (op == '/')
5085 {
5086 if (n2 == 0) /* give an error message? */
5087 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005088#ifdef FEAT_NUM64
5089 if (n1 == 0)
5090 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
5091 else if (n1 < 0)
5092 n1 = -0x7fffffffffffffff;
5093 else
5094 n1 = 0x7fffffffffffffff;
5095#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 if (n1 == 0)
5097 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5098 else if (n1 < 0)
5099 n1 = -0x7fffffffL;
5100 else
5101 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005102#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005103 }
5104 else
5105 n1 = n1 / n2;
5106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 {
5109 if (n2 == 0) /* give an error message? */
5110 n1 = 0;
5111 else
5112 n1 = n1 % n2;
5113 }
5114 rettv->v_type = VAR_NUMBER;
5115 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118 }
5119
5120 return OK;
5121}
5122
5123/*
5124 * Handle sixth level expression:
5125 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005126 * "string" string constant
5127 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 * &option-name option value
5129 * @r register contents
5130 * identifier variable value
5131 * function() function call
5132 * $VAR environment variable
5133 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005134 * [expr, expr] List
5135 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 *
5137 * Also handle:
5138 * ! in front logical NOT
5139 * - in front unary minus
5140 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 * trailing [] subscript in String or List
5142 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 *
5144 * "arg" must point to the first non-white of the expression.
5145 * "arg" is advanced to the next non-white after the recognized expression.
5146 *
5147 * Return OK or FAIL.
5148 */
5149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005150eval7(
5151 char_u **arg,
5152 typval_T *rettv,
5153 int evaluate,
5154 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005156 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 int len;
5158 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 char_u *start_leader, *end_leader;
5160 int ret = OK;
5161 char_u *alias;
5162
5163 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005164 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
5169 /*
5170 * Skip '!' and '-' characters. They are handled later.
5171 */
5172 start_leader = *arg;
5173 while (**arg == '!' || **arg == '-' || **arg == '+')
5174 *arg = skipwhite(*arg + 1);
5175 end_leader = *arg;
5176
5177 switch (**arg)
5178 {
5179 /*
5180 * Number constant.
5181 */
5182 case '0':
5183 case '1':
5184 case '2':
5185 case '3':
5186 case '4':
5187 case '5':
5188 case '6':
5189 case '7':
5190 case '8':
5191 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005192 {
5193#ifdef FEAT_FLOAT
5194 char_u *p = skipdigits(*arg + 1);
5195 int get_float = FALSE;
5196
5197 /* We accept a float when the format matches
5198 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005199 * strict to avoid backwards compatibility problems.
5200 * Don't look for a float after the "." operator, so that
5201 * ":let vers = 1.2.3" doesn't fail. */
5202 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005204 get_float = TRUE;
5205 p = skipdigits(p + 2);
5206 if (*p == 'e' || *p == 'E')
5207 {
5208 ++p;
5209 if (*p == '-' || *p == '+')
5210 ++p;
5211 if (!vim_isdigit(*p))
5212 get_float = FALSE;
5213 else
5214 p = skipdigits(p + 1);
5215 }
5216 if (ASCII_ISALPHA(*p) || *p == '.')
5217 get_float = FALSE;
5218 }
5219 if (get_float)
5220 {
5221 float_T f;
5222
5223 *arg += string2float(*arg, &f);
5224 if (evaluate)
5225 {
5226 rettv->v_type = VAR_FLOAT;
5227 rettv->vval.v_float = f;
5228 }
5229 }
5230 else
5231#endif
5232 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005233 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005234 *arg += len;
5235 if (evaluate)
5236 {
5237 rettv->v_type = VAR_NUMBER;
5238 rettv->vval.v_number = n;
5239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
5244 /*
5245 * String constant: "string".
5246 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005247 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 break;
5249
5250 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005253 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 break;
5255
5256 /*
5257 * List: [expr, expr]
5258 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005259 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 break;
5261
5262 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 * Dictionary: {key: val, key: val}
5264 */
5265 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5266 break;
5267
5268 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005269 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005271 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 break;
5273
5274 /*
5275 * Environment variable: $VAR.
5276 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005277 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 break;
5279
5280 /*
5281 * Register contents: @r.
5282 */
5283 case '@': ++*arg;
5284 if (evaluate)
5285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005286 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005287 rettv->vval.v_string = get_reg_contents(**arg,
5288 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
5290 if (**arg != NUL)
5291 ++*arg;
5292 break;
5293
5294 /*
5295 * nested expression: (expression).
5296 */
5297 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (**arg == ')')
5300 ++*arg;
5301 else if (ret == OK)
5302 {
5303 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005304 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 ret = FAIL;
5306 }
5307 break;
5308
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 break;
5311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312
5313 if (ret == NOTDONE)
5314 {
5315 /*
5316 * Must be a variable or function name.
5317 * Can also be a curly-braces kind of name: {expr}.
5318 */
5319 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005320 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 if (alias != NULL)
5322 s = alias;
5323
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005324 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 ret = FAIL;
5326 else
5327 {
5328 if (**arg == '(') /* recursive! */
5329 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005330 partial_T *partial;
5331
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 /* If "s" is the name of a variable of type VAR_FUNC
5333 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005334 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005335
5336 /* Invoke the function. */
5337 ret = get_func_tv(s, len, rettv, arg,
5338 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005339 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005340
5341 /* If evaluate is FALSE rettv->v_type was not set in
5342 * get_func_tv, but it's needed in handle_subscript() to parse
5343 * what follows. So set it here. */
5344 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5345 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005346 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005347 rettv->v_type = VAR_FUNC;
5348 }
5349
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 /* Stop the expression evaluation when immediately
5351 * aborting on error, or when an interrupt occurred or
5352 * an exception was thrown but not caught. */
5353 if (aborting())
5354 {
5355 if (ret == OK)
5356 clear_tv(rettv);
5357 ret = FAIL;
5358 }
5359 }
5360 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005361 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005362 else
5363 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005365 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366 }
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 *arg = skipwhite(*arg);
5369
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5371 * expr(expr). */
5372 if (ret == OK)
5373 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374
5375 /*
5376 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5377 */
5378 if (ret == OK && evaluate && end_leader > start_leader)
5379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005380 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005381 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005382#ifdef FEAT_FLOAT
5383 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005384
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005385 if (rettv->v_type == VAR_FLOAT)
5386 f = rettv->vval.v_float;
5387 else
5388#endif
5389 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005390 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005392 clear_tv(rettv);
5393 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005395 else
5396 {
5397 while (end_leader > start_leader)
5398 {
5399 --end_leader;
5400 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005401 {
5402#ifdef FEAT_FLOAT
5403 if (rettv->v_type == VAR_FLOAT)
5404 f = !f;
5405 else
5406#endif
5407 val = !val;
5408 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005409 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005410 {
5411#ifdef FEAT_FLOAT
5412 if (rettv->v_type == VAR_FLOAT)
5413 f = -f;
5414 else
5415#endif
5416 val = -val;
5417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005418 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005419#ifdef FEAT_FLOAT
5420 if (rettv->v_type == VAR_FLOAT)
5421 {
5422 clear_tv(rettv);
5423 rettv->vval.v_float = f;
5424 }
5425 else
5426#endif
5427 {
5428 clear_tv(rettv);
5429 rettv->v_type = VAR_NUMBER;
5430 rettv->vval.v_number = val;
5431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
5434
5435 return ret;
5436}
5437
5438/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005439 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5440 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5442 */
5443 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005444eval_index(
5445 char_u **arg,
5446 typval_T *rettv,
5447 int evaluate,
5448 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449{
5450 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005451 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 long len = -1;
5454 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457
Bram Moolenaara03f2332016-02-06 18:09:59 +01005458 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005460 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005461 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005462 if (verbose)
5463 EMSG(_("E695: Cannot index a Funcref"));
5464 return FAIL;
5465 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005466#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005467 if (verbose)
5468 EMSG(_(e_float_as_string));
5469 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005470#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005471 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005472 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005473 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005474 if (verbose)
5475 EMSG(_("E909: Cannot index a special variable"));
5476 return FAIL;
5477 case VAR_UNKNOWN:
5478 if (evaluate)
5479 return FAIL;
5480 /* FALLTHROUGH */
5481
5482 case VAR_STRING:
5483 case VAR_NUMBER:
5484 case VAR_LIST:
5485 case VAR_DICT:
5486 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005487 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005489 init_tv(&var1);
5490 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 /*
5494 * dict.name
5495 */
5496 key = *arg + 1;
5497 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5498 ;
5499 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 }
5503 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005505 /*
5506 * something[idx]
5507 *
5508 * Get the (first) variable from inside the [].
5509 */
5510 *arg = skipwhite(*arg + 1);
5511 if (**arg == ':')
5512 empty1 = TRUE;
5513 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5514 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005515 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5516 {
5517 /* not a number or string */
5518 clear_tv(&var1);
5519 return FAIL;
5520 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005521
5522 /*
5523 * Get the second variable from inside the [:].
5524 */
5525 if (**arg == ':')
5526 {
5527 range = TRUE;
5528 *arg = skipwhite(*arg + 1);
5529 if (**arg == ']')
5530 empty2 = TRUE;
5531 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005533 if (!empty1)
5534 clear_tv(&var1);
5535 return FAIL;
5536 }
5537 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5538 {
5539 /* not a number or string */
5540 if (!empty1)
5541 clear_tv(&var1);
5542 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005543 return FAIL;
5544 }
5545 }
5546
5547 /* Check for the ']'. */
5548 if (**arg != ']')
5549 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005550 if (verbose)
5551 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552 clear_tv(&var1);
5553 if (range)
5554 clear_tv(&var2);
5555 return FAIL;
5556 }
5557 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 }
5559
5560 if (evaluate)
5561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562 n1 = 0;
5563 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 n1 = get_tv_number(&var1);
5566 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 }
5568 if (range)
5569 {
5570 if (empty2)
5571 n2 = -1;
5572 else
5573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 n2 = get_tv_number(&var2);
5575 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005576 }
5577 }
5578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005581 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005582 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005583 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005584 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005585 case VAR_SPECIAL:
5586 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005587 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005588 break; /* not evaluating, skipping over subscript */
5589
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 case VAR_NUMBER:
5591 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005592 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 len = (long)STRLEN(s);
5594 if (range)
5595 {
5596 /* The resulting variable is a substring. If the indexes
5597 * are out of range the result is empty. */
5598 if (n1 < 0)
5599 {
5600 n1 = len + n1;
5601 if (n1 < 0)
5602 n1 = 0;
5603 }
5604 if (n2 < 0)
5605 n2 = len + n2;
5606 else if (n2 >= len)
5607 n2 = len;
5608 if (n1 >= len || n2 < 0 || n1 > n2)
5609 s = NULL;
5610 else
5611 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5612 }
5613 else
5614 {
5615 /* The resulting variable is a string of a single
5616 * character. If the index is too big or negative the
5617 * result is empty. */
5618 if (n1 >= len || n1 < 0)
5619 s = NULL;
5620 else
5621 s = vim_strnsave(s + n1, 1);
5622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 clear_tv(rettv);
5624 rettv->v_type = VAR_STRING;
5625 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 break;
5627
5628 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 if (n1 < 0)
5631 n1 = len + n1;
5632 if (!empty1 && (n1 < 0 || n1 >= len))
5633 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005634 /* For a range we allow invalid values and return an empty
5635 * list. A list index out of range is an error. */
5636 if (!range)
5637 {
5638 if (verbose)
5639 EMSGN(_(e_listidx), n1);
5640 return FAIL;
5641 }
5642 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005643 }
5644 if (range)
5645 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005646 list_T *l;
5647 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648
5649 if (n2 < 0)
5650 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005651 else if (n2 >= len)
5652 n2 = len - 1;
5653 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005654 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005655 l = list_alloc();
5656 if (l == NULL)
5657 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005658 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005659 n1 <= n2; ++n1)
5660 {
5661 if (list_append_tv(l, &item->li_tv) == FAIL)
5662 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005663 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664 return FAIL;
5665 }
5666 item = item->li_next;
5667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 clear_tv(rettv);
5669 rettv->v_type = VAR_LIST;
5670 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005671 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005672 }
5673 else
5674 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005675 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 clear_tv(rettv);
5677 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 }
5679 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680
5681 case VAR_DICT:
5682 if (range)
5683 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005684 if (verbose)
5685 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 if (len == -1)
5687 clear_tv(&var1);
5688 return FAIL;
5689 }
5690 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005691 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692
5693 if (len == -1)
5694 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005695 key = get_tv_string_chk(&var1);
5696 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 clear_tv(&var1);
5699 return FAIL;
5700 }
5701 }
5702
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005703 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005705 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005706 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 if (len == -1)
5708 clear_tv(&var1);
5709 if (item == NULL)
5710 return FAIL;
5711
5712 copy_tv(&item->di_tv, &var1);
5713 clear_tv(rettv);
5714 *rettv = var1;
5715 }
5716 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 }
5718 }
5719
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720 return OK;
5721}
5722
5723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 * Get an option value.
5725 * "arg" points to the '&' or '+' before the option name.
5726 * "arg" is advanced to character after the option name.
5727 * Return OK or FAIL.
5728 */
5729 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005730get_option_tv(
5731 char_u **arg,
5732 typval_T *rettv, /* when NULL, only check if option exists */
5733 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735 char_u *option_end;
5736 long numval;
5737 char_u *stringval;
5738 int opt_type;
5739 int c;
5740 int working = (**arg == '+'); /* has("+option") */
5741 int ret = OK;
5742 int opt_flags;
5743
5744 /*
5745 * Isolate the option name and find its value.
5746 */
5747 option_end = find_option_end(arg, &opt_flags);
5748 if (option_end == NULL)
5749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005750 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 EMSG2(_("E112: Option name missing: %s"), *arg);
5752 return FAIL;
5753 }
5754
5755 if (!evaluate)
5756 {
5757 *arg = option_end;
5758 return OK;
5759 }
5760
5761 c = *option_end;
5762 *option_end = NUL;
5763 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005764 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 if (opt_type == -3) /* invalid name */
5767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005768 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 EMSG2(_("E113: Unknown option: %s"), *arg);
5770 ret = FAIL;
5771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005772 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
5774 if (opt_type == -2) /* hidden string option */
5775 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005776 rettv->v_type = VAR_STRING;
5777 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779 else if (opt_type == -1) /* hidden number option */
5780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005781 rettv->v_type = VAR_NUMBER;
5782 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
5784 else if (opt_type == 1) /* number option */
5785 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005786 rettv->v_type = VAR_NUMBER;
5787 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 }
5789 else /* string option */
5790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005791 rettv->v_type = VAR_STRING;
5792 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
5794 }
5795 else if (working && (opt_type == -2 || opt_type == -1))
5796 ret = FAIL;
5797
5798 *option_end = c; /* put back for error messages */
5799 *arg = option_end;
5800
5801 return ret;
5802}
5803
5804/*
5805 * Allocate a variable for a string constant.
5806 * Return OK or FAIL.
5807 */
5808 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005809get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
5811 char_u *p;
5812 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 int extra = 0;
5814
5815 /*
5816 * Find the end of the string, skipping backslashed characters.
5817 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005818 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {
5820 if (*p == '\\' && p[1] != NUL)
5821 {
5822 ++p;
5823 /* A "\<x>" form occupies at least 4 characters, and produces up
5824 * to 6 characters: reserve space for 2 extra */
5825 if (*p == '<')
5826 extra += 2;
5827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 }
5829
5830 if (*p != '"')
5831 {
5832 EMSG2(_("E114: Missing quote: %s"), *arg);
5833 return FAIL;
5834 }
5835
5836 /* If only parsing, set *arg and return here */
5837 if (!evaluate)
5838 {
5839 *arg = p + 1;
5840 return OK;
5841 }
5842
5843 /*
5844 * Copy the string into allocated memory, handling backslashed
5845 * characters.
5846 */
5847 name = alloc((unsigned)(p - *arg + extra));
5848 if (name == NULL)
5849 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 rettv->v_type = VAR_STRING;
5851 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {
5855 if (*p == '\\')
5856 {
5857 switch (*++p)
5858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 case 'b': *name++ = BS; ++p; break;
5860 case 'e': *name++ = ESC; ++p; break;
5861 case 'f': *name++ = FF; ++p; break;
5862 case 'n': *name++ = NL; ++p; break;
5863 case 'r': *name++ = CAR; ++p; break;
5864 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
5866 case 'X': /* hex: "\x1", "\x12" */
5867 case 'x':
5868 case 'u': /* Unicode: "\u0023" */
5869 case 'U':
5870 if (vim_isxdigit(p[1]))
5871 {
5872 int n, nr;
5873 int c = toupper(*p);
5874
5875 if (c == 'X')
5876 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005877 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005879 else
5880 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 nr = 0;
5882 while (--n >= 0 && vim_isxdigit(p[1]))
5883 {
5884 ++p;
5885 nr = (nr << 4) + hex2nr(*p);
5886 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888#ifdef FEAT_MBYTE
5889 /* For "\u" store the number according to
5890 * 'encoding'. */
5891 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 else
5894#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 break;
5898
5899 /* octal: "\1", "\12", "\123" */
5900 case '0':
5901 case '1':
5902 case '2':
5903 case '3':
5904 case '4':
5905 case '5':
5906 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 case '7': *name = *p++ - '0';
5908 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 *name = (*name << 3) + *p++ - '0';
5911 if (*p >= '0' && *p <= '7')
5912 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005914 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 break;
5916
5917 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 if (extra != 0)
5920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 break;
5923 }
5924 /* FALLTHROUGH */
5925
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 break;
5928 }
5929 }
5930 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 *arg = p + 1;
5936
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 return OK;
5938}
5939
5940/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 * Return OK or FAIL.
5943 */
5944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005945get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
5947 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005948 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005949 int reduce = 0;
5950
5951 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005952 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005953 */
5954 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5955 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005956 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005957 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005958 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005959 break;
5960 ++reduce;
5961 ++p;
5962 }
5963 }
5964
Bram Moolenaar8c711452005-01-14 21:53:12 +00005965 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005967 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005968 return FAIL;
5969 }
5970
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005972 if (!evaluate)
5973 {
5974 *arg = p + 1;
5975 return OK;
5976 }
5977
5978 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005979 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005980 */
5981 str = alloc((unsigned)((p - *arg) - reduce));
5982 if (str == NULL)
5983 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984 rettv->v_type = VAR_STRING;
5985 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005986
Bram Moolenaar8c711452005-01-14 21:53:12 +00005987 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005988 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005989 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005990 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005991 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005992 break;
5993 ++p;
5994 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005995 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005996 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005997 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005998 *arg = p + 1;
5999
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006000 return OK;
6001}
6002
Bram Moolenaarddecc252016-04-06 22:59:37 +02006003 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006004partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02006005{
6006 int i;
6007
6008 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006009 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006010 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006011 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006012 func_unref(pt->pt_name);
6013 vim_free(pt->pt_name);
6014 vim_free(pt);
6015}
6016
6017/*
6018 * Unreference a closure: decrement the reference count and free it when it
6019 * becomes zero.
6020 */
6021 void
6022partial_unref(partial_T *pt)
6023{
6024 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006025 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006026}
6027
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006028/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 * Allocate a variable for a List and fill it from "*arg".
6030 * Return OK or FAIL.
6031 */
6032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 list_T *l = NULL;
6036 typval_T tv;
6037 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038
6039 if (evaluate)
6040 {
6041 l = list_alloc();
6042 if (l == NULL)
6043 return FAIL;
6044 }
6045
6046 *arg = skipwhite(*arg + 1);
6047 while (**arg != ']' && **arg != NUL)
6048 {
6049 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6050 goto failret;
6051 if (evaluate)
6052 {
6053 item = listitem_alloc();
6054 if (item != NULL)
6055 {
6056 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006057 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 list_append(l, item);
6059 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006060 else
6061 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 }
6063
6064 if (**arg == ']')
6065 break;
6066 if (**arg != ',')
6067 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006068 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 goto failret;
6070 }
6071 *arg = skipwhite(*arg + 1);
6072 }
6073
6074 if (**arg != ']')
6075 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006076 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077failret:
6078 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006079 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080 return FAIL;
6081 }
6082
6083 *arg = skipwhite(*arg + 1);
6084 if (evaluate)
6085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006086 rettv->v_type = VAR_LIST;
6087 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 ++l->lv_refcount;
6089 }
6090
6091 return OK;
6092}
6093
6094/*
6095 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006096 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006098 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006101 list_T *l;
6102
6103 l = (list_T *)alloc_clear(sizeof(list_T));
6104 if (l != NULL)
6105 {
6106 /* Prepend the list to the list of lists for garbage collection. */
6107 if (first_list != NULL)
6108 first_list->lv_used_prev = l;
6109 l->lv_used_prev = NULL;
6110 l->lv_used_next = first_list;
6111 first_list = l;
6112 }
6113 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114}
6115
6116/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006117 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006118 * Returns OK or FAIL.
6119 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006120 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006121rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006122{
6123 list_T *l = list_alloc();
6124
6125 if (l == NULL)
6126 return FAIL;
6127
6128 rettv->vval.v_list = l;
6129 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006130 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006131 ++l->lv_refcount;
6132 return OK;
6133}
6134
6135/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136 * Unreference a list: decrement the reference count and free it when it
6137 * becomes zero.
6138 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006140list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006142 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006143 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144}
6145
6146/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006147 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148 * Ignores the reference count.
6149 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006150 static void
6151list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006152{
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006155 for (item = l->lv_first; item != NULL; item = l->lv_first)
6156 {
6157 /* Remove the item before deleting it. */
6158 l->lv_first = item->li_next;
6159 clear_tv(&item->li_tv);
6160 vim_free(item);
6161 }
6162}
6163
6164 static void
6165list_free_list(list_T *l)
6166{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006167 /* Remove the list from the list of lists for garbage collection. */
6168 if (l->lv_used_prev == NULL)
6169 first_list = l->lv_used_next;
6170 else
6171 l->lv_used_prev->lv_used_next = l->lv_used_next;
6172 if (l->lv_used_next != NULL)
6173 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6174
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 vim_free(l);
6176}
6177
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006178 void
6179list_free(list_T *l)
6180{
6181 if (!in_free_unref_items)
6182 {
6183 list_free_contents(l);
6184 list_free_list(l);
6185 }
6186}
6187
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188/*
6189 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006190 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006192 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006193listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194{
Bram Moolenaar33570922005-01-25 22:26:29 +00006195 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196}
6197
6198/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006199 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006202listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006203{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006204 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006205 vim_free(item);
6206}
6207
6208/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006209 * Remove a list item from a List and free it. Also clears the value.
6210 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006212listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006213{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006214 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006215 listitem_free(item);
6216}
6217
6218/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 * Get the number of items in a list.
6220 */
6221 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006222list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006223{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 if (l == NULL)
6225 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006226 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227}
6228
6229/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 * Return TRUE when two lists have exactly the same values.
6231 */
6232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006233list_equal(
6234 list_T *l1,
6235 list_T *l2,
6236 int ic, /* ignore case for strings */
6237 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006238{
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006241 if (l1 == NULL || l2 == NULL)
6242 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006243 if (l1 == l2)
6244 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006245 if (list_len(l1) != list_len(l2))
6246 return FALSE;
6247
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006248 for (item1 = l1->lv_first, item2 = l2->lv_first;
6249 item1 != NULL && item2 != NULL;
6250 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006251 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006252 return FALSE;
6253 return item1 == NULL && item2 == NULL;
6254}
6255
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006256/*
6257 * Return the dictitem that an entry in a hashtable points to.
6258 */
6259 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006260dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006261{
6262 return HI2DI(hi);
6263}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006264
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006266 * Return TRUE when two dictionaries have exactly the same key/values.
6267 */
6268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006269dict_equal(
6270 dict_T *d1,
6271 dict_T *d2,
6272 int ic, /* ignore case for strings */
6273 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006274{
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 hashitem_T *hi;
6276 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006277 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006278
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006279 if (d1 == NULL && d2 == NULL)
6280 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006281 if (d1 == NULL || d2 == NULL)
6282 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006283 if (d1 == d2)
6284 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006285 if (dict_len(d1) != dict_len(d2))
6286 return FALSE;
6287
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006288 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006290 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006291 if (!HASHITEM_EMPTY(hi))
6292 {
6293 item2 = dict_find(d2, hi->hi_key, -1);
6294 if (item2 == NULL)
6295 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006296 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006297 return FALSE;
6298 --todo;
6299 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006300 }
6301 return TRUE;
6302}
6303
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006304static int tv_equal_recurse_limit;
6305
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006306 static int
6307func_equal(
6308 typval_T *tv1,
6309 typval_T *tv2,
6310 int ic) /* ignore case */
6311{
6312 char_u *s1, *s2;
6313 dict_T *d1, *d2;
6314 int a1, a2;
6315 int i;
6316
6317 /* empty and NULL function name considered the same */
6318 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6319 : tv1->vval.v_partial->pt_name;
6320 if (s1 != NULL && *s1 == NUL)
6321 s1 = NULL;
6322 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6323 : tv2->vval.v_partial->pt_name;
6324 if (s2 != NULL && *s2 == NUL)
6325 s2 = NULL;
6326 if (s1 == NULL || s2 == NULL)
6327 {
6328 if (s1 != s2)
6329 return FALSE;
6330 }
6331 else if (STRCMP(s1, s2) != 0)
6332 return FALSE;
6333
6334 /* empty dict and NULL dict is different */
6335 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6336 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6337 if (d1 == NULL || d2 == NULL)
6338 {
6339 if (d1 != d2)
6340 return FALSE;
6341 }
6342 else if (!dict_equal(d1, d2, ic, TRUE))
6343 return FALSE;
6344
6345 /* empty list and no list considered the same */
6346 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6347 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6348 if (a1 != a2)
6349 return FALSE;
6350 for (i = 0; i < a1; ++i)
6351 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6352 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6353 return FALSE;
6354
6355 return TRUE;
6356}
6357
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006358/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006359 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006360 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006361 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006362 */
6363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006364tv_equal(
6365 typval_T *tv1,
6366 typval_T *tv2,
6367 int ic, /* ignore case */
6368 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006369{
6370 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006371 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006372 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006373 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006374
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006375 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006376 * recursiveness to a limit. We guess they are equal then.
6377 * A fixed limit has the problem of still taking an awful long time.
6378 * Reduce the limit every time running into it. That should work fine for
6379 * deeply linked structures that are not recursively linked and catch
6380 * recursiveness quickly. */
6381 if (!recursive)
6382 tv_equal_recurse_limit = 1000;
6383 if (recursive_cnt >= tv_equal_recurse_limit)
6384 {
6385 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006386 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006387 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006388
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006389 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6390 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006391 if ((tv1->v_type == VAR_FUNC
6392 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6393 && (tv2->v_type == VAR_FUNC
6394 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6395 {
6396 ++recursive_cnt;
6397 r = func_equal(tv1, tv2, ic);
6398 --recursive_cnt;
6399 return r;
6400 }
6401
6402 if (tv1->v_type != tv2->v_type)
6403 return FALSE;
6404
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006405 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006407 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006408 ++recursive_cnt;
6409 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6410 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006411 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006412
6413 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006414 ++recursive_cnt;
6415 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6416 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006417 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006418
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006419 case VAR_NUMBER:
6420 return tv1->vval.v_number == tv2->vval.v_number;
6421
6422 case VAR_STRING:
6423 s1 = get_tv_string_buf(tv1, buf1);
6424 s2 = get_tv_string_buf(tv2, buf2);
6425 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006426
6427 case VAR_SPECIAL:
6428 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006429
6430 case VAR_FLOAT:
6431#ifdef FEAT_FLOAT
6432 return tv1->vval.v_float == tv2->vval.v_float;
6433#endif
6434 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006435#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006436 return tv1->vval.v_job == tv2->vval.v_job;
6437#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006438 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006439#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006440 return tv1->vval.v_channel == tv2->vval.v_channel;
6441#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006442 case VAR_FUNC:
6443 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006444 case VAR_UNKNOWN:
6445 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006447
Bram Moolenaara03f2332016-02-06 18:09:59 +01006448 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6449 * does not equal anything, not even itself. */
6450 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451}
6452
6453/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454 * Locate item with index "n" in list "l" and return it.
6455 * A negative index is counted from the end; -1 is the last item.
6456 * Returns NULL when "n" is out of range.
6457 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006458 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006459list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460{
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006462 long idx;
6463
6464 if (l == NULL)
6465 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006466
6467 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006469 n = l->lv_len + n;
6470
6471 /* Check for index out of range. */
6472 if (n < 0 || n >= l->lv_len)
6473 return NULL;
6474
6475 /* When there is a cached index may start search from there. */
6476 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006477 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006478 if (n < l->lv_idx / 2)
6479 {
6480 /* closest to the start of the list */
6481 item = l->lv_first;
6482 idx = 0;
6483 }
6484 else if (n > (l->lv_idx + l->lv_len) / 2)
6485 {
6486 /* closest to the end of the list */
6487 item = l->lv_last;
6488 idx = l->lv_len - 1;
6489 }
6490 else
6491 {
6492 /* closest to the cached index */
6493 item = l->lv_idx_item;
6494 idx = l->lv_idx;
6495 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496 }
6497 else
6498 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006499 if (n < l->lv_len / 2)
6500 {
6501 /* closest to the start of the list */
6502 item = l->lv_first;
6503 idx = 0;
6504 }
6505 else
6506 {
6507 /* closest to the end of the list */
6508 item = l->lv_last;
6509 idx = l->lv_len - 1;
6510 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006512
6513 while (n > idx)
6514 {
6515 /* search forward */
6516 item = item->li_next;
6517 ++idx;
6518 }
6519 while (n < idx)
6520 {
6521 /* search backward */
6522 item = item->li_prev;
6523 --idx;
6524 }
6525
6526 /* cache the used index */
6527 l->lv_idx = idx;
6528 l->lv_idx_item = item;
6529
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530 return item;
6531}
6532
6533/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006534 * Get list item "l[idx]" as a number.
6535 */
6536 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006537list_find_nr(
6538 list_T *l,
6539 long idx,
6540 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006541{
6542 listitem_T *li;
6543
6544 li = list_find(l, idx);
6545 if (li == NULL)
6546 {
6547 if (errorp != NULL)
6548 *errorp = TRUE;
6549 return -1L;
6550 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006551 return (long)get_tv_number_chk(&li->li_tv, errorp);
Bram Moolenaara5525202006-03-02 22:52:09 +00006552}
6553
6554/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006555 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6556 */
6557 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006558list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006559{
6560 listitem_T *li;
6561
6562 li = list_find(l, idx - 1);
6563 if (li == NULL)
6564 {
6565 EMSGN(_(e_listidx), idx);
6566 return NULL;
6567 }
6568 return get_tv_string(&li->li_tv);
6569}
6570
6571/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006572 * Locate "item" list "l" and return its index.
6573 * Returns -1 when "item" is not in the list.
6574 */
6575 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006576list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006577{
6578 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006580
6581 if (l == NULL)
6582 return -1;
6583 idx = 0;
6584 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6585 ++idx;
6586 if (li == NULL)
6587 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006588 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006589}
6590
6591/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 * Append item "item" to the end of list "l".
6593 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006595list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596{
6597 if (l->lv_last == NULL)
6598 {
6599 /* empty list */
6600 l->lv_first = item;
6601 l->lv_last = item;
6602 item->li_prev = NULL;
6603 }
6604 else
6605 {
6606 l->lv_last->li_next = item;
6607 item->li_prev = l->lv_last;
6608 l->lv_last = item;
6609 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006610 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 item->li_next = NULL;
6612}
6613
6614/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006616 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006619list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006621 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
Bram Moolenaar05159a02005-02-26 23:04:13 +00006623 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006625 copy_tv(tv, &li->li_tv);
6626 list_append(l, li);
6627 return OK;
6628}
6629
6630/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006631 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006632 * Return FAIL when out of memory.
6633 */
6634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006635list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006636{
6637 listitem_T *li = listitem_alloc();
6638
6639 if (li == NULL)
6640 return FAIL;
6641 li->li_tv.v_type = VAR_DICT;
6642 li->li_tv.v_lock = 0;
6643 li->li_tv.vval.v_dict = dict;
6644 list_append(list, li);
6645 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 return OK;
6647}
6648
6649/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006650 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006651 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006652 * Returns FAIL when out of memory.
6653 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006654 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006655list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006656{
6657 listitem_T *li = listitem_alloc();
6658
6659 if (li == NULL)
6660 return FAIL;
6661 list_append(l, li);
6662 li->li_tv.v_type = VAR_STRING;
6663 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006664 if (str == NULL)
6665 li->li_tv.vval.v_string = NULL;
6666 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006667 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006668 return FAIL;
6669 return OK;
6670}
6671
6672/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006673 * Append "n" to list "l".
6674 * Returns FAIL when out of memory.
6675 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006677list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006678{
6679 listitem_T *li;
6680
6681 li = listitem_alloc();
6682 if (li == NULL)
6683 return FAIL;
6684 li->li_tv.v_type = VAR_NUMBER;
6685 li->li_tv.v_lock = 0;
6686 li->li_tv.vval.v_number = n;
6687 list_append(l, li);
6688 return OK;
6689}
6690
6691/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006692 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006693 * If "item" is NULL append at the end.
6694 * Return FAIL when out of memory.
6695 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006696 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006697list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006698{
Bram Moolenaar33570922005-01-25 22:26:29 +00006699 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006700
6701 if (ni == NULL)
6702 return FAIL;
6703 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006704 list_insert(l, ni, item);
6705 return OK;
6706}
6707
6708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006709list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006710{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006711 if (item == NULL)
6712 /* Append new item at end of list. */
6713 list_append(l, ni);
6714 else
6715 {
6716 /* Insert new item before existing item. */
6717 ni->li_prev = item->li_prev;
6718 ni->li_next = item;
6719 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006720 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006721 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006722 ++l->lv_idx;
6723 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006724 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006725 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006726 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006727 l->lv_idx_item = NULL;
6728 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006729 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006730 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006731 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732}
6733
6734/*
6735 * Extend "l1" with "l2".
6736 * If "bef" is NULL append at the end, otherwise insert before this item.
6737 * Returns FAIL when out of memory.
6738 */
6739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006740list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741{
Bram Moolenaar33570922005-01-25 22:26:29 +00006742 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006743 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006744
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006745 /* We also quit the loop when we have inserted the original item count of
6746 * the list, avoid a hang when we extend a list with itself. */
6747 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006748 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6749 return FAIL;
6750 return OK;
6751}
6752
6753/*
6754 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6755 * Return FAIL when out of memory.
6756 */
6757 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006758list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006759{
Bram Moolenaar33570922005-01-25 22:26:29 +00006760 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006761
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006762 if (l1 == NULL || l2 == NULL)
6763 return FAIL;
6764
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006765 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006766 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006767 if (l == NULL)
6768 return FAIL;
6769 tv->v_type = VAR_LIST;
6770 tv->vval.v_list = l;
6771
6772 /* append all items from the second list */
6773 return list_extend(l, l2, NULL);
6774}
6775
6776/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006777 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006778 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006779 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780 * Returns NULL when out of memory.
6781 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006782 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006783list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006784{
Bram Moolenaar33570922005-01-25 22:26:29 +00006785 list_T *copy;
6786 listitem_T *item;
6787 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788
6789 if (orig == NULL)
6790 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791
6792 copy = list_alloc();
6793 if (copy != NULL)
6794 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006795 if (copyID != 0)
6796 {
6797 /* Do this before adding the items, because one of the items may
6798 * refer back to this list. */
6799 orig->lv_copyID = copyID;
6800 orig->lv_copylist = copy;
6801 }
6802 for (item = orig->lv_first; item != NULL && !got_int;
6803 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006804 {
6805 ni = listitem_alloc();
6806 if (ni == NULL)
6807 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006808 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006809 {
6810 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6811 {
6812 vim_free(ni);
6813 break;
6814 }
6815 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006816 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006818 list_append(copy, ni);
6819 }
6820 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006821 if (item != NULL)
6822 {
6823 list_unref(copy);
6824 copy = NULL;
6825 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006826 }
6827
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006828 return copy;
6829}
6830
6831/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006832 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006833 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006834 * This used to be called list_remove, but that conflicts with a Sun header
6835 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006838vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006839{
Bram Moolenaar33570922005-01-25 22:26:29 +00006840 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006841
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006842 /* notify watchers */
6843 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006844 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006845 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006846 list_fix_watch(l, ip);
6847 if (ip == item2)
6848 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006850
6851 if (item2->li_next == NULL)
6852 l->lv_last = item->li_prev;
6853 else
6854 item2->li_next->li_prev = item->li_prev;
6855 if (item->li_prev == NULL)
6856 l->lv_first = item2->li_next;
6857 else
6858 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006859 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006860}
6861
6862/*
6863 * Return an allocated string with the string representation of a list.
6864 * May return NULL.
6865 */
6866 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006867list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006868{
6869 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006870
6871 if (tv->vval.v_list == NULL)
6872 return NULL;
6873 ga_init2(&ga, (int)sizeof(char), 80);
6874 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006875 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6876 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006877 {
6878 vim_free(ga.ga_data);
6879 return NULL;
6880 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006881 ga_append(&ga, ']');
6882 ga_append(&ga, NUL);
6883 return (char_u *)ga.ga_data;
6884}
6885
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006886typedef struct join_S {
6887 char_u *s;
6888 char_u *tofree;
6889} join_T;
6890
6891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006892list_join_inner(
6893 garray_T *gap, /* to store the result in */
6894 list_T *l,
6895 char_u *sep,
6896 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006897 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006898 int copyID,
6899 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006900{
6901 int i;
6902 join_T *p;
6903 int len;
6904 int sumlen = 0;
6905 int first = TRUE;
6906 char_u *tofree;
6907 char_u numbuf[NUMBUFLEN];
6908 listitem_T *item;
6909 char_u *s;
6910
6911 /* Stringify each item in the list. */
6912 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6913 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006914 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6915 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006916 if (s == NULL)
6917 return FAIL;
6918
6919 len = (int)STRLEN(s);
6920 sumlen += len;
6921
Bram Moolenaarcde88542015-08-11 19:14:00 +02006922 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006923 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6924 if (tofree != NULL || s != numbuf)
6925 {
6926 p->s = s;
6927 p->tofree = tofree;
6928 }
6929 else
6930 {
6931 p->s = vim_strnsave(s, len);
6932 p->tofree = p->s;
6933 }
6934
6935 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006936 if (did_echo_string_emsg) /* recursion error, bail out */
6937 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006938 }
6939
6940 /* Allocate result buffer with its total size, avoid re-allocation and
6941 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6942 if (join_gap->ga_len >= 2)
6943 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6944 if (ga_grow(gap, sumlen + 2) == FAIL)
6945 return FAIL;
6946
6947 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6948 {
6949 if (first)
6950 first = FALSE;
6951 else
6952 ga_concat(gap, sep);
6953 p = ((join_T *)join_gap->ga_data) + i;
6954
6955 if (p->s != NULL)
6956 ga_concat(gap, p->s);
6957 line_breakcheck();
6958 }
6959
6960 return OK;
6961}
6962
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006963/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006964 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006965 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006966 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006967 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006969list_join(
6970 garray_T *gap,
6971 list_T *l,
6972 char_u *sep,
6973 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006974 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006975 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006976{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006977 garray_T join_ga;
6978 int retval;
6979 join_T *p;
6980 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006981
Bram Moolenaard39a7512015-04-16 22:51:22 +02006982 if (l->lv_len < 1)
6983 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006984 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006985 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6986 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006987
6988 /* Dispose each item in join_ga. */
6989 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006990 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006991 p = (join_T *)join_ga.ga_data;
6992 for (i = 0; i < join_ga.ga_len; ++i)
6993 {
6994 vim_free(p->tofree);
6995 ++p;
6996 }
6997 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006998 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006999
7000 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007001}
7002
7003/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007004 * Return the next (unique) copy ID.
7005 * Used for serializing nested structures.
7006 */
7007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007008get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007009{
7010 current_copyID += COPYID_INC;
7011 return current_copyID;
7012}
7013
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007014/* Used by get_func_tv() */
7015static garray_T funcargs = GA_EMPTY;
7016
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007017/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 * Garbage collection for lists and dictionaries.
7019 *
7020 * We use reference counts to be able to free most items right away when they
7021 * are no longer used. But for composite items it's possible that it becomes
7022 * unused while the reference count is > 0: When there is a recursive
7023 * reference. Example:
7024 * :let l = [1, 2, 3]
7025 * :let d = {9: l}
7026 * :let l[1] = d
7027 *
7028 * Since this is quite unusual we handle this with garbage collection: every
7029 * once in a while find out which lists and dicts are not referenced from any
7030 * variable.
7031 *
7032 * Here is a good reference text about garbage collection (refers to Python
7033 * but it applies to all reference-counting mechanisms):
7034 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007035 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007036
7037/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007039 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007041 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007042 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007043garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007044{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007045 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 buf_T *buf;
7048 win_T *wp;
7049 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007050 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007051 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007052 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007053#ifdef FEAT_WINDOWS
7054 tabpage_T *tp;
7055#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007057 if (!testing)
7058 {
7059 /* Only do this once. */
7060 want_garbage_collect = FALSE;
7061 may_garbage_collect = FALSE;
7062 garbage_collect_at_exit = FALSE;
7063 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007064
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007065 /* We advance by two because we add one for items referenced through
7066 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007067 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007068
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 /*
7070 * 1. Go through all accessible variables and mark all lists and dicts
7071 * with copyID.
7072 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007073
7074 /* Don't free variables in the previous_funccal list unless they are only
7075 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007076 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007077 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7078 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7080 NULL);
7081 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7082 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007083 }
7084
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085 /* script-local variables */
7086 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007087 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088
7089 /* buffer-local variables */
7090 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7092 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093
7094 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007095 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7097 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007098#ifdef FEAT_AUTOCMD
7099 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7101 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007102#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007104#ifdef FEAT_WINDOWS
7105 /* tabpage-local variables */
7106 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7108 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007109#endif
7110
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007112 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113
7114 /* function-local variables */
7115 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7116 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007117 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7118 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119 }
7120
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007121 /* function call arguments, if v:testing is set. */
7122 for (i = 0; i < funcargs.ga_len; ++i)
7123 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7124 copyID, NULL, NULL);
7125
Bram Moolenaard812df62008-11-09 12:46:09 +00007126 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007128
Bram Moolenaar1dced572012-04-05 16:54:08 +02007129#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007131#endif
7132
Bram Moolenaardb913952012-06-29 12:54:53 +02007133#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007134 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007135#endif
7136
7137#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007138 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007139#endif
7140
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007141#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007142 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007143 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007144#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007145#ifdef FEAT_NETBEANS_INTG
7146 abort = abort || set_ref_in_nb_channel(copyID);
7147#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007148
Bram Moolenaare3188e22016-05-31 21:13:04 +02007149#ifdef FEAT_TIMERS
7150 abort = abort || set_ref_in_timer(copyID);
7151#endif
7152
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007153 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007154 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007155 /*
7156 * 2. Free lists and dictionaries that are not referenced.
7157 */
7158 did_free = free_unref_items(copyID);
7159
7160 /*
7161 * 3. Check if any funccal can be freed now.
7162 */
7163 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007164 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 if (can_free_funccal(*pfc, copyID))
7166 {
7167 fc = *pfc;
7168 *pfc = fc->caller;
7169 free_funccal(fc, TRUE);
7170 did_free = TRUE;
7171 did_free_funccal = TRUE;
7172 }
7173 else
7174 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007175 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007176 if (did_free_funccal)
7177 /* When a funccal was freed some more items might be garbage
7178 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007179 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007180 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007181 else if (p_verbose > 0)
7182 {
7183 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7184 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007185
7186 return did_free;
7187}
7188
7189/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007190 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007191 */
7192 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007193free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007194{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007195 dict_T *dd, *dd_next;
7196 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007197 int did_free = FALSE;
7198
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007199 /* Let all "free" functions know that we are here. This means no
7200 * dictionaries, lists, channels or jobs are to be freed, because we will
7201 * do that here. */
7202 in_free_unref_items = TRUE;
7203
7204 /*
7205 * PASS 1: free the contents of the items. We don't free the items
7206 * themselves yet, so that it is possible to decrement refcount counters
7207 */
7208
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007209 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007210 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007211 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007212 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007213 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007214 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007215 /* Free the Dictionary and ordinary items it contains, but don't
7216 * recurse into Lists and Dictionaries, they will be in the list
7217 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007218 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007219 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007220 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007221
7222 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007223 * Go through the list of lists and free items without the copyID.
7224 * But don't free a list that has a watcher (used in a for loop), these
7225 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007226 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007227 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7228 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7229 && ll->lv_watch == NULL)
7230 {
7231 /* Free the List and ordinary items it contains, but don't recurse
7232 * into Lists and Dictionaries, they will be in the list of dicts
7233 * or list of lists. */
7234 list_free_contents(ll);
7235 did_free = TRUE;
7236 }
7237
7238#ifdef FEAT_JOB_CHANNEL
7239 /* Go through the list of jobs and free items without the copyID. This
7240 * must happen before doing channels, because jobs refer to channels, but
7241 * the reference from the channel to the job isn't tracked. */
7242 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7243
7244 /* Go through the list of channels and free items without the copyID. */
7245 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7246#endif
7247
7248 /*
7249 * PASS 2: free the items themselves.
7250 */
7251 for (dd = first_dict; dd != NULL; dd = dd_next)
7252 {
7253 dd_next = dd->dv_used_next;
7254 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7255 dict_free_dict(dd);
7256 }
7257
7258 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007259 {
7260 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007261 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7262 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007263 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007264 /* Free the List and ordinary items it contains, but don't recurse
7265 * into Lists and Dictionaries, they will be in the list of dicts
7266 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007267 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007268 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007269 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007270
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007271#ifdef FEAT_JOB_CHANNEL
7272 /* Go through the list of jobs and free items without the copyID. This
7273 * must happen before doing channels, because jobs refer to channels, but
7274 * the reference from the channel to the job isn't tracked. */
7275 free_unused_jobs(copyID, COPYID_MASK);
7276
7277 /* Go through the list of channels and free items without the copyID. */
7278 free_unused_channels(copyID, COPYID_MASK);
7279#endif
7280
7281 in_free_unref_items = FALSE;
7282
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007283 return did_free;
7284}
7285
7286/*
7287 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007288 * "list_stack" is used to add lists to be marked. Can be NULL.
7289 *
7290 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007291 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007293set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007294{
7295 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007296 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007297 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007298 hashtab_T *cur_ht;
7299 ht_stack_T *ht_stack = NULL;
7300 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007301
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007302 cur_ht = ht;
7303 for (;;)
7304 {
7305 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007306 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007307 /* Mark each item in the hashtab. If the item contains a hashtab
7308 * it is added to ht_stack, if it contains a list it is added to
7309 * list_stack. */
7310 todo = (int)cur_ht->ht_used;
7311 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7312 if (!HASHITEM_EMPTY(hi))
7313 {
7314 --todo;
7315 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7316 &ht_stack, list_stack);
7317 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007318 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007319
7320 if (ht_stack == NULL)
7321 break;
7322
7323 /* take an item from the stack */
7324 cur_ht = ht_stack->ht;
7325 tempitem = ht_stack;
7326 ht_stack = ht_stack->prev;
7327 free(tempitem);
7328 }
7329
7330 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007331}
7332
7333/*
7334 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007335 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7336 *
7337 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007338 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007340set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007341{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007342 listitem_T *li;
7343 int abort = FALSE;
7344 list_T *cur_l;
7345 list_stack_T *list_stack = NULL;
7346 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007347
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007348 cur_l = l;
7349 for (;;)
7350 {
7351 if (!abort)
7352 /* Mark each item in the list. If the item contains a hashtab
7353 * it is added to ht_stack, if it contains a list it is added to
7354 * list_stack. */
7355 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7356 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7357 ht_stack, &list_stack);
7358 if (list_stack == NULL)
7359 break;
7360
7361 /* take an item from the stack */
7362 cur_l = list_stack->list;
7363 tempitem = list_stack;
7364 list_stack = list_stack->prev;
7365 free(tempitem);
7366 }
7367
7368 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007369}
7370
7371/*
7372 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007373 * "list_stack" is used to add lists to be marked. Can be NULL.
7374 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7375 *
7376 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007377 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007378 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007379set_ref_in_item(
7380 typval_T *tv,
7381 int copyID,
7382 ht_stack_T **ht_stack,
7383 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007384{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007385 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007386
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007387 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007388 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007389 dict_T *dd = tv->vval.v_dict;
7390
Bram Moolenaara03f2332016-02-06 18:09:59 +01007391 if (dd != NULL && dd->dv_copyID != copyID)
7392 {
7393 /* Didn't see this dict yet. */
7394 dd->dv_copyID = copyID;
7395 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007396 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007397 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7398 }
7399 else
7400 {
7401 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7402 if (newitem == NULL)
7403 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007404 else
7405 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007406 newitem->ht = &dd->dv_hashtab;
7407 newitem->prev = *ht_stack;
7408 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007409 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007410 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007411 }
7412 }
7413 else if (tv->v_type == VAR_LIST)
7414 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007415 list_T *ll = tv->vval.v_list;
7416
Bram Moolenaara03f2332016-02-06 18:09:59 +01007417 if (ll != NULL && ll->lv_copyID != copyID)
7418 {
7419 /* Didn't see this list yet. */
7420 ll->lv_copyID = copyID;
7421 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007422 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007423 abort = set_ref_in_list(ll, copyID, ht_stack);
7424 }
7425 else
7426 {
7427 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007428 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007429 if (newitem == NULL)
7430 abort = TRUE;
7431 else
7432 {
7433 newitem->list = ll;
7434 newitem->prev = *list_stack;
7435 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007436 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007437 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007438 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007439 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007440 else if (tv->v_type == VAR_PARTIAL)
7441 {
7442 partial_T *pt = tv->vval.v_partial;
7443 int i;
7444
7445 /* A partial does not have a copyID, because it cannot contain itself.
7446 */
7447 if (pt != NULL)
7448 {
7449 if (pt->pt_dict != NULL)
7450 {
7451 typval_T dtv;
7452
7453 dtv.v_type = VAR_DICT;
7454 dtv.vval.v_dict = pt->pt_dict;
7455 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7456 }
7457
7458 for (i = 0; i < pt->pt_argc; ++i)
7459 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7460 ht_stack, list_stack);
7461 }
7462 }
7463#ifdef FEAT_JOB_CHANNEL
7464 else if (tv->v_type == VAR_JOB)
7465 {
7466 job_T *job = tv->vval.v_job;
7467 typval_T dtv;
7468
7469 if (job != NULL && job->jv_copyID != copyID)
7470 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007471 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007472 if (job->jv_channel != NULL)
7473 {
7474 dtv.v_type = VAR_CHANNEL;
7475 dtv.vval.v_channel = job->jv_channel;
7476 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7477 }
7478 if (job->jv_exit_partial != NULL)
7479 {
7480 dtv.v_type = VAR_PARTIAL;
7481 dtv.vval.v_partial = job->jv_exit_partial;
7482 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7483 }
7484 }
7485 }
7486 else if (tv->v_type == VAR_CHANNEL)
7487 {
7488 channel_T *ch =tv->vval.v_channel;
7489 int part;
7490 typval_T dtv;
7491 jsonq_T *jq;
7492 cbq_T *cq;
7493
7494 if (ch != NULL && ch->ch_copyID != copyID)
7495 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007496 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007497 for (part = PART_SOCK; part <= PART_IN; ++part)
7498 {
7499 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7500 jq = jq->jq_next)
7501 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7502 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7503 cq = cq->cq_next)
7504 if (cq->cq_partial != NULL)
7505 {
7506 dtv.v_type = VAR_PARTIAL;
7507 dtv.vval.v_partial = cq->cq_partial;
7508 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7509 }
7510 if (ch->ch_part[part].ch_partial != NULL)
7511 {
7512 dtv.v_type = VAR_PARTIAL;
7513 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7514 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7515 }
7516 }
7517 if (ch->ch_partial != NULL)
7518 {
7519 dtv.v_type = VAR_PARTIAL;
7520 dtv.vval.v_partial = ch->ch_partial;
7521 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7522 }
7523 if (ch->ch_close_partial != NULL)
7524 {
7525 dtv.v_type = VAR_PARTIAL;
7526 dtv.vval.v_partial = ch->ch_close_partial;
7527 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7528 }
7529 }
7530 }
7531#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007532 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007533}
7534
7535/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536 * Allocate an empty header for a dictionary.
7537 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007538 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007539dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540{
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007545 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007546 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007547 if (first_dict != NULL)
7548 first_dict->dv_used_prev = d;
7549 d->dv_used_next = first_dict;
7550 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007551 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007552
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007554 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007555 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007556 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007557 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007558 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560}
7561
7562/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007563 * Allocate an empty dict for a return value.
7564 * Returns OK or FAIL.
7565 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007566 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007567rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007568{
7569 dict_T *d = dict_alloc();
7570
7571 if (d == NULL)
7572 return FAIL;
7573
7574 rettv->vval.v_dict = d;
7575 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007576 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007577 ++d->dv_refcount;
7578 return OK;
7579}
7580
7581
7582/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 * Unreference a Dictionary: decrement the reference count and free it when it
7584 * becomes zero.
7585 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007587dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007589 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007590 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591}
7592
7593/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007594 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 * Ignores the reference count.
7596 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007597 static void
7598dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007600 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007602 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007604 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007605 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007606 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007607 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007609 if (!HASHITEM_EMPTY(hi))
7610 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007611 /* Remove the item before deleting it, just in case there is
7612 * something recursive causing trouble. */
7613 di = HI2DI(hi);
7614 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007615 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007616 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007617 --todo;
7618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007620 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007621}
7622
7623 static void
7624dict_free_dict(dict_T *d)
7625{
7626 /* Remove the dict from the list of dicts for garbage collection. */
7627 if (d->dv_used_prev == NULL)
7628 first_dict = d->dv_used_next;
7629 else
7630 d->dv_used_prev->dv_used_next = d->dv_used_next;
7631 if (d->dv_used_next != NULL)
7632 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 vim_free(d);
7634}
7635
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007636 void
7637dict_free(dict_T *d)
7638{
7639 if (!in_free_unref_items)
7640 {
7641 dict_free_contents(d);
7642 dict_free_dict(d);
7643 }
7644}
7645
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646/*
7647 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007648 * The "key" is copied to the new item.
7649 * Note that the value of the item "di_tv" still needs to be initialized!
7650 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007652 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007653dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654{
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007657 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007658 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007659 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007660 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007661 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007662 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664}
7665
7666/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 * Make a copy of a Dictionary item.
7668 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007670dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671{
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007674 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7675 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 if (di != NULL)
7677 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007678 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007679 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007680 copy_tv(&org->di_tv, &di->di_tv);
7681 }
7682 return di;
7683}
7684
7685/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007686 * Remove item "item" from Dictionary "dict" and free it.
7687 */
7688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007689dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007690{
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007694 if (HASHITEM_EMPTY(hi))
7695 EMSG2(_(e_intern2), "dictitem_remove()");
7696 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 dictitem_free(item);
7699}
7700
7701/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 * Free a dict item. Also clears the value.
7703 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007705dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007706{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007708 if (item->di_flags & DI_FLAGS_ALLOC)
7709 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710}
7711
7712/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7714 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007715 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 * Returns NULL when out of memory.
7717 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007719dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007720{
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 dict_T *copy;
7722 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007723 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007724 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007725
7726 if (orig == NULL)
7727 return NULL;
7728
7729 copy = dict_alloc();
7730 if (copy != NULL)
7731 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007732 if (copyID != 0)
7733 {
7734 orig->dv_copyID = copyID;
7735 orig->dv_copydict = copy;
7736 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007737 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007738 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007740 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007742 --todo;
7743
7744 di = dictitem_alloc(hi->hi_key);
7745 if (di == NULL)
7746 break;
7747 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007748 {
7749 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7750 copyID) == FAIL)
7751 {
7752 vim_free(di);
7753 break;
7754 }
7755 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007756 else
7757 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7758 if (dict_add(copy, di) == FAIL)
7759 {
7760 dictitem_free(di);
7761 break;
7762 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007763 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007764 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007765
Bram Moolenaare9a41262005-01-15 22:18:47 +00007766 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007767 if (todo > 0)
7768 {
7769 dict_unref(copy);
7770 copy = NULL;
7771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007772 }
7773
7774 return copy;
7775}
7776
7777/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007778 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007779 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007780 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007781 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007782dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007783{
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785}
7786
Bram Moolenaar8c711452005-01-14 21:53:12 +00007787/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007788 * Add a number or string entry to dictionary "d".
7789 * When "str" is NULL use number "nr", otherwise use "str".
7790 * Returns FAIL when out of memory and when key already exists.
7791 */
7792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007793dict_add_nr_str(
7794 dict_T *d,
7795 char *key,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007796 varnumber_T nr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007797 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007798{
7799 dictitem_T *item;
7800
7801 item = dictitem_alloc((char_u *)key);
7802 if (item == NULL)
7803 return FAIL;
7804 item->di_tv.v_lock = 0;
7805 if (str == NULL)
7806 {
7807 item->di_tv.v_type = VAR_NUMBER;
7808 item->di_tv.vval.v_number = nr;
7809 }
7810 else
7811 {
7812 item->di_tv.v_type = VAR_STRING;
7813 item->di_tv.vval.v_string = vim_strsave(str);
7814 }
7815 if (dict_add(d, item) == FAIL)
7816 {
7817 dictitem_free(item);
7818 return FAIL;
7819 }
7820 return OK;
7821}
7822
7823/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007824 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007825 * Returns FAIL when out of memory and when key already exists.
7826 */
7827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007829{
7830 dictitem_T *item;
7831
7832 item = dictitem_alloc((char_u *)key);
7833 if (item == NULL)
7834 return FAIL;
7835 item->di_tv.v_lock = 0;
7836 item->di_tv.v_type = VAR_LIST;
7837 item->di_tv.vval.v_list = list;
7838 if (dict_add(d, item) == FAIL)
7839 {
7840 dictitem_free(item);
7841 return FAIL;
7842 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007843 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007844 return OK;
7845}
7846
7847/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007848 * Get the number of items in a Dictionary.
7849 */
7850 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007851dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007852{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853 if (d == NULL)
7854 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007855 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856}
7857
7858/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007859 * Find item "key[len]" in Dictionary "d".
7860 * If "len" is negative use strlen(key).
7861 * Returns NULL when not found.
7862 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007863 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007864dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007865{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007866#define AKEYLEN 200
7867 char_u buf[AKEYLEN];
7868 char_u *akey;
7869 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007871
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007872 if (d == NULL)
7873 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007874 if (len < 0)
7875 akey = key;
7876 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007877 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007878 tofree = akey = vim_strnsave(key, len);
7879 if (akey == NULL)
7880 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007881 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007882 else
7883 {
7884 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007885 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007886 akey = buf;
7887 }
7888
Bram Moolenaar33570922005-01-25 22:26:29 +00007889 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007890 vim_free(tofree);
7891 if (HASHITEM_EMPTY(hi))
7892 return NULL;
7893 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007894}
7895
7896/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007897 * Get a string item from a dictionary.
7898 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007899 * Returns NULL if the entry doesn't exist or out of memory.
7900 */
7901 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007902get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007903{
7904 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007905 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007906
7907 di = dict_find(d, key, -1);
7908 if (di == NULL)
7909 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007910 s = get_tv_string(&di->di_tv);
7911 if (save && s != NULL)
7912 s = vim_strsave(s);
7913 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007914}
7915
7916/*
7917 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007918 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007919 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007920 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007921get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007922{
7923 dictitem_T *di;
7924
7925 di = dict_find(d, key, -1);
7926 if (di == NULL)
7927 return 0;
7928 return get_tv_number(&di->di_tv);
7929}
7930
7931/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 * Return an allocated string with the string representation of a Dictionary.
7933 * May return NULL.
7934 */
7935 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007936dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007937{
7938 garray_T ga;
7939 int first = TRUE;
7940 char_u *tofree;
7941 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007945 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007947 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 return NULL;
7949 ga_init2(&ga, (int)sizeof(char), 80);
7950 ga_append(&ga, '{');
7951
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007952 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007953 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007955 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007957 --todo;
7958
7959 if (first)
7960 first = FALSE;
7961 else
7962 ga_concat(&ga, (char_u *)", ");
7963
7964 tofree = string_quote(hi->hi_key, FALSE);
7965 if (tofree != NULL)
7966 {
7967 ga_concat(&ga, tofree);
7968 vim_free(tofree);
7969 }
7970 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007971 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7972 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007973 if (s != NULL)
7974 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007975 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007976 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007977 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007978 line_breakcheck();
7979
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007982 if (todo > 0)
7983 {
7984 vim_free(ga.ga_data);
7985 return NULL;
7986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007987
7988 ga_append(&ga, '}');
7989 ga_append(&ga, NUL);
7990 return (char_u *)ga.ga_data;
7991}
7992
7993/*
7994 * Allocate a variable for a Dictionary and fill it from "*arg".
7995 * Return OK or FAIL. Returns NOTDONE for {expr}.
7996 */
7997 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007998get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007999{
Bram Moolenaar33570922005-01-25 22:26:29 +00008000 dict_T *d = NULL;
8001 typval_T tvkey;
8002 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00008003 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00008004 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008005 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008006 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00008007
8008 /*
8009 * First check if it's not a curly-braces thing: {expr}.
8010 * Must do this without evaluating, otherwise a function may be called
8011 * twice. Unfortunately this means we need to call eval1() twice for the
8012 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008013 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008014 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008015 if (*start != '}')
8016 {
8017 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
8018 return FAIL;
8019 if (*start == '}')
8020 return NOTDONE;
8021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008022
8023 if (evaluate)
8024 {
8025 d = dict_alloc();
8026 if (d == NULL)
8027 return FAIL;
8028 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008029 tvkey.v_type = VAR_UNKNOWN;
8030 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008031
8032 *arg = skipwhite(*arg + 1);
8033 while (**arg != '}' && **arg != NUL)
8034 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008035 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008036 goto failret;
8037 if (**arg != ':')
8038 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008039 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008040 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008041 goto failret;
8042 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008043 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008044 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008045 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008046 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008047 {
8048 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008049 clear_tv(&tvkey);
8050 goto failret;
8051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008052 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053
8054 *arg = skipwhite(*arg + 1);
8055 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8056 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008057 if (evaluate)
8058 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008059 goto failret;
8060 }
8061 if (evaluate)
8062 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008063 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008064 if (item != NULL)
8065 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008066 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008067 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068 clear_tv(&tv);
8069 goto failret;
8070 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008071 item = dictitem_alloc(key);
8072 clear_tv(&tvkey);
8073 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008074 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008075 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008076 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008077 if (dict_add(d, item) == FAIL)
8078 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008079 }
8080 }
8081
8082 if (**arg == '}')
8083 break;
8084 if (**arg != ',')
8085 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008086 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008087 goto failret;
8088 }
8089 *arg = skipwhite(*arg + 1);
8090 }
8091
8092 if (**arg != '}')
8093 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008094 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008095failret:
8096 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008097 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008098 return FAIL;
8099 }
8100
8101 *arg = skipwhite(*arg + 1);
8102 if (evaluate)
8103 {
8104 rettv->v_type = VAR_DICT;
8105 rettv->vval.v_dict = d;
8106 ++d->dv_refcount;
8107 }
8108
8109 return OK;
8110}
8111
Bram Moolenaar17a13432016-01-24 14:22:10 +01008112 static char *
8113get_var_special_name(int nr)
8114{
8115 switch (nr)
8116 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008117 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008118 case VVAL_TRUE: return "v:true";
8119 case VVAL_NONE: return "v:none";
8120 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008121 }
8122 EMSG2(_(e_intern2), "get_var_special_name()");
8123 return "42";
8124}
8125
Bram Moolenaar8c711452005-01-14 21:53:12 +00008126/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008127 * Return a string with the string representation of a variable.
8128 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008129 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008130 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008131 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8132 * "string()", otherwise does not put quotes around strings, as ":echo"
8133 * displays values.
8134 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8135 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008136 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008137 */
8138 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008139echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008140 typval_T *tv,
8141 char_u **tofree,
8142 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008143 int copyID,
8144 int echo_style,
8145 int restore_copyID,
8146 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008147{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008148 static int recurse = 0;
8149 char_u *r = NULL;
8150
Bram Moolenaar33570922005-01-25 22:26:29 +00008151 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008152 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008153 if (!did_echo_string_emsg)
8154 {
8155 /* Only give this message once for a recursive call to avoid
8156 * flooding the user with errors. And stop iterating over lists
8157 * and dicts. */
8158 did_echo_string_emsg = TRUE;
8159 EMSG(_("E724: variable nested too deep for displaying"));
8160 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008161 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008162 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163 }
8164 ++recurse;
8165
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008166 switch (tv->v_type)
8167 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008168 case VAR_STRING:
8169 if (echo_style && !dict_val)
8170 {
8171 *tofree = NULL;
8172 r = get_tv_string_buf(tv, numbuf);
8173 }
8174 else
8175 {
8176 *tofree = string_quote(tv->vval.v_string, FALSE);
8177 r = *tofree;
8178 }
8179 break;
8180
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008181 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008182 if (echo_style)
8183 {
8184 *tofree = NULL;
8185 r = tv->vval.v_string;
8186 }
8187 else
8188 {
8189 *tofree = string_quote(tv->vval.v_string, TRUE);
8190 r = *tofree;
8191 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008192 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008193
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008194 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008195 {
8196 partial_T *pt = tv->vval.v_partial;
8197 char_u *fname = string_quote(pt == NULL ? NULL
8198 : pt->pt_name, FALSE);
8199 garray_T ga;
8200 int i;
8201 char_u *tf;
8202
8203 ga_init2(&ga, 1, 100);
8204 ga_concat(&ga, (char_u *)"function(");
8205 if (fname != NULL)
8206 {
8207 ga_concat(&ga, fname);
8208 vim_free(fname);
8209 }
8210 if (pt != NULL && pt->pt_argc > 0)
8211 {
8212 ga_concat(&ga, (char_u *)", [");
8213 for (i = 0; i < pt->pt_argc; ++i)
8214 {
8215 if (i > 0)
8216 ga_concat(&ga, (char_u *)", ");
8217 ga_concat(&ga,
8218 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8219 vim_free(tf);
8220 }
8221 ga_concat(&ga, (char_u *)"]");
8222 }
8223 if (pt != NULL && pt->pt_dict != NULL)
8224 {
8225 typval_T dtv;
8226
8227 ga_concat(&ga, (char_u *)", ");
8228 dtv.v_type = VAR_DICT;
8229 dtv.vval.v_dict = pt->pt_dict;
8230 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8231 vim_free(tf);
8232 }
8233 ga_concat(&ga, (char_u *)")");
8234
8235 *tofree = ga.ga_data;
8236 r = *tofree;
8237 break;
8238 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008239
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008241 if (tv->vval.v_list == NULL)
8242 {
8243 *tofree = NULL;
8244 r = NULL;
8245 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008246 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8247 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008248 {
8249 *tofree = NULL;
8250 r = (char_u *)"[...]";
8251 }
8252 else
8253 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008254 int old_copyID = tv->vval.v_list->lv_copyID;
8255
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008256 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008257 *tofree = list2string(tv, copyID, restore_copyID);
8258 if (restore_copyID)
8259 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008260 r = *tofree;
8261 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008262 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008263
Bram Moolenaar8c711452005-01-14 21:53:12 +00008264 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008265 if (tv->vval.v_dict == NULL)
8266 {
8267 *tofree = NULL;
8268 r = NULL;
8269 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008270 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8271 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008272 {
8273 *tofree = NULL;
8274 r = (char_u *)"{...}";
8275 }
8276 else
8277 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008278 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008279 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008280 *tofree = dict2string(tv, copyID, restore_copyID);
8281 if (restore_copyID)
8282 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008283 r = *tofree;
8284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008285 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008286
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008288 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008289 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008290 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008291 *tofree = NULL;
8292 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008293 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008294
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008295 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008296#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008297 *tofree = NULL;
8298 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8299 r = numbuf;
8300 break;
8301#endif
8302
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008303 case VAR_SPECIAL:
8304 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008305 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008306 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008307 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008308
Bram Moolenaar8502c702014-06-17 12:51:16 +02008309 if (--recurse == 0)
8310 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008311 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008312}
8313
8314/*
8315 * Return a string with the string representation of a variable.
8316 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8317 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008318 * Does not put quotes around strings, as ":echo" displays values.
8319 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8320 * May return NULL.
8321 */
8322 static char_u *
8323echo_string(
8324 typval_T *tv,
8325 char_u **tofree,
8326 char_u *numbuf,
8327 int copyID)
8328{
8329 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8330}
8331
8332/*
8333 * Return a string with the string representation of a variable.
8334 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8335 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008336 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008337 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008338 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008340tv2string(
8341 typval_T *tv,
8342 char_u **tofree,
8343 char_u *numbuf,
8344 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008345{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008346 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008347}
8348
8349/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 * Return string "str" in ' quotes, doubling ' characters.
8351 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008352 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008353 */
8354 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008355string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008356{
Bram Moolenaar33570922005-01-25 22:26:29 +00008357 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008358 char_u *p, *r, *s;
8359
Bram Moolenaar33570922005-01-25 22:26:29 +00008360 len = (function ? 13 : 3);
8361 if (str != NULL)
8362 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008363 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008364 for (p = str; *p != NUL; mb_ptr_adv(p))
8365 if (*p == '\'')
8366 ++len;
8367 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008368 s = r = alloc(len);
8369 if (r != NULL)
8370 {
8371 if (function)
8372 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008373 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008374 r += 10;
8375 }
8376 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008377 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008378 if (str != NULL)
8379 for (p = str; *p != NUL; )
8380 {
8381 if (*p == '\'')
8382 *r++ = '\'';
8383 MB_COPY_CHAR(p, r);
8384 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008385 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008386 if (function)
8387 *r++ = ')';
8388 *r++ = NUL;
8389 }
8390 return s;
8391}
8392
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008393#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008394/*
8395 * Convert the string "text" to a floating point number.
8396 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8397 * this always uses a decimal point.
8398 * Returns the length of the text that was consumed.
8399 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008401string2float(
8402 char_u *text,
8403 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404{
8405 char *s = (char *)text;
8406 float_T f;
8407
8408 f = strtod(s, &s);
8409 *value = f;
8410 return (int)((char_u *)s - text);
8411}
8412#endif
8413
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 * Get the value of an environment variable.
8416 * "arg" is pointing to the '$'. It is advanced to after the name.
8417 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008418 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 */
8420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008421get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
8423 char_u *string = NULL;
8424 int len;
8425 int cc;
8426 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008427 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
8429 ++*arg;
8430 name = *arg;
8431 len = get_env_len(arg);
8432 if (evaluate)
8433 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008434 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008435 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008436
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008437 cc = name[len];
8438 name[len] = NUL;
8439 /* first try vim_getenv(), fast for normal environment vars */
8440 string = vim_getenv(name, &mustfree);
8441 if (string != NULL && *string != NUL)
8442 {
8443 if (!mustfree)
8444 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008446 else
8447 {
8448 if (mustfree)
8449 vim_free(string);
8450
8451 /* next try expanding things like $VIM and ${HOME} */
8452 string = expand_env_save(name - 1);
8453 if (string != NULL && *string == '$')
8454 {
8455 vim_free(string);
8456 string = NULL;
8457 }
8458 }
8459 name[len] = cc;
8460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008461 rettv->v_type = VAR_STRING;
8462 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 }
8464
8465 return OK;
8466}
8467
8468/*
8469 * Array with names and number of arguments of all internal functions
8470 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8471 */
8472static struct fst
8473{
8474 char *f_name; /* function name */
8475 char f_min_argc; /* minimal number of arguments */
8476 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008477 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008478 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479} functions[] =
8480{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008481#ifdef FEAT_FLOAT
8482 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008483 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008484#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008485 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008486 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {"append", 2, 2, f_append},
8488 {"argc", 0, 0, f_argc},
8489 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008490 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008491 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008492#ifdef FEAT_FLOAT
8493 {"asin", 1, 1, f_asin}, /* WJMc */
8494#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008495 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008496 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008497 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008498 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008499 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008500 {"assert_notequal", 2, 3, f_assert_notequal},
8501 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008502 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008503#ifdef FEAT_FLOAT
8504 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008505 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008508 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"bufexists", 1, 1, f_bufexists},
8510 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8511 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8512 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8513 {"buflisted", 1, 1, f_buflisted},
8514 {"bufloaded", 1, 1, f_bufloaded},
8515 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008516 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008517 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {"bufwinnr", 1, 1, f_bufwinnr},
8519 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008520 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008521 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008522 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008523#ifdef FEAT_FLOAT
8524 {"ceil", 1, 1, f_ceil},
8525#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008526#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008527 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008528 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8529 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008530 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008531 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008532 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008533 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008534 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008535 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008536 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008537 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008538 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8539 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008540 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008541 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008542#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008543 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008544 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008546 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008548#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008549 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008550 {"complete_add", 1, 1, f_complete_add},
8551 {"complete_check", 0, 0, f_complete_check},
8552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008554 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008555#ifdef FEAT_FLOAT
8556 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008557 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008558#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008559 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008561 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008562 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008563 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008565 {"diff_filler", 1, 1, f_diff_filler},
8566 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008567 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008569 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 {"eventhandler", 0, 0, f_eventhandler},
8571 {"executable", 1, 1, f_executable},
Bram Moolenaar79815f12016-07-09 17:07:29 +02008572 {"execute", 1, 2, f_execute},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008573 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008575#ifdef FEAT_FLOAT
8576 {"exp", 1, 1, f_exp},
8577#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008578 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008579 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008580 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8582 {"filereadable", 1, 1, f_filereadable},
8583 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008584 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008585 {"finddir", 1, 3, f_finddir},
8586 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008587#ifdef FEAT_FLOAT
8588 {"float2nr", 1, 1, f_float2nr},
8589 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008590 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008591#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008592 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 {"fnamemodify", 2, 2, f_fnamemodify},
8594 {"foldclosed", 1, 1, f_foldclosed},
8595 {"foldclosedend", 1, 1, f_foldclosedend},
8596 {"foldlevel", 1, 1, f_foldlevel},
8597 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008598 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008600 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008601 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008602 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008603 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008604 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 {"getchar", 0, 1, f_getchar},
8606 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008607 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 {"getcmdline", 0, 0, f_getcmdline},
8609 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008610 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008611 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02008612#if defined(FEAT_CMDL_COMPL)
8613 {"getcompletion", 2, 2, f_getcompletion},
8614#endif
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008615 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008616 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008617 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008618 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 {"getfsize", 1, 1, f_getfsize},
8620 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008621 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008622 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008623 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008624 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008625 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008626 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008627 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008628 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008630 {"gettabvar", 2, 3, f_gettabvar},
8631 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {"getwinposx", 0, 0, f_getwinposx},
8633 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008634 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008635 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008636 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008637 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008639 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008640 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008641 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8643 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8644 {"histadd", 2, 2, f_histadd},
8645 {"histdel", 1, 2, f_histdel},
8646 {"histget", 1, 2, f_histget},
8647 {"histnr", 1, 1, f_histnr},
8648 {"hlID", 1, 1, f_hlID},
8649 {"hlexists", 1, 1, f_hlexists},
8650 {"hostname", 0, 0, f_hostname},
8651 {"iconv", 3, 3, f_iconv},
8652 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008653 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008654 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008656 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {"inputrestore", 0, 0, f_inputrestore},
8658 {"inputsave", 0, 0, f_inputsave},
8659 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008660 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008661 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008663 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008664#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8665 {"isnan", 1, 1, f_isnan},
8666#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008667 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008668#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008669 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008670 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008671 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008672 {"job_start", 1, 2, f_job_start},
8673 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008674 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008675#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008676 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008677 {"js_decode", 1, 1, f_js_decode},
8678 {"js_encode", 1, 1, f_js_encode},
8679 {"json_decode", 1, 1, f_json_decode},
8680 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008681 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008683 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 {"libcall", 3, 3, f_libcall},
8685 {"libcallnr", 3, 3, f_libcallnr},
8686 {"line", 1, 1, f_line},
8687 {"line2byte", 1, 1, f_line2byte},
8688 {"lispindent", 1, 1, f_lispindent},
8689 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008690#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008691 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008692 {"log10", 1, 1, f_log10},
8693#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008694#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008695 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008696#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008697 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008698 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008699 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008700 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008701 {"matchadd", 2, 5, f_matchadd},
8702 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008703 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008704 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008705 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008706 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008707 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008708 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008709 {"max", 1, 1, f_max},
8710 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008711#ifdef vim_mkdir
8712 {"mkdir", 1, 3, f_mkdir},
8713#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008714 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008715#ifdef FEAT_MZSCHEME
8716 {"mzeval", 1, 1, f_mzeval},
8717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008719 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008720 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008721 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008722#ifdef FEAT_PERL
8723 {"perleval", 1, 1, f_perleval},
8724#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008725#ifdef FEAT_FLOAT
8726 {"pow", 2, 2, f_pow},
8727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008729 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008730 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008731#ifdef FEAT_PYTHON3
8732 {"py3eval", 1, 1, f_py3eval},
8733#endif
8734#ifdef FEAT_PYTHON
8735 {"pyeval", 1, 1, f_pyeval},
8736#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008737 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008738 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008739 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008740#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008741 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008742#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008743 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {"remote_expr", 2, 3, f_remote_expr},
8745 {"remote_foreground", 1, 1, f_remote_foreground},
8746 {"remote_peek", 1, 2, f_remote_peek},
8747 {"remote_read", 1, 1, f_remote_read},
8748 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008749 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008751 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008753 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008754#ifdef FEAT_FLOAT
8755 {"round", 1, 1, f_round},
8756#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008757 {"screenattr", 2, 2, f_screenattr},
8758 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008759 {"screencol", 0, 0, f_screencol},
8760 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008761 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008762 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008763 {"searchpair", 3, 7, f_searchpair},
8764 {"searchpairpos", 3, 7, f_searchpairpos},
8765 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {"server2client", 2, 2, f_server2client},
8767 {"serverlist", 0, 0, f_serverlist},
8768 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008769 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008771 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008773 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008774 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008775 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008776 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008778 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008779 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008781#ifdef FEAT_CRYPT
8782 {"sha256", 1, 1, f_sha256},
8783#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008784 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008785 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008787#ifdef FEAT_FLOAT
8788 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008789 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008790#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008791 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008792 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008793 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008794 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008795 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008796#ifdef FEAT_FLOAT
8797 {"sqrt", 1, 1, f_sqrt},
8798 {"str2float", 1, 1, f_str2float},
8799#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008800 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008801 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008802 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008803 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804#ifdef HAVE_STRFTIME
8805 {"strftime", 1, 2, f_strftime},
8806#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008807 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008808 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008809 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {"strlen", 1, 1, f_strlen},
8811 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008812 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008814 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008815 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 {"substitute", 4, 4, f_substitute},
8817 {"synID", 3, 3, f_synID},
8818 {"synIDattr", 2, 3, f_synIDattr},
8819 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008820 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008821 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008822 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008823 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008824 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008825 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008826 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008827 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008828 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008829#ifdef FEAT_FLOAT
8830 {"tan", 1, 1, f_tan},
8831 {"tanh", 1, 1, f_tanh},
8832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008834 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8835 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008836 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8837#ifdef FEAT_JOB_CHANNEL
8838 {"test_null_channel", 0, 0, f_test_null_channel},
8839#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008840 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008841#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008842 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008843#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008844 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008845 {"test_null_partial", 0, 0, f_test_null_partial},
8846 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008847 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008848#ifdef FEAT_TIMERS
8849 {"timer_start", 2, 3, f_timer_start},
8850 {"timer_stop", 1, 1, f_timer_stop},
8851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {"tolower", 1, 1, f_tolower},
8853 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008854 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008855#ifdef FEAT_FLOAT
8856 {"trunc", 1, 1, f_trunc},
8857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008859 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008860 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008861 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008862 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 {"virtcol", 1, 1, f_virtcol},
8864 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008865 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008866 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008867 {"win_getid", 0, 2, f_win_getid},
8868 {"win_gotoid", 1, 1, f_win_gotoid},
8869 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8870 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 {"winbufnr", 1, 1, f_winbufnr},
8872 {"wincol", 0, 0, f_wincol},
8873 {"winheight", 1, 1, f_winheight},
8874 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008875 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008877 {"winrestview", 1, 1, f_winrestview},
8878 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008880 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008881 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008882 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883};
8884
8885#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8886
8887/*
8888 * Function given to ExpandGeneric() to obtain the list of internal
8889 * or user defined function names.
8890 */
8891 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008892get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893{
8894 static int intidx = -1;
8895 char_u *name;
8896
8897 if (idx == 0)
8898 intidx = -1;
8899 if (intidx < 0)
8900 {
8901 name = get_user_func_name(xp, idx);
8902 if (name != NULL)
8903 return name;
8904 }
8905 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8906 {
8907 STRCPY(IObuff, functions[intidx].f_name);
8908 STRCAT(IObuff, "(");
8909 if (functions[intidx].f_max_argc == 0)
8910 STRCAT(IObuff, ")");
8911 return IObuff;
8912 }
8913
8914 return NULL;
8915}
8916
8917/*
8918 * Function given to ExpandGeneric() to obtain the list of internal or
8919 * user defined variable or function names.
8920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008922get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
8924 static int intidx = -1;
8925 char_u *name;
8926
8927 if (idx == 0)
8928 intidx = -1;
8929 if (intidx < 0)
8930 {
8931 name = get_function_name(xp, idx);
8932 if (name != NULL)
8933 return name;
8934 }
8935 return get_user_var_name(xp, ++intidx);
8936}
8937
8938#endif /* FEAT_CMDL_COMPL */
8939
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008940#if defined(EBCDIC) || defined(PROTO)
8941/*
8942 * Compare struct fst by function name.
8943 */
8944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008945compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008946{
8947 struct fst *p1 = (struct fst *)s1;
8948 struct fst *p2 = (struct fst *)s2;
8949
8950 return STRCMP(p1->f_name, p2->f_name);
8951}
8952
8953/*
8954 * Sort the function table by function name.
8955 * The sorting of the table above is ASCII dependant.
8956 * On machines using EBCDIC we have to sort it.
8957 */
8958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008959sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008960{
8961 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8962
8963 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8964}
8965#endif
8966
8967
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968/*
8969 * Find internal function in table above.
8970 * Return index, or -1 if not found
8971 */
8972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008973find_internal_func(
8974 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975{
8976 int first = 0;
8977 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8978 int cmp;
8979 int x;
8980
8981 /*
8982 * Find the function name in the table. Binary search.
8983 */
8984 while (first <= last)
8985 {
8986 x = first + ((unsigned)(last - first) >> 1);
8987 cmp = STRCMP(name, functions[x].f_name);
8988 if (cmp < 0)
8989 last = x - 1;
8990 else if (cmp > 0)
8991 first = x + 1;
8992 else
8993 return x;
8994 }
8995 return -1;
8996}
8997
8998/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008999 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
9000 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01009001 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
9002 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009003 */
9004 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01009005deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009006{
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009008 int cc;
9009
Bram Moolenaar65639032016-03-16 21:40:30 +01009010 if (partialp != NULL)
9011 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009012
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009013 cc = name[*lenp];
9014 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01009015 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009018 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 {
9021 *lenp = 0;
9022 return (char_u *)""; /* just in case */
9023 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009024 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00009025 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009026 }
9027
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009028 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9029 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009030 partial_T *pt = v->di_tv.vval.v_partial;
9031
9032 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009033 {
9034 *lenp = 0;
9035 return (char_u *)""; /* just in case */
9036 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009037 if (partialp != NULL)
9038 *partialp = pt;
9039 *lenp = (int)STRLEN(pt->pt_name);
9040 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009041 }
9042
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009043 return name;
9044}
9045
9046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 * Allocate a variable for the result of a function.
9048 * Return OK or FAIL.
9049 */
9050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009051get_func_tv(
9052 char_u *name, /* name of the function */
9053 int len, /* length of "name" */
9054 typval_T *rettv,
9055 char_u **arg, /* argument, pointing to the '(' */
9056 linenr_T firstline, /* first line of range */
9057 linenr_T lastline, /* last line of range */
9058 int *doesrange, /* return: function handled range */
9059 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009060 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009061 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063 char_u *argp;
9064 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009065 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 int argcount = 0; /* number of arguments found */
9067
9068 /*
9069 * Get the arguments.
9070 */
9071 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009072 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 {
9074 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9075 if (*argp == ')' || *argp == ',' || *argp == NUL)
9076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9078 {
9079 ret = FAIL;
9080 break;
9081 }
9082 ++argcount;
9083 if (*argp != ',')
9084 break;
9085 }
9086 if (*argp == ')')
9087 ++argp;
9088 else
9089 ret = FAIL;
9090
9091 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009092 {
9093 int i = 0;
9094
9095 if (get_vim_var_nr(VV_TESTING))
9096 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009097 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009098 * what variables are used on the call stack. */
9099 if (funcargs.ga_itemsize == 0)
9100 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9101 for (i = 0; i < argcount; ++i)
9102 if (ga_grow(&funcargs, 1) == OK)
9103 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9104 &argvars[i];
9105 }
9106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009108 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009109
9110 funcargs.ga_len -= i;
9111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009113 {
9114 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009115 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009116 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009117 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119
9120 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122
9123 *arg = skipwhite(argp);
9124 return ret;
9125}
9126
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009127#define ERROR_UNKNOWN 0
9128#define ERROR_TOOMANY 1
9129#define ERROR_TOOFEW 2
9130#define ERROR_SCRIPT 3
9131#define ERROR_DICT 4
9132#define ERROR_NONE 5
9133#define ERROR_OTHER 6
9134#define FLEN_FIXED 40
9135
9136/*
9137 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9138 * Change <SNR>123_name() to K_SNR 123_name().
9139 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9140 * (slow).
9141 */
9142 static char_u *
9143fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9144{
9145 int llen;
9146 char_u *fname;
9147 int i;
9148
9149 llen = eval_fname_script(name);
9150 if (llen > 0)
9151 {
9152 fname_buf[0] = K_SPECIAL;
9153 fname_buf[1] = KS_EXTRA;
9154 fname_buf[2] = (int)KE_SNR;
9155 i = 3;
9156 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9157 {
9158 if (current_SID <= 0)
9159 *error = ERROR_SCRIPT;
9160 else
9161 {
9162 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9163 i = (int)STRLEN(fname_buf);
9164 }
9165 }
9166 if (i + STRLEN(name + llen) < FLEN_FIXED)
9167 {
9168 STRCPY(fname_buf + i, name + llen);
9169 fname = fname_buf;
9170 }
9171 else
9172 {
9173 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9174 if (fname == NULL)
9175 *error = ERROR_OTHER;
9176 else
9177 {
9178 *tofree = fname;
9179 mch_memmove(fname, fname_buf, (size_t)i);
9180 STRCPY(fname + i, name + llen);
9181 }
9182 }
9183 }
9184 else
9185 fname = name;
9186 return fname;
9187}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188
9189/*
9190 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009191 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009192 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009194 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009195call_func(
9196 char_u *funcname, /* name of the function */
9197 int len, /* length of "name" */
9198 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009199 int argcount_in, /* number of "argvars" */
9200 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009201 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009202 linenr_T firstline, /* first line of range */
9203 linenr_T lastline, /* last line of range */
9204 int *doesrange, /* return: function handled range */
9205 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009206 partial_T *partial, /* optional, can be NULL */
9207 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
9209 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 int error = ERROR_NONE;
9211 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009214 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009216 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009217 int argcount = argcount_in;
9218 typval_T *argvars = argvars_in;
9219 dict_T *selfdict = selfdict_in;
9220 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9221 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009222
9223 /* Make a copy of the name, if it comes from a funcref variable it could
9224 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009225 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009226 if (name == NULL)
9227 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009229 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
9231 *doesrange = FALSE;
9232
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009233 if (partial != NULL)
9234 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009235 /* When the function has a partial with a dict and there is a dict
9236 * argument, use the dict argument. That is backwards compatible.
9237 * When the dict was bound explicitly use the one from the partial. */
9238 if (partial->pt_dict != NULL
9239 && (selfdict_in == NULL || !partial->pt_auto))
9240 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009241 if (error == ERROR_NONE && partial->pt_argc > 0)
9242 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009243 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9244 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9245 for (i = 0; i < argcount_in; ++i)
9246 argv[i + argv_clear] = argvars_in[i];
9247 argvars = argv;
9248 argcount = partial->pt_argc + argcount_in;
9249 }
9250 }
9251
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252
9253 /* execute the function if no errors detected and executing */
9254 if (evaluate && error == ERROR_NONE)
9255 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009256 char_u *rfname = fname;
9257
9258 /* Ignore "g:" before a function name. */
9259 if (fname[0] == 'g' && fname[1] == ':')
9260 rfname = fname + 2;
9261
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009262 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9263 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 error = ERROR_UNKNOWN;
9265
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009266 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 {
9268 /*
9269 * User defined function.
9270 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009271 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009274 /* Trigger FuncUndefined event, may load the function. */
9275 if (fp == NULL
9276 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009277 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009278 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009280 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009281 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 }
9283#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009284 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009285 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009286 {
9287 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009288 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009289 }
9290
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 if (fp != NULL)
9292 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009293 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009295 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009297 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009299 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009300 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 else
9302 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009303 int did_save_redo = FALSE;
9304
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 /*
9306 * Call the user function.
9307 * Save and restore search patterns, script variables and
9308 * redo buffer.
9309 */
9310 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009311#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009312 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009313#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009314 {
9315 saveRedobuff();
9316 did_save_redo = TRUE;
9317 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009318 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009320 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009321 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9322 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9323 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009324 /* Function was unreferenced while being used, free it
9325 * now. */
9326 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009327 if (did_save_redo)
9328 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 restore_search_patterns();
9330 error = ERROR_NONE;
9331 }
9332 }
9333 }
9334 else
9335 {
9336 /*
9337 * Find the function name in the table, call its implementation.
9338 */
9339 i = find_internal_func(fname);
9340 if (i >= 0)
9341 {
9342 if (argcount < functions[i].f_min_argc)
9343 error = ERROR_TOOFEW;
9344 else if (argcount > functions[i].f_max_argc)
9345 error = ERROR_TOOMANY;
9346 else
9347 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009348 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 error = ERROR_NONE;
9351 }
9352 }
9353 }
9354 /*
9355 * The function call (or "FuncUndefined" autocommand sequence) might
9356 * have been aborted by an error, an interrupt, or an explicitly thrown
9357 * exception that has not been caught so far. This situation can be
9358 * tested for by calling aborting(). For an error in an internal
9359 * function or for the "E132" error in call_user_func(), however, the
9360 * throw point at which the "force_abort" flag (temporarily reset by
9361 * emsg()) is normally updated has not been reached yet. We need to
9362 * update that flag first to make aborting() reliable.
9363 */
9364 update_force_abort();
9365 }
9366 if (error == ERROR_NONE)
9367 ret = OK;
9368
9369 /*
9370 * Report an error unless the argument evaluation or function call has been
9371 * cancelled due to an aborting error, an interrupt, or an exception.
9372 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009373 if (!aborting())
9374 {
9375 switch (error)
9376 {
9377 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009378 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009379 break;
9380 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009381 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009382 break;
9383 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009384 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009385 name);
9386 break;
9387 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009388 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009389 name);
9390 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009391 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009392 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009393 name);
9394 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009395 }
9396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009398 while (argv_clear > 0)
9399 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009400 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009401 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402
9403 return ret;
9404}
9405
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009406/*
9407 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009408 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009409 */
9410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009411emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009412{
9413 char_u *p;
9414
9415 if (*name == K_SPECIAL)
9416 p = concat_str((char_u *)"<SNR>", name + 3);
9417 else
9418 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009419 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009420 if (p != name)
9421 vim_free(p);
9422}
9423
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009424/*
9425 * Return TRUE for a non-zero Number and a non-empty String.
9426 */
9427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009428non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009429{
9430 return ((argvars[0].v_type == VAR_NUMBER
9431 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02009432 || (argvars[0].v_type == VAR_SPECIAL
9433 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009434 || (argvars[0].v_type == VAR_STRING
9435 && argvars[0].vval.v_string != NULL
9436 && *argvars[0].vval.v_string != NUL));
9437}
9438
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439/*********************************************
9440 * Implementation of the built-in functions
9441 */
9442
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009443#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009444static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009445
9446/*
9447 * Get the float value of "argvars[0]" into "f".
9448 * Returns FAIL when the argument is not a Number or Float.
9449 */
9450 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009451get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009452{
9453 if (argvars[0].v_type == VAR_FLOAT)
9454 {
9455 *f = argvars[0].vval.v_float;
9456 return OK;
9457 }
9458 if (argvars[0].v_type == VAR_NUMBER)
9459 {
9460 *f = (float_T)argvars[0].vval.v_number;
9461 return OK;
9462 }
9463 EMSG(_("E808: Number or Float required"));
9464 return FAIL;
9465}
9466
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009467/*
9468 * "abs(expr)" function
9469 */
9470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009471f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009472{
9473 if (argvars[0].v_type == VAR_FLOAT)
9474 {
9475 rettv->v_type = VAR_FLOAT;
9476 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9477 }
9478 else
9479 {
9480 varnumber_T n;
9481 int error = FALSE;
9482
9483 n = get_tv_number_chk(&argvars[0], &error);
9484 if (error)
9485 rettv->vval.v_number = -1;
9486 else if (n > 0)
9487 rettv->vval.v_number = n;
9488 else
9489 rettv->vval.v_number = -n;
9490 }
9491}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009492
9493/*
9494 * "acos()" function
9495 */
9496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009497f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009498{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009499 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500
9501 rettv->v_type = VAR_FLOAT;
9502 if (get_float_arg(argvars, &f) == OK)
9503 rettv->vval.v_float = acos(f);
9504 else
9505 rettv->vval.v_float = 0.0;
9506}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009507#endif
9508
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009510 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 */
9512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009513f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
Bram Moolenaar33570922005-01-25 22:26:29 +00009515 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009518 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009520 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009521 && !tv_check_lock(l->lv_lock,
9522 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009523 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009524 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009525 }
9526 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009527 EMSG(_(e_listreq));
9528}
9529
9530/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009531 * "and(expr, expr)" function
9532 */
9533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009535{
9536 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9537 & get_tv_number_chk(&argvars[1], NULL);
9538}
9539
9540/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009541 * "append(lnum, string/list)" function
9542 */
9543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009544f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009545{
9546 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 list_T *l = NULL;
9549 listitem_T *li = NULL;
9550 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009551 long added = 0;
9552
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009553 /* When coming here from Insert mode, sync undo, so that this can be
9554 * undone separately from what was previously inserted. */
9555 if (u_sync_once == 2)
9556 {
9557 u_sync_once = 1; /* notify that u_sync() was called */
9558 u_sync(TRUE);
9559 }
9560
Bram Moolenaar0d660222005-01-07 21:51:51 +00009561 lnum = get_tv_lnum(argvars);
9562 if (lnum >= 0
9563 && lnum <= curbuf->b_ml.ml_line_count
9564 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009565 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009566 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009567 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009568 l = argvars[1].vval.v_list;
9569 if (l == NULL)
9570 return;
9571 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009572 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009573 for (;;)
9574 {
9575 if (l == NULL)
9576 tv = &argvars[1]; /* append a string */
9577 else if (li == NULL)
9578 break; /* end of list */
9579 else
9580 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 line = get_tv_string_chk(tv);
9582 if (line == NULL) /* type error */
9583 {
9584 rettv->vval.v_number = 1; /* Failed */
9585 break;
9586 }
9587 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009588 ++added;
9589 if (l == NULL)
9590 break;
9591 li = li->li_next;
9592 }
9593
9594 appended_lines_mark(lnum, added);
9595 if (curwin->w_cursor.lnum > lnum)
9596 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 else
9599 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600}
9601
9602/*
9603 * "argc()" function
9604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009606f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609}
9610
9611/*
9612 * "argidx()" function
9613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009615f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618}
9619
9620/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009621 * "arglistid()" function
9622 */
9623 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009624f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009625{
9626 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009627
9628 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009629 wp = find_tabwin(&argvars[0], &argvars[1]);
9630 if (wp != NULL)
9631 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009632}
9633
9634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 * "argv(nr)" function
9636 */
9637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009638f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
9640 int idx;
9641
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009642 if (argvars[0].v_type != VAR_UNKNOWN)
9643 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009644 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009645 if (idx >= 0 && idx < ARGCOUNT)
9646 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9647 else
9648 rettv->vval.v_string = NULL;
9649 rettv->v_type = VAR_STRING;
9650 }
9651 else if (rettv_list_alloc(rettv) == OK)
9652 for (idx = 0; idx < ARGCOUNT; ++idx)
9653 list_append_string(rettv->vval.v_list,
9654 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655}
9656
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009657typedef enum
9658{
9659 ASSERT_EQUAL,
9660 ASSERT_NOTEQUAL,
9661 ASSERT_MATCH,
9662 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009663 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009664} assert_type_T;
9665
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009666static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009667static 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 +01009668static void assert_error(garray_T *gap);
9669static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009670
9671/*
9672 * Prepare "gap" for an assert error and add the sourcing position.
9673 */
9674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009675prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009676{
9677 char buf[NUMBUFLEN];
9678
9679 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009680 if (sourcing_name != NULL)
9681 {
9682 ga_concat(gap, sourcing_name);
9683 if (sourcing_lnum > 0)
9684 ga_concat(gap, (char_u *)" ");
9685 }
9686 if (sourcing_lnum > 0)
9687 {
9688 sprintf(buf, "line %ld", (long)sourcing_lnum);
9689 ga_concat(gap, (char_u *)buf);
9690 }
9691 if (sourcing_name != NULL || sourcing_lnum > 0)
9692 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009693}
9694
9695/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009696 * Append "str" to "gap", escaping unprintable characters.
9697 * Changes NL to \n, CR to \r, etc.
9698 */
9699 static void
9700ga_concat_esc(garray_T *gap, char_u *str)
9701{
9702 char_u *p;
9703 char_u buf[NUMBUFLEN];
9704
Bram Moolenaarf1551962016-03-15 12:55:58 +01009705 if (str == NULL)
9706 {
9707 ga_concat(gap, (char_u *)"NULL");
9708 return;
9709 }
9710
Bram Moolenaar23689172016-02-15 22:37:37 +01009711 for (p = str; *p != NUL; ++p)
9712 switch (*p)
9713 {
9714 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9715 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9716 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9717 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9718 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9719 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9720 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9721 default:
9722 if (*p < ' ')
9723 {
9724 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9725 ga_concat(gap, buf);
9726 }
9727 else
9728 ga_append(gap, *p);
9729 break;
9730 }
9731}
9732
9733/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009734 * Fill "gap" with information about an assert error.
9735 */
9736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009737fill_assert_error(
9738 garray_T *gap,
9739 typval_T *opt_msg_tv,
9740 char_u *exp_str,
9741 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009742 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009743 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009744{
9745 char_u numbuf[NUMBUFLEN];
9746 char_u *tofree;
9747
9748 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9749 {
9750 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9751 vim_free(tofree);
9752 }
9753 else
9754 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009755 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009756 ga_concat(gap, (char_u *)"Pattern ");
9757 else
9758 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009759 if (exp_str == NULL)
9760 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009761 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009762 vim_free(tofree);
9763 }
9764 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009765 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009766 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009767 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009768 else if (atype == ASSERT_NOTMATCH)
9769 ga_concat(gap, (char_u *)" does match ");
9770 else if (atype == ASSERT_NOTEQUAL)
9771 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009772 else
9773 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009774 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009775 vim_free(tofree);
9776 }
9777}
Bram Moolenaar43345542015-11-29 17:35:35 +01009778
9779/*
9780 * Add an assert error to v:errors.
9781 */
9782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009783assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009784{
9785 struct vimvar *vp = &vimvars[VV_ERRORS];
9786
9787 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9788 /* Make sure v:errors is a list. */
9789 set_vim_var_list(VV_ERRORS, list_alloc());
9790 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9791}
9792
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009793 static void
9794assert_equal_common(typval_T *argvars, assert_type_T atype)
9795{
9796 garray_T ga;
9797
9798 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9799 != (atype == ASSERT_EQUAL))
9800 {
9801 prepare_assert_error(&ga);
9802 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9803 atype);
9804 assert_error(&ga);
9805 ga_clear(&ga);
9806 }
9807}
9808
Bram Moolenaar43345542015-11-29 17:35:35 +01009809/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009810 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009811 */
9812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009813f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009814{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009815 assert_equal_common(argvars, ASSERT_EQUAL);
9816}
Bram Moolenaar43345542015-11-29 17:35:35 +01009817
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009818/*
9819 * "assert_notequal(expected, actual[, msg])" function
9820 */
9821 static void
9822f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9823{
9824 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009825}
9826
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009827/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009828 * "assert_exception(string[, msg])" function
9829 */
9830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009831f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009832{
9833 garray_T ga;
9834 char *error;
9835
9836 error = (char *)get_tv_string_chk(&argvars[0]);
9837 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9838 {
9839 prepare_assert_error(&ga);
9840 ga_concat(&ga, (char_u *)"v:exception is not set");
9841 assert_error(&ga);
9842 ga_clear(&ga);
9843 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009844 else if (error != NULL
9845 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009846 {
9847 prepare_assert_error(&ga);
9848 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009849 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009850 assert_error(&ga);
9851 ga_clear(&ga);
9852 }
9853}
9854
9855/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009856 * "assert_fails(cmd [, error])" function
9857 */
9858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009859f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009860{
9861 char_u *cmd = get_tv_string_chk(&argvars[0]);
9862 garray_T ga;
9863
9864 called_emsg = FALSE;
9865 suppress_errthrow = TRUE;
9866 emsg_silent = TRUE;
9867 do_cmdline_cmd(cmd);
9868 if (!called_emsg)
9869 {
9870 prepare_assert_error(&ga);
9871 ga_concat(&ga, (char_u *)"command did not fail: ");
9872 ga_concat(&ga, cmd);
9873 assert_error(&ga);
9874 ga_clear(&ga);
9875 }
9876 else if (argvars[1].v_type != VAR_UNKNOWN)
9877 {
9878 char_u buf[NUMBUFLEN];
9879 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9880
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009881 if (error == NULL
9882 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009883 {
9884 prepare_assert_error(&ga);
9885 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009886 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009887 assert_error(&ga);
9888 ga_clear(&ga);
9889 }
9890 }
9891
9892 called_emsg = FALSE;
9893 suppress_errthrow = FALSE;
9894 emsg_silent = FALSE;
9895 emsg_on_display = FALSE;
9896 set_vim_var_string(VV_ERRMSG, NULL, 0);
9897}
9898
9899/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009900 * Common for assert_true() and assert_false().
9901 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009903assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009904{
9905 int error = FALSE;
9906 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009907
Bram Moolenaar37127922016-02-06 20:29:28 +01009908 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009909 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009910 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009911 if (argvars[0].v_type != VAR_NUMBER
9912 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9913 || error)
9914 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009915 prepare_assert_error(&ga);
9916 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009917 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009918 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009919 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009920 ga_clear(&ga);
9921 }
9922}
9923
9924/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009925 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009926 */
9927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009928f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009929{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009930 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009931}
9932
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009933 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009934assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009935{
9936 garray_T ga;
9937 char_u buf1[NUMBUFLEN];
9938 char_u buf2[NUMBUFLEN];
9939 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9940 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9941
Bram Moolenaar72188e92016-03-28 22:48:29 +02009942 if (pat == NULL || text == NULL)
9943 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009944 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009945 {
9946 prepare_assert_error(&ga);
9947 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009948 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009949 assert_error(&ga);
9950 ga_clear(&ga);
9951 }
9952}
9953
9954/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009955 * "assert_match(pattern, actual[, msg])" function
9956 */
9957 static void
9958f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9959{
9960 assert_match_common(argvars, ASSERT_MATCH);
9961}
9962
9963/*
9964 * "assert_notmatch(pattern, actual[, msg])" function
9965 */
9966 static void
9967f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9968{
9969 assert_match_common(argvars, ASSERT_NOTMATCH);
9970}
9971
9972/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009973 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009974 */
9975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009976f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009977{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009978 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009979}
9980
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009981#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009982/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009983 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009984 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009986f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009987{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009988 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009989
9990 rettv->v_type = VAR_FLOAT;
9991 if (get_float_arg(argvars, &f) == OK)
9992 rettv->vval.v_float = asin(f);
9993 else
9994 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009995}
9996
9997/*
9998 * "atan()" function
9999 */
10000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010001f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010002{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +010010003 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010004
10005 rettv->v_type = VAR_FLOAT;
10006 if (get_float_arg(argvars, &f) == OK)
10007 rettv->vval.v_float = atan(f);
10008 else
10009 rettv->vval.v_float = 0.0;
10010}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010011
10012/*
10013 * "atan2()" function
10014 */
10015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010016f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010017{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010018 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010019
10020 rettv->v_type = VAR_FLOAT;
10021 if (get_float_arg(argvars, &fx) == OK
10022 && get_float_arg(&argvars[1], &fy) == OK)
10023 rettv->vval.v_float = atan2(fx, fy);
10024 else
10025 rettv->vval.v_float = 0.0;
10026}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010027#endif
10028
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029/*
10030 * "browse(save, title, initdir, default)" function
10031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010033f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034{
10035#ifdef FEAT_BROWSE
10036 int save;
10037 char_u *title;
10038 char_u *initdir;
10039 char_u *defname;
10040 char_u buf[NUMBUFLEN];
10041 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010042 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010044 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010045 title = get_tv_string_chk(&argvars[1]);
10046 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10047 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010049 if (error || title == NULL || initdir == NULL || defname == NULL)
10050 rettv->vval.v_string = NULL;
10051 else
10052 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010053 do_browse(save ? BROWSE_SAVE : 0,
10054 title, defname, NULL, initdir, NULL, curbuf);
10055#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010057#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010059}
10060
10061/*
10062 * "browsedir(title, initdir)" function
10063 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010065f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010066{
10067#ifdef FEAT_BROWSE
10068 char_u *title;
10069 char_u *initdir;
10070 char_u buf[NUMBUFLEN];
10071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010072 title = get_tv_string_chk(&argvars[0]);
10073 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010074
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010075 if (title == NULL || initdir == NULL)
10076 rettv->vval.v_string = NULL;
10077 else
10078 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010079 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010086static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010087
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088/*
10089 * Find a buffer by number or exact name.
10090 */
10091 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010092find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
10094 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010096 if (avar->v_type == VAR_NUMBER)
10097 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010098 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010100 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010101 if (buf == NULL)
10102 {
10103 /* No full path name match, try a match with a URL or a "nofile"
10104 * buffer, these don't use the full path. */
10105 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10106 if (buf->b_fname != NULL
10107 && (path_with_url(buf->b_fname)
10108#ifdef FEAT_QUICKFIX
10109 || bt_nofile(buf)
10110#endif
10111 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010112 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010113 break;
10114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 }
10116 return buf;
10117}
10118
10119/*
10120 * "bufexists(expr)" function
10121 */
10122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010123f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126}
10127
10128/*
10129 * "buflisted(expr)" function
10130 */
10131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010132f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133{
10134 buf_T *buf;
10135
10136 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138}
10139
10140/*
10141 * "bufloaded(expr)" function
10142 */
10143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010144f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145{
10146 buf_T *buf;
10147
10148 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150}
10151
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010152 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010153buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 int save_magic;
10156 char_u *save_cpo;
10157 buf_T *buf;
10158
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10160 save_magic = p_magic;
10161 p_magic = TRUE;
10162 save_cpo = p_cpo;
10163 p_cpo = (char_u *)"";
10164
10165 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010166 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167
10168 p_magic = save_magic;
10169 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010170 return buf;
10171}
10172
10173/*
10174 * Get buffer by number or pattern.
10175 */
10176 static buf_T *
10177get_buf_tv(typval_T *tv, int curtab_only)
10178{
10179 char_u *name = tv->vval.v_string;
10180 buf_T *buf;
10181
10182 if (tv->v_type == VAR_NUMBER)
10183 return buflist_findnr((int)tv->vval.v_number);
10184 if (tv->v_type != VAR_STRING)
10185 return NULL;
10186 if (name == NULL || *name == NUL)
10187 return curbuf;
10188 if (name[0] == '$' && name[1] == NUL)
10189 return lastbuf;
10190
10191 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
10193 /* If not found, try expanding the name, like done for bufexists(). */
10194 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010195 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196
10197 return buf;
10198}
10199
10200/*
10201 * "bufname(expr)" function
10202 */
10203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010204f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205{
10206 buf_T *buf;
10207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010210 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010211 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 --emsg_off;
10217}
10218
10219/*
10220 * "bufnr(expr)" function
10221 */
10222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010223f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224{
10225 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010226 int error = FALSE;
10227 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010231 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010232 --emsg_off;
10233
10234 /* If the buffer isn't found and the second argument is not zero create a
10235 * new buffer. */
10236 if (buf == NULL
10237 && argvars[1].v_type != VAR_UNKNOWN
10238 && get_tv_number_chk(&argvars[1], &error) != 0
10239 && !error
10240 && (name = get_tv_string_chk(&argvars[0])) != NULL
10241 && !error)
10242 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010245 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248}
10249
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010251buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252{
10253#ifdef FEAT_WINDOWS
10254 win_T *wp;
10255 int winnr = 0;
10256#endif
10257 buf_T *buf;
10258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010261 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262#ifdef FEAT_WINDOWS
10263 for (wp = firstwin; wp; wp = wp->w_next)
10264 {
10265 ++winnr;
10266 if (wp->w_buffer == buf)
10267 break;
10268 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010269 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010271 rettv->vval.v_number = (curwin->w_buffer == buf
10272 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273#endif
10274 --emsg_off;
10275}
10276
10277/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010278 * "bufwinid(nr)" function
10279 */
10280 static void
10281f_bufwinid(typval_T *argvars, typval_T *rettv)
10282{
10283 buf_win_common(argvars, rettv, FALSE);
10284}
10285
10286/*
10287 * "bufwinnr(nr)" function
10288 */
10289 static void
10290f_bufwinnr(typval_T *argvars, typval_T *rettv)
10291{
10292 buf_win_common(argvars, rettv, TRUE);
10293}
10294
10295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 * "byte2line(byte)" function
10297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010299f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300{
10301#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303#else
10304 long boff = 0;
10305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 (linenr_T)0, &boff);
10312#endif
10313}
10314
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010316byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010317{
10318#ifdef FEAT_MBYTE
10319 char_u *t;
10320#endif
10321 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010322 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010324 str = get_tv_string_chk(&argvars[0]);
10325 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010328 return;
10329
10330#ifdef FEAT_MBYTE
10331 t = str;
10332 for ( ; idx > 0; idx--)
10333 {
10334 if (*t == NUL) /* EOL reached */
10335 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010336 if (enc_utf8 && comp)
10337 t += utf_ptr2len(t);
10338 else
10339 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010340 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010341 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010342#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010343 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010345#endif
10346}
10347
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010348/*
10349 * "byteidx()" function
10350 */
10351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010352f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010353{
10354 byteidx(argvars, rettv, FALSE);
10355}
10356
10357/*
10358 * "byteidxcomp()" function
10359 */
10360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010361f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010362{
10363 byteidx(argvars, rettv, TRUE);
10364}
10365
Bram Moolenaardb913952012-06-29 12:54:53 +020010366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010367func_call(
10368 char_u *name,
10369 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010370 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010371 dict_T *selfdict,
10372 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010373{
10374 listitem_T *item;
10375 typval_T argv[MAX_FUNC_ARGS + 1];
10376 int argc = 0;
10377 int dummy;
10378 int r = 0;
10379
10380 for (item = args->vval.v_list->lv_first; item != NULL;
10381 item = item->li_next)
10382 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010383 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010384 {
10385 EMSG(_("E699: Too many arguments"));
10386 break;
10387 }
10388 /* Make a copy of each argument. This is needed to be able to set
10389 * v_lock to VAR_FIXED in the copy without changing the original list.
10390 */
10391 copy_tv(&item->li_tv, &argv[argc++]);
10392 }
10393
10394 if (item == NULL)
10395 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10396 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010397 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010398
10399 /* Free the arguments. */
10400 while (argc > 0)
10401 clear_tv(&argv[--argc]);
10402
10403 return r;
10404}
10405
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010406/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010407 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010408 */
10409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010410f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010411{
10412 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010413 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010414 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010415
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010416 if (argvars[1].v_type != VAR_LIST)
10417 {
10418 EMSG(_(e_listreq));
10419 return;
10420 }
10421 if (argvars[1].vval.v_list == NULL)
10422 return;
10423
10424 if (argvars[0].v_type == VAR_FUNC)
10425 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010426 else if (argvars[0].v_type == VAR_PARTIAL)
10427 {
10428 partial = argvars[0].vval.v_partial;
10429 func = partial->pt_name;
10430 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010431 else
10432 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010433 if (*func == NUL)
10434 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010435
Bram Moolenaare9a41262005-01-15 22:18:47 +000010436 if (argvars[2].v_type != VAR_UNKNOWN)
10437 {
10438 if (argvars[2].v_type != VAR_DICT)
10439 {
10440 EMSG(_(e_dictreq));
10441 return;
10442 }
10443 selfdict = argvars[2].vval.v_dict;
10444 }
10445
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010446 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010447}
10448
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010449#ifdef FEAT_FLOAT
10450/*
10451 * "ceil({float})" function
10452 */
10453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010454f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010455{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010456 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010457
10458 rettv->v_type = VAR_FLOAT;
10459 if (get_float_arg(argvars, &f) == OK)
10460 rettv->vval.v_float = ceil(f);
10461 else
10462 rettv->vval.v_float = 0.0;
10463}
10464#endif
10465
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010466#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010467/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010468 * "ch_close()" function
10469 */
10470 static void
10471f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10472{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010473 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010474
10475 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010476 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010477 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010478 channel_clear(channel);
10479 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010480}
10481
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010482/*
10483 * "ch_getbufnr()" function
10484 */
10485 static void
10486f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10487{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010488 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010489
10490 rettv->vval.v_number = -1;
10491 if (channel != NULL)
10492 {
10493 char_u *what = get_tv_string(&argvars[1]);
10494 int part;
10495
10496 if (STRCMP(what, "err") == 0)
10497 part = PART_ERR;
10498 else if (STRCMP(what, "out") == 0)
10499 part = PART_OUT;
10500 else if (STRCMP(what, "in") == 0)
10501 part = PART_IN;
10502 else
10503 part = PART_SOCK;
10504 if (channel->ch_part[part].ch_buffer != NULL)
10505 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10506 }
10507}
10508
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010509/*
10510 * "ch_getjob()" function
10511 */
10512 static void
10513f_ch_getjob(typval_T *argvars, typval_T *rettv)
10514{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010515 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010516
10517 if (channel != NULL)
10518 {
10519 rettv->v_type = VAR_JOB;
10520 rettv->vval.v_job = channel->ch_job;
10521 if (channel->ch_job != NULL)
10522 ++channel->ch_job->jv_refcount;
10523 }
10524}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010525
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010526/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010527 * "ch_info()" function
10528 */
10529 static void
10530f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10531{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010532 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010533
10534 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10535 channel_info(channel, rettv->vval.v_dict);
10536}
10537
10538/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010539 * "ch_log()" function
10540 */
10541 static void
10542f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10543{
10544 char_u *msg = get_tv_string(&argvars[0]);
10545 channel_T *channel = NULL;
10546
10547 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010548 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010549
10550 ch_log(channel, (char *)msg);
10551}
10552
10553/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010554 * "ch_logfile()" function
10555 */
10556 static void
10557f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10558{
10559 char_u *fname;
10560 char_u *opt = (char_u *)"";
10561 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010562
10563 fname = get_tv_string(&argvars[0]);
10564 if (argvars[1].v_type == VAR_STRING)
10565 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010566 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010567}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010568
10569/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010570 * "ch_open()" function
10571 */
10572 static void
10573f_ch_open(typval_T *argvars, typval_T *rettv)
10574{
Bram Moolenaar77073442016-02-13 23:23:53 +010010575 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010576 if (check_restricted() || check_secure())
10577 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010578 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010579}
10580
10581/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010582 * "ch_read()" function
10583 */
10584 static void
10585f_ch_read(typval_T *argvars, typval_T *rettv)
10586{
10587 common_channel_read(argvars, rettv, FALSE);
10588}
10589
10590/*
10591 * "ch_readraw()" function
10592 */
10593 static void
10594f_ch_readraw(typval_T *argvars, typval_T *rettv)
10595{
10596 common_channel_read(argvars, rettv, TRUE);
10597}
10598
10599/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010600 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010601 */
10602 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010603f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10604{
10605 ch_expr_common(argvars, rettv, TRUE);
10606}
10607
10608/*
10609 * "ch_sendexpr()" function
10610 */
10611 static void
10612f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10613{
10614 ch_expr_common(argvars, rettv, FALSE);
10615}
10616
10617/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010618 * "ch_evalraw()" function
10619 */
10620 static void
10621f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10622{
10623 ch_raw_common(argvars, rettv, TRUE);
10624}
10625
10626/*
10627 * "ch_sendraw()" function
10628 */
10629 static void
10630f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10631{
10632 ch_raw_common(argvars, rettv, FALSE);
10633}
10634
10635/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010636 * "ch_setoptions()" function
10637 */
10638 static void
10639f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10640{
10641 channel_T *channel;
10642 jobopt_T opt;
10643
Bram Moolenaar437905c2016-04-26 19:01:05 +020010644 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010645 if (channel == NULL)
10646 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010647 clear_job_options(&opt);
10648 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010649 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10650 channel_set_options(channel, &opt);
10651 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010652}
10653
10654/*
10655 * "ch_status()" function
10656 */
10657 static void
10658f_ch_status(typval_T *argvars, typval_T *rettv)
10659{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010660 channel_T *channel;
10661
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010662 /* return an empty string by default */
10663 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010664 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010665
Bram Moolenaar437905c2016-04-26 19:01:05 +020010666 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010667 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010668}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010669#endif
10670
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010671/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010672 * "changenr()" function
10673 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010675f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010676{
10677 rettv->vval.v_number = curbuf->b_u_seq_cur;
10678}
10679
10680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 * "char2nr(string)" function
10682 */
10683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010684f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
10686#ifdef FEAT_MBYTE
10687 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010688 {
10689 int utf8 = 0;
10690
10691 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010692 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010010693
10694 if (utf8)
10695 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10696 else
10697 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 else
10700#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702}
10703
10704/*
10705 * "cindent(lnum)" function
10706 */
10707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010708f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709{
10710#ifdef FEAT_CINDENT
10711 pos_T pos;
10712 linenr_T lnum;
10713
10714 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10717 {
10718 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 curwin->w_cursor = pos;
10721 }
10722 else
10723#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725}
10726
10727/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010728 * "clearmatches()" function
10729 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010731f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010732{
10733#ifdef FEAT_SEARCH_EXTRA
10734 clear_matches(curwin);
10735#endif
10736}
10737
10738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 * "col(string)" function
10740 */
10741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010742f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743{
10744 colnr_T col = 0;
10745 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010746 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010748 fp = var2fpos(&argvars[0], FALSE, &fnum);
10749 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 {
10751 if (fp->col == MAXCOL)
10752 {
10753 /* '> can be MAXCOL, get the length of the line then */
10754 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010755 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 else
10757 col = MAXCOL;
10758 }
10759 else
10760 {
10761 col = fp->col + 1;
10762#ifdef FEAT_VIRTUALEDIT
10763 /* col(".") when the cursor is on the NUL at the end of the line
10764 * because of "coladd" can be seen as an extra column. */
10765 if (virtual_active() && fp == &curwin->w_cursor)
10766 {
10767 char_u *p = ml_get_cursor();
10768
10769 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10770 curwin->w_virtcol - curwin->w_cursor.coladd))
10771 {
10772# ifdef FEAT_MBYTE
10773 int l;
10774
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010775 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 col += l;
10777# else
10778 if (*p != NUL && p[1] == NUL)
10779 ++col;
10780# endif
10781 }
10782 }
10783#endif
10784 }
10785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010786 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787}
10788
Bram Moolenaar572cb562005-08-05 21:35:02 +000010789#if defined(FEAT_INS_EXPAND)
10790/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010791 * "complete()" function
10792 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010794f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010795{
10796 int startcol;
10797
10798 if ((State & INSERT) == 0)
10799 {
10800 EMSG(_("E785: complete() can only be used in Insert mode"));
10801 return;
10802 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010803
10804 /* Check for undo allowed here, because if something was already inserted
10805 * the line was already saved for undo and this check isn't done. */
10806 if (!undo_allowed())
10807 return;
10808
Bram Moolenaarade00832006-03-10 21:46:58 +000010809 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10810 {
10811 EMSG(_(e_invarg));
10812 return;
10813 }
10814
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010815 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +000010816 if (startcol <= 0)
10817 return;
10818
10819 set_completion(startcol - 1, argvars[1].vval.v_list);
10820}
10821
10822/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010823 * "complete_add()" function
10824 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010826f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010827{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010828 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010829}
10830
10831/*
10832 * "complete_check()" function
10833 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010835f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010836{
10837 int saved = RedrawingDisabled;
10838
10839 RedrawingDisabled = 0;
10840 ins_compl_check_keys(0);
10841 rettv->vval.v_number = compl_interrupted;
10842 RedrawingDisabled = saved;
10843}
10844#endif
10845
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846/*
10847 * "confirm(message, buttons[, default [, type]])" function
10848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010850f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851{
10852#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10853 char_u *message;
10854 char_u *buttons = NULL;
10855 char_u buf[NUMBUFLEN];
10856 char_u buf2[NUMBUFLEN];
10857 int def = 1;
10858 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010859 char_u *typestr;
10860 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010862 message = get_tv_string_chk(&argvars[0]);
10863 if (message == NULL)
10864 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010865 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010867 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10868 if (buttons == NULL)
10869 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010870 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010872 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010873 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10876 if (typestr == NULL)
10877 error = TRUE;
10878 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010880 switch (TOUPPER_ASC(*typestr))
10881 {
10882 case 'E': type = VIM_ERROR; break;
10883 case 'Q': type = VIM_QUESTION; break;
10884 case 'I': type = VIM_INFO; break;
10885 case 'W': type = VIM_WARNING; break;
10886 case 'G': type = VIM_GENERIC; break;
10887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 }
10889 }
10890 }
10891 }
10892
10893 if (buttons == NULL || *buttons == NUL)
10894 buttons = (char_u *)_("&Ok");
10895
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010896 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010898 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899#endif
10900}
10901
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010902/*
10903 * "copy()" function
10904 */
10905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010906f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010907{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010908 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010909}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010911#ifdef FEAT_FLOAT
10912/*
10913 * "cos()" function
10914 */
10915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010916f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010917{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010918 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010919
10920 rettv->v_type = VAR_FLOAT;
10921 if (get_float_arg(argvars, &f) == OK)
10922 rettv->vval.v_float = cos(f);
10923 else
10924 rettv->vval.v_float = 0.0;
10925}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010926
10927/*
10928 * "cosh()" function
10929 */
10930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010931f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010932{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010933 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010934
10935 rettv->v_type = VAR_FLOAT;
10936 if (get_float_arg(argvars, &f) == OK)
10937 rettv->vval.v_float = cosh(f);
10938 else
10939 rettv->vval.v_float = 0.0;
10940}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010941#endif
10942
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010944 * "count()" function
10945 */
10946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010947f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010948{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010949 long n = 0;
10950 int ic = FALSE;
10951
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010953 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010954 listitem_T *li;
10955 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010957
Bram Moolenaare9a41262005-01-15 22:18:47 +000010958 if ((l = argvars[0].vval.v_list) != NULL)
10959 {
10960 li = l->lv_first;
10961 if (argvars[2].v_type != VAR_UNKNOWN)
10962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 int error = FALSE;
10964
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010965 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010966 if (argvars[3].v_type != VAR_UNKNOWN)
10967 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010968 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010969 if (!error)
10970 {
10971 li = list_find(l, idx);
10972 if (li == NULL)
10973 EMSGN(_(e_listidx), idx);
10974 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010975 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010976 if (error)
10977 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 }
10979
10980 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010981 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 ++n;
10983 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010984 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010985 else if (argvars[0].v_type == VAR_DICT)
10986 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010987 int todo;
10988 dict_T *d;
10989 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010990
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010991 if ((d = argvars[0].vval.v_dict) != NULL)
10992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010993 int error = FALSE;
10994
Bram Moolenaare9a41262005-01-15 22:18:47 +000010995 if (argvars[2].v_type != VAR_UNKNOWN)
10996 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010997 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010998 if (argvars[3].v_type != VAR_UNKNOWN)
10999 EMSG(_(e_invarg));
11000 }
11001
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011002 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011004 {
11005 if (!HASHITEM_EMPTY(hi))
11006 {
11007 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011008 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011009 ++n;
11010 }
11011 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011012 }
11013 }
11014 else
11015 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011016 rettv->vval.v_number = n;
11017}
11018
11019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11021 *
11022 * Checks the existence of a cscope connection.
11023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011025f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026{
11027#ifdef FEAT_CSCOPE
11028 int num = 0;
11029 char_u *dbpath = NULL;
11030 char_u *prepend = NULL;
11031 char_u buf[NUMBUFLEN];
11032
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011033 if (argvars[0].v_type != VAR_UNKNOWN
11034 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 num = (int)get_tv_number(&argvars[0]);
11037 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011038 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 }
11041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043#endif
11044}
11045
11046/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011047 * "cursor(lnum, col)" function, or
11048 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011050 * Moves the cursor to the specified line and column.
11051 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011054f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055{
11056 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011057#ifdef FEAT_VIRTUALEDIT
11058 long coladd = 0;
11059#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011060 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011062 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011063 if (argvars[1].v_type == VAR_UNKNOWN)
11064 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011065 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011066 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011067
Bram Moolenaar493c1782014-05-28 14:34:46 +020011068 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011069 {
11070 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011071 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011072 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011073 line = pos.lnum;
11074 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011075#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011076 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011077#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011078 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011079 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011080 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011081 set_curswant = FALSE;
11082 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011083 }
11084 else
11085 {
11086 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011087 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011088#ifdef FEAT_VIRTUALEDIT
11089 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011090 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011091#endif
11092 }
11093 if (line < 0 || col < 0
11094#ifdef FEAT_VIRTUALEDIT
11095 || coladd < 0
11096#endif
11097 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011098 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 if (line > 0)
11100 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 if (col > 0)
11102 curwin->w_cursor.col = col - 1;
11103#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011104 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105#endif
11106
11107 /* Make sure the cursor is in a valid position. */
11108 check_cursor();
11109#ifdef FEAT_MBYTE
11110 /* Correct cursor for multi-byte character. */
11111 if (has_mbyte)
11112 mb_adjust_cursor();
11113#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011114
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011115 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011116 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117}
11118
11119/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011120 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 */
11122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011123f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011125 int noref = 0;
11126
11127 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011128 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011129 if (noref < 0 || noref > 1)
11130 EMSG(_(e_invarg));
11131 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011132 {
11133 current_copyID += COPYID_INC;
11134 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136}
11137
11138/*
11139 * "delete()" function
11140 */
11141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011142f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011144 char_u nbuf[NUMBUFLEN];
11145 char_u *name;
11146 char_u *flags;
11147
11148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011150 return;
11151
11152 name = get_tv_string(&argvars[0]);
11153 if (name == NULL || *name == NUL)
11154 {
11155 EMSG(_(e_invarg));
11156 return;
11157 }
11158
11159 if (argvars[1].v_type != VAR_UNKNOWN)
11160 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011162 flags = (char_u *)"";
11163
11164 if (*flags == NUL)
11165 /* delete a file */
11166 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11167 else if (STRCMP(flags, "d") == 0)
11168 /* delete an empty directory */
11169 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11170 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011171 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011172 rettv->vval.v_number = delete_recursive(name);
11173 else
11174 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175}
11176
11177/*
11178 * "did_filetype()" function
11179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011181f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182{
11183#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185#endif
11186}
11187
11188/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011189 * "diff_filler()" function
11190 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011192f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011193{
11194#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011196#endif
11197}
11198
11199/*
11200 * "diff_hlID()" function
11201 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011203f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011204{
11205#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011207 static linenr_T prev_lnum = 0;
11208 static int changedtick = 0;
11209 static int fnum = 0;
11210 static int change_start = 0;
11211 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011212 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011213 int filler_lines;
11214 int col;
11215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 if (lnum < 0) /* ignore type error in {lnum} arg */
11217 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011218 if (lnum != prev_lnum
11219 || changedtick != curbuf->b_changedtick
11220 || fnum != curbuf->b_fnum)
11221 {
11222 /* New line, buffer, change: need to get the values. */
11223 filler_lines = diff_check(curwin, lnum);
11224 if (filler_lines < 0)
11225 {
11226 if (filler_lines == -1)
11227 {
11228 change_start = MAXCOL;
11229 change_end = -1;
11230 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11231 hlID = HLF_ADD; /* added line */
11232 else
11233 hlID = HLF_CHD; /* changed line */
11234 }
11235 else
11236 hlID = HLF_ADD; /* added line */
11237 }
11238 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011239 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011240 prev_lnum = lnum;
11241 changedtick = curbuf->b_changedtick;
11242 fnum = curbuf->b_fnum;
11243 }
11244
11245 if (hlID == HLF_CHD || hlID == HLF_TXD)
11246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011248 if (col >= change_start && col <= change_end)
11249 hlID = HLF_TXD; /* changed text */
11250 else
11251 hlID = HLF_CHD; /* changed line */
11252 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011253 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011254#endif
11255}
11256
11257/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011258 * "empty({expr})" function
11259 */
11260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011261f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011262{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011263 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011264
11265 switch (argvars[0].v_type)
11266 {
11267 case VAR_STRING:
11268 case VAR_FUNC:
11269 n = argvars[0].vval.v_string == NULL
11270 || *argvars[0].vval.v_string == NUL;
11271 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011272 case VAR_PARTIAL:
11273 n = FALSE;
11274 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011275 case VAR_NUMBER:
11276 n = argvars[0].vval.v_number == 0;
11277 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011278 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011279#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011280 n = argvars[0].vval.v_float == 0.0;
11281 break;
11282#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011283 case VAR_LIST:
11284 n = argvars[0].vval.v_list == NULL
11285 || argvars[0].vval.v_list->lv_first == NULL;
11286 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011287 case VAR_DICT:
11288 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011289 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011290 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011291 case VAR_SPECIAL:
11292 n = argvars[0].vval.v_number != VVAL_TRUE;
11293 break;
11294
Bram Moolenaar835dc632016-02-07 14:27:38 +010011295 case VAR_JOB:
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_job == NULL
11298 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11299 break;
11300#endif
11301 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011302#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011303 n = argvars[0].vval.v_channel == NULL
11304 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011305 break;
11306#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011307 case VAR_UNKNOWN:
11308 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11309 n = TRUE;
11310 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011311 }
11312
11313 rettv->vval.v_number = n;
11314}
11315
11316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 * "escape({string}, {chars})" function
11318 */
11319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011320f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321{
11322 char_u buf[NUMBUFLEN];
11323
Bram Moolenaar758711c2005-02-02 23:11:38 +000011324 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11325 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327}
11328
11329/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011330 * "eval()" function
11331 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011333f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011334{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011335 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 s = get_tv_string_chk(&argvars[0]);
11338 if (s != NULL)
11339 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011340
Bram Moolenaar615b9972015-01-14 17:15:05 +010011341 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11343 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011344 if (p != NULL && !aborting())
11345 EMSG2(_(e_invexpr2), p);
11346 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011348 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011349 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011350 else if (*s != NUL)
11351 EMSG(_(e_trailing));
11352}
11353
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 * "eventhandler()" function
11356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011358f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361}
11362
11363/*
11364 * "executable()" function
11365 */
11366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011367f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011369 char_u *name = get_tv_string(&argvars[0]);
11370
11371 /* Check in $PATH and also check directly if there is a directory name. */
11372 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11373 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011374}
11375
Bram Moolenaar79815f12016-07-09 17:07:29 +020011376static garray_T redir_execute_ga;
11377
11378/*
11379 * Append "value[value_len]" to the execute() output.
11380 */
11381 void
11382execute_redir_str(char_u *value, int value_len)
11383{
11384 int len;
11385
11386 if (value_len == -1)
11387 len = (int)STRLEN(value); /* Append the entire string */
11388 else
11389 len = value_len; /* Append only "value_len" characters */
11390 if (ga_grow(&redir_execute_ga, len) == OK)
11391 {
11392 mch_memmove((char *)redir_execute_ga.ga_data
11393 + redir_execute_ga.ga_len, value, len);
11394 redir_execute_ga.ga_len += len;
11395 }
11396}
11397
11398/*
11399 * Get next line from a list.
11400 * Called by do_cmdline() to get the next line.
11401 * Returns allocated string, or NULL for end of function.
11402 */
11403
11404 static char_u *
11405get_list_line(
11406 int c UNUSED,
11407 void *cookie,
11408 int indent UNUSED)
11409{
11410 listitem_T **p = (listitem_T **)cookie;
11411 listitem_T *item = *p;
11412 char_u buf[NUMBUFLEN];
11413 char_u *s;
11414
11415 if (item == NULL)
11416 return NULL;
11417 s = get_tv_string_buf_chk(&item->li_tv, buf);
11418 *p = item->li_next;
11419 return s == NULL ? NULL : vim_strsave(s);
11420}
11421
11422/*
11423 * "execute()" function
11424 */
11425 static void
11426f_execute(typval_T *argvars, typval_T *rettv)
11427{
11428 char_u *cmd = NULL;
11429 list_T *list = NULL;
11430 int save_msg_silent = msg_silent;
11431 int save_emsg_silent = emsg_silent;
11432 int save_emsg_noredir = emsg_noredir;
11433 int save_redir_execute = redir_execute;
11434 garray_T save_ga;
11435
11436 rettv->vval.v_string = NULL;
11437 rettv->v_type = VAR_STRING;
11438
11439 if (argvars[0].v_type == VAR_LIST)
11440 {
11441 list = argvars[0].vval.v_list;
11442 if (list == NULL || list->lv_first == NULL)
11443 /* empty list, no commands, empty output */
11444 return;
11445 ++list->lv_refcount;
11446 }
11447 else
11448 {
11449 cmd = get_tv_string_chk(&argvars[0]);
11450 if (cmd == NULL)
11451 return;
11452 }
11453
Bram Moolenaar79815f12016-07-09 17:07:29 +020011454 if (argvars[1].v_type != VAR_UNKNOWN)
11455 {
11456 char_u buf[NUMBUFLEN];
11457 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
11458
11459 if (s == NULL)
11460 return;
11461 if (STRNCMP(s, "silent", 6) == 0)
11462 ++msg_silent;
11463 if (STRCMP(s, "silent!") == 0)
11464 {
11465 emsg_silent = TRUE;
11466 emsg_noredir = TRUE;
11467 }
11468 }
11469 else
11470 ++msg_silent;
11471
Bram Moolenaared59aa62016-07-09 17:41:12 +020011472 if (redir_execute)
11473 save_ga = redir_execute_ga;
11474 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
11475 redir_execute = TRUE;
11476
Bram Moolenaar79815f12016-07-09 17:07:29 +020011477 if (cmd != NULL)
11478 do_cmdline_cmd(cmd);
11479 else
11480 {
11481 listitem_T *item = list->lv_first;
11482
11483 do_cmdline(NULL, get_list_line, (void *)&item,
11484 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
11485 --list->lv_refcount;
11486 }
11487
11488 rettv->vval.v_string = redir_execute_ga.ga_data;
11489 msg_silent = save_msg_silent;
11490 emsg_silent = save_emsg_silent;
11491 emsg_noredir = save_emsg_noredir;
11492
11493 redir_execute = save_redir_execute;
11494 if (redir_execute)
11495 redir_execute_ga = save_ga;
11496
11497 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
11498 * line. Put it back in the first column. */
11499 msg_col = 0;
11500}
11501
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011502/*
11503 * "exepath()" function
11504 */
11505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011506f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011507{
11508 char_u *p = NULL;
11509
Bram Moolenaarb5971142015-03-21 17:32:19 +010011510 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011511 rettv->v_type = VAR_STRING;
11512 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513}
11514
11515/*
11516 * "exists()" function
11517 */
11518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011519f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520{
11521 char_u *p;
11522 char_u *name;
11523 int n = FALSE;
11524 int len = 0;
11525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527 if (*p == '$') /* environment variable */
11528 {
11529 /* first try "normal" environment variables (fast) */
11530 if (mch_getenv(p + 1) != NULL)
11531 n = TRUE;
11532 else
11533 {
11534 /* try expanding things like $VIM and ${HOME} */
11535 p = expand_env_save(p);
11536 if (p != NULL && *p != '$')
11537 n = TRUE;
11538 vim_free(p);
11539 }
11540 }
11541 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011543 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011544 if (*skipwhite(p) != NUL)
11545 n = FALSE; /* trailing garbage */
11546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547 else if (*p == '*') /* internal or user defined function */
11548 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011549 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 }
11551 else if (*p == ':')
11552 {
11553 n = cmd_exists(p + 1);
11554 }
11555 else if (*p == '#')
11556 {
11557#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011558 if (p[1] == '#')
11559 n = autocmd_supported(p + 2);
11560 else
11561 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562#endif
11563 }
11564 else /* internal variable */
11565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011566 char_u *tofree;
11567 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011569 /* get_name_len() takes care of expanding curly braces */
11570 name = p;
11571 len = get_name_len(&p, &tofree, TRUE, FALSE);
11572 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011574 if (tofree != NULL)
11575 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011576 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011577 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011579 /* handle d.key, l[idx], f(expr) */
11580 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11581 if (n)
11582 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 }
11584 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011585 if (*p != NUL)
11586 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011588 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 }
11590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011591 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592}
11593
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011594#ifdef FEAT_FLOAT
11595/*
11596 * "exp()" function
11597 */
11598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011599f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011600{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011601 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011602
11603 rettv->v_type = VAR_FLOAT;
11604 if (get_float_arg(argvars, &f) == OK)
11605 rettv->vval.v_float = exp(f);
11606 else
11607 rettv->vval.v_float = 0.0;
11608}
11609#endif
11610
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611/*
11612 * "expand()" function
11613 */
11614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011615f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616{
11617 char_u *s;
11618 int len;
11619 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011620 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011623 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011626 if (argvars[1].v_type != VAR_UNKNOWN
11627 && argvars[2].v_type != VAR_UNKNOWN
11628 && get_tv_number_chk(&argvars[2], &error)
11629 && !error)
11630 {
11631 rettv->v_type = VAR_LIST;
11632 rettv->vval.v_list = NULL;
11633 }
11634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 if (*s == '%' || *s == '#' || *s == '<')
11637 {
11638 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011639 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011641 if (rettv->v_type == VAR_LIST)
11642 {
11643 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11644 list_append_string(rettv->vval.v_list, result, -1);
11645 else
11646 vim_free(result);
11647 }
11648 else
11649 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 }
11651 else
11652 {
11653 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011654 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011655 if (argvars[1].v_type != VAR_UNKNOWN
11656 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011657 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011658 if (!error)
11659 {
11660 ExpandInit(&xpc);
11661 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011662 if (p_wic)
11663 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011664 if (rettv->v_type == VAR_STRING)
11665 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11666 options, WILD_ALL);
11667 else if (rettv_list_alloc(rettv) != FAIL)
11668 {
11669 int i;
11670
11671 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11672 for (i = 0; i < xpc.xp_numfiles; i++)
11673 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11674 ExpandCleanup(&xpc);
11675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 }
11677 else
11678 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 }
11680}
11681
11682/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011683 * Go over all entries in "d2" and add them to "d1".
11684 * When "action" is "error" then a duplicate key is an error.
11685 * When "action" is "force" then a duplicate key is overwritten.
11686 * Otherwise duplicate keys are ignored ("action" is "keep").
11687 */
11688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011689dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011690{
11691 dictitem_T *di1;
11692 hashitem_T *hi2;
11693 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011694 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011695
11696 todo = (int)d2->dv_hashtab.ht_used;
11697 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11698 {
11699 if (!HASHITEM_EMPTY(hi2))
11700 {
11701 --todo;
11702 di1 = dict_find(d1, hi2->hi_key, -1);
11703 if (d1->dv_scope != 0)
11704 {
11705 /* Disallow replacing a builtin function in l: and g:.
11706 * Check the key to be valid when adding to any
11707 * scope. */
11708 if (d1->dv_scope == VAR_DEF_SCOPE
11709 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11710 && var_check_func_name(hi2->hi_key,
11711 di1 == NULL))
11712 break;
11713 if (!valid_varname(hi2->hi_key))
11714 break;
11715 }
11716 if (di1 == NULL)
11717 {
11718 di1 = dictitem_copy(HI2DI(hi2));
11719 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11720 dictitem_free(di1);
11721 }
11722 else if (*action == 'e')
11723 {
11724 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11725 break;
11726 }
11727 else if (*action == 'f' && HI2DI(hi2) != di1)
11728 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011729 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11730 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011731 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011732 clear_tv(&di1->di_tv);
11733 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11734 }
11735 }
11736 }
11737}
11738
11739/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011740 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011741 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011742 */
11743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011744f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011745{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011746 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011747
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011749 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011750 list_T *l1, *l2;
11751 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011752 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011753 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011754
Bram Moolenaare9a41262005-01-15 22:18:47 +000011755 l1 = argvars[0].vval.v_list;
11756 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011757 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011758 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011759 {
11760 if (argvars[2].v_type != VAR_UNKNOWN)
11761 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011762 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011763 if (error)
11764 return; /* type error; errmsg already given */
11765
Bram Moolenaar758711c2005-02-02 23:11:38 +000011766 if (before == l1->lv_len)
11767 item = NULL;
11768 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011769 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011770 item = list_find(l1, before);
11771 if (item == NULL)
11772 {
11773 EMSGN(_(e_listidx), before);
11774 return;
11775 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011776 }
11777 }
11778 else
11779 item = NULL;
11780 list_extend(l1, l2, item);
11781
Bram Moolenaare9a41262005-01-15 22:18:47 +000011782 copy_tv(&argvars[0], rettv);
11783 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011784 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011785 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11786 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011787 dict_T *d1, *d2;
11788 char_u *action;
11789 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011790
11791 d1 = argvars[0].vval.v_dict;
11792 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011793 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011794 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011795 {
11796 /* Check the third argument. */
11797 if (argvars[2].v_type != VAR_UNKNOWN)
11798 {
11799 static char *(av[]) = {"keep", "force", "error"};
11800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 action = get_tv_string_chk(&argvars[2]);
11802 if (action == NULL)
11803 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011804 for (i = 0; i < 3; ++i)
11805 if (STRCMP(action, av[i]) == 0)
11806 break;
11807 if (i == 3)
11808 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011809 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011810 return;
11811 }
11812 }
11813 else
11814 action = (char_u *)"force";
11815
Bram Moolenaara9922d62013-05-30 13:01:18 +020011816 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011817
Bram Moolenaare9a41262005-01-15 22:18:47 +000011818 copy_tv(&argvars[0], rettv);
11819 }
11820 }
11821 else
11822 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011823}
11824
11825/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011826 * "feedkeys()" function
11827 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011829f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011830{
11831 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011832 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011833 char_u *keys, *flags;
11834 char_u nbuf[NUMBUFLEN];
11835 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011836 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011837 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011838 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011839
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011840 /* This is not allowed in the sandbox. If the commands would still be
11841 * executed in the sandbox it would be OK, but it probably happens later,
11842 * when "sandbox" is no longer set. */
11843 if (check_secure())
11844 return;
11845
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011846 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011847
11848 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011849 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011850 flags = get_tv_string_buf(&argvars[1], nbuf);
11851 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011852 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011853 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011854 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011855 case 'n': remap = FALSE; break;
11856 case 'm': remap = TRUE; break;
11857 case 't': typed = TRUE; break;
11858 case 'i': insert = TRUE; break;
11859 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011860 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011861 }
11862 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011863 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011864
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011865 if (*keys != NUL || execute)
11866 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011867 /* Need to escape K_SPECIAL and CSI before putting the string in the
11868 * typeahead buffer. */
11869 keys_esc = vim_strsave_escape_csi(keys);
11870 if (keys_esc != NULL)
11871 {
11872 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011873 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011874 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011875 if (vgetc_busy)
11876 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011877 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011878 {
11879 int save_msg_scroll = msg_scroll;
11880
11881 /* Avoid a 1 second delay when the keys start Insert mode. */
11882 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011883
Bram Moolenaar245c4102016-04-20 17:37:41 +020011884 if (!dangerous)
11885 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011886 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011887 if (!dangerous)
11888 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011889 msg_scroll |= save_msg_scroll;
11890 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011891 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011892 }
11893}
11894
11895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 * "filereadable()" function
11897 */
11898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011899f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011901 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 char_u *p;
11903 int n;
11904
Bram Moolenaarc236c162008-07-13 17:41:49 +000011905#ifndef O_NONBLOCK
11906# define O_NONBLOCK 0
11907#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011908 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011909 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11910 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 {
11912 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011913 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 }
11915 else
11916 n = FALSE;
11917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919}
11920
11921/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011922 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 * rights to write into.
11924 */
11925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011926f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011928 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929}
11930
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011931 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011932findfilendir(
11933 typval_T *argvars UNUSED,
11934 typval_T *rettv,
11935 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011936{
11937#ifdef FEAT_SEARCHPATH
11938 char_u *fname;
11939 char_u *fresult = NULL;
11940 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11941 char_u *p;
11942 char_u pathbuf[NUMBUFLEN];
11943 int count = 1;
11944 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011945 int error = FALSE;
11946#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011947
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011948 rettv->vval.v_string = NULL;
11949 rettv->v_type = VAR_STRING;
11950
11951#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011953
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011954 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011956 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11957 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011958 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 else
11960 {
11961 if (*p != NUL)
11962 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011965 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011967 }
11968
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011969 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11970 error = TRUE;
11971
11972 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011974 do
11975 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011976 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011977 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011978 fresult = find_file_in_path_option(first ? fname : NULL,
11979 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011980 0, first, path,
11981 find_what,
11982 curbuf->b_ffname,
11983 find_what == FINDFILE_DIR
11984 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011986
11987 if (fresult != NULL && rettv->v_type == VAR_LIST)
11988 list_append_string(rettv->vval.v_list, fresult, -1);
11989
11990 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011991 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011992
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011993 if (rettv->v_type == VAR_STRING)
11994 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011995#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011996}
11997
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011998static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011999static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012000
12001/*
12002 * Implementation of map() and filter().
12003 */
12004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012005filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012006{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012007 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000012008 listitem_T *li, *nli;
12009 list_T *l = NULL;
12010 dictitem_T *di;
12011 hashtab_T *ht;
12012 hashitem_T *hi;
12013 dict_T *d = NULL;
12014 typval_T save_val;
12015 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012016 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012017 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012018 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020012019 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012020 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012021 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012022 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012023
Bram Moolenaare9a41262005-01-15 22:18:47 +000012024 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012025 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012026 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012027 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012028 return;
12029 }
12030 else if (argvars[0].v_type == VAR_DICT)
12031 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012032 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012033 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012034 return;
12035 }
12036 else
12037 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000012038 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012039 return;
12040 }
12041
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012042 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012043 /* On type errors, the preceding call has already displayed an error
12044 * message. Avoid a misleading error message for an empty string that
12045 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012046 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012048 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012049
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012050 /* We reset "did_emsg" to be able to detect whether an error
12051 * occurred during evaluation of the expression. */
12052 save_did_emsg = did_emsg;
12053 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012054
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012055 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012056 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012057 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 vimvars[VV_KEY].vv_type = VAR_STRING;
12059
12060 ht = &d->dv_hashtab;
12061 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012062 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012063 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012065 if (!HASHITEM_EMPTY(hi))
12066 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012067 int r;
12068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012069 --todo;
12070 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012071 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012072 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12073 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012074 break;
12075 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012076 r = filter_map_one(&di->di_tv, expr, map, &rem);
12077 clear_tv(&vimvars[VV_KEY].vv_tv);
12078 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012079 break;
12080 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012081 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012082 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12083 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012084 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012085 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012087 }
12088 }
12089 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012090 }
12091 else
12092 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012093 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012095 for (li = l->lv_first; li != NULL; li = nli)
12096 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012097 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012098 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012099 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012100 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012101 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012102 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012103 break;
12104 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012105 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012106 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012107 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012108 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012109
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012110 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012111 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012112
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012113 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012114 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012115
12116 copy_tv(&argvars[0], rettv);
12117}
12118
12119 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012120filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012121{
Bram Moolenaar33570922005-01-25 22:26:29 +000012122 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012123 typval_T argv[3];
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012124 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000012125 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012126 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012127 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012128
Bram Moolenaar33570922005-01-25 22:26:29 +000012129 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012130 argv[0] = vimvars[VV_KEY].vv_tv;
12131 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012132 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012133 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012134 s = expr->vval.v_string;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012135 if (call_func(s, (int)STRLEN(s),
12136 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
12137 goto theend;
12138 }
12139 else if (expr->v_type == VAR_PARTIAL)
12140 {
12141 partial_T *partial = expr->vval.v_partial;
12142
12143 s = partial->pt_name;
12144 if (call_func(s, (int)STRLEN(s),
12145 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12146 == FAIL)
12147 goto theend;
12148 }
12149 else
12150 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012151 s = get_tv_string_buf_chk(expr, buf);
12152 if (s == NULL)
12153 goto theend;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012154 s = skipwhite(s);
12155 if (eval1(&s, &rettv, TRUE) == FAIL)
12156 goto theend;
12157 if (*s != NUL) /* check for trailing chars after expr */
12158 {
12159 EMSG2(_(e_invexpr2), s);
12160 goto theend;
12161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012162 }
12163 if (map)
12164 {
12165 /* map(): replace the list item value */
12166 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012167 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012168 *tv = rettv;
12169 }
12170 else
12171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012172 int error = FALSE;
12173
Bram Moolenaare9a41262005-01-15 22:18:47 +000012174 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012176 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012177 /* On type error, nothing has been removed; return FAIL to stop the
12178 * loop. The error message was given by get_tv_number_chk(). */
12179 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012180 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012181 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012182 retval = OK;
12183theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012184 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012185 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012186}
12187
12188/*
12189 * "filter()" function
12190 */
12191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012192f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012193{
12194 filter_map(argvars, rettv, FALSE);
12195}
12196
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012197/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012198 * "finddir({fname}[, {path}[, {count}]])" function
12199 */
12200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012201f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012202{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012203 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012204}
12205
12206/*
12207 * "findfile({fname}[, {path}[, {count}]])" function
12208 */
12209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012210f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012211{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012212 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213}
12214
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012215#ifdef FEAT_FLOAT
12216/*
12217 * "float2nr({float})" function
12218 */
12219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012220f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012221{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012222 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012223
12224 if (get_float_arg(argvars, &f) == OK)
12225 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012226# ifdef FEAT_NUM64
12227 if (f < -0x7fffffffffffffff)
12228 rettv->vval.v_number = -0x7fffffffffffffff;
12229 else if (f > 0x7fffffffffffffff)
12230 rettv->vval.v_number = 0x7fffffffffffffff;
12231 else
12232 rettv->vval.v_number = (varnumber_T)f;
12233# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012234 if (f < -0x7fffffff)
12235 rettv->vval.v_number = -0x7fffffff;
12236 else if (f > 0x7fffffff)
12237 rettv->vval.v_number = 0x7fffffff;
12238 else
12239 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012240# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012241 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012242}
12243
12244/*
12245 * "floor({float})" function
12246 */
12247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012248f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012249{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012250 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012251
12252 rettv->v_type = VAR_FLOAT;
12253 if (get_float_arg(argvars, &f) == OK)
12254 rettv->vval.v_float = floor(f);
12255 else
12256 rettv->vval.v_float = 0.0;
12257}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012258
12259/*
12260 * "fmod()" function
12261 */
12262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012263f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012264{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012265 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012266
12267 rettv->v_type = VAR_FLOAT;
12268 if (get_float_arg(argvars, &fx) == OK
12269 && get_float_arg(&argvars[1], &fy) == OK)
12270 rettv->vval.v_float = fmod(fx, fy);
12271 else
12272 rettv->vval.v_float = 0.0;
12273}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012274#endif
12275
Bram Moolenaar0d660222005-01-07 21:51:51 +000012276/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012277 * "fnameescape({string})" function
12278 */
12279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012280f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012281{
12282 rettv->vval.v_string = vim_strsave_fnameescape(
12283 get_tv_string(&argvars[0]), FALSE);
12284 rettv->v_type = VAR_STRING;
12285}
12286
12287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 * "fnamemodify({fname}, {mods})" function
12289 */
12290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012291f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292{
12293 char_u *fname;
12294 char_u *mods;
12295 int usedlen = 0;
12296 int len;
12297 char_u *fbuf = NULL;
12298 char_u buf[NUMBUFLEN];
12299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012300 fname = get_tv_string_chk(&argvars[0]);
12301 mods = get_tv_string_buf_chk(&argvars[1], buf);
12302 if (fname == NULL || mods == NULL)
12303 fname = NULL;
12304 else
12305 {
12306 len = (int)STRLEN(fname);
12307 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012310 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012314 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 vim_free(fbuf);
12316}
12317
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012318static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319
12320/*
12321 * "foldclosed()" function
12322 */
12323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012324foldclosed_both(
12325 typval_T *argvars UNUSED,
12326 typval_T *rettv,
12327 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328{
12329#ifdef FEAT_FOLDING
12330 linenr_T lnum;
12331 linenr_T first, last;
12332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012333 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12335 {
12336 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12337 {
12338 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 return;
12343 }
12344 }
12345#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012346 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347}
12348
12349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012350 * "foldclosed()" function
12351 */
12352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012353f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012354{
12355 foldclosed_both(argvars, rettv, FALSE);
12356}
12357
12358/*
12359 * "foldclosedend()" function
12360 */
12361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012362f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012363{
12364 foldclosed_both(argvars, rettv, TRUE);
12365}
12366
12367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 * "foldlevel()" function
12369 */
12370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012371f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372{
12373#ifdef FEAT_FOLDING
12374 linenr_T lnum;
12375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012378 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380}
12381
12382/*
12383 * "foldtext()" function
12384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012386f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387{
12388#ifdef FEAT_FOLDING
12389 linenr_T lnum;
12390 char_u *s;
12391 char_u *r;
12392 int len;
12393 char *txt;
12394#endif
12395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396 rettv->v_type = VAR_STRING;
12397 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012399 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12400 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12401 <= curbuf->b_ml.ml_line_count
12402 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 {
12404 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012405 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12406 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 {
12408 if (!linewhite(lnum))
12409 break;
12410 ++lnum;
12411 }
12412
12413 /* Find interesting text in this line. */
12414 s = skipwhite(ml_get(lnum));
12415 /* skip C comment-start */
12416 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012419 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012420 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012421 {
12422 s = skipwhite(ml_get(lnum + 1));
12423 if (*s == '*')
12424 s = skipwhite(s + 1);
12425 }
12426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 txt = _("+-%s%3ld lines: ");
12428 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012429 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 + 20 /* for %3ld */
12431 + STRLEN(s))); /* concatenated */
12432 if (r != NULL)
12433 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012434 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12435 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12436 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 len = (int)STRLEN(r);
12438 STRCAT(r, s);
12439 /* remove 'foldmarker' and 'commentstring' */
12440 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 }
12443 }
12444#endif
12445}
12446
12447/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012448 * "foldtextresult(lnum)" function
12449 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012451f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012452{
12453#ifdef FEAT_FOLDING
12454 linenr_T lnum;
12455 char_u *text;
12456 char_u buf[51];
12457 foldinfo_T foldinfo;
12458 int fold_count;
12459#endif
12460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012461 rettv->v_type = VAR_STRING;
12462 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012463#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012464 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012465 /* treat illegal types and illegal string values for {lnum} the same */
12466 if (lnum < 0)
12467 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012468 fold_count = foldedCount(curwin, lnum, &foldinfo);
12469 if (fold_count > 0)
12470 {
12471 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12472 &foldinfo, buf);
12473 if (text == buf)
12474 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012476 }
12477#endif
12478}
12479
12480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 * "foreground()" function
12482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012484f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486#ifdef FEAT_GUI
12487 if (gui.in_use)
12488 gui_mch_set_foreground();
12489#else
12490# ifdef WIN32
12491 win32_set_foreground();
12492# endif
12493#endif
12494}
12495
12496/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012497 * "function()" function
12498 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012500f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012501{
12502 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012503 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012504 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012505 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012506
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012507 if (argvars[0].v_type == VAR_FUNC)
12508 {
12509 /* function(MyFunc, [arg], dict) */
12510 s = argvars[0].vval.v_string;
12511 }
12512 else if (argvars[0].v_type == VAR_PARTIAL
12513 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012514 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012515 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012516 arg_pt = argvars[0].vval.v_partial;
12517 s = arg_pt->pt_name;
12518 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012519 else
12520 {
12521 /* function('MyFunc', [arg], dict) */
12522 s = get_tv_string(&argvars[0]);
12523 use_string = TRUE;
12524 }
12525
12526 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012527 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012528 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012529 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12530 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012531 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012532 else
12533 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012534 int dict_idx = 0;
12535 int arg_idx = 0;
12536 list_T *list = NULL;
12537
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012538 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012539 {
12540 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012541 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012542
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012543 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12544 * also be called from another script. Using trans_function_name()
12545 * would also work, but some plugins depend on the name being
12546 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012547 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012548 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12549 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012550 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012551 STRCPY(name, sid_buf);
12552 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012553 }
12554 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012555 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012556 name = vim_strsave(s);
12557
12558 if (argvars[1].v_type != VAR_UNKNOWN)
12559 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012560 if (argvars[2].v_type != VAR_UNKNOWN)
12561 {
12562 /* function(name, [args], dict) */
12563 arg_idx = 1;
12564 dict_idx = 2;
12565 }
12566 else if (argvars[1].v_type == VAR_DICT)
12567 /* function(name, dict) */
12568 dict_idx = 1;
12569 else
12570 /* function(name, [args]) */
12571 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012572 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012573 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012574 if (argvars[dict_idx].v_type != VAR_DICT)
12575 {
12576 EMSG(_("E922: expected a dict"));
12577 vim_free(name);
12578 return;
12579 }
12580 if (argvars[dict_idx].vval.v_dict == NULL)
12581 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012582 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012583 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012584 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012585 if (argvars[arg_idx].v_type != VAR_LIST)
12586 {
12587 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12588 vim_free(name);
12589 return;
12590 }
12591 list = argvars[arg_idx].vval.v_list;
12592 if (list == NULL || list->lv_len == 0)
12593 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012594 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012595 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012596 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012597 {
12598 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012599
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012600 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012601 if (pt == NULL)
12602 vim_free(name);
12603 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012604 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012605 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012606 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012607 listitem_T *li;
12608 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012609 int arg_len = 0;
12610 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012611
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012612 if (arg_pt != NULL)
12613 arg_len = arg_pt->pt_argc;
12614 if (list != NULL)
12615 lv_len = list->lv_len;
12616 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012617 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012618 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012619 if (pt->pt_argv == NULL)
12620 {
12621 vim_free(pt);
12622 vim_free(name);
12623 return;
12624 }
12625 else
12626 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012627 for (i = 0; i < arg_len; i++)
12628 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12629 if (lv_len > 0)
12630 for (li = list->lv_first; li != NULL;
12631 li = li->li_next)
12632 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012633 }
12634 }
12635
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012636 /* For "function(dict.func, [], dict)" and "func" is a partial
12637 * use "dict". That is backwards compatible. */
12638 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012639 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012640 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012641 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12642 ++pt->pt_dict->dv_refcount;
12643 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012644 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012645 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012646 /* If the dict was bound automatically the result is also
12647 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012648 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012649 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012650 if (pt->pt_dict != NULL)
12651 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012652 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012653
12654 pt->pt_refcount = 1;
12655 pt->pt_name = name;
12656 func_ref(pt->pt_name);
12657 }
12658 rettv->v_type = VAR_PARTIAL;
12659 rettv->vval.v_partial = pt;
12660 }
12661 else
12662 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012663 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012664 rettv->v_type = VAR_FUNC;
12665 rettv->vval.v_string = name;
12666 func_ref(name);
12667 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012668 }
12669}
12670
12671/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012672 * "garbagecollect()" function
12673 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012675f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012676{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012677 /* This is postponed until we are back at the toplevel, because we may be
12678 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12679 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012680
12681 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12682 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012683}
12684
12685/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012686 * "get()" function
12687 */
12688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012689f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012690{
Bram Moolenaar33570922005-01-25 22:26:29 +000012691 listitem_T *li;
12692 list_T *l;
12693 dictitem_T *di;
12694 dict_T *d;
12695 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012696
Bram Moolenaare9a41262005-01-15 22:18:47 +000012697 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012698 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012699 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012701 int error = FALSE;
12702
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012703 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012704 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012705 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012706 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012707 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012708 else if (argvars[0].v_type == VAR_DICT)
12709 {
12710 if ((d = argvars[0].vval.v_dict) != NULL)
12711 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012712 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012713 if (di != NULL)
12714 tv = &di->di_tv;
12715 }
12716 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012717 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012718 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012719 partial_T *pt;
12720 partial_T fref_pt;
12721
12722 if (argvars[0].v_type == VAR_PARTIAL)
12723 pt = argvars[0].vval.v_partial;
12724 else
12725 {
12726 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12727 fref_pt.pt_name = argvars[0].vval.v_string;
12728 pt = &fref_pt;
12729 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012730
12731 if (pt != NULL)
12732 {
12733 char_u *what = get_tv_string(&argvars[1]);
12734
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012735 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012736 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012737 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012738 if (pt->pt_name == NULL)
12739 rettv->vval.v_string = NULL;
12740 else
12741 rettv->vval.v_string = vim_strsave(pt->pt_name);
12742 }
12743 else if (STRCMP(what, "dict") == 0)
12744 {
12745 rettv->v_type = VAR_DICT;
12746 rettv->vval.v_dict = pt->pt_dict;
12747 if (pt->pt_dict != NULL)
12748 ++pt->pt_dict->dv_refcount;
12749 }
12750 else if (STRCMP(what, "args") == 0)
12751 {
12752 rettv->v_type = VAR_LIST;
12753 if (rettv_list_alloc(rettv) == OK)
12754 {
12755 int i;
12756
12757 for (i = 0; i < pt->pt_argc; ++i)
12758 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12759 }
12760 }
12761 else
12762 EMSG2(_(e_invarg2), what);
12763 return;
12764 }
12765 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012766 else
12767 EMSG2(_(e_listdictarg), "get()");
12768
12769 if (tv == NULL)
12770 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012771 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012772 copy_tv(&argvars[2], rettv);
12773 }
12774 else
12775 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012776}
12777
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012778static 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 +000012779
12780/*
12781 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012782 * Return a range (from start to end) of lines in rettv from the specified
12783 * buffer.
12784 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012785 */
12786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012787get_buffer_lines(
12788 buf_T *buf,
12789 linenr_T start,
12790 linenr_T end,
12791 int retlist,
12792 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012793{
12794 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012795
Bram Moolenaar959a1432013-12-14 12:17:38 +010012796 rettv->v_type = VAR_STRING;
12797 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012798 if (retlist && rettv_list_alloc(rettv) == FAIL)
12799 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012800
12801 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12802 return;
12803
12804 if (!retlist)
12805 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012806 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12807 p = ml_get_buf(buf, start, FALSE);
12808 else
12809 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012810 rettv->vval.v_string = vim_strsave(p);
12811 }
12812 else
12813 {
12814 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012815 return;
12816
12817 if (start < 1)
12818 start = 1;
12819 if (end > buf->b_ml.ml_line_count)
12820 end = buf->b_ml.ml_line_count;
12821 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012822 if (list_append_string(rettv->vval.v_list,
12823 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012824 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012825 }
12826}
12827
12828/*
12829 * "getbufline()" function
12830 */
12831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012832f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012833{
12834 linenr_T lnum;
12835 linenr_T end;
12836 buf_T *buf;
12837
12838 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12839 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012840 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012841 --emsg_off;
12842
Bram Moolenaar661b1822005-07-28 22:36:45 +000012843 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012844 if (argvars[2].v_type == VAR_UNKNOWN)
12845 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012846 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012847 end = get_tv_lnum_buf(&argvars[2], buf);
12848
Bram Moolenaar342337a2005-07-21 21:11:17 +000012849 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012850}
12851
Bram Moolenaar0d660222005-01-07 21:51:51 +000012852/*
12853 * "getbufvar()" function
12854 */
12855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012856f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012857{
12858 buf_T *buf;
12859 buf_T *save_curbuf;
12860 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012861 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012862 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012864 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12865 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012866 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012867 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012868
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012869 rettv->v_type = VAR_STRING;
12870 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012871
12872 if (buf != NULL && varname != NULL)
12873 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012874 /* set curbuf to be our buf, temporarily */
12875 save_curbuf = curbuf;
12876 curbuf = buf;
12877
Bram Moolenaar0d660222005-01-07 21:51:51 +000012878 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012879 {
12880 if (get_option_tv(&varname, rettv, TRUE) == OK)
12881 done = TRUE;
12882 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012883 else if (STRCMP(varname, "changedtick") == 0)
12884 {
12885 rettv->v_type = VAR_NUMBER;
12886 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012887 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012888 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012889 else
12890 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012891 /* Look up the variable. */
12892 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12893 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12894 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012895 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012896 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012897 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012898 done = TRUE;
12899 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012900 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012901
12902 /* restore previous notion of curbuf */
12903 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012904 }
12905
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012906 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12907 /* use the default value */
12908 copy_tv(&argvars[2], rettv);
12909
Bram Moolenaar0d660222005-01-07 21:51:51 +000012910 --emsg_off;
12911}
12912
12913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 * "getchar()" function
12915 */
12916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012917f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918{
12919 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012920 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012922 /* Position the cursor. Needed after a message that ends in a space. */
12923 windgoto(msg_row, msg_col);
12924
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925 ++no_mapping;
12926 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012927 for (;;)
12928 {
12929 if (argvars[0].v_type == VAR_UNKNOWN)
12930 /* getchar(): blocking wait. */
12931 n = safe_vgetc();
12932 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12933 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012934 n = vpeekc_any();
12935 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012936 /* illegal argument or getchar(0) and no char avail: return zero */
12937 n = 0;
12938 else
12939 /* getchar(0) and char avail: return char */
12940 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012941
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012942 if (n == K_IGNORE)
12943 continue;
12944 break;
12945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946 --no_mapping;
12947 --allow_keys;
12948
Bram Moolenaar219b8702006-11-01 14:32:36 +000012949 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012950 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012951 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12952 vimvars[VV_MOUSE_COL].vv_nr = 0;
12953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 if (IS_SPECIAL(n) || mod_mask != 0)
12956 {
12957 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12958 int i = 0;
12959
12960 /* Turn a special key into three bytes, plus modifier. */
12961 if (mod_mask != 0)
12962 {
12963 temp[i++] = K_SPECIAL;
12964 temp[i++] = KS_MODIFIER;
12965 temp[i++] = mod_mask;
12966 }
12967 if (IS_SPECIAL(n))
12968 {
12969 temp[i++] = K_SPECIAL;
12970 temp[i++] = K_SECOND(n);
12971 temp[i++] = K_THIRD(n);
12972 }
12973#ifdef FEAT_MBYTE
12974 else if (has_mbyte)
12975 i += (*mb_char2bytes)(n, temp + i);
12976#endif
12977 else
12978 temp[i++] = n;
12979 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980 rettv->v_type = VAR_STRING;
12981 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012982
12983#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012984 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012985 {
12986 int row = mouse_row;
12987 int col = mouse_col;
12988 win_T *win;
12989 linenr_T lnum;
12990# ifdef FEAT_WINDOWS
12991 win_T *wp;
12992# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012993 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012994
12995 if (row >= 0 && col >= 0)
12996 {
12997 /* Find the window at the mouse coordinates and compute the
12998 * text position. */
12999 win = mouse_find_win(&row, &col);
13000 (void)mouse_comp_pos(win, &row, &col, &lnum);
13001# ifdef FEAT_WINDOWS
13002 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000013003 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000013004# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000013005 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020013006 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000013007 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
13008 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
13009 }
13010 }
13011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012 }
13013}
13014
13015/*
13016 * "getcharmod()" function
13017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013019f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013021 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022}
13023
13024/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013025 * "getcharsearch()" function
13026 */
13027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013028f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013029{
13030 if (rettv_dict_alloc(rettv) != FAIL)
13031 {
13032 dict_T *dict = rettv->vval.v_dict;
13033
13034 dict_add_nr_str(dict, "char", 0L, last_csearch());
13035 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
13036 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
13037 }
13038}
13039
13040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 * "getcmdline()" function
13042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013044f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046 rettv->v_type = VAR_STRING;
13047 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048}
13049
13050/*
13051 * "getcmdpos()" function
13052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013054f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057}
13058
13059/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013060 * "getcmdtype()" function
13061 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013063f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013064{
13065 rettv->v_type = VAR_STRING;
13066 rettv->vval.v_string = alloc(2);
13067 if (rettv->vval.v_string != NULL)
13068 {
13069 rettv->vval.v_string[0] = get_cmdline_type();
13070 rettv->vval.v_string[1] = NUL;
13071 }
13072}
13073
13074/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013075 * "getcmdwintype()" function
13076 */
13077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013078f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013079{
13080 rettv->v_type = VAR_STRING;
13081 rettv->vval.v_string = NULL;
13082#ifdef FEAT_CMDWIN
13083 rettv->vval.v_string = alloc(2);
13084 if (rettv->vval.v_string != NULL)
13085 {
13086 rettv->vval.v_string[0] = cmdwin_type;
13087 rettv->vval.v_string[1] = NUL;
13088 }
13089#endif
13090}
13091
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013092#if defined(FEAT_CMDL_COMPL)
13093/*
13094 * "getcompletion()" function
13095 */
13096 static void
13097f_getcompletion(typval_T *argvars, typval_T *rettv)
13098{
13099 char_u *pat;
13100 expand_T xpc;
13101 int options = WILD_KEEP_ALL | WILD_SILENT | WILD_USE_NL
13102 | WILD_LIST_NOTFOUND | WILD_NO_BEEP;
13103
13104 if (p_wic)
13105 options |= WILD_ICASE;
13106
13107 ExpandInit(&xpc);
13108 xpc.xp_pattern = get_tv_string(&argvars[0]);
13109 xpc.xp_pattern_len = STRLEN(xpc.xp_pattern);
13110 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
13111 if (xpc.xp_context == EXPAND_NOTHING)
13112 {
13113 if (argvars[1].v_type == VAR_STRING)
13114 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
13115 else
13116 EMSG(_(e_invarg));
13117 return;
13118 }
13119
13120 if (xpc.xp_context == EXPAND_MENUS)
13121 {
13122 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
13123 xpc.xp_pattern_len = STRLEN(xpc.xp_pattern);
13124 }
13125
13126 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
13127 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
13128 {
13129 int i;
13130
13131 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
13132
13133 for (i = 0; i < xpc.xp_numfiles; i++)
13134 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13135 }
13136 vim_free(pat);
13137 ExpandCleanup(&xpc);
13138}
13139#endif
13140
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142 * "getcwd()" function
13143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013145f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013147 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013148 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013150 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013151 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010013152
13153 wp = find_tabwin(&argvars[0], &argvars[1]);
13154 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013156 if (wp->w_localdir != NULL)
13157 rettv->vval.v_string = vim_strsave(wp->w_localdir);
13158 else if(globaldir != NULL)
13159 rettv->vval.v_string = vim_strsave(globaldir);
13160 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020013161 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013162 cwd = alloc(MAXPATHL);
13163 if (cwd != NULL)
13164 {
13165 if (mch_dirname(cwd, MAXPATHL) != FAIL)
13166 rettv->vval.v_string = vim_strsave(cwd);
13167 vim_free(cwd);
13168 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020013169 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010013170#ifdef BACKSLASH_IN_FILENAME
13171 if (rettv->vval.v_string != NULL)
13172 slash_adjust(rettv->vval.v_string);
13173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 }
13175}
13176
13177/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013178 * "getfontname()" function
13179 */
13180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013181f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013182{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013183 rettv->v_type = VAR_STRING;
13184 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013185#ifdef FEAT_GUI
13186 if (gui.in_use)
13187 {
13188 GuiFont font;
13189 char_u *name = NULL;
13190
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013191 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013192 {
13193 /* Get the "Normal" font. Either the name saved by
13194 * hl_set_font_name() or from the font ID. */
13195 font = gui.norm_font;
13196 name = hl_get_font_name();
13197 }
13198 else
13199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013200 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013201 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13202 return;
13203 font = gui_mch_get_font(name, FALSE);
13204 if (font == NOFONT)
13205 return; /* Invalid font name, return empty string. */
13206 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013208 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013209 gui_mch_free_font(font);
13210 }
13211#endif
13212}
13213
13214/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013215 * "getfperm({fname})" function
13216 */
13217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013218f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013219{
13220 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013221 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013222 char_u *perm = NULL;
13223 char_u flags[] = "rwx";
13224 int i;
13225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013226 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013229 if (mch_stat((char *)fname, &st) >= 0)
13230 {
13231 perm = vim_strsave((char_u *)"---------");
13232 if (perm != NULL)
13233 {
13234 for (i = 0; i < 9; i++)
13235 {
13236 if (st.st_mode & (1 << (8 - i)))
13237 perm[i] = flags[i % 3];
13238 }
13239 }
13240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013242}
13243
13244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 * "getfsize({fname})" function
13246 */
13247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013248f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249{
13250 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013251 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013253 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256
13257 if (mch_stat((char *)fname, &st) >= 0)
13258 {
13259 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013260 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013264
13265 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013266 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013267 rettv->vval.v_number = -2;
13268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269 }
13270 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272}
13273
13274/*
13275 * "getftime({fname})" function
13276 */
13277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013278f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279{
13280 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013281 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013283 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284
13285 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013286 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289}
13290
13291/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013292 * "getftype({fname})" function
13293 */
13294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013295f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013296{
13297 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013298 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013299 char_u *type = NULL;
13300 char *t;
13301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013302 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013304 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013305 if (mch_lstat((char *)fname, &st) >= 0)
13306 {
13307#ifdef S_ISREG
13308 if (S_ISREG(st.st_mode))
13309 t = "file";
13310 else if (S_ISDIR(st.st_mode))
13311 t = "dir";
13312# ifdef S_ISLNK
13313 else if (S_ISLNK(st.st_mode))
13314 t = "link";
13315# endif
13316# ifdef S_ISBLK
13317 else if (S_ISBLK(st.st_mode))
13318 t = "bdev";
13319# endif
13320# ifdef S_ISCHR
13321 else if (S_ISCHR(st.st_mode))
13322 t = "cdev";
13323# endif
13324# ifdef S_ISFIFO
13325 else if (S_ISFIFO(st.st_mode))
13326 t = "fifo";
13327# endif
13328# ifdef S_ISSOCK
13329 else if (S_ISSOCK(st.st_mode))
13330 t = "fifo";
13331# endif
13332 else
13333 t = "other";
13334#else
13335# ifdef S_IFMT
13336 switch (st.st_mode & S_IFMT)
13337 {
13338 case S_IFREG: t = "file"; break;
13339 case S_IFDIR: t = "dir"; break;
13340# ifdef S_IFLNK
13341 case S_IFLNK: t = "link"; break;
13342# endif
13343# ifdef S_IFBLK
13344 case S_IFBLK: t = "bdev"; break;
13345# endif
13346# ifdef S_IFCHR
13347 case S_IFCHR: t = "cdev"; break;
13348# endif
13349# ifdef S_IFIFO
13350 case S_IFIFO: t = "fifo"; break;
13351# endif
13352# ifdef S_IFSOCK
13353 case S_IFSOCK: t = "socket"; break;
13354# endif
13355 default: t = "other";
13356 }
13357# else
13358 if (mch_isdir(fname))
13359 t = "dir";
13360 else
13361 t = "file";
13362# endif
13363#endif
13364 type = vim_strsave((char_u *)t);
13365 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013366 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013367}
13368
13369/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013370 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013371 */
13372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013373f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013374{
13375 linenr_T lnum;
13376 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013377 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013378
13379 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013380 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013381 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013382 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013383 retlist = FALSE;
13384 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013385 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013386 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013387 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013388 retlist = TRUE;
13389 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013390
Bram Moolenaar342337a2005-07-21 21:11:17 +000013391 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013392}
13393
13394/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013395 * "getmatches()" function
13396 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013398f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013399{
13400#ifdef FEAT_SEARCH_EXTRA
13401 dict_T *dict;
13402 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013403 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013404
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013405 if (rettv_list_alloc(rettv) == OK)
13406 {
13407 while (cur != NULL)
13408 {
13409 dict = dict_alloc();
13410 if (dict == NULL)
13411 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013412 if (cur->match.regprog == NULL)
13413 {
13414 /* match added with matchaddpos() */
13415 for (i = 0; i < MAXPOSMATCH; ++i)
13416 {
13417 llpos_T *llpos;
13418 char buf[6];
13419 list_T *l;
13420
13421 llpos = &cur->pos.pos[i];
13422 if (llpos->lnum == 0)
13423 break;
13424 l = list_alloc();
13425 if (l == NULL)
13426 break;
13427 list_append_number(l, (varnumber_T)llpos->lnum);
13428 if (llpos->col > 0)
13429 {
13430 list_append_number(l, (varnumber_T)llpos->col);
13431 list_append_number(l, (varnumber_T)llpos->len);
13432 }
13433 sprintf(buf, "pos%d", i + 1);
13434 dict_add_list(dict, buf, l);
13435 }
13436 }
13437 else
13438 {
13439 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13440 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013441 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013442 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13443 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013444# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013445 if (cur->conceal_char)
13446 {
13447 char_u buf[MB_MAXBYTES + 1];
13448
13449 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13450 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13451 }
13452# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013453 list_append_dict(rettv->vval.v_list, dict);
13454 cur = cur->next;
13455 }
13456 }
13457#endif
13458}
13459
13460/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013461 * "getpid()" function
13462 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013464f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013465{
13466 rettv->vval.v_number = mch_get_pid();
13467}
13468
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013470getpos_both(
13471 typval_T *argvars,
13472 typval_T *rettv,
13473 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013474{
Bram Moolenaara5525202006-03-02 22:52:09 +000013475 pos_T *fp;
13476 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013477 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013478
13479 if (rettv_list_alloc(rettv) == OK)
13480 {
13481 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013482 if (getcurpos)
13483 fp = &curwin->w_cursor;
13484 else
13485 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013486 if (fnum != -1)
13487 list_append_number(l, (varnumber_T)fnum);
13488 else
13489 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013490 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13491 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013492 list_append_number(l, (fp != NULL)
13493 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013494 : (varnumber_T)0);
13495 list_append_number(l,
13496#ifdef FEAT_VIRTUALEDIT
13497 (fp != NULL) ? (varnumber_T)fp->coladd :
13498#endif
13499 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013500 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013501 {
13502 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013503 list_append_number(l, curwin->w_curswant == MAXCOL ?
13504 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013505 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013506 }
13507 else
13508 rettv->vval.v_number = FALSE;
13509}
13510
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013511
13512/*
13513 * "getcurpos()" function
13514 */
13515 static void
13516f_getcurpos(typval_T *argvars, typval_T *rettv)
13517{
13518 getpos_both(argvars, rettv, TRUE);
13519}
13520
13521/*
13522 * "getpos(string)" function
13523 */
13524 static void
13525f_getpos(typval_T *argvars, typval_T *rettv)
13526{
13527 getpos_both(argvars, rettv, FALSE);
13528}
13529
Bram Moolenaara5525202006-03-02 22:52:09 +000013530/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013531 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013532 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013534f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013535{
13536#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013537 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013538#endif
13539
Bram Moolenaar2641f772005-03-25 21:58:17 +000013540#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013541 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013542 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013543 wp = NULL;
13544 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13545 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013546 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013547 if (wp == NULL)
13548 return;
13549 }
13550
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013551 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013552 }
13553#endif
13554}
13555
13556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 * "getreg()" function
13558 */
13559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013560f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561{
13562 char_u *strregname;
13563 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013564 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013565 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013568 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 strregname = get_tv_string_chk(&argvars[0]);
13571 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013572 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013573 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013574 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013575 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013576 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013577 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013580 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013581
13582 if (error)
13583 return;
13584
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 regname = (strregname == NULL ? '"' : *strregname);
13586 if (regname == 0)
13587 regname = '"';
13588
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013589 if (return_list)
13590 {
13591 rettv->v_type = VAR_LIST;
13592 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13593 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013594 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013595 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013596 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013597 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013598 }
13599 else
13600 {
13601 rettv->v_type = VAR_STRING;
13602 rettv->vval.v_string = get_reg_contents(regname,
13603 arg2 ? GREG_EXPR_SRC : 0);
13604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605}
13606
13607/*
13608 * "getregtype()" function
13609 */
13610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013611f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612{
13613 char_u *strregname;
13614 int regname;
13615 char_u buf[NUMBUFLEN + 2];
13616 long reglen = 0;
13617
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013618 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013619 {
13620 strregname = get_tv_string_chk(&argvars[0]);
13621 if (strregname == NULL) /* type error; errmsg already given */
13622 {
13623 rettv->v_type = VAR_STRING;
13624 rettv->vval.v_string = NULL;
13625 return;
13626 }
13627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 else
13629 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013630 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631
13632 regname = (strregname == NULL ? '"' : *strregname);
13633 if (regname == 0)
13634 regname = '"';
13635
13636 buf[0] = NUL;
13637 buf[1] = NUL;
13638 switch (get_reg_type(regname, &reglen))
13639 {
13640 case MLINE: buf[0] = 'V'; break;
13641 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 case MBLOCK:
13643 buf[0] = Ctrl_V;
13644 sprintf((char *)buf + 1, "%ld", reglen + 1);
13645 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647 rettv->v_type = VAR_STRING;
13648 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649}
13650
13651/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013652 * "gettabvar()" function
13653 */
13654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013655f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013656{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013657 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013658 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013659 dictitem_T *v;
13660 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013661 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013662
13663 rettv->v_type = VAR_STRING;
13664 rettv->vval.v_string = NULL;
13665
13666 varname = get_tv_string_chk(&argvars[1]);
13667 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13668 if (tp != NULL && varname != NULL)
13669 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013670 /* Set tp to be our tabpage, temporarily. Also set the window to the
13671 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013672 if (switch_win(&oldcurwin, &oldtabpage,
13673 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013674 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013675 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013676 /* look up the variable */
13677 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13678 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13679 if (v != NULL)
13680 {
13681 copy_tv(&v->di_tv, rettv);
13682 done = TRUE;
13683 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013684 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013685
13686 /* restore previous notion of curwin */
13687 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013688 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013689
13690 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013691 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013692}
13693
13694/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013695 * "gettabwinvar()" function
13696 */
13697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013698f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013699{
13700 getwinvar(argvars, rettv, 1);
13701}
13702
13703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 * "getwinposx()" function
13705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013707f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013709 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710#ifdef FEAT_GUI
13711 if (gui.in_use)
13712 {
13713 int x, y;
13714
13715 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 }
13718#endif
13719}
13720
13721/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013722 * "win_findbuf()" function
13723 */
13724 static void
13725f_win_findbuf(typval_T *argvars, typval_T *rettv)
13726{
13727 if (rettv_list_alloc(rettv) != FAIL)
13728 win_findbuf(argvars, rettv->vval.v_list);
13729}
13730
13731/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013732 * "win_getid()" function
13733 */
13734 static void
13735f_win_getid(typval_T *argvars, typval_T *rettv)
13736{
13737 rettv->vval.v_number = win_getid(argvars);
13738}
13739
13740/*
13741 * "win_gotoid()" function
13742 */
13743 static void
13744f_win_gotoid(typval_T *argvars, typval_T *rettv)
13745{
13746 rettv->vval.v_number = win_gotoid(argvars);
13747}
13748
13749/*
13750 * "win_id2tabwin()" function
13751 */
13752 static void
13753f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13754{
13755 if (rettv_list_alloc(rettv) != FAIL)
13756 win_id2tabwin(argvars, rettv->vval.v_list);
13757}
13758
13759/*
13760 * "win_id2win()" function
13761 */
13762 static void
13763f_win_id2win(typval_T *argvars, typval_T *rettv)
13764{
13765 rettv->vval.v_number = win_id2win(argvars);
13766}
13767
13768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 * "getwinposy()" function
13770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013772f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775#ifdef FEAT_GUI
13776 if (gui.in_use)
13777 {
13778 int x, y;
13779
13780 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782 }
13783#endif
13784}
13785
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013786/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013787 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013788 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013789 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013790find_win_by_nr(
13791 typval_T *vp,
13792 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013793{
13794#ifdef FEAT_WINDOWS
13795 win_T *wp;
13796#endif
13797 int nr;
13798
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013799 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013800
13801#ifdef FEAT_WINDOWS
13802 if (nr < 0)
13803 return NULL;
13804 if (nr == 0)
13805 return curwin;
13806
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013807 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13808 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013809 if (nr >= LOWEST_WIN_ID)
13810 {
13811 if (wp->w_id == nr)
13812 return wp;
13813 }
13814 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013815 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013816 if (nr >= LOWEST_WIN_ID)
13817 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013818 return wp;
13819#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013820 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013821 return curwin;
13822 return NULL;
13823#endif
13824}
13825
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013827 * Find window specified by "wvp" in tabpage "tvp".
13828 */
13829 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013830find_tabwin(
13831 typval_T *wvp, /* VAR_UNKNOWN for current window */
13832 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013833{
13834 win_T *wp = NULL;
13835 tabpage_T *tp = NULL;
13836 long n;
13837
13838 if (wvp->v_type != VAR_UNKNOWN)
13839 {
13840 if (tvp->v_type != VAR_UNKNOWN)
13841 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013842 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013843 if (n >= 0)
13844 tp = find_tabpage(n);
13845 }
13846 else
13847 tp = curtab;
13848
13849 if (tp != NULL)
13850 wp = find_win_by_nr(wvp, tp);
13851 }
13852 else
13853 wp = curwin;
13854
13855 return wp;
13856}
13857
13858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859 * "getwinvar()" function
13860 */
13861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013862f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013864 getwinvar(argvars, rettv, 0);
13865}
13866
13867/*
13868 * getwinvar() and gettabwinvar()
13869 */
13870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013871getwinvar(
13872 typval_T *argvars,
13873 typval_T *rettv,
13874 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013875{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013876 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013878 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013879 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013880 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013881#ifdef FEAT_WINDOWS
13882 win_T *oldcurwin;
13883 tabpage_T *oldtabpage;
13884 int need_switch_win;
13885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013887#ifdef FEAT_WINDOWS
13888 if (off == 1)
13889 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13890 else
13891 tp = curtab;
13892#endif
13893 win = find_win_by_nr(&argvars[off], tp);
13894 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013895 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013897 rettv->v_type = VAR_STRING;
13898 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
13900 if (win != NULL && varname != NULL)
13901 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013902#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013903 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013904 * otherwise the window is not valid. Only do this when needed,
13905 * autocommands get blocked. */
13906 need_switch_win = !(tp == curtab && win == curwin);
13907 if (!need_switch_win
13908 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13909#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013910 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013911 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013912 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013913 if (get_option_tv(&varname, rettv, 1) == OK)
13914 done = TRUE;
13915 }
13916 else
13917 {
13918 /* Look up the variable. */
13919 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13920 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13921 varname, FALSE);
13922 if (v != NULL)
13923 {
13924 copy_tv(&v->di_tv, rettv);
13925 done = TRUE;
13926 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013929
Bram Moolenaarba117c22015-09-29 16:53:22 +020013930#ifdef FEAT_WINDOWS
13931 if (need_switch_win)
13932 /* restore previous notion of curwin */
13933 restore_win(oldcurwin, oldtabpage, TRUE);
13934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 }
13936
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013937 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13938 /* use the default return value */
13939 copy_tv(&argvars[off + 2], rettv);
13940
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 --emsg_off;
13942}
13943
13944/*
13945 * "glob()" function
13946 */
13947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013948f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013950 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013952 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013954 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013955 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013957 if (argvars[1].v_type != VAR_UNKNOWN)
13958 {
13959 if (get_tv_number_chk(&argvars[1], &error))
13960 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013961 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013962 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013963 if (get_tv_number_chk(&argvars[2], &error))
13964 {
13965 rettv->v_type = VAR_LIST;
13966 rettv->vval.v_list = NULL;
13967 }
13968 if (argvars[3].v_type != VAR_UNKNOWN
13969 && get_tv_number_chk(&argvars[3], &error))
13970 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013971 }
13972 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013973 if (!error)
13974 {
13975 ExpandInit(&xpc);
13976 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013977 if (p_wic)
13978 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013979 if (rettv->v_type == VAR_STRING)
13980 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013981 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013982 else if (rettv_list_alloc(rettv) != FAIL)
13983 {
13984 int i;
13985
13986 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13987 NULL, options, WILD_ALL_KEEP);
13988 for (i = 0; i < xpc.xp_numfiles; i++)
13989 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13990
13991 ExpandCleanup(&xpc);
13992 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013993 }
13994 else
13995 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996}
13997
13998/*
13999 * "globpath()" function
14000 */
14001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014002f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014004 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014006 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014007 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014008 garray_T ga;
14009 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014011 /* When the optional second argument is non-zero, don't remove matches
14012 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014013 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014014 if (argvars[2].v_type != VAR_UNKNOWN)
14015 {
14016 if (get_tv_number_chk(&argvars[2], &error))
14017 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010014018 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014019 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010014020 if (get_tv_number_chk(&argvars[3], &error))
14021 {
14022 rettv->v_type = VAR_LIST;
14023 rettv->vval.v_list = NULL;
14024 }
14025 if (argvars[4].v_type != VAR_UNKNOWN
14026 && get_tv_number_chk(&argvars[4], &error))
14027 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014028 }
14029 }
14030 if (file != NULL && !error)
14031 {
14032 ga_init2(&ga, (int)sizeof(char_u *), 10);
14033 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
14034 if (rettv->v_type == VAR_STRING)
14035 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
14036 else if (rettv_list_alloc(rettv) != FAIL)
14037 for (i = 0; i < ga.ga_len; ++i)
14038 list_append_string(rettv->vval.v_list,
14039 ((char_u **)(ga.ga_data))[i], -1);
14040 ga_clear_strings(&ga);
14041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044}
14045
14046/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014047 * "glob2regpat()" function
14048 */
14049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014050f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014051{
14052 char_u *pat = get_tv_string_chk(&argvars[0]);
14053
14054 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010014055 rettv->vval.v_string = (pat == NULL)
14056 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014057}
14058
14059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 * "has()" function
14061 */
14062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014063f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064{
14065 int i;
14066 char_u *name;
14067 int n = FALSE;
14068 static char *(has_list[]) =
14069 {
14070#ifdef AMIGA
14071 "amiga",
14072# ifdef FEAT_ARP
14073 "arp",
14074# endif
14075#endif
14076#ifdef __BEOS__
14077 "beos",
14078#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000014079#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 "mac",
14081#endif
14082#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010014083 "macunix", /* built with 'darwin' enabled */
14084#endif
14085#if defined(__APPLE__) && __APPLE__ == 1
14086 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088#ifdef __QNX__
14089 "qnx",
14090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091#ifdef UNIX
14092 "unix",
14093#endif
14094#ifdef VMS
14095 "vms",
14096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097#ifdef WIN32
14098 "win32",
14099#endif
14100#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
14101 "win32unix",
14102#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010014103#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 "win64",
14105#endif
14106#ifdef EBCDIC
14107 "ebcdic",
14108#endif
14109#ifndef CASE_INSENSITIVE_FILENAME
14110 "fname_case",
14111#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014112#ifdef HAVE_ACL
14113 "acl",
14114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115#ifdef FEAT_ARABIC
14116 "arabic",
14117#endif
14118#ifdef FEAT_AUTOCMD
14119 "autocmd",
14120#endif
14121#ifdef FEAT_BEVAL
14122 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000014123# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
14124 "balloon_multiline",
14125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126#endif
14127#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
14128 "builtin_terms",
14129# ifdef ALL_BUILTIN_TCAPS
14130 "all_builtin_terms",
14131# endif
14132#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020014133#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
14134 || defined(FEAT_GUI_W32) \
14135 || defined(FEAT_GUI_MOTIF))
14136 "browsefilter",
14137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138#ifdef FEAT_BYTEOFF
14139 "byte_offset",
14140#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014141#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010014142 "channel",
14143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144#ifdef FEAT_CINDENT
14145 "cindent",
14146#endif
14147#ifdef FEAT_CLIENTSERVER
14148 "clientserver",
14149#endif
14150#ifdef FEAT_CLIPBOARD
14151 "clipboard",
14152#endif
14153#ifdef FEAT_CMDL_COMPL
14154 "cmdline_compl",
14155#endif
14156#ifdef FEAT_CMDHIST
14157 "cmdline_hist",
14158#endif
14159#ifdef FEAT_COMMENTS
14160 "comments",
14161#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014162#ifdef FEAT_CONCEAL
14163 "conceal",
14164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165#ifdef FEAT_CRYPT
14166 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010014167 "crypt-blowfish",
14168 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169#endif
14170#ifdef FEAT_CSCOPE
14171 "cscope",
14172#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014173#ifdef FEAT_CURSORBIND
14174 "cursorbind",
14175#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014176#ifdef CURSOR_SHAPE
14177 "cursorshape",
14178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179#ifdef DEBUG
14180 "debug",
14181#endif
14182#ifdef FEAT_CON_DIALOG
14183 "dialog_con",
14184#endif
14185#ifdef FEAT_GUI_DIALOG
14186 "dialog_gui",
14187#endif
14188#ifdef FEAT_DIFF
14189 "diff",
14190#endif
14191#ifdef FEAT_DIGRAPHS
14192 "digraphs",
14193#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014194#ifdef FEAT_DIRECTX
14195 "directx",
14196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197#ifdef FEAT_DND
14198 "dnd",
14199#endif
14200#ifdef FEAT_EMACS_TAGS
14201 "emacs_tags",
14202#endif
14203 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014204 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205#ifdef FEAT_SEARCH_EXTRA
14206 "extra_search",
14207#endif
14208#ifdef FEAT_FKMAP
14209 "farsi",
14210#endif
14211#ifdef FEAT_SEARCHPATH
14212 "file_in_path",
14213#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014214#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014215 "filterpipe",
14216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217#ifdef FEAT_FIND_ID
14218 "find_in_path",
14219#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014220#ifdef FEAT_FLOAT
14221 "float",
14222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223#ifdef FEAT_FOLDING
14224 "folding",
14225#endif
14226#ifdef FEAT_FOOTER
14227 "footer",
14228#endif
14229#if !defined(USE_SYSTEM) && defined(UNIX)
14230 "fork",
14231#endif
14232#ifdef FEAT_GETTEXT
14233 "gettext",
14234#endif
14235#ifdef FEAT_GUI
14236 "gui",
14237#endif
14238#ifdef FEAT_GUI_ATHENA
14239# ifdef FEAT_GUI_NEXTAW
14240 "gui_neXtaw",
14241# else
14242 "gui_athena",
14243# endif
14244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245#ifdef FEAT_GUI_GTK
14246 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014247# ifdef USE_GTK3
14248 "gui_gtk3",
14249# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014253#ifdef FEAT_GUI_GNOME
14254 "gui_gnome",
14255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256#ifdef FEAT_GUI_MAC
14257 "gui_mac",
14258#endif
14259#ifdef FEAT_GUI_MOTIF
14260 "gui_motif",
14261#endif
14262#ifdef FEAT_GUI_PHOTON
14263 "gui_photon",
14264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265#ifdef FEAT_GUI_W32
14266 "gui_win32",
14267#endif
14268#ifdef FEAT_HANGULIN
14269 "hangul_input",
14270#endif
14271#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14272 "iconv",
14273#endif
14274#ifdef FEAT_INS_EXPAND
14275 "insert_expand",
14276#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014277#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014278 "job",
14279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280#ifdef FEAT_JUMPLIST
14281 "jumplist",
14282#endif
14283#ifdef FEAT_KEYMAP
14284 "keymap",
14285#endif
14286#ifdef FEAT_LANGMAP
14287 "langmap",
14288#endif
14289#ifdef FEAT_LIBCALL
14290 "libcall",
14291#endif
14292#ifdef FEAT_LINEBREAK
14293 "linebreak",
14294#endif
14295#ifdef FEAT_LISP
14296 "lispindent",
14297#endif
14298#ifdef FEAT_LISTCMDS
14299 "listcmds",
14300#endif
14301#ifdef FEAT_LOCALMAP
14302 "localmap",
14303#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014304#ifdef FEAT_LUA
14305# ifndef DYNAMIC_LUA
14306 "lua",
14307# endif
14308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309#ifdef FEAT_MENU
14310 "menu",
14311#endif
14312#ifdef FEAT_SESSION
14313 "mksession",
14314#endif
14315#ifdef FEAT_MODIFY_FNAME
14316 "modify_fname",
14317#endif
14318#ifdef FEAT_MOUSE
14319 "mouse",
14320#endif
14321#ifdef FEAT_MOUSESHAPE
14322 "mouseshape",
14323#endif
14324#if defined(UNIX) || defined(VMS)
14325# ifdef FEAT_MOUSE_DEC
14326 "mouse_dec",
14327# endif
14328# ifdef FEAT_MOUSE_GPM
14329 "mouse_gpm",
14330# endif
14331# ifdef FEAT_MOUSE_JSB
14332 "mouse_jsbterm",
14333# endif
14334# ifdef FEAT_MOUSE_NET
14335 "mouse_netterm",
14336# endif
14337# ifdef FEAT_MOUSE_PTERM
14338 "mouse_pterm",
14339# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014340# ifdef FEAT_MOUSE_SGR
14341 "mouse_sgr",
14342# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014343# ifdef FEAT_SYSMOUSE
14344 "mouse_sysmouse",
14345# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014346# ifdef FEAT_MOUSE_URXVT
14347 "mouse_urxvt",
14348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349# ifdef FEAT_MOUSE_XTERM
14350 "mouse_xterm",
14351# endif
14352#endif
14353#ifdef FEAT_MBYTE
14354 "multi_byte",
14355#endif
14356#ifdef FEAT_MBYTE_IME
14357 "multi_byte_ime",
14358#endif
14359#ifdef FEAT_MULTI_LANG
14360 "multi_lang",
14361#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014362#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014363#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014364 "mzscheme",
14365#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014366#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014367#ifdef FEAT_NUM64
14368 "num64",
14369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370#ifdef FEAT_OLE
14371 "ole",
14372#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014373 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374#ifdef FEAT_PATH_EXTRA
14375 "path_extra",
14376#endif
14377#ifdef FEAT_PERL
14378#ifndef DYNAMIC_PERL
14379 "perl",
14380#endif
14381#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014382#ifdef FEAT_PERSISTENT_UNDO
14383 "persistent_undo",
14384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385#ifdef FEAT_PYTHON
14386#ifndef DYNAMIC_PYTHON
14387 "python",
14388#endif
14389#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014390#ifdef FEAT_PYTHON3
14391#ifndef DYNAMIC_PYTHON3
14392 "python3",
14393#endif
14394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395#ifdef FEAT_POSTSCRIPT
14396 "postscript",
14397#endif
14398#ifdef FEAT_PRINTER
14399 "printer",
14400#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014401#ifdef FEAT_PROFILE
14402 "profile",
14403#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014404#ifdef FEAT_RELTIME
14405 "reltime",
14406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407#ifdef FEAT_QUICKFIX
14408 "quickfix",
14409#endif
14410#ifdef FEAT_RIGHTLEFT
14411 "rightleft",
14412#endif
14413#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14414 "ruby",
14415#endif
14416#ifdef FEAT_SCROLLBIND
14417 "scrollbind",
14418#endif
14419#ifdef FEAT_CMDL_INFO
14420 "showcmd",
14421 "cmdline_info",
14422#endif
14423#ifdef FEAT_SIGNS
14424 "signs",
14425#endif
14426#ifdef FEAT_SMARTINDENT
14427 "smartindent",
14428#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014429#ifdef STARTUPTIME
14430 "startuptime",
14431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432#ifdef FEAT_STL_OPT
14433 "statusline",
14434#endif
14435#ifdef FEAT_SUN_WORKSHOP
14436 "sun_workshop",
14437#endif
14438#ifdef FEAT_NETBEANS_INTG
14439 "netbeans_intg",
14440#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014441#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014442 "spell",
14443#endif
14444#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 "syntax",
14446#endif
14447#if defined(USE_SYSTEM) || !defined(UNIX)
14448 "system",
14449#endif
14450#ifdef FEAT_TAG_BINS
14451 "tag_binary",
14452#endif
14453#ifdef FEAT_TAG_OLDSTATIC
14454 "tag_old_static",
14455#endif
14456#ifdef FEAT_TAG_ANYWHITE
14457 "tag_any_white",
14458#endif
14459#ifdef FEAT_TCL
14460# ifndef DYNAMIC_TCL
14461 "tcl",
14462# endif
14463#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014464#ifdef FEAT_TERMGUICOLORS
14465 "termguicolors",
14466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467#ifdef TERMINFO
14468 "terminfo",
14469#endif
14470#ifdef FEAT_TERMRESPONSE
14471 "termresponse",
14472#endif
14473#ifdef FEAT_TEXTOBJ
14474 "textobjects",
14475#endif
14476#ifdef HAVE_TGETENT
14477 "tgetent",
14478#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014479#ifdef FEAT_TIMERS
14480 "timers",
14481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482#ifdef FEAT_TITLE
14483 "title",
14484#endif
14485#ifdef FEAT_TOOLBAR
14486 "toolbar",
14487#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014488#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14489 "unnamedplus",
14490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491#ifdef FEAT_USR_CMDS
14492 "user-commands", /* was accidentally included in 5.4 */
14493 "user_commands",
14494#endif
14495#ifdef FEAT_VIMINFO
14496 "viminfo",
14497#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014498#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 "vertsplit",
14500#endif
14501#ifdef FEAT_VIRTUALEDIT
14502 "virtualedit",
14503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505#ifdef FEAT_VISUALEXTRA
14506 "visualextra",
14507#endif
14508#ifdef FEAT_VREPLACE
14509 "vreplace",
14510#endif
14511#ifdef FEAT_WILDIGN
14512 "wildignore",
14513#endif
14514#ifdef FEAT_WILDMENU
14515 "wildmenu",
14516#endif
14517#ifdef FEAT_WINDOWS
14518 "windows",
14519#endif
14520#ifdef FEAT_WAK
14521 "winaltkeys",
14522#endif
14523#ifdef FEAT_WRITEBACKUP
14524 "writebackup",
14525#endif
14526#ifdef FEAT_XIM
14527 "xim",
14528#endif
14529#ifdef FEAT_XFONTSET
14530 "xfontset",
14531#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014532#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014533 "xpm",
14534 "xpm_w32", /* for backward compatibility */
14535#else
14536# if defined(HAVE_XPM)
14537 "xpm",
14538# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540#ifdef USE_XSMP
14541 "xsmp",
14542#endif
14543#ifdef USE_XSMP_INTERACT
14544 "xsmp_interact",
14545#endif
14546#ifdef FEAT_XCLIPBOARD
14547 "xterm_clipboard",
14548#endif
14549#ifdef FEAT_XTERM_SAVE
14550 "xterm_save",
14551#endif
14552#if defined(UNIX) && defined(FEAT_X11)
14553 "X11",
14554#endif
14555 NULL
14556 };
14557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014558 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 for (i = 0; has_list[i] != NULL; ++i)
14560 if (STRICMP(name, has_list[i]) == 0)
14561 {
14562 n = TRUE;
14563 break;
14564 }
14565
14566 if (n == FALSE)
14567 {
14568 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014569 {
14570 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014571 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014572 && vim_isdigit(name[6])
14573 && vim_isdigit(name[8])
14574 && vim_isdigit(name[10]))
14575 {
14576 int major = atoi((char *)name + 6);
14577 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014578
14579 /* Expect "patch-9.9.01234". */
14580 n = (major < VIM_VERSION_MAJOR
14581 || (major == VIM_VERSION_MAJOR
14582 && (minor < VIM_VERSION_MINOR
14583 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014584 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014585 }
14586 else
14587 n = has_patch(atoi((char *)name + 5));
14588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 else if (STRICMP(name, "vim_starting") == 0)
14590 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014591#ifdef FEAT_MBYTE
14592 else if (STRICMP(name, "multi_byte_encoding") == 0)
14593 n = has_mbyte;
14594#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014595#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14596 else if (STRICMP(name, "balloon_multiline") == 0)
14597 n = multiline_balloon_available();
14598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599#ifdef DYNAMIC_TCL
14600 else if (STRICMP(name, "tcl") == 0)
14601 n = tcl_enabled(FALSE);
14602#endif
14603#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14604 else if (STRICMP(name, "iconv") == 0)
14605 n = iconv_enabled(FALSE);
14606#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014607#ifdef DYNAMIC_LUA
14608 else if (STRICMP(name, "lua") == 0)
14609 n = lua_enabled(FALSE);
14610#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014611#ifdef DYNAMIC_MZSCHEME
14612 else if (STRICMP(name, "mzscheme") == 0)
14613 n = mzscheme_enabled(FALSE);
14614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615#ifdef DYNAMIC_RUBY
14616 else if (STRICMP(name, "ruby") == 0)
14617 n = ruby_enabled(FALSE);
14618#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014619#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620#ifdef DYNAMIC_PYTHON
14621 else if (STRICMP(name, "python") == 0)
14622 n = python_enabled(FALSE);
14623#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014624#endif
14625#ifdef FEAT_PYTHON3
14626#ifdef DYNAMIC_PYTHON3
14627 else if (STRICMP(name, "python3") == 0)
14628 n = python3_enabled(FALSE);
14629#endif
14630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631#ifdef DYNAMIC_PERL
14632 else if (STRICMP(name, "perl") == 0)
14633 n = perl_enabled(FALSE);
14634#endif
14635#ifdef FEAT_GUI
14636 else if (STRICMP(name, "gui_running") == 0)
14637 n = (gui.in_use || gui.starting);
14638# ifdef FEAT_GUI_W32
14639 else if (STRICMP(name, "gui_win32s") == 0)
14640 n = gui_is_win32s();
14641# endif
14642# ifdef FEAT_BROWSE
14643 else if (STRICMP(name, "browse") == 0)
14644 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14645# endif
14646#endif
14647#ifdef FEAT_SYN_HL
14648 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014649 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650#endif
14651#if defined(WIN3264)
14652 else if (STRICMP(name, "win95") == 0)
14653 n = mch_windows95();
14654#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014655#ifdef FEAT_NETBEANS_INTG
14656 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014657 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 }
14660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014661 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662}
14663
14664/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014665 * "has_key()" function
14666 */
14667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014668f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014669{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014670 if (argvars[0].v_type != VAR_DICT)
14671 {
14672 EMSG(_(e_dictreq));
14673 return;
14674 }
14675 if (argvars[0].vval.v_dict == NULL)
14676 return;
14677
14678 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014679 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014680}
14681
14682/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014683 * "haslocaldir()" function
14684 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014686f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014687{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014688 win_T *wp = NULL;
14689
14690 wp = find_tabwin(&argvars[0], &argvars[1]);
14691 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014692}
14693
14694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 * "hasmapto()" function
14696 */
14697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014698f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699{
14700 char_u *name;
14701 char_u *mode;
14702 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014703 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014705 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014706 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 mode = (char_u *)"nvo";
14708 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014710 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014711 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014712 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714
Bram Moolenaar2c932302006-03-18 21:42:09 +000014715 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014716 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014718 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719}
14720
14721/*
14722 * "histadd()" function
14723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014725f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726{
14727#ifdef FEAT_CMDHIST
14728 int histype;
14729 char_u *str;
14730 char_u buf[NUMBUFLEN];
14731#endif
14732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014733 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 if (check_restricted() || check_secure())
14735 return;
14736#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014737 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14738 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739 if (histype >= 0)
14740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014741 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 if (*str != NUL)
14743 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014744 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 return;
14748 }
14749 }
14750#endif
14751}
14752
14753/*
14754 * "histdel()" function
14755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014757f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758{
14759#ifdef FEAT_CMDHIST
14760 int n;
14761 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014762 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014764 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14765 if (str == NULL)
14766 n = 0;
14767 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014769 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014770 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014772 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014773 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774 else
14775 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014776 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014777 get_tv_string_buf(&argvars[1], buf));
14778 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779#endif
14780}
14781
14782/*
14783 * "histget()" function
14784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014786f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787{
14788#ifdef FEAT_CMDHIST
14789 int type;
14790 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014791 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14794 if (str == NULL)
14795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014797 {
14798 type = get_histtype(str);
14799 if (argvars[1].v_type == VAR_UNKNOWN)
14800 idx = get_history_idx(type);
14801 else
14802 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14803 /* -1 on type error */
14804 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014807 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014809 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810}
14811
14812/*
14813 * "histnr()" function
14814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014816f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817{
14818 int i;
14819
14820#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014821 char_u *history = get_tv_string_chk(&argvars[0]);
14822
14823 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 if (i >= HIST_CMD && i < HIST_COUNT)
14825 i = get_history_idx(i);
14826 else
14827#endif
14828 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830}
14831
14832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 * "highlightID(name)" function
14834 */
14835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014836f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014838 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839}
14840
14841/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014842 * "highlight_exists()" function
14843 */
14844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014845f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014846{
14847 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14848}
14849
14850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 * "hostname()" function
14852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014854f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855{
14856 char_u hostname[256];
14857
14858 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014859 rettv->v_type = VAR_STRING;
14860 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861}
14862
14863/*
14864 * iconv() function
14865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014867f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868{
14869#ifdef FEAT_MBYTE
14870 char_u buf1[NUMBUFLEN];
14871 char_u buf2[NUMBUFLEN];
14872 char_u *from, *to, *str;
14873 vimconv_T vimconv;
14874#endif
14875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014876 rettv->v_type = VAR_STRING;
14877 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878
14879#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014880 str = get_tv_string(&argvars[0]);
14881 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14882 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 vimconv.vc_type = CONV_NONE;
14884 convert_setup(&vimconv, from, to);
14885
14886 /* If the encodings are equal, no conversion needed. */
14887 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014890 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891
14892 convert_setup(&vimconv, NULL, NULL);
14893 vim_free(from);
14894 vim_free(to);
14895#endif
14896}
14897
14898/*
14899 * "indent()" function
14900 */
14901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014902f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903{
14904 linenr_T lnum;
14905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014906 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014910 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911}
14912
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014913/*
14914 * "index()" function
14915 */
14916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014917f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014918{
Bram Moolenaar33570922005-01-25 22:26:29 +000014919 list_T *l;
14920 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014921 long idx = 0;
14922 int ic = FALSE;
14923
14924 rettv->vval.v_number = -1;
14925 if (argvars[0].v_type != VAR_LIST)
14926 {
14927 EMSG(_(e_listreq));
14928 return;
14929 }
14930 l = argvars[0].vval.v_list;
14931 if (l != NULL)
14932 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014933 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014934 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014936 int error = FALSE;
14937
Bram Moolenaar758711c2005-02-02 23:11:38 +000014938 /* Start at specified item. Use the cached index that list_find()
14939 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014940 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014941 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014942 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014943 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014944 if (error)
14945 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014946 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014947
Bram Moolenaar758711c2005-02-02 23:11:38 +000014948 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014949 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014950 {
14951 rettv->vval.v_number = idx;
14952 break;
14953 }
14954 }
14955}
14956
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957static int inputsecret_flag = 0;
14958
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014959static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014960
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014962 * This function is used by f_input() and f_inputdialog() functions. The third
14963 * argument to f_input() specifies the type of completion to use at the
14964 * prompt. The third argument to f_inputdialog() specifies the value to return
14965 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 */
14967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014968get_user_input(
14969 typval_T *argvars,
14970 typval_T *rettv,
14971 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014973 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 char_u *p = NULL;
14975 int c;
14976 char_u buf[NUMBUFLEN];
14977 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014978 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014979 int xp_type = EXPAND_NOTHING;
14980 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014983 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984
14985#ifdef NO_CONSOLE_INPUT
14986 /* While starting up, there is no place to enter text. */
14987 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989#endif
14990
14991 cmd_silent = FALSE; /* Want to see the prompt. */
14992 if (prompt != NULL)
14993 {
14994 /* Only the part of the message after the last NL is considered as
14995 * prompt for the command line */
14996 p = vim_strrchr(prompt, '\n');
14997 if (p == NULL)
14998 p = prompt;
14999 else
15000 {
15001 ++p;
15002 c = *p;
15003 *p = NUL;
15004 msg_start();
15005 msg_clr_eos();
15006 msg_puts_attr(prompt, echo_attr);
15007 msg_didout = FALSE;
15008 msg_starthere();
15009 *p = c;
15010 }
15011 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015013 if (argvars[1].v_type != VAR_UNKNOWN)
15014 {
15015 defstr = get_tv_string_buf_chk(&argvars[1], buf);
15016 if (defstr != NULL)
15017 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015019 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015020 {
15021 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000015022 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000015023 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015024
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020015025 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000015026 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015027
Bram Moolenaar4463f292005-09-25 22:20:24 +000015028 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
15029 if (xp_name == NULL)
15030 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015031
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015032 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015033
Bram Moolenaar4463f292005-09-25 22:20:24 +000015034 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
15035 &xp_arg) == FAIL)
15036 return;
15037 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015038 }
15039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015040 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015041 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015042 int save_ex_normal_busy = ex_normal_busy;
15043 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015044 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015045 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
15046 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015047 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015048 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020015049 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020015050 && argvars[1].v_type != VAR_UNKNOWN
15051 && argvars[2].v_type != VAR_UNKNOWN)
15052 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
15053 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015054
15055 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015057 /* since the user typed this, no need to wait for return */
15058 need_wait_return = FALSE;
15059 msg_didout = FALSE;
15060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061 cmd_silent = cmd_silent_save;
15062}
15063
15064/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015065 * "input()" function
15066 * Also handles inputsecret() when inputsecret is set.
15067 */
15068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015069f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015070{
15071 get_user_input(argvars, rettv, FALSE);
15072}
15073
15074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075 * "inputdialog()" function
15076 */
15077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015078f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079{
15080#if defined(FEAT_GUI_TEXTDIALOG)
15081 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
15082 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
15083 {
15084 char_u *message;
15085 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015086 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 message = get_tv_string_chk(&argvars[0]);
15089 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000015090 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000015091 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092 else
15093 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015094 if (message != NULL && defstr != NULL
15095 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010015096 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015097 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 else
15099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015100 if (message != NULL && defstr != NULL
15101 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015102 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015103 rettv->vval.v_string = vim_strsave(
15104 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015106 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015108 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 }
15110 else
15111#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015112 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113}
15114
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015115/*
15116 * "inputlist()" function
15117 */
15118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015119f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015120{
15121 listitem_T *li;
15122 int selected;
15123 int mouse_used;
15124
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015125#ifdef NO_CONSOLE_INPUT
15126 /* While starting up, there is no place to enter text. */
15127 if (no_console_input())
15128 return;
15129#endif
15130 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
15131 {
15132 EMSG2(_(e_listarg), "inputlist()");
15133 return;
15134 }
15135
15136 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000015137 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015138 lines_left = Rows; /* avoid more prompt */
15139 msg_scroll = TRUE;
15140 msg_clr_eos();
15141
15142 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
15143 {
15144 msg_puts(get_tv_string(&li->li_tv));
15145 msg_putchar('\n');
15146 }
15147
15148 /* Ask for choice. */
15149 selected = prompt_for_number(&mouse_used);
15150 if (mouse_used)
15151 selected -= lines_left;
15152
15153 rettv->vval.v_number = selected;
15154}
15155
15156
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
15158
15159/*
15160 * "inputrestore()" function
15161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015163f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164{
15165 if (ga_userinput.ga_len > 0)
15166 {
15167 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
15169 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015170 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 }
15172 else if (p_verbose > 1)
15173 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000015174 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015175 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 }
15177}
15178
15179/*
15180 * "inputsave()" function
15181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015183f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015185 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 if (ga_grow(&ga_userinput, 1) == OK)
15187 {
15188 save_typeahead((tasave_T *)(ga_userinput.ga_data)
15189 + ga_userinput.ga_len);
15190 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015191 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 }
15193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015194 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195}
15196
15197/*
15198 * "inputsecret()" function
15199 */
15200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015201f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202{
15203 ++cmdline_star;
15204 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015205 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 --cmdline_star;
15207 --inputsecret_flag;
15208}
15209
15210/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015211 * "insert()" function
15212 */
15213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015214f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015215{
15216 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015217 listitem_T *item;
15218 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015219 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015220
15221 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015222 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015223 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015224 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015225 {
15226 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015227 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015228 if (error)
15229 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015230
Bram Moolenaar758711c2005-02-02 23:11:38 +000015231 if (before == l->lv_len)
15232 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015233 else
15234 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015235 item = list_find(l, before);
15236 if (item == NULL)
15237 {
15238 EMSGN(_(e_listidx), before);
15239 l = NULL;
15240 }
15241 }
15242 if (l != NULL)
15243 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015244 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015245 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015246 }
15247 }
15248}
15249
15250/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015251 * "invert(expr)" function
15252 */
15253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015254f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015255{
15256 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15257}
15258
15259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 * "isdirectory()" function
15261 */
15262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015263f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015265 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266}
15267
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015268/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015269 * "islocked()" function
15270 */
15271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015272f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015273{
15274 lval_T lv;
15275 char_u *end;
15276 dictitem_T *di;
15277
15278 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015279 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15280 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015281 if (end != NULL && lv.ll_name != NULL)
15282 {
15283 if (*end != NUL)
15284 EMSG(_(e_trailing));
15285 else
15286 {
15287 if (lv.ll_tv == NULL)
15288 {
15289 if (check_changedtick(lv.ll_name))
15290 rettv->vval.v_number = 1; /* always locked */
15291 else
15292 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015293 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015294 if (di != NULL)
15295 {
15296 /* Consider a variable locked when:
15297 * 1. the variable itself is locked
15298 * 2. the value of the variable is locked.
15299 * 3. the List or Dict value is locked.
15300 */
15301 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15302 || tv_islocked(&di->di_tv));
15303 }
15304 }
15305 }
15306 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015307 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015308 else if (lv.ll_newkey != NULL)
15309 EMSG2(_(e_dictkey), lv.ll_newkey);
15310 else if (lv.ll_list != NULL)
15311 /* List item. */
15312 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15313 else
15314 /* Dictionary item. */
15315 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15316 }
15317 }
15318
15319 clear_lval(&lv);
15320}
15321
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015322#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15323/*
15324 * "isnan()" function
15325 */
15326 static void
15327f_isnan(typval_T *argvars, typval_T *rettv)
15328{
15329 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15330 && isnan(argvars[0].vval.v_float);
15331}
15332#endif
15333
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015334static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015335
15336/*
15337 * Turn a dict into a list:
15338 * "what" == 0: list of keys
15339 * "what" == 1: list of values
15340 * "what" == 2: list of items
15341 */
15342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015343dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015344{
Bram Moolenaar33570922005-01-25 22:26:29 +000015345 list_T *l2;
15346 dictitem_T *di;
15347 hashitem_T *hi;
15348 listitem_T *li;
15349 listitem_T *li2;
15350 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015351 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015352
Bram Moolenaar8c711452005-01-14 21:53:12 +000015353 if (argvars[0].v_type != VAR_DICT)
15354 {
15355 EMSG(_(e_dictreq));
15356 return;
15357 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015358 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015359 return;
15360
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015361 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015362 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015363
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015364 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015365 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015366 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015367 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015369 --todo;
15370 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015371
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015372 li = listitem_alloc();
15373 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015374 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015375 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015376
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015377 if (what == 0)
15378 {
15379 /* keys() */
15380 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015381 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015382 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15383 }
15384 else if (what == 1)
15385 {
15386 /* values() */
15387 copy_tv(&di->di_tv, &li->li_tv);
15388 }
15389 else
15390 {
15391 /* items() */
15392 l2 = list_alloc();
15393 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015394 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015395 li->li_tv.vval.v_list = l2;
15396 if (l2 == NULL)
15397 break;
15398 ++l2->lv_refcount;
15399
15400 li2 = listitem_alloc();
15401 if (li2 == NULL)
15402 break;
15403 list_append(l2, li2);
15404 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015405 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015406 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15407
15408 li2 = listitem_alloc();
15409 if (li2 == NULL)
15410 break;
15411 list_append(l2, li2);
15412 copy_tv(&di->di_tv, &li2->li_tv);
15413 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015414 }
15415 }
15416}
15417
15418/*
15419 * "items(dict)" function
15420 */
15421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015422f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015423{
15424 dict_list(argvars, rettv, 2);
15425}
15426
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015427#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015428/*
15429 * Get the job from the argument.
15430 * Returns NULL if the job is invalid.
15431 */
15432 static job_T *
15433get_job_arg(typval_T *tv)
15434{
15435 job_T *job;
15436
15437 if (tv->v_type != VAR_JOB)
15438 {
15439 EMSG2(_(e_invarg2), get_tv_string(tv));
15440 return NULL;
15441 }
15442 job = tv->vval.v_job;
15443
15444 if (job == NULL)
15445 EMSG(_("E916: not a valid job"));
15446 return job;
15447}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015448
Bram Moolenaar835dc632016-02-07 14:27:38 +010015449/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015450 * "job_getchannel()" function
15451 */
15452 static void
15453f_job_getchannel(typval_T *argvars, typval_T *rettv)
15454{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015455 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015456
Bram Moolenaar65edff82016-02-21 16:40:11 +010015457 if (job != NULL)
15458 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015459 rettv->v_type = VAR_CHANNEL;
15460 rettv->vval.v_channel = job->jv_channel;
15461 if (job->jv_channel != NULL)
15462 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015463 }
15464}
15465
15466/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015467 * "job_info()" function
15468 */
15469 static void
15470f_job_info(typval_T *argvars, typval_T *rettv)
15471{
15472 job_T *job = get_job_arg(&argvars[0]);
15473
15474 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15475 job_info(job, rettv->vval.v_dict);
15476}
15477
15478/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015479 * "job_setoptions()" function
15480 */
15481 static void
15482f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15483{
15484 job_T *job = get_job_arg(&argvars[0]);
15485 jobopt_T opt;
15486
15487 if (job == NULL)
15488 return;
15489 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015490 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15491 job_set_options(job, &opt);
15492 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015493}
15494
15495/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015496 * "job_start()" function
15497 */
15498 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015499f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015500{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015501 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015502 if (check_restricted() || check_secure())
15503 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015504 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015505}
15506
15507/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015508 * "job_status()" function
15509 */
15510 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015511f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015512{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015513 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015514
Bram Moolenaar65edff82016-02-21 16:40:11 +010015515 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015516 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015517 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015518 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015519 }
15520}
15521
15522/*
15523 * "job_stop()" function
15524 */
15525 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015526f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015527{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015528 job_T *job = get_job_arg(&argvars[0]);
15529
15530 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015531 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015532}
15533#endif
15534
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015536 * "join()" function
15537 */
15538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015539f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015540{
15541 garray_T ga;
15542 char_u *sep;
15543
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015544 if (argvars[0].v_type != VAR_LIST)
15545 {
15546 EMSG(_(e_listreq));
15547 return;
15548 }
15549 if (argvars[0].vval.v_list == NULL)
15550 return;
15551 if (argvars[1].v_type == VAR_UNKNOWN)
15552 sep = (char_u *)" ";
15553 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015554 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015555
15556 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015557
15558 if (sep != NULL)
15559 {
15560 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015561 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015562 ga_append(&ga, NUL);
15563 rettv->vval.v_string = (char_u *)ga.ga_data;
15564 }
15565 else
15566 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015567}
15568
15569/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015570 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015571 */
15572 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015573f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015574{
15575 js_read_T reader;
15576
15577 reader.js_buf = get_tv_string(&argvars[0]);
15578 reader.js_fill = NULL;
15579 reader.js_used = 0;
15580 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15581 EMSG(_(e_invarg));
15582}
15583
15584/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015585 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015586 */
15587 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015588f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015589{
15590 rettv->v_type = VAR_STRING;
15591 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15592}
15593
15594/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015595 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015596 */
15597 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015598f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015599{
15600 js_read_T reader;
15601
15602 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015603 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015604 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015605 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015606 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015607}
15608
15609/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015610 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015611 */
15612 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015613f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015614{
15615 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015616 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015617}
15618
15619/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015620 * "keys()" function
15621 */
15622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015623f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015624{
15625 dict_list(argvars, rettv, 0);
15626}
15627
15628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629 * "last_buffer_nr()" function.
15630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015632f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633{
15634 int n = 0;
15635 buf_T *buf;
15636
15637 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15638 if (n < buf->b_fnum)
15639 n = buf->b_fnum;
15640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015641 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015642}
15643
15644/*
15645 * "len()" function
15646 */
15647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015648f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015649{
15650 switch (argvars[0].v_type)
15651 {
15652 case VAR_STRING:
15653 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015654 rettv->vval.v_number = (varnumber_T)STRLEN(
15655 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015656 break;
15657 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015658 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015659 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015660 case VAR_DICT:
15661 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15662 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015663 case VAR_UNKNOWN:
15664 case VAR_SPECIAL:
15665 case VAR_FLOAT:
15666 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015667 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015668 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015669 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015670 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015671 break;
15672 }
15673}
15674
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015675static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015676
15677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015678libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015679{
15680#ifdef FEAT_LIBCALL
15681 char_u *string_in;
15682 char_u **string_result;
15683 int nr_result;
15684#endif
15685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015686 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015687 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015688 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015689
15690 if (check_restricted() || check_secure())
15691 return;
15692
15693#ifdef FEAT_LIBCALL
15694 /* The first two args must be strings, otherwise its meaningless */
15695 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15696 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015697 string_in = NULL;
15698 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015699 string_in = argvars[2].vval.v_string;
15700 if (type == VAR_NUMBER)
15701 string_result = NULL;
15702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015703 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015704 if (mch_libcall(argvars[0].vval.v_string,
15705 argvars[1].vval.v_string,
15706 string_in,
15707 argvars[2].vval.v_number,
15708 string_result,
15709 &nr_result) == OK
15710 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015711 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015712 }
15713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714}
15715
15716/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015717 * "libcall()" function
15718 */
15719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015720f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015721{
15722 libcall_common(argvars, rettv, VAR_STRING);
15723}
15724
15725/*
15726 * "libcallnr()" function
15727 */
15728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015729f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015730{
15731 libcall_common(argvars, rettv, VAR_NUMBER);
15732}
15733
15734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 * "line(string)" function
15736 */
15737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015738f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739{
15740 linenr_T lnum = 0;
15741 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015742 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015744 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 if (fp != NULL)
15746 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015747 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748}
15749
15750/*
15751 * "line2byte(lnum)" function
15752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015754f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755{
15756#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015757 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758#else
15759 linenr_T lnum;
15760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015761 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015763 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015765 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15766 if (rettv->vval.v_number >= 0)
15767 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768#endif
15769}
15770
15771/*
15772 * "lispindent(lnum)" function
15773 */
15774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015775f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776{
15777#ifdef FEAT_LISP
15778 pos_T pos;
15779 linenr_T lnum;
15780
15781 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015782 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15784 {
15785 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 curwin->w_cursor = pos;
15788 }
15789 else
15790#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792}
15793
15794/*
15795 * "localtime()" function
15796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015798f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015800 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801}
15802
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015803static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804
15805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015806get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015807{
15808 char_u *keys;
15809 char_u *which;
15810 char_u buf[NUMBUFLEN];
15811 char_u *keys_buf = NULL;
15812 char_u *rhs;
15813 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015814 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015815 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015816 mapblock_T *mp;
15817 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818
15819 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015820 rettv->v_type = VAR_STRING;
15821 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015823 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 if (*keys == NUL)
15825 return;
15826
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015827 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015829 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015830 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015831 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015832 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015833 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015834 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015835 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837 else
15838 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015839 if (which == NULL)
15840 return;
15841
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 mode = get_map_mode(&which, 0);
15843
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015844 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015845 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015847
15848 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015850 /* Return a string. */
15851 if (rhs != NULL)
15852 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853
Bram Moolenaarbd743252010-10-20 21:23:33 +020015854 }
15855 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15856 {
15857 /* Return a dictionary. */
15858 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15859 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15860 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861
Bram Moolenaarbd743252010-10-20 21:23:33 +020015862 dict_add_nr_str(dict, "lhs", 0L, lhs);
15863 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15864 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15865 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15866 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15867 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15868 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015869 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015870 dict_add_nr_str(dict, "mode", 0L, mapmode);
15871
15872 vim_free(lhs);
15873 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 }
15875}
15876
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015877#ifdef FEAT_FLOAT
15878/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015879 * "log()" function
15880 */
15881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015882f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015883{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015884 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015885
15886 rettv->v_type = VAR_FLOAT;
15887 if (get_float_arg(argvars, &f) == OK)
15888 rettv->vval.v_float = log(f);
15889 else
15890 rettv->vval.v_float = 0.0;
15891}
15892
15893/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015894 * "log10()" function
15895 */
15896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015897f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015898{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015899 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015900
15901 rettv->v_type = VAR_FLOAT;
15902 if (get_float_arg(argvars, &f) == OK)
15903 rettv->vval.v_float = log10(f);
15904 else
15905 rettv->vval.v_float = 0.0;
15906}
15907#endif
15908
Bram Moolenaar1dced572012-04-05 16:54:08 +020015909#ifdef FEAT_LUA
15910/*
15911 * "luaeval()" function
15912 */
15913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015914f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015915{
15916 char_u *str;
15917 char_u buf[NUMBUFLEN];
15918
15919 str = get_tv_string_buf(&argvars[0], buf);
15920 do_luaeval(str, argvars + 1, rettv);
15921}
15922#endif
15923
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015925 * "map()" function
15926 */
15927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015928f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015929{
15930 filter_map(argvars, rettv, TRUE);
15931}
15932
15933/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015934 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935 */
15936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015937f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015939 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940}
15941
15942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015943 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 */
15945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015946f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015948 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949}
15950
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015951static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952
15953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015954find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015956 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015957 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015958 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959 char_u *pat;
15960 regmatch_T regmatch;
15961 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015962 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963 char_u *save_cpo;
15964 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015965 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015966 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015967 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015968 list_T *l = NULL;
15969 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015970 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015971 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972
15973 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15974 save_cpo = p_cpo;
15975 p_cpo = (char_u *)"";
15976
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015977 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015978 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015979 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015980 /* type 3: return empty list when there are no matches.
15981 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015982 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015983 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015984 if (type == 4
15985 && (list_append_string(rettv->vval.v_list,
15986 (char_u *)"", 0) == FAIL
15987 || list_append_number(rettv->vval.v_list,
15988 (varnumber_T)-1) == FAIL
15989 || list_append_number(rettv->vval.v_list,
15990 (varnumber_T)-1) == FAIL
15991 || list_append_number(rettv->vval.v_list,
15992 (varnumber_T)-1) == FAIL))
15993 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015994 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015995 rettv->vval.v_list = NULL;
15996 goto theend;
15997 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015998 }
15999 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016001 rettv->v_type = VAR_STRING;
16002 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016005 if (argvars[0].v_type == VAR_LIST)
16006 {
16007 if ((l = argvars[0].vval.v_list) == NULL)
16008 goto theend;
16009 li = l->lv_first;
16010 }
16011 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016012 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016013 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016014 len = (long)STRLEN(str);
16015 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016017 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16018 if (pat == NULL)
16019 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016020
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016021 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016023 int error = FALSE;
16024
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016025 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016026 if (error)
16027 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016028 if (l != NULL)
16029 {
16030 li = list_find(l, start);
16031 if (li == NULL)
16032 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016033 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016034 }
16035 else
16036 {
16037 if (start < 0)
16038 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016039 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016040 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016041 /* When "count" argument is there ignore matches before "start",
16042 * otherwise skip part of the string. Differs when pattern is "^"
16043 * or "\<". */
16044 if (argvars[3].v_type != VAR_UNKNOWN)
16045 startcol = start;
16046 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016047 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016048 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016049 len -= start;
16050 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016051 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016052
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016053 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016054 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016055 if (error)
16056 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 }
16058
16059 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16060 if (regmatch.regprog != NULL)
16061 {
16062 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016063
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016064 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016065 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016066 if (l != NULL)
16067 {
16068 if (li == NULL)
16069 {
16070 match = FALSE;
16071 break;
16072 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016073 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016074 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016075 if (str == NULL)
16076 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016077 }
16078
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000016079 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016080
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016081 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016082 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016083 if (l == NULL && !match)
16084 break;
16085
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016086 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016087 if (l != NULL)
16088 {
16089 li = li->li_next;
16090 ++idx;
16091 }
16092 else
16093 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016094#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016095 startcol = (colnr_T)(regmatch.startp[0]
16096 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016097#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020016098 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016099#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016100 if (startcol > (colnr_T)len
16101 || str + startcol <= regmatch.startp[0])
16102 {
16103 match = FALSE;
16104 break;
16105 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016106 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016107 }
16108
16109 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016111 if (type == 4)
16112 {
16113 listitem_T *li1 = rettv->vval.v_list->lv_first;
16114 listitem_T *li2 = li1->li_next;
16115 listitem_T *li3 = li2->li_next;
16116 listitem_T *li4 = li3->li_next;
16117
Bram Moolenaar3c809342016-06-01 22:34:48 +020016118 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016119 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
16120 (int)(regmatch.endp[0] - regmatch.startp[0]));
16121 li3->li_tv.vval.v_number =
16122 (varnumber_T)(regmatch.startp[0] - expr);
16123 li4->li_tv.vval.v_number =
16124 (varnumber_T)(regmatch.endp[0] - expr);
16125 if (l != NULL)
16126 li2->li_tv.vval.v_number = (varnumber_T)idx;
16127 }
16128 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016129 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016130 int i;
16131
16132 /* return list with matched string and submatches */
16133 for (i = 0; i < NSUBEXP; ++i)
16134 {
16135 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016136 {
16137 if (list_append_string(rettv->vval.v_list,
16138 (char_u *)"", 0) == FAIL)
16139 break;
16140 }
16141 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016142 regmatch.startp[i],
16143 (int)(regmatch.endp[i] - regmatch.startp[i]))
16144 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016145 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016146 }
16147 }
16148 else if (type == 2)
16149 {
16150 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016151 if (l != NULL)
16152 copy_tv(&li->li_tv, rettv);
16153 else
16154 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016156 }
16157 else if (l != NULL)
16158 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 else
16160 {
16161 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016162 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 (varnumber_T)(regmatch.startp[0] - str);
16164 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016165 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016167 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 }
16169 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016170 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 }
16172
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016173 if (type == 4 && l == NULL)
16174 /* matchstrpos() without a list: drop the second item. */
16175 listitem_remove(rettv->vval.v_list,
16176 rettv->vval.v_list->lv_first->li_next);
16177
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016179 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 p_cpo = save_cpo;
16181}
16182
16183/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016184 * "match()" function
16185 */
16186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016187f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016188{
16189 find_some_match(argvars, rettv, 1);
16190}
16191
16192/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016193 * "matchadd()" function
16194 */
16195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016196f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016197{
16198#ifdef FEAT_SEARCH_EXTRA
16199 char_u buf[NUMBUFLEN];
16200 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16201 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16202 int prio = 10; /* default priority */
16203 int id = -1;
16204 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016205 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016206
16207 rettv->vval.v_number = -1;
16208
16209 if (grp == NULL || pat == NULL)
16210 return;
16211 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016212 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016213 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016214 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016215 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016216 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016217 if (argvars[4].v_type != VAR_UNKNOWN)
16218 {
16219 if (argvars[4].v_type != VAR_DICT)
16220 {
16221 EMSG(_(e_dictreq));
16222 return;
16223 }
16224 if (dict_find(argvars[4].vval.v_dict,
16225 (char_u *)"conceal", -1) != NULL)
16226 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16227 (char_u *)"conceal", FALSE);
16228 }
16229 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016230 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016231 if (error == TRUE)
16232 return;
16233 if (id >= 1 && id <= 3)
16234 {
16235 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16236 return;
16237 }
16238
Bram Moolenaar6561d522015-07-21 15:48:27 +020016239 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16240 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016241#endif
16242}
16243
16244/*
16245 * "matchaddpos()" function
16246 */
16247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016248f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016249{
16250#ifdef FEAT_SEARCH_EXTRA
16251 char_u buf[NUMBUFLEN];
16252 char_u *group;
16253 int prio = 10;
16254 int id = -1;
16255 int error = FALSE;
16256 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016257 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016258
16259 rettv->vval.v_number = -1;
16260
16261 group = get_tv_string_buf_chk(&argvars[0], buf);
16262 if (group == NULL)
16263 return;
16264
16265 if (argvars[1].v_type != VAR_LIST)
16266 {
16267 EMSG2(_(e_listarg), "matchaddpos()");
16268 return;
16269 }
16270 l = argvars[1].vval.v_list;
16271 if (l == NULL)
16272 return;
16273
16274 if (argvars[2].v_type != VAR_UNKNOWN)
16275 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016276 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016277 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016278 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016279 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016280 if (argvars[4].v_type != VAR_UNKNOWN)
16281 {
16282 if (argvars[4].v_type != VAR_DICT)
16283 {
16284 EMSG(_(e_dictreq));
16285 return;
16286 }
16287 if (dict_find(argvars[4].vval.v_dict,
16288 (char_u *)"conceal", -1) != NULL)
16289 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16290 (char_u *)"conceal", FALSE);
16291 }
16292 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016293 }
16294 if (error == TRUE)
16295 return;
16296
16297 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16298 if (id == 1 || id == 2)
16299 {
16300 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16301 return;
16302 }
16303
Bram Moolenaar6561d522015-07-21 15:48:27 +020016304 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16305 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016306#endif
16307}
16308
16309/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016310 * "matcharg()" function
16311 */
16312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016313f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016314{
16315 if (rettv_list_alloc(rettv) == OK)
16316 {
16317#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016318 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016319 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016320
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016321 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016322 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016323 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16324 {
16325 list_append_string(rettv->vval.v_list,
16326 syn_id2name(m->hlg_id), -1);
16327 list_append_string(rettv->vval.v_list, m->pattern, -1);
16328 }
16329 else
16330 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016331 list_append_string(rettv->vval.v_list, NULL, -1);
16332 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016333 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016334 }
16335#endif
16336 }
16337}
16338
16339/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016340 * "matchdelete()" function
16341 */
16342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016343f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016344{
16345#ifdef FEAT_SEARCH_EXTRA
16346 rettv->vval.v_number = match_delete(curwin,
16347 (int)get_tv_number(&argvars[0]), TRUE);
16348#endif
16349}
16350
16351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016352 * "matchend()" function
16353 */
16354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016355f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356{
16357 find_some_match(argvars, rettv, 0);
16358}
16359
16360/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016361 * "matchlist()" function
16362 */
16363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016364f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016365{
16366 find_some_match(argvars, rettv, 3);
16367}
16368
16369/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016370 * "matchstr()" function
16371 */
16372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016373f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016374{
16375 find_some_match(argvars, rettv, 2);
16376}
16377
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016378/*
16379 * "matchstrpos()" function
16380 */
16381 static void
16382f_matchstrpos(typval_T *argvars, typval_T *rettv)
16383{
16384 find_some_match(argvars, rettv, 4);
16385}
16386
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016387static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016388
16389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016390max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016391{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016392 varnumber_T n = 0;
16393 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016394 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016395
16396 if (argvars[0].v_type == VAR_LIST)
16397 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016398 list_T *l;
16399 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016400
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016401 l = argvars[0].vval.v_list;
16402 if (l != NULL)
16403 {
16404 li = l->lv_first;
16405 if (li != NULL)
16406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016407 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016408 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016409 {
16410 li = li->li_next;
16411 if (li == NULL)
16412 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016413 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016414 if (domax ? i > n : i < n)
16415 n = i;
16416 }
16417 }
16418 }
16419 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016420 else if (argvars[0].v_type == VAR_DICT)
16421 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016422 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016423 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016424 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016425 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016426
16427 d = argvars[0].vval.v_dict;
16428 if (d != NULL)
16429 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016430 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016431 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016433 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016435 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016436 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016437 if (first)
16438 {
16439 n = i;
16440 first = FALSE;
16441 }
16442 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016443 n = i;
16444 }
16445 }
16446 }
16447 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016448 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016449 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016450 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016451}
16452
16453/*
16454 * "max()" function
16455 */
16456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016457f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016458{
16459 max_min(argvars, rettv, TRUE);
16460}
16461
16462/*
16463 * "min()" function
16464 */
16465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016466f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016467{
16468 max_min(argvars, rettv, FALSE);
16469}
16470
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016471static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016472
16473/*
16474 * Create the directory in which "dir" is located, and higher levels when
16475 * needed.
16476 */
16477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016478mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016479{
16480 char_u *p;
16481 char_u *updir;
16482 int r = FAIL;
16483
16484 /* Get end of directory name in "dir".
16485 * We're done when it's "/" or "c:/". */
16486 p = gettail_sep(dir);
16487 if (p <= get_past_head(dir))
16488 return OK;
16489
16490 /* If the directory exists we're done. Otherwise: create it.*/
16491 updir = vim_strnsave(dir, (int)(p - dir));
16492 if (updir == NULL)
16493 return FAIL;
16494 if (mch_isdir(updir))
16495 r = OK;
16496 else if (mkdir_recurse(updir, prot) == OK)
16497 r = vim_mkdir_emsg(updir, prot);
16498 vim_free(updir);
16499 return r;
16500}
16501
16502#ifdef vim_mkdir
16503/*
16504 * "mkdir()" function
16505 */
16506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016507f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016508{
16509 char_u *dir;
16510 char_u buf[NUMBUFLEN];
16511 int prot = 0755;
16512
16513 rettv->vval.v_number = FAIL;
16514 if (check_restricted() || check_secure())
16515 return;
16516
16517 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016518 if (*dir == NUL)
16519 rettv->vval.v_number = FAIL;
16520 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016521 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016522 if (*gettail(dir) == NUL)
16523 /* remove trailing slashes */
16524 *gettail_sep(dir) = NUL;
16525
16526 if (argvars[1].v_type != VAR_UNKNOWN)
16527 {
16528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016529 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016530 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16531 mkdir_recurse(dir, prot);
16532 }
16533 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016534 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016535}
16536#endif
16537
Bram Moolenaar0d660222005-01-07 21:51:51 +000016538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 * "mode()" function
16540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016542f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016544 char_u buf[3];
16545
16546 buf[1] = NUL;
16547 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016549 if (time_for_testing == 93784)
16550 {
16551 /* Testing the two-character code. */
16552 buf[0] = 'x';
16553 buf[1] = '!';
16554 }
16555 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 {
16557 if (VIsual_select)
16558 buf[0] = VIsual_mode + 's' - 'v';
16559 else
16560 buf[0] = VIsual_mode;
16561 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016562 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016563 || State == CONFIRM)
16564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016566 if (State == ASKMORE)
16567 buf[1] = 'm';
16568 else if (State == CONFIRM)
16569 buf[1] = '?';
16570 }
16571 else if (State == EXTERNCMD)
16572 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 else if (State & INSERT)
16574 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016575#ifdef FEAT_VREPLACE
16576 if (State & VREPLACE_FLAG)
16577 {
16578 buf[0] = 'R';
16579 buf[1] = 'v';
16580 }
16581 else
16582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583 if (State & REPLACE_FLAG)
16584 buf[0] = 'R';
16585 else
16586 buf[0] = 'i';
16587 }
16588 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016591 if (exmode_active)
16592 buf[1] = 'v';
16593 }
16594 else if (exmode_active)
16595 {
16596 buf[0] = 'c';
16597 buf[1] = 'e';
16598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016602 if (finish_op)
16603 buf[1] = 'o';
16604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016606 /* Clear out the minor mode when the argument is not a non-zero number or
16607 * non-empty string. */
16608 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016609 buf[1] = NUL;
16610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016611 rettv->vval.v_string = vim_strsave(buf);
16612 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613}
16614
Bram Moolenaar429fa852013-04-15 12:27:36 +020016615#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016616/*
16617 * "mzeval()" function
16618 */
16619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016620f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016621{
16622 char_u *str;
16623 char_u buf[NUMBUFLEN];
16624
16625 str = get_tv_string_buf(&argvars[0], buf);
16626 do_mzeval(str, rettv);
16627}
Bram Moolenaar75676462013-01-30 14:55:42 +010016628
16629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016630mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016631{
16632 typval_T argvars[3];
16633
16634 argvars[0].v_type = VAR_STRING;
16635 argvars[0].vval.v_string = name;
16636 copy_tv(args, &argvars[1]);
16637 argvars[2].v_type = VAR_UNKNOWN;
16638 f_call(argvars, rettv);
16639 clear_tv(&argvars[1]);
16640}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016641#endif
16642
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016644 * "nextnonblank()" function
16645 */
16646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016647f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016648{
16649 linenr_T lnum;
16650
16651 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16652 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016653 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016654 {
16655 lnum = 0;
16656 break;
16657 }
16658 if (*skipwhite(ml_get(lnum)) != NUL)
16659 break;
16660 }
16661 rettv->vval.v_number = lnum;
16662}
16663
16664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 * "nr2char()" function
16666 */
16667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016668f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669{
16670 char_u buf[NUMBUFLEN];
16671
16672#ifdef FEAT_MBYTE
16673 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016674 {
16675 int utf8 = 0;
16676
16677 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016678 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016679 if (utf8)
16680 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16681 else
16682 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 else
16685#endif
16686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016687 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688 buf[1] = NUL;
16689 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016690 rettv->v_type = VAR_STRING;
16691 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016692}
16693
16694/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016695 * "or(expr, expr)" function
16696 */
16697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016698f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016699{
16700 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16701 | get_tv_number_chk(&argvars[1], NULL);
16702}
16703
16704/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016705 * "pathshorten()" function
16706 */
16707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016708f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016709{
16710 char_u *p;
16711
16712 rettv->v_type = VAR_STRING;
16713 p = get_tv_string_chk(&argvars[0]);
16714 if (p == NULL)
16715 rettv->vval.v_string = NULL;
16716 else
16717 {
16718 p = vim_strsave(p);
16719 rettv->vval.v_string = p;
16720 if (p != NULL)
16721 shorten_dir(p);
16722 }
16723}
16724
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016725#ifdef FEAT_PERL
16726/*
16727 * "perleval()" function
16728 */
16729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016730f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016731{
16732 char_u *str;
16733 char_u buf[NUMBUFLEN];
16734
16735 str = get_tv_string_buf(&argvars[0], buf);
16736 do_perleval(str, rettv);
16737}
16738#endif
16739
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016740#ifdef FEAT_FLOAT
16741/*
16742 * "pow()" function
16743 */
16744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016745f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016746{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016747 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016748
16749 rettv->v_type = VAR_FLOAT;
16750 if (get_float_arg(argvars, &fx) == OK
16751 && get_float_arg(&argvars[1], &fy) == OK)
16752 rettv->vval.v_float = pow(fx, fy);
16753 else
16754 rettv->vval.v_float = 0.0;
16755}
16756#endif
16757
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016758/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016759 * "prevnonblank()" function
16760 */
16761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016762f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016763{
16764 linenr_T lnum;
16765
16766 lnum = get_tv_lnum(argvars);
16767 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16768 lnum = 0;
16769 else
16770 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16771 --lnum;
16772 rettv->vval.v_number = lnum;
16773}
16774
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016775/* This dummy va_list is here because:
16776 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16777 * - locally in the function results in a "used before set" warning
16778 * - using va_start() to initialize it gives "function with fixed args" error */
16779static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016780
Bram Moolenaar8c711452005-01-14 21:53:12 +000016781/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016782 * "printf()" function
16783 */
16784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016785f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016786{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016787 char_u buf[NUMBUFLEN];
16788 int len;
16789 char_u *s;
16790 int saved_did_emsg = did_emsg;
16791 char *fmt;
16792
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016793 rettv->v_type = VAR_STRING;
16794 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016795
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016796 /* Get the required length, allocate the buffer and do it for real. */
16797 did_emsg = FALSE;
16798 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16799 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16800 if (!did_emsg)
16801 {
16802 s = alloc(len + 1);
16803 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016804 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016805 rettv->vval.v_string = s;
16806 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016807 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016808 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016809 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016810}
16811
16812/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016813 * "pumvisible()" function
16814 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016816f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016817{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016818#ifdef FEAT_INS_EXPAND
16819 if (pum_visible())
16820 rettv->vval.v_number = 1;
16821#endif
16822}
16823
Bram Moolenaardb913952012-06-29 12:54:53 +020016824#ifdef FEAT_PYTHON3
16825/*
16826 * "py3eval()" function
16827 */
16828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016829f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016830{
16831 char_u *str;
16832 char_u buf[NUMBUFLEN];
16833
16834 str = get_tv_string_buf(&argvars[0], buf);
16835 do_py3eval(str, rettv);
16836}
16837#endif
16838
16839#ifdef FEAT_PYTHON
16840/*
16841 * "pyeval()" function
16842 */
16843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016844f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016845{
16846 char_u *str;
16847 char_u buf[NUMBUFLEN];
16848
16849 str = get_tv_string_buf(&argvars[0], buf);
16850 do_pyeval(str, rettv);
16851}
16852#endif
16853
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016854/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016855 * "range()" function
16856 */
16857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016858f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016859{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016860 varnumber_T start;
16861 varnumber_T end;
16862 varnumber_T stride = 1;
16863 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016864 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016867 if (argvars[1].v_type == VAR_UNKNOWN)
16868 {
16869 end = start - 1;
16870 start = 0;
16871 }
16872 else
16873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016874 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016876 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016877 }
16878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016879 if (error)
16880 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016881 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016882 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016883 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016884 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016885 else
16886 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016887 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016888 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016889 if (list_append_number(rettv->vval.v_list,
16890 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016891 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016892 }
16893}
16894
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016895/*
16896 * "readfile()" function
16897 */
16898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016899f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016900{
16901 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016902 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016903 char_u *fname;
16904 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016905 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16906 int io_size = sizeof(buf);
16907 int readlen; /* size of last fread() */
16908 char_u *prev = NULL; /* previously read bytes, if any */
16909 long prevlen = 0; /* length of data in prev */
16910 long prevsize = 0; /* size of prev buffer */
16911 long maxline = MAXLNUM;
16912 long cnt = 0;
16913 char_u *p; /* position in buf */
16914 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016915
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016916 if (argvars[1].v_type != VAR_UNKNOWN)
16917 {
16918 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16919 binary = TRUE;
16920 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016921 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016922 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016923
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016924 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016925 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016926
16927 /* Always open the file in binary mode, library functions have a mind of
16928 * their own about CR-LF conversion. */
16929 fname = get_tv_string(&argvars[0]);
16930 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16931 {
16932 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16933 return;
16934 }
16935
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016936 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016937 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016938 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016939
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016940 /* This for loop processes what was read, but is also entered at end
16941 * of file so that either:
16942 * - an incomplete line gets written
16943 * - a "binary" file gets an empty line at the end if it ends in a
16944 * newline. */
16945 for (p = buf, start = buf;
16946 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16947 ++p)
16948 {
16949 if (*p == '\n' || readlen <= 0)
16950 {
16951 listitem_T *li;
16952 char_u *s = NULL;
16953 long_u len = p - start;
16954
16955 /* Finished a line. Remove CRs before NL. */
16956 if (readlen > 0 && !binary)
16957 {
16958 while (len > 0 && start[len - 1] == '\r')
16959 --len;
16960 /* removal may cross back to the "prev" string */
16961 if (len == 0)
16962 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16963 --prevlen;
16964 }
16965 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016966 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016967 else
16968 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016969 /* Change "prev" buffer to be the right size. This way
16970 * the bytes are only copied once, and very long lines are
16971 * allocated only once. */
16972 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016973 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016974 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016975 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016976 prev = NULL; /* the list will own the string */
16977 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016978 }
16979 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016980 if (s == NULL)
16981 {
16982 do_outofmem_msg((long_u) prevlen + len + 1);
16983 failed = TRUE;
16984 break;
16985 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016986
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016987 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016988 {
16989 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016990 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016991 break;
16992 }
16993 li->li_tv.v_type = VAR_STRING;
16994 li->li_tv.v_lock = 0;
16995 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016996 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016997
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016998 start = p + 1; /* step over newline */
16999 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017000 break;
17001 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017002 else if (*p == NUL)
17003 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020017004#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017005 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
17006 * when finding the BF and check the previous two bytes. */
17007 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020017008 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017009 /* Find the two bytes before the 0xbf. If p is at buf, or buf
17010 * + 1, these may be in the "prev" string. */
17011 char_u back1 = p >= buf + 1 ? p[-1]
17012 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
17013 char_u back2 = p >= buf + 2 ? p[-2]
17014 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
17015 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017016
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017017 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017018 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017019 char_u *dest = p - 2;
17020
17021 /* Usually a BOM is at the beginning of a file, and so at
17022 * the beginning of a line; then we can just step over it.
17023 */
17024 if (start == dest)
17025 start = p + 1;
17026 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020017027 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017028 /* have to shuffle buf to close gap */
17029 int adjust_prevlen = 0;
17030
17031 if (dest < buf)
17032 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017033 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017034 dest = buf;
17035 }
17036 if (readlen > p - buf + 1)
17037 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
17038 readlen -= 3 - adjust_prevlen;
17039 prevlen -= adjust_prevlen;
17040 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020017041 }
17042 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017043 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017044#endif
17045 } /* for */
17046
17047 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
17048 break;
17049 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017050 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017051 /* There's part of a line in buf, store it in "prev". */
17052 if (p - start + prevlen >= prevsize)
17053 {
17054 /* need bigger "prev" buffer */
17055 char_u *newprev;
17056
17057 /* A common use case is ordinary text files and "prev" gets a
17058 * fragment of a line, so the first allocation is made
17059 * small, to avoid repeatedly 'allocing' large and
17060 * 'reallocing' small. */
17061 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017062 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017063 else
17064 {
17065 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017066 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017067 prevsize = grow50pc > growmin ? grow50pc : growmin;
17068 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020017069 newprev = prev == NULL ? alloc(prevsize)
17070 : vim_realloc(prev, prevsize);
17071 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017072 {
17073 do_outofmem_msg((long_u)prevsize);
17074 failed = TRUE;
17075 break;
17076 }
17077 prev = newprev;
17078 }
17079 /* Add the line part to end of "prev". */
17080 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017081 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017082 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017083 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017084
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017085 /*
17086 * For a negative line count use only the lines at the end of the file,
17087 * free the rest.
17088 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017089 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017090 while (cnt > -maxline)
17091 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017092 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017093 --cnt;
17094 }
17095
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017096 if (failed)
17097 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020017098 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017099 /* readfile doc says an empty list is returned on error */
17100 rettv->vval.v_list = list_alloc();
17101 }
17102
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017103 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017104 fclose(fd);
17105}
17106
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017107#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017108static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017109
17110/*
17111 * Convert a List to proftime_T.
17112 * Return FAIL when there is something wrong.
17113 */
17114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017115list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017116{
17117 long n1, n2;
17118 int error = FALSE;
17119
17120 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
17121 || arg->vval.v_list->lv_len != 2)
17122 return FAIL;
17123 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
17124 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
17125# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017126 tm->HighPart = n1;
17127 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017128# else
17129 tm->tv_sec = n1;
17130 tm->tv_usec = n2;
17131# endif
17132 return error ? FAIL : OK;
17133}
17134#endif /* FEAT_RELTIME */
17135
17136/*
17137 * "reltime()" function
17138 */
17139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017140f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017141{
17142#ifdef FEAT_RELTIME
17143 proftime_T res;
17144 proftime_T start;
17145
17146 if (argvars[0].v_type == VAR_UNKNOWN)
17147 {
17148 /* No arguments: get current time. */
17149 profile_start(&res);
17150 }
17151 else if (argvars[1].v_type == VAR_UNKNOWN)
17152 {
17153 if (list2proftime(&argvars[0], &res) == FAIL)
17154 return;
17155 profile_end(&res);
17156 }
17157 else
17158 {
17159 /* Two arguments: compute the difference. */
17160 if (list2proftime(&argvars[0], &start) == FAIL
17161 || list2proftime(&argvars[1], &res) == FAIL)
17162 return;
17163 profile_sub(&res, &start);
17164 }
17165
17166 if (rettv_list_alloc(rettv) == OK)
17167 {
17168 long n1, n2;
17169
17170# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017171 n1 = res.HighPart;
17172 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017173# else
17174 n1 = res.tv_sec;
17175 n2 = res.tv_usec;
17176# endif
17177 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17178 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17179 }
17180#endif
17181}
17182
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017183#ifdef FEAT_FLOAT
17184/*
17185 * "reltimefloat()" function
17186 */
17187 static void
17188f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17189{
17190# ifdef FEAT_RELTIME
17191 proftime_T tm;
17192# endif
17193
17194 rettv->v_type = VAR_FLOAT;
17195 rettv->vval.v_float = 0;
17196# ifdef FEAT_RELTIME
17197 if (list2proftime(&argvars[0], &tm) == OK)
17198 rettv->vval.v_float = profile_float(&tm);
17199# endif
17200}
17201#endif
17202
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017203/*
17204 * "reltimestr()" function
17205 */
17206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017207f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017208{
17209#ifdef FEAT_RELTIME
17210 proftime_T tm;
17211#endif
17212
17213 rettv->v_type = VAR_STRING;
17214 rettv->vval.v_string = NULL;
17215#ifdef FEAT_RELTIME
17216 if (list2proftime(&argvars[0], &tm) == OK)
17217 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17218#endif
17219}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017220
Bram Moolenaar0d660222005-01-07 21:51:51 +000017221#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017222static void make_connection(void);
17223static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017224
17225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017226make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227{
17228 if (X_DISPLAY == NULL
17229# ifdef FEAT_GUI
17230 && !gui.in_use
17231# endif
17232 )
17233 {
17234 x_force_connect = TRUE;
17235 setup_term_clip();
17236 x_force_connect = FALSE;
17237 }
17238}
17239
17240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017241check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017242{
17243 make_connection();
17244 if (X_DISPLAY == NULL)
17245 {
17246 EMSG(_("E240: No connection to Vim server"));
17247 return FAIL;
17248 }
17249 return OK;
17250}
17251#endif
17252
17253#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017255remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017256{
17257 char_u *server_name;
17258 char_u *keys;
17259 char_u *r = NULL;
17260 char_u buf[NUMBUFLEN];
17261# ifdef WIN32
17262 HWND w;
17263# else
17264 Window w;
17265# endif
17266
17267 if (check_restricted() || check_secure())
17268 return;
17269
17270# ifdef FEAT_X11
17271 if (check_connection() == FAIL)
17272 return;
17273# endif
17274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017275 server_name = get_tv_string_chk(&argvars[0]);
17276 if (server_name == NULL)
17277 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017278 keys = get_tv_string_buf(&argvars[1], buf);
17279# ifdef WIN32
17280 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17281# else
17282 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17283 < 0)
17284# endif
17285 {
17286 if (r != NULL)
17287 EMSG(r); /* sending worked but evaluation failed */
17288 else
17289 EMSG2(_("E241: Unable to send to %s"), server_name);
17290 return;
17291 }
17292
17293 rettv->vval.v_string = r;
17294
17295 if (argvars[2].v_type != VAR_UNKNOWN)
17296 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017297 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017298 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017299 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017300
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017301 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 v.di_tv.v_type = VAR_STRING;
17303 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017304 idvar = get_tv_string_chk(&argvars[2]);
17305 if (idvar != NULL)
17306 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017307 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017308 }
17309}
17310#endif
17311
17312/*
17313 * "remote_expr()" function
17314 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017316f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317{
17318 rettv->v_type = VAR_STRING;
17319 rettv->vval.v_string = NULL;
17320#ifdef FEAT_CLIENTSERVER
17321 remote_common(argvars, rettv, TRUE);
17322#endif
17323}
17324
17325/*
17326 * "remote_foreground()" function
17327 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017329f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017330{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017331#ifdef FEAT_CLIENTSERVER
17332# ifdef WIN32
17333 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017334 {
17335 char_u *server_name = get_tv_string_chk(&argvars[0]);
17336
17337 if (server_name != NULL)
17338 serverForeground(server_name);
17339 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017340# else
17341 /* Send a foreground() expression to the server. */
17342 argvars[1].v_type = VAR_STRING;
17343 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17344 argvars[2].v_type = VAR_UNKNOWN;
17345 remote_common(argvars, rettv, TRUE);
17346 vim_free(argvars[1].vval.v_string);
17347# endif
17348#endif
17349}
17350
Bram Moolenaar0d660222005-01-07 21:51:51 +000017351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017352f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017353{
17354#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017355 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017356 char_u *s = NULL;
17357# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017358 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017359# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017360 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017361
17362 if (check_restricted() || check_secure())
17363 {
17364 rettv->vval.v_number = -1;
17365 return;
17366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 serverid = get_tv_string_chk(&argvars[0]);
17368 if (serverid == NULL)
17369 {
17370 rettv->vval.v_number = -1;
17371 return; /* type error; errmsg already given */
17372 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017373# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017374 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017375 if (n == 0)
17376 rettv->vval.v_number = -1;
17377 else
17378 {
17379 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17380 rettv->vval.v_number = (s != NULL);
17381 }
17382# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017383 if (check_connection() == FAIL)
17384 return;
17385
17386 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017387 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017388# endif
17389
17390 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017392 char_u *retvar;
17393
Bram Moolenaar33570922005-01-25 22:26:29 +000017394 v.di_tv.v_type = VAR_STRING;
17395 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017396 retvar = get_tv_string_chk(&argvars[1]);
17397 if (retvar != NULL)
17398 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017399 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017400 }
17401#else
17402 rettv->vval.v_number = -1;
17403#endif
17404}
17405
Bram Moolenaar0d660222005-01-07 21:51:51 +000017406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017407f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017408{
17409 char_u *r = NULL;
17410
17411#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017412 char_u *serverid = get_tv_string_chk(&argvars[0]);
17413
17414 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017415 {
17416# ifdef WIN32
17417 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017418 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017419
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017420 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017421 if (n != 0)
17422 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17423 if (r == NULL)
17424# else
17425 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017426 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017427# endif
17428 EMSG(_("E277: Unable to read a server reply"));
17429 }
17430#endif
17431 rettv->v_type = VAR_STRING;
17432 rettv->vval.v_string = r;
17433}
17434
17435/*
17436 * "remote_send()" function
17437 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017439f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440{
17441 rettv->v_type = VAR_STRING;
17442 rettv->vval.v_string = NULL;
17443#ifdef FEAT_CLIENTSERVER
17444 remote_common(argvars, rettv, FALSE);
17445#endif
17446}
17447
17448/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017449 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017450 */
17451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017452f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017453{
Bram Moolenaar33570922005-01-25 22:26:29 +000017454 list_T *l;
17455 listitem_T *item, *item2;
17456 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017457 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017458 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017459 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017460 dict_T *d;
17461 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017462 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017463
Bram Moolenaar8c711452005-01-14 21:53:12 +000017464 if (argvars[0].v_type == VAR_DICT)
17465 {
17466 if (argvars[2].v_type != VAR_UNKNOWN)
17467 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017468 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017469 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017471 key = get_tv_string_chk(&argvars[1]);
17472 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017474 di = dict_find(d, key, -1);
17475 if (di == NULL)
17476 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017477 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17478 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017479 {
17480 *rettv = di->di_tv;
17481 init_tv(&di->di_tv);
17482 dictitem_remove(d, di);
17483 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017484 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017485 }
17486 }
17487 else if (argvars[0].v_type != VAR_LIST)
17488 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017489 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017490 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017492 int error = FALSE;
17493
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017494 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017495 if (error)
17496 ; /* type error: do nothing, errmsg already given */
17497 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017498 EMSGN(_(e_listidx), idx);
17499 else
17500 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017501 if (argvars[2].v_type == VAR_UNKNOWN)
17502 {
17503 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017504 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017505 *rettv = item->li_tv;
17506 vim_free(item);
17507 }
17508 else
17509 {
17510 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017511 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017512 if (error)
17513 ; /* type error: do nothing */
17514 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017515 EMSGN(_(e_listidx), end);
17516 else
17517 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017518 int cnt = 0;
17519
17520 for (li = item; li != NULL; li = li->li_next)
17521 {
17522 ++cnt;
17523 if (li == item2)
17524 break;
17525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017526 if (li == NULL) /* didn't find "item2" after "item" */
17527 EMSG(_(e_invrange));
17528 else
17529 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017530 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017531 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017532 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017533 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017534 l->lv_first = item;
17535 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017536 item->li_prev = NULL;
17537 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017538 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017539 }
17540 }
17541 }
17542 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017543 }
17544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545}
17546
17547/*
17548 * "rename({from}, {to})" function
17549 */
17550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017551f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552{
17553 char_u buf[NUMBUFLEN];
17554
17555 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17559 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560}
17561
17562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017563 * "repeat()" function
17564 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017566f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017567{
17568 char_u *p;
17569 int n;
17570 int slen;
17571 int len;
17572 char_u *r;
17573 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017574
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017575 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017576 if (argvars[0].v_type == VAR_LIST)
17577 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017578 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017579 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017580 if (list_extend(rettv->vval.v_list,
17581 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017582 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017583 }
17584 else
17585 {
17586 p = get_tv_string(&argvars[0]);
17587 rettv->v_type = VAR_STRING;
17588 rettv->vval.v_string = NULL;
17589
17590 slen = (int)STRLEN(p);
17591 len = slen * n;
17592 if (len <= 0)
17593 return;
17594
17595 r = alloc(len + 1);
17596 if (r != NULL)
17597 {
17598 for (i = 0; i < n; i++)
17599 mch_memmove(r + i * slen, p, (size_t)slen);
17600 r[len] = NUL;
17601 }
17602
17603 rettv->vval.v_string = r;
17604 }
17605}
17606
17607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 * "resolve()" function
17609 */
17610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017611f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612{
17613 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017614#ifdef HAVE_READLINK
17615 char_u *buf = NULL;
17616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017618 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619#ifdef FEAT_SHORTCUT
17620 {
17621 char_u *v = NULL;
17622
17623 v = mch_resolve_shortcut(p);
17624 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017625 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 }
17629#else
17630# ifdef HAVE_READLINK
17631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 char_u *cpy;
17633 int len;
17634 char_u *remain = NULL;
17635 char_u *q;
17636 int is_relative_to_current = FALSE;
17637 int has_trailing_pathsep = FALSE;
17638 int limit = 100;
17639
17640 p = vim_strsave(p);
17641
17642 if (p[0] == '.' && (vim_ispathsep(p[1])
17643 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17644 is_relative_to_current = TRUE;
17645
17646 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017647 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017648 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017650 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652
17653 q = getnextcomp(p);
17654 if (*q != NUL)
17655 {
17656 /* Separate the first path component in "p", and keep the
17657 * remainder (beginning with the path separator). */
17658 remain = vim_strsave(q - 1);
17659 q[-1] = NUL;
17660 }
17661
Bram Moolenaard9462e32011-04-11 21:35:11 +020017662 buf = alloc(MAXPATHL + 1);
17663 if (buf == NULL)
17664 goto fail;
17665
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 for (;;)
17667 {
17668 for (;;)
17669 {
17670 len = readlink((char *)p, (char *)buf, MAXPATHL);
17671 if (len <= 0)
17672 break;
17673 buf[len] = NUL;
17674
17675 if (limit-- == 0)
17676 {
17677 vim_free(p);
17678 vim_free(remain);
17679 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 goto fail;
17682 }
17683
17684 /* Ensure that the result will have a trailing path separator
17685 * if the argument has one. */
17686 if (remain == NULL && has_trailing_pathsep)
17687 add_pathsep(buf);
17688
17689 /* Separate the first path component in the link value and
17690 * concatenate the remainders. */
17691 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17692 if (*q != NUL)
17693 {
17694 if (remain == NULL)
17695 remain = vim_strsave(q - 1);
17696 else
17697 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017698 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 if (cpy != NULL)
17700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 vim_free(remain);
17702 remain = cpy;
17703 }
17704 }
17705 q[-1] = NUL;
17706 }
17707
17708 q = gettail(p);
17709 if (q > p && *q == NUL)
17710 {
17711 /* Ignore trailing path separator. */
17712 q[-1] = NUL;
17713 q = gettail(p);
17714 }
17715 if (q > p && !mch_isFullName(buf))
17716 {
17717 /* symlink is relative to directory of argument */
17718 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17719 if (cpy != NULL)
17720 {
17721 STRCPY(cpy, p);
17722 STRCPY(gettail(cpy), buf);
17723 vim_free(p);
17724 p = cpy;
17725 }
17726 }
17727 else
17728 {
17729 vim_free(p);
17730 p = vim_strsave(buf);
17731 }
17732 }
17733
17734 if (remain == NULL)
17735 break;
17736
17737 /* Append the first path component of "remain" to "p". */
17738 q = getnextcomp(remain + 1);
17739 len = q - remain - (*q != NUL);
17740 cpy = vim_strnsave(p, STRLEN(p) + len);
17741 if (cpy != NULL)
17742 {
17743 STRNCAT(cpy, remain, len);
17744 vim_free(p);
17745 p = cpy;
17746 }
17747 /* Shorten "remain". */
17748 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017749 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 else
17751 {
17752 vim_free(remain);
17753 remain = NULL;
17754 }
17755 }
17756
17757 /* If the result is a relative path name, make it explicitly relative to
17758 * the current directory if and only if the argument had this form. */
17759 if (!vim_ispathsep(*p))
17760 {
17761 if (is_relative_to_current
17762 && *p != NUL
17763 && !(p[0] == '.'
17764 && (p[1] == NUL
17765 || vim_ispathsep(p[1])
17766 || (p[1] == '.'
17767 && (p[2] == NUL
17768 || vim_ispathsep(p[2]))))))
17769 {
17770 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017771 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772 if (cpy != NULL)
17773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 vim_free(p);
17775 p = cpy;
17776 }
17777 }
17778 else if (!is_relative_to_current)
17779 {
17780 /* Strip leading "./". */
17781 q = p;
17782 while (q[0] == '.' && vim_ispathsep(q[1]))
17783 q += 2;
17784 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017785 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 }
17787 }
17788
17789 /* Ensure that the result will have no trailing path separator
17790 * if the argument had none. But keep "/" or "//". */
17791 if (!has_trailing_pathsep)
17792 {
17793 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017794 if (after_pathsep(p, q))
17795 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 }
17797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017798 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 }
17800# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017801 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802# endif
17803#endif
17804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017805 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806
17807#ifdef HAVE_READLINK
17808fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017809 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017811 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812}
17813
17814/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017815 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 */
17817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017818f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819{
Bram Moolenaar33570922005-01-25 22:26:29 +000017820 list_T *l;
17821 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822
Bram Moolenaar0d660222005-01-07 21:51:51 +000017823 if (argvars[0].v_type != VAR_LIST)
17824 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017825 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017826 && !tv_check_lock(l->lv_lock,
17827 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017828 {
17829 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017830 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017831 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017832 while (li != NULL)
17833 {
17834 ni = li->li_prev;
17835 list_append(l, li);
17836 li = ni;
17837 }
17838 rettv->vval.v_list = l;
17839 rettv->v_type = VAR_LIST;
17840 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017841 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843}
17844
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017845#define SP_NOMOVE 0x01 /* don't move cursor */
17846#define SP_REPEAT 0x02 /* repeat to find outer pair */
17847#define SP_RETCOUNT 0x04 /* return matchcount */
17848#define SP_SETPCMARK 0x08 /* set previous context mark */
17849#define SP_START 0x10 /* accept match at start position */
17850#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17851#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017852#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017853
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017854static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017855
17856/*
17857 * Get flags for a search function.
17858 * Possibly sets "p_ws".
17859 * Returns BACKWARD, FORWARD or zero (for an error).
17860 */
17861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017862get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017863{
17864 int dir = FORWARD;
17865 char_u *flags;
17866 char_u nbuf[NUMBUFLEN];
17867 int mask;
17868
17869 if (varp->v_type != VAR_UNKNOWN)
17870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017871 flags = get_tv_string_buf_chk(varp, nbuf);
17872 if (flags == NULL)
17873 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017874 while (*flags != NUL)
17875 {
17876 switch (*flags)
17877 {
17878 case 'b': dir = BACKWARD; break;
17879 case 'w': p_ws = TRUE; break;
17880 case 'W': p_ws = FALSE; break;
17881 default: mask = 0;
17882 if (flagsp != NULL)
17883 switch (*flags)
17884 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017885 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017886 case 'e': mask = SP_END; break;
17887 case 'm': mask = SP_RETCOUNT; break;
17888 case 'n': mask = SP_NOMOVE; break;
17889 case 'p': mask = SP_SUBPAT; break;
17890 case 'r': mask = SP_REPEAT; break;
17891 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017892 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017893 }
17894 if (mask == 0)
17895 {
17896 EMSG2(_(e_invarg2), flags);
17897 dir = 0;
17898 }
17899 else
17900 *flagsp |= mask;
17901 }
17902 if (dir == 0)
17903 break;
17904 ++flags;
17905 }
17906 }
17907 return dir;
17908}
17909
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017911 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017914search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017916 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 char_u *pat;
17918 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017919 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 int save_p_ws = p_ws;
17921 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017922 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017923 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017924 proftime_T tm;
17925#ifdef FEAT_RELTIME
17926 long time_limit = 0;
17927#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017928 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017929 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017931 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017932 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017933 if (dir == 0)
17934 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017935 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017936 if (flags & SP_START)
17937 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017938 if (flags & SP_END)
17939 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017940 if (flags & SP_COLUMN)
17941 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017942
Bram Moolenaar76929292008-01-06 19:07:36 +000017943 /* Optional arguments: line number to stop searching and timeout. */
17944 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017945 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017946 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017947 if (lnum_stop < 0)
17948 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017949#ifdef FEAT_RELTIME
17950 if (argvars[3].v_type != VAR_UNKNOWN)
17951 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017952 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017953 if (time_limit < 0)
17954 goto theend;
17955 }
17956#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017957 }
17958
Bram Moolenaar76929292008-01-06 19:07:36 +000017959#ifdef FEAT_RELTIME
17960 /* Set the time limit, if there is one. */
17961 profile_setlimit(time_limit, &tm);
17962#endif
17963
Bram Moolenaar231334e2005-07-25 20:46:57 +000017964 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017965 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017966 * Check to make sure only those flags are set.
17967 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17968 * flags cannot be set. Check for that condition also.
17969 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017970 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017971 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017973 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017974 goto theend;
17975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017977 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017978 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017979 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017980 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017982 if (flags & SP_SUBPAT)
17983 retval = subpatnum;
17984 else
17985 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017986 if (flags & SP_SETPCMARK)
17987 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017989 if (match_pos != NULL)
17990 {
17991 /* Store the match cursor position */
17992 match_pos->lnum = pos.lnum;
17993 match_pos->col = pos.col + 1;
17994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 /* "/$" will put the cursor after the end of the line, may need to
17996 * correct that here */
17997 check_cursor();
17998 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017999
18000 /* If 'n' flag is used: restore cursor position. */
18001 if (flags & SP_NOMOVE)
18002 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000018003 else
18004 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018005theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018007
18008 return retval;
18009}
18010
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018011#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018012
18013/*
18014 * round() is not in C90, use ceil() or floor() instead.
18015 */
18016 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010018017vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018018{
18019 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
18020}
18021
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018022/*
18023 * "round({float})" function
18024 */
18025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018026f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018027{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018028 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018029
18030 rettv->v_type = VAR_FLOAT;
18031 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018032 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018033 else
18034 rettv->vval.v_float = 0.0;
18035}
18036#endif
18037
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018038/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020018039 * "screenattr()" function
18040 */
18041 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010018042f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020018043{
18044 int row;
18045 int col;
18046 int c;
18047
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018048 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
18049 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020018050 if (row < 0 || row >= screen_Rows
18051 || col < 0 || col >= screen_Columns)
18052 c = -1;
18053 else
18054 c = ScreenAttrs[LineOffset[row] + col];
18055 rettv->vval.v_number = c;
18056}
18057
18058/*
18059 * "screenchar()" function
18060 */
18061 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010018062f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020018063{
18064 int row;
18065 int col;
18066 int off;
18067 int c;
18068
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018069 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
18070 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020018071 if (row < 0 || row >= screen_Rows
18072 || col < 0 || col >= screen_Columns)
18073 c = -1;
18074 else
18075 {
18076 off = LineOffset[row] + col;
18077#ifdef FEAT_MBYTE
18078 if (enc_utf8 && ScreenLinesUC[off] != 0)
18079 c = ScreenLinesUC[off];
18080 else
18081#endif
18082 c = ScreenLines[off];
18083 }
18084 rettv->vval.v_number = c;
18085}
18086
18087/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018088 * "screencol()" function
18089 *
18090 * First column is 1 to be consistent with virtcol().
18091 */
18092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018093f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018094{
18095 rettv->vval.v_number = screen_screencol() + 1;
18096}
18097
18098/*
18099 * "screenrow()" function
18100 */
18101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018102f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018103{
18104 rettv->vval.v_number = screen_screenrow() + 1;
18105}
18106
18107/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018108 * "search()" function
18109 */
18110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018111f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018112{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018113 int flags = 0;
18114
18115 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116}
18117
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018119 * "searchdecl()" function
18120 */
18121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018122f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018123{
18124 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018125 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018126 int error = FALSE;
18127 char_u *name;
18128
18129 rettv->vval.v_number = 1; /* default: FAIL */
18130
18131 name = get_tv_string_chk(&argvars[0]);
18132 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000018133 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018134 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018135 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018136 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018137 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018138 if (!error && name != NULL)
18139 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018140 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018141}
18142
18143/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018144 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018147searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148{
18149 char_u *spat, *mpat, *epat;
18150 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 int dir;
18153 int flags = 0;
18154 char_u nbuf1[NUMBUFLEN];
18155 char_u nbuf2[NUMBUFLEN];
18156 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018157 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018158 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018159 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018162 spat = get_tv_string_chk(&argvars[0]);
18163 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18164 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18165 if (spat == NULL || mpat == NULL || epat == NULL)
18166 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 /* Handle the optional fourth argument: flags */
18169 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018170 if (dir == 0)
18171 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018172
18173 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018174 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18175 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018176 if ((flags & (SP_END | SP_SUBPAT)) != 0
18177 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018178 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018179 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018180 goto theend;
18181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018183 /* Using 'r' implies 'W', otherwise it doesn't work. */
18184 if (flags & SP_REPEAT)
18185 p_ws = FALSE;
18186
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018187 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018188 if (argvars[3].v_type == VAR_UNKNOWN
18189 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 skip = (char_u *)"";
18191 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018193 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018194 if (argvars[5].v_type != VAR_UNKNOWN)
18195 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018196 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018197 if (lnum_stop < 0)
18198 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018199#ifdef FEAT_RELTIME
18200 if (argvars[6].v_type != VAR_UNKNOWN)
18201 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018202 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018203 if (time_limit < 0)
18204 goto theend;
18205 }
18206#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018207 }
18208 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018209 if (skip == NULL)
18210 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018212 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018213 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018214
18215theend:
18216 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018217
18218 return retval;
18219}
18220
18221/*
18222 * "searchpair()" function
18223 */
18224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018225f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018226{
18227 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18228}
18229
18230/*
18231 * "searchpairpos()" function
18232 */
18233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018234f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018235{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018236 pos_T match_pos;
18237 int lnum = 0;
18238 int col = 0;
18239
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018240 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018241 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018242
18243 if (searchpair_cmn(argvars, &match_pos) > 0)
18244 {
18245 lnum = match_pos.lnum;
18246 col = match_pos.col;
18247 }
18248
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018249 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18250 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018251}
18252
18253/*
18254 * Search for a start/middle/end thing.
18255 * Used by searchpair(), see its documentation for the details.
18256 * Returns 0 or -1 for no match,
18257 */
18258 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018259do_searchpair(
18260 char_u *spat, /* start pattern */
18261 char_u *mpat, /* middle pattern */
18262 char_u *epat, /* end pattern */
18263 int dir, /* BACKWARD or FORWARD */
18264 char_u *skip, /* skip expression */
18265 int flags, /* SP_SETPCMARK and other SP_ values */
18266 pos_T *match_pos,
18267 linenr_T lnum_stop, /* stop at this line if not zero */
18268 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018269{
18270 char_u *save_cpo;
18271 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18272 long retval = 0;
18273 pos_T pos;
18274 pos_T firstpos;
18275 pos_T foundpos;
18276 pos_T save_cursor;
18277 pos_T save_pos;
18278 int n;
18279 int r;
18280 int nest = 1;
18281 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018282 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018283 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018284
18285 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18286 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018287 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018288
Bram Moolenaar76929292008-01-06 19:07:36 +000018289#ifdef FEAT_RELTIME
18290 /* Set the time limit, if there is one. */
18291 profile_setlimit(time_limit, &tm);
18292#endif
18293
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018294 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18295 * start/middle/end (pat3, for the top pair). */
18296 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18297 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18298 if (pat2 == NULL || pat3 == NULL)
18299 goto theend;
18300 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18301 if (*mpat == NUL)
18302 STRCPY(pat3, pat2);
18303 else
18304 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18305 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018306 if (flags & SP_START)
18307 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018308
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 save_cursor = curwin->w_cursor;
18310 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018311 clearpos(&firstpos);
18312 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 pat = pat3;
18314 for (;;)
18315 {
18316 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018317 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18319 /* didn't find it or found the first match again: FAIL */
18320 break;
18321
18322 if (firstpos.lnum == 0)
18323 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018324 if (equalpos(pos, foundpos))
18325 {
18326 /* Found the same position again. Can happen with a pattern that
18327 * has "\zs" at the end and searching backwards. Advance one
18328 * character and try again. */
18329 if (dir == BACKWARD)
18330 decl(&pos);
18331 else
18332 incl(&pos);
18333 }
18334 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018336 /* clear the start flag to avoid getting stuck here */
18337 options &= ~SEARCH_START;
18338
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 /* If the skip pattern matches, ignore this match. */
18340 if (*skip != NUL)
18341 {
18342 save_pos = curwin->w_cursor;
18343 curwin->w_cursor = pos;
18344 r = eval_to_bool(skip, &err, NULL, FALSE);
18345 curwin->w_cursor = save_pos;
18346 if (err)
18347 {
18348 /* Evaluating {skip} caused an error, break here. */
18349 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018350 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351 break;
18352 }
18353 if (r)
18354 continue;
18355 }
18356
18357 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18358 {
18359 /* Found end when searching backwards or start when searching
18360 * forward: nested pair. */
18361 ++nest;
18362 pat = pat2; /* nested, don't search for middle */
18363 }
18364 else
18365 {
18366 /* Found end when searching forward or start when searching
18367 * backward: end of (nested) pair; or found middle in outer pair. */
18368 if (--nest == 1)
18369 pat = pat3; /* outer level, search for middle */
18370 }
18371
18372 if (nest == 0)
18373 {
18374 /* Found the match: return matchcount or line number. */
18375 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018376 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018378 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018379 if (flags & SP_SETPCMARK)
18380 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381 curwin->w_cursor = pos;
18382 if (!(flags & SP_REPEAT))
18383 break;
18384 nest = 1; /* search for next unmatched */
18385 }
18386 }
18387
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018388 if (match_pos != NULL)
18389 {
18390 /* Store the match cursor position */
18391 match_pos->lnum = curwin->w_cursor.lnum;
18392 match_pos->col = curwin->w_cursor.col + 1;
18393 }
18394
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018396 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 curwin->w_cursor = save_cursor;
18398
18399theend:
18400 vim_free(pat2);
18401 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018402 if (p_cpo == empty_option)
18403 p_cpo = save_cpo;
18404 else
18405 /* Darn, evaluating the {skip} expression changed the value. */
18406 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018407
18408 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409}
18410
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018411/*
18412 * "searchpos()" function
18413 */
18414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018415f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018416{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018417 pos_T match_pos;
18418 int lnum = 0;
18419 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018420 int n;
18421 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018422
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018423 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018424 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018425
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018426 n = search_cmn(argvars, &match_pos, &flags);
18427 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018428 {
18429 lnum = match_pos.lnum;
18430 col = match_pos.col;
18431 }
18432
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018433 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18434 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018435 if (flags & SP_SUBPAT)
18436 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018437}
18438
Bram Moolenaar0d660222005-01-07 21:51:51 +000018439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018440f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018442#ifdef FEAT_CLIENTSERVER
18443 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018444 char_u *server = get_tv_string_chk(&argvars[0]);
18445 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446
Bram Moolenaar0d660222005-01-07 21:51:51 +000018447 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018448 if (server == NULL || reply == NULL)
18449 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018450 if (check_restricted() || check_secure())
18451 return;
18452# ifdef FEAT_X11
18453 if (check_connection() == FAIL)
18454 return;
18455# endif
18456
18457 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018459 EMSG(_("E258: Unable to send to client"));
18460 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018462 rettv->vval.v_number = 0;
18463#else
18464 rettv->vval.v_number = -1;
18465#endif
18466}
18467
Bram Moolenaar0d660222005-01-07 21:51:51 +000018468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018469f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018470{
18471 char_u *r = NULL;
18472
18473#ifdef FEAT_CLIENTSERVER
18474# ifdef WIN32
18475 r = serverGetVimNames();
18476# else
18477 make_connection();
18478 if (X_DISPLAY != NULL)
18479 r = serverGetVimNames(X_DISPLAY);
18480# endif
18481#endif
18482 rettv->v_type = VAR_STRING;
18483 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484}
18485
18486/*
18487 * "setbufvar()" function
18488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018490f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491{
18492 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018495 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 char_u nbuf[NUMBUFLEN];
18497
18498 if (check_restricted() || check_secure())
18499 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018500 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18501 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018502 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 varp = &argvars[2];
18504
18505 if (buf != NULL && varname != NULL && varp != NULL)
18506 {
18507 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509
18510 if (*varname == '&')
18511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018512 long numval;
18513 char_u *strval;
18514 int error = FALSE;
18515
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018517 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018518 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018519 if (!error && strval != NULL)
18520 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 }
18522 else
18523 {
18524 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18525 if (bufvarname != NULL)
18526 {
18527 STRCPY(bufvarname, "b:");
18528 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018529 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 vim_free(bufvarname);
18531 }
18532 }
18533
18534 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537}
18538
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018540f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018541{
18542 dict_T *d;
18543 dictitem_T *di;
18544 char_u *csearch;
18545
18546 if (argvars[0].v_type != VAR_DICT)
18547 {
18548 EMSG(_(e_dictreq));
18549 return;
18550 }
18551
18552 if ((d = argvars[0].vval.v_dict) != NULL)
18553 {
18554 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18555 if (csearch != NULL)
18556 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018557#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018558 if (enc_utf8)
18559 {
18560 int pcc[MAX_MCO];
18561 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018562
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018563 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18564 }
18565 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018566#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018567 set_last_csearch(PTR2CHAR(csearch),
18568 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018569 }
18570
18571 di = dict_find(d, (char_u *)"forward", -1);
18572 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018573 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018574 ? FORWARD : BACKWARD);
18575
18576 di = dict_find(d, (char_u *)"until", -1);
18577 if (di != NULL)
18578 set_csearch_until(!!get_tv_number(&di->di_tv));
18579 }
18580}
18581
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582/*
18583 * "setcmdpos()" function
18584 */
18585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018586f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018588 int pos = (int)get_tv_number(&argvars[0]) - 1;
18589
18590 if (pos >= 0)
18591 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592}
18593
18594/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018595 * "setfperm({fname}, {mode})" function
18596 */
18597 static void
18598f_setfperm(typval_T *argvars, typval_T *rettv)
18599{
18600 char_u *fname;
18601 char_u modebuf[NUMBUFLEN];
18602 char_u *mode_str;
18603 int i;
18604 int mask;
18605 int mode = 0;
18606
18607 rettv->vval.v_number = 0;
18608 fname = get_tv_string_chk(&argvars[0]);
18609 if (fname == NULL)
18610 return;
18611 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18612 if (mode_str == NULL)
18613 return;
18614 if (STRLEN(mode_str) != 9)
18615 {
18616 EMSG2(_(e_invarg2), mode_str);
18617 return;
18618 }
18619
18620 mask = 1;
18621 for (i = 8; i >= 0; --i)
18622 {
18623 if (mode_str[i] != '-')
18624 mode |= mask;
18625 mask = mask << 1;
18626 }
18627 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18628}
18629
18630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 * "setline()" function
18632 */
18633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018634f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635{
18636 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018637 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018638 list_T *l = NULL;
18639 listitem_T *li = NULL;
18640 long added = 0;
18641 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018643 lnum = get_tv_lnum(&argvars[0]);
18644 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018646 l = argvars[1].vval.v_list;
18647 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018649 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018650 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018651
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018652 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018653 for (;;)
18654 {
18655 if (l != NULL)
18656 {
18657 /* list argument, get next string */
18658 if (li == NULL)
18659 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018660 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018661 li = li->li_next;
18662 }
18663
18664 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018665 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018666 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018667
18668 /* When coming here from Insert mode, sync undo, so that this can be
18669 * undone separately from what was previously inserted. */
18670 if (u_sync_once == 2)
18671 {
18672 u_sync_once = 1; /* notify that u_sync() was called */
18673 u_sync(TRUE);
18674 }
18675
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018676 if (lnum <= curbuf->b_ml.ml_line_count)
18677 {
18678 /* existing line, replace it */
18679 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18680 {
18681 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018682 if (lnum == curwin->w_cursor.lnum)
18683 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018684 rettv->vval.v_number = 0; /* OK */
18685 }
18686 }
18687 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18688 {
18689 /* lnum is one past the last line, append the line */
18690 ++added;
18691 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18692 rettv->vval.v_number = 0; /* OK */
18693 }
18694
18695 if (l == NULL) /* only one string argument */
18696 break;
18697 ++lnum;
18698 }
18699
18700 if (added > 0)
18701 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702}
18703
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018704static 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 +000018705
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018707 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018708 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018710set_qf_ll_list(
18711 win_T *wp UNUSED,
18712 typval_T *list_arg UNUSED,
18713 typval_T *action_arg UNUSED,
18714 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018715{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018716#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018717 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018718 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018719 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018720#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018721
Bram Moolenaar2641f772005-03-25 21:58:17 +000018722 rettv->vval.v_number = -1;
18723
18724#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018725 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018726 EMSG(_(e_listreq));
18727 else
18728 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018729 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018730
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018731 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018732 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018733 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018734 if (act == NULL)
18735 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018736 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018737 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018738 else
18739 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018740 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018741 else if (action_arg->v_type == VAR_UNKNOWN)
18742 action = ' ';
18743 else
18744 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018745
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018746 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018747 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018748 rettv->vval.v_number = 0;
18749 }
18750#endif
18751}
18752
18753/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018754 * "setloclist()" function
18755 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018757f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018758{
18759 win_T *win;
18760
18761 rettv->vval.v_number = -1;
18762
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018763 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018764 if (win != NULL)
18765 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18766}
18767
18768/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018769 * "setmatches()" function
18770 */
18771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018772f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018773{
18774#ifdef FEAT_SEARCH_EXTRA
18775 list_T *l;
18776 listitem_T *li;
18777 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018778 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018779
18780 rettv->vval.v_number = -1;
18781 if (argvars[0].v_type != VAR_LIST)
18782 {
18783 EMSG(_(e_listreq));
18784 return;
18785 }
18786 if ((l = argvars[0].vval.v_list) != NULL)
18787 {
18788
18789 /* To some extent make sure that we are dealing with a list from
18790 * "getmatches()". */
18791 li = l->lv_first;
18792 while (li != NULL)
18793 {
18794 if (li->li_tv.v_type != VAR_DICT
18795 || (d = li->li_tv.vval.v_dict) == NULL)
18796 {
18797 EMSG(_(e_invarg));
18798 return;
18799 }
18800 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018801 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18802 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018803 && dict_find(d, (char_u *)"priority", -1) != NULL
18804 && dict_find(d, (char_u *)"id", -1) != NULL))
18805 {
18806 EMSG(_(e_invarg));
18807 return;
18808 }
18809 li = li->li_next;
18810 }
18811
18812 clear_matches(curwin);
18813 li = l->lv_first;
18814 while (li != NULL)
18815 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018816 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018817 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018818 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018819 char_u *group;
18820 int priority;
18821 int id;
18822 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018823
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018824 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018825 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18826 {
18827 if (s == NULL)
18828 {
18829 s = list_alloc();
18830 if (s == NULL)
18831 return;
18832 }
18833
18834 /* match from matchaddpos() */
18835 for (i = 1; i < 9; i++)
18836 {
18837 sprintf((char *)buf, (char *)"pos%d", i);
18838 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18839 {
18840 if (di->di_tv.v_type != VAR_LIST)
18841 return;
18842
18843 list_append_tv(s, &di->di_tv);
18844 s->lv_refcount++;
18845 }
18846 else
18847 break;
18848 }
18849 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018850
18851 group = get_dict_string(d, (char_u *)"group", FALSE);
18852 priority = (int)get_dict_number(d, (char_u *)"priority");
18853 id = (int)get_dict_number(d, (char_u *)"id");
18854 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18855 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18856 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018857 if (i == 0)
18858 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018859 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018860 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018861 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018862 }
18863 else
18864 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018865 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018866 list_unref(s);
18867 s = NULL;
18868 }
18869
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018870 li = li->li_next;
18871 }
18872 rettv->vval.v_number = 0;
18873 }
18874#endif
18875}
18876
18877/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018878 * "setpos()" function
18879 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018881f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018882{
18883 pos_T pos;
18884 int fnum;
18885 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018886 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018887
Bram Moolenaar08250432008-02-13 11:42:46 +000018888 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018889 name = get_tv_string_chk(argvars);
18890 if (name != NULL)
18891 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018892 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018893 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018894 if (--pos.col < 0)
18895 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018896 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018897 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018898 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018899 if (fnum == curbuf->b_fnum)
18900 {
18901 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018902 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018903 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018904 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018905 curwin->w_set_curswant = FALSE;
18906 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018907 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018908 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018909 }
18910 else
18911 EMSG(_(e_invarg));
18912 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018913 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18914 {
18915 /* set mark */
18916 if (setmark_pos(name[1], &pos, fnum) == OK)
18917 rettv->vval.v_number = 0;
18918 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018919 else
18920 EMSG(_(e_invarg));
18921 }
18922 }
18923}
18924
18925/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018926 * "setqflist()" function
18927 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018929f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018930{
18931 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18932}
18933
18934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 * "setreg()" function
18936 */
18937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018938f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939{
18940 int regname;
18941 char_u *strregname;
18942 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018943 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 int append;
18945 char_u yank_type;
18946 long block_len;
18947
18948 block_len = -1;
18949 yank_type = MAUTO;
18950 append = FALSE;
18951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018952 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018953 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018955 if (strregname == NULL)
18956 return; /* type error; errmsg already given */
18957 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 if (regname == 0 || regname == '@')
18959 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018961 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018963 stropt = get_tv_string_chk(&argvars[2]);
18964 if (stropt == NULL)
18965 return; /* type error */
18966 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 switch (*stropt)
18968 {
18969 case 'a': case 'A': /* append */
18970 append = TRUE;
18971 break;
18972 case 'v': case 'c': /* character-wise selection */
18973 yank_type = MCHAR;
18974 break;
18975 case 'V': case 'l': /* line-wise selection */
18976 yank_type = MLINE;
18977 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 case 'b': case Ctrl_V: /* block-wise selection */
18979 yank_type = MBLOCK;
18980 if (VIM_ISDIGIT(stropt[1]))
18981 {
18982 ++stropt;
18983 block_len = getdigits(&stropt) - 1;
18984 --stropt;
18985 }
18986 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 }
18988 }
18989
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018990 if (argvars[1].v_type == VAR_LIST)
18991 {
18992 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018993 char_u **allocval;
18994 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018995 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018996 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018997 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018998 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018999 int len;
19000
19001 /* If the list is NULL handle like an empty list. */
19002 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019003
Bram Moolenaar7d647822014-04-05 21:28:56 +020019004 /* First half: use for pointers to result lines; second half: use for
19005 * pointers to allocated copies. */
19006 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019007 if (lstval == NULL)
19008 return;
19009 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020019010 allocval = lstval + len + 2;
19011 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019012
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020019013 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019014 li = li->li_next)
19015 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020019016 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019017 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020019018 goto free_lstval;
19019 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019020 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020019021 /* Need to make a copy, next get_tv_string_buf_chk() will
19022 * overwrite the string. */
19023 strval = vim_strsave(buf);
19024 if (strval == NULL)
19025 goto free_lstval;
19026 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019027 }
19028 *curval++ = strval;
19029 }
19030 *curval++ = NULL;
19031
19032 write_reg_contents_lst(regname, lstval, -1,
19033 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020019034free_lstval:
19035 while (curallocval > allocval)
19036 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019037 vim_free(lstval);
19038 }
19039 else
19040 {
19041 strval = get_tv_string_chk(&argvars[1]);
19042 if (strval == NULL)
19043 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019044 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019047 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048}
19049
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019050/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019051 * "settabvar()" function
19052 */
19053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019054f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019055{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019056#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019057 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019058 tabpage_T *tp;
19059#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019060 char_u *varname, *tabvarname;
19061 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019062
19063 rettv->vval.v_number = 0;
19064
19065 if (check_restricted() || check_secure())
19066 return;
19067
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019068#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019069 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019070#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019071 varname = get_tv_string_chk(&argvars[1]);
19072 varp = &argvars[2];
19073
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019074 if (varname != NULL && varp != NULL
19075#ifdef FEAT_WINDOWS
19076 && tp != NULL
19077#endif
19078 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019079 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019080#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019081 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019082 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019083#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019084
19085 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
19086 if (tabvarname != NULL)
19087 {
19088 STRCPY(tabvarname, "t:");
19089 STRCPY(tabvarname + 2, varname);
19090 set_var(tabvarname, varp, TRUE);
19091 vim_free(tabvarname);
19092 }
19093
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019094#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019095 /* Restore current tabpage */
19096 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019097 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019098#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019099 }
19100}
19101
19102/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019103 * "settabwinvar()" function
19104 */
19105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019106f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019107{
19108 setwinvar(argvars, rettv, 1);
19109}
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110
19111/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019112 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019115f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019117 setwinvar(argvars, rettv, 0);
19118}
19119
19120/*
19121 * "setwinvar()" and "settabwinvar()" functions
19122 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020019123
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019125setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019126{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 win_T *win;
19128#ifdef FEAT_WINDOWS
19129 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019130 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020019131 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132#endif
19133 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019134 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019136 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137
19138 if (check_restricted() || check_secure())
19139 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019140
19141#ifdef FEAT_WINDOWS
19142 if (off == 1)
19143 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
19144 else
19145 tp = curtab;
19146#endif
19147 win = find_win_by_nr(&argvars[off], tp);
19148 varname = get_tv_string_chk(&argvars[off + 1]);
19149 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150
19151 if (win != NULL && varname != NULL && varp != NULL)
19152 {
19153#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019154 need_switch_win = !(tp == curtab && win == curwin);
19155 if (!need_switch_win
19156 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019159 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019161 long numval;
19162 char_u *strval;
19163 int error = FALSE;
19164
19165 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019166 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019167 strval = get_tv_string_buf_chk(varp, nbuf);
19168 if (!error && strval != NULL)
19169 set_option_value(varname, numval, strval, OPT_LOCAL);
19170 }
19171 else
19172 {
19173 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19174 if (winvarname != NULL)
19175 {
19176 STRCPY(winvarname, "w:");
19177 STRCPY(winvarname + 2, varname);
19178 set_var(winvarname, varp, TRUE);
19179 vim_free(winvarname);
19180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 }
19182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019184 if (need_switch_win)
19185 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186#endif
19187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188}
19189
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019190#ifdef FEAT_CRYPT
19191/*
19192 * "sha256({string})" function
19193 */
19194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019195f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019196{
19197 char_u *p;
19198
19199 p = get_tv_string(&argvars[0]);
19200 rettv->vval.v_string = vim_strsave(
19201 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19202 rettv->v_type = VAR_STRING;
19203}
19204#endif /* FEAT_CRYPT */
19205
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019207 * "shellescape({string})" function
19208 */
19209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019210f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019211{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019212 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019213 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019214 rettv->v_type = VAR_STRING;
19215}
19216
19217/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019218 * shiftwidth() function
19219 */
19220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019221f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019222{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019223 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019224}
19225
19226/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019227 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 */
19229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019230f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019232 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233
Bram Moolenaar0d660222005-01-07 21:51:51 +000019234 p = get_tv_string(&argvars[0]);
19235 rettv->vval.v_string = vim_strsave(p);
19236 simplify_filename(rettv->vval.v_string); /* simplify in place */
19237 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238}
19239
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019240#ifdef FEAT_FLOAT
19241/*
19242 * "sin()" function
19243 */
19244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019245f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019246{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019247 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019248
19249 rettv->v_type = VAR_FLOAT;
19250 if (get_float_arg(argvars, &f) == OK)
19251 rettv->vval.v_float = sin(f);
19252 else
19253 rettv->vval.v_float = 0.0;
19254}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019255
19256/*
19257 * "sinh()" function
19258 */
19259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019260f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019261{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019262 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019263
19264 rettv->v_type = VAR_FLOAT;
19265 if (get_float_arg(argvars, &f) == OK)
19266 rettv->vval.v_float = sinh(f);
19267 else
19268 rettv->vval.v_float = 0.0;
19269}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019270#endif
19271
Bram Moolenaar0d660222005-01-07 21:51:51 +000019272static int
19273#ifdef __BORLANDC__
19274 _RTLENTRYF
19275#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019276 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019277static int
19278#ifdef __BORLANDC__
19279 _RTLENTRYF
19280#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019281 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019282
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019283/* struct used in the array that's given to qsort() */
19284typedef struct
19285{
19286 listitem_T *item;
19287 int idx;
19288} sortItem_T;
19289
Bram Moolenaar0b962472016-02-22 22:51:33 +010019290/* struct storing information about current sort */
19291typedef struct
19292{
19293 int item_compare_ic;
19294 int item_compare_numeric;
19295 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019296#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019297 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019298#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019299 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019300 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019301 dict_T *item_compare_selfdict;
19302 int item_compare_func_err;
19303 int item_compare_keep_zero;
19304} sortinfo_T;
19305static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019306static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019307#define ITEM_COMPARE_FAIL 999
19308
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019310 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019312 static int
19313#ifdef __BORLANDC__
19314_RTLENTRYF
19315#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019316item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019318 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019319 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019320 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019321 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019322 int res;
19323 char_u numbuf1[NUMBUFLEN];
19324 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019326 si1 = (sortItem_T *)s1;
19327 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019328 tv1 = &si1->item->li_tv;
19329 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019330
Bram Moolenaar0b962472016-02-22 22:51:33 +010019331 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019332 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019333 varnumber_T v1 = get_tv_number(tv1);
19334 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019335
19336 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19337 }
19338
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019339#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019340 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019341 {
19342 float_T v1 = get_tv_float(tv1);
19343 float_T v2 = get_tv_float(tv2);
19344
19345 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19346 }
19347#endif
19348
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019349 /* tv2string() puts quotes around a string and allocates memory. Don't do
19350 * that for string variables. Use a single quote when comparing with a
19351 * non-string to do what the docs promise. */
19352 if (tv1->v_type == VAR_STRING)
19353 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019354 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019355 p1 = (char_u *)"'";
19356 else
19357 p1 = tv1->vval.v_string;
19358 }
19359 else
19360 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19361 if (tv2->v_type == VAR_STRING)
19362 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019363 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019364 p2 = (char_u *)"'";
19365 else
19366 p2 = tv2->vval.v_string;
19367 }
19368 else
19369 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019370 if (p1 == NULL)
19371 p1 = (char_u *)"";
19372 if (p2 == NULL)
19373 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019374 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019375 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019376 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019377 res = STRICMP(p1, p2);
19378 else
19379 res = STRCMP(p1, p2);
19380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019382 {
19383 double n1, n2;
19384 n1 = strtod((char *)p1, (char **)&p1);
19385 n2 = strtod((char *)p2, (char **)&p2);
19386 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19387 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019388
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019389 /* When the result would be zero, compare the item indexes. Makes the
19390 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019391 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019392 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019393
Bram Moolenaar0d660222005-01-07 21:51:51 +000019394 vim_free(tofree1);
19395 vim_free(tofree2);
19396 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397}
19398
19399 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019400#ifdef __BORLANDC__
19401_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019403item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019404{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019405 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019406 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019407 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019408 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019410 char_u *func_name;
19411 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019413 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019414 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019415 return 0;
19416
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019417 si1 = (sortItem_T *)s1;
19418 si2 = (sortItem_T *)s2;
19419
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019420 if (partial == NULL)
19421 func_name = sortinfo->item_compare_func;
19422 else
19423 func_name = partial->pt_name;
19424
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019425 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019426 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019427 copy_tv(&si1->item->li_tv, &argv[0]);
19428 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019429
19430 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019431 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019432 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019433 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019434 clear_tv(&argv[0]);
19435 clear_tv(&argv[1]);
19436
19437 if (res == FAIL)
19438 res = ITEM_COMPARE_FAIL;
19439 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019440 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019441 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019442 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019443 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019444
19445 /* When the result would be zero, compare the pointers themselves. Makes
19446 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019447 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019448 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019449
Bram Moolenaar0d660222005-01-07 21:51:51 +000019450 return res;
19451}
19452
19453/*
19454 * "sort({list})" function
19455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019457do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458{
Bram Moolenaar33570922005-01-25 22:26:29 +000019459 list_T *l;
19460 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019461 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019462 sortinfo_T *old_sortinfo;
19463 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019464 long len;
19465 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466
Bram Moolenaar0b962472016-02-22 22:51:33 +010019467 /* Pointer to current info struct used in compare function. Save and
19468 * restore the current one for nested calls. */
19469 old_sortinfo = sortinfo;
19470 sortinfo = &info;
19471
Bram Moolenaar0d660222005-01-07 21:51:51 +000019472 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019473 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 else
19475 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019476 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019477 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019478 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19479 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019480 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019481 rettv->vval.v_list = l;
19482 rettv->v_type = VAR_LIST;
19483 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484
Bram Moolenaar0d660222005-01-07 21:51:51 +000019485 len = list_len(l);
19486 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019487 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488
Bram Moolenaar0b962472016-02-22 22:51:33 +010019489 info.item_compare_ic = FALSE;
19490 info.item_compare_numeric = FALSE;
19491 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019492#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019493 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019494#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019495 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019496 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019497 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019498 if (argvars[1].v_type != VAR_UNKNOWN)
19499 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019500 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019501 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019502 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019503 else if (argvars[1].v_type == VAR_PARTIAL)
19504 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019505 else
19506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019507 int error = FALSE;
19508
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019509 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019510 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019511 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019512 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019513 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019514 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019515 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019516 else if (i != 0)
19517 {
19518 EMSG(_(e_invarg));
19519 goto theend;
19520 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019521 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019522 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019523 if (*info.item_compare_func == NUL)
19524 {
19525 /* empty string means default sort */
19526 info.item_compare_func = NULL;
19527 }
19528 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019529 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019530 info.item_compare_func = NULL;
19531 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019532 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019533 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019534 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019535 info.item_compare_func = NULL;
19536 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019537 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019538#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019539 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019540 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019541 info.item_compare_func = NULL;
19542 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019543 }
19544#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019545 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019546 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019547 info.item_compare_func = NULL;
19548 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019549 }
19550 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019551 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019552
19553 if (argvars[2].v_type != VAR_UNKNOWN)
19554 {
19555 /* optional third argument: {dict} */
19556 if (argvars[2].v_type != VAR_DICT)
19557 {
19558 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019559 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019560 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019561 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019562 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564
Bram Moolenaar0d660222005-01-07 21:51:51 +000019565 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019566 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019567 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019568 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569
Bram Moolenaar327aa022014-03-25 18:24:23 +010019570 i = 0;
19571 if (sort)
19572 {
19573 /* sort(): ptrs will be the list to sort */
19574 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019575 {
19576 ptrs[i].item = li;
19577 ptrs[i].idx = i;
19578 ++i;
19579 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019580
Bram Moolenaar0b962472016-02-22 22:51:33 +010019581 info.item_compare_func_err = FALSE;
19582 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019583 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019584 if ((info.item_compare_func != NULL
19585 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019586 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019587 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019588 EMSG(_("E702: Sort compare function failed"));
19589 else
19590 {
19591 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019592 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019593 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019594 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019595 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019596
Bram Moolenaar0b962472016-02-22 22:51:33 +010019597 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019598 {
19599 /* Clear the List and append the items in sorted order. */
19600 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19601 l->lv_len = 0;
19602 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019603 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019604 }
19605 }
19606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019608 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019609 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019610
19611 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019612 info.item_compare_func_err = FALSE;
19613 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019614 item_compare_func_ptr = info.item_compare_func != NULL
19615 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019616 ? item_compare2 : item_compare;
19617
19618 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19619 li = li->li_next)
19620 {
19621 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19622 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019623 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019624 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019625 {
19626 EMSG(_("E882: Uniq compare function failed"));
19627 break;
19628 }
19629 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019630
Bram Moolenaar0b962472016-02-22 22:51:33 +010019631 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019632 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019633 while (--i >= 0)
19634 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019635 li = ptrs[i].item->li_next;
19636 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019637 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019638 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019639 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019640 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019641 list_fix_watch(l, li);
19642 listitem_free(li);
19643 l->lv_len--;
19644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019645 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019646 }
19647
19648 vim_free(ptrs);
19649 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019650theend:
19651 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019652}
19653
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019654/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019655 * "sort({list})" function
19656 */
19657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019658f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019659{
19660 do_sort_uniq(argvars, rettv, TRUE);
19661}
19662
19663/*
19664 * "uniq({list})" function
19665 */
19666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019667f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019668{
19669 do_sort_uniq(argvars, rettv, FALSE);
19670}
19671
19672/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019673 * "soundfold({word})" function
19674 */
19675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019676f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019677{
19678 char_u *s;
19679
19680 rettv->v_type = VAR_STRING;
19681 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019682#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019683 rettv->vval.v_string = eval_soundfold(s);
19684#else
19685 rettv->vval.v_string = vim_strsave(s);
19686#endif
19687}
19688
19689/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019690 * "spellbadword()" function
19691 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019693f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019694{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019695 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019696 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019697 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019698
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019699 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019700 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019701
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019702#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019703 if (argvars[0].v_type == VAR_UNKNOWN)
19704 {
19705 /* Find the start and length of the badly spelled word. */
19706 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19707 if (len != 0)
19708 word = ml_get_cursor();
19709 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019710 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019711 {
19712 char_u *str = get_tv_string_chk(&argvars[0]);
19713 int capcol = -1;
19714
19715 if (str != NULL)
19716 {
19717 /* Check the argument for spelling. */
19718 while (*str != NUL)
19719 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019720 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019721 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019722 {
19723 word = str;
19724 break;
19725 }
19726 str += len;
19727 }
19728 }
19729 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019730#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019731
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019732 list_append_string(rettv->vval.v_list, word, len);
19733 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019734 attr == HLF_SPB ? "bad" :
19735 attr == HLF_SPR ? "rare" :
19736 attr == HLF_SPL ? "local" :
19737 attr == HLF_SPC ? "caps" :
19738 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019739}
19740
19741/*
19742 * "spellsuggest()" function
19743 */
19744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019745f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019746{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019747#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019748 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019749 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019750 int maxcount;
19751 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019752 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019753 listitem_T *li;
19754 int need_capital = FALSE;
19755#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019756
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019757 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019758 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019759
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019760#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019761 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019762 {
19763 str = get_tv_string(&argvars[0]);
19764 if (argvars[1].v_type != VAR_UNKNOWN)
19765 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019766 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019767 if (maxcount <= 0)
19768 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019769 if (argvars[2].v_type != VAR_UNKNOWN)
19770 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019771 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019772 if (typeerr)
19773 return;
19774 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019775 }
19776 else
19777 maxcount = 25;
19778
Bram Moolenaar4770d092006-01-12 23:22:24 +000019779 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019780
19781 for (i = 0; i < ga.ga_len; ++i)
19782 {
19783 str = ((char_u **)ga.ga_data)[i];
19784
19785 li = listitem_alloc();
19786 if (li == NULL)
19787 vim_free(str);
19788 else
19789 {
19790 li->li_tv.v_type = VAR_STRING;
19791 li->li_tv.v_lock = 0;
19792 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019793 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019794 }
19795 }
19796 ga_clear(&ga);
19797 }
19798#endif
19799}
19800
Bram Moolenaar0d660222005-01-07 21:51:51 +000019801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019802f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019803{
19804 char_u *str;
19805 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019806 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019807 regmatch_T regmatch;
19808 char_u patbuf[NUMBUFLEN];
19809 char_u *save_cpo;
19810 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019811 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019812 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019813 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019814
19815 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19816 save_cpo = p_cpo;
19817 p_cpo = (char_u *)"";
19818
19819 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019820 if (argvars[1].v_type != VAR_UNKNOWN)
19821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019822 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19823 if (pat == NULL)
19824 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019825 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019826 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019827 }
19828 if (pat == NULL || *pat == NUL)
19829 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019830
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019831 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019833 if (typeerr)
19834 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835
Bram Moolenaar0d660222005-01-07 21:51:51 +000019836 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19837 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019839 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019840 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019841 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019842 if (*str == NUL)
19843 match = FALSE; /* empty item at the end */
19844 else
19845 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019846 if (match)
19847 end = regmatch.startp[0];
19848 else
19849 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019850 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19851 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019852 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019853 if (list_append_string(rettv->vval.v_list, str,
19854 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019855 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019856 }
19857 if (!match)
19858 break;
19859 /* Advance to just after the match. */
19860 if (regmatch.endp[0] > str)
19861 col = 0;
19862 else
19863 {
19864 /* Don't get stuck at the same match. */
19865#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019866 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019867#else
19868 col = 1;
19869#endif
19870 }
19871 str = regmatch.endp[0];
19872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873
Bram Moolenaar473de612013-06-08 18:19:48 +020019874 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876
Bram Moolenaar0d660222005-01-07 21:51:51 +000019877 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878}
19879
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019880#ifdef FEAT_FLOAT
19881/*
19882 * "sqrt()" function
19883 */
19884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019885f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019886{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019887 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019888
19889 rettv->v_type = VAR_FLOAT;
19890 if (get_float_arg(argvars, &f) == OK)
19891 rettv->vval.v_float = sqrt(f);
19892 else
19893 rettv->vval.v_float = 0.0;
19894}
19895
19896/*
19897 * "str2float()" function
19898 */
19899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019900f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019901{
19902 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19903
19904 if (*p == '+')
19905 p = skipwhite(p + 1);
19906 (void)string2float(p, &rettv->vval.v_float);
19907 rettv->v_type = VAR_FLOAT;
19908}
19909#endif
19910
Bram Moolenaar2c932302006-03-18 21:42:09 +000019911/*
19912 * "str2nr()" function
19913 */
19914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019915f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019916{
19917 int base = 10;
19918 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019919 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019920 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019921
19922 if (argvars[1].v_type != VAR_UNKNOWN)
19923 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019924 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019925 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019926 {
19927 EMSG(_(e_invarg));
19928 return;
19929 }
19930 }
19931
19932 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019933 if (*p == '+')
19934 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019935 switch (base)
19936 {
19937 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19938 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19939 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19940 default: what = 0;
19941 }
19942 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019943 rettv->vval.v_number = n;
19944}
19945
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946#ifdef HAVE_STRFTIME
19947/*
19948 * "strftime({format}[, {time}])" function
19949 */
19950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019951f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952{
19953 char_u result_buf[256];
19954 struct tm *curtime;
19955 time_t seconds;
19956 char_u *p;
19957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019958 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019960 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019961 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 seconds = time(NULL);
19963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019964 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 curtime = localtime(&seconds);
19966 /* MSVC returns NULL for an invalid value of seconds. */
19967 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019968 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 else
19970 {
19971# ifdef FEAT_MBYTE
19972 vimconv_T conv;
19973 char_u *enc;
19974
19975 conv.vc_type = CONV_NONE;
19976 enc = enc_locale();
19977 convert_setup(&conv, p_enc, enc);
19978 if (conv.vc_type != CONV_NONE)
19979 p = string_convert(&conv, p, NULL);
19980# endif
19981 if (p != NULL)
19982 (void)strftime((char *)result_buf, sizeof(result_buf),
19983 (char *)p, curtime);
19984 else
19985 result_buf[0] = NUL;
19986
19987# ifdef FEAT_MBYTE
19988 if (conv.vc_type != CONV_NONE)
19989 vim_free(p);
19990 convert_setup(&conv, enc, p_enc);
19991 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019992 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 else
19994# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019995 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996
19997# ifdef FEAT_MBYTE
19998 /* Release conversion descriptors */
19999 convert_setup(&conv, NULL, NULL);
20000 vim_free(enc);
20001# endif
20002 }
20003}
20004#endif
20005
20006/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020007 * "strgetchar()" function
20008 */
20009 static void
20010f_strgetchar(typval_T *argvars, typval_T *rettv)
20011{
20012 char_u *str;
20013 int len;
20014 int error = FALSE;
20015 int charidx;
20016
20017 rettv->vval.v_number = -1;
20018 str = get_tv_string_chk(&argvars[0]);
20019 if (str == NULL)
20020 return;
20021 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020022 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020023 if (error)
20024 return;
20025#ifdef FEAT_MBYTE
20026 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020020027 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020028
20029 while (charidx >= 0 && byteidx < len)
20030 {
20031 if (charidx == 0)
20032 {
20033 rettv->vval.v_number = mb_ptr2char(str + byteidx);
20034 break;
20035 }
20036 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020020037 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020038 }
20039 }
20040#else
20041 if (charidx < len)
20042 rettv->vval.v_number = str[charidx];
20043#endif
20044}
20045
20046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 * "stridx()" function
20048 */
20049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020050f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051{
20052 char_u buf[NUMBUFLEN];
20053 char_u *needle;
20054 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000020057 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020059 needle = get_tv_string_chk(&argvars[1]);
20060 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000020061 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020062 if (needle == NULL || haystack == NULL)
20063 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 if (argvars[2].v_type != VAR_UNKNOWN)
20066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020067 int error = FALSE;
20068
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020069 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020070 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000020071 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020072 if (start_idx >= 0)
20073 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 }
20075
20076 pos = (char_u *)strstr((char *)haystack, (char *)needle);
20077 if (pos != NULL)
20078 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079}
20080
20081/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020082 * "string()" function
20083 */
20084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020085f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020086{
20087 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020088 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020090 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010020091 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
20092 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020093 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020094 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020095 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096}
20097
20098/*
20099 * "strlen()" function
20100 */
20101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020102f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104 rettv->vval.v_number = (varnumber_T)(STRLEN(
20105 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106}
20107
20108/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020109 * "strchars()" function
20110 */
20111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020112f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020113{
20114 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020115 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020020116#ifdef FEAT_MBYTE
20117 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020118 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020020119#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020120
20121 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020122 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020123 if (skipcc < 0 || skipcc > 1)
20124 EMSG(_(e_invarg));
20125 else
20126 {
20127#ifdef FEAT_MBYTE
20128 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
20129 while (*s != NUL)
20130 {
20131 func_mb_ptr2char_adv(&s);
20132 ++len;
20133 }
20134 rettv->vval.v_number = len;
20135#else
20136 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
20137#endif
20138 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020020139}
20140
20141/*
Bram Moolenaardc536092010-07-18 15:45:49 +020020142 * "strdisplaywidth()" function
20143 */
20144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020145f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020020146{
20147 char_u *s = get_tv_string(&argvars[0]);
20148 int col = 0;
20149
20150 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020151 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020020152
Bram Moolenaar8a09b982010-07-22 22:20:57 +020020153 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020020154}
20155
20156/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020157 * "strwidth()" function
20158 */
20159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020160f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020161{
20162 char_u *s = get_tv_string(&argvars[0]);
20163
20164 rettv->vval.v_number = (varnumber_T)(
20165#ifdef FEAT_MBYTE
20166 mb_string2cells(s, -1)
20167#else
20168 STRLEN(s)
20169#endif
20170 );
20171}
20172
20173/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020174 * "strcharpart()" function
20175 */
20176 static void
20177f_strcharpart(typval_T *argvars, typval_T *rettv)
20178{
20179#ifdef FEAT_MBYTE
20180 char_u *p;
20181 int nchar;
20182 int nbyte = 0;
20183 int charlen;
20184 int len = 0;
20185 int slen;
20186 int error = FALSE;
20187
20188 p = get_tv_string(&argvars[0]);
20189 slen = (int)STRLEN(p);
20190
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020191 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020192 if (!error)
20193 {
20194 if (nchar > 0)
20195 while (nchar > 0 && nbyte < slen)
20196 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020197 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020198 --nchar;
20199 }
20200 else
20201 nbyte = nchar;
20202 if (argvars[2].v_type != VAR_UNKNOWN)
20203 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020204 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020205 while (charlen > 0 && nbyte + len < slen)
20206 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020207 int off = nbyte + len;
20208
20209 if (off < 0)
20210 len += 1;
20211 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020212 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020213 --charlen;
20214 }
20215 }
20216 else
20217 len = slen - nbyte; /* default: all bytes that are available. */
20218 }
20219
20220 /*
20221 * Only return the overlap between the specified part and the actual
20222 * string.
20223 */
20224 if (nbyte < 0)
20225 {
20226 len += nbyte;
20227 nbyte = 0;
20228 }
20229 else if (nbyte > slen)
20230 nbyte = slen;
20231 if (len < 0)
20232 len = 0;
20233 else if (nbyte + len > slen)
20234 len = slen - nbyte;
20235
20236 rettv->v_type = VAR_STRING;
20237 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20238#else
20239 f_strpart(argvars, rettv);
20240#endif
20241}
20242
20243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 * "strpart()" function
20245 */
20246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020247f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248{
20249 char_u *p;
20250 int n;
20251 int len;
20252 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020253 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 slen = (int)STRLEN(p);
20257
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020258 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020259 if (error)
20260 len = 0;
20261 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020262 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 else
20264 len = slen - n; /* default len: all bytes that are available. */
20265
20266 /*
20267 * Only return the overlap between the specified part and the actual
20268 * string.
20269 */
20270 if (n < 0)
20271 {
20272 len += n;
20273 n = 0;
20274 }
20275 else if (n > slen)
20276 n = slen;
20277 if (len < 0)
20278 len = 0;
20279 else if (n + len > slen)
20280 len = slen - n;
20281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020282 rettv->v_type = VAR_STRING;
20283 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284}
20285
20286/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020287 * "strridx()" function
20288 */
20289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020290f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020291{
20292 char_u buf[NUMBUFLEN];
20293 char_u *needle;
20294 char_u *haystack;
20295 char_u *rest;
20296 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020297 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020299 needle = get_tv_string_chk(&argvars[1]);
20300 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020301
20302 rettv->vval.v_number = -1;
20303 if (needle == NULL || haystack == NULL)
20304 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020305
20306 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020307 if (argvars[2].v_type != VAR_UNKNOWN)
20308 {
20309 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020310 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020311 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020312 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020313 }
20314 else
20315 end_idx = haystack_len;
20316
Bram Moolenaar0d660222005-01-07 21:51:51 +000020317 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020318 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020319 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020320 lastmatch = haystack + end_idx;
20321 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020322 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020323 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020324 for (rest = haystack; *rest != '\0'; ++rest)
20325 {
20326 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020327 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020328 break;
20329 lastmatch = rest;
20330 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020331 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020332
20333 if (lastmatch == NULL)
20334 rettv->vval.v_number = -1;
20335 else
20336 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20337}
20338
20339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 * "strtrans()" function
20341 */
20342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020343f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020345 rettv->v_type = VAR_STRING;
20346 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347}
20348
20349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020350 * "submatch()" function
20351 */
20352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020353f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020354{
Bram Moolenaar41571762014-04-02 19:00:58 +020020355 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020356 int no;
20357 int retList = 0;
20358
20359 no = (int)get_tv_number_chk(&argvars[0], &error);
20360 if (error)
20361 return;
20362 error = FALSE;
20363 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020364 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020365 if (error)
20366 return;
20367
20368 if (retList == 0)
20369 {
20370 rettv->v_type = VAR_STRING;
20371 rettv->vval.v_string = reg_submatch(no);
20372 }
20373 else
20374 {
20375 rettv->v_type = VAR_LIST;
20376 rettv->vval.v_list = reg_submatch_list(no);
20377 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020378}
20379
20380/*
20381 * "substitute()" function
20382 */
20383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020384f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020385{
20386 char_u patbuf[NUMBUFLEN];
20387 char_u subbuf[NUMBUFLEN];
20388 char_u flagsbuf[NUMBUFLEN];
20389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020390 char_u *str = get_tv_string_chk(&argvars[0]);
20391 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20392 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20393 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20394
Bram Moolenaar0d660222005-01-07 21:51:51 +000020395 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020396 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20397 rettv->vval.v_string = NULL;
20398 else
20399 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020400}
20401
20402/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020403 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020406f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407{
20408 int id = 0;
20409#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020410 linenr_T lnum;
20411 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020413 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020415 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020416 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20417 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020419 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020420 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020421 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422#endif
20423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425}
20426
20427/*
20428 * "synIDattr(id, what [, mode])" function
20429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020431f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432{
20433 char_u *p = NULL;
20434#ifdef FEAT_SYN_HL
20435 int id;
20436 char_u *what;
20437 char_u *mode;
20438 char_u modebuf[NUMBUFLEN];
20439 int modec;
20440
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020441 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020442 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020443 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020445 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020447 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 modec = 0; /* replace invalid with current */
20449 }
20450 else
20451 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020452#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020453 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 modec = 'g';
20455 else
20456#endif
20457 if (t_colors > 1)
20458 modec = 'c';
20459 else
20460 modec = 't';
20461 }
20462
20463
20464 switch (TOLOWER_ASC(what[0]))
20465 {
20466 case 'b':
20467 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20468 p = highlight_color(id, what, modec);
20469 else /* bold */
20470 p = highlight_has_attr(id, HL_BOLD, modec);
20471 break;
20472
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020473 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 p = highlight_color(id, what, modec);
20475 break;
20476
20477 case 'i':
20478 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20479 p = highlight_has_attr(id, HL_INVERSE, modec);
20480 else /* italic */
20481 p = highlight_has_attr(id, HL_ITALIC, modec);
20482 break;
20483
20484 case 'n': /* name */
20485 p = get_highlight_name(NULL, id - 1);
20486 break;
20487
20488 case 'r': /* reverse */
20489 p = highlight_has_attr(id, HL_INVERSE, modec);
20490 break;
20491
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020492 case 's':
20493 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20494 p = highlight_color(id, what, modec);
20495 else /* standout */
20496 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 break;
20498
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020499 case 'u':
20500 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20501 /* underline */
20502 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20503 else
20504 /* undercurl */
20505 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 break;
20507 }
20508
20509 if (p != NULL)
20510 p = vim_strsave(p);
20511#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020512 rettv->v_type = VAR_STRING;
20513 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514}
20515
20516/*
20517 * "synIDtrans(id)" function
20518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020520f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521{
20522 int id;
20523
20524#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020525 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526
20527 if (id > 0)
20528 id = syn_get_final_id(id);
20529 else
20530#endif
20531 id = 0;
20532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020533 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534}
20535
20536/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020537 * "synconcealed(lnum, col)" function
20538 */
20539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020540f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020541{
20542#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020543 linenr_T lnum;
20544 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020545 int syntax_flags = 0;
20546 int cchar;
20547 int matchid = 0;
20548 char_u str[NUMBUFLEN];
20549#endif
20550
20551 rettv->v_type = VAR_LIST;
20552 rettv->vval.v_list = NULL;
20553
20554#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20555 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020556 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020557
20558 vim_memset(str, NUL, sizeof(str));
20559
20560 if (rettv_list_alloc(rettv) != FAIL)
20561 {
20562 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20563 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20564 && curwin->w_p_cole > 0)
20565 {
20566 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20567 syntax_flags = get_syntax_info(&matchid);
20568
20569 /* get the conceal character */
20570 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20571 {
20572 cchar = syn_get_sub_char();
20573 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20574 cchar = lcs_conceal;
20575 if (cchar != NUL)
20576 {
20577# ifdef FEAT_MBYTE
20578 if (has_mbyte)
20579 (*mb_char2bytes)(cchar, str);
20580 else
20581# endif
20582 str[0] = cchar;
20583 }
20584 }
20585 }
20586
20587 list_append_number(rettv->vval.v_list,
20588 (syntax_flags & HL_CONCEAL) != 0);
20589 /* -1 to auto-determine strlen */
20590 list_append_string(rettv->vval.v_list, str, -1);
20591 list_append_number(rettv->vval.v_list, matchid);
20592 }
20593#endif
20594}
20595
20596/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020597 * "synstack(lnum, col)" function
20598 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020600f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020601{
20602#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020603 linenr_T lnum;
20604 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020605 int i;
20606 int id;
20607#endif
20608
20609 rettv->v_type = VAR_LIST;
20610 rettv->vval.v_list = NULL;
20611
20612#ifdef FEAT_SYN_HL
20613 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020614 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020615
20616 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020617 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020618 && rettv_list_alloc(rettv) != FAIL)
20619 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020620 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020621 for (i = 0; ; ++i)
20622 {
20623 id = syn_get_stack_item(i);
20624 if (id < 0)
20625 break;
20626 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20627 break;
20628 }
20629 }
20630#endif
20631}
20632
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020634get_cmd_output_as_rettv(
20635 typval_T *argvars,
20636 typval_T *rettv,
20637 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020639 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020641 char_u *infile = NULL;
20642 char_u buf[NUMBUFLEN];
20643 int err = FALSE;
20644 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020645 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020646 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020648 rettv->v_type = VAR_STRING;
20649 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020650 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020651 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020652
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020653 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020654 {
20655 /*
20656 * Write the string to a temp file, to be used for input of the shell
20657 * command.
20658 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020659 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020660 {
20661 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020662 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020663 }
20664
20665 fd = mch_fopen((char *)infile, WRITEBIN);
20666 if (fd == NULL)
20667 {
20668 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020669 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020670 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020671 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020672 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020673 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20674 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020675 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020676 else
20677 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020678 size_t len;
20679
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020680 p = get_tv_string_buf_chk(&argvars[1], buf);
20681 if (p == NULL)
20682 {
20683 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020684 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020685 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020686 len = STRLEN(p);
20687 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020688 err = TRUE;
20689 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020690 if (fclose(fd) != 0)
20691 err = TRUE;
20692 if (err)
20693 {
20694 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020695 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020696 }
20697 }
20698
Bram Moolenaar52a72462014-08-29 15:53:52 +020020699 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20700 * echoes typeahead, that messes up the display. */
20701 if (!msg_silent)
20702 flags += SHELL_COOKED;
20703
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020704 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020706 int len;
20707 listitem_T *li;
20708 char_u *s = NULL;
20709 char_u *start;
20710 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020711 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712
Bram Moolenaar52a72462014-08-29 15:53:52 +020020713 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020714 if (res == NULL)
20715 goto errret;
20716
20717 list = list_alloc();
20718 if (list == NULL)
20719 goto errret;
20720
20721 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020723 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020724 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020725 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020726 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020727
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020728 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020729 if (s == NULL)
20730 goto errret;
20731
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020732 for (p = s; start < end; ++p, ++start)
20733 *p = *start == NUL ? NL : *start;
20734 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020735
20736 li = listitem_alloc();
20737 if (li == NULL)
20738 {
20739 vim_free(s);
20740 goto errret;
20741 }
20742 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020743 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020744 li->li_tv.vval.v_string = s;
20745 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020747
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020748 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020749 rettv->v_type = VAR_LIST;
20750 rettv->vval.v_list = list;
20751 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020753 else
20754 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020755 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020756#ifdef USE_CR
20757 /* translate <CR> into <NL> */
20758 if (res != NULL)
20759 {
20760 char_u *s;
20761
20762 for (s = res; *s; ++s)
20763 {
20764 if (*s == CAR)
20765 *s = NL;
20766 }
20767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768#else
20769# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020770 /* translate <CR><NL> into <NL> */
20771 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020773 char_u *s, *d;
20774
20775 d = res;
20776 for (s = res; *s; ++s)
20777 {
20778 if (s[0] == CAR && s[1] == NL)
20779 ++s;
20780 *d++ = *s;
20781 }
20782 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784# endif
20785#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020786 rettv->vval.v_string = res;
20787 res = NULL;
20788 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020789
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020790errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020791 if (infile != NULL)
20792 {
20793 mch_remove(infile);
20794 vim_free(infile);
20795 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020796 if (res != NULL)
20797 vim_free(res);
20798 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020799 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020800}
20801
20802/*
20803 * "system()" function
20804 */
20805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020806f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020807{
20808 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20809}
20810
20811/*
20812 * "systemlist()" function
20813 */
20814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020815f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020816{
20817 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818}
20819
20820/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020821 * "tabpagebuflist()" function
20822 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020824f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020825{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020826#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020827 tabpage_T *tp;
20828 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020829
20830 if (argvars[0].v_type == VAR_UNKNOWN)
20831 wp = firstwin;
20832 else
20833 {
20834 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20835 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020836 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020837 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020838 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020839 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020840 for (; wp != NULL; wp = wp->w_next)
20841 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020842 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020843 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020844 }
20845#endif
20846}
20847
20848
20849/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020850 * "tabpagenr()" function
20851 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020853f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020854{
20855 int nr = 1;
20856#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020857 char_u *arg;
20858
20859 if (argvars[0].v_type != VAR_UNKNOWN)
20860 {
20861 arg = get_tv_string_chk(&argvars[0]);
20862 nr = 0;
20863 if (arg != NULL)
20864 {
20865 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020866 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020867 else
20868 EMSG2(_(e_invexpr2), arg);
20869 }
20870 }
20871 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020872 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020873#endif
20874 rettv->vval.v_number = nr;
20875}
20876
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020877
20878#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020879static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020880
20881/*
20882 * Common code for tabpagewinnr() and winnr().
20883 */
20884 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020885get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020886{
20887 win_T *twin;
20888 int nr = 1;
20889 win_T *wp;
20890 char_u *arg;
20891
20892 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20893 if (argvar->v_type != VAR_UNKNOWN)
20894 {
20895 arg = get_tv_string_chk(argvar);
20896 if (arg == NULL)
20897 nr = 0; /* type error; errmsg already given */
20898 else if (STRCMP(arg, "$") == 0)
20899 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20900 else if (STRCMP(arg, "#") == 0)
20901 {
20902 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20903 if (twin == NULL)
20904 nr = 0;
20905 }
20906 else
20907 {
20908 EMSG2(_(e_invexpr2), arg);
20909 nr = 0;
20910 }
20911 }
20912
20913 if (nr > 0)
20914 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20915 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020916 {
20917 if (wp == NULL)
20918 {
20919 /* didn't find it in this tabpage */
20920 nr = 0;
20921 break;
20922 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020923 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020924 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020925 return nr;
20926}
20927#endif
20928
20929/*
20930 * "tabpagewinnr()" function
20931 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020933f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020934{
20935 int nr = 1;
20936#ifdef FEAT_WINDOWS
20937 tabpage_T *tp;
20938
20939 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20940 if (tp == NULL)
20941 nr = 0;
20942 else
20943 nr = get_winnr(tp, &argvars[1]);
20944#endif
20945 rettv->vval.v_number = nr;
20946}
20947
20948
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020949/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020950 * "tagfiles()" function
20951 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020953f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020954{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020955 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020956 tagname_T tn;
20957 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020958
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020959 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020960 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020961 fname = alloc(MAXPATHL);
20962 if (fname == NULL)
20963 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020964
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020965 for (first = TRUE; ; first = FALSE)
20966 if (get_tagfname(&tn, first, fname) == FAIL
20967 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020968 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020969 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020970 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020971}
20972
20973/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020974 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020975 */
20976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020977f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020978{
20979 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020980
20981 tag_pattern = get_tv_string(&argvars[0]);
20982
20983 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020984 if (*tag_pattern == NUL)
20985 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020986
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020987 if (rettv_list_alloc(rettv) == OK)
20988 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020989}
20990
20991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 * "tempname()" function
20993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020995f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996{
20997 static int x = 'A';
20998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020999 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020021000 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001
21002 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
21003 * names. Skip 'I' and 'O', they are used for shell redirection. */
21004 do
21005 {
21006 if (x == 'Z')
21007 x = '0';
21008 else if (x == '9')
21009 x = 'A';
21010 else
21011 {
21012#ifdef EBCDIC
21013 if (x == 'I')
21014 x = 'J';
21015 else if (x == 'R')
21016 x = 'S';
21017 else
21018#endif
21019 ++x;
21020 }
21021 } while (x == 'I' || x == 'O');
21022}
21023
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021024#ifdef FEAT_FLOAT
21025/*
21026 * "tan()" function
21027 */
21028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021030{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021031 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021032
21033 rettv->v_type = VAR_FLOAT;
21034 if (get_float_arg(argvars, &f) == OK)
21035 rettv->vval.v_float = tan(f);
21036 else
21037 rettv->vval.v_float = 0.0;
21038}
21039
21040/*
21041 * "tanh()" function
21042 */
21043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021044f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021045{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021046 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021047
21048 rettv->v_type = VAR_FLOAT;
21049 if (get_float_arg(argvars, &f) == OK)
21050 rettv->vval.v_float = tanh(f);
21051 else
21052 rettv->vval.v_float = 0.0;
21053}
21054#endif
21055
Bram Moolenaar574860b2016-05-24 17:33:34 +020021056/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021057 * "test_alloc_fail(id, countdown, repeat)" function
21058 */
21059 static void
21060f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
21061{
21062 if (argvars[0].v_type != VAR_NUMBER
21063 || argvars[0].vval.v_number <= 0
21064 || argvars[1].v_type != VAR_NUMBER
21065 || argvars[1].vval.v_number < 0
21066 || argvars[2].v_type != VAR_NUMBER)
21067 EMSG(_(e_invarg));
21068 else
21069 {
21070 alloc_fail_id = argvars[0].vval.v_number;
21071 if (alloc_fail_id >= aid_last)
21072 EMSG(_(e_invarg));
21073 alloc_fail_countdown = argvars[1].vval.v_number;
21074 alloc_fail_repeat = argvars[2].vval.v_number;
21075 did_outofmem_msg = FALSE;
21076 }
21077}
21078
21079/*
21080 * "test_disable_char_avail({expr})" function
21081 */
21082 static void
21083f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
21084{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021085 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021086}
21087
21088/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020021089 * "test_garbagecollect_now()" function
21090 */
21091 static void
21092f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
21093{
21094 /* This is dangerous, any Lists and Dicts used internally may be freed
21095 * while still in use. */
21096 garbage_collect(TRUE);
21097}
21098
21099#ifdef FEAT_JOB_CHANNEL
21100 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021101f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021102{
21103 rettv->v_type = VAR_CHANNEL;
21104 rettv->vval.v_channel = NULL;
21105}
21106#endif
21107
21108 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021109f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021110{
21111 rettv->v_type = VAR_DICT;
21112 rettv->vval.v_dict = NULL;
21113}
21114
21115#ifdef FEAT_JOB_CHANNEL
21116 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021117f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021118{
21119 rettv->v_type = VAR_JOB;
21120 rettv->vval.v_job = NULL;
21121}
21122#endif
21123
21124 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021125f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021126{
21127 rettv->v_type = VAR_LIST;
21128 rettv->vval.v_list = NULL;
21129}
21130
21131 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021132f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021133{
21134 rettv->v_type = VAR_PARTIAL;
21135 rettv->vval.v_partial = NULL;
21136}
21137
21138 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021139f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021140{
21141 rettv->v_type = VAR_STRING;
21142 rettv->vval.v_string = NULL;
21143}
21144
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021145 static void
21146f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
21147{
21148 time_for_testing = (time_t)get_tv_number(&argvars[0]);
21149}
21150
Bram Moolenaar975b5272016-03-15 23:10:59 +010021151#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
21152/*
21153 * Get a callback from "arg". It can be a Funcref or a function name.
21154 * When "arg" is zero return an empty string.
21155 * Return NULL for an invalid argument.
21156 */
21157 char_u *
21158get_callback(typval_T *arg, partial_T **pp)
21159{
21160 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
21161 {
21162 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010021163 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021164 return (*pp)->pt_name;
21165 }
21166 *pp = NULL;
21167 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
21168 return arg->vval.v_string;
21169 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
21170 return (char_u *)"";
21171 EMSG(_("E921: Invalid callback argument"));
21172 return NULL;
21173}
21174#endif
21175
21176#ifdef FEAT_TIMERS
21177/*
21178 * "timer_start(time, callback [, options])" function
21179 */
21180 static void
21181f_timer_start(typval_T *argvars, typval_T *rettv)
21182{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021183 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010021184 timer_T *timer;
21185 int repeat = 0;
21186 char_u *callback;
21187 dict_T *dict;
21188
Bram Moolenaar38499922016-04-22 20:46:52 +020021189 if (check_secure())
21190 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021191 if (argvars[2].v_type != VAR_UNKNOWN)
21192 {
21193 if (argvars[2].v_type != VAR_DICT
21194 || (dict = argvars[2].vval.v_dict) == NULL)
21195 {
21196 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21197 return;
21198 }
21199 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21200 repeat = get_dict_number(dict, (char_u *)"repeat");
21201 }
21202
21203 timer = create_timer(msec, repeat);
21204 callback = get_callback(&argvars[1], &timer->tr_partial);
21205 if (callback == NULL)
21206 {
21207 stop_timer(timer);
21208 rettv->vval.v_number = -1;
21209 }
21210 else
21211 {
21212 timer->tr_callback = vim_strsave(callback);
21213 rettv->vval.v_number = timer->tr_id;
21214 }
21215}
21216
21217/*
21218 * "timer_stop(timer)" function
21219 */
21220 static void
21221f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21222{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021223 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021224
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021225 if (argvars[0].v_type != VAR_NUMBER)
21226 {
Bram Moolenaared59aa62016-07-09 17:41:12 +020021227 EMSG(_(e_number_exp));
21228 return;
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021229 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021230 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021231 if (timer != NULL)
21232 stop_timer(timer);
21233}
21234#endif
21235
Bram Moolenaard52d9742005-08-21 22:20:28 +000021236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 * "tolower(string)" function
21238 */
21239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021240f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241{
21242 char_u *p;
21243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021244 p = vim_strsave(get_tv_string(&argvars[0]));
21245 rettv->v_type = VAR_STRING;
21246 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247
21248 if (p != NULL)
21249 while (*p != NUL)
21250 {
21251#ifdef FEAT_MBYTE
21252 int l;
21253
21254 if (enc_utf8)
21255 {
21256 int c, lc;
21257
21258 c = utf_ptr2char(p);
21259 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021260 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 /* TODO: reallocate string when byte count changes. */
21262 if (utf_char2len(lc) == l)
21263 utf_char2bytes(lc, p);
21264 p += l;
21265 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021266 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 p += l; /* skip multi-byte character */
21268 else
21269#endif
21270 {
21271 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21272 ++p;
21273 }
21274 }
21275}
21276
21277/*
21278 * "toupper(string)" function
21279 */
21280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021281f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021284 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285}
21286
21287/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021288 * "tr(string, fromstr, tostr)" function
21289 */
21290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021291f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021292{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021293 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021294 char_u *fromstr;
21295 char_u *tostr;
21296 char_u *p;
21297#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021298 int inlen;
21299 int fromlen;
21300 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021301 int idx;
21302 char_u *cpstr;
21303 int cplen;
21304 int first = TRUE;
21305#endif
21306 char_u buf[NUMBUFLEN];
21307 char_u buf2[NUMBUFLEN];
21308 garray_T ga;
21309
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021310 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021311 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21312 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021313
21314 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021315 rettv->v_type = VAR_STRING;
21316 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021317 if (fromstr == NULL || tostr == NULL)
21318 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021319 ga_init2(&ga, (int)sizeof(char), 80);
21320
21321#ifdef FEAT_MBYTE
21322 if (!has_mbyte)
21323#endif
21324 /* not multi-byte: fromstr and tostr must be the same length */
21325 if (STRLEN(fromstr) != STRLEN(tostr))
21326 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021327#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021328error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021329#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021330 EMSG2(_(e_invarg2), fromstr);
21331 ga_clear(&ga);
21332 return;
21333 }
21334
21335 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021336 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021337 {
21338#ifdef FEAT_MBYTE
21339 if (has_mbyte)
21340 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021341 inlen = (*mb_ptr2len)(in_str);
21342 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021343 cplen = inlen;
21344 idx = 0;
21345 for (p = fromstr; *p != NUL; p += fromlen)
21346 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021347 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021348 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021349 {
21350 for (p = tostr; *p != NUL; p += tolen)
21351 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021352 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021353 if (idx-- == 0)
21354 {
21355 cplen = tolen;
21356 cpstr = p;
21357 break;
21358 }
21359 }
21360 if (*p == NUL) /* tostr is shorter than fromstr */
21361 goto error;
21362 break;
21363 }
21364 ++idx;
21365 }
21366
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021367 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021368 {
21369 /* Check that fromstr and tostr have the same number of
21370 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021371 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021372 first = FALSE;
21373 for (p = tostr; *p != NUL; p += tolen)
21374 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021375 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021376 --idx;
21377 }
21378 if (idx != 0)
21379 goto error;
21380 }
21381
Bram Moolenaarcde88542015-08-11 19:14:00 +020021382 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021383 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021384 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021385
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021386 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021387 }
21388 else
21389#endif
21390 {
21391 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021392 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021393 if (p != NULL)
21394 ga_append(&ga, tostr[p - fromstr]);
21395 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021396 ga_append(&ga, *in_str);
21397 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021398 }
21399 }
21400
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021401 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021402 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021403 ga_append(&ga, NUL);
21404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021405 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021406}
21407
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021408#ifdef FEAT_FLOAT
21409/*
21410 * "trunc({float})" function
21411 */
21412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021413f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021414{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021415 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021416
21417 rettv->v_type = VAR_FLOAT;
21418 if (get_float_arg(argvars, &f) == OK)
21419 /* trunc() is not in C90, use floor() or ceil() instead. */
21420 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21421 else
21422 rettv->vval.v_float = 0.0;
21423}
21424#endif
21425
Bram Moolenaar8299df92004-07-10 09:47:34 +000021426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 * "type(expr)" function
21428 */
21429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021430f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021432 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021433
21434 switch (argvars[0].v_type)
21435 {
21436 case VAR_NUMBER: n = 0; break;
21437 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021438 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021439 case VAR_FUNC: n = 2; break;
21440 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021441 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021442 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021443 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021444 if (argvars[0].vval.v_number == VVAL_FALSE
21445 || argvars[0].vval.v_number == VVAL_TRUE)
21446 n = 6;
21447 else
21448 n = 7;
21449 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021450 case VAR_JOB: n = 8; break;
21451 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021452 case VAR_UNKNOWN:
21453 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21454 n = -1;
21455 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021456 }
21457 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458}
21459
21460/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021461 * "undofile(name)" function
21462 */
21463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021464f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021465{
21466 rettv->v_type = VAR_STRING;
21467#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021468 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021469 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021470
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021471 if (*fname == NUL)
21472 {
21473 /* If there is no file name there will be no undo file. */
21474 rettv->vval.v_string = NULL;
21475 }
21476 else
21477 {
21478 char_u *ffname = FullName_save(fname, FALSE);
21479
21480 if (ffname != NULL)
21481 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21482 vim_free(ffname);
21483 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021484 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021485#else
21486 rettv->vval.v_string = NULL;
21487#endif
21488}
21489
21490/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021491 * "undotree()" function
21492 */
21493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021494f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021495{
21496 if (rettv_dict_alloc(rettv) == OK)
21497 {
21498 dict_T *dict = rettv->vval.v_dict;
21499 list_T *list;
21500
Bram Moolenaar730cde92010-06-27 05:18:54 +020021501 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021502 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021503 dict_add_nr_str(dict, "save_last",
21504 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021505 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21506 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021507 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021508
21509 list = list_alloc();
21510 if (list != NULL)
21511 {
21512 u_eval_tree(curbuf->b_u_oldhead, list);
21513 dict_add_list(dict, "entries", list);
21514 }
21515 }
21516}
21517
21518/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021519 * "values(dict)" function
21520 */
21521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021522f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021523{
21524 dict_list(argvars, rettv, 1);
21525}
21526
21527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 * "virtcol(string)" function
21529 */
21530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021531f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532{
21533 colnr_T vcol = 0;
21534 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021535 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021537 fp = var2fpos(&argvars[0], FALSE, &fnum);
21538 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21539 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540 {
21541 getvvcol(curwin, fp, NULL, NULL, &vcol);
21542 ++vcol;
21543 }
21544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021545 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546}
21547
21548/*
21549 * "visualmode()" function
21550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021552f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554 char_u str[2];
21555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021556 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 str[0] = curbuf->b_visual_mode_eval;
21558 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021559 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560
21561 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021562 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564}
21565
21566/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021567 * "wildmenumode()" function
21568 */
21569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021570f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021571{
21572#ifdef FEAT_WILDMENU
21573 if (wild_menu_showing)
21574 rettv->vval.v_number = 1;
21575#endif
21576}
21577
21578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 * "winbufnr(nr)" function
21580 */
21581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021582f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583{
21584 win_T *wp;
21585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021586 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021588 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021590 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591}
21592
21593/*
21594 * "wincol()" function
21595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021597f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598{
21599 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021600 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601}
21602
21603/*
21604 * "winheight(nr)" function
21605 */
21606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021607f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608{
21609 win_T *wp;
21610
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021611 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021615 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616}
21617
21618/*
21619 * "winline()" function
21620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021622f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623{
21624 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626}
21627
21628/*
21629 * "winnr()" function
21630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021632f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633{
21634 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021635
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021637 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021639 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640}
21641
21642/*
21643 * "winrestcmd()" function
21644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021646f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647{
21648#ifdef FEAT_WINDOWS
21649 win_T *wp;
21650 int winnr = 1;
21651 garray_T ga;
21652 char_u buf[50];
21653
21654 ga_init2(&ga, (int)sizeof(char), 70);
21655 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21656 {
21657 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21658 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21660 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 ++winnr;
21662 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021663 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021665 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021667 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670}
21671
21672/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021673 * "winrestview()" function
21674 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021676f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021677{
21678 dict_T *dict;
21679
21680 if (argvars[0].v_type != VAR_DICT
21681 || (dict = argvars[0].vval.v_dict) == NULL)
21682 EMSG(_(e_invarg));
21683 else
21684 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021685 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021686 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021687 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021688 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021689#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021690 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021691 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021692#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021693 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21694 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021695 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021696 curwin->w_set_curswant = FALSE;
21697 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021698
Bram Moolenaar82c25852014-05-28 16:47:16 +020021699 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021700 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021701#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021702 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021703 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021704#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021705 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021706 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021707 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021708 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021709
21710 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021711 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021712# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021713 win_new_width(curwin, W_WIDTH(curwin));
21714# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021715 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021716
Bram Moolenaarb851a962014-10-31 15:45:52 +010021717 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021718 curwin->w_topline = 1;
21719 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21720 curwin->w_topline = curbuf->b_ml.ml_line_count;
21721#ifdef FEAT_DIFF
21722 check_topfill(curwin, TRUE);
21723#endif
21724 }
21725}
21726
21727/*
21728 * "winsaveview()" function
21729 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021731f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021732{
21733 dict_T *dict;
21734
Bram Moolenaara800b422010-06-27 01:15:55 +020021735 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021736 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021737 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021738
21739 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21740 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21741#ifdef FEAT_VIRTUALEDIT
21742 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21743#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021744 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021745 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21746
21747 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21748#ifdef FEAT_DIFF
21749 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21750#endif
21751 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21752 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21753}
21754
21755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 * "winwidth(nr)" function
21757 */
21758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021759f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760{
21761 win_T *wp;
21762
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021763 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021765 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021767#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021768 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021770 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771#endif
21772}
21773
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021775 * "wordcount()" function
21776 */
21777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021778f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021779{
21780 if (rettv_dict_alloc(rettv) == FAIL)
21781 return;
21782 cursor_pos_info(rettv->vval.v_dict);
21783}
21784
21785/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021786 * Write list of strings to file
21787 */
21788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021789write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021790{
21791 listitem_T *li;
21792 int c;
21793 int ret = OK;
21794 char_u *s;
21795
21796 for (li = list->lv_first; li != NULL; li = li->li_next)
21797 {
21798 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21799 {
21800 if (*s == '\n')
21801 c = putc(NUL, fd);
21802 else
21803 c = putc(*s, fd);
21804 if (c == EOF)
21805 {
21806 ret = FAIL;
21807 break;
21808 }
21809 }
21810 if (!binary || li->li_next != NULL)
21811 if (putc('\n', fd) == EOF)
21812 {
21813 ret = FAIL;
21814 break;
21815 }
21816 if (ret == FAIL)
21817 {
21818 EMSG(_(e_write));
21819 break;
21820 }
21821 }
21822 return ret;
21823}
21824
21825/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021826 * "writefile()" function
21827 */
21828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021829f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021830{
21831 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021832 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021833 char_u *fname;
21834 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021835 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021836
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021837 if (check_restricted() || check_secure())
21838 return;
21839
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021840 if (argvars[0].v_type != VAR_LIST)
21841 {
21842 EMSG2(_(e_listarg), "writefile()");
21843 return;
21844 }
21845 if (argvars[0].vval.v_list == NULL)
21846 return;
21847
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021848 if (argvars[2].v_type != VAR_UNKNOWN)
21849 {
21850 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21851 binary = TRUE;
21852 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21853 append = TRUE;
21854 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021855
21856 /* Always open the file in binary mode, library functions have a mind of
21857 * their own about CR-LF conversion. */
21858 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021859 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21860 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021861 {
21862 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21863 ret = -1;
21864 }
21865 else
21866 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021867 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21868 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021869 fclose(fd);
21870 }
21871
21872 rettv->vval.v_number = ret;
21873}
21874
21875/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021876 * "xor(expr, expr)" function
21877 */
21878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021879f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021880{
21881 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21882 ^ get_tv_number_chk(&argvars[1], NULL);
21883}
21884
21885
21886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021888 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 */
21890 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021891var2fpos(
21892 typval_T *varp,
21893 int dollar_lnum, /* TRUE when $ is last line */
21894 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021896 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021898 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899
Bram Moolenaara5525202006-03-02 22:52:09 +000021900 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021901 if (varp->v_type == VAR_LIST)
21902 {
21903 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021904 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021905 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021906 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021907
21908 l = varp->vval.v_list;
21909 if (l == NULL)
21910 return NULL;
21911
21912 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021913 pos.lnum = list_find_nr(l, 0L, &error);
21914 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021915 return NULL; /* invalid line number */
21916
21917 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021918 pos.col = list_find_nr(l, 1L, &error);
21919 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021920 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021921 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021922
21923 /* We accept "$" for the column number: last column. */
21924 li = list_find(l, 1L);
21925 if (li != NULL && li->li_tv.v_type == VAR_STRING
21926 && li->li_tv.vval.v_string != NULL
21927 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21928 pos.col = len + 1;
21929
Bram Moolenaara5525202006-03-02 22:52:09 +000021930 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021931 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021932 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021933 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021934
Bram Moolenaara5525202006-03-02 22:52:09 +000021935#ifdef FEAT_VIRTUALEDIT
21936 /* Get the virtual offset. Defaults to zero. */
21937 pos.coladd = list_find_nr(l, 2L, &error);
21938 if (error)
21939 pos.coladd = 0;
21940#endif
21941
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021942 return &pos;
21943 }
21944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021945 name = get_tv_string_chk(varp);
21946 if (name == NULL)
21947 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021948 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021950 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21951 {
21952 if (VIsual_active)
21953 return &VIsual;
21954 return &curwin->w_cursor;
21955 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021956 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021958 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21960 return NULL;
21961 return pp;
21962 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021963
21964#ifdef FEAT_VIRTUALEDIT
21965 pos.coladd = 0;
21966#endif
21967
Bram Moolenaar477933c2007-07-17 14:32:23 +000021968 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021969 {
21970 pos.col = 0;
21971 if (name[1] == '0') /* "w0": first visible line */
21972 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021973 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021974 pos.lnum = curwin->w_topline;
21975 return &pos;
21976 }
21977 else if (name[1] == '$') /* "w$": last visible line */
21978 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021979 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021980 pos.lnum = curwin->w_botline - 1;
21981 return &pos;
21982 }
21983 }
21984 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021986 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 {
21988 pos.lnum = curbuf->b_ml.ml_line_count;
21989 pos.col = 0;
21990 }
21991 else
21992 {
21993 pos.lnum = curwin->w_cursor.lnum;
21994 pos.col = (colnr_T)STRLEN(ml_get_curline());
21995 }
21996 return &pos;
21997 }
21998 return NULL;
21999}
22000
22001/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022002 * Convert list in "arg" into a position and optional file number.
22003 * When "fnump" is NULL there is no file number, only 3 items.
22004 * Note that the column is passed on as-is, the caller may want to decrement
22005 * it to use 1 for the first column.
22006 * Return FAIL when conversion is not possible, doesn't check the position for
22007 * validity.
22008 */
22009 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022010list2fpos(
22011 typval_T *arg,
22012 pos_T *posp,
22013 int *fnump,
22014 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022015{
22016 list_T *l = arg->vval.v_list;
22017 long i = 0;
22018 long n;
22019
Bram Moolenaar493c1782014-05-28 14:34:46 +020022020 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
22021 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000022022 if (arg->v_type != VAR_LIST
22023 || l == NULL
22024 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020022025 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022026 return FAIL;
22027
22028 if (fnump != NULL)
22029 {
22030 n = list_find_nr(l, i++, NULL); /* fnum */
22031 if (n < 0)
22032 return FAIL;
22033 if (n == 0)
22034 n = curbuf->b_fnum; /* current buffer */
22035 *fnump = n;
22036 }
22037
22038 n = list_find_nr(l, i++, NULL); /* lnum */
22039 if (n < 0)
22040 return FAIL;
22041 posp->lnum = n;
22042
22043 n = list_find_nr(l, i++, NULL); /* col */
22044 if (n < 0)
22045 return FAIL;
22046 posp->col = n;
22047
22048#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020022049 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022050 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000022051 posp->coladd = 0;
22052 else
22053 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022054#endif
22055
Bram Moolenaar493c1782014-05-28 14:34:46 +020022056 if (curswantp != NULL)
22057 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
22058
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022059 return OK;
22060}
22061
22062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 * Get the length of an environment variable name.
22064 * Advance "arg" to the first character after the name.
22065 * Return 0 for error.
22066 */
22067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022068get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069{
22070 char_u *p;
22071 int len;
22072
22073 for (p = *arg; vim_isIDc(*p); ++p)
22074 ;
22075 if (p == *arg) /* no name found */
22076 return 0;
22077
22078 len = (int)(p - *arg);
22079 *arg = p;
22080 return len;
22081}
22082
22083/*
22084 * Get the length of the name of a function or internal variable.
22085 * "arg" is advanced to the first non-white character after the name.
22086 * Return 0 if something is wrong.
22087 */
22088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022089get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090{
22091 char_u *p;
22092 int len;
22093
22094 /* Find the end of the name. */
22095 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022096 {
22097 if (*p == ':')
22098 {
22099 /* "s:" is start of "s:var", but "n:" is not and can be used in
22100 * slice "[n:]". Also "xx:" is not a namespace. */
22101 len = (int)(p - *arg);
22102 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
22103 || len > 1)
22104 break;
22105 }
22106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 if (p == *arg) /* no name found */
22108 return 0;
22109
22110 len = (int)(p - *arg);
22111 *arg = skipwhite(p);
22112
22113 return len;
22114}
22115
22116/*
Bram Moolenaara7043832005-01-21 11:56:39 +000022117 * Get the length of the name of a variable or function.
22118 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022120 * Return -1 if curly braces expansion failed.
22121 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 * If the name contains 'magic' {}'s, expand them and return the
22123 * expanded name in an allocated string via 'alias' - caller must free.
22124 */
22125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022126get_name_len(
22127 char_u **arg,
22128 char_u **alias,
22129 int evaluate,
22130 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131{
22132 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133 char_u *p;
22134 char_u *expr_start;
22135 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136
22137 *alias = NULL; /* default to no alias */
22138
22139 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
22140 && (*arg)[2] == (int)KE_SNR)
22141 {
22142 /* hard coded <SNR>, already translated */
22143 *arg += 3;
22144 return get_id_len(arg) + 3;
22145 }
22146 len = eval_fname_script(*arg);
22147 if (len > 0)
22148 {
22149 /* literal "<SID>", "s:" or "<SNR>" */
22150 *arg += len;
22151 }
22152
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022154 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022156 p = find_name_end(*arg, &expr_start, &expr_end,
22157 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 if (expr_start != NULL)
22159 {
22160 char_u *temp_string;
22161
22162 if (!evaluate)
22163 {
22164 len += (int)(p - *arg);
22165 *arg = skipwhite(p);
22166 return len;
22167 }
22168
22169 /*
22170 * Include any <SID> etc in the expanded string:
22171 * Thus the -len here.
22172 */
22173 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
22174 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022175 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176 *alias = temp_string;
22177 *arg = skipwhite(p);
22178 return (int)STRLEN(temp_string);
22179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180
22181 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022182 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 EMSG2(_(e_invexpr2), *arg);
22184
22185 return len;
22186}
22187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022188/*
22189 * Find the end of a variable or function name, taking care of magic braces.
22190 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
22191 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022192 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022193 * Return a pointer to just after the name. Equal to "arg" if there is no
22194 * valid name.
22195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022197find_name_end(
22198 char_u *arg,
22199 char_u **expr_start,
22200 char_u **expr_end,
22201 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022203 int mb_nest = 0;
22204 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022206 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022208 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022210 *expr_start = NULL;
22211 *expr_end = NULL;
22212 }
22213
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022214 /* Quick check for valid starting character. */
22215 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22216 return arg;
22217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022218 for (p = arg; *p != NUL
22219 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022220 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022221 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022222 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022223 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022224 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022225 if (*p == '\'')
22226 {
22227 /* skip over 'string' to avoid counting [ and ] inside it. */
22228 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22229 ;
22230 if (*p == NUL)
22231 break;
22232 }
22233 else if (*p == '"')
22234 {
22235 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22236 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22237 if (*p == '\\' && p[1] != NUL)
22238 ++p;
22239 if (*p == NUL)
22240 break;
22241 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022242 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22243 {
22244 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022245 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022246 len = (int)(p - arg);
22247 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022248 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022249 break;
22250 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022252 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022254 if (*p == '[')
22255 ++br_nest;
22256 else if (*p == ']')
22257 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022260 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022262 if (*p == '{')
22263 {
22264 mb_nest++;
22265 if (expr_start != NULL && *expr_start == NULL)
22266 *expr_start = p;
22267 }
22268 else if (*p == '}')
22269 {
22270 mb_nest--;
22271 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22272 *expr_end = p;
22273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 }
22276
22277 return p;
22278}
22279
22280/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022281 * Expands out the 'magic' {}'s in a variable/function name.
22282 * Note that this can call itself recursively, to deal with
22283 * constructs like foo{bar}{baz}{bam}
22284 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22285 * "in_start" ^
22286 * "expr_start" ^
22287 * "expr_end" ^
22288 * "in_end" ^
22289 *
22290 * Returns a new allocated string, which the caller must free.
22291 * Returns NULL for failure.
22292 */
22293 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022294make_expanded_name(
22295 char_u *in_start,
22296 char_u *expr_start,
22297 char_u *expr_end,
22298 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022299{
22300 char_u c1;
22301 char_u *retval = NULL;
22302 char_u *temp_result;
22303 char_u *nextcmd = NULL;
22304
22305 if (expr_end == NULL || in_end == NULL)
22306 return NULL;
22307 *expr_start = NUL;
22308 *expr_end = NUL;
22309 c1 = *in_end;
22310 *in_end = NUL;
22311
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022312 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022313 if (temp_result != NULL && nextcmd == NULL)
22314 {
22315 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22316 + (in_end - expr_end) + 1));
22317 if (retval != NULL)
22318 {
22319 STRCPY(retval, in_start);
22320 STRCAT(retval, temp_result);
22321 STRCAT(retval, expr_end + 1);
22322 }
22323 }
22324 vim_free(temp_result);
22325
22326 *in_end = c1; /* put char back for error messages */
22327 *expr_start = '{';
22328 *expr_end = '}';
22329
22330 if (retval != NULL)
22331 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022332 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022333 if (expr_start != NULL)
22334 {
22335 /* Further expansion! */
22336 temp_result = make_expanded_name(retval, expr_start,
22337 expr_end, temp_result);
22338 vim_free(retval);
22339 retval = temp_result;
22340 }
22341 }
22342
22343 return retval;
22344}
22345
22346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022348 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 */
22350 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022351eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022353 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22354}
22355
22356/*
22357 * Return TRUE if character "c" can be used as the first character in a
22358 * variable or function name (excluding '{' and '}').
22359 */
22360 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022361eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022362{
22363 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364}
22365
22366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 * Set number v: variable to "val".
22368 */
22369 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022370set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022372 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373}
22374
22375/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022376 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022378 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022379get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022381 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382}
22383
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022384/*
22385 * Get string v: variable value. Uses a static buffer, can only be used once.
22386 */
22387 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022388get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022389{
22390 return get_tv_string(&vimvars[idx].vv_tv);
22391}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022392
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022394 * Get List v: variable value. Caller must take care of reference count when
22395 * needed.
22396 */
22397 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022398get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022399{
22400 return vimvars[idx].vv_list;
22401}
22402
22403/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022404 * Set v:char to character "c".
22405 */
22406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022407set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022408{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022409 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022410
22411#ifdef FEAT_MBYTE
22412 if (has_mbyte)
22413 buf[(*mb_char2bytes)(c, buf)] = NUL;
22414 else
22415#endif
22416 {
22417 buf[0] = c;
22418 buf[1] = NUL;
22419 }
22420 set_vim_var_string(VV_CHAR, buf, -1);
22421}
22422
22423/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022424 * Set v:count to "count" and v:count1 to "count1".
22425 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 */
22427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022428set_vcount(
22429 long count,
22430 long count1,
22431 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022433 if (set_prevcount)
22434 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022435 vimvars[VV_COUNT].vv_nr = count;
22436 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437}
22438
22439/*
22440 * Set string v: variable to a copy of "val".
22441 */
22442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022443set_vim_var_string(
22444 int idx,
22445 char_u *val,
22446 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447{
Bram Moolenaara542c682016-01-31 16:28:04 +010022448 clear_tv(&vimvars[idx].vv_di.di_tv);
22449 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022451 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022453 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022455 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456}
22457
22458/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022459 * Set List v: variable to "val".
22460 */
22461 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022462set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022463{
Bram Moolenaara542c682016-01-31 16:28:04 +010022464 clear_tv(&vimvars[idx].vv_di.di_tv);
22465 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022466 vimvars[idx].vv_list = val;
22467 if (val != NULL)
22468 ++val->lv_refcount;
22469}
22470
22471/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022472 * Set Dictionary v: variable to "val".
22473 */
22474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022475set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022476{
22477 int todo;
22478 hashitem_T *hi;
22479
Bram Moolenaara542c682016-01-31 16:28:04 +010022480 clear_tv(&vimvars[idx].vv_di.di_tv);
22481 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022482 vimvars[idx].vv_dict = val;
22483 if (val != NULL)
22484 {
22485 ++val->dv_refcount;
22486
22487 /* Set readonly */
22488 todo = (int)val->dv_hashtab.ht_used;
22489 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22490 {
22491 if (HASHITEM_EMPTY(hi))
22492 continue;
22493 --todo;
22494 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22495 }
22496 }
22497}
22498
22499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 * Set v:register if needed.
22501 */
22502 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022503set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504{
22505 char_u regname;
22506
22507 if (c == 0 || c == ' ')
22508 regname = '"';
22509 else
22510 regname = c;
22511 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022512 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 set_vim_var_string(VV_REG, &regname, 1);
22514}
22515
22516/*
22517 * Get or set v:exception. If "oldval" == NULL, return the current value.
22518 * Otherwise, restore the value to "oldval" and return NULL.
22519 * Must always be called in pairs to save and restore v:exception! Does not
22520 * take care of memory allocations.
22521 */
22522 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022523v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524{
22525 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022526 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527
Bram Moolenaare9a41262005-01-15 22:18:47 +000022528 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 return NULL;
22530}
22531
22532/*
22533 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22534 * Otherwise, restore the value to "oldval" and return NULL.
22535 * Must always be called in pairs to save and restore v:throwpoint! Does not
22536 * take care of memory allocations.
22537 */
22538 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022539v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540{
22541 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022542 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543
Bram Moolenaare9a41262005-01-15 22:18:47 +000022544 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 return NULL;
22546}
22547
22548#if defined(FEAT_AUTOCMD) || defined(PROTO)
22549/*
22550 * Set v:cmdarg.
22551 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22552 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22553 * Must always be called in pairs!
22554 */
22555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022556set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557{
22558 char_u *oldval;
22559 char_u *newval;
22560 unsigned len;
22561
Bram Moolenaare9a41262005-01-15 22:18:47 +000022562 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022563 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022565 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022566 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022567 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 }
22569
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022570 if (eap->force_bin == FORCE_BIN)
22571 len = 6;
22572 else if (eap->force_bin == FORCE_NOBIN)
22573 len = 8;
22574 else
22575 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022576
22577 if (eap->read_edit)
22578 len += 7;
22579
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022580 if (eap->force_ff != 0)
22581 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22582# ifdef FEAT_MBYTE
22583 if (eap->force_enc != 0)
22584 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022585 if (eap->bad_char != 0)
22586 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022587# endif
22588
22589 newval = alloc(len + 1);
22590 if (newval == NULL)
22591 return NULL;
22592
22593 if (eap->force_bin == FORCE_BIN)
22594 sprintf((char *)newval, " ++bin");
22595 else if (eap->force_bin == FORCE_NOBIN)
22596 sprintf((char *)newval, " ++nobin");
22597 else
22598 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022599
22600 if (eap->read_edit)
22601 STRCAT(newval, " ++edit");
22602
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022603 if (eap->force_ff != 0)
22604 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22605 eap->cmd + eap->force_ff);
22606# ifdef FEAT_MBYTE
22607 if (eap->force_enc != 0)
22608 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22609 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022610 if (eap->bad_char == BAD_KEEP)
22611 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22612 else if (eap->bad_char == BAD_DROP)
22613 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22614 else if (eap->bad_char != 0)
22615 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022616# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022617 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022618 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619}
22620#endif
22621
22622/*
22623 * Get the value of internal variable "name".
22624 * Return OK or FAIL.
22625 */
22626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022627get_var_tv(
22628 char_u *name,
22629 int len, /* length of "name" */
22630 typval_T *rettv, /* NULL when only checking existence */
22631 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22632 int verbose, /* may give error message */
22633 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634{
22635 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022636 typval_T *tv = NULL;
22637 typval_T atv;
22638 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640
22641 /* truncate the name, so that we can use strcmp() */
22642 cc = name[len];
22643 name[len] = NUL;
22644
22645 /*
22646 * Check for "b:changedtick".
22647 */
22648 if (STRCMP(name, "b:changedtick") == 0)
22649 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022650 atv.v_type = VAR_NUMBER;
22651 atv.vval.v_number = curbuf->b_changedtick;
22652 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 }
22654
22655 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 * Check for user-defined variables.
22657 */
22658 else
22659 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022660 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022662 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022663 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022664 if (dip != NULL)
22665 *dip = v;
22666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 }
22668
Bram Moolenaare9a41262005-01-15 22:18:47 +000022669 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022671 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022672 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 ret = FAIL;
22674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022675 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022676 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677
22678 name[len] = cc;
22679
22680 return ret;
22681}
22682
22683/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022684 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22685 * Also handle function call with Funcref variable: func(expr)
22686 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22687 */
22688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022689handle_subscript(
22690 char_u **arg,
22691 typval_T *rettv,
22692 int evaluate, /* do more than finding the end */
22693 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022694{
22695 int ret = OK;
22696 dict_T *selfdict = NULL;
22697 char_u *s;
22698 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022699 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022700
22701 while (ret == OK
22702 && (**arg == '['
22703 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022704 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22705 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022706 && !vim_iswhite(*(*arg - 1)))
22707 {
22708 if (**arg == '(')
22709 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022710 partial_T *pt = NULL;
22711
Bram Moolenaard9fba312005-06-26 22:34:35 +000022712 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022713 if (evaluate)
22714 {
22715 functv = *rettv;
22716 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022717
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022718 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022719 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022720 {
22721 pt = functv.vval.v_partial;
22722 s = pt->pt_name;
22723 }
22724 else
22725 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022726 }
22727 else
22728 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022729 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022730 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022731 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022732
22733 /* Clear the funcref afterwards, so that deleting it while
22734 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022735 if (evaluate)
22736 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022737
22738 /* Stop the expression evaluation when immediately aborting on
22739 * error, or when an interrupt occurred or an exception was thrown
22740 * but not caught. */
22741 if (aborting())
22742 {
22743 if (ret == OK)
22744 clear_tv(rettv);
22745 ret = FAIL;
22746 }
22747 dict_unref(selfdict);
22748 selfdict = NULL;
22749 }
22750 else /* **arg == '[' || **arg == '.' */
22751 {
22752 dict_unref(selfdict);
22753 if (rettv->v_type == VAR_DICT)
22754 {
22755 selfdict = rettv->vval.v_dict;
22756 if (selfdict != NULL)
22757 ++selfdict->dv_refcount;
22758 }
22759 else
22760 selfdict = NULL;
22761 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22762 {
22763 clear_tv(rettv);
22764 ret = FAIL;
22765 }
22766 }
22767 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022768
Bram Moolenaar1d429612016-05-24 15:44:17 +020022769 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22770 * Don't do this when "Func" is already a partial that was bound
22771 * explicitly (pt_auto is FALSE). */
22772 if (selfdict != NULL
22773 && (rettv->v_type == VAR_FUNC
22774 || (rettv->v_type == VAR_PARTIAL
22775 && (rettv->vval.v_partial->pt_auto
22776 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022777 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022778 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22779 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022780 char_u *tofree = NULL;
22781 ufunc_T *fp;
22782 char_u fname_buf[FLEN_FIXED + 1];
22783 int error;
22784
22785 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022786 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022787 fp = find_func(fname);
22788 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022789
Bram Moolenaar65639032016-03-16 21:40:30 +010022790 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022791 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022792 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22793
22794 if (pt != NULL)
22795 {
22796 pt->pt_refcount = 1;
22797 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022798 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022799 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022800 if (rettv->v_type == VAR_FUNC)
22801 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022802 /* Just a function: Take over the function name and use
22803 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022804 pt->pt_name = rettv->vval.v_string;
22805 }
22806 else
22807 {
22808 partial_T *ret_pt = rettv->vval.v_partial;
22809 int i;
22810
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022811 /* Partial: copy the function name, use selfdict and copy
22812 * args. Can't take over name or args, the partial might
22813 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022814 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022815 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022816 if (ret_pt->pt_argc > 0)
22817 {
22818 pt->pt_argv = (typval_T *)alloc(
22819 sizeof(typval_T) * ret_pt->pt_argc);
22820 if (pt->pt_argv == NULL)
22821 /* out of memory: drop the arguments */
22822 pt->pt_argc = 0;
22823 else
22824 {
22825 pt->pt_argc = ret_pt->pt_argc;
22826 for (i = 0; i < pt->pt_argc; i++)
22827 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22828 }
22829 }
22830 partial_unref(ret_pt);
22831 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022832 rettv->v_type = VAR_PARTIAL;
22833 rettv->vval.v_partial = pt;
22834 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022835 }
22836 }
22837
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022838 dict_unref(selfdict);
22839 return ret;
22840}
22841
22842/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022843 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022844 * value).
22845 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022846 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022847alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022848{
Bram Moolenaar33570922005-01-25 22:26:29 +000022849 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022850}
22851
22852/*
22853 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 * The string "s" must have been allocated, it is consumed.
22855 * Return NULL for out of memory, the variable otherwise.
22856 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022857 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022858alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859{
Bram Moolenaar33570922005-01-25 22:26:29 +000022860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022862 rettv = alloc_tv();
22863 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022865 rettv->v_type = VAR_STRING;
22866 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 }
22868 else
22869 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022870 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871}
22872
22873/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022874 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022877free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878{
22879 if (varp != NULL)
22880 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022881 switch (varp->v_type)
22882 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022883 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022884 func_unref(varp->vval.v_string);
22885 /*FALLTHROUGH*/
22886 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022887 vim_free(varp->vval.v_string);
22888 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022889 case VAR_PARTIAL:
22890 partial_unref(varp->vval.v_partial);
22891 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022892 case VAR_LIST:
22893 list_unref(varp->vval.v_list);
22894 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022895 case VAR_DICT:
22896 dict_unref(varp->vval.v_dict);
22897 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022898 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022899#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022900 job_unref(varp->vval.v_job);
22901 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022902#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022903 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022904#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022905 channel_unref(varp->vval.v_channel);
22906 break;
22907#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022908 case VAR_NUMBER:
22909 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022910 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022911 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022912 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 vim_free(varp);
22915 }
22916}
22917
22918/*
22919 * Free the memory for a variable value and set the value to NULL or 0.
22920 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022921 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022922clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923{
22924 if (varp != NULL)
22925 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022926 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022928 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022929 func_unref(varp->vval.v_string);
22930 /*FALLTHROUGH*/
22931 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022932 vim_free(varp->vval.v_string);
22933 varp->vval.v_string = NULL;
22934 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022935 case VAR_PARTIAL:
22936 partial_unref(varp->vval.v_partial);
22937 varp->vval.v_partial = NULL;
22938 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022939 case VAR_LIST:
22940 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022941 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022942 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022943 case VAR_DICT:
22944 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022945 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022946 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022947 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022948 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022949 varp->vval.v_number = 0;
22950 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022951 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022952#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022953 varp->vval.v_float = 0.0;
22954 break;
22955#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022956 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022957#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022958 job_unref(varp->vval.v_job);
22959 varp->vval.v_job = NULL;
22960#endif
22961 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022962 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022963#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022964 channel_unref(varp->vval.v_channel);
22965 varp->vval.v_channel = NULL;
22966#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022967 case VAR_UNKNOWN:
22968 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022970 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 }
22972}
22973
22974/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022975 * Set the value of a variable to NULL without freeing items.
22976 */
22977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022978init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022979{
22980 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022981 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022982}
22983
22984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 * Get the number value of a variable.
22986 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022987 * For incompatible types, return 0.
22988 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22989 * caller of incompatible types: it sets *denote to TRUE if "denote"
22990 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022991 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022992 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022993get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022995 int error = FALSE;
22996
22997 return get_tv_number_chk(varp, &error); /* return 0L on error */
22998}
22999
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023000 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023001get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023002{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023003 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023005 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023007 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023008 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023009 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023010#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000023011 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023012 break;
23013#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023014 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023015 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023016 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023017 break;
23018 case VAR_STRING:
23019 if (varp->vval.v_string != NULL)
23020 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010023021 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023022 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023023 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023024 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023025 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023026 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023027 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023028 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010023029 case VAR_SPECIAL:
23030 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
23031 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023032 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023033#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023034 EMSG(_("E910: Using a Job as a Number"));
23035 break;
23036#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023037 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023038#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023039 EMSG(_("E913: Using a Channel as a Number"));
23040 break;
23041#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010023042 case VAR_UNKNOWN:
23043 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023044 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023046 if (denote == NULL) /* useful for values that must be unsigned */
23047 n = -1;
23048 else
23049 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023050 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051}
23052
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023053#ifdef FEAT_FLOAT
23054 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023055get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023056{
23057 switch (varp->v_type)
23058 {
23059 case VAR_NUMBER:
23060 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023061 case VAR_FLOAT:
23062 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023063 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023064 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023065 EMSG(_("E891: Using a Funcref as a Float"));
23066 break;
23067 case VAR_STRING:
23068 EMSG(_("E892: Using a String as a Float"));
23069 break;
23070 case VAR_LIST:
23071 EMSG(_("E893: Using a List as a Float"));
23072 break;
23073 case VAR_DICT:
23074 EMSG(_("E894: Using a Dictionary as a Float"));
23075 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023076 case VAR_SPECIAL:
23077 EMSG(_("E907: Using a special value as a Float"));
23078 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023079 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023080# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023081 EMSG(_("E911: Using a Job as a Float"));
23082 break;
23083# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023084 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023085# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023086 EMSG(_("E914: Using a Channel as a Float"));
23087 break;
23088# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010023089 case VAR_UNKNOWN:
23090 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023091 break;
23092 }
23093 return 0;
23094}
23095#endif
23096
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023098 * Get the lnum from the first argument.
23099 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023100 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 */
23102 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023103get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104{
Bram Moolenaar33570922005-01-25 22:26:29 +000023105 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106 linenr_T lnum;
23107
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023108 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109 if (lnum == 0) /* no valid number, try using line() */
23110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023111 rettv.v_type = VAR_NUMBER;
23112 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023113 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 }
23116 return lnum;
23117}
23118
23119/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023120 * Get the lnum from the first argument.
23121 * Also accepts "$", then "buf" is used.
23122 * Returns 0 on error.
23123 */
23124 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023125get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000023126{
23127 if (argvars[0].v_type == VAR_STRING
23128 && argvars[0].vval.v_string != NULL
23129 && argvars[0].vval.v_string[0] == '$'
23130 && buf != NULL)
23131 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023132 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000023133}
23134
23135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 * Get the string value of a variable.
23137 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000023138 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23139 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 * If the String variable has never been set, return an empty string.
23141 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023142 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
23143 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023145 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023146get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147{
23148 static char_u mybuf[NUMBUFLEN];
23149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023150 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151}
23152
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023153 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023154get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023156 char_u *res = get_tv_string_buf_chk(varp, buf);
23157
23158 return res != NULL ? res : (char_u *)"";
23159}
23160
Bram Moolenaar7d647822014-04-05 21:28:56 +020023161/*
23162 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23163 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000023164 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023165get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023166{
23167 static char_u mybuf[NUMBUFLEN];
23168
23169 return get_tv_string_buf_chk(varp, mybuf);
23170}
23171
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023172 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023173get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023174{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023175 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023177 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023178 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
23179 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023180 return buf;
23181 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023182 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023183 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023184 break;
23185 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023186 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000023187 break;
23188 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023189 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023190 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023191 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023192#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020023193 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023194 break;
23195#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023196 case VAR_STRING:
23197 if (varp->vval.v_string != NULL)
23198 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023199 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023200 case VAR_SPECIAL:
23201 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23202 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023203 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023204#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023205 {
23206 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023207 char *status;
23208
23209 if (job == NULL)
23210 return (char_u *)"no process";
23211 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023212 : job->jv_status == JOB_ENDED ? "dead"
23213 : "run";
23214# ifdef UNIX
23215 vim_snprintf((char *)buf, NUMBUFLEN,
23216 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023217# elif defined(WIN32)
23218 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023219 "process %ld %s",
23220 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023221 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023222# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023223 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023224 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23225# endif
23226 return buf;
23227 }
23228#endif
23229 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023230 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023231#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023232 {
23233 channel_T *channel = varp->vval.v_channel;
23234 char *status = channel_status(channel);
23235
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023236 if (channel == NULL)
23237 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23238 else
23239 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023240 "channel %d %s", channel->ch_id, status);
23241 return buf;
23242 }
23243#endif
23244 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023245 case VAR_UNKNOWN:
23246 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023247 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023249 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250}
23251
23252/*
23253 * Find variable "name" in the list of variables.
23254 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023255 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023256 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023257 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023259 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023260find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023263 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264
Bram Moolenaara7043832005-01-21 11:56:39 +000023265 ht = find_var_ht(name, &varname);
23266 if (htp != NULL)
23267 *htp = ht;
23268 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023270 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271}
23272
23273/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023274 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023275 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023277 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023278find_var_in_ht(
23279 hashtab_T *ht,
23280 int htname,
23281 char_u *varname,
23282 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023283{
Bram Moolenaar33570922005-01-25 22:26:29 +000023284 hashitem_T *hi;
23285
23286 if (*varname == NUL)
23287 {
23288 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023289 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023290 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023291 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023292 case 'g': return &globvars_var;
23293 case 'v': return &vimvars_var;
23294 case 'b': return &curbuf->b_bufvar;
23295 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023296#ifdef FEAT_WINDOWS
23297 case 't': return &curtab->tp_winvar;
23298#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023299 case 'l': return current_funccal == NULL
23300 ? NULL : &current_funccal->l_vars_var;
23301 case 'a': return current_funccal == NULL
23302 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023303 }
23304 return NULL;
23305 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023306
23307 hi = hash_find(ht, varname);
23308 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023309 {
23310 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023311 * worked find the variable again. Don't auto-load a script if it was
23312 * loaded already, otherwise it would be loaded every time when
23313 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023314 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023315 {
23316 /* Note: script_autoload() may make "hi" invalid. It must either
23317 * be obtained again or not used. */
23318 if (!script_autoload(varname, FALSE) || aborting())
23319 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023320 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023321 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023322 if (HASHITEM_EMPTY(hi))
23323 return NULL;
23324 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023325 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023326}
23327
23328/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023329 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023330 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023331 * Set "varname" to the start of name without ':'.
23332 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023333 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023334find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023336 hashitem_T *hi;
23337
Bram Moolenaar73627d02015-08-11 15:46:09 +020023338 if (name[0] == NUL)
23339 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 if (name[1] != ':')
23341 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023342 /* The name must not start with a colon or #. */
23343 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 return NULL;
23345 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023346
23347 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023348 hi = hash_find(&compat_hashtab, name);
23349 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023350 return &compat_hashtab;
23351
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023353 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023354 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 }
23356 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023357 if (*name == 'g') /* global variable */
23358 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023359 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23360 */
23361 if (vim_strchr(name + 2, ':') != NULL
23362 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023363 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023365 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023367 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023368#ifdef FEAT_WINDOWS
23369 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023370 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023371#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023372 if (*name == 'v') /* v: variable */
23373 return &vimvarht;
23374 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023375 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023376 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023377 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 if (*name == 's' /* script variable */
23379 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23380 return &SCRIPT_VARS(current_SID);
23381 return NULL;
23382}
23383
23384/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023385 * Get function call environment based on bactrace debug level
23386 */
23387 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023388get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023389{
23390 int i;
23391 funccall_T *funccal;
23392 funccall_T *temp_funccal;
23393
23394 funccal = current_funccal;
23395 if (debug_backtrace_level > 0)
23396 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023397 for (i = 0; i < debug_backtrace_level; i++)
23398 {
23399 temp_funccal = funccal->caller;
23400 if (temp_funccal)
23401 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023402 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023403 /* backtrace level overflow. reset to max */
23404 debug_backtrace_level = i;
23405 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023406 }
23407 return funccal;
23408}
23409
23410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023412 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 * Returns NULL when it doesn't exist.
23414 */
23415 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023416get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417{
Bram Moolenaar33570922005-01-25 22:26:29 +000023418 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023420 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 if (v == NULL)
23422 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023423 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424}
23425
23426/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023427 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 * sourcing this script and when executing functions defined in the script.
23429 */
23430 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023431new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432{
Bram Moolenaara7043832005-01-21 11:56:39 +000023433 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023434 hashtab_T *ht;
23435 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023436
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23438 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023439 /* Re-allocating ga_data means that an ht_array pointing to
23440 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023441 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023442 for (i = 1; i <= ga_scripts.ga_len; ++i)
23443 {
23444 ht = &SCRIPT_VARS(i);
23445 if (ht->ht_mask == HT_INIT_SIZE - 1)
23446 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023447 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023448 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023449 }
23450
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 while (ga_scripts.ga_len < id)
23452 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023453 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023454 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023455 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457 }
23458 }
23459}
23460
23461/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023462 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23463 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 */
23465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023466init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467{
Bram Moolenaar33570922005-01-25 22:26:29 +000023468 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023469 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023470 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023471 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023472 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023473 dict_var->di_tv.vval.v_dict = dict;
23474 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023475 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023476 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23477 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478}
23479
23480/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023481 * Unreference a dictionary initialized by init_var_dict().
23482 */
23483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023484unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023485{
23486 /* Now the dict needs to be freed if no one else is using it, go back to
23487 * normal reference counting. */
23488 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23489 dict_unref(dict);
23490}
23491
23492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023494 * Frees all allocated variables and the value they contain.
23495 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 */
23497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023498vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023499{
23500 vars_clear_ext(ht, TRUE);
23501}
23502
23503/*
23504 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23505 */
23506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023507vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508{
Bram Moolenaara7043832005-01-21 11:56:39 +000023509 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023510 hashitem_T *hi;
23511 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512
Bram Moolenaar33570922005-01-25 22:26:29 +000023513 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023514 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023515 for (hi = ht->ht_array; todo > 0; ++hi)
23516 {
23517 if (!HASHITEM_EMPTY(hi))
23518 {
23519 --todo;
23520
Bram Moolenaar33570922005-01-25 22:26:29 +000023521 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023522 * ht_array might change then. hash_clear() takes care of it
23523 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023524 v = HI2DI(hi);
23525 if (free_val)
23526 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023527 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023528 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023529 }
23530 }
23531 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023532 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533}
23534
Bram Moolenaara7043832005-01-21 11:56:39 +000023535/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023536 * Delete a variable from hashtab "ht" at item "hi".
23537 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023540delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541{
Bram Moolenaar33570922005-01-25 22:26:29 +000023542 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023543
23544 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023545 clear_tv(&di->di_tv);
23546 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547}
23548
23549/*
23550 * List the value of one internal variable.
23551 */
23552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023553list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023555 char_u *tofree;
23556 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023557 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023558
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023559 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023560 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023561 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023562 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563}
23564
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023566list_one_var_a(
23567 char_u *prefix,
23568 char_u *name,
23569 int type,
23570 char_u *string,
23571 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572{
Bram Moolenaar31859182007-08-14 20:41:13 +000023573 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23574 msg_start();
23575 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576 if (name != NULL) /* "a:" vars don't have a name stored */
23577 msg_puts(name);
23578 msg_putchar(' ');
23579 msg_advance(22);
23580 if (type == VAR_NUMBER)
23581 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023582 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023583 msg_putchar('*');
23584 else if (type == VAR_LIST)
23585 {
23586 msg_putchar('[');
23587 if (*string == '[')
23588 ++string;
23589 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023590 else if (type == VAR_DICT)
23591 {
23592 msg_putchar('{');
23593 if (*string == '{')
23594 ++string;
23595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 else
23597 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023598
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023600
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023601 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023602 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023603 if (*first)
23604 {
23605 msg_clr_eos();
23606 *first = FALSE;
23607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608}
23609
23610/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023611 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 * If the variable already exists, the value is updated.
23613 * Otherwise the variable is created.
23614 */
23615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023616set_var(
23617 char_u *name,
23618 typval_T *tv,
23619 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620{
Bram Moolenaar33570922005-01-25 22:26:29 +000023621 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023623 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023625 ht = find_var_ht(name, &varname);
23626 if (ht == NULL || *varname == NUL)
23627 {
23628 EMSG2(_(e_illvar), name);
23629 return;
23630 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023631 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023632
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023633 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23634 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023635 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023636
Bram Moolenaar33570922005-01-25 22:26:29 +000023637 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023639 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023640 if (var_check_ro(v->di_flags, name, FALSE)
23641 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023642 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023643
23644 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023645 * Handle setting internal v: variables separately where needed to
23646 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023647 */
23648 if (ht == &vimvarht)
23649 {
23650 if (v->di_tv.v_type == VAR_STRING)
23651 {
23652 vim_free(v->di_tv.vval.v_string);
23653 if (copy || tv->v_type != VAR_STRING)
23654 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23655 else
23656 {
23657 /* Take over the string to avoid an extra alloc/free. */
23658 v->di_tv.vval.v_string = tv->vval.v_string;
23659 tv->vval.v_string = NULL;
23660 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023661 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023662 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023663 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023664 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023665 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023666 if (STRCMP(varname, "searchforward") == 0)
23667 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023668#ifdef FEAT_SEARCH_EXTRA
23669 else if (STRCMP(varname, "hlsearch") == 0)
23670 {
23671 no_hlsearch = !v->di_tv.vval.v_number;
23672 redraw_all_later(SOME_VALID);
23673 }
23674#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023675 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023676 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023677 else if (v->di_tv.v_type != tv->v_type)
23678 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023679 }
23680
23681 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 }
23683 else /* add a new variable */
23684 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023685 /* Can't add "v:" variable. */
23686 if (ht == &vimvarht)
23687 {
23688 EMSG2(_(e_illvar), name);
23689 return;
23690 }
23691
Bram Moolenaar92124a32005-06-17 22:03:40 +000023692 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023693 if (!valid_varname(varname))
23694 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023695
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023696 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23697 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023698 if (v == NULL)
23699 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023700 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023701 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023703 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 return;
23705 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023706 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023708
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023709 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023710 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023711 else
23712 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023713 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023714 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023715 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717}
23718
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023719/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023720 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023721 * Also give an error message.
23722 */
23723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023724var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023725{
23726 if (flags & DI_FLAGS_RO)
23727 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023728 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023729 return TRUE;
23730 }
23731 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23732 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023733 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023734 return TRUE;
23735 }
23736 return FALSE;
23737}
23738
23739/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023740 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23741 * Also give an error message.
23742 */
23743 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023744var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023745{
23746 if (flags & DI_FLAGS_FIX)
23747 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023748 EMSG2(_("E795: Cannot delete variable %s"),
23749 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023750 return TRUE;
23751 }
23752 return FALSE;
23753}
23754
23755/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023756 * Check if a funcref is assigned to a valid variable name.
23757 * Return TRUE and give an error if not.
23758 */
23759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023760var_check_func_name(
23761 char_u *name, /* points to start of variable name */
23762 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023763{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023764 /* Allow for w: b: s: and t:. */
23765 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023766 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23767 ? name[2] : name[0]))
23768 {
23769 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23770 name);
23771 return TRUE;
23772 }
23773 /* Don't allow hiding a function. When "v" is not NULL we might be
23774 * assigning another function to the same var, the type is checked
23775 * below. */
23776 if (new_var && function_exists(name))
23777 {
23778 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23779 name);
23780 return TRUE;
23781 }
23782 return FALSE;
23783}
23784
23785/*
23786 * Check if a variable name is valid.
23787 * Return FALSE and give an error if not.
23788 */
23789 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023790valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023791{
23792 char_u *p;
23793
23794 for (p = varname; *p != NUL; ++p)
23795 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23796 && *p != AUTOLOAD_CHAR)
23797 {
23798 EMSG2(_(e_illvar), varname);
23799 return FALSE;
23800 }
23801 return TRUE;
23802}
23803
23804/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023805 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023806 * Also give an error message, using "name" or _("name") when use_gettext is
23807 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023808 */
23809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023810tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023811{
23812 if (lock & VAR_LOCKED)
23813 {
23814 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023815 name == NULL ? (char_u *)_("Unknown")
23816 : use_gettext ? (char_u *)_(name)
23817 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023818 return TRUE;
23819 }
23820 if (lock & VAR_FIXED)
23821 {
23822 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023823 name == NULL ? (char_u *)_("Unknown")
23824 : use_gettext ? (char_u *)_(name)
23825 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023826 return TRUE;
23827 }
23828 return FALSE;
23829}
23830
23831/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023832 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023833 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023834 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023835 * It is OK for "from" and "to" to point to the same item. This is used to
23836 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023837 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023839copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023841 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023842 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023843 switch (from->v_type)
23844 {
23845 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023846 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023847 to->vval.v_number = from->vval.v_number;
23848 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023849 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023850#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023851 to->vval.v_float = from->vval.v_float;
23852 break;
23853#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023854 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023855#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023856 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023857 if (to->vval.v_job != NULL)
23858 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023859 break;
23860#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023861 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023862#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023863 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023864 if (to->vval.v_channel != NULL)
23865 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023866 break;
23867#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023868 case VAR_STRING:
23869 case VAR_FUNC:
23870 if (from->vval.v_string == NULL)
23871 to->vval.v_string = NULL;
23872 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023873 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023874 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023875 if (from->v_type == VAR_FUNC)
23876 func_ref(to->vval.v_string);
23877 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023878 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023879 case VAR_PARTIAL:
23880 if (from->vval.v_partial == NULL)
23881 to->vval.v_partial = NULL;
23882 else
23883 {
23884 to->vval.v_partial = from->vval.v_partial;
23885 ++to->vval.v_partial->pt_refcount;
23886 }
23887 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023888 case VAR_LIST:
23889 if (from->vval.v_list == NULL)
23890 to->vval.v_list = NULL;
23891 else
23892 {
23893 to->vval.v_list = from->vval.v_list;
23894 ++to->vval.v_list->lv_refcount;
23895 }
23896 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023897 case VAR_DICT:
23898 if (from->vval.v_dict == NULL)
23899 to->vval.v_dict = NULL;
23900 else
23901 {
23902 to->vval.v_dict = from->vval.v_dict;
23903 ++to->vval.v_dict->dv_refcount;
23904 }
23905 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023906 case VAR_UNKNOWN:
23907 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023908 break;
23909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910}
23911
23912/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023913 * Make a copy of an item.
23914 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023915 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23916 * reference to an already copied list/dict can be used.
23917 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023918 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023920item_copy(
23921 typval_T *from,
23922 typval_T *to,
23923 int deep,
23924 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023925{
23926 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023927 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023928
Bram Moolenaar33570922005-01-25 22:26:29 +000023929 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023930 {
23931 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023932 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023933 }
23934 ++recurse;
23935
23936 switch (from->v_type)
23937 {
23938 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023939 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023940 case VAR_STRING:
23941 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023942 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023943 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023944 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023945 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023946 copy_tv(from, to);
23947 break;
23948 case VAR_LIST:
23949 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023950 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023951 if (from->vval.v_list == NULL)
23952 to->vval.v_list = NULL;
23953 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23954 {
23955 /* use the copy made earlier */
23956 to->vval.v_list = from->vval.v_list->lv_copylist;
23957 ++to->vval.v_list->lv_refcount;
23958 }
23959 else
23960 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23961 if (to->vval.v_list == NULL)
23962 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023963 break;
23964 case VAR_DICT:
23965 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023966 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023967 if (from->vval.v_dict == NULL)
23968 to->vval.v_dict = NULL;
23969 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23970 {
23971 /* use the copy made earlier */
23972 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23973 ++to->vval.v_dict->dv_refcount;
23974 }
23975 else
23976 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23977 if (to->vval.v_dict == NULL)
23978 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023979 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023980 case VAR_UNKNOWN:
23981 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023982 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023983 }
23984 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023985 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023986}
23987
23988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023989 * ":echo expr1 ..." print each argument separated with a space, add a
23990 * newline at the end.
23991 * ":echon expr1 ..." print each argument plain.
23992 */
23993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023994ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995{
23996 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023997 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023998 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 char_u *p;
24000 int needclr = TRUE;
24001 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024002 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003
24004 if (eap->skip)
24005 ++emsg_skip;
24006 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
24007 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024008 /* If eval1() causes an error message the text from the command may
24009 * still need to be cleared. E.g., "echo 22,44". */
24010 need_clr_eos = needclr;
24011
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024013 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 {
24015 /*
24016 * Report the invalid expression unless the expression evaluation
24017 * has been cancelled due to an aborting error, an interrupt, or an
24018 * exception.
24019 */
24020 if (!aborting())
24021 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024022 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 break;
24024 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024025 need_clr_eos = FALSE;
24026
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 if (!eap->skip)
24028 {
24029 if (atstart)
24030 {
24031 atstart = FALSE;
24032 /* Call msg_start() after eval1(), evaluating the expression
24033 * may cause a message to appear. */
24034 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010024035 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020024036 /* Mark the saved text as finishing the line, so that what
24037 * follows is displayed on a new line when scrolling back
24038 * at the more prompt. */
24039 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010024041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042 }
24043 else if (eap->cmdidx == CMD_echo)
24044 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010024045 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024046 if (p != NULL)
24047 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024049 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024051 if (*p != TAB && needclr)
24052 {
24053 /* remove any text still there from the command */
24054 msg_clr_eos();
24055 needclr = FALSE;
24056 }
24057 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 }
24059 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024060 {
24061#ifdef FEAT_MBYTE
24062 if (has_mbyte)
24063 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000024064 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024065
24066 (void)msg_outtrans_len_attr(p, i, echo_attr);
24067 p += i - 1;
24068 }
24069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024071 (void)msg_outtrans_len_attr(p, 1, echo_attr);
24072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024074 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024076 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077 arg = skipwhite(arg);
24078 }
24079 eap->nextcmd = check_nextcmd(arg);
24080
24081 if (eap->skip)
24082 --emsg_skip;
24083 else
24084 {
24085 /* remove text that may still be there from the command */
24086 if (needclr)
24087 msg_clr_eos();
24088 if (eap->cmdidx == CMD_echo)
24089 msg_end();
24090 }
24091}
24092
24093/*
24094 * ":echohl {name}".
24095 */
24096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024097ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098{
24099 int id;
24100
24101 id = syn_name2id(eap->arg);
24102 if (id == 0)
24103 echo_attr = 0;
24104 else
24105 echo_attr = syn_id2attr(id);
24106}
24107
24108/*
24109 * ":execute expr1 ..." execute the result of an expression.
24110 * ":echomsg expr1 ..." Print a message
24111 * ":echoerr expr1 ..." Print an error
24112 * Each gets spaces around each argument and a newline at the end for
24113 * echo commands
24114 */
24115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024116ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117{
24118 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024119 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120 int ret = OK;
24121 char_u *p;
24122 garray_T ga;
24123 int len;
24124 int save_did_emsg;
24125
24126 ga_init2(&ga, 1, 80);
24127
24128 if (eap->skip)
24129 ++emsg_skip;
24130 while (*arg != NUL && *arg != '|' && *arg != '\n')
24131 {
24132 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024133 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 {
24135 /*
24136 * Report the invalid expression unless the expression evaluation
24137 * has been cancelled due to an aborting error, an interrupt, or an
24138 * exception.
24139 */
24140 if (!aborting())
24141 EMSG2(_(e_invexpr2), p);
24142 ret = FAIL;
24143 break;
24144 }
24145
24146 if (!eap->skip)
24147 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024148 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 len = (int)STRLEN(p);
24150 if (ga_grow(&ga, len + 2) == FAIL)
24151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024152 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 ret = FAIL;
24154 break;
24155 }
24156 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 ga.ga_len += len;
24160 }
24161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024162 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 arg = skipwhite(arg);
24164 }
24165
24166 if (ret != FAIL && ga.ga_data != NULL)
24167 {
24168 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000024169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000024171 out_flush();
24172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 else if (eap->cmdidx == CMD_echoerr)
24174 {
24175 /* We don't want to abort following commands, restore did_emsg. */
24176 save_did_emsg = did_emsg;
24177 EMSG((char_u *)ga.ga_data);
24178 if (!force_abort)
24179 did_emsg = save_did_emsg;
24180 }
24181 else if (eap->cmdidx == CMD_execute)
24182 do_cmdline((char_u *)ga.ga_data,
24183 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
24184 }
24185
24186 ga_clear(&ga);
24187
24188 if (eap->skip)
24189 --emsg_skip;
24190
24191 eap->nextcmd = check_nextcmd(arg);
24192}
24193
24194/*
24195 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24196 * "arg" points to the "&" or '+' when called, to "option" when returning.
24197 * Returns NULL when no option name found. Otherwise pointer to the char
24198 * after the option name.
24199 */
24200 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024201find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024202{
24203 char_u *p = *arg;
24204
24205 ++p;
24206 if (*p == 'g' && p[1] == ':')
24207 {
24208 *opt_flags = OPT_GLOBAL;
24209 p += 2;
24210 }
24211 else if (*p == 'l' && p[1] == ':')
24212 {
24213 *opt_flags = OPT_LOCAL;
24214 p += 2;
24215 }
24216 else
24217 *opt_flags = 0;
24218
24219 if (!ASCII_ISALPHA(*p))
24220 return NULL;
24221 *arg = p;
24222
24223 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24224 p += 4; /* termcap option */
24225 else
24226 while (ASCII_ISALPHA(*p))
24227 ++p;
24228 return p;
24229}
24230
24231/*
24232 * ":function"
24233 */
24234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024235ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236{
24237 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024238 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 int j;
24240 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024242 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 char_u *name = NULL;
24244 char_u *p;
24245 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024246 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 garray_T newargs;
24248 garray_T newlines;
24249 int varargs = FALSE;
24250 int mustend = FALSE;
24251 int flags = 0;
24252 ufunc_T *fp;
24253 int indent;
24254 int nesting;
24255 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024256 dictitem_T *v;
24257 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024258 static int func_nr = 0; /* number for nameless function */
24259 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024260 hashtab_T *ht;
24261 int todo;
24262 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024263 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264
24265 /*
24266 * ":function" without argument: list functions.
24267 */
24268 if (ends_excmd(*eap->arg))
24269 {
24270 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024271 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024272 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024273 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024274 {
24275 if (!HASHITEM_EMPTY(hi))
24276 {
24277 --todo;
24278 fp = HI2UF(hi);
24279 if (!isdigit(*fp->uf_name))
24280 list_func_head(fp, FALSE);
24281 }
24282 }
24283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 eap->nextcmd = check_nextcmd(eap->arg);
24285 return;
24286 }
24287
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024288 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024289 * ":function /pat": list functions matching pattern.
24290 */
24291 if (*eap->arg == '/')
24292 {
24293 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24294 if (!eap->skip)
24295 {
24296 regmatch_T regmatch;
24297
24298 c = *p;
24299 *p = NUL;
24300 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24301 *p = c;
24302 if (regmatch.regprog != NULL)
24303 {
24304 regmatch.rm_ic = p_ic;
24305
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024306 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024307 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24308 {
24309 if (!HASHITEM_EMPTY(hi))
24310 {
24311 --todo;
24312 fp = HI2UF(hi);
24313 if (!isdigit(*fp->uf_name)
24314 && vim_regexec(&regmatch, fp->uf_name, 0))
24315 list_func_head(fp, FALSE);
24316 }
24317 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024318 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024319 }
24320 }
24321 if (*p == '/')
24322 ++p;
24323 eap->nextcmd = check_nextcmd(p);
24324 return;
24325 }
24326
24327 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024328 * Get the function name. There are these situations:
24329 * func normal function name
24330 * "name" == func, "fudi.fd_dict" == NULL
24331 * dict.func new dictionary entry
24332 * "name" == NULL, "fudi.fd_dict" set,
24333 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24334 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024335 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024336 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24337 * dict.func existing dict entry that's not a Funcref
24338 * "name" == NULL, "fudi.fd_dict" set,
24339 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024340 * s:func script-local function name
24341 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024343 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024344 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024345 paren = (vim_strchr(p, '(') != NULL);
24346 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 {
24348 /*
24349 * Return on an invalid expression in braces, unless the expression
24350 * evaluation has been cancelled due to an aborting error, an
24351 * interrupt, or an exception.
24352 */
24353 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024355 if (!eap->skip && fudi.fd_newkey != NULL)
24356 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024357 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024360 else
24361 eap->skip = TRUE;
24362 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024363
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 /* An error in a function call during evaluation of an expression in magic
24365 * braces should not cause the function not to be defined. */
24366 saved_did_emsg = did_emsg;
24367 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368
24369 /*
24370 * ":function func" with only function name: list function.
24371 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024372 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 {
24374 if (!ends_excmd(*skipwhite(p)))
24375 {
24376 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024377 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 }
24379 eap->nextcmd = check_nextcmd(p);
24380 if (eap->nextcmd != NULL)
24381 *p = NUL;
24382 if (!eap->skip && !got_int)
24383 {
24384 fp = find_func(name);
24385 if (fp != NULL)
24386 {
24387 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024388 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024390 if (FUNCLINE(fp, j) == NULL)
24391 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 msg_putchar('\n');
24393 msg_outnum((long)(j + 1));
24394 if (j < 9)
24395 msg_putchar(' ');
24396 if (j < 99)
24397 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024398 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 out_flush(); /* show a line at a time */
24400 ui_breakcheck();
24401 }
24402 if (!got_int)
24403 {
24404 msg_putchar('\n');
24405 msg_puts((char_u *)" endfunction");
24406 }
24407 }
24408 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024409 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024411 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 }
24413
24414 /*
24415 * ":function name(arg1, arg2)" Define function.
24416 */
24417 p = skipwhite(p);
24418 if (*p != '(')
24419 {
24420 if (!eap->skip)
24421 {
24422 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024423 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024424 }
24425 /* attempt to continue by skipping some text */
24426 if (vim_strchr(p, '(') != NULL)
24427 p = vim_strchr(p, '(');
24428 }
24429 p = skipwhite(p + 1);
24430
24431 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24432 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24433
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024434 if (!eap->skip)
24435 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024436 /* Check the name of the function. Unless it's a dictionary function
24437 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024438 if (name != NULL)
24439 arg = name;
24440 else
24441 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024442 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024443 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24444 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024445 {
24446 if (*arg == K_SPECIAL)
24447 j = 3;
24448 else
24449 j = 0;
24450 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24451 : eval_isnamec(arg[j])))
24452 ++j;
24453 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024454 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024455 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024456 /* Disallow using the g: dict. */
24457 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24458 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024459 }
24460
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461 /*
24462 * Isolate the arguments: "arg1, arg2, ...)"
24463 */
24464 while (*p != ')')
24465 {
24466 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24467 {
24468 varargs = TRUE;
24469 p += 3;
24470 mustend = TRUE;
24471 }
24472 else
24473 {
24474 arg = p;
24475 while (ASCII_ISALNUM(*p) || *p == '_')
24476 ++p;
24477 if (arg == p || isdigit(*arg)
24478 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24479 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24480 {
24481 if (!eap->skip)
24482 EMSG2(_("E125: Illegal argument: %s"), arg);
24483 break;
24484 }
24485 if (ga_grow(&newargs, 1) == FAIL)
24486 goto erret;
24487 c = *p;
24488 *p = NUL;
24489 arg = vim_strsave(arg);
24490 if (arg == NULL)
24491 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024492
24493 /* Check for duplicate argument name. */
24494 for (i = 0; i < newargs.ga_len; ++i)
24495 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24496 {
24497 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024498 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024499 goto erret;
24500 }
24501
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24503 *p = c;
24504 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 if (*p == ',')
24506 ++p;
24507 else
24508 mustend = TRUE;
24509 }
24510 p = skipwhite(p);
24511 if (mustend && *p != ')')
24512 {
24513 if (!eap->skip)
24514 EMSG2(_(e_invarg2), eap->arg);
24515 break;
24516 }
24517 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024518 if (*p != ')')
24519 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024520 ++p; /* skip the ')' */
24521
Bram Moolenaare9a41262005-01-15 22:18:47 +000024522 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523 for (;;)
24524 {
24525 p = skipwhite(p);
24526 if (STRNCMP(p, "range", 5) == 0)
24527 {
24528 flags |= FC_RANGE;
24529 p += 5;
24530 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024531 else if (STRNCMP(p, "dict", 4) == 0)
24532 {
24533 flags |= FC_DICT;
24534 p += 4;
24535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024536 else if (STRNCMP(p, "abort", 5) == 0)
24537 {
24538 flags |= FC_ABORT;
24539 p += 5;
24540 }
24541 else
24542 break;
24543 }
24544
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024545 /* When there is a line break use what follows for the function body.
24546 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24547 if (*p == '\n')
24548 line_arg = p + 1;
24549 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 EMSG(_(e_trailing));
24551
24552 /*
24553 * Read the body of the function, until ":endfunction" is found.
24554 */
24555 if (KeyTyped)
24556 {
24557 /* Check if the function already exists, don't let the user type the
24558 * whole function before telling him it doesn't work! For a script we
24559 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024560 if (!eap->skip && !eap->forceit)
24561 {
24562 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24563 EMSG(_(e_funcdict));
24564 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024565 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024568 if (!eap->skip && did_emsg)
24569 goto erret;
24570
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571 msg_putchar('\n'); /* don't overwrite the function name */
24572 cmdline_row = msg_row;
24573 }
24574
24575 indent = 2;
24576 nesting = 0;
24577 for (;;)
24578 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024579 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024580 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024581 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024582 saved_wait_return = FALSE;
24583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024585 sourcing_lnum_off = sourcing_lnum;
24586
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024587 if (line_arg != NULL)
24588 {
24589 /* Use eap->arg, split up in parts by line breaks. */
24590 theline = line_arg;
24591 p = vim_strchr(theline, '\n');
24592 if (p == NULL)
24593 line_arg += STRLEN(line_arg);
24594 else
24595 {
24596 *p = NUL;
24597 line_arg = p + 1;
24598 }
24599 }
24600 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 theline = getcmdline(':', 0L, indent);
24602 else
24603 theline = eap->getline(':', eap->cookie, indent);
24604 if (KeyTyped)
24605 lines_left = Rows - 1;
24606 if (theline == NULL)
24607 {
24608 EMSG(_("E126: Missing :endfunction"));
24609 goto erret;
24610 }
24611
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024612 /* Detect line continuation: sourcing_lnum increased more than one. */
24613 if (sourcing_lnum > sourcing_lnum_off + 1)
24614 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24615 else
24616 sourcing_lnum_off = 0;
24617
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618 if (skip_until != NULL)
24619 {
24620 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24621 * don't check for ":endfunc". */
24622 if (STRCMP(theline, skip_until) == 0)
24623 {
24624 vim_free(skip_until);
24625 skip_until = NULL;
24626 }
24627 }
24628 else
24629 {
24630 /* skip ':' and blanks*/
24631 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24632 ;
24633
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024634 /* Check for "endfunction". */
24635 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024637 if (line_arg == NULL)
24638 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 break;
24640 }
24641
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024642 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 * at "end". */
24644 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24645 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024646 else if (STRNCMP(p, "if", 2) == 0
24647 || STRNCMP(p, "wh", 2) == 0
24648 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649 || STRNCMP(p, "try", 3) == 0)
24650 indent += 2;
24651
24652 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024653 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024654 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024655 if (*p == '!')
24656 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024658 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024659 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024660 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024661 ++nesting;
24662 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 }
24664 }
24665
24666 /* Check for ":append" or ":insert". */
24667 p = skip_range(p, NULL);
24668 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24669 || (p[0] == 'i'
24670 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24671 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24672 skip_until = vim_strsave((char_u *)".");
24673
24674 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24675 arg = skipwhite(skiptowhite(p));
24676 if (arg[0] == '<' && arg[1] =='<'
24677 && ((p[0] == 'p' && p[1] == 'y'
24678 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24679 || (p[0] == 'p' && p[1] == 'e'
24680 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24681 || (p[0] == 't' && p[1] == 'c'
24682 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024683 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24684 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24686 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024687 || (p[0] == 'm' && p[1] == 'z'
24688 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 ))
24690 {
24691 /* ":python <<" continues until a dot, like ":append" */
24692 p = skipwhite(arg + 2);
24693 if (*p == NUL)
24694 skip_until = vim_strsave((char_u *)".");
24695 else
24696 skip_until = vim_strsave(p);
24697 }
24698 }
24699
24700 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024701 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024702 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024703 if (line_arg == NULL)
24704 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024706 }
24707
24708 /* Copy the line to newly allocated memory. get_one_sourceline()
24709 * allocates 250 bytes per line, this saves 80% on average. The cost
24710 * is an extra alloc/free. */
24711 p = vim_strsave(theline);
24712 if (p != NULL)
24713 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024714 if (line_arg == NULL)
24715 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024716 theline = p;
24717 }
24718
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024719 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24720
24721 /* Add NULL lines for continuation lines, so that the line count is
24722 * equal to the index in the growarray. */
24723 while (sourcing_lnum_off-- > 0)
24724 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024725
24726 /* Check for end of eap->arg. */
24727 if (line_arg != NULL && *line_arg == NUL)
24728 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729 }
24730
24731 /* Don't define the function when skipping commands or when an error was
24732 * detected. */
24733 if (eap->skip || did_emsg)
24734 goto erret;
24735
24736 /*
24737 * If there are no errors, add the function
24738 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024739 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024740 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024741 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024742 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024743 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024744 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024745 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024746 goto erret;
24747 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024748
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024749 fp = find_func(name);
24750 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024752 if (!eap->forceit)
24753 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024754 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024755 goto erret;
24756 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024757 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024758 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024759 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024760 name);
24761 goto erret;
24762 }
24763 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024764 ga_clear_strings(&(fp->uf_args));
24765 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024766 vim_free(name);
24767 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 }
24770 else
24771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024772 char numbuf[20];
24773
24774 fp = NULL;
24775 if (fudi.fd_newkey == NULL && !eap->forceit)
24776 {
24777 EMSG(_(e_funcdict));
24778 goto erret;
24779 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024780 if (fudi.fd_di == NULL)
24781 {
24782 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024783 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024784 goto erret;
24785 }
24786 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024787 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024788 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024789
24790 /* Give the function a sequential number. Can only be used with a
24791 * Funcref! */
24792 vim_free(name);
24793 sprintf(numbuf, "%d", ++func_nr);
24794 name = vim_strsave((char_u *)numbuf);
24795 if (name == NULL)
24796 goto erret;
24797 }
24798
24799 if (fp == NULL)
24800 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024801 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024802 {
24803 int slen, plen;
24804 char_u *scriptname;
24805
24806 /* Check that the autoload name matches the script name. */
24807 j = FAIL;
24808 if (sourcing_name != NULL)
24809 {
24810 scriptname = autoload_name(name);
24811 if (scriptname != NULL)
24812 {
24813 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024814 plen = (int)STRLEN(p);
24815 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024816 if (slen > plen && fnamecmp(p,
24817 sourcing_name + slen - plen) == 0)
24818 j = OK;
24819 vim_free(scriptname);
24820 }
24821 }
24822 if (j == FAIL)
24823 {
24824 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24825 goto erret;
24826 }
24827 }
24828
24829 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830 if (fp == NULL)
24831 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024832
24833 if (fudi.fd_dict != NULL)
24834 {
24835 if (fudi.fd_di == NULL)
24836 {
24837 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024838 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024839 if (fudi.fd_di == NULL)
24840 {
24841 vim_free(fp);
24842 goto erret;
24843 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024844 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24845 {
24846 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024847 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024848 goto erret;
24849 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024850 }
24851 else
24852 /* overwrite existing dict entry */
24853 clear_tv(&fudi.fd_di->di_tv);
24854 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024855 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024856 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024857 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024858
24859 /* behave like "dict" was used */
24860 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024861 }
24862
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024864 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024865 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24866 {
24867 vim_free(fp);
24868 goto erret;
24869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024871 fp->uf_args = newargs;
24872 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024873#ifdef FEAT_PROFILE
24874 fp->uf_tml_count = NULL;
24875 fp->uf_tml_total = NULL;
24876 fp->uf_tml_self = NULL;
24877 fp->uf_profiling = FALSE;
24878 if (prof_def_func())
24879 func_do_profile(fp);
24880#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024881 fp->uf_varargs = varargs;
24882 fp->uf_flags = flags;
24883 fp->uf_calls = 0;
24884 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024885 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886
24887erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 ga_clear_strings(&newargs);
24889 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024890ret_free:
24891 vim_free(skip_until);
24892 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024895 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896}
24897
24898/*
24899 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024900 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024902 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024903 * TFN_INT: internal function name OK
24904 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024905 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 * Advances "pp" to just after the function name (if no error).
24907 */
24908 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024909trans_function_name(
24910 char_u **pp,
24911 int skip, /* only find the end, don't evaluate */
24912 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024913 funcdict_T *fdp, /* return: info about dictionary used */
24914 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024916 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917 char_u *start;
24918 char_u *end;
24919 int lead;
24920 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024922 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024923
24924 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024925 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024926 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024927
24928 /* Check for hard coded <SNR>: already translated function ID (from a user
24929 * command). */
24930 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24931 && (*pp)[2] == (int)KE_SNR)
24932 {
24933 *pp += 3;
24934 len = get_id_len(pp) + 3;
24935 return vim_strnsave(start, len);
24936 }
24937
24938 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24939 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024941 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024942 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024943
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024944 /* Note that TFN_ flags use the same values as GLV_ flags. */
24945 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024946 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024947 if (end == start)
24948 {
24949 if (!skip)
24950 EMSG(_("E129: Function name required"));
24951 goto theend;
24952 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024953 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024954 {
24955 /*
24956 * Report an invalid expression in braces, unless the expression
24957 * evaluation has been cancelled due to an aborting error, an
24958 * interrupt, or an exception.
24959 */
24960 if (!aborting())
24961 {
24962 if (end != NULL)
24963 EMSG2(_(e_invarg2), start);
24964 }
24965 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024966 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024967 goto theend;
24968 }
24969
24970 if (lv.ll_tv != NULL)
24971 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024972 if (fdp != NULL)
24973 {
24974 fdp->fd_dict = lv.ll_dict;
24975 fdp->fd_newkey = lv.ll_newkey;
24976 lv.ll_newkey = NULL;
24977 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024978 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024979 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24980 {
24981 name = vim_strsave(lv.ll_tv->vval.v_string);
24982 *pp = end;
24983 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024984 else if (lv.ll_tv->v_type == VAR_PARTIAL
24985 && lv.ll_tv->vval.v_partial != NULL)
24986 {
24987 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24988 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024989 if (partial != NULL)
24990 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024991 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024992 else
24993 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024994 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24995 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024996 EMSG(_(e_funcref));
24997 else
24998 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024999 name = NULL;
25000 }
25001 goto theend;
25002 }
25003
25004 if (lv.ll_name == NULL)
25005 {
25006 /* Error found, but continue after the function name. */
25007 *pp = end;
25008 goto theend;
25009 }
25010
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025011 /* Check if the name is a Funcref. If so, use the value. */
25012 if (lv.ll_exp_name != NULL)
25013 {
25014 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010025015 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025016 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025017 if (name == lv.ll_exp_name)
25018 name = NULL;
25019 }
25020 else
25021 {
25022 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010025023 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025024 if (name == *pp)
25025 name = NULL;
25026 }
25027 if (name != NULL)
25028 {
25029 name = vim_strsave(name);
25030 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020025031 if (STRNCMP(name, "<SNR>", 5) == 0)
25032 {
25033 /* Change "<SNR>" to the byte sequence. */
25034 name[0] = K_SPECIAL;
25035 name[1] = KS_EXTRA;
25036 name[2] = (int)KE_SNR;
25037 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
25038 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025039 goto theend;
25040 }
25041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025042 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000025043 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025044 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000025045 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
25046 && STRNCMP(lv.ll_name, "s:", 2) == 0)
25047 {
25048 /* When there was "s:" already or the name expanded to get a
25049 * leading "s:" then remove it. */
25050 lv.ll_name += 2;
25051 len -= 2;
25052 lead = 2;
25053 }
25054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025055 else
Bram Moolenaara7043832005-01-21 11:56:39 +000025056 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025057 /* skip over "s:" and "g:" */
25058 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000025059 lv.ll_name += 2;
25060 len = (int)(end - lv.ll_name);
25061 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025062
25063 /*
25064 * Copy the function name to allocated memory.
25065 * Accept <SID>name() inside a script, translate into <SNR>123_name().
25066 * Accept <SNR>123_name() outside a script.
25067 */
25068 if (skip)
25069 lead = 0; /* do nothing */
25070 else if (lead > 0)
25071 {
25072 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000025073 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
25074 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025075 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000025076 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025077 if (current_SID <= 0)
25078 {
25079 EMSG(_(e_usingsid));
25080 goto theend;
25081 }
25082 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
25083 lead += (int)STRLEN(sid_buf);
25084 }
25085 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025086 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025087 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025088 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025089 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025090 goto theend;
25091 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025092 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025093 {
25094 char_u *cp = vim_strchr(lv.ll_name, ':');
25095
25096 if (cp != NULL && cp < end)
25097 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025098 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025099 goto theend;
25100 }
25101 }
25102
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025103 name = alloc((unsigned)(len + lead + 1));
25104 if (name != NULL)
25105 {
25106 if (lead > 0)
25107 {
25108 name[0] = K_SPECIAL;
25109 name[1] = KS_EXTRA;
25110 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000025111 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025112 STRCPY(name + 3, sid_buf);
25113 }
25114 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025115 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025116 }
25117 *pp = end;
25118
25119theend:
25120 clear_lval(&lv);
25121 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122}
25123
25124/*
25125 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
25126 * Return 2 if "p" starts with "s:".
25127 * Return 0 otherwise.
25128 */
25129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025130eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025131{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010025132 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
25133 * the standard library function. */
25134 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
25135 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136 return 5;
25137 if (p[0] == 's' && p[1] == ':')
25138 return 2;
25139 return 0;
25140}
25141
25142/*
25143 * Return TRUE if "p" starts with "<SID>" or "s:".
25144 * Only works if eval_fname_script() returned non-zero for "p"!
25145 */
25146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025147eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148{
25149 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
25150}
25151
25152/*
25153 * List the head of the function: "name(arg1, arg2)".
25154 */
25155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025156list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025157{
25158 int j;
25159
25160 msg_start();
25161 if (indent)
25162 MSG_PUTS(" ");
25163 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025164 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165 {
25166 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025167 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168 }
25169 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025170 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025172 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025173 {
25174 if (j)
25175 MSG_PUTS(", ");
25176 msg_puts(FUNCARG(fp, j));
25177 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025178 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025179 {
25180 if (j)
25181 MSG_PUTS(", ");
25182 MSG_PUTS("...");
25183 }
25184 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020025185 if (fp->uf_flags & FC_ABORT)
25186 MSG_PUTS(" abort");
25187 if (fp->uf_flags & FC_RANGE)
25188 MSG_PUTS(" range");
25189 if (fp->uf_flags & FC_DICT)
25190 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025191 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000025192 if (p_verbose > 0)
25193 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194}
25195
25196/*
25197 * Find a function by name, return pointer to it in ufuncs.
25198 * Return NULL for unknown function.
25199 */
25200 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025201find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025203 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025205 hi = hash_find(&func_hashtab, name);
25206 if (!HASHITEM_EMPTY(hi))
25207 return HI2UF(hi);
25208 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209}
25210
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025211#if defined(EXITFREE) || defined(PROTO)
25212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025213free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025214{
25215 hashitem_T *hi;
25216
25217 /* Need to start all over every time, because func_free() may change the
25218 * hash table. */
25219 while (func_hashtab.ht_used > 0)
25220 for (hi = func_hashtab.ht_array; ; ++hi)
25221 if (!HASHITEM_EMPTY(hi))
25222 {
25223 func_free(HI2UF(hi));
25224 break;
25225 }
25226}
25227#endif
25228
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025229 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025230translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025231{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025232 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025233 return find_internal_func(name) >= 0;
25234 return find_func(name) != NULL;
25235}
25236
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025237/*
25238 * Return TRUE if a function "name" exists.
25239 */
25240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025241function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025242{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025243 char_u *nm = name;
25244 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025245 int n = FALSE;
25246
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025247 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025248 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025249 nm = skipwhite(nm);
25250
25251 /* Only accept "funcname", "funcname ", "funcname (..." and
25252 * "funcname(...", not "funcname!...". */
25253 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025254 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025255 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025256 return n;
25257}
25258
Bram Moolenaara1544c02013-05-30 12:35:52 +020025259 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025260get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025261{
25262 char_u *nm = name;
25263 char_u *p;
25264
Bram Moolenaar65639032016-03-16 21:40:30 +010025265 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025266
25267 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025268 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025269 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025270
Bram Moolenaara1544c02013-05-30 12:35:52 +020025271 vim_free(p);
25272 return NULL;
25273}
25274
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025275/*
25276 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025277 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25278 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025279 */
25280 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025281builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025282{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025283 char_u *p;
25284
25285 if (!ASCII_ISLOWER(name[0]))
25286 return FALSE;
25287 p = vim_strchr(name, AUTOLOAD_CHAR);
25288 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025289}
25290
Bram Moolenaar05159a02005-02-26 23:04:13 +000025291#if defined(FEAT_PROFILE) || defined(PROTO)
25292/*
25293 * Start profiling function "fp".
25294 */
25295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025296func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025297{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025298 int len = fp->uf_lines.ga_len;
25299
25300 if (len == 0)
25301 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025302 fp->uf_tm_count = 0;
25303 profile_zero(&fp->uf_tm_self);
25304 profile_zero(&fp->uf_tm_total);
25305 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025306 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025307 if (fp->uf_tml_total == NULL)
25308 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025309 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025310 if (fp->uf_tml_self == NULL)
25311 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025312 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025313 fp->uf_tml_idx = -1;
25314 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25315 || fp->uf_tml_self == NULL)
25316 return; /* out of memory */
25317
25318 fp->uf_profiling = TRUE;
25319}
25320
25321/*
25322 * Dump the profiling results for all functions in file "fd".
25323 */
25324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025325func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025326{
25327 hashitem_T *hi;
25328 int todo;
25329 ufunc_T *fp;
25330 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025331 ufunc_T **sorttab;
25332 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025333
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025334 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025335 if (todo == 0)
25336 return; /* nothing to dump */
25337
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025338 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025339
Bram Moolenaar05159a02005-02-26 23:04:13 +000025340 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25341 {
25342 if (!HASHITEM_EMPTY(hi))
25343 {
25344 --todo;
25345 fp = HI2UF(hi);
25346 if (fp->uf_profiling)
25347 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025348 if (sorttab != NULL)
25349 sorttab[st_len++] = fp;
25350
Bram Moolenaar05159a02005-02-26 23:04:13 +000025351 if (fp->uf_name[0] == K_SPECIAL)
25352 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25353 else
25354 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25355 if (fp->uf_tm_count == 1)
25356 fprintf(fd, "Called 1 time\n");
25357 else
25358 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25359 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25360 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25361 fprintf(fd, "\n");
25362 fprintf(fd, "count total (s) self (s)\n");
25363
25364 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25365 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025366 if (FUNCLINE(fp, i) == NULL)
25367 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025368 prof_func_line(fd, fp->uf_tml_count[i],
25369 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025370 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25371 }
25372 fprintf(fd, "\n");
25373 }
25374 }
25375 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025376
25377 if (sorttab != NULL && st_len > 0)
25378 {
25379 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25380 prof_total_cmp);
25381 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25382 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25383 prof_self_cmp);
25384 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25385 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025386
25387 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025388}
Bram Moolenaar73830342005-02-28 22:48:19 +000025389
25390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025391prof_sort_list(
25392 FILE *fd,
25393 ufunc_T **sorttab,
25394 int st_len,
25395 char *title,
25396 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025397{
25398 int i;
25399 ufunc_T *fp;
25400
25401 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25402 fprintf(fd, "count total (s) self (s) function\n");
25403 for (i = 0; i < 20 && i < st_len; ++i)
25404 {
25405 fp = sorttab[i];
25406 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25407 prefer_self);
25408 if (fp->uf_name[0] == K_SPECIAL)
25409 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25410 else
25411 fprintf(fd, " %s()\n", fp->uf_name);
25412 }
25413 fprintf(fd, "\n");
25414}
25415
25416/*
25417 * Print the count and times for one function or function line.
25418 */
25419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025420prof_func_line(
25421 FILE *fd,
25422 int count,
25423 proftime_T *total,
25424 proftime_T *self,
25425 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025426{
25427 if (count > 0)
25428 {
25429 fprintf(fd, "%5d ", count);
25430 if (prefer_self && profile_equal(total, self))
25431 fprintf(fd, " ");
25432 else
25433 fprintf(fd, "%s ", profile_msg(total));
25434 if (!prefer_self && profile_equal(total, self))
25435 fprintf(fd, " ");
25436 else
25437 fprintf(fd, "%s ", profile_msg(self));
25438 }
25439 else
25440 fprintf(fd, " ");
25441}
25442
25443/*
25444 * Compare function for total time sorting.
25445 */
25446 static int
25447#ifdef __BORLANDC__
25448_RTLENTRYF
25449#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025450prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025451{
25452 ufunc_T *p1, *p2;
25453
25454 p1 = *(ufunc_T **)s1;
25455 p2 = *(ufunc_T **)s2;
25456 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25457}
25458
25459/*
25460 * Compare function for self time sorting.
25461 */
25462 static int
25463#ifdef __BORLANDC__
25464_RTLENTRYF
25465#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025466prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025467{
25468 ufunc_T *p1, *p2;
25469
25470 p1 = *(ufunc_T **)s1;
25471 p2 = *(ufunc_T **)s2;
25472 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25473}
25474
Bram Moolenaar05159a02005-02-26 23:04:13 +000025475#endif
25476
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025477/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025478 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025479 * Return TRUE if a package was loaded.
25480 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025482script_autoload(
25483 char_u *name,
25484 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025485{
25486 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025487 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025488 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025489 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025490
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025491 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025492 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025493 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025494 return FALSE;
25495
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025496 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025497
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025498 /* Find the name in the list of previously loaded package names. Skip
25499 * "autoload/", it's always the same. */
25500 for (i = 0; i < ga_loaded.ga_len; ++i)
25501 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25502 break;
25503 if (!reload && i < ga_loaded.ga_len)
25504 ret = FALSE; /* was loaded already */
25505 else
25506 {
25507 /* Remember the name if it wasn't loaded already. */
25508 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25509 {
25510 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25511 tofree = NULL;
25512 }
25513
25514 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025515 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025516 ret = TRUE;
25517 }
25518
25519 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025520 return ret;
25521}
25522
25523/*
25524 * Return the autoload script name for a function or variable name.
25525 * Returns NULL when out of memory.
25526 */
25527 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025528autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025529{
25530 char_u *p;
25531 char_u *scriptname;
25532
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025533 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025534 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25535 if (scriptname == NULL)
25536 return FALSE;
25537 STRCPY(scriptname, "autoload/");
25538 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025539 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025540 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025541 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025542 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025543 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025544}
25545
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25547
25548/*
25549 * Function given to ExpandGeneric() to obtain the list of user defined
25550 * function names.
25551 */
25552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025553get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025554{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025555 static long_u done;
25556 static hashitem_T *hi;
25557 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558
25559 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025560 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025561 done = 0;
25562 hi = func_hashtab.ht_array;
25563 }
25564 if (done < func_hashtab.ht_used)
25565 {
25566 if (done++ > 0)
25567 ++hi;
25568 while (HASHITEM_EMPTY(hi))
25569 ++hi;
25570 fp = HI2UF(hi);
25571
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025572 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025573 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025574
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025575 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25576 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577
25578 cat_func_name(IObuff, fp);
25579 if (xp->xp_context != EXPAND_USER_FUNC)
25580 {
25581 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025582 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025583 STRCAT(IObuff, ")");
25584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585 return IObuff;
25586 }
25587 return NULL;
25588}
25589
25590#endif /* FEAT_CMDL_COMPL */
25591
25592/*
25593 * Copy the function name of "fp" to buffer "buf".
25594 * "buf" must be able to hold the function name plus three bytes.
25595 * Takes care of script-local function names.
25596 */
25597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025598cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025600 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601 {
25602 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025603 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025604 }
25605 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025606 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607}
25608
25609/*
25610 * ":delfunction {name}"
25611 */
25612 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025613ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025615 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616 char_u *p;
25617 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025618 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619
25620 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025621 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025622 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025624 {
25625 if (fudi.fd_dict != NULL && !eap->skip)
25626 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025629 if (!ends_excmd(*skipwhite(p)))
25630 {
25631 vim_free(name);
25632 EMSG(_(e_trailing));
25633 return;
25634 }
25635 eap->nextcmd = check_nextcmd(p);
25636 if (eap->nextcmd != NULL)
25637 *p = NUL;
25638
25639 if (!eap->skip)
25640 fp = find_func(name);
25641 vim_free(name);
25642
25643 if (!eap->skip)
25644 {
25645 if (fp == NULL)
25646 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025647 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 return;
25649 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025650 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651 {
25652 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25653 return;
25654 }
25655
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025656 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025658 /* Delete the dict item that refers to the function, it will
25659 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025660 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025662 else
25663 func_free(fp);
25664 }
25665}
25666
25667/*
25668 * Free a function and remove it from the list of functions.
25669 */
25670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025671func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025672{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025673 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025674
25675 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025676 ga_clear_strings(&(fp->uf_args));
25677 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025678#ifdef FEAT_PROFILE
25679 vim_free(fp->uf_tml_count);
25680 vim_free(fp->uf_tml_total);
25681 vim_free(fp->uf_tml_self);
25682#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025683
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025684 /* remove the function from the function hashtable */
25685 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25686 if (HASHITEM_EMPTY(hi))
25687 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025688 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025689 hash_remove(&func_hashtab, hi);
25690
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025691 vim_free(fp);
25692}
25693
25694/*
25695 * Unreference a Function: decrement the reference count and free it when it
25696 * becomes zero. Only for numbered functions.
25697 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025699func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025700{
25701 ufunc_T *fp;
25702
25703 if (name != NULL && isdigit(*name))
25704 {
25705 fp = find_func(name);
25706 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025707 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025708#ifdef EXITFREE
25709 if (!entered_free_all_mem)
25710#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025711 EMSG2(_(e_intern2), "func_unref()");
25712 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025713 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025714 {
25715 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025716 * when "uf_calls" becomes zero. */
25717 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025718 func_free(fp);
25719 }
25720 }
25721}
25722
25723/*
25724 * Count a reference to a Function.
25725 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025727func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025728{
25729 ufunc_T *fp;
25730
25731 if (name != NULL && isdigit(*name))
25732 {
25733 fp = find_func(name);
25734 if (fp == NULL)
25735 EMSG2(_(e_intern2), "func_ref()");
25736 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025737 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 }
25739}
25740
25741/*
25742 * Call a user function.
25743 */
25744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025745call_user_func(
25746 ufunc_T *fp, /* pointer to function */
25747 int argcount, /* nr of args */
25748 typval_T *argvars, /* arguments */
25749 typval_T *rettv, /* return value */
25750 linenr_T firstline, /* first line of range */
25751 linenr_T lastline, /* last line of range */
25752 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753{
Bram Moolenaar33570922005-01-25 22:26:29 +000025754 char_u *save_sourcing_name;
25755 linenr_T save_sourcing_lnum;
25756 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025757 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025758 int save_did_emsg;
25759 static int depth = 0;
25760 dictitem_T *v;
25761 int fixvar_idx = 0; /* index in fixvar[] */
25762 int i;
25763 int ai;
25764 char_u numbuf[NUMBUFLEN];
25765 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025766 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025767#ifdef FEAT_PROFILE
25768 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025769 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771
25772 /* If depth of calling is getting too high, don't execute the function */
25773 if (depth >= p_mfd)
25774 {
25775 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025776 rettv->v_type = VAR_NUMBER;
25777 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778 return;
25779 }
25780 ++depth;
25781
25782 line_breakcheck(); /* check for CTRL-C hit */
25783
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025784 fc = (funccall_T *)alloc(sizeof(funccall_T));
25785 fc->caller = current_funccal;
25786 current_funccal = fc;
25787 fc->func = fp;
25788 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025789 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025790 fc->linenr = 0;
25791 fc->returned = FALSE;
25792 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025794 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25795 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796
Bram Moolenaar33570922005-01-25 22:26:29 +000025797 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025798 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025799 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25800 * each argument variable and saves a lot of time.
25801 */
25802 /*
25803 * Init l: variables.
25804 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025805 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025806 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025807 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025808 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25809 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025810 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025811 name = v->di_key;
25812 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025813 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025814 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025815 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025816 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025817 v->di_tv.vval.v_dict = selfdict;
25818 ++selfdict->dv_refcount;
25819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025820
Bram Moolenaar33570922005-01-25 22:26:29 +000025821 /*
25822 * Init a: variables.
25823 * Set a:0 to "argcount".
25824 * Set a:000 to a list with room for the "..." arguments.
25825 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025826 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025827 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025828 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025829 /* Use "name" to avoid a warning from some compiler that checks the
25830 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025831 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025832 name = v->di_key;
25833 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025834 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025835 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025836 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025837 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025838 v->di_tv.vval.v_list = &fc->l_varlist;
25839 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25840 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25841 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025842
25843 /*
25844 * Set a:firstline to "firstline" and a:lastline to "lastline".
25845 * Set a:name to named arguments.
25846 * Set a:N to the "..." arguments.
25847 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025848 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025849 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025850 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025851 (varnumber_T)lastline);
25852 for (i = 0; i < argcount; ++i)
25853 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025854 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025855 if (ai < 0)
25856 /* named argument a:name */
25857 name = FUNCARG(fp, i);
25858 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025859 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025860 /* "..." argument a:1, a:2, etc. */
25861 sprintf((char *)numbuf, "%d", ai + 1);
25862 name = numbuf;
25863 }
25864 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25865 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025866 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025867 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25868 }
25869 else
25870 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025871 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25872 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025873 if (v == NULL)
25874 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025875 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025876 }
25877 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025878 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025879
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025880 /* Note: the values are copied directly to avoid alloc/free.
25881 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025882 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025883 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025884
25885 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25886 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025887 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25888 fc->l_listitems[ai].li_tv = argvars[i];
25889 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025890 }
25891 }
25892
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893 /* Don't redraw while executing the function. */
25894 ++RedrawingDisabled;
25895 save_sourcing_name = sourcing_name;
25896 save_sourcing_lnum = sourcing_lnum;
25897 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025898 /* need space for function name + ("function " + 3) or "[number]" */
25899 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25900 + STRLEN(fp->uf_name) + 20;
25901 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 if (sourcing_name != NULL)
25903 {
25904 if (save_sourcing_name != NULL
25905 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025906 sprintf((char *)sourcing_name, "%s[%d]..",
25907 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908 else
25909 STRCPY(sourcing_name, "function ");
25910 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25911
25912 if (p_verbose >= 12)
25913 {
25914 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025915 verbose_enter_scroll();
25916
Bram Moolenaar555b2802005-05-19 21:08:39 +000025917 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025918 if (p_verbose >= 14)
25919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025920 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025921 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025922 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025923 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025924
25925 msg_puts((char_u *)"(");
25926 for (i = 0; i < argcount; ++i)
25927 {
25928 if (i > 0)
25929 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025930 if (argvars[i].v_type == VAR_NUMBER)
25931 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025932 else
25933 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025934 /* Do not want errors such as E724 here. */
25935 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025936 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025937 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025938 if (s != NULL)
25939 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025940 if (vim_strsize(s) > MSG_BUF_CLEN)
25941 {
25942 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25943 s = buf;
25944 }
25945 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025946 vim_free(tofree);
25947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948 }
25949 }
25950 msg_puts((char_u *)")");
25951 }
25952 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025953
25954 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025955 --no_wait_return;
25956 }
25957 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025958#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025959 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025960 {
25961 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25962 func_do_profile(fp);
25963 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025964 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025965 {
25966 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025967 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025968 profile_zero(&fp->uf_tm_children);
25969 }
25970 script_prof_save(&wait_start);
25971 }
25972#endif
25973
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025975 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976 save_did_emsg = did_emsg;
25977 did_emsg = FALSE;
25978
25979 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025980 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025981 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25982
25983 --RedrawingDisabled;
25984
25985 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025986 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025988 clear_tv(rettv);
25989 rettv->v_type = VAR_NUMBER;
25990 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991 }
25992
Bram Moolenaar05159a02005-02-26 23:04:13 +000025993#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025994 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025995 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025996 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025997 profile_end(&call_start);
25998 profile_sub_wait(&wait_start, &call_start);
25999 profile_add(&fp->uf_tm_total, &call_start);
26000 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026001 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026002 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026003 profile_add(&fc->caller->func->uf_tm_children, &call_start);
26004 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026005 }
26006 }
26007#endif
26008
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009 /* when being verbose, mention the return value */
26010 if (p_verbose >= 12)
26011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026013 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000026016 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026017 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000026018 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026019 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000026020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000026021 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000026022 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000026023 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000026024 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026025 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000026026
Bram Moolenaar555b2802005-05-19 21:08:39 +000026027 /* The value may be very long. Skip the middle part, so that we
26028 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020026029 * truncate it at the end. Don't want errors such as E724 here. */
26030 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026031 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020026032 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026033 if (s != NULL)
26034 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010026035 if (vim_strsize(s) > MSG_BUF_CLEN)
26036 {
26037 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
26038 s = buf;
26039 }
26040 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026041 vim_free(tofree);
26042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043 }
26044 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026045
26046 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026047 --no_wait_return;
26048 }
26049
26050 vim_free(sourcing_name);
26051 sourcing_name = save_sourcing_name;
26052 sourcing_lnum = save_sourcing_lnum;
26053 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026054#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026055 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026056 script_prof_restore(&wait_start);
26057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058
26059 if (p_verbose >= 12 && sourcing_name != NULL)
26060 {
26061 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026062 verbose_enter_scroll();
26063
Bram Moolenaar555b2802005-05-19 21:08:39 +000026064 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026066
26067 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068 --no_wait_return;
26069 }
26070
26071 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026072 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026073 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026074
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026075 /* If the a:000 list and the l: and a: dicts are not referenced we can
26076 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026077 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
26078 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
26079 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
26080 {
26081 free_funccal(fc, FALSE);
26082 }
26083 else
26084 {
26085 hashitem_T *hi;
26086 listitem_T *li;
26087 int todo;
26088
26089 /* "fc" is still in use. This can happen when returning "a:000" or
26090 * assigning "l:" to a global variable.
26091 * Link "fc" in the list for garbage collection later. */
26092 fc->caller = previous_funccal;
26093 previous_funccal = fc;
26094
26095 /* Make a copy of the a: variables, since we didn't do that above. */
26096 todo = (int)fc->l_avars.dv_hashtab.ht_used;
26097 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
26098 {
26099 if (!HASHITEM_EMPTY(hi))
26100 {
26101 --todo;
26102 v = HI2DI(hi);
26103 copy_tv(&v->di_tv, &v->di_tv);
26104 }
26105 }
26106
26107 /* Make a copy of the a:000 items, since we didn't do that above. */
26108 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26109 copy_tv(&li->li_tv, &li->li_tv);
26110 }
26111}
26112
26113/*
26114 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026115 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026116 */
26117 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026118can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026119{
26120 return (fc->l_varlist.lv_copyID != copyID
26121 && fc->l_vars.dv_copyID != copyID
26122 && fc->l_avars.dv_copyID != copyID);
26123}
26124
26125/*
26126 * Free "fc" and what it contains.
26127 */
26128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026129free_funccal(
26130 funccall_T *fc,
26131 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026132{
26133 listitem_T *li;
26134
26135 /* The a: variables typevals may not have been allocated, only free the
26136 * allocated variables. */
26137 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
26138
26139 /* free all l: variables */
26140 vars_clear(&fc->l_vars.dv_hashtab);
26141
26142 /* Free the a:000 variables if they were allocated. */
26143 if (free_val)
26144 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26145 clear_tv(&li->li_tv);
26146
26147 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026148}
26149
26150/*
Bram Moolenaar33570922005-01-25 22:26:29 +000026151 * Add a number variable "name" to dict "dp" with value "nr".
26152 */
26153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026154add_nr_var(
26155 dict_T *dp,
26156 dictitem_T *v,
26157 char *name,
26158 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000026159{
26160 STRCPY(v->di_key, name);
26161 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
26162 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
26163 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000026164 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000026165 v->di_tv.vval.v_number = nr;
26166}
26167
26168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169 * ":return [expr]"
26170 */
26171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026172ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026173{
26174 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000026175 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026176 int returning = FALSE;
26177
26178 if (current_funccal == NULL)
26179 {
26180 EMSG(_("E133: :return not inside a function"));
26181 return;
26182 }
26183
26184 if (eap->skip)
26185 ++emsg_skip;
26186
26187 eap->nextcmd = NULL;
26188 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026189 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190 {
26191 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026192 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026194 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026195 }
26196 /* It's safer to return also on error. */
26197 else if (!eap->skip)
26198 {
26199 /*
26200 * Return unless the expression evaluation has been cancelled due to an
26201 * aborting error, an interrupt, or an exception.
26202 */
26203 if (!aborting())
26204 returning = do_return(eap, FALSE, TRUE, NULL);
26205 }
26206
26207 /* When skipping or the return gets pending, advance to the next command
26208 * in this line (!returning). Otherwise, ignore the rest of the line.
26209 * Following lines will be ignored by get_func_line(). */
26210 if (returning)
26211 eap->nextcmd = NULL;
26212 else if (eap->nextcmd == NULL) /* no argument */
26213 eap->nextcmd = check_nextcmd(arg);
26214
26215 if (eap->skip)
26216 --emsg_skip;
26217}
26218
26219/*
26220 * Return from a function. Possibly makes the return pending. Also called
26221 * for a pending return at the ":endtry" or after returning from an extra
26222 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026223 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026224 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225 * FALSE when the return gets pending.
26226 */
26227 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026228do_return(
26229 exarg_T *eap,
26230 int reanimate,
26231 int is_cmd,
26232 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026233{
26234 int idx;
26235 struct condstack *cstack = eap->cstack;
26236
26237 if (reanimate)
26238 /* Undo the return. */
26239 current_funccal->returned = FALSE;
26240
26241 /*
26242 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26243 * not in its finally clause (which then is to be executed next) is found.
26244 * In this case, make the ":return" pending for execution at the ":endtry".
26245 * Otherwise, return normally.
26246 */
26247 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26248 if (idx >= 0)
26249 {
26250 cstack->cs_pending[idx] = CSTP_RETURN;
26251
26252 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026253 /* A pending return again gets pending. "rettv" points to an
26254 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026256 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026257 else
26258 {
26259 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026260 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026261 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026262 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026264 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026265 {
26266 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026267 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026268 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269 else
26270 EMSG(_(e_outofmem));
26271 }
26272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026273 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026274
26275 if (reanimate)
26276 {
26277 /* The pending return value could be overwritten by a ":return"
26278 * without argument in a finally clause; reset the default
26279 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026280 current_funccal->rettv->v_type = VAR_NUMBER;
26281 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026282 }
26283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026284 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026285 }
26286 else
26287 {
26288 current_funccal->returned = TRUE;
26289
26290 /* If the return is carried out now, store the return value. For
26291 * a return immediately after reanimation, the value is already
26292 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026293 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026295 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026296 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026298 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026299 }
26300 }
26301
26302 return idx < 0;
26303}
26304
26305/*
26306 * Free the variable with a pending return value.
26307 */
26308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026309discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310{
Bram Moolenaar33570922005-01-25 22:26:29 +000026311 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026312}
26313
26314/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026315 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026316 * is an allocated string. Used by report_pending() for verbose messages.
26317 */
26318 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026319get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026320{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026321 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026322 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026323 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026324
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026325 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026326 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026327 if (s == NULL)
26328 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026329
26330 STRCPY(IObuff, ":return ");
26331 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26332 if (STRLEN(s) + 8 >= IOSIZE)
26333 STRCPY(IObuff + IOSIZE - 4, "...");
26334 vim_free(tofree);
26335 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026336}
26337
26338/*
26339 * Get next function line.
26340 * Called by do_cmdline() to get the next line.
26341 * Returns allocated string, or NULL for end of function.
26342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026343 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026344get_func_line(
26345 int c UNUSED,
26346 void *cookie,
26347 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026348{
Bram Moolenaar33570922005-01-25 22:26:29 +000026349 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026350 ufunc_T *fp = fcp->func;
26351 char_u *retval;
26352 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353
26354 /* If breakpoints have been added/deleted need to check for it. */
26355 if (fcp->dbg_tick != debug_tick)
26356 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026357 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026358 sourcing_lnum);
26359 fcp->dbg_tick = debug_tick;
26360 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026361#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026362 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026363 func_line_end(cookie);
26364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026365
Bram Moolenaar05159a02005-02-26 23:04:13 +000026366 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026367 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26368 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369 retval = NULL;
26370 else
26371 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026372 /* Skip NULL lines (continuation lines). */
26373 while (fcp->linenr < gap->ga_len
26374 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26375 ++fcp->linenr;
26376 if (fcp->linenr >= gap->ga_len)
26377 retval = NULL;
26378 else
26379 {
26380 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26381 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026382#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026383 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026384 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026385#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026387 }
26388
26389 /* Did we encounter a breakpoint? */
26390 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26391 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026392 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026393 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026394 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026395 sourcing_lnum);
26396 fcp->dbg_tick = debug_tick;
26397 }
26398
26399 return retval;
26400}
26401
Bram Moolenaar05159a02005-02-26 23:04:13 +000026402#if defined(FEAT_PROFILE) || defined(PROTO)
26403/*
26404 * Called when starting to read a function line.
26405 * "sourcing_lnum" must be correct!
26406 * When skipping lines it may not actually be executed, but we won't find out
26407 * until later and we need to store the time now.
26408 */
26409 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026410func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026411{
26412 funccall_T *fcp = (funccall_T *)cookie;
26413 ufunc_T *fp = fcp->func;
26414
26415 if (fp->uf_profiling && sourcing_lnum >= 1
26416 && sourcing_lnum <= fp->uf_lines.ga_len)
26417 {
26418 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026419 /* Skip continuation lines. */
26420 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26421 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026422 fp->uf_tml_execed = FALSE;
26423 profile_start(&fp->uf_tml_start);
26424 profile_zero(&fp->uf_tml_children);
26425 profile_get_wait(&fp->uf_tml_wait);
26426 }
26427}
26428
26429/*
26430 * Called when actually executing a function line.
26431 */
26432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026433func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026434{
26435 funccall_T *fcp = (funccall_T *)cookie;
26436 ufunc_T *fp = fcp->func;
26437
26438 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26439 fp->uf_tml_execed = TRUE;
26440}
26441
26442/*
26443 * Called when done with a function line.
26444 */
26445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026446func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026447{
26448 funccall_T *fcp = (funccall_T *)cookie;
26449 ufunc_T *fp = fcp->func;
26450
26451 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26452 {
26453 if (fp->uf_tml_execed)
26454 {
26455 ++fp->uf_tml_count[fp->uf_tml_idx];
26456 profile_end(&fp->uf_tml_start);
26457 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026458 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026459 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26460 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026461 }
26462 fp->uf_tml_idx = -1;
26463 }
26464}
26465#endif
26466
Bram Moolenaar071d4272004-06-13 20:20:40 +000026467/*
26468 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026469 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026470 */
26471 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026472func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026473{
Bram Moolenaar33570922005-01-25 22:26:29 +000026474 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026475
26476 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26477 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026478 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026479 || fcp->returned);
26480}
26481
26482/*
26483 * return TRUE if cookie indicates a function which "abort"s on errors.
26484 */
26485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026486func_has_abort(
26487 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026488{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026489 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026490}
26491
26492#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26493typedef enum
26494{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026495 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26496 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26497 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026498} var_flavour_T;
26499
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026500static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026501
26502 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026503var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026504{
26505 char_u *p = varname;
26506
26507 if (ASCII_ISUPPER(*p))
26508 {
26509 while (*(++p))
26510 if (ASCII_ISLOWER(*p))
26511 return VAR_FLAVOUR_SESSION;
26512 return VAR_FLAVOUR_VIMINFO;
26513 }
26514 else
26515 return VAR_FLAVOUR_DEFAULT;
26516}
26517#endif
26518
26519#if defined(FEAT_VIMINFO) || defined(PROTO)
26520/*
26521 * Restore global vars that start with a capital from the viminfo file
26522 */
26523 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026524read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026525{
26526 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026527 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026528 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026529 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026530
26531 if (!writing && (find_viminfo_parameter('!') != NULL))
26532 {
26533 tab = vim_strchr(virp->vir_line + 1, '\t');
26534 if (tab != NULL)
26535 {
26536 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026537 switch (*tab)
26538 {
26539 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026540#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026541 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026542#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026543 case 'D': type = VAR_DICT; break;
26544 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026545 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026547
26548 tab = vim_strchr(tab, '\t');
26549 if (tab != NULL)
26550 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026551 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026552 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026553 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026554 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026555#ifdef FEAT_FLOAT
26556 else if (type == VAR_FLOAT)
26557 (void)string2float(tab + 1, &tv.vval.v_float);
26558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026559 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026560 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026561 if (type == VAR_DICT || type == VAR_LIST)
26562 {
26563 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26564
26565 if (etv == NULL)
26566 /* Failed to parse back the dict or list, use it as a
26567 * string. */
26568 tv.v_type = VAR_STRING;
26569 else
26570 {
26571 vim_free(tv.vval.v_string);
26572 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026573 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026574 }
26575 }
26576
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026577 /* when in a function use global variables */
26578 save_funccal = current_funccal;
26579 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026580 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026581 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026582
26583 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026584 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026585 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26586 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026587 }
26588 }
26589 }
26590
26591 return viminfo_readline(virp);
26592}
26593
26594/*
26595 * Write global vars that start with a capital to the viminfo file
26596 */
26597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026598write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026599{
Bram Moolenaar33570922005-01-25 22:26:29 +000026600 hashitem_T *hi;
26601 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026602 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026603 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026604 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026605 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026606 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026607
26608 if (find_viminfo_parameter('!') == NULL)
26609 return;
26610
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026611 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026612
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026613 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026614 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026615 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026616 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026617 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026618 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026619 this_var = HI2DI(hi);
26620 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026621 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026622 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026623 {
26624 case VAR_STRING: s = "STR"; break;
26625 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026626 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026627 case VAR_DICT: s = "DIC"; break;
26628 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026629 case VAR_SPECIAL: s = "XPL"; break;
26630
26631 case VAR_UNKNOWN:
26632 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026633 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026634 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026635 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026636 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026637 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026638 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026639 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026640 if (p != NULL)
26641 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026642 vim_free(tofree);
26643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026644 }
26645 }
26646}
26647#endif
26648
26649#if defined(FEAT_SESSION) || defined(PROTO)
26650 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026651store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026652{
Bram Moolenaar33570922005-01-25 22:26:29 +000026653 hashitem_T *hi;
26654 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026655 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026656 char_u *p, *t;
26657
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026658 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026659 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026660 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026661 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026662 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026663 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026664 this_var = HI2DI(hi);
26665 if ((this_var->di_tv.v_type == VAR_NUMBER
26666 || this_var->di_tv.v_type == VAR_STRING)
26667 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026668 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026669 /* Escape special characters with a backslash. Turn a LF and
26670 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026671 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026672 (char_u *)"\\\"\n\r");
26673 if (p == NULL) /* out of memory */
26674 break;
26675 for (t = p; *t != NUL; ++t)
26676 if (*t == '\n')
26677 *t = 'n';
26678 else if (*t == '\r')
26679 *t = 'r';
26680 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026681 this_var->di_key,
26682 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26683 : ' ',
26684 p,
26685 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26686 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026687 || put_eol(fd) == FAIL)
26688 {
26689 vim_free(p);
26690 return FAIL;
26691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026692 vim_free(p);
26693 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026694#ifdef FEAT_FLOAT
26695 else if (this_var->di_tv.v_type == VAR_FLOAT
26696 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26697 {
26698 float_T f = this_var->di_tv.vval.v_float;
26699 int sign = ' ';
26700
26701 if (f < 0)
26702 {
26703 f = -f;
26704 sign = '-';
26705 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026706 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026707 this_var->di_key, sign, f) < 0)
26708 || put_eol(fd) == FAIL)
26709 return FAIL;
26710 }
26711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026712 }
26713 }
26714 return OK;
26715}
26716#endif
26717
Bram Moolenaar661b1822005-07-28 22:36:45 +000026718/*
26719 * Display script name where an item was last set.
26720 * Should only be invoked when 'verbose' is non-zero.
26721 */
26722 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026723last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026724{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026725 char_u *p;
26726
Bram Moolenaar661b1822005-07-28 22:36:45 +000026727 if (scriptID != 0)
26728 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026729 p = home_replace_save(NULL, get_scriptname(scriptID));
26730 if (p != NULL)
26731 {
26732 verbose_enter();
26733 MSG_PUTS(_("\n\tLast set from "));
26734 MSG_PUTS(p);
26735 vim_free(p);
26736 verbose_leave();
26737 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026738 }
26739}
26740
Bram Moolenaard812df62008-11-09 12:46:09 +000026741/*
26742 * List v:oldfiles in a nice way.
26743 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026744 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026745ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026746{
26747 list_T *l = vimvars[VV_OLDFILES].vv_list;
26748 listitem_T *li;
26749 int nr = 0;
26750
26751 if (l == NULL)
26752 msg((char_u *)_("No old files"));
26753 else
26754 {
26755 msg_start();
26756 msg_scroll = TRUE;
26757 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26758 {
26759 msg_outnum((long)++nr);
26760 MSG_PUTS(": ");
26761 msg_outtrans(get_tv_string(&li->li_tv));
26762 msg_putchar('\n');
26763 out_flush(); /* output one line at a time */
26764 ui_breakcheck();
26765 }
26766 /* Assume "got_int" was set to truncate the listing. */
26767 got_int = FALSE;
26768
26769#ifdef FEAT_BROWSE_CMD
26770 if (cmdmod.browse)
26771 {
26772 quit_more = FALSE;
26773 nr = prompt_for_number(FALSE);
26774 msg_starthere();
26775 if (nr > 0)
26776 {
26777 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26778 (long)nr);
26779
26780 if (p != NULL)
26781 {
26782 p = expand_env_save(p);
26783 eap->arg = p;
26784 eap->cmdidx = CMD_edit;
26785 cmdmod.browse = FALSE;
26786 do_exedit(eap, NULL);
26787 vim_free(p);
26788 }
26789 }
26790 }
26791#endif
26792 }
26793}
26794
Bram Moolenaar53744302015-07-17 17:38:22 +020026795/* reset v:option_new, v:option_old and v:option_type */
26796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026797reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026798{
26799 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26800 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26801 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26802}
26803
26804
Bram Moolenaar071d4272004-06-13 20:20:40 +000026805#endif /* FEAT_EVAL */
26806
Bram Moolenaar071d4272004-06-13 20:20:40 +000026807
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026808#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026809
26810#ifdef WIN3264
26811/*
26812 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26813 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026814static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26815static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26816static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026817
26818/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026819 * Get the short path (8.3) for the filename in "fnamep".
26820 * Only works for a valid file name.
26821 * When the path gets longer "fnamep" is changed and the allocated buffer
26822 * is put in "bufp".
26823 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26824 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026825 */
26826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026827get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026828{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026829 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026830 char_u *newbuf;
26831
26832 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026833 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026834 if (l > len - 1)
26835 {
26836 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026837 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026838 newbuf = vim_strnsave(*fnamep, l);
26839 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026840 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026841
26842 vim_free(*bufp);
26843 *fnamep = *bufp = newbuf;
26844
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026845 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026846 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026847 }
26848
26849 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026850 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026851}
26852
26853/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026854 * Get the short path (8.3) for the filename in "fname". The converted
26855 * path is returned in "bufp".
26856 *
26857 * Some of the directories specified in "fname" may not exist. This function
26858 * will shorten the existing directories at the beginning of the path and then
26859 * append the remaining non-existing path.
26860 *
26861 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026862 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026863 * bufp - Pointer to an allocated buffer for the filename.
26864 * fnamelen - Length of the filename pointed to by fname
26865 *
26866 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026867 */
26868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026869shortpath_for_invalid_fname(
26870 char_u **fname,
26871 char_u **bufp,
26872 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026873{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026874 char_u *short_fname, *save_fname, *pbuf_unused;
26875 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026876 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026877 int old_len, len;
26878 int new_len, sfx_len;
26879 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026880
26881 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026882 old_len = *fnamelen;
26883 save_fname = vim_strnsave(*fname, old_len);
26884 pbuf_unused = NULL;
26885 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026886
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026887 endp = save_fname + old_len - 1; /* Find the end of the copy */
26888 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026889
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026890 /*
26891 * Try shortening the supplied path till it succeeds by removing one
26892 * directory at a time from the tail of the path.
26893 */
26894 len = 0;
26895 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026896 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026897 /* go back one path-separator */
26898 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26899 --endp;
26900 if (endp <= save_fname)
26901 break; /* processed the complete path */
26902
26903 /*
26904 * Replace the path separator with a NUL and try to shorten the
26905 * resulting path.
26906 */
26907 ch = *endp;
26908 *endp = 0;
26909 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026910 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026911 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26912 {
26913 retval = FAIL;
26914 goto theend;
26915 }
26916 *endp = ch; /* preserve the string */
26917
26918 if (len > 0)
26919 break; /* successfully shortened the path */
26920
26921 /* failed to shorten the path. Skip the path separator */
26922 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026923 }
26924
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026925 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026926 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026927 /*
26928 * Succeeded in shortening the path. Now concatenate the shortened
26929 * path with the remaining path at the tail.
26930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026931
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026932 /* Compute the length of the new path. */
26933 sfx_len = (int)(save_endp - endp) + 1;
26934 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026935
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026936 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026937 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026938 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026939 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026940 /* There is not enough space in the currently allocated string,
26941 * copy it to a buffer big enough. */
26942 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026943 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026944 {
26945 retval = FAIL;
26946 goto theend;
26947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026948 }
26949 else
26950 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026951 /* Transfer short_fname to the main buffer (it's big enough),
26952 * unless get_short_pathname() did its work in-place. */
26953 *fname = *bufp = save_fname;
26954 if (short_fname != save_fname)
26955 vim_strncpy(save_fname, short_fname, len);
26956 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026957 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026958
26959 /* concat the not-shortened part of the path */
26960 vim_strncpy(*fname + len, endp, sfx_len);
26961 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026962 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026963
26964theend:
26965 vim_free(pbuf_unused);
26966 vim_free(save_fname);
26967
26968 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026969}
26970
26971/*
26972 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026973 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026974 */
26975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026976shortpath_for_partial(
26977 char_u **fnamep,
26978 char_u **bufp,
26979 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026980{
26981 int sepcount, len, tflen;
26982 char_u *p;
26983 char_u *pbuf, *tfname;
26984 int hasTilde;
26985
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026986 /* Count up the path separators from the RHS.. so we know which part
26987 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026988 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026989 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026990 if (vim_ispathsep(*p))
26991 ++sepcount;
26992
26993 /* Need full path first (use expand_env() to remove a "~/") */
26994 hasTilde = (**fnamep == '~');
26995 if (hasTilde)
26996 pbuf = tfname = expand_env_save(*fnamep);
26997 else
26998 pbuf = tfname = FullName_save(*fnamep, FALSE);
26999
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000027000 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027001
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027002 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
27003 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027004
27005 if (len == 0)
27006 {
27007 /* Don't have a valid filename, so shorten the rest of the
27008 * path if we can. This CAN give us invalid 8.3 filenames, but
27009 * there's not a lot of point in guessing what it might be.
27010 */
27011 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027012 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
27013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027014 }
27015
27016 /* Count the paths backward to find the beginning of the desired string. */
27017 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027018 {
27019#ifdef FEAT_MBYTE
27020 if (has_mbyte)
27021 p -= mb_head_off(tfname, p);
27022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027023 if (vim_ispathsep(*p))
27024 {
27025 if (sepcount == 0 || (hasTilde && sepcount == 1))
27026 break;
27027 else
27028 sepcount --;
27029 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027031 if (hasTilde)
27032 {
27033 --p;
27034 if (p >= tfname)
27035 *p = '~';
27036 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027037 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027038 }
27039 else
27040 ++p;
27041
27042 /* Copy in the string - p indexes into tfname - allocated at pbuf */
27043 vim_free(*bufp);
27044 *fnamelen = (int)STRLEN(p);
27045 *bufp = pbuf;
27046 *fnamep = p;
27047
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027048 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027049}
27050#endif /* WIN3264 */
27051
27052/*
27053 * Adjust a filename, according to a string of modifiers.
27054 * *fnamep must be NUL terminated when called. When returning, the length is
27055 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027056 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027057 * When there is an error, *fnamep is set to NULL.
27058 */
27059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010027060modify_fname(
27061 char_u *src, /* string with modifiers */
27062 int *usedlen, /* characters after src that are used */
27063 char_u **fnamep, /* file name so far */
27064 char_u **bufp, /* buffer for allocated file name or NULL */
27065 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027066{
27067 int valid = 0;
27068 char_u *tail;
27069 char_u *s, *p, *pbuf;
27070 char_u dirname[MAXPATHL];
27071 int c;
27072 int has_fullname = 0;
27073#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027074 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027075 int has_shortname = 0;
27076#endif
27077
27078repeat:
27079 /* ":p" - full path/file_name */
27080 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
27081 {
27082 has_fullname = 1;
27083
27084 valid |= VALID_PATH;
27085 *usedlen += 2;
27086
27087 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
27088 if ((*fnamep)[0] == '~'
27089#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
27090 && ((*fnamep)[1] == '/'
27091# ifdef BACKSLASH_IN_FILENAME
27092 || (*fnamep)[1] == '\\'
27093# endif
27094 || (*fnamep)[1] == NUL)
27095
27096#endif
27097 )
27098 {
27099 *fnamep = expand_env_save(*fnamep);
27100 vim_free(*bufp); /* free any allocated file name */
27101 *bufp = *fnamep;
27102 if (*fnamep == NULL)
27103 return -1;
27104 }
27105
27106 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027107 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000027108 {
27109 if (vim_ispathsep(*p)
27110 && p[1] == '.'
27111 && (p[2] == NUL
27112 || vim_ispathsep(p[2])
27113 || (p[2] == '.'
27114 && (p[3] == NUL || vim_ispathsep(p[3])))))
27115 break;
27116 }
27117
27118 /* FullName_save() is slow, don't use it when not needed. */
27119 if (*p != NUL || !vim_isAbsName(*fnamep))
27120 {
27121 *fnamep = FullName_save(*fnamep, *p != NUL);
27122 vim_free(*bufp); /* free any allocated file name */
27123 *bufp = *fnamep;
27124 if (*fnamep == NULL)
27125 return -1;
27126 }
27127
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027128#ifdef WIN3264
27129# if _WIN32_WINNT >= 0x0500
27130 if (vim_strchr(*fnamep, '~') != NULL)
27131 {
27132 /* Expand 8.3 filename to full path. Needed to make sure the same
27133 * file does not have two different names.
27134 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
27135 p = alloc(_MAX_PATH + 1);
27136 if (p != NULL)
27137 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010027138 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027139 {
27140 vim_free(*bufp);
27141 *bufp = *fnamep = p;
27142 }
27143 else
27144 vim_free(p);
27145 }
27146 }
27147# endif
27148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027149 /* Append a path separator to a directory. */
27150 if (mch_isdir(*fnamep))
27151 {
27152 /* Make room for one or two extra characters. */
27153 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
27154 vim_free(*bufp); /* free any allocated file name */
27155 *bufp = *fnamep;
27156 if (*fnamep == NULL)
27157 return -1;
27158 add_pathsep(*fnamep);
27159 }
27160 }
27161
27162 /* ":." - path relative to the current directory */
27163 /* ":~" - path relative to the home directory */
27164 /* ":8" - shortname path - postponed till after */
27165 while (src[*usedlen] == ':'
27166 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
27167 {
27168 *usedlen += 2;
27169 if (c == '8')
27170 {
27171#ifdef WIN3264
27172 has_shortname = 1; /* Postpone this. */
27173#endif
27174 continue;
27175 }
27176 pbuf = NULL;
27177 /* Need full path first (use expand_env() to remove a "~/") */
27178 if (!has_fullname)
27179 {
27180 if (c == '.' && **fnamep == '~')
27181 p = pbuf = expand_env_save(*fnamep);
27182 else
27183 p = pbuf = FullName_save(*fnamep, FALSE);
27184 }
27185 else
27186 p = *fnamep;
27187
27188 has_fullname = 0;
27189
27190 if (p != NULL)
27191 {
27192 if (c == '.')
27193 {
27194 mch_dirname(dirname, MAXPATHL);
27195 s = shorten_fname(p, dirname);
27196 if (s != NULL)
27197 {
27198 *fnamep = s;
27199 if (pbuf != NULL)
27200 {
27201 vim_free(*bufp); /* free any allocated file name */
27202 *bufp = pbuf;
27203 pbuf = NULL;
27204 }
27205 }
27206 }
27207 else
27208 {
27209 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27210 /* Only replace it when it starts with '~' */
27211 if (*dirname == '~')
27212 {
27213 s = vim_strsave(dirname);
27214 if (s != NULL)
27215 {
27216 *fnamep = s;
27217 vim_free(*bufp);
27218 *bufp = s;
27219 }
27220 }
27221 }
27222 vim_free(pbuf);
27223 }
27224 }
27225
27226 tail = gettail(*fnamep);
27227 *fnamelen = (int)STRLEN(*fnamep);
27228
27229 /* ":h" - head, remove "/file_name", can be repeated */
27230 /* Don't remove the first "/" or "c:\" */
27231 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27232 {
27233 valid |= VALID_HEAD;
27234 *usedlen += 2;
27235 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027236 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027237 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027238 *fnamelen = (int)(tail - *fnamep);
27239#ifdef VMS
27240 if (*fnamelen > 0)
27241 *fnamelen += 1; /* the path separator is part of the path */
27242#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027243 if (*fnamelen == 0)
27244 {
27245 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27246 p = vim_strsave((char_u *)".");
27247 if (p == NULL)
27248 return -1;
27249 vim_free(*bufp);
27250 *bufp = *fnamep = tail = p;
27251 *fnamelen = 1;
27252 }
27253 else
27254 {
27255 while (tail > s && !after_pathsep(s, tail))
27256 mb_ptr_back(*fnamep, tail);
27257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027258 }
27259
27260 /* ":8" - shortname */
27261 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27262 {
27263 *usedlen += 2;
27264#ifdef WIN3264
27265 has_shortname = 1;
27266#endif
27267 }
27268
27269#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027270 /*
27271 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027272 */
27273 if (has_shortname)
27274 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027275 /* Copy the string if it is shortened by :h and when it wasn't copied
27276 * yet, because we are going to change it in place. Avoids changing
27277 * the buffer name for "%:8". */
27278 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027279 {
27280 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027281 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027282 return -1;
27283 vim_free(*bufp);
27284 *bufp = *fnamep = p;
27285 }
27286
27287 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027288 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027289 if (!has_fullname && !vim_isAbsName(*fnamep))
27290 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027291 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027292 return -1;
27293 }
27294 else
27295 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027296 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027297
Bram Moolenaardc935552011-08-17 15:23:23 +020027298 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027299 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027300 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027301 return -1;
27302
27303 if (l == 0)
27304 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027305 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027306 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027307 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027308 return -1;
27309 }
27310 *fnamelen = l;
27311 }
27312 }
27313#endif /* WIN3264 */
27314
27315 /* ":t" - tail, just the basename */
27316 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27317 {
27318 *usedlen += 2;
27319 *fnamelen -= (int)(tail - *fnamep);
27320 *fnamep = tail;
27321 }
27322
27323 /* ":e" - extension, can be repeated */
27324 /* ":r" - root, without extension, can be repeated */
27325 while (src[*usedlen] == ':'
27326 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27327 {
27328 /* find a '.' in the tail:
27329 * - for second :e: before the current fname
27330 * - otherwise: The last '.'
27331 */
27332 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27333 s = *fnamep - 2;
27334 else
27335 s = *fnamep + *fnamelen - 1;
27336 for ( ; s > tail; --s)
27337 if (s[0] == '.')
27338 break;
27339 if (src[*usedlen + 1] == 'e') /* :e */
27340 {
27341 if (s > tail)
27342 {
27343 *fnamelen += (int)(*fnamep - (s + 1));
27344 *fnamep = s + 1;
27345#ifdef VMS
27346 /* cut version from the extension */
27347 s = *fnamep + *fnamelen - 1;
27348 for ( ; s > *fnamep; --s)
27349 if (s[0] == ';')
27350 break;
27351 if (s > *fnamep)
27352 *fnamelen = s - *fnamep;
27353#endif
27354 }
27355 else if (*fnamep <= tail)
27356 *fnamelen = 0;
27357 }
27358 else /* :r */
27359 {
27360 if (s > tail) /* remove one extension */
27361 *fnamelen = (int)(s - *fnamep);
27362 }
27363 *usedlen += 2;
27364 }
27365
27366 /* ":s?pat?foo?" - substitute */
27367 /* ":gs?pat?foo?" - global substitute */
27368 if (src[*usedlen] == ':'
27369 && (src[*usedlen + 1] == 's'
27370 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27371 {
27372 char_u *str;
27373 char_u *pat;
27374 char_u *sub;
27375 int sep;
27376 char_u *flags;
27377 int didit = FALSE;
27378
27379 flags = (char_u *)"";
27380 s = src + *usedlen + 2;
27381 if (src[*usedlen + 1] == 'g')
27382 {
27383 flags = (char_u *)"g";
27384 ++s;
27385 }
27386
27387 sep = *s++;
27388 if (sep)
27389 {
27390 /* find end of pattern */
27391 p = vim_strchr(s, sep);
27392 if (p != NULL)
27393 {
27394 pat = vim_strnsave(s, (int)(p - s));
27395 if (pat != NULL)
27396 {
27397 s = p + 1;
27398 /* find end of substitution */
27399 p = vim_strchr(s, sep);
27400 if (p != NULL)
27401 {
27402 sub = vim_strnsave(s, (int)(p - s));
27403 str = vim_strnsave(*fnamep, *fnamelen);
27404 if (sub != NULL && str != NULL)
27405 {
27406 *usedlen = (int)(p + 1 - src);
27407 s = do_string_sub(str, pat, sub, flags);
27408 if (s != NULL)
27409 {
27410 *fnamep = s;
27411 *fnamelen = (int)STRLEN(s);
27412 vim_free(*bufp);
27413 *bufp = s;
27414 didit = TRUE;
27415 }
27416 }
27417 vim_free(sub);
27418 vim_free(str);
27419 }
27420 vim_free(pat);
27421 }
27422 }
27423 /* after using ":s", repeat all the modifiers */
27424 if (didit)
27425 goto repeat;
27426 }
27427 }
27428
Bram Moolenaar26df0922014-02-23 23:39:13 +010027429 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27430 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027431 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027432 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027433 if (c != NUL)
27434 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027435 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027436 if (c != NUL)
27437 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027438 if (p == NULL)
27439 return -1;
27440 vim_free(*bufp);
27441 *bufp = *fnamep = p;
27442 *fnamelen = (int)STRLEN(p);
27443 *usedlen += 2;
27444 }
27445
Bram Moolenaar071d4272004-06-13 20:20:40 +000027446 return valid;
27447}
27448
27449/*
27450 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27451 * "flags" can be "g" to do a global substitute.
27452 * Returns an allocated string, NULL for error.
27453 */
27454 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027455do_string_sub(
27456 char_u *str,
27457 char_u *pat,
27458 char_u *sub,
27459 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027460{
27461 int sublen;
27462 regmatch_T regmatch;
27463 int i;
27464 int do_all;
27465 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027466 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027467 garray_T ga;
27468 char_u *ret;
27469 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027470 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027471
27472 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27473 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027474 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027475
27476 ga_init2(&ga, 1, 200);
27477
27478 do_all = (flags[0] == 'g');
27479
27480 regmatch.rm_ic = p_ic;
27481 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27482 if (regmatch.regprog != NULL)
27483 {
27484 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027485 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027486 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27487 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027488 /* Skip empty match except for first match. */
27489 if (regmatch.startp[0] == regmatch.endp[0])
27490 {
27491 if (zero_width == regmatch.startp[0])
27492 {
27493 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027494 i = MB_PTR2LEN(tail);
27495 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27496 (size_t)i);
27497 ga.ga_len += i;
27498 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027499 continue;
27500 }
27501 zero_width = regmatch.startp[0];
27502 }
27503
Bram Moolenaar071d4272004-06-13 20:20:40 +000027504 /*
27505 * Get some space for a temporary buffer to do the substitution
27506 * into. It will contain:
27507 * - The text up to where the match is.
27508 * - The substituted text.
27509 * - The text after the match.
27510 */
27511 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027512 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027513 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27514 {
27515 ga_clear(&ga);
27516 break;
27517 }
27518
27519 /* copy the text up to where the match is */
27520 i = (int)(regmatch.startp[0] - tail);
27521 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27522 /* add the substituted text */
27523 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27524 + ga.ga_len + i, TRUE, TRUE, FALSE);
27525 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027526 tail = regmatch.endp[0];
27527 if (*tail == NUL)
27528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027529 if (!do_all)
27530 break;
27531 }
27532
27533 if (ga.ga_data != NULL)
27534 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27535
Bram Moolenaar473de612013-06-08 18:19:48 +020027536 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027537 }
27538
27539 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27540 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027541 if (p_cpo == empty_option)
27542 p_cpo = save_cpo;
27543 else
27544 /* Darn, evaluating {sub} expression changed the value. */
27545 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027546
27547 return ret;
27548}
27549
27550#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */