blob: c8d84c5d5b4b316cb4a20feb0c7776e2c1863a5e [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},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100374 {VV_NAME("false", VAR_SPECIAL), VV_RO},
375 {VV_NAME("true", VAR_SPECIAL), VV_RO},
376 {VV_NAME("null", VAR_SPECIAL), VV_RO},
377 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100378 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200379 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000380};
381
382/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_type vv_di.di_tv.v_type
384#define vv_nr vv_di.di_tv.vval.v_number
385#define vv_float vv_di.di_tv.vval.v_float
386#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000387#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200388#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000389#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000390
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200391static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000392#define vimvarht vimvardict.dv_hashtab
393
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100394static void prepare_vimvar(int idx, typval_T *save_tv);
395static void restore_vimvar(int idx, typval_T *save_tv);
396static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
397static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
398static char_u *skip_var_one(char_u *arg);
399static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
400static void list_glob_vars(int *first);
401static void list_buf_vars(int *first);
402static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100404static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_vim_vars(int *first);
407static void list_script_vars(int *first);
408static void list_func_vars(int *first);
409static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
410static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
411static int check_changedtick(char_u *arg);
412static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
413static void clear_lval(lval_T *lp);
414static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
415static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
416static void list_fix_watch(list_T *l, listitem_T *item);
417static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
418static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
419static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
420static void item_lock(typval_T *tv, int deep, int lock);
421static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
424static int eval1(char_u **arg, typval_T *rettv, int evaluate);
425static int eval2(char_u **arg, typval_T *rettv, int evaluate);
426static int eval3(char_u **arg, typval_T *rettv, int evaluate);
427static int eval4(char_u **arg, typval_T *rettv, int evaluate);
428static int eval5(char_u **arg, typval_T *rettv, int evaluate);
429static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
430static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000431
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100432static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
433static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
434static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
435static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200437static void list_free_contents(list_T *l);
438static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static long list_len(list_T *l);
440static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
441static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
442static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
443static long list_find_nr(list_T *l, long idx, int *errorp);
444static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100445static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
446static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
447static list_T *list_copy(list_T *orig, int deep, int copyID);
448static char_u *list2string(typval_T *tv, int copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
451static int free_unref_items(int copyID);
452static dictitem_T *dictitem_copy(dictitem_T *org);
453static void dictitem_remove(dict_T *dict, dictitem_T *item);
454static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
455static long dict_len(dict_T *d);
456static char_u *dict2string(typval_T *tv, int copyID);
457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
458static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100462static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
463static 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 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467static void dict_free_contents(dict_T *d);
468static void dict_free_dict(dict_T *d);
469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_abs(typval_T *argvars, typval_T *rettv);
472static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100475static void f_and(typval_T *argvars, typval_T *rettv);
476static void f_append(typval_T *argvars, typval_T *rettv);
477static void f_argc(typval_T *argvars, typval_T *rettv);
478static void f_argidx(typval_T *argvars, typval_T *rettv);
479static void f_arglistid(typval_T *argvars, typval_T *rettv);
480static void f_argv(typval_T *argvars, typval_T *rettv);
481static void f_assert_equal(typval_T *argvars, typval_T *rettv);
482static void f_assert_exception(typval_T *argvars, typval_T *rettv);
483static void f_assert_fails(typval_T *argvars, typval_T *rettv);
484static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200485static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200486static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
487static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100488static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100490static void f_asin(typval_T *argvars, typval_T *rettv);
491static void f_atan(typval_T *argvars, typval_T *rettv);
492static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100494static void f_browse(typval_T *argvars, typval_T *rettv);
495static void f_browsedir(typval_T *argvars, typval_T *rettv);
496static void f_bufexists(typval_T *argvars, typval_T *rettv);
497static void f_buflisted(typval_T *argvars, typval_T *rettv);
498static void f_bufloaded(typval_T *argvars, typval_T *rettv);
499static void f_bufname(typval_T *argvars, typval_T *rettv);
500static void f_bufnr(typval_T *argvars, typval_T *rettv);
501static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
502static void f_byte2line(typval_T *argvars, typval_T *rettv);
503static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
504static void f_byteidx(typval_T *argvars, typval_T *rettv);
505static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
506static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000509#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100510#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100511static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100512static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100514static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100515static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100516static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100517static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100518static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
519static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100520static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100521static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100522static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
523static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100524static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100525static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_changenr(typval_T *argvars, typval_T *rettv);
528static void f_char2nr(typval_T *argvars, typval_T *rettv);
529static void f_cindent(typval_T *argvars, typval_T *rettv);
530static void f_clearmatches(typval_T *argvars, typval_T *rettv);
531static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000532#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_complete(typval_T *argvars, typval_T *rettv);
534static void f_complete_add(typval_T *argvars, typval_T *rettv);
535static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_confirm(typval_T *argvars, typval_T *rettv);
538static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_cos(typval_T *argvars, typval_T *rettv);
541static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100543static void f_count(typval_T *argvars, typval_T *rettv);
544static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
545static void f_cursor(typval_T *argsvars, typval_T *rettv);
546static void f_deepcopy(typval_T *argvars, typval_T *rettv);
547static void f_delete(typval_T *argvars, typval_T *rettv);
548static void f_did_filetype(typval_T *argvars, typval_T *rettv);
549static void f_diff_filler(typval_T *argvars, typval_T *rettv);
550static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
551static void f_empty(typval_T *argvars, typval_T *rettv);
552static void f_escape(typval_T *argvars, typval_T *rettv);
553static void f_eval(typval_T *argvars, typval_T *rettv);
554static void f_eventhandler(typval_T *argvars, typval_T *rettv);
555static void f_executable(typval_T *argvars, typval_T *rettv);
556static void f_exepath(typval_T *argvars, typval_T *rettv);
557static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_expand(typval_T *argvars, typval_T *rettv);
562static void f_extend(typval_T *argvars, typval_T *rettv);
563static void f_feedkeys(typval_T *argvars, typval_T *rettv);
564static void f_filereadable(typval_T *argvars, typval_T *rettv);
565static void f_filewritable(typval_T *argvars, typval_T *rettv);
566static void f_filter(typval_T *argvars, typval_T *rettv);
567static void f_finddir(typval_T *argvars, typval_T *rettv);
568static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000569#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100570static void f_float2nr(typval_T *argvars, typval_T *rettv);
571static void f_floor(typval_T *argvars, typval_T *rettv);
572static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000573#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100574static void f_fnameescape(typval_T *argvars, typval_T *rettv);
575static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
576static void f_foldclosed(typval_T *argvars, typval_T *rettv);
577static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
578static void f_foldlevel(typval_T *argvars, typval_T *rettv);
579static void f_foldtext(typval_T *argvars, typval_T *rettv);
580static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
581static void f_foreground(typval_T *argvars, typval_T *rettv);
582static void f_function(typval_T *argvars, typval_T *rettv);
583static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
584static void f_get(typval_T *argvars, typval_T *rettv);
585static void f_getbufline(typval_T *argvars, typval_T *rettv);
586static void f_getbufvar(typval_T *argvars, typval_T *rettv);
587static void f_getchar(typval_T *argvars, typval_T *rettv);
588static void f_getcharmod(typval_T *argvars, typval_T *rettv);
589static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
590static void f_getcmdline(typval_T *argvars, typval_T *rettv);
591static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
592static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
593static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
594static void f_getcwd(typval_T *argvars, typval_T *rettv);
595static void f_getfontname(typval_T *argvars, typval_T *rettv);
596static void f_getfperm(typval_T *argvars, typval_T *rettv);
597static void f_getfsize(typval_T *argvars, typval_T *rettv);
598static void f_getftime(typval_T *argvars, typval_T *rettv);
599static void f_getftype(typval_T *argvars, typval_T *rettv);
600static void f_getline(typval_T *argvars, typval_T *rettv);
601static void f_getmatches(typval_T *argvars, typval_T *rettv);
602static void f_getpid(typval_T *argvars, typval_T *rettv);
603static void f_getcurpos(typval_T *argvars, typval_T *rettv);
604static void f_getpos(typval_T *argvars, typval_T *rettv);
605static void f_getqflist(typval_T *argvars, typval_T *rettv);
606static void f_getreg(typval_T *argvars, typval_T *rettv);
607static void f_getregtype(typval_T *argvars, typval_T *rettv);
608static void f_gettabvar(typval_T *argvars, typval_T *rettv);
609static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
610static void f_getwinposx(typval_T *argvars, typval_T *rettv);
611static void f_getwinposy(typval_T *argvars, typval_T *rettv);
612static void f_getwinvar(typval_T *argvars, typval_T *rettv);
613static void f_glob(typval_T *argvars, typval_T *rettv);
614static void f_globpath(typval_T *argvars, typval_T *rettv);
615static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
616static void f_has(typval_T *argvars, typval_T *rettv);
617static void f_has_key(typval_T *argvars, typval_T *rettv);
618static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
619static void f_hasmapto(typval_T *argvars, typval_T *rettv);
620static void f_histadd(typval_T *argvars, typval_T *rettv);
621static void f_histdel(typval_T *argvars, typval_T *rettv);
622static void f_histget(typval_T *argvars, typval_T *rettv);
623static void f_histnr(typval_T *argvars, typval_T *rettv);
624static void f_hlID(typval_T *argvars, typval_T *rettv);
625static void f_hlexists(typval_T *argvars, typval_T *rettv);
626static void f_hostname(typval_T *argvars, typval_T *rettv);
627static void f_iconv(typval_T *argvars, typval_T *rettv);
628static void f_indent(typval_T *argvars, typval_T *rettv);
629static void f_index(typval_T *argvars, typval_T *rettv);
630static void f_input(typval_T *argvars, typval_T *rettv);
631static void f_inputdialog(typval_T *argvars, typval_T *rettv);
632static void f_inputlist(typval_T *argvars, typval_T *rettv);
633static void f_inputrestore(typval_T *argvars, typval_T *rettv);
634static void f_inputsave(typval_T *argvars, typval_T *rettv);
635static void f_inputsecret(typval_T *argvars, typval_T *rettv);
636static void f_insert(typval_T *argvars, typval_T *rettv);
637static void f_invert(typval_T *argvars, typval_T *rettv);
638static void f_isdirectory(typval_T *argvars, typval_T *rettv);
639static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100640#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
641static void f_isnan(typval_T *argvars, typval_T *rettv);
642#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100643static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100644#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100645static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100646static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100647static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100648static void f_job_start(typval_T *argvars, typval_T *rettv);
649static void f_job_stop(typval_T *argvars, typval_T *rettv);
650static void f_job_status(typval_T *argvars, typval_T *rettv);
651#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100652static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100653static void f_js_decode(typval_T *argvars, typval_T *rettv);
654static void f_js_encode(typval_T *argvars, typval_T *rettv);
655static void f_json_decode(typval_T *argvars, typval_T *rettv);
656static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_keys(typval_T *argvars, typval_T *rettv);
658static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
659static void f_len(typval_T *argvars, typval_T *rettv);
660static void f_libcall(typval_T *argvars, typval_T *rettv);
661static void f_libcallnr(typval_T *argvars, typval_T *rettv);
662static void f_line(typval_T *argvars, typval_T *rettv);
663static void f_line2byte(typval_T *argvars, typval_T *rettv);
664static void f_lispindent(typval_T *argvars, typval_T *rettv);
665static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000666#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_log(typval_T *argvars, typval_T *rettv);
668static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200670#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200672#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_map(typval_T *argvars, typval_T *rettv);
674static void f_maparg(typval_T *argvars, typval_T *rettv);
675static void f_mapcheck(typval_T *argvars, typval_T *rettv);
676static void f_match(typval_T *argvars, typval_T *rettv);
677static void f_matchadd(typval_T *argvars, typval_T *rettv);
678static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
679static void f_matcharg(typval_T *argvars, typval_T *rettv);
680static void f_matchdelete(typval_T *argvars, typval_T *rettv);
681static void f_matchend(typval_T *argvars, typval_T *rettv);
682static void f_matchlist(typval_T *argvars, typval_T *rettv);
683static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200684static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_max(typval_T *argvars, typval_T *rettv);
686static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000687#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100691#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100693#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
695static void f_nr2char(typval_T *argvars, typval_T *rettv);
696static void f_or(typval_T *argvars, typval_T *rettv);
697static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100698#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100700#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000701#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
705static void f_printf(typval_T *argvars, typval_T *rettv);
706static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200707#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200709#endif
710#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100711static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_range(typval_T *argvars, typval_T *rettv);
714static void f_readfile(typval_T *argvars, typval_T *rettv);
715static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100716#ifdef FEAT_FLOAT
717static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
718#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_reltimestr(typval_T *argvars, typval_T *rettv);
720static void f_remote_expr(typval_T *argvars, typval_T *rettv);
721static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
722static void f_remote_peek(typval_T *argvars, typval_T *rettv);
723static void f_remote_read(typval_T *argvars, typval_T *rettv);
724static void f_remote_send(typval_T *argvars, typval_T *rettv);
725static void f_remove(typval_T *argvars, typval_T *rettv);
726static void f_rename(typval_T *argvars, typval_T *rettv);
727static void f_repeat(typval_T *argvars, typval_T *rettv);
728static void f_resolve(typval_T *argvars, typval_T *rettv);
729static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000732#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100733static void f_screenattr(typval_T *argvars, typval_T *rettv);
734static void f_screenchar(typval_T *argvars, typval_T *rettv);
735static void f_screencol(typval_T *argvars, typval_T *rettv);
736static void f_screenrow(typval_T *argvars, typval_T *rettv);
737static void f_search(typval_T *argvars, typval_T *rettv);
738static void f_searchdecl(typval_T *argvars, typval_T *rettv);
739static void f_searchpair(typval_T *argvars, typval_T *rettv);
740static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
741static void f_searchpos(typval_T *argvars, typval_T *rettv);
742static void f_server2client(typval_T *argvars, typval_T *rettv);
743static void f_serverlist(typval_T *argvars, typval_T *rettv);
744static void f_setbufvar(typval_T *argvars, typval_T *rettv);
745static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
746static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100747static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_setline(typval_T *argvars, typval_T *rettv);
749static void f_setloclist(typval_T *argvars, typval_T *rettv);
750static void f_setmatches(typval_T *argvars, typval_T *rettv);
751static void f_setpos(typval_T *argvars, typval_T *rettv);
752static void f_setqflist(typval_T *argvars, typval_T *rettv);
753static void f_setreg(typval_T *argvars, typval_T *rettv);
754static void f_settabvar(typval_T *argvars, typval_T *rettv);
755static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
756static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100757#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100759#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_shellescape(typval_T *argvars, typval_T *rettv);
761static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
762static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_sin(typval_T *argvars, typval_T *rettv);
765static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_sort(typval_T *argvars, typval_T *rettv);
768static void f_soundfold(typval_T *argvars, typval_T *rettv);
769static void f_spellbadword(typval_T *argvars, typval_T *rettv);
770static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
771static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000772#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_sqrt(typval_T *argvars, typval_T *rettv);
774static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_str2nr(typval_T *argvars, typval_T *rettv);
777static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000780#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200781static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_stridx(typval_T *argvars, typval_T *rettv);
783static void f_string(typval_T *argvars, typval_T *rettv);
784static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200785static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_strpart(typval_T *argvars, typval_T *rettv);
787static void f_strridx(typval_T *argvars, typval_T *rettv);
788static void f_strtrans(typval_T *argvars, typval_T *rettv);
789static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
790static void f_strwidth(typval_T *argvars, typval_T *rettv);
791static void f_submatch(typval_T *argvars, typval_T *rettv);
792static void f_substitute(typval_T *argvars, typval_T *rettv);
793static void f_synID(typval_T *argvars, typval_T *rettv);
794static void f_synIDattr(typval_T *argvars, typval_T *rettv);
795static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
796static void f_synstack(typval_T *argvars, typval_T *rettv);
797static void f_synconcealed(typval_T *argvars, typval_T *rettv);
798static void f_system(typval_T *argvars, typval_T *rettv);
799static void f_systemlist(typval_T *argvars, typval_T *rettv);
800static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
801static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
802static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
803static void f_taglist(typval_T *argvars, typval_T *rettv);
804static void f_tagfiles(typval_T *argvars, typval_T *rettv);
805static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200806static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
807static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200808static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
809#ifdef FEAT_JOB_CHANNEL
810static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
811#endif
812static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
813#ifdef FEAT_JOB_CHANNEL
814static void f_test_null_job(typval_T *argvars, typval_T *rettv);
815#endif
816static void f_test_null_list(typval_T *argvars, typval_T *rettv);
817static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
818static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200819#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static void f_tan(typval_T *argvars, typval_T *rettv);
821static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200822#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100823#ifdef FEAT_TIMERS
824static void f_timer_start(typval_T *argvars, typval_T *rettv);
825static void f_timer_stop(typval_T *argvars, typval_T *rettv);
826#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static void f_tolower(typval_T *argvars, typval_T *rettv);
828static void f_toupper(typval_T *argvars, typval_T *rettv);
829static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000830#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100831static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000832#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_type(typval_T *argvars, typval_T *rettv);
834static void f_undofile(typval_T *argvars, typval_T *rettv);
835static void f_undotree(typval_T *argvars, typval_T *rettv);
836static void f_uniq(typval_T *argvars, typval_T *rettv);
837static void f_values(typval_T *argvars, typval_T *rettv);
838static void f_virtcol(typval_T *argvars, typval_T *rettv);
839static void f_visualmode(typval_T *argvars, typval_T *rettv);
840static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100841static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100842static void f_win_getid(typval_T *argvars, typval_T *rettv);
843static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
844static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
845static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static void f_winbufnr(typval_T *argvars, typval_T *rettv);
847static void f_wincol(typval_T *argvars, typval_T *rettv);
848static void f_winheight(typval_T *argvars, typval_T *rettv);
849static void f_winline(typval_T *argvars, typval_T *rettv);
850static void f_winnr(typval_T *argvars, typval_T *rettv);
851static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
852static void f_winrestview(typval_T *argvars, typval_T *rettv);
853static void f_winsaveview(typval_T *argvars, typval_T *rettv);
854static void f_winwidth(typval_T *argvars, typval_T *rettv);
855static void f_writefile(typval_T *argvars, typval_T *rettv);
856static void f_wordcount(typval_T *argvars, typval_T *rettv);
857static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
860static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
861static int get_env_len(char_u **arg);
862static int get_id_len(char_u **arg);
863static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
864static 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 +0000865#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
866#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
867 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
869static int eval_isnamec(int c);
870static int eval_isnamec1(int c);
871static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
872static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static typval_T *alloc_string_tv(char_u *string);
874static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100875#ifdef FEAT_FLOAT
876static float_T get_tv_float(typval_T *varp);
877#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static linenr_T get_tv_lnum(typval_T *argvars);
879static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
881static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
882static hashtab_T *find_var_ht(char_u *name, char_u **varname);
883static funccall_T *get_funccal(void);
884static void vars_clear_ext(hashtab_T *ht, int free_val);
885static void delete_var(hashtab_T *ht, hashitem_T *hi);
886static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
887static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
888static void set_var(char_u *name, typval_T *varp, int copy);
889static int var_check_ro(int flags, char_u *name, int use_gettext);
890static int var_check_fixed(int flags, char_u *name, int use_gettext);
891static int var_check_func_name(char_u *name, int new_var);
892static int valid_varname(char_u *varname);
893static int tv_check_lock(int lock, char_u *name, int use_gettext);
894static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
895static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100896static 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 +0100897static int eval_fname_script(char_u *p);
898static int eval_fname_sid(char_u *p);
899static void list_func_head(ufunc_T *fp, int indent);
900static ufunc_T *find_func(char_u *name);
901static int function_exists(char_u *name);
902static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100904static void func_do_profile(ufunc_T *fp);
905static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
906static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000907static int
908# ifdef __BORLANDC__
909 _RTLENTRYF
910# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100911 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000912static int
913# ifdef __BORLANDC__
914 _RTLENTRYF
915# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100916 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100918static int script_autoload(char_u *name, int reload);
919static char_u *autoload_name(char_u *name);
920static void cat_func_name(char_u *buf, ufunc_T *fp);
921static void func_free(ufunc_T *fp);
922static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
923static int can_free_funccal(funccall_T *fc, int copyID) ;
924static void free_funccal(funccall_T *fc, int free_val);
925static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
926static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
927static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
928static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
929static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
930static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
931static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
932static int write_list(FILE *fd, list_T *list, int binary);
933static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000934
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200935
936#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100937static int compare_func_name(const void *s1, const void *s2);
938static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200939#endif
940
Bram Moolenaar33570922005-01-25 22:26:29 +0000941/*
942 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000943 */
944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100945eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 int i;
948 struct vimvar *p;
949
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200950 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
951 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200952 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000953 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000954 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000955
956 for (i = 0; i < VV_LEN; ++i)
957 {
958 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200959 if (STRLEN(p->vv_name) > 16)
960 {
961 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
962 getout(1);
963 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 STRCPY(p->vv_di.di_key, p->vv_name);
965 if (p->vv_flags & VV_RO)
966 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
967 else if (p->vv_flags & VV_RO_SBX)
968 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
969 else
970 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000971
972 /* add to v: scope dict, unless the value is not always available */
973 if (p->vv_type != VAR_UNKNOWN)
974 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000976 /* add to compat scope dict */
977 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000978 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100979 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
980
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000981 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100982 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200983 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100984 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100985
986 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
987 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
988 set_vim_var_nr(VV_NONE, VVAL_NONE);
989 set_vim_var_nr(VV_NULL, VVAL_NULL);
990
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200991 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200992
993#ifdef EBCDIC
994 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100995 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200996 */
997 sortFunctions();
998#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000999}
1000
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001#if defined(EXITFREE) || defined(PROTO)
1002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001003eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004{
1005 int i;
1006 struct vimvar *p;
1007
1008 for (i = 0; i < VV_LEN; ++i)
1009 {
1010 p = &vimvars[i];
1011 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001012 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001013 vim_free(p->vv_str);
1014 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001015 }
1016 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1017 {
1018 list_unref(p->vv_list);
1019 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001020 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021 }
1022 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001023 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001024 hash_clear(&compat_hashtab);
1025
Bram Moolenaard9fba312005-06-26 22:34:35 +00001026 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001027# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001028 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001029# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030
1031 /* global variables */
1032 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001033
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001034 /* autoloaded script names */
1035 ga_clear_strings(&ga_loaded);
1036
Bram Moolenaarcca74132013-09-25 21:00:28 +02001037 /* Script-local variables. First clear all the variables and in a second
1038 * loop free the scriptvar_T, because a variable in one script might hold
1039 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001040 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001041 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001042 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001043 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 ga_clear(&ga_scripts);
1045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001046 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001047 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001048
1049 /* functions */
1050 free_all_functions();
1051 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001052}
1053#endif
1054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 * Return the name of the executed function.
1057 */
1058 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001059func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/*
1065 * Return the address holding the next breakpoint line for a funccall cookie.
1066 */
1067 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Return the address holding the debug tick for a funccall cookie.
1075 */
1076 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/*
1083 * Return the nesting level for a funccall cookie.
1084 */
1085 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001086func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar33570922005-01-25 22:26:29 +00001088 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089}
1090
1091/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001092funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001094/* pointer to list of previously used funccal, still around because some
1095 * item in it is still being used. */
1096funccall_T *previous_funccal = NULL;
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098/*
1099 * Return TRUE when a function was ended by a ":return" command.
1100 */
1101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104 return current_funccal->returned;
1105}
1106
1107
1108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 * Set an internal variable to a string value. Creates the variable if it does
1110 * not already exist.
1111 */
1112 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001113set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001115 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001116 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 val = vim_strsave(value);
1119 if (val != NULL)
1120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001122 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001124 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 }
1127 }
1128}
1129
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132static char_u *redir_endp = NULL;
1133static char_u *redir_varname = NULL;
1134
1135/*
1136 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001137 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 * Returns OK if successfully completed the setup. FAIL otherwise.
1139 */
1140 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001141var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142{
1143 int save_emsg;
1144 int err;
1145 typval_T tv;
1146
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001147 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001148 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 {
1150 EMSG(_(e_invarg));
1151 return FAIL;
1152 }
1153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 redir_varname = vim_strsave(name);
1156 if (redir_varname == NULL)
1157 return FAIL;
1158
1159 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1160 if (redir_lval == NULL)
1161 {
1162 var_redir_stop();
1163 return FAIL;
1164 }
1165
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166 /* The output is stored in growarray "redir_ga" until redirection ends. */
1167 ga_init2(&redir_ga, (int)sizeof(char), 500);
1168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001170 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001171 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1173 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001174 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 if (redir_endp != NULL && *redir_endp != NUL)
1176 /* Trailing characters are present after the variable name */
1177 EMSG(_(e_trailing));
1178 else
1179 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 var_redir_stop();
1182 return FAIL;
1183 }
1184
1185 /* check if we can write to the variable: set it to or append an empty
1186 * string */
1187 save_emsg = did_emsg;
1188 did_emsg = FALSE;
1189 tv.v_type = VAR_STRING;
1190 tv.vval.v_string = (char_u *)"";
1191 if (append)
1192 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1193 else
1194 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001195 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001197 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 if (err)
1199 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001200 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
1202 return FAIL;
1203 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204
1205 return OK;
1206}
1207
1208/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209 * Append "value[value_len]" to the variable set by var_redir_start().
1210 * The actual appending is postponed until redirection ends, because the value
1211 * appended may in fact be the string we write to, changing it may cause freed
1212 * memory to be used:
1213 * :redir => foo
1214 * :let foo
1215 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 */
1217 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001218var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001220 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221
1222 if (redir_lval == NULL)
1223 return;
1224
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001225 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001226 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001228 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001230 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001231 {
1232 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234 }
1235 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237}
1238
1239/*
1240 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001241 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001242 */
1243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001244var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001245{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001246 typval_T tv;
1247
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248 if (redir_lval != NULL)
1249 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001250 /* If there was no error: assign the text to the variable. */
1251 if (redir_endp != NULL)
1252 {
1253 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1254 tv.v_type = VAR_STRING;
1255 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001256 /* Call get_lval() again, if it's inside a Dict or List it may
1257 * have changed. */
1258 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001259 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001260 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1261 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1262 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001263 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001264
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001265 /* free the collected output */
1266 vim_free(redir_ga.ga_data);
1267 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001268
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001269 vim_free(redir_lval);
1270 redir_lval = NULL;
1271 }
1272 vim_free(redir_varname);
1273 redir_varname = NULL;
1274}
1275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276# if defined(FEAT_MBYTE) || defined(PROTO)
1277 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001278eval_charconvert(
1279 char_u *enc_from,
1280 char_u *enc_to,
1281 char_u *fname_from,
1282 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 int err = FALSE;
1285
1286 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1287 set_vim_var_string(VV_CC_TO, enc_to, -1);
1288 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1289 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1290 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1291 err = TRUE;
1292 set_vim_var_string(VV_CC_FROM, NULL, -1);
1293 set_vim_var_string(VV_CC_TO, NULL, -1);
1294 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296
1297 if (err)
1298 return FAIL;
1299 return OK;
1300}
1301# endif
1302
1303# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 int err = FALSE;
1308
1309 set_vim_var_string(VV_FNAME_IN, fname, -1);
1310 set_vim_var_string(VV_CMDARG, args, -1);
1311 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1312 err = TRUE;
1313 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1314 set_vim_var_string(VV_CMDARG, NULL, -1);
1315
1316 if (err)
1317 {
1318 mch_remove(fname);
1319 return FAIL;
1320 }
1321 return OK;
1322}
1323# endif
1324
1325# if defined(FEAT_DIFF) || defined(PROTO)
1326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327eval_diff(
1328 char_u *origfile,
1329 char_u *newfile,
1330 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 int err = FALSE;
1333
1334 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1335 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1336 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1337 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1338 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1339 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1340 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1341}
1342
1343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001344eval_patch(
1345 char_u *origfile,
1346 char_u *difffile,
1347 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int err;
1350
1351 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1352 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1353 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1354 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1355 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1356 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1357 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1358}
1359# endif
1360
1361/*
1362 * Top level evaluation function, returning a boolean.
1363 * Sets "error" to TRUE if there was an error.
1364 * Return TRUE or FALSE.
1365 */
1366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367eval_to_bool(
1368 char_u *arg,
1369 int *error,
1370 char_u **nextcmd,
1371 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar33570922005-01-25 22:26:29 +00001373 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 int retval = FALSE;
1375
1376 if (skip)
1377 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 else
1381 {
1382 *error = FALSE;
1383 if (!skip)
1384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001385 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 }
1389 if (skip)
1390 --emsg_skip;
1391
1392 return retval;
1393}
1394
1395/*
1396 * Top level evaluation function, returning a string. If "skip" is TRUE,
1397 * only parsing to "nextcmd" is done, without reporting errors. Return
1398 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1399 */
1400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001401eval_to_string_skip(
1402 char_u *arg,
1403 char_u **nextcmd,
1404 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
Bram Moolenaar33570922005-01-25 22:26:29 +00001406 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 char_u *retval;
1408
1409 if (skip)
1410 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 retval = NULL;
1413 else
1414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 retval = vim_strsave(get_tv_string(&tv));
1416 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 if (skip)
1419 --emsg_skip;
1420
1421 return retval;
1422}
1423
1424/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001425 * Skip over an expression at "*pp".
1426 * Return FAIL for an error, OK otherwise.
1427 */
1428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001429skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001430{
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001432
1433 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001435}
1436
1437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001439 * When "convert" is TRUE convert a List into a sequence of lines and convert
1440 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 * Return pointer to allocated memory, or NULL for failure.
1442 */
1443 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001444eval_to_string(
1445 char_u *arg,
1446 char_u **nextcmd,
1447 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001451 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001452#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001453 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001456 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 retval = NULL;
1458 else
1459 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001460 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001461 {
1462 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001463 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001464 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001465 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001466 if (tv.vval.v_list->lv_len > 0)
1467 ga_append(&ga, NL);
1468 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 ga_append(&ga, NUL);
1470 retval = (char_u *)ga.ga_data;
1471 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001472#ifdef FEAT_FLOAT
1473 else if (convert && tv.v_type == VAR_FLOAT)
1474 {
1475 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1476 retval = vim_strsave(numbuf);
1477 }
1478#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001479 else
1480 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483
1484 return retval;
1485}
1486
1487/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001488 * Call eval_to_string() without using current local variables and using
1489 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
1491 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492eval_to_string_safe(
1493 char_u *arg,
1494 char_u **nextcmd,
1495 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 char_u *retval;
1498 void *save_funccalp;
1499
1500 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001501 if (use_sandbox)
1502 ++sandbox;
1503 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001504 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001505 if (use_sandbox)
1506 --sandbox;
1507 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 restore_funccal(save_funccalp);
1509 return retval;
1510}
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512/*
1513 * Top level evaluation function, returning a number.
1514 * Evaluates "expr" silently.
1515 * Returns -1 for an error.
1516 */
1517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar33570922005-01-25 22:26:29 +00001520 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001522 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 ++emsg_off;
1525
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 retval = -1;
1528 else
1529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001530 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533 --emsg_off;
1534
1535 return retval;
1536}
1537
Bram Moolenaara40058a2005-07-11 22:42:07 +00001538/*
1539 * Prepare v: variable "idx" to be used.
1540 * Save the current typeval in "save_tv".
1541 * When not used yet add the variable to the v: hashtable.
1542 */
1543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001545{
1546 *save_tv = vimvars[idx].vv_tv;
1547 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1548 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1549}
1550
1551/*
1552 * Restore v: variable "idx" to typeval "save_tv".
1553 * When no longer defined, remove the variable from the v: hashtable.
1554 */
1555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001557{
1558 hashitem_T *hi;
1559
Bram Moolenaara40058a2005-07-11 22:42:07 +00001560 vimvars[idx].vv_tv = *save_tv;
1561 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1562 {
1563 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1564 if (HASHITEM_EMPTY(hi))
1565 EMSG2(_(e_intern2), "restore_vimvar()");
1566 else
1567 hash_remove(&vimvarht, hi);
1568 }
1569}
1570
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001571#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001572/*
1573 * Evaluate an expression to a list with suggestions.
1574 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001575 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001576 */
1577 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001579{
1580 typval_T save_val;
1581 typval_T rettv;
1582 list_T *list = NULL;
1583 char_u *p = skipwhite(expr);
1584
1585 /* Set "v:val" to the bad word. */
1586 prepare_vimvar(VV_VAL, &save_val);
1587 vimvars[VV_VAL].vv_type = VAR_STRING;
1588 vimvars[VV_VAL].vv_str = badword;
1589 if (p_verbose == 0)
1590 ++emsg_off;
1591
1592 if (eval1(&p, &rettv, TRUE) == OK)
1593 {
1594 if (rettv.v_type != VAR_LIST)
1595 clear_tv(&rettv);
1596 else
1597 list = rettv.vval.v_list;
1598 }
1599
1600 if (p_verbose == 0)
1601 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001602 restore_vimvar(VV_VAL, &save_val);
1603
1604 return list;
1605}
1606
1607/*
1608 * "list" is supposed to contain two items: a word and a number. Return the
1609 * word in "pp" and the number as the return value.
1610 * Return -1 if anything isn't right.
1611 * Used to get the good word and score from the eval_spell_expr() result.
1612 */
1613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001615{
1616 listitem_T *li;
1617
1618 li = list->lv_first;
1619 if (li == NULL)
1620 return -1;
1621 *pp = get_tv_string(&li->li_tv);
1622
1623 li = li->li_next;
1624 if (li == NULL)
1625 return -1;
1626 return get_tv_number(&li->li_tv);
1627}
1628#endif
1629
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001630/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001631 * Top level evaluation function.
1632 * Returns an allocated typval_T with the result.
1633 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001634 */
1635 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001637{
1638 typval_T *tv;
1639
1640 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001641 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001642 {
1643 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001644 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001645 }
1646
1647 return tv;
1648}
1649
1650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001653 * Uses argv[argc] for the function arguments. Only Number and String
1654 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001658call_vim_function(
1659 char_u *func,
1660 int argc,
1661 char_u **argv,
1662 int safe, /* use the sandbox */
1663 int str_arg_only, /* all arguments are strings */
1664 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 long n;
1668 int len;
1669 int i;
1670 int doesrange;
1671 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001674 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 for (i = 0; i < argc; i++)
1679 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001680 /* Pass a NULL or empty argument as an empty string */
1681 if (argv[i] == NULL || *argv[i] == NUL)
1682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001683 argvars[i].v_type = VAR_STRING;
1684 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001685 continue;
1686 }
1687
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001688 if (str_arg_only)
1689 len = 0;
1690 else
1691 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001692 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (len != 0 && len == (int)STRLEN(argv[i]))
1694 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001695 argvars[i].v_type = VAR_NUMBER;
1696 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
1698 else
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_STRING;
1701 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 }
1704
1705 if (safe)
1706 {
1707 save_funccalp = save_funccal();
1708 ++sandbox;
1709 }
1710
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1712 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001714 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (safe)
1716 {
1717 --sandbox;
1718 restore_funccal(save_funccalp);
1719 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 vim_free(argvars);
1721
1722 if (ret == FAIL)
1723 clear_tv(rettv);
1724
1725 return ret;
1726}
1727
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001728/*
1729 * Call vimL function "func" and return the result as a number.
1730 * Returns -1 when calling the function fails.
1731 * Uses argv[argc] for the function arguments.
1732 */
1733 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734call_func_retnr(
1735 char_u *func,
1736 int argc,
1737 char_u **argv,
1738 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001739{
1740 typval_T rettv;
1741 long retval;
1742
1743 /* All arguments are passed as strings, no conversion to number. */
1744 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1745 return -1;
1746
1747 retval = get_tv_number_chk(&rettv, NULL);
1748 clear_tv(&rettv);
1749 return retval;
1750}
1751
1752#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1753 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1754
Bram Moolenaar4f688582007-07-24 12:34:30 +00001755# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001757 * Call vimL function "func" and return the result as a string.
1758 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759 * Uses argv[argc] for the function arguments.
1760 */
1761 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762call_func_retstr(
1763 char_u *func,
1764 int argc,
1765 char_u **argv,
1766 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767{
1768 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001769 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001771 /* All arguments are passed as strings, no conversion to number. */
1772 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773 return NULL;
1774
1775 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 return retval;
1778}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001779# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001781/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001782 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001784 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785 */
1786 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787call_func_retlist(
1788 char_u *func,
1789 int argc,
1790 char_u **argv,
1791 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001792{
1793 typval_T rettv;
1794
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001795 /* All arguments are passed as strings, no conversion to number. */
1796 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797 return NULL;
1798
1799 if (rettv.v_type != VAR_LIST)
1800 {
1801 clear_tv(&rettv);
1802 return NULL;
1803 }
1804
1805 return rettv.vval.v_list;
1806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808
1809/*
1810 * Save the current function call pointer, and set it to NULL.
1811 * Used when executing autocommands and for ":source".
1812 */
1813 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 current_funccal = NULL;
1819 return (void *)fc;
1820}
1821
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001825 funccall_T *fc = (funccall_T *)vfc;
1826
1827 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828}
1829
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830#if defined(FEAT_PROFILE) || defined(PROTO)
1831/*
1832 * Prepare profiling for entering a child or something else that is not
1833 * counted for the script/function itself.
1834 * Should always be called in pair with prof_child_exit().
1835 */
1836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837prof_child_enter(
1838 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001839{
1840 funccall_T *fc = current_funccal;
1841
1842 if (fc != NULL && fc->func->uf_profiling)
1843 profile_start(&fc->prof_child);
1844 script_prof_save(tm);
1845}
1846
1847/*
1848 * Take care of time spent in a child.
1849 * Should always be called after prof_child_enter().
1850 */
1851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852prof_child_exit(
1853 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001854{
1855 funccall_T *fc = current_funccal;
1856
1857 if (fc != NULL && fc->func->uf_profiling)
1858 {
1859 profile_end(&fc->prof_child);
1860 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1861 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1862 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1863 }
1864 script_prof_restore(tm);
1865}
1866#endif
1867
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#ifdef FEAT_FOLDING
1870/*
1871 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1872 * it in "*cp". Doesn't give error messages.
1873 */
1874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 int retval;
1879 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001880 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1881 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001884 if (use_sandbox)
1885 ++sandbox;
1886 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 retval = 0;
1890 else
1891 {
1892 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (tv.v_type == VAR_NUMBER)
1894 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001895 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 retval = 0;
1897 else
1898 {
1899 /* If the result is a string, check if there is a non-digit before
1900 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (!VIM_ISDIGIT(*s) && *s != '-')
1903 *cp = *s++;
1904 retval = atol((char *)s);
1905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001909 if (use_sandbox)
1910 --sandbox;
1911 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 return retval;
1914}
1915#endif
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 * ":let" list all variable values
1919 * ":let var1 var2" list variable values
1920 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 * ":let var += expr" assignment command.
1922 * ":let var -= expr" assignment command.
1923 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
1926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001927ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 int var_count = 0;
1934 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001936 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001937 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaardb552d602006-03-23 22:59:57 +00001939 argend = skip_var_list(arg, &var_count, &semicolon);
1940 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001942 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1943 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001944 expr = skipwhite(argend);
1945 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1946 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001948 /*
1949 * ":let" without "=": list variables
1950 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 if (*arg == '[')
1952 EMSG(_(e_invarg));
1953 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001955 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001959 list_glob_vars(&first);
1960 list_buf_vars(&first);
1961 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001962#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001963 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001964#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965 list_script_vars(&first);
1966 list_func_vars(&first);
1967 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 eap->nextcmd = check_nextcmd(arg);
1970 }
1971 else
1972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 op[0] = '=';
1974 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001975 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001977 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1978 op[0] = *expr; /* +=, -= or .= */
1979 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001981 else
1982 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (eap->skip)
1985 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (eap->skip)
1988 {
1989 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 --emsg_skip;
1992 }
1993 else if (i != FAIL)
1994 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 }
2000}
2001
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002/*
2003 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2004 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002005 * When "nextchars" is not NULL it points to a string with characters that
2006 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2007 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 * Returns OK or FAIL;
2009 */
2010 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002011ex_let_vars(
2012 char_u *arg_start,
2013 typval_T *tv,
2014 int copy, /* copy values from "tv", don't move */
2015 int semicolon, /* from skip_var_list() */
2016 int var_count, /* from skip_var_list() */
2017 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018{
2019 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002020 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002022 listitem_T *item;
2023 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024
2025 if (*arg != '[')
2026 {
2027 /*
2028 * ":let var = expr" or ":for var in list"
2029 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 return FAIL;
2032 return OK;
2033 }
2034
2035 /*
2036 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2037 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002038 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 {
2040 EMSG(_(e_listreq));
2041 return FAIL;
2042 }
2043
2044 i = list_len(l);
2045 if (semicolon == 0 && var_count < i)
2046 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002047 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 return FAIL;
2049 }
2050 if (var_count - semicolon > i)
2051 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002052 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 return FAIL;
2054 }
2055
2056 item = l->lv_first;
2057 while (*arg != ']')
2058 {
2059 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002060 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 item = item->li_next;
2062 if (arg == NULL)
2063 return FAIL;
2064
2065 arg = skipwhite(arg);
2066 if (*arg == ';')
2067 {
2068 /* Put the rest of the list (may be empty) in the var after ';'.
2069 * Create a new list for this. */
2070 l = list_alloc();
2071 if (l == NULL)
2072 return FAIL;
2073 while (item != NULL)
2074 {
2075 list_append_tv(l, &item->li_tv);
2076 item = item->li_next;
2077 }
2078
2079 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002080 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 ltv.vval.v_list = l;
2082 l->lv_refcount = 1;
2083
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002084 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2085 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 clear_tv(&ltv);
2087 if (arg == NULL)
2088 return FAIL;
2089 break;
2090 }
2091 else if (*arg != ',' && *arg != ']')
2092 {
2093 EMSG2(_(e_intern2), "ex_let_vars()");
2094 return FAIL;
2095 }
2096 }
2097
2098 return OK;
2099}
2100
2101/*
2102 * Skip over assignable variable "var" or list of variables "[var, var]".
2103 * Used for ":let varvar = expr" and ":for varvar in expr".
2104 * For "[var, var]" increment "*var_count" for each variable.
2105 * for "[var, var; var]" set "semicolon".
2106 * Return NULL for an error.
2107 */
2108 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002109skip_var_list(
2110 char_u *arg,
2111 int *var_count,
2112 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113{
2114 char_u *p, *s;
2115
2116 if (*arg == '[')
2117 {
2118 /* "[var, var]": find the matching ']'. */
2119 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002120 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 {
2122 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2123 s = skip_var_one(p);
2124 if (s == p)
2125 {
2126 EMSG2(_(e_invarg2), p);
2127 return NULL;
2128 }
2129 ++*var_count;
2130
2131 p = skipwhite(s);
2132 if (*p == ']')
2133 break;
2134 else if (*p == ';')
2135 {
2136 if (*semicolon == 1)
2137 {
2138 EMSG(_("Double ; in list of variables"));
2139 return NULL;
2140 }
2141 *semicolon = 1;
2142 }
2143 else if (*p != ',')
2144 {
2145 EMSG2(_(e_invarg2), p);
2146 return NULL;
2147 }
2148 }
2149 return p + 1;
2150 }
2151 else
2152 return skip_var_one(arg);
2153}
2154
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002156 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002157 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002159 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002160skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002162 if (*arg == '@' && arg[1] != NUL)
2163 return arg + 2;
2164 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2165 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166}
2167
Bram Moolenaara7043832005-01-21 11:56:39 +00002168/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002169 * List variables for hashtab "ht" with prefix "prefix".
2170 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173list_hashtable_vars(
2174 hashtab_T *ht,
2175 char_u *prefix,
2176 int empty,
2177 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002178{
Bram Moolenaar33570922005-01-25 22:26:29 +00002179 hashitem_T *hi;
2180 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002181 int todo;
2182
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002183 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2185 {
2186 if (!HASHITEM_EMPTY(hi))
2187 {
2188 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002189 di = HI2DI(hi);
2190 if (empty || di->di_tv.v_type != VAR_STRING
2191 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002193 }
2194 }
2195}
2196
2197/*
2198 * List global variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002202{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002204}
2205
2206/*
2207 * List buffer variables.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002211{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u numbuf[NUMBUFLEN];
2213
Bram Moolenaar429fa852013-04-15 12:27:36 +02002214 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216
2217 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2219 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002220}
2221
2222/*
2223 * List window variables.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002227{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002228 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002230}
2231
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002232#ifdef FEAT_WINDOWS
2233/*
2234 * List tab page variables.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002239 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241}
2242#endif
2243
Bram Moolenaara7043832005-01-21 11:56:39 +00002244/*
2245 * List Vim variables.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251}
2252
2253/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002254 * List script-local variables, if there is a script.
2255 */
2256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002257list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002258{
2259 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2261 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002262}
2263
2264/*
2265 * List function variables, if there is a function.
2266 */
2267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002268list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002269{
2270 if (current_funccal != NULL)
2271 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002272 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002273}
2274
2275/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 * List variables in "arg".
2277 */
2278 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002279list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280{
2281 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 char_u *name_start;
2285 char_u *arg_subsc;
2286 char_u *tofree;
2287 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288
2289 while (!ends_excmd(*arg) && !got_int)
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002293 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2295 {
2296 emsg_severe = TRUE;
2297 EMSG(_(e_trailing));
2298 break;
2299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 /* get_name_len() takes care of expanding curly braces */
2304 name_start = name = arg;
2305 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2306 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 /* This is mainly to keep test 49 working: when expanding
2309 * curly braces fails overrule the exception error message. */
2310 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 emsg_severe = TRUE;
2313 EMSG2(_(e_invarg2), arg);
2314 break;
2315 }
2316 error = TRUE;
2317 }
2318 else
2319 {
2320 if (tofree != NULL)
2321 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002322 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 else
2325 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 /* handle d.key, l[idx], f(expr) */
2327 arg_subsc = arg;
2328 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002329 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002335 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002336 case 'g': list_glob_vars(first); break;
2337 case 'b': list_buf_vars(first); break;
2338 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002339#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002340 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002341#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002342 case 'v': list_vim_vars(first); break;
2343 case 's': list_script_vars(first); break;
2344 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 default:
2346 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002347 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
2349 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002350 {
2351 char_u numbuf[NUMBUFLEN];
2352 char_u *tf;
2353 int c;
2354 char_u *s;
2355
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002356 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002357 c = *arg;
2358 *arg = NUL;
2359 list_one_var_a((char_u *)"",
2360 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002361 tv.v_type,
2362 s == NULL ? (char_u *)"" : s,
2363 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002364 *arg = c;
2365 vim_free(tf);
2366 }
2367 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 }
2370 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002371
2372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374
2375 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 }
2377
2378 return arg;
2379}
2380
2381/*
2382 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2383 * Returns a pointer to the char just after the var name.
2384 * Returns NULL if there is an error.
2385 */
2386 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002387ex_let_one(
2388 char_u *arg, /* points to variable name */
2389 typval_T *tv, /* value to assign to variable */
2390 int copy, /* copy value from "tv" */
2391 char_u *endchars, /* valid chars after variable name or NULL */
2392 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393{
2394 int c1;
2395 char_u *name;
2396 char_u *p;
2397 char_u *arg_end = NULL;
2398 int len;
2399 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401
2402 /*
2403 * ":let $VAR = expr": Set environment variable.
2404 */
2405 if (*arg == '$')
2406 {
2407 /* Find the end of the name. */
2408 ++arg;
2409 name = arg;
2410 len = get_env_len(&arg);
2411 if (len == 0)
2412 EMSG2(_(e_invarg2), name - 1);
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (op != NULL && (*op == '+' || *op == '-'))
2416 EMSG2(_(e_letwrong), op);
2417 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002420 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 {
2422 c1 = name[len];
2423 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 p = get_tv_string_chk(tv);
2425 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 {
2427 int mustfree = FALSE;
2428 char_u *s = vim_getenv(name, &mustfree);
2429
2430 if (s != NULL)
2431 {
2432 p = tofree = concat_str(s, p);
2433 if (mustfree)
2434 vim_free(s);
2435 }
2436 }
2437 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 if (STRICMP(name, "HOME") == 0)
2441 init_homedir();
2442 else if (didset_vim && STRICMP(name, "VIM") == 0)
2443 didset_vim = FALSE;
2444 else if (didset_vimruntime
2445 && STRICMP(name, "VIMRUNTIME") == 0)
2446 didset_vimruntime = FALSE;
2447 arg_end = arg;
2448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 }
2452 }
2453 }
2454
2455 /*
2456 * ":let &option = expr": Set option value.
2457 * ":let &l:option = expr": Set local option value.
2458 * ":let &g:option = expr": Set global option value.
2459 */
2460 else if (*arg == '&')
2461 {
2462 /* Find the end of the name. */
2463 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 if (p == NULL || (endchars != NULL
2465 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 EMSG(_(e_letunexp));
2467 else
2468 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 long n;
2470 int opt_type;
2471 long numval;
2472 char_u *stringval = NULL;
2473 char_u *s;
2474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 c1 = *p;
2476 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477
2478 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 s = get_tv_string_chk(tv); /* != NULL if number or string */
2480 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 {
2482 opt_type = get_option_value(arg, &numval,
2483 &stringval, opt_flags);
2484 if ((opt_type == 1 && *op == '.')
2485 || (opt_type == 0 && *op != '.'))
2486 EMSG2(_(e_letwrong), op);
2487 else
2488 {
2489 if (opt_type == 1) /* number */
2490 {
2491 if (*op == '+')
2492 n = numval + n;
2493 else
2494 n = numval - n;
2495 }
2496 else if (opt_type == 0 && stringval != NULL) /* string */
2497 {
2498 s = concat_str(stringval, s);
2499 vim_free(stringval);
2500 stringval = s;
2501 }
2502 }
2503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 if (s != NULL)
2505 {
2506 set_option_value(arg, n, s, opt_flags);
2507 arg_end = p;
2508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let @r = expr": Set register contents.
2516 */
2517 else if (*arg == '@')
2518 {
2519 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002520 if (op != NULL && (*op == '+' || *op == '-'))
2521 EMSG2(_(e_letwrong), op);
2522 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 EMSG(_(e_letunexp));
2525 else
2526 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 char_u *s;
2529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530 p = get_tv_string_chk(tv);
2531 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002533 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 if (s != NULL)
2535 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 vim_free(s);
2538 }
2539 }
2540 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 arg_end = arg + 1;
2544 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 }
2547 }
2548
2549 /*
2550 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2561 EMSG(_(e_letunexp));
2562 else
2563 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 arg_end = p;
2566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 }
2570
2571 else
2572 EMSG2(_(e_invarg2), arg);
2573
2574 return arg_end;
2575}
2576
2577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2579 */
2580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002581check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582{
2583 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2584 {
2585 EMSG2(_(e_readonlyvar), arg);
2586 return TRUE;
2587 }
2588 return FALSE;
2589}
2590
2591/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 * Get an lval: variable, Dict item or List item that can be assigned a value
2593 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2594 * "name.key", "name.key[expr]" etc.
2595 * Indexing only works if "name" is an existing List or Dictionary.
2596 * "name" points to the start of the name.
2597 * If "rettv" is not NULL it points to the value to be assigned.
2598 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2599 * wrong; must end in space or cmd separator.
2600 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 * flags:
2602 * GLV_QUIET: do not give error messages
2603 * GLV_NO_AUTOLOAD: do not use script autoloading
2604 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002606 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 */
2609 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002610get_lval(
2611 char_u *name,
2612 typval_T *rettv,
2613 lval_T *lp,
2614 int unlet,
2615 int skip,
2616 int flags, /* GLV_ values */
2617 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 char_u *expr_start, *expr_end;
2621 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 dictitem_T *v;
2623 typval_T var1;
2624 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002630 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002633 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634
2635 if (skip)
2636 {
2637 /* When skipping just find the end of the name. */
2638 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002639 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 }
2641
2642 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002643 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (expr_start != NULL)
2645 {
2646 /* Don't expand the name when we already know there is an error. */
2647 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2648 && *p != '[' && *p != '.')
2649 {
2650 EMSG(_(e_trailing));
2651 return NULL;
2652 }
2653
2654 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2655 if (lp->ll_exp_name == NULL)
2656 {
2657 /* Report an invalid expression in braces, unless the
2658 * expression evaluation has been cancelled due to an
2659 * aborting error, an interrupt, or an exception. */
2660 if (!aborting() && !quiet)
2661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002662 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 EMSG2(_(e_invarg2), name);
2664 return NULL;
2665 }
2666 }
2667 lp->ll_name = lp->ll_exp_name;
2668 }
2669 else
2670 lp->ll_name = name;
2671
2672 /* Without [idx] or .key we are done. */
2673 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2674 return p;
2675
2676 cc = *p;
2677 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002678 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (v == NULL && !quiet)
2680 EMSG2(_(e_undefvar), lp->ll_name);
2681 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 if (v == NULL)
2683 return NULL;
2684
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 /*
2686 * Loop until no more [idx] or .key is following.
2687 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002688 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2692 && !(lp->ll_tv->v_type == VAR_DICT
2693 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
2696 EMSG(_("E689: Can only index a List or Dictionary"));
2697 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_("E708: [:] must come last"));
2703 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 len = -1;
2707 if (*p == '.')
2708 {
2709 key = p + 1;
2710 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2711 ;
2712 if (len == 0)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
2716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 p = key + len;
2719 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 else
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p == ':')
2725 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 else
2727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 empty1 = FALSE;
2729 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 if (get_tv_string_chk(&var1) == NULL)
2732 {
2733 /* not a number or string */
2734 clear_tv(&var1);
2735 return NULL;
2736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
2738
2739 /* Optionally get the second index [ :expr]. */
2740 if (*p == ':')
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 if (!empty1)
2747 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (rettv != NULL && (rettv->v_type != VAR_LIST
2751 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (!empty1)
2756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 }
2759 p = skipwhite(p + 1);
2760 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 else
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (!empty1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 if (get_tv_string_chk(&var2) == NULL)
2772 {
2773 /* not a number or string */
2774 if (!empty1)
2775 clear_tv(&var1);
2776 clear_tv(&var2);
2777 return NULL;
2778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002784
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
2788 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (!empty1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 }
2795
2796 /* Skip to past ']'. */
2797 ++p;
2798 }
2799
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
2802 if (len == -1)
2803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002805 key = get_tv_string_chk(&var1); /* is number or string */
2806 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 lp->ll_list = NULL;
2813 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002814 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002815
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002816 /* When assigning to a scope dictionary check that a function and
2817 * variable name is valid (only variable name unless it is l: or
2818 * g: dictionary). Disallow overwriting a builtin function. */
2819 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002821 int prevval;
2822 int wrong;
2823
2824 if (len != -1)
2825 {
2826 prevval = key[len];
2827 key[len] = NUL;
2828 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002829 else
2830 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002831 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2832 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002834 || !valid_varname(key);
2835 if (len != -1)
2836 key[len] = prevval;
2837 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 /* Can't add "v:" variable. */
2844 if (lp->ll_dict == &vimvardict)
2845 {
2846 EMSG2(_(e_illvar), name);
2847 return NULL;
2848 }
2849
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002850 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 if (len == -1)
2856 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 }
2859 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (len == -1)
2864 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 p = NULL;
2867 break;
2868 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002869 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002870 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002871 return NULL;
2872
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 if (len == -1)
2874 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 }
2877 else
2878 {
2879 /*
2880 * Get the number and item for the only or first index of the List.
2881 */
2882 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 else
2885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002886 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 clear_tv(&var1);
2888 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_list = lp->ll_tv->vval.v_list;
2891 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2892 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002894 if (lp->ll_n1 < 0)
2895 {
2896 lp->ll_n1 = 0;
2897 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2898 }
2899 }
2900 if (lp->ll_li == NULL)
2901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 }
2908
2909 /*
2910 * May need to find the item or absolute index for the second
2911 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 * When no index given: "lp->ll_empty2" is TRUE.
2913 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002914 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002917 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002923 {
2924 if (!quiet)
2925 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002927 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929 }
2930
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2932 if (lp->ll_n1 < 0)
2933 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2934 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002935 {
2936 if (!quiet)
2937 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 }
2941
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 }
2945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 return p;
2947}
2948
2949/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 */
2952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002953clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954{
2955 vim_free(lp->ll_exp_name);
2956 vim_free(lp->ll_newkey);
2957}
2958
2959/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 */
2964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002965set_var_lval(
2966 lval_T *lp,
2967 char_u *endp,
2968 typval_T *rettv,
2969 int copy,
2970 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971{
2972 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002973 listitem_T *ri;
2974 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975
2976 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 cc = *endp;
2981 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002986 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002987 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002988 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002989 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002991 if ((di == NULL
2992 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2993 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2994 FALSE)))
2995 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 set_var(lp->ll_name, &tv, FALSE);
2997 clear_tv(&tv);
2998 }
2999 }
3000 else
3001 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 else if (tv_check_lock(lp->ll_newkey == NULL
3006 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003007 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003008 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 else if (lp->ll_range)
3010 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003011 listitem_T *ll_li = lp->ll_li;
3012 int ll_n1 = lp->ll_n1;
3013
3014 /*
3015 * Check whether any of the list items is locked
3016 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003017 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003018 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003019 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003020 return;
3021 ri = ri->li_next;
3022 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3023 break;
3024 ll_li = ll_li->li_next;
3025 ++ll_n1;
3026 }
3027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 /*
3029 * Assign the List values to the list items.
3030 */
3031 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 if (op != NULL && *op != '=')
3034 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3035 else
3036 {
3037 clear_tv(&lp->ll_li->li_tv);
3038 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 ri = ri->li_next;
3041 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3042 break;
3043 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003046 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 ri = NULL;
3049 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003050 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 lp->ll_li = lp->ll_li->li_next;
3053 ++lp->ll_n1;
3054 }
3055 if (ri != NULL)
3056 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 else if (lp->ll_empty2
3058 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 : lp->ll_n1 != lp->ll_n2)
3060 EMSG(_("E711: List value has not enough items"));
3061 }
3062 else
3063 {
3064 /*
3065 * Assign to a List or Dictionary item.
3066 */
3067 if (lp->ll_newkey != NULL)
3068 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 if (op != NULL && *op != '=')
3070 {
3071 EMSG2(_(e_letwrong), op);
3072 return;
3073 }
3074
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003076 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 if (di == NULL)
3078 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003079 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3080 {
3081 vim_free(di);
3082 return;
3083 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003084 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 else if (op != NULL && *op != '=')
3087 {
3088 tv_op(lp->ll_tv, rettv, op);
3089 return;
3090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 /*
3095 * Assign the value to the variable or list item.
3096 */
3097 if (copy)
3098 copy_tv(rettv, lp->ll_tv);
3099 else
3100 {
3101 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003102 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003103 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003104 }
3105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106}
3107
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3110 * Returns OK or FAIL.
3111 */
3112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114{
3115 long n;
3116 char_u numbuf[NUMBUFLEN];
3117 char_u *s;
3118
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003119 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3120 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3121 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 {
3123 switch (tv1->v_type)
3124 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003125 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 case VAR_DICT:
3127 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003128 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003129 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003130 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003131 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 break;
3133
3134 case VAR_LIST:
3135 if (*op != '+' || tv2->v_type != VAR_LIST)
3136 break;
3137 /* List += List */
3138 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3139 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3140 return OK;
3141
3142 case VAR_NUMBER:
3143 case VAR_STRING:
3144 if (tv2->v_type == VAR_LIST)
3145 break;
3146 if (*op == '+' || *op == '-')
3147 {
3148 /* nr += nr or nr -= nr*/
3149 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150#ifdef FEAT_FLOAT
3151 if (tv2->v_type == VAR_FLOAT)
3152 {
3153 float_T f = n;
3154
3155 if (*op == '+')
3156 f += tv2->vval.v_float;
3157 else
3158 f -= tv2->vval.v_float;
3159 clear_tv(tv1);
3160 tv1->v_type = VAR_FLOAT;
3161 tv1->vval.v_float = f;
3162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003163 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164#endif
3165 {
3166 if (*op == '+')
3167 n += get_tv_number(tv2);
3168 else
3169 n -= get_tv_number(tv2);
3170 clear_tv(tv1);
3171 tv1->v_type = VAR_NUMBER;
3172 tv1->vval.v_number = n;
3173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003174 }
3175 else
3176 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177 if (tv2->v_type == VAR_FLOAT)
3178 break;
3179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 /* str .= str */
3181 s = get_tv_string(tv1);
3182 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3183 clear_tv(tv1);
3184 tv1->v_type = VAR_STRING;
3185 tv1->vval.v_string = s;
3186 }
3187 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003190#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191 {
3192 float_T f;
3193
3194 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3195 && tv2->v_type != VAR_NUMBER
3196 && tv2->v_type != VAR_STRING))
3197 break;
3198 if (tv2->v_type == VAR_FLOAT)
3199 f = tv2->vval.v_float;
3200 else
3201 f = get_tv_number(tv2);
3202 if (*op == '+')
3203 tv1->vval.v_float += f;
3204 else
3205 tv1->vval.v_float -= f;
3206 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003208 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003209 }
3210 }
3211
3212 EMSG2(_(e_letwrong), op);
3213 return FAIL;
3214}
3215
3216/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 * Add a watcher to a list.
3218 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221{
3222 lw->lw_next = l->lv_watch;
3223 l->lv_watch = lw;
3224}
3225
3226/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003227 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 * No warning when it isn't found...
3229 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234
3235 lwp = &l->lv_watch;
3236 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3237 {
3238 if (lw == lwrem)
3239 {
3240 *lwp = lw->lw_next;
3241 break;
3242 }
3243 lwp = &lw->lw_next;
3244 }
3245}
3246
3247/*
3248 * Just before removing an item from a list: advance watchers to the next
3249 * item.
3250 */
3251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3257 if (lw->lw_item == item)
3258 lw->lw_item = item->li_next;
3259}
3260
3261/*
3262 * Evaluate the expression used in a ":for var in expr" command.
3263 * "arg" points to "var".
3264 * Set "*errp" to TRUE for an error, FALSE otherwise;
3265 * Return a pointer that holds the info. Null when there is an error.
3266 */
3267 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268eval_for_line(
3269 char_u *arg,
3270 int *errp,
3271 char_u **nextcmdp,
3272 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273{
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 typval_T tv;
3277 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278
3279 *errp = TRUE; /* default: there is an error */
3280
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 if (fi == NULL)
3283 return NULL;
3284
3285 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3286 if (expr == NULL)
3287 return fi;
3288
3289 expr = skipwhite(expr);
3290 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3291 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003292 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 return fi;
3294 }
3295
3296 if (skip)
3297 ++emsg_skip;
3298 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3299 {
3300 *errp = FALSE;
3301 if (!skip)
3302 {
3303 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003304 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003305 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 clear_tv(&tv);
3308 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003309 else if (l == NULL)
3310 {
3311 /* a null list is like an empty list: do nothing */
3312 clear_tv(&tv);
3313 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 else
3315 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003316 /* No need to increment the refcount, it's already set for the
3317 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 fi->fi_list = l;
3319 list_add_watch(l, &fi->fi_lw);
3320 fi->fi_lw.lw_item = l->lv_first;
3321 }
3322 }
3323 }
3324 if (skip)
3325 --emsg_skip;
3326
3327 return fi;
3328}
3329
3330/*
3331 * Use the first item in a ":for" list. Advance to the next.
3332 * Assign the values to the variable (list). "arg" points to the first one.
3333 * Return TRUE when a valid item was found, FALSE when at end of list or
3334 * something wrong.
3335 */
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003339 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003341 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342
3343 item = fi->fi_lw.lw_item;
3344 if (item == NULL)
3345 result = FALSE;
3346 else
3347 {
3348 fi->fi_lw.lw_item = item->li_next;
3349 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3350 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3351 }
3352 return result;
3353}
3354
3355/*
3356 * Free the structure used to store info used by ":for".
3357 */
3358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360{
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003363 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003364 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003366 list_unref(fi->fi_list);
3367 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 vim_free(fi);
3369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3372
3373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374set_context_for_expression(
3375 expand_T *xp,
3376 char_u *arg,
3377 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 int got_eq = FALSE;
3380 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 if (cmdidx == CMD_let)
3384 {
3385 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003386 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003387 {
3388 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003389 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003390 {
3391 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003393 if (vim_iswhite(*p))
3394 break;
3395 }
3396 return;
3397 }
3398 }
3399 else
3400 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3401 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 while ((xp->xp_pattern = vim_strpbrk(arg,
3403 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3404 {
3405 c = *xp->xp_pattern;
3406 if (c == '&')
3407 {
3408 c = xp->xp_pattern[1];
3409 if (c == '&')
3410 {
3411 ++xp->xp_pattern;
3412 xp->xp_context = cmdidx != CMD_let || got_eq
3413 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3414 }
3415 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003418 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3419 xp->xp_pattern += 2;
3420
3421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 else if (c == '$')
3424 {
3425 /* environment variable */
3426 xp->xp_context = EXPAND_ENV_VARS;
3427 }
3428 else if (c == '=')
3429 {
3430 got_eq = TRUE;
3431 xp->xp_context = EXPAND_EXPRESSION;
3432 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003433 else if (c == '#'
3434 && xp->xp_context == EXPAND_EXPRESSION)
3435 {
3436 /* Autoload function/variable contains '#'. */
3437 break;
3438 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003439 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 && xp->xp_context == EXPAND_FUNCTIONS
3441 && vim_strchr(xp->xp_pattern, '(') == NULL)
3442 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003443 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 break;
3445 }
3446 else if (cmdidx != CMD_let || got_eq)
3447 {
3448 if (c == '"') /* string */
3449 {
3450 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3451 if (c == '\\' && xp->xp_pattern[1] != NUL)
3452 ++xp->xp_pattern;
3453 xp->xp_context = EXPAND_NOTHING;
3454 }
3455 else if (c == '\'') /* literal string */
3456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003457 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3459 /* skip */ ;
3460 xp->xp_context = EXPAND_NOTHING;
3461 }
3462 else if (c == '|')
3463 {
3464 if (xp->xp_pattern[1] == '|')
3465 {
3466 ++xp->xp_pattern;
3467 xp->xp_context = EXPAND_EXPRESSION;
3468 }
3469 else
3470 xp->xp_context = EXPAND_COMMANDS;
3471 }
3472 else
3473 xp->xp_context = EXPAND_EXPRESSION;
3474 }
3475 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003476 /* Doesn't look like something valid, expand as an expression
3477 * anyway. */
3478 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 arg = xp->xp_pattern;
3480 if (*arg != NUL)
3481 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3482 /* skip */ ;
3483 }
3484 xp->xp_pattern = arg;
3485}
3486
3487#endif /* FEAT_CMDL_COMPL */
3488
3489/*
3490 * ":1,25call func(arg1, arg2)" function call.
3491 */
3492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 char_u *arg = eap->arg;
3496 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003498 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 linenr_T lnum;
3502 int doesrange;
3503 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003504 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003505 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003507 if (eap->skip)
3508 {
3509 /* trans_function_name() doesn't work well when skipping, use eval0()
3510 * instead to skip to any following command, e.g. for:
3511 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003512 ++emsg_skip;
3513 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3514 clear_tv(&rettv);
3515 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003516 return;
3517 }
3518
Bram Moolenaar65639032016-03-16 21:40:30 +01003519 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003520 if (fudi.fd_newkey != NULL)
3521 {
3522 /* Still need to give an error message for missing key. */
3523 EMSG2(_(e_dictkey), fudi.fd_newkey);
3524 vim_free(fudi.fd_newkey);
3525 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003526 if (tofree == NULL)
3527 return;
3528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 /* Increase refcount on dictionary, it could get deleted when evaluating
3530 * the arguments. */
3531 if (fudi.fd_dict != NULL)
3532 ++fudi.fd_dict->dv_refcount;
3533
Bram Moolenaar65639032016-03-16 21:40:30 +01003534 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3535 * contents. For VAR_PARTIAL get its partial, unless we already have one
3536 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003537 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003538 name = deref_func_name(tofree, &len,
3539 partial != NULL ? NULL : &partial, FALSE);
3540
Bram Moolenaar532c7802005-01-27 14:44:31 +00003541 /* Skip white space to allow ":call func ()". Not good, but required for
3542 * backward compatibility. */
3543 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 if (*startarg != '(')
3547 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003548 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 goto end;
3550 }
3551
3552 /*
3553 * When skipping, evaluate the function once, to find the end of the
3554 * arguments.
3555 * When the function takes a range, this is discovered after the first
3556 * call, and the loop is broken.
3557 */
3558 if (eap->skip)
3559 {
3560 ++emsg_skip;
3561 lnum = eap->line2; /* do it once, also with an invalid range */
3562 }
3563 else
3564 lnum = eap->line1;
3565 for ( ; lnum <= eap->line2; ++lnum)
3566 {
3567 if (!eap->skip && eap->addr_count > 0)
3568 {
3569 curwin->w_cursor.lnum = lnum;
3570 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003571#ifdef FEAT_VIRTUALEDIT
3572 curwin->w_cursor.coladd = 0;
3573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003576 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003577 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003578 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 failed = TRUE;
3581 break;
3582 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003583
3584 /* Handle a function returning a Funcref, Dictionary or List. */
3585 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3586 {
3587 failed = TRUE;
3588 break;
3589 }
3590
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003591 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (doesrange || eap->skip)
3593 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003597 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003598 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (aborting())
3600 break;
3601 }
3602 if (eap->skip)
3603 --emsg_skip;
3604
3605 if (!failed)
3606 {
3607 /* Check for trailing illegal characters and a following command. */
3608 if (!ends_excmd(*arg))
3609 {
3610 emsg_severe = TRUE;
3611 EMSG(_(e_trailing));
3612 }
3613 else
3614 eap->nextcmd = check_nextcmd(arg);
3615 }
3616
3617end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003618 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003619 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620}
3621
3622/*
3623 * ":unlet[!] var1 ... " command.
3624 */
3625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 ex_unletlock(eap, eap->arg, 0);
3629}
3630
3631/*
3632 * ":lockvar" and ":unlockvar" commands
3633 */
3634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 int deep = 2;
3639
3640 if (eap->forceit)
3641 deep = -1;
3642 else if (vim_isdigit(*arg))
3643 {
3644 deep = getdigits(&arg);
3645 arg = skipwhite(arg);
3646 }
3647
3648 ex_unletlock(eap, arg, deep);
3649}
3650
3651/*
3652 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3653 */
3654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655ex_unletlock(
3656 exarg_T *eap,
3657 char_u *argstart,
3658 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003659{
3660 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 do
3666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003668 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003669 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 if (lv.ll_name == NULL)
3671 error = TRUE; /* error but continue parsing */
3672 if (name_end == NULL || (!vim_iswhite(*name_end)
3673 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 if (name_end != NULL)
3676 {
3677 emsg_severe = TRUE;
3678 EMSG(_(e_trailing));
3679 }
3680 if (!(eap->skip || error))
3681 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 break;
3683 }
3684
3685 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 {
3687 if (eap->cmdidx == CMD_unlet)
3688 {
3689 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3690 error = TRUE;
3691 }
3692 else
3693 {
3694 if (do_lock_var(&lv, name_end, deep,
3695 eap->cmdidx == CMD_lockvar) == FAIL)
3696 error = TRUE;
3697 }
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 if (!eap->skip)
3701 clear_lval(&lv);
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 arg = skipwhite(name_end);
3704 } while (!ends_excmd(*arg));
3705
3706 eap->nextcmd = check_nextcmd(arg);
3707}
3708
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710do_unlet_var(
3711 lval_T *lp,
3712 char_u *name_end,
3713 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 int ret = OK;
3716 int cc;
3717
3718 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003730 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003731 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003733 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 else if (lp->ll_range)
3736 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003738 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003739 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003740
3741 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3742 {
3743 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003744 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003745 return FAIL;
3746 ll_li = li;
3747 ++ll_n1;
3748 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003749
3750 /* Delete a range of List items. */
3751 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3752 {
3753 li = lp->ll_li->li_next;
3754 listitem_remove(lp->ll_list, lp->ll_li);
3755 lp->ll_li = li;
3756 ++lp->ll_n1;
3757 }
3758 }
3759 else
3760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003761 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003762 /* unlet a List item. */
3763 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003766 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003767 }
3768
3769 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003770}
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772/*
3773 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 */
3776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hashtab_T *ht;
3780 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003782 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003783 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 ht = find_var_ht(name, &varname);
3786 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003788 if (ht == &globvarht)
3789 d = &globvardict;
3790 else if (current_funccal != NULL
3791 && ht == &current_funccal->l_vars.dv_hashtab)
3792 d = &current_funccal->l_vars;
3793 else if (ht == &compat_hashtab)
3794 d = &vimvardict;
3795 else
3796 {
3797 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3798 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3799 }
3800 if (d == NULL)
3801 {
3802 EMSG2(_(e_intern2), "do_unlet()");
3803 return FAIL;
3804 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 hi = hash_find(ht, varname);
3806 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003808 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003809 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003810 || var_check_ro(di->di_flags, name, FALSE)
3811 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003812 return FAIL;
3813
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003814 delete_var(ht, hi);
3815 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 if (forceit)
3819 return OK;
3820 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822}
3823
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003824/*
3825 * Lock or unlock variable indicated by "lp".
3826 * "deep" is the levels to go (-1 for unlimited);
3827 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3828 */
3829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003830do_lock_var(
3831 lval_T *lp,
3832 char_u *name_end,
3833 int deep,
3834 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003835{
3836 int ret = OK;
3837 int cc;
3838 dictitem_T *di;
3839
3840 if (deep == 0) /* nothing to do */
3841 return OK;
3842
3843 if (lp->ll_tv == NULL)
3844 {
3845 cc = *name_end;
3846 *name_end = NUL;
3847
3848 /* Normal name or expanded name. */
3849 if (check_changedtick(lp->ll_name))
3850 ret = FAIL;
3851 else
3852 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003853 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003854 if (di == NULL)
3855 ret = FAIL;
3856 else
3857 {
3858 if (lock)
3859 di->di_flags |= DI_FLAGS_LOCK;
3860 else
3861 di->di_flags &= ~DI_FLAGS_LOCK;
3862 item_lock(&di->di_tv, deep, lock);
3863 }
3864 }
3865 *name_end = cc;
3866 }
3867 else if (lp->ll_range)
3868 {
3869 listitem_T *li = lp->ll_li;
3870
3871 /* (un)lock a range of List items. */
3872 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3873 {
3874 item_lock(&li->li_tv, deep, lock);
3875 li = li->li_next;
3876 ++lp->ll_n1;
3877 }
3878 }
3879 else if (lp->ll_list != NULL)
3880 /* (un)lock a List item. */
3881 item_lock(&lp->ll_li->li_tv, deep, lock);
3882 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003883 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003884 item_lock(&lp->ll_di->di_tv, deep, lock);
3885
3886 return ret;
3887}
3888
3889/*
3890 * Lock or unlock an item. "deep" is nr of levels to go.
3891 */
3892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003893item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003894{
3895 static int recurse = 0;
3896 list_T *l;
3897 listitem_T *li;
3898 dict_T *d;
3899 hashitem_T *hi;
3900 int todo;
3901
3902 if (recurse >= DICT_MAXNEST)
3903 {
3904 EMSG(_("E743: variable nested too deep for (un)lock"));
3905 return;
3906 }
3907 if (deep == 0)
3908 return;
3909 ++recurse;
3910
3911 /* lock/unlock the item itself */
3912 if (lock)
3913 tv->v_lock |= VAR_LOCKED;
3914 else
3915 tv->v_lock &= ~VAR_LOCKED;
3916
3917 switch (tv->v_type)
3918 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003919 case VAR_UNKNOWN:
3920 case VAR_NUMBER:
3921 case VAR_STRING:
3922 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003923 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003924 case VAR_FLOAT:
3925 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003926 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003927 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003928 break;
3929
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003930 case VAR_LIST:
3931 if ((l = tv->vval.v_list) != NULL)
3932 {
3933 if (lock)
3934 l->lv_lock |= VAR_LOCKED;
3935 else
3936 l->lv_lock &= ~VAR_LOCKED;
3937 if (deep < 0 || deep > 1)
3938 /* recursive: lock/unlock the items the List contains */
3939 for (li = l->lv_first; li != NULL; li = li->li_next)
3940 item_lock(&li->li_tv, deep - 1, lock);
3941 }
3942 break;
3943 case VAR_DICT:
3944 if ((d = tv->vval.v_dict) != NULL)
3945 {
3946 if (lock)
3947 d->dv_lock |= VAR_LOCKED;
3948 else
3949 d->dv_lock &= ~VAR_LOCKED;
3950 if (deep < 0 || deep > 1)
3951 {
3952 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003953 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003954 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3955 {
3956 if (!HASHITEM_EMPTY(hi))
3957 {
3958 --todo;
3959 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3960 }
3961 }
3962 }
3963 }
3964 }
3965 --recurse;
3966}
3967
Bram Moolenaara40058a2005-07-11 22:42:07 +00003968/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003969 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3970 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003971 */
3972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003974{
3975 return (tv->v_lock & VAR_LOCKED)
3976 || (tv->v_type == VAR_LIST
3977 && tv->vval.v_list != NULL
3978 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3979 || (tv->v_type == VAR_DICT
3980 && tv->vval.v_dict != NULL
3981 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3982}
3983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3985/*
3986 * Delete all "menutrans_" variables.
3987 */
3988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003989del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003992 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaar33570922005-01-25 22:26:29 +00003994 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003995 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003997 {
3998 if (!HASHITEM_EMPTY(hi))
3999 {
4000 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4002 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 }
4004 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006}
4007#endif
4008
4009#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4010
4011/*
4012 * Local string buffer for the next two functions to store a variable name
4013 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4014 * get_user_var_name().
4015 */
4016
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004017static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
4019static char_u *varnamebuf = NULL;
4020static int varnamebuflen = 0;
4021
4022/*
4023 * Function to concatenate a prefix and a variable name.
4024 */
4025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004026cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 int len;
4029
4030 len = (int)STRLEN(name) + 3;
4031 if (len > varnamebuflen)
4032 {
4033 vim_free(varnamebuf);
4034 len += 10; /* some additional space */
4035 varnamebuf = alloc(len);
4036 if (varnamebuf == NULL)
4037 {
4038 varnamebuflen = 0;
4039 return NULL;
4040 }
4041 varnamebuflen = len;
4042 }
4043 *varnamebuf = prefix;
4044 varnamebuf[1] = ':';
4045 STRCPY(varnamebuf + 2, name);
4046 return varnamebuf;
4047}
4048
4049/*
4050 * Function given to ExpandGeneric() to obtain the list of user defined
4051 * (global/buffer/window/built-in) variable names.
4052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004054get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 static long_u gdone;
4057 static long_u bdone;
4058 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059#ifdef FEAT_WINDOWS
4060 static long_u tdone;
4061#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 static int vidx;
4063 static hashitem_T *hi;
4064 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004068 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 tdone = 0;
4071#endif
4072 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004073
4074 /* Global variables */
4075 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004077 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004079 else
4080 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004081 while (HASHITEM_EMPTY(hi))
4082 ++hi;
4083 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4084 return cat_prefix_varname('g', hi->hi_key);
4085 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004087
4088 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004089 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004092 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004094 else
4095 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 while (HASHITEM_EMPTY(hi))
4097 ++hi;
4098 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004102 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return (char_u *)"b:changedtick";
4104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004105
4106 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004107 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004110 if (wdone++ == 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('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004118
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004119#ifdef FEAT_WINDOWS
4120 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004121 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004122 if (tdone < ht->ht_used)
4123 {
4124 if (tdone++ == 0)
4125 hi = ht->ht_array;
4126 else
4127 ++hi;
4128 while (HASHITEM_EMPTY(hi))
4129 ++hi;
4130 return cat_prefix_varname('t', hi->hi_key);
4131 }
4132#endif
4133
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 /* v: variables */
4135 if (vidx < VV_LEN)
4136 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 vim_free(varnamebuf);
4139 varnamebuf = NULL;
4140 varnamebuflen = 0;
4141 return NULL;
4142}
4143
4144#endif /* FEAT_CMDL_COMPL */
4145
4146/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004147 * Return TRUE if "pat" matches "text".
4148 * Does not use 'cpo' and always uses 'magic'.
4149 */
4150 static int
4151pattern_match(char_u *pat, char_u *text, int ic)
4152{
4153 int matches = FALSE;
4154 char_u *save_cpo;
4155 regmatch_T regmatch;
4156
4157 /* avoid 'l' flag in 'cpoptions' */
4158 save_cpo = p_cpo;
4159 p_cpo = (char_u *)"";
4160 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4161 if (regmatch.regprog != NULL)
4162 {
4163 regmatch.rm_ic = ic;
4164 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4165 vim_regfree(regmatch.regprog);
4166 }
4167 p_cpo = save_cpo;
4168 return matches;
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * types for expressions.
4173 */
4174typedef enum
4175{
4176 TYPE_UNKNOWN = 0
4177 , TYPE_EQUAL /* == */
4178 , TYPE_NEQUAL /* != */
4179 , TYPE_GREATER /* > */
4180 , TYPE_GEQUAL /* >= */
4181 , TYPE_SMALLER /* < */
4182 , TYPE_SEQUAL /* <= */
4183 , TYPE_MATCH /* =~ */
4184 , TYPE_NOMATCH /* !~ */
4185} exptype_T;
4186
4187/*
4188 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4191 */
4192
4193/*
4194 * Handle zero level expression.
4195 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004197 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 * Return OK or FAIL.
4199 */
4200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201eval0(
4202 char_u *arg,
4203 typval_T *rettv,
4204 char_u **nextcmd,
4205 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 int ret;
4208 char_u *p;
4209
4210 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 if (ret == FAIL || !ends_excmd(*p))
4213 {
4214 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 /*
4217 * Report the invalid expression unless the expression evaluation has
4218 * been cancelled due to an aborting error, an interrupt, or an
4219 * exception.
4220 */
4221 if (!aborting())
4222 EMSG2(_(e_invexpr2), arg);
4223 ret = FAIL;
4224 }
4225 if (nextcmd != NULL)
4226 *nextcmd = check_nextcmd(p);
4227
4228 return ret;
4229}
4230
4231/*
4232 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004233 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 *
4235 * "arg" must point to the first non-white of the expression.
4236 * "arg" is advanced to the next non-white after the recognized expression.
4237 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004238 * Note: "rettv.v_lock" is not set.
4239 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 * Return OK or FAIL.
4241 */
4242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004243eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 /*
4249 * Get the first variable.
4250 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return FAIL;
4253
4254 if ((*arg)[0] == '?')
4255 {
4256 result = FALSE;
4257 if (evaluate)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 int error = FALSE;
4260
4261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 return FAIL;
4274
4275 /*
4276 * Check for the ":".
4277 */
4278 if ((*arg)[0] != ':')
4279 {
4280 EMSG(_("E109: Missing ':' after '?'"));
4281 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return FAIL;
4284 }
4285
4286 /*
4287 * Get the third variable.
4288 */
4289 *arg = skipwhite(*arg + 1);
4290 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4291 {
4292 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 return FAIL;
4295 }
4296 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle first level expression:
4305 * expr2 || expr2 || expr2 logical OR
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004313eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 long result;
4317 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 /*
4321 * Get the first variable.
4322 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 return FAIL;
4325
4326 /*
4327 * Repeat until there is no following "||".
4328 */
4329 first = TRUE;
4330 result = FALSE;
4331 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4332 {
4333 if (evaluate && first)
4334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (error)
4339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 first = FALSE;
4341 }
4342
4343 /*
4344 * Get the second variable.
4345 */
4346 *arg = skipwhite(*arg + 2);
4347 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4348 return FAIL;
4349
4350 /*
4351 * Compute the result.
4352 */
4353 if (evaluate && !result)
4354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (error)
4359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 if (evaluate)
4362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 rettv->v_type = VAR_NUMBER;
4364 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 }
4367
4368 return OK;
4369}
4370
4371/*
4372 * Handle second level expression:
4373 * expr3 && expr3 && expr3 logical AND
4374 *
4375 * "arg" must point to the first non-white of the expression.
4376 * "arg" is advanced to the next non-white after the recognized expression.
4377 *
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004381eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 long result;
4385 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388 /*
4389 * Get the first variable.
4390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return FAIL;
4393
4394 /*
4395 * Repeat until there is no following "&&".
4396 */
4397 first = TRUE;
4398 result = TRUE;
4399 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4400 {
4401 if (evaluate && first)
4402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (error)
4407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 first = FALSE;
4409 }
4410
4411 /*
4412 * Get the second variable.
4413 */
4414 *arg = skipwhite(*arg + 2);
4415 if (eval4(arg, &var2, evaluate && result) == FAIL)
4416 return FAIL;
4417
4418 /*
4419 * Compute the result.
4420 */
4421 if (evaluate && result)
4422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426 if (error)
4427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 if (evaluate)
4430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 rettv->v_type = VAR_NUMBER;
4432 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 }
4435
4436 return OK;
4437}
4438
4439/*
4440 * Handle third level expression:
4441 * var1 == var2
4442 * var1 =~ var2
4443 * var1 != var2
4444 * var1 !~ var2
4445 * var1 > var2
4446 * var1 >= var2
4447 * var1 < var2
4448 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 * var1 is var2
4450 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 *
4452 * "arg" must point to the first non-white of the expression.
4453 * "arg" is advanced to the next non-white after the recognized expression.
4454 *
4455 * Return OK or FAIL.
4456 */
4457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004458eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 char_u *p;
4462 int i;
4463 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 int len = 2;
4466 long n1, n2;
4467 char_u *s1, *s2;
4468 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /*
4472 * Get the first variable.
4473 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return FAIL;
4476
4477 p = *arg;
4478 switch (p[0])
4479 {
4480 case '=': if (p[1] == '=')
4481 type = TYPE_EQUAL;
4482 else if (p[1] == '~')
4483 type = TYPE_MATCH;
4484 break;
4485 case '!': if (p[1] == '=')
4486 type = TYPE_NEQUAL;
4487 else if (p[1] == '~')
4488 type = TYPE_NOMATCH;
4489 break;
4490 case '>': if (p[1] != '=')
4491 {
4492 type = TYPE_GREATER;
4493 len = 1;
4494 }
4495 else
4496 type = TYPE_GEQUAL;
4497 break;
4498 case '<': if (p[1] != '=')
4499 {
4500 type = TYPE_SMALLER;
4501 len = 1;
4502 }
4503 else
4504 type = TYPE_SEQUAL;
4505 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 case 'i': if (p[1] == 's')
4507 {
4508 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4509 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004510 i = p[len];
4511 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 {
4513 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4514 type_is = TRUE;
4515 }
4516 }
4517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519
4520 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004521 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
4523 if (type != TYPE_UNKNOWN)
4524 {
4525 /* extra question mark appended: ignore case */
4526 if (p[len] == '?')
4527 {
4528 ic = TRUE;
4529 ++len;
4530 }
4531 /* extra '#' appended: match case */
4532 else if (p[len] == '#')
4533 {
4534 ic = FALSE;
4535 ++len;
4536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004537 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
4539 ic = p_ic;
4540
4541 /*
4542 * Get the second variable.
4543 */
4544 *arg = skipwhite(p + len);
4545 if (eval5(arg, &var2, evaluate) == FAIL)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 return FAIL;
4549 }
4550
4551 if (evaluate)
4552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 if (type_is && rettv->v_type != var2.v_type)
4554 {
4555 /* For "is" a different type always means FALSE, for "notis"
4556 * it means TRUE. */
4557 n1 = (type == TYPE_NEQUAL);
4558 }
4559 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4560 {
4561 if (type_is)
4562 {
4563 n1 = (rettv->v_type == var2.v_type
4564 && rettv->vval.v_list == var2.vval.v_list);
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 else if (rettv->v_type != var2.v_type
4569 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4570 {
4571 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004572 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004573 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004574 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 clear_tv(rettv);
4576 clear_tv(&var2);
4577 return FAIL;
4578 }
4579 else
4580 {
4581 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004582 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4583 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004584 if (type == TYPE_NEQUAL)
4585 n1 = !n1;
4586 }
4587 }
4588
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004589 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4590 {
4591 if (type_is)
4592 {
4593 n1 = (rettv->v_type == var2.v_type
4594 && rettv->vval.v_dict == var2.vval.v_dict);
4595 if (type == TYPE_NEQUAL)
4596 n1 = !n1;
4597 }
4598 else if (rettv->v_type != var2.v_type
4599 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4600 {
4601 if (rettv->v_type != var2.v_type)
4602 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4603 else
4604 EMSG(_("E736: Invalid operation for Dictionary"));
4605 clear_tv(rettv);
4606 clear_tv(&var2);
4607 return FAIL;
4608 }
4609 else
4610 {
4611 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004612 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4613 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004614 if (type == TYPE_NEQUAL)
4615 n1 = !n1;
4616 }
4617 }
4618
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004619 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4620 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004621 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004622 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004624 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 clear_tv(rettv);
4626 clear_tv(&var2);
4627 return FAIL;
4628 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004629 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004630 if (type == TYPE_NEQUAL)
4631 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004632 }
4633
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634#ifdef FEAT_FLOAT
4635 /*
4636 * If one of the two variables is a float, compare as a float.
4637 * When using "=~" or "!~", always compare as string.
4638 */
4639 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4640 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4641 {
4642 float_T f1, f2;
4643
4644 if (rettv->v_type == VAR_FLOAT)
4645 f1 = rettv->vval.v_float;
4646 else
4647 f1 = get_tv_number(rettv);
4648 if (var2.v_type == VAR_FLOAT)
4649 f2 = var2.vval.v_float;
4650 else
4651 f2 = get_tv_number(&var2);
4652 n1 = FALSE;
4653 switch (type)
4654 {
4655 case TYPE_EQUAL: n1 = (f1 == f2); break;
4656 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4657 case TYPE_GREATER: n1 = (f1 > f2); break;
4658 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4659 case TYPE_SMALLER: n1 = (f1 < f2); break;
4660 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4661 case TYPE_UNKNOWN:
4662 case TYPE_MATCH:
4663 case TYPE_NOMATCH: break; /* avoid gcc warning */
4664 }
4665 }
4666#endif
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 /*
4669 * If one of the two variables is a number, compare as a number.
4670 * When using "=~" or "!~", always compare as string.
4671 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004672 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 n1 = get_tv_number(rettv);
4676 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 switch (type)
4678 {
4679 case TYPE_EQUAL: n1 = (n1 == n2); break;
4680 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4681 case TYPE_GREATER: n1 = (n1 > n2); break;
4682 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4683 case TYPE_SMALLER: n1 = (n1 < n2); break;
4684 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4685 case TYPE_UNKNOWN:
4686 case TYPE_MATCH:
4687 case TYPE_NOMATCH: break; /* avoid gcc warning */
4688 }
4689 }
4690 else
4691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 s1 = get_tv_string_buf(rettv, buf1);
4693 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4695 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4696 else
4697 i = 0;
4698 n1 = FALSE;
4699 switch (type)
4700 {
4701 case TYPE_EQUAL: n1 = (i == 0); break;
4702 case TYPE_NEQUAL: n1 = (i != 0); break;
4703 case TYPE_GREATER: n1 = (i > 0); break;
4704 case TYPE_GEQUAL: n1 = (i >= 0); break;
4705 case TYPE_SMALLER: n1 = (i < 0); break;
4706 case TYPE_SEQUAL: n1 = (i <= 0); break;
4707
4708 case TYPE_MATCH:
4709 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004710 n1 = pattern_match(s2, s1, ic);
4711 if (type == TYPE_NOMATCH)
4712 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 break;
4714
4715 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4716 }
4717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718 clear_tv(rettv);
4719 clear_tv(&var2);
4720 rettv->v_type = VAR_NUMBER;
4721 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
4723 }
4724
4725 return OK;
4726}
4727
4728/*
4729 * Handle fourth level expression:
4730 * + number addition
4731 * - number subtraction
4732 * . string concatenation
4733 *
4734 * "arg" must point to the first non-white of the expression.
4735 * "arg" is advanced to the next non-white after the recognized expression.
4736 *
4737 * Return OK or FAIL.
4738 */
4739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004740eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
Bram Moolenaar33570922005-01-25 22:26:29 +00004742 typval_T var2;
4743 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 int op;
4745 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 float_T f1 = 0, f2 = 0;
4748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 char_u *s1, *s2;
4750 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4751 char_u *p;
4752
4753 /*
4754 * Get the first variable.
4755 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004756 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 return FAIL;
4758
4759 /*
4760 * Repeat computing, until no '+', '-' or '.' is following.
4761 */
4762 for (;;)
4763 {
4764 op = **arg;
4765 if (op != '+' && op != '-' && op != '.')
4766 break;
4767
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768 if ((op != '+' || rettv->v_type != VAR_LIST)
4769#ifdef FEAT_FLOAT
4770 && (op == '.' || rettv->v_type != VAR_FLOAT)
4771#endif
4772 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 {
4774 /* For "list + ...", an illegal use of the first operand as
4775 * a number cannot be determined before evaluating the 2nd
4776 * operand: if this is also a list, all is ok.
4777 * For "something . ...", "something - ..." or "non-list + ...",
4778 * we know that the first operand needs to be a string or number
4779 * without evaluating the 2nd operand. So check before to avoid
4780 * side effects after an error. */
4781 if (evaluate && get_tv_string_chk(rettv) == NULL)
4782 {
4783 clear_tv(rettv);
4784 return FAIL;
4785 }
4786 }
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 /*
4789 * Get the second variable.
4790 */
4791 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796 }
4797
4798 if (evaluate)
4799 {
4800 /*
4801 * Compute the result.
4802 */
4803 if (op == '.')
4804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4806 s2 = get_tv_string_buf_chk(&var2, buf2);
4807 if (s2 == NULL) /* type error ? */
4808 {
4809 clear_tv(rettv);
4810 clear_tv(&var2);
4811 return FAIL;
4812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004813 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
4815 rettv->v_type = VAR_STRING;
4816 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004818 else if (op == '+' && rettv->v_type == VAR_LIST
4819 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004820 {
4821 /* concatenate Lists */
4822 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4823 &var3) == FAIL)
4824 {
4825 clear_tv(rettv);
4826 clear_tv(&var2);
4827 return FAIL;
4828 }
4829 clear_tv(rettv);
4830 *rettv = var3;
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 else
4833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 int error = FALSE;
4835
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836#ifdef FEAT_FLOAT
4837 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839 f1 = rettv->vval.v_float;
4840 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842 else
4843#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845 n1 = get_tv_number_chk(rettv, &error);
4846 if (error)
4847 {
4848 /* This can only happen for "list + non-list". For
4849 * "non-list + ..." or "something - ...", we returned
4850 * before evaluating the 2nd operand. */
4851 clear_tv(rettv);
4852 return FAIL;
4853 }
4854#ifdef FEAT_FLOAT
4855 if (var2.v_type == VAR_FLOAT)
4856 f1 = n1;
4857#endif
4858 }
4859#ifdef FEAT_FLOAT
4860 if (var2.v_type == VAR_FLOAT)
4861 {
4862 f2 = var2.vval.v_float;
4863 n2 = 0;
4864 }
4865 else
4866#endif
4867 {
4868 n2 = get_tv_number_chk(&var2, &error);
4869 if (error)
4870 {
4871 clear_tv(rettv);
4872 clear_tv(&var2);
4873 return FAIL;
4874 }
4875#ifdef FEAT_FLOAT
4876 if (rettv->v_type == VAR_FLOAT)
4877 f2 = n2;
4878#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881
4882#ifdef FEAT_FLOAT
4883 /* If there is a float on either side the result is a float. */
4884 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4885 {
4886 if (op == '+')
4887 f1 = f1 + f2;
4888 else
4889 f1 = f1 - f2;
4890 rettv->v_type = VAR_FLOAT;
4891 rettv->vval.v_float = f1;
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#endif
4895 {
4896 if (op == '+')
4897 n1 = n1 + n2;
4898 else
4899 n1 = n1 - n2;
4900 rettv->v_type = VAR_NUMBER;
4901 rettv->vval.v_number = n1;
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 }
4907 return OK;
4908}
4909
4910/*
4911 * Handle fifth level expression:
4912 * * number multiplication
4913 * / number division
4914 * % number modulo
4915 *
4916 * "arg" must point to the first non-white of the expression.
4917 * "arg" is advanced to the next non-white after the recognized expression.
4918 *
4919 * Return OK or FAIL.
4920 */
4921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004922eval6(
4923 char_u **arg,
4924 typval_T *rettv,
4925 int evaluate,
4926 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
Bram Moolenaar33570922005-01-25 22:26:29 +00004928 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 int op;
4930 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#ifdef FEAT_FLOAT
4932 int use_float = FALSE;
4933 float_T f1 = 0, f2;
4934#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004935 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
4937 /*
4938 * Get the first variable.
4939 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004940 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 return FAIL;
4942
4943 /*
4944 * Repeat computing, until no '*', '/' or '%' is following.
4945 */
4946 for (;;)
4947 {
4948 op = **arg;
4949 if (op != '*' && op != '/' && op != '%')
4950 break;
4951
4952 if (evaluate)
4953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954#ifdef FEAT_FLOAT
4955 if (rettv->v_type == VAR_FLOAT)
4956 {
4957 f1 = rettv->vval.v_float;
4958 use_float = TRUE;
4959 n1 = 0;
4960 }
4961 else
4962#endif
4963 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004965 if (error)
4966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else
4969 n1 = 0;
4970
4971 /*
4972 * Get the second variable.
4973 */
4974 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004975 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 return FAIL;
4977
4978 if (evaluate)
4979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980#ifdef FEAT_FLOAT
4981 if (var2.v_type == VAR_FLOAT)
4982 {
4983 if (!use_float)
4984 {
4985 f1 = n1;
4986 use_float = TRUE;
4987 }
4988 f2 = var2.vval.v_float;
4989 n2 = 0;
4990 }
4991 else
4992#endif
4993 {
4994 n2 = get_tv_number_chk(&var2, &error);
4995 clear_tv(&var2);
4996 if (error)
4997 return FAIL;
4998#ifdef FEAT_FLOAT
4999 if (use_float)
5000 f2 = n2;
5001#endif
5002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004 /*
5005 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008#ifdef FEAT_FLOAT
5009 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 f1 = f1 * f2;
5013 else if (op == '/')
5014 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005015# ifdef VMS
5016 /* VMS crashes on divide by zero, work around it */
5017 if (f2 == 0.0)
5018 {
5019 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005020 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005021 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005022 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005023 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005024 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005025 }
5026 else
5027 f1 = f1 / f2;
5028# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 /* We rely on the floating point library to handle divide
5030 * by zero to result in "inf" and not a crash. */
5031 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005032# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005036 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 return FAIL;
5038 }
5039 rettv->v_type = VAR_FLOAT;
5040 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 if (op == '*')
5046 n1 = n1 * n2;
5047 else if (op == '/')
5048 {
5049 if (n2 == 0) /* give an error message? */
5050 {
5051 if (n1 == 0)
5052 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5053 else if (n1 < 0)
5054 n1 = -0x7fffffffL;
5055 else
5056 n1 = 0x7fffffffL;
5057 }
5058 else
5059 n1 = n1 / n2;
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005062 {
5063 if (n2 == 0) /* give an error message? */
5064 n1 = 0;
5065 else
5066 n1 = n1 % n2;
5067 }
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 }
5073
5074 return OK;
5075}
5076
5077/*
5078 * Handle sixth level expression:
5079 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005080 * "string" string constant
5081 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 * &option-name option value
5083 * @r register contents
5084 * identifier variable value
5085 * function() function call
5086 * $VAR environment variable
5087 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005088 * [expr, expr] List
5089 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 *
5091 * Also handle:
5092 * ! in front logical NOT
5093 * - in front unary minus
5094 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 * trailing [] subscript in String or List
5096 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 *
5098 * "arg" must point to the first non-white of the expression.
5099 * "arg" is advanced to the next non-white after the recognized expression.
5100 *
5101 * Return OK or FAIL.
5102 */
5103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104eval7(
5105 char_u **arg,
5106 typval_T *rettv,
5107 int evaluate,
5108 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 long n;
5111 int len;
5112 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 char_u *start_leader, *end_leader;
5114 int ret = OK;
5115 char_u *alias;
5116
5117 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 /*
5124 * Skip '!' and '-' characters. They are handled later.
5125 */
5126 start_leader = *arg;
5127 while (**arg == '!' || **arg == '-' || **arg == '+')
5128 *arg = skipwhite(*arg + 1);
5129 end_leader = *arg;
5130
5131 switch (**arg)
5132 {
5133 /*
5134 * Number constant.
5135 */
5136 case '0':
5137 case '1':
5138 case '2':
5139 case '3':
5140 case '4':
5141 case '5':
5142 case '6':
5143 case '7':
5144 case '8':
5145 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 {
5147#ifdef FEAT_FLOAT
5148 char_u *p = skipdigits(*arg + 1);
5149 int get_float = FALSE;
5150
5151 /* We accept a float when the format matches
5152 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005153 * strict to avoid backwards compatibility problems.
5154 * Don't look for a float after the "." operator, so that
5155 * ":let vers = 1.2.3" doesn't fail. */
5156 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005158 get_float = TRUE;
5159 p = skipdigits(p + 2);
5160 if (*p == 'e' || *p == 'E')
5161 {
5162 ++p;
5163 if (*p == '-' || *p == '+')
5164 ++p;
5165 if (!vim_isdigit(*p))
5166 get_float = FALSE;
5167 else
5168 p = skipdigits(p + 1);
5169 }
5170 if (ASCII_ISALPHA(*p) || *p == '.')
5171 get_float = FALSE;
5172 }
5173 if (get_float)
5174 {
5175 float_T f;
5176
5177 *arg += string2float(*arg, &f);
5178 if (evaluate)
5179 {
5180 rettv->v_type = VAR_FLOAT;
5181 rettv->vval.v_float = f;
5182 }
5183 }
5184 else
5185#endif
5186 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005187 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005188 *arg += len;
5189 if (evaluate)
5190 {
5191 rettv->v_type = VAR_NUMBER;
5192 rettv->vval.v_number = n;
5193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198 /*
5199 * String constant: "string".
5200 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203
5204 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 break;
5209
5210 /*
5211 * List: [expr, expr]
5212 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215
5216 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 * Dictionary: {key: val, key: val}
5218 */
5219 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5220 break;
5221
5222 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005223 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005225 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 break;
5227
5228 /*
5229 * Environment variable: $VAR.
5230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233
5234 /*
5235 * Register contents: @r.
5236 */
5237 case '@': ++*arg;
5238 if (evaluate)
5239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005240 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005241 rettv->vval.v_string = get_reg_contents(**arg,
5242 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 if (**arg != NUL)
5245 ++*arg;
5246 break;
5247
5248 /*
5249 * nested expression: (expression).
5250 */
5251 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (**arg == ')')
5254 ++*arg;
5255 else if (ret == OK)
5256 {
5257 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 ret = FAIL;
5260 }
5261 break;
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 break;
5265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266
5267 if (ret == NOTDONE)
5268 {
5269 /*
5270 * Must be a variable or function name.
5271 * Can also be a curly-braces kind of name: {expr}.
5272 */
5273 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 if (alias != NULL)
5276 s = alias;
5277
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 ret = FAIL;
5280 else
5281 {
5282 if (**arg == '(') /* recursive! */
5283 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005284 partial_T *partial;
5285
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 /* If "s" is the name of a variable of type VAR_FUNC
5287 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005288 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289
5290 /* Invoke the function. */
5291 ret = get_func_tv(s, len, rettv, arg,
5292 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005293 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005294
5295 /* If evaluate is FALSE rettv->v_type was not set in
5296 * get_func_tv, but it's needed in handle_subscript() to parse
5297 * what follows. So set it here. */
5298 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5299 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005300 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005301 rettv->v_type = VAR_FUNC;
5302 }
5303
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 /* Stop the expression evaluation when immediately
5305 * aborting on error, or when an interrupt occurred or
5306 * an exception was thrown but not caught. */
5307 if (aborting())
5308 {
5309 if (ret == OK)
5310 clear_tv(rettv);
5311 ret = FAIL;
5312 }
5313 }
5314 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005315 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005316 else
5317 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005319 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 }
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 *arg = skipwhite(*arg);
5323
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005324 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5325 * expr(expr). */
5326 if (ret == OK)
5327 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 /*
5330 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5331 */
5332 if (ret == OK && evaluate && end_leader > start_leader)
5333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005335 int val = 0;
5336#ifdef FEAT_FLOAT
5337 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339 if (rettv->v_type == VAR_FLOAT)
5340 f = rettv->vval.v_float;
5341 else
5342#endif
5343 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 clear_tv(rettv);
5347 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 else
5350 {
5351 while (end_leader > start_leader)
5352 {
5353 --end_leader;
5354 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005355 {
5356#ifdef FEAT_FLOAT
5357 if (rettv->v_type == VAR_FLOAT)
5358 f = !f;
5359 else
5360#endif
5361 val = !val;
5362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005364 {
5365#ifdef FEAT_FLOAT
5366 if (rettv->v_type == VAR_FLOAT)
5367 f = -f;
5368 else
5369#endif
5370 val = -val;
5371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005372 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005373#ifdef FEAT_FLOAT
5374 if (rettv->v_type == VAR_FLOAT)
5375 {
5376 clear_tv(rettv);
5377 rettv->vval.v_float = f;
5378 }
5379 else
5380#endif
5381 {
5382 clear_tv(rettv);
5383 rettv->v_type = VAR_NUMBER;
5384 rettv->vval.v_number = val;
5385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388
5389 return ret;
5390}
5391
5392/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005393 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5394 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5396 */
5397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005398eval_index(
5399 char_u **arg,
5400 typval_T *rettv,
5401 int evaluate,
5402 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403{
5404 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 long len = -1;
5408 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005410 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005414 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005415 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005416 if (verbose)
5417 EMSG(_("E695: Cannot index a Funcref"));
5418 return FAIL;
5419 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005420#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005421 if (verbose)
5422 EMSG(_(e_float_as_string));
5423 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005424#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005425 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005426 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005427 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005428 if (verbose)
5429 EMSG(_("E909: Cannot index a special variable"));
5430 return FAIL;
5431 case VAR_UNKNOWN:
5432 if (evaluate)
5433 return FAIL;
5434 /* FALLTHROUGH */
5435
5436 case VAR_STRING:
5437 case VAR_NUMBER:
5438 case VAR_LIST:
5439 case VAR_DICT:
5440 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005443 init_tv(&var1);
5444 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 /*
5448 * dict.name
5449 */
5450 key = *arg + 1;
5451 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5452 ;
5453 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 /*
5460 * something[idx]
5461 *
5462 * Get the (first) variable from inside the [].
5463 */
5464 *arg = skipwhite(*arg + 1);
5465 if (**arg == ':')
5466 empty1 = TRUE;
5467 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5468 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005469 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5470 {
5471 /* not a number or string */
5472 clear_tv(&var1);
5473 return FAIL;
5474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475
5476 /*
5477 * Get the second variable from inside the [:].
5478 */
5479 if (**arg == ':')
5480 {
5481 range = TRUE;
5482 *arg = skipwhite(*arg + 1);
5483 if (**arg == ']')
5484 empty2 = TRUE;
5485 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005487 if (!empty1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5492 {
5493 /* not a number or string */
5494 if (!empty1)
5495 clear_tv(&var1);
5496 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 return FAIL;
5498 }
5499 }
5500
5501 /* Check for the ']'. */
5502 if (**arg != ']')
5503 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005504 if (verbose)
5505 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 clear_tv(&var1);
5507 if (range)
5508 clear_tv(&var2);
5509 return FAIL;
5510 }
5511 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 }
5513
5514 if (evaluate)
5515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 n1 = 0;
5517 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 n1 = get_tv_number(&var1);
5520 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 }
5522 if (range)
5523 {
5524 if (empty2)
5525 n2 = -1;
5526 else
5527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 n2 = get_tv_number(&var2);
5529 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005535 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005536 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005537 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005538 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005539 case VAR_SPECIAL:
5540 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005541 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005542 break; /* not evaluating, skipping over subscript */
5543
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 case VAR_NUMBER:
5545 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 len = (long)STRLEN(s);
5548 if (range)
5549 {
5550 /* The resulting variable is a substring. If the indexes
5551 * are out of range the result is empty. */
5552 if (n1 < 0)
5553 {
5554 n1 = len + n1;
5555 if (n1 < 0)
5556 n1 = 0;
5557 }
5558 if (n2 < 0)
5559 n2 = len + n2;
5560 else if (n2 >= len)
5561 n2 = len;
5562 if (n1 >= len || n2 < 0 || n1 > n2)
5563 s = NULL;
5564 else
5565 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5566 }
5567 else
5568 {
5569 /* The resulting variable is a string of a single
5570 * character. If the index is too big or negative the
5571 * result is empty. */
5572 if (n1 >= len || n1 < 0)
5573 s = NULL;
5574 else
5575 s = vim_strnsave(s + n1, 1);
5576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 clear_tv(rettv);
5578 rettv->v_type = VAR_STRING;
5579 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 break;
5581
5582 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 if (n1 < 0)
5585 n1 = len + n1;
5586 if (!empty1 && (n1 < 0 || n1 >= len))
5587 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005588 /* For a range we allow invalid values and return an empty
5589 * list. A list index out of range is an error. */
5590 if (!range)
5591 {
5592 if (verbose)
5593 EMSGN(_(e_listidx), n1);
5594 return FAIL;
5595 }
5596 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 }
5598 if (range)
5599 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 list_T *l;
5601 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602
5603 if (n2 < 0)
5604 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005605 else if (n2 >= len)
5606 n2 = len - 1;
5607 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005608 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 l = list_alloc();
5610 if (l == NULL)
5611 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 n1 <= n2; ++n1)
5614 {
5615 if (list_append_tv(l, &item->li_tv) == FAIL)
5616 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005617 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 return FAIL;
5619 }
5620 item = item->li_next;
5621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622 clear_tv(rettv);
5623 rettv->v_type = VAR_LIST;
5624 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005625 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 }
5627 else
5628 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005629 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 clear_tv(rettv);
5631 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 }
5633 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634
5635 case VAR_DICT:
5636 if (range)
5637 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005638 if (verbose)
5639 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 if (len == -1)
5641 clear_tv(&var1);
5642 return FAIL;
5643 }
5644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005645 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646
5647 if (len == -1)
5648 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005649 key = get_tv_string_chk(&var1);
5650 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 clear_tv(&var1);
5653 return FAIL;
5654 }
5655 }
5656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005657 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005659 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005660 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005661 if (len == -1)
5662 clear_tv(&var1);
5663 if (item == NULL)
5664 return FAIL;
5665
5666 copy_tv(&item->di_tv, &var1);
5667 clear_tv(rettv);
5668 *rettv = var1;
5669 }
5670 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 }
5672 }
5673
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 return OK;
5675}
5676
5677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 * Get an option value.
5679 * "arg" points to the '&' or '+' before the option name.
5680 * "arg" is advanced to character after the option name.
5681 * Return OK or FAIL.
5682 */
5683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005684get_option_tv(
5685 char_u **arg,
5686 typval_T *rettv, /* when NULL, only check if option exists */
5687 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 char_u *option_end;
5690 long numval;
5691 char_u *stringval;
5692 int opt_type;
5693 int c;
5694 int working = (**arg == '+'); /* has("+option") */
5695 int ret = OK;
5696 int opt_flags;
5697
5698 /*
5699 * Isolate the option name and find its value.
5700 */
5701 option_end = find_option_end(arg, &opt_flags);
5702 if (option_end == NULL)
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 EMSG2(_("E112: Option name missing: %s"), *arg);
5706 return FAIL;
5707 }
5708
5709 if (!evaluate)
5710 {
5711 *arg = option_end;
5712 return OK;
5713 }
5714
5715 c = *option_end;
5716 *option_end = NUL;
5717 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005718 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 if (opt_type == -3) /* invalid name */
5721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005722 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 EMSG2(_("E113: Unknown option: %s"), *arg);
5724 ret = FAIL;
5725 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 if (opt_type == -2) /* hidden string option */
5729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730 rettv->v_type = VAR_STRING;
5731 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 else if (opt_type == -1) /* hidden number option */
5734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005735 rettv->v_type = VAR_NUMBER;
5736 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738 else if (opt_type == 1) /* number option */
5739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740 rettv->v_type = VAR_NUMBER;
5741 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743 else /* string option */
5744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005745 rettv->v_type = VAR_STRING;
5746 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 }
5749 else if (working && (opt_type == -2 || opt_type == -1))
5750 ret = FAIL;
5751
5752 *option_end = c; /* put back for error messages */
5753 *arg = option_end;
5754
5755 return ret;
5756}
5757
5758/*
5759 * Allocate a variable for a string constant.
5760 * Return OK or FAIL.
5761 */
5762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 char_u *p;
5766 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 int extra = 0;
5768
5769 /*
5770 * Find the end of the string, skipping backslashed characters.
5771 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
5774 if (*p == '\\' && p[1] != NUL)
5775 {
5776 ++p;
5777 /* A "\<x>" form occupies at least 4 characters, and produces up
5778 * to 6 characters: reserve space for 2 extra */
5779 if (*p == '<')
5780 extra += 2;
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783
5784 if (*p != '"')
5785 {
5786 EMSG2(_("E114: Missing quote: %s"), *arg);
5787 return FAIL;
5788 }
5789
5790 /* If only parsing, set *arg and return here */
5791 if (!evaluate)
5792 {
5793 *arg = p + 1;
5794 return OK;
5795 }
5796
5797 /*
5798 * Copy the string into allocated memory, handling backslashed
5799 * characters.
5800 */
5801 name = alloc((unsigned)(p - *arg + extra));
5802 if (name == NULL)
5803 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 rettv->v_type = VAR_STRING;
5805 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
5809 if (*p == '\\')
5810 {
5811 switch (*++p)
5812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case 'b': *name++ = BS; ++p; break;
5814 case 'e': *name++ = ESC; ++p; break;
5815 case 'f': *name++ = FF; ++p; break;
5816 case 'n': *name++ = NL; ++p; break;
5817 case 'r': *name++ = CAR; ++p; break;
5818 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
5820 case 'X': /* hex: "\x1", "\x12" */
5821 case 'x':
5822 case 'u': /* Unicode: "\u0023" */
5823 case 'U':
5824 if (vim_isxdigit(p[1]))
5825 {
5826 int n, nr;
5827 int c = toupper(*p);
5828
5829 if (c == 'X')
5830 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005831 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005833 else
5834 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 nr = 0;
5836 while (--n >= 0 && vim_isxdigit(p[1]))
5837 {
5838 ++p;
5839 nr = (nr << 4) + hex2nr(*p);
5840 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#ifdef FEAT_MBYTE
5843 /* For "\u" store the number according to
5844 * 'encoding'. */
5845 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 else
5848#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852
5853 /* octal: "\1", "\12", "\123" */
5854 case '0':
5855 case '1':
5856 case '2':
5857 case '3':
5858 case '4':
5859 case '5':
5860 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 case '7': *name = *p++ - '0';
5862 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 *name = (*name << 3) + *p++ - '0';
5865 if (*p >= '0' && *p <= '7')
5866 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 break;
5870
5871 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (extra != 0)
5874 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 break;
5877 }
5878 /* FALLTHROUGH */
5879
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 break;
5882 }
5883 }
5884 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 *arg = p + 1;
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 return OK;
5892}
5893
5894/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 * Return OK or FAIL.
5897 */
5898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005899get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 int reduce = 0;
5904
5905 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 */
5908 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5909 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 break;
5914 ++reduce;
5915 ++p;
5916 }
5917 }
5918
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 return FAIL;
5923 }
5924
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 if (!evaluate)
5927 {
5928 *arg = p + 1;
5929 return OK;
5930 }
5931
5932 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 */
5935 str = alloc((unsigned)((p - *arg) - reduce));
5936 if (str == NULL)
5937 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 rettv->v_type = VAR_STRING;
5939 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 break;
5947 ++p;
5948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 *arg = p + 1;
5953
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 return OK;
5955}
5956
Bram Moolenaarddecc252016-04-06 22:59:37 +02005957 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005958partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005959{
5960 int i;
5961
5962 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005963 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005964 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005966 func_unref(pt->pt_name);
5967 vim_free(pt->pt_name);
5968 vim_free(pt);
5969}
5970
5971/*
5972 * Unreference a closure: decrement the reference count and free it when it
5973 * becomes zero.
5974 */
5975 void
5976partial_unref(partial_T *pt)
5977{
5978 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005979 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005980}
5981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 * Allocate a variable for a List and fill it from "*arg".
5984 * Return OK or FAIL.
5985 */
5986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 list_T *l = NULL;
5990 typval_T tv;
5991 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992
5993 if (evaluate)
5994 {
5995 l = list_alloc();
5996 if (l == NULL)
5997 return FAIL;
5998 }
5999
6000 *arg = skipwhite(*arg + 1);
6001 while (**arg != ']' && **arg != NUL)
6002 {
6003 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6004 goto failret;
6005 if (evaluate)
6006 {
6007 item = listitem_alloc();
6008 if (item != NULL)
6009 {
6010 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006011 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 list_append(l, item);
6013 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 else
6015 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 }
6017
6018 if (**arg == ']')
6019 break;
6020 if (**arg != ',')
6021 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006022 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023 goto failret;
6024 }
6025 *arg = skipwhite(*arg + 1);
6026 }
6027
6028 if (**arg != ']')
6029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031failret:
6032 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006033 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 return FAIL;
6035 }
6036
6037 *arg = skipwhite(*arg + 1);
6038 if (evaluate)
6039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 rettv->v_type = VAR_LIST;
6041 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 ++l->lv_refcount;
6043 }
6044
6045 return OK;
6046}
6047
6048/*
6049 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006050 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006052 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 list_T *l;
6056
6057 l = (list_T *)alloc_clear(sizeof(list_T));
6058 if (l != NULL)
6059 {
6060 /* Prepend the list to the list of lists for garbage collection. */
6061 if (first_list != NULL)
6062 first_list->lv_used_prev = l;
6063 l->lv_used_prev = NULL;
6064 l->lv_used_next = first_list;
6065 first_list = l;
6066 }
6067 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068}
6069
6070/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006071 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006072 * Returns OK or FAIL.
6073 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006076{
6077 list_T *l = list_alloc();
6078
6079 if (l == NULL)
6080 return FAIL;
6081
6082 rettv->vval.v_list = l;
6083 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006084 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006085 ++l->lv_refcount;
6086 return OK;
6087}
6088
6089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 * Unreference a list: decrement the reference count and free it when it
6091 * becomes zero.
6092 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006094list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006096 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006097 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098}
6099
6100/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006101 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 * Ignores the reference count.
6103 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006104 static void
6105list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106{
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006109 for (item = l->lv_first; item != NULL; item = l->lv_first)
6110 {
6111 /* Remove the item before deleting it. */
6112 l->lv_first = item->li_next;
6113 clear_tv(&item->li_tv);
6114 vim_free(item);
6115 }
6116}
6117
6118 static void
6119list_free_list(list_T *l)
6120{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 /* Remove the list from the list of lists for garbage collection. */
6122 if (l->lv_used_prev == NULL)
6123 first_list = l->lv_used_next;
6124 else
6125 l->lv_used_prev->lv_used_next = l->lv_used_next;
6126 if (l->lv_used_next != NULL)
6127 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6128
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 vim_free(l);
6130}
6131
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006132 void
6133list_free(list_T *l)
6134{
6135 if (!in_free_unref_items)
6136 {
6137 list_free_contents(l);
6138 list_free_list(l);
6139 }
6140}
6141
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142/*
6143 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006144 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006146 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006147listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148{
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150}
6151
6152/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006153 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006156listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 vim_free(item);
6160}
6161
6162/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006163 * Remove a list item from a List and free it. Also clears the value.
6164 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006166listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006167{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006168 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006169 listitem_free(item);
6170}
6171
6172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 * Get the number of items in a list.
6174 */
6175 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 if (l == NULL)
6179 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006180 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181}
6182
6183/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 * Return TRUE when two lists have exactly the same values.
6185 */
6186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006187list_equal(
6188 list_T *l1,
6189 list_T *l2,
6190 int ic, /* ignore case for strings */
6191 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006195 if (l1 == NULL || l2 == NULL)
6196 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 if (l1 == l2)
6198 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006199 if (list_len(l1) != list_len(l2))
6200 return FALSE;
6201
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202 for (item1 = l1->lv_first, item2 = l2->lv_first;
6203 item1 != NULL && item2 != NULL;
6204 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 return FALSE;
6207 return item1 == NULL && item2 == NULL;
6208}
6209
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006210/*
6211 * Return the dictitem that an entry in a hashtable points to.
6212 */
6213 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006215{
6216 return HI2DI(hi);
6217}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006220 * Return TRUE when two dictionaries have exactly the same key/values.
6221 */
6222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223dict_equal(
6224 dict_T *d1,
6225 dict_T *d2,
6226 int ic, /* ignore case for strings */
6227 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228{
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 hashitem_T *hi;
6230 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006231 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006232
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006233 if (d1 == NULL || d2 == NULL)
6234 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006235 if (d1 == d2)
6236 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006237 if (dict_len(d1) != dict_len(d2))
6238 return FALSE;
6239
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006240 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006242 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006243 if (!HASHITEM_EMPTY(hi))
6244 {
6245 item2 = dict_find(d2, hi->hi_key, -1);
6246 if (item2 == NULL)
6247 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006248 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249 return FALSE;
6250 --todo;
6251 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006252 }
6253 return TRUE;
6254}
6255
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006256static int tv_equal_recurse_limit;
6257
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006258/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006259 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006260 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006261 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262 */
6263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006264tv_equal(
6265 typval_T *tv1,
6266 typval_T *tv2,
6267 int ic, /* ignore case */
6268 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269{
6270 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006271 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006272 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006273 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006274
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006275 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6276 if ((tv1->v_type == VAR_FUNC
6277 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6278 && (tv2->v_type == VAR_FUNC
6279 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6280 {
6281 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6282 : tv1->vval.v_partial->pt_name;
6283 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6284 : tv2->vval.v_partial->pt_name;
6285 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6286 }
6287
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006288 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006289 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006290
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006291 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006292 * recursiveness to a limit. We guess they are equal then.
6293 * A fixed limit has the problem of still taking an awful long time.
6294 * Reduce the limit every time running into it. That should work fine for
6295 * deeply linked structures that are not recursively linked and catch
6296 * recursiveness quickly. */
6297 if (!recursive)
6298 tv_equal_recurse_limit = 1000;
6299 if (recursive_cnt >= tv_equal_recurse_limit)
6300 {
6301 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006302 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006303 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006304
6305 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006306 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006307 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006308 ++recursive_cnt;
6309 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6310 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006311 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006312
6313 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006314 ++recursive_cnt;
6315 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6316 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006317 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006318
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006319 case VAR_NUMBER:
6320 return tv1->vval.v_number == tv2->vval.v_number;
6321
6322 case VAR_STRING:
6323 s1 = get_tv_string_buf(tv1, buf1);
6324 s2 = get_tv_string_buf(tv2, buf2);
6325 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006326
6327 case VAR_SPECIAL:
6328 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006329
6330 case VAR_FLOAT:
6331#ifdef FEAT_FLOAT
6332 return tv1->vval.v_float == tv2->vval.v_float;
6333#endif
6334 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006335#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006336 return tv1->vval.v_job == tv2->vval.v_job;
6337#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006338 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006339#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006340 return tv1->vval.v_channel == tv2->vval.v_channel;
6341#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006342 case VAR_FUNC:
6343 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006344 case VAR_UNKNOWN:
6345 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006346 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006347
Bram Moolenaara03f2332016-02-06 18:09:59 +01006348 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6349 * does not equal anything, not even itself. */
6350 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351}
6352
6353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354 * Locate item with index "n" in list "l" and return it.
6355 * A negative index is counted from the end; -1 is the last item.
6356 * Returns NULL when "n" is out of range.
6357 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006358 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006359list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006360{
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 long idx;
6363
6364 if (l == NULL)
6365 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006366
6367 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006369 n = l->lv_len + n;
6370
6371 /* Check for index out of range. */
6372 if (n < 0 || n >= l->lv_len)
6373 return NULL;
6374
6375 /* When there is a cached index may start search from there. */
6376 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006378 if (n < l->lv_idx / 2)
6379 {
6380 /* closest to the start of the list */
6381 item = l->lv_first;
6382 idx = 0;
6383 }
6384 else if (n > (l->lv_idx + l->lv_len) / 2)
6385 {
6386 /* closest to the end of the list */
6387 item = l->lv_last;
6388 idx = l->lv_len - 1;
6389 }
6390 else
6391 {
6392 /* closest to the cached index */
6393 item = l->lv_idx_item;
6394 idx = l->lv_idx;
6395 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396 }
6397 else
6398 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006399 if (n < l->lv_len / 2)
6400 {
6401 /* closest to the start of the list */
6402 item = l->lv_first;
6403 idx = 0;
6404 }
6405 else
6406 {
6407 /* closest to the end of the list */
6408 item = l->lv_last;
6409 idx = l->lv_len - 1;
6410 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006412
6413 while (n > idx)
6414 {
6415 /* search forward */
6416 item = item->li_next;
6417 ++idx;
6418 }
6419 while (n < idx)
6420 {
6421 /* search backward */
6422 item = item->li_prev;
6423 --idx;
6424 }
6425
6426 /* cache the used index */
6427 l->lv_idx = idx;
6428 l->lv_idx_item = item;
6429
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 return item;
6431}
6432
6433/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006434 * Get list item "l[idx]" as a number.
6435 */
6436 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006437list_find_nr(
6438 list_T *l,
6439 long idx,
6440 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006441{
6442 listitem_T *li;
6443
6444 li = list_find(l, idx);
6445 if (li == NULL)
6446 {
6447 if (errorp != NULL)
6448 *errorp = TRUE;
6449 return -1L;
6450 }
6451 return get_tv_number_chk(&li->li_tv, errorp);
6452}
6453
6454/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006455 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6456 */
6457 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006458list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006459{
6460 listitem_T *li;
6461
6462 li = list_find(l, idx - 1);
6463 if (li == NULL)
6464 {
6465 EMSGN(_(e_listidx), idx);
6466 return NULL;
6467 }
6468 return get_tv_string(&li->li_tv);
6469}
6470
6471/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006472 * Locate "item" list "l" and return its index.
6473 * Returns -1 when "item" is not in the list.
6474 */
6475 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006477{
6478 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006480
6481 if (l == NULL)
6482 return -1;
6483 idx = 0;
6484 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6485 ++idx;
6486 if (li == NULL)
6487 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006488 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006489}
6490
6491/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 * Append item "item" to the end of list "l".
6493 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496{
6497 if (l->lv_last == NULL)
6498 {
6499 /* empty list */
6500 l->lv_first = item;
6501 l->lv_last = item;
6502 item->li_prev = NULL;
6503 }
6504 else
6505 {
6506 l->lv_last->li_next = item;
6507 item->li_prev = l->lv_last;
6508 l->lv_last = item;
6509 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006510 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 item->li_next = NULL;
6512}
6513
6514/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006521 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522
Bram Moolenaar05159a02005-02-26 23:04:13 +00006523 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006525 copy_tv(tv, &li->li_tv);
6526 list_append(l, li);
6527 return OK;
6528}
6529
6530/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006531 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006532 * Return FAIL when out of memory.
6533 */
6534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006535list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006536{
6537 listitem_T *li = listitem_alloc();
6538
6539 if (li == NULL)
6540 return FAIL;
6541 li->li_tv.v_type = VAR_DICT;
6542 li->li_tv.v_lock = 0;
6543 li->li_tv.vval.v_dict = dict;
6544 list_append(list, li);
6545 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 return OK;
6547}
6548
6549/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006550 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006551 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006552 * Returns FAIL when out of memory.
6553 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006554 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006556{
6557 listitem_T *li = listitem_alloc();
6558
6559 if (li == NULL)
6560 return FAIL;
6561 list_append(l, li);
6562 li->li_tv.v_type = VAR_STRING;
6563 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006564 if (str == NULL)
6565 li->li_tv.vval.v_string = NULL;
6566 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006567 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006568 return FAIL;
6569 return OK;
6570}
6571
6572/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006573 * Append "n" to list "l".
6574 * Returns FAIL when out of memory.
6575 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006578{
6579 listitem_T *li;
6580
6581 li = listitem_alloc();
6582 if (li == NULL)
6583 return FAIL;
6584 li->li_tv.v_type = VAR_NUMBER;
6585 li->li_tv.v_lock = 0;
6586 li->li_tv.vval.v_number = n;
6587 list_append(l, li);
6588 return OK;
6589}
6590
6591/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 * If "item" is NULL append at the end.
6594 * Return FAIL when out of memory.
6595 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006596 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598{
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600
6601 if (ni == NULL)
6602 return FAIL;
6603 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006604 list_insert(l, ni, item);
6605 return OK;
6606}
6607
6608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006610{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006611 if (item == NULL)
6612 /* Append new item at end of list. */
6613 list_append(l, ni);
6614 else
6615 {
6616 /* Insert new item before existing item. */
6617 ni->li_prev = item->li_prev;
6618 ni->li_next = item;
6619 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006620 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006621 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006622 ++l->lv_idx;
6623 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006625 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006627 l->lv_idx_item = NULL;
6628 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006630 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632}
6633
6634/*
6635 * Extend "l1" with "l2".
6636 * If "bef" is NULL append at the end, otherwise insert before this item.
6637 * Returns FAIL when out of memory.
6638 */
6639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006640list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641{
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006643 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006645 /* We also quit the loop when we have inserted the original item count of
6646 * the list, avoid a hang when we extend a list with itself. */
6647 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6649 return FAIL;
6650 return OK;
6651}
6652
6653/*
6654 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6655 * Return FAIL when out of memory.
6656 */
6657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006658list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006659{
Bram Moolenaar33570922005-01-25 22:26:29 +00006660 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006662 if (l1 == NULL || l2 == NULL)
6663 return FAIL;
6664
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006666 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667 if (l == NULL)
6668 return FAIL;
6669 tv->v_type = VAR_LIST;
6670 tv->vval.v_list = l;
6671
6672 /* append all items from the second list */
6673 return list_extend(l, l2, NULL);
6674}
6675
6676/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006677 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680 * Returns NULL when out of memory.
6681 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006682 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006683list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684{
Bram Moolenaar33570922005-01-25 22:26:29 +00006685 list_T *copy;
6686 listitem_T *item;
6687 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688
6689 if (orig == NULL)
6690 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006691
6692 copy = list_alloc();
6693 if (copy != NULL)
6694 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006695 if (copyID != 0)
6696 {
6697 /* Do this before adding the items, because one of the items may
6698 * refer back to this list. */
6699 orig->lv_copyID = copyID;
6700 orig->lv_copylist = copy;
6701 }
6702 for (item = orig->lv_first; item != NULL && !got_int;
6703 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704 {
6705 ni = listitem_alloc();
6706 if (ni == NULL)
6707 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006708 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006709 {
6710 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6711 {
6712 vim_free(ni);
6713 break;
6714 }
6715 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006716 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006717 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006718 list_append(copy, ni);
6719 }
6720 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006721 if (item != NULL)
6722 {
6723 list_unref(copy);
6724 copy = NULL;
6725 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006726 }
6727
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 return copy;
6729}
6730
6731/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006733 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006734 * This used to be called list_remove, but that conflicts with a Sun header
6735 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006736 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006738vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006739{
Bram Moolenaar33570922005-01-25 22:26:29 +00006740 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006741
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742 /* notify watchers */
6743 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006745 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006746 list_fix_watch(l, ip);
6747 if (ip == item2)
6748 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006749 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006750
6751 if (item2->li_next == NULL)
6752 l->lv_last = item->li_prev;
6753 else
6754 item2->li_next->li_prev = item->li_prev;
6755 if (item->li_prev == NULL)
6756 l->lv_first = item2->li_next;
6757 else
6758 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006759 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006760}
6761
6762/*
6763 * Return an allocated string with the string representation of a list.
6764 * May return NULL.
6765 */
6766 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006767list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006768{
6769 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770
6771 if (tv->vval.v_list == NULL)
6772 return NULL;
6773 ga_init2(&ga, (int)sizeof(char), 80);
6774 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006775 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006776 {
6777 vim_free(ga.ga_data);
6778 return NULL;
6779 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780 ga_append(&ga, ']');
6781 ga_append(&ga, NUL);
6782 return (char_u *)ga.ga_data;
6783}
6784
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785typedef struct join_S {
6786 char_u *s;
6787 char_u *tofree;
6788} join_T;
6789
6790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006791list_join_inner(
6792 garray_T *gap, /* to store the result in */
6793 list_T *l,
6794 char_u *sep,
6795 int echo_style,
6796 int copyID,
6797 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798{
6799 int i;
6800 join_T *p;
6801 int len;
6802 int sumlen = 0;
6803 int first = TRUE;
6804 char_u *tofree;
6805 char_u numbuf[NUMBUFLEN];
6806 listitem_T *item;
6807 char_u *s;
6808
6809 /* Stringify each item in the list. */
6810 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6811 {
6812 if (echo_style)
6813 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6814 else
6815 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6816 if (s == NULL)
6817 return FAIL;
6818
6819 len = (int)STRLEN(s);
6820 sumlen += len;
6821
Bram Moolenaarcde88542015-08-11 19:14:00 +02006822 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006823 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6824 if (tofree != NULL || s != numbuf)
6825 {
6826 p->s = s;
6827 p->tofree = tofree;
6828 }
6829 else
6830 {
6831 p->s = vim_strnsave(s, len);
6832 p->tofree = p->s;
6833 }
6834
6835 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006836 if (did_echo_string_emsg) /* recursion error, bail out */
6837 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006838 }
6839
6840 /* Allocate result buffer with its total size, avoid re-allocation and
6841 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6842 if (join_gap->ga_len >= 2)
6843 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6844 if (ga_grow(gap, sumlen + 2) == FAIL)
6845 return FAIL;
6846
6847 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6848 {
6849 if (first)
6850 first = FALSE;
6851 else
6852 ga_concat(gap, sep);
6853 p = ((join_T *)join_gap->ga_data) + i;
6854
6855 if (p->s != NULL)
6856 ga_concat(gap, p->s);
6857 line_breakcheck();
6858 }
6859
6860 return OK;
6861}
6862
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006863/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006864 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006865 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006866 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006867 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006869list_join(
6870 garray_T *gap,
6871 list_T *l,
6872 char_u *sep,
6873 int echo_style,
6874 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006875{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006876 garray_T join_ga;
6877 int retval;
6878 join_T *p;
6879 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006880
Bram Moolenaard39a7512015-04-16 22:51:22 +02006881 if (l->lv_len < 1)
6882 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006883 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6884 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6885
6886 /* Dispose each item in join_ga. */
6887 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006888 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006889 p = (join_T *)join_ga.ga_data;
6890 for (i = 0; i < join_ga.ga_len; ++i)
6891 {
6892 vim_free(p->tofree);
6893 ++p;
6894 }
6895 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006896 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006897
6898 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006899}
6900
6901/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006902 * Return the next (unique) copy ID.
6903 * Used for serializing nested structures.
6904 */
6905 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006906get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006907{
6908 current_copyID += COPYID_INC;
6909 return current_copyID;
6910}
6911
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006912/* Used by get_func_tv() */
6913static garray_T funcargs = GA_EMPTY;
6914
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006915/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 * Garbage collection for lists and dictionaries.
6917 *
6918 * We use reference counts to be able to free most items right away when they
6919 * are no longer used. But for composite items it's possible that it becomes
6920 * unused while the reference count is > 0: When there is a recursive
6921 * reference. Example:
6922 * :let l = [1, 2, 3]
6923 * :let d = {9: l}
6924 * :let l[1] = d
6925 *
6926 * Since this is quite unusual we handle this with garbage collection: every
6927 * once in a while find out which lists and dicts are not referenced from any
6928 * variable.
6929 *
6930 * Here is a good reference text about garbage collection (refers to Python
6931 * but it applies to all reference-counting mechanisms):
6932 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006933 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006934
6935/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02006937 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006941garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006942{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 buf_T *buf;
6946 win_T *wp;
6947 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006948 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006949 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006950 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006951#ifdef FEAT_WINDOWS
6952 tabpage_T *tp;
6953#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006955 if (!testing)
6956 {
6957 /* Only do this once. */
6958 want_garbage_collect = FALSE;
6959 may_garbage_collect = FALSE;
6960 garbage_collect_at_exit = FALSE;
6961 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006962
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 /* We advance by two because we add one for items referenced through
6964 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006965 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 /*
6968 * 1. Go through all accessible variables and mark all lists and dicts
6969 * with copyID.
6970 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971
6972 /* Don't free variables in the previous_funccal list unless they are only
6973 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006974 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6976 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6978 NULL);
6979 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6980 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 }
6982
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 /* script-local variables */
6984 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006985 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986
6987 /* buffer-local variables */
6988 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6990 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991
6992 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006993 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006994 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6995 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006996#ifdef FEAT_AUTOCMD
6997 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006998 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6999 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007000#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007002#ifdef FEAT_WINDOWS
7003 /* tabpage-local variables */
7004 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007005 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7006 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007007#endif
7008
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007010 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011
7012 /* function-local variables */
7013 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7014 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7016 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 }
7018
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007019 /* function call arguments, if v:testing is set. */
7020 for (i = 0; i < funcargs.ga_len; ++i)
7021 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7022 copyID, NULL, NULL);
7023
Bram Moolenaard812df62008-11-09 12:46:09 +00007024 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007026
Bram Moolenaar1dced572012-04-05 16:54:08 +02007027#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007029#endif
7030
Bram Moolenaardb913952012-06-29 12:54:53 +02007031#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007033#endif
7034
7035#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007037#endif
7038
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007039#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007040 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007041 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007042#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007043#ifdef FEAT_NETBEANS_INTG
7044 abort = abort || set_ref_in_nb_channel(copyID);
7045#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007046
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007048 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 /*
7050 * 2. Free lists and dictionaries that are not referenced.
7051 */
7052 did_free = free_unref_items(copyID);
7053
7054 /*
7055 * 3. Check if any funccal can be freed now.
7056 */
7057 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007058 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059 if (can_free_funccal(*pfc, copyID))
7060 {
7061 fc = *pfc;
7062 *pfc = fc->caller;
7063 free_funccal(fc, TRUE);
7064 did_free = TRUE;
7065 did_free_funccal = TRUE;
7066 }
7067 else
7068 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007069 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 if (did_free_funccal)
7071 /* When a funccal was freed some more items might be garbage
7072 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007073 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 else if (p_verbose > 0)
7076 {
7077 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7078 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007079
7080 return did_free;
7081}
7082
7083/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007084 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007085 */
7086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007087free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007088{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007089 dict_T *dd, *dd_next;
7090 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007091 int did_free = FALSE;
7092
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007093 /* Let all "free" functions know that we are here. This means no
7094 * dictionaries, lists, channels or jobs are to be freed, because we will
7095 * do that here. */
7096 in_free_unref_items = TRUE;
7097
7098 /*
7099 * PASS 1: free the contents of the items. We don't free the items
7100 * themselves yet, so that it is possible to decrement refcount counters
7101 */
7102
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007104 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007106 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007107 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007108 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007109 /* Free the Dictionary and ordinary items it contains, but don't
7110 * recurse into Lists and Dictionaries, they will be in the list
7111 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007112 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115
7116 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007117 * Go through the list of lists and free items without the copyID.
7118 * But don't free a list that has a watcher (used in a for loop), these
7119 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007121 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7122 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7123 && ll->lv_watch == NULL)
7124 {
7125 /* Free the List and ordinary items it contains, but don't recurse
7126 * into Lists and Dictionaries, they will be in the list of dicts
7127 * or list of lists. */
7128 list_free_contents(ll);
7129 did_free = TRUE;
7130 }
7131
7132#ifdef FEAT_JOB_CHANNEL
7133 /* Go through the list of jobs and free items without the copyID. This
7134 * must happen before doing channels, because jobs refer to channels, but
7135 * the reference from the channel to the job isn't tracked. */
7136 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7137
7138 /* Go through the list of channels and free items without the copyID. */
7139 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7140#endif
7141
7142 /*
7143 * PASS 2: free the items themselves.
7144 */
7145 for (dd = first_dict; dd != NULL; dd = dd_next)
7146 {
7147 dd_next = dd->dv_used_next;
7148 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7149 dict_free_dict(dd);
7150 }
7151
7152 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007153 {
7154 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007155 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7156 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007157 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007158 /* Free the List and ordinary items it contains, but don't recurse
7159 * into Lists and Dictionaries, they will be in the list of dicts
7160 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007161 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007162 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007163 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007164
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007165#ifdef FEAT_JOB_CHANNEL
7166 /* Go through the list of jobs and free items without the copyID. This
7167 * must happen before doing channels, because jobs refer to channels, but
7168 * the reference from the channel to the job isn't tracked. */
7169 free_unused_jobs(copyID, COPYID_MASK);
7170
7171 /* Go through the list of channels and free items without the copyID. */
7172 free_unused_channels(copyID, COPYID_MASK);
7173#endif
7174
7175 in_free_unref_items = FALSE;
7176
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007177 return did_free;
7178}
7179
7180/*
7181 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 * "list_stack" is used to add lists to be marked. Can be NULL.
7183 *
7184 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007187set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188{
7189 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007190 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 hashtab_T *cur_ht;
7193 ht_stack_T *ht_stack = NULL;
7194 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007195
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007196 cur_ht = ht;
7197 for (;;)
7198 {
7199 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007201 /* Mark each item in the hashtab. If the item contains a hashtab
7202 * it is added to ht_stack, if it contains a list it is added to
7203 * list_stack. */
7204 todo = (int)cur_ht->ht_used;
7205 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7206 if (!HASHITEM_EMPTY(hi))
7207 {
7208 --todo;
7209 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7210 &ht_stack, list_stack);
7211 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007212 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007213
7214 if (ht_stack == NULL)
7215 break;
7216
7217 /* take an item from the stack */
7218 cur_ht = ht_stack->ht;
7219 tempitem = ht_stack;
7220 ht_stack = ht_stack->prev;
7221 free(tempitem);
7222 }
7223
7224 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007225}
7226
7227/*
7228 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007229 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7230 *
7231 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007232 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007233 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007235{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007236 listitem_T *li;
7237 int abort = FALSE;
7238 list_T *cur_l;
7239 list_stack_T *list_stack = NULL;
7240 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007242 cur_l = l;
7243 for (;;)
7244 {
7245 if (!abort)
7246 /* Mark each item in the list. If the item contains a hashtab
7247 * it is added to ht_stack, if it contains a list it is added to
7248 * list_stack. */
7249 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7250 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7251 ht_stack, &list_stack);
7252 if (list_stack == NULL)
7253 break;
7254
7255 /* take an item from the stack */
7256 cur_l = list_stack->list;
7257 tempitem = list_stack;
7258 list_stack = list_stack->prev;
7259 free(tempitem);
7260 }
7261
7262 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007263}
7264
7265/*
7266 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007267 * "list_stack" is used to add lists to be marked. Can be NULL.
7268 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7269 *
7270 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007271 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007273set_ref_in_item(
7274 typval_T *tv,
7275 int copyID,
7276 ht_stack_T **ht_stack,
7277 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007278{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007279 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007280
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007281 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007282 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007283 dict_T *dd = tv->vval.v_dict;
7284
Bram Moolenaara03f2332016-02-06 18:09:59 +01007285 if (dd != NULL && dd->dv_copyID != copyID)
7286 {
7287 /* Didn't see this dict yet. */
7288 dd->dv_copyID = copyID;
7289 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007290 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007291 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7292 }
7293 else
7294 {
7295 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7296 if (newitem == NULL)
7297 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007298 else
7299 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007300 newitem->ht = &dd->dv_hashtab;
7301 newitem->prev = *ht_stack;
7302 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007303 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007304 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007305 }
7306 }
7307 else if (tv->v_type == VAR_LIST)
7308 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007309 list_T *ll = tv->vval.v_list;
7310
Bram Moolenaara03f2332016-02-06 18:09:59 +01007311 if (ll != NULL && ll->lv_copyID != copyID)
7312 {
7313 /* Didn't see this list yet. */
7314 ll->lv_copyID = copyID;
7315 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007316 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007317 abort = set_ref_in_list(ll, copyID, ht_stack);
7318 }
7319 else
7320 {
7321 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007322 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007323 if (newitem == NULL)
7324 abort = TRUE;
7325 else
7326 {
7327 newitem->list = ll;
7328 newitem->prev = *list_stack;
7329 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007330 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007331 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007332 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007333 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007334 else if (tv->v_type == VAR_PARTIAL)
7335 {
7336 partial_T *pt = tv->vval.v_partial;
7337 int i;
7338
7339 /* A partial does not have a copyID, because it cannot contain itself.
7340 */
7341 if (pt != NULL)
7342 {
7343 if (pt->pt_dict != NULL)
7344 {
7345 typval_T dtv;
7346
7347 dtv.v_type = VAR_DICT;
7348 dtv.vval.v_dict = pt->pt_dict;
7349 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7350 }
7351
7352 for (i = 0; i < pt->pt_argc; ++i)
7353 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7354 ht_stack, list_stack);
7355 }
7356 }
7357#ifdef FEAT_JOB_CHANNEL
7358 else if (tv->v_type == VAR_JOB)
7359 {
7360 job_T *job = tv->vval.v_job;
7361 typval_T dtv;
7362
7363 if (job != NULL && job->jv_copyID != copyID)
7364 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007365 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007366 if (job->jv_channel != NULL)
7367 {
7368 dtv.v_type = VAR_CHANNEL;
7369 dtv.vval.v_channel = job->jv_channel;
7370 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7371 }
7372 if (job->jv_exit_partial != NULL)
7373 {
7374 dtv.v_type = VAR_PARTIAL;
7375 dtv.vval.v_partial = job->jv_exit_partial;
7376 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7377 }
7378 }
7379 }
7380 else if (tv->v_type == VAR_CHANNEL)
7381 {
7382 channel_T *ch =tv->vval.v_channel;
7383 int part;
7384 typval_T dtv;
7385 jsonq_T *jq;
7386 cbq_T *cq;
7387
7388 if (ch != NULL && ch->ch_copyID != copyID)
7389 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007390 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007391 for (part = PART_SOCK; part <= PART_IN; ++part)
7392 {
7393 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7394 jq = jq->jq_next)
7395 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7396 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7397 cq = cq->cq_next)
7398 if (cq->cq_partial != NULL)
7399 {
7400 dtv.v_type = VAR_PARTIAL;
7401 dtv.vval.v_partial = cq->cq_partial;
7402 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7403 }
7404 if (ch->ch_part[part].ch_partial != NULL)
7405 {
7406 dtv.v_type = VAR_PARTIAL;
7407 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7408 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7409 }
7410 }
7411 if (ch->ch_partial != NULL)
7412 {
7413 dtv.v_type = VAR_PARTIAL;
7414 dtv.vval.v_partial = ch->ch_partial;
7415 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7416 }
7417 if (ch->ch_close_partial != NULL)
7418 {
7419 dtv.v_type = VAR_PARTIAL;
7420 dtv.vval.v_partial = ch->ch_close_partial;
7421 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7422 }
7423 }
7424 }
7425#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007426 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007427}
7428
7429/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 * Allocate an empty header for a dictionary.
7431 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007432 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007433dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434{
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007436
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007439 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007440 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007441 if (first_dict != NULL)
7442 first_dict->dv_used_prev = d;
7443 d->dv_used_next = first_dict;
7444 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007445 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007446
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007448 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007449 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007450 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007451 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007452 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007453 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454}
7455
7456/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007457 * Allocate an empty dict for a return value.
7458 * Returns OK or FAIL.
7459 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007461rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007462{
7463 dict_T *d = dict_alloc();
7464
7465 if (d == NULL)
7466 return FAIL;
7467
7468 rettv->vval.v_dict = d;
7469 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007470 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007471 ++d->dv_refcount;
7472 return OK;
7473}
7474
7475
7476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477 * Unreference a Dictionary: decrement the reference count and free it when it
7478 * becomes zero.
7479 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007483 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007484 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485}
7486
7487/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007488 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 * Ignores the reference count.
7490 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007491 static void
7492dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007496 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007498 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007499 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007500 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 if (!HASHITEM_EMPTY(hi))
7504 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007505 /* Remove the item before deleting it, just in case there is
7506 * something recursive causing trouble. */
7507 di = HI2DI(hi);
7508 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007509 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007510 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 --todo;
7512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007515}
7516
7517 static void
7518dict_free_dict(dict_T *d)
7519{
7520 /* Remove the dict from the list of dicts for garbage collection. */
7521 if (d->dv_used_prev == NULL)
7522 first_dict = d->dv_used_next;
7523 else
7524 d->dv_used_prev->dv_used_next = d->dv_used_next;
7525 if (d->dv_used_next != NULL)
7526 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 vim_free(d);
7528}
7529
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007530 void
7531dict_free(dict_T *d)
7532{
7533 if (!in_free_unref_items)
7534 {
7535 dict_free_contents(d);
7536 dict_free_dict(d);
7537 }
7538}
7539
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540/*
7541 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542 * The "key" is copied to the new item.
7543 * Note that the value of the item "di_tv" still needs to be initialized!
7544 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007546 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548{
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007551 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007555 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007556 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558}
7559
7560/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 * Make a copy of a Dictionary item.
7562 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007565{
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007568 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7569 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007570 if (di != NULL)
7571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007573 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 copy_tv(&org->di_tv, &di->di_tv);
7575 }
7576 return di;
7577}
7578
7579/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 * Remove item "item" from Dictionary "dict" and free it.
7581 */
7582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007583dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584{
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 if (HASHITEM_EMPTY(hi))
7589 EMSG2(_(e_intern2), "dictitem_remove()");
7590 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 dictitem_free(item);
7593}
7594
7595/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 * Free a dict item. Also clears the value.
7597 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007598 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007599dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007602 if (item->di_flags & DI_FLAGS_ALLOC)
7603 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604}
7605
7606/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007607 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7608 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610 * Returns NULL when out of memory.
7611 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007613dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614{
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 dict_T *copy;
7616 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007617 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619
7620 if (orig == NULL)
7621 return NULL;
7622
7623 copy = dict_alloc();
7624 if (copy != NULL)
7625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007626 if (copyID != 0)
7627 {
7628 orig->dv_copyID = copyID;
7629 orig->dv_copydict = copy;
7630 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007631 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007632 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 --todo;
7637
7638 di = dictitem_alloc(hi->hi_key);
7639 if (di == NULL)
7640 break;
7641 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007642 {
7643 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7644 copyID) == FAIL)
7645 {
7646 vim_free(di);
7647 break;
7648 }
7649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007650 else
7651 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7652 if (dict_add(copy, di) == FAIL)
7653 {
7654 dictitem_free(di);
7655 break;
7656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007658 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659
Bram Moolenaare9a41262005-01-15 22:18:47 +00007660 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007661 if (todo > 0)
7662 {
7663 dict_unref(copy);
7664 copy = NULL;
7665 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 }
7667
7668 return copy;
7669}
7670
7671/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007673 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007676dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677{
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679}
7680
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007682 * Add a number or string entry to dictionary "d".
7683 * When "str" is NULL use number "nr", otherwise use "str".
7684 * Returns FAIL when out of memory and when key already exists.
7685 */
7686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007687dict_add_nr_str(
7688 dict_T *d,
7689 char *key,
7690 long nr,
7691 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007692{
7693 dictitem_T *item;
7694
7695 item = dictitem_alloc((char_u *)key);
7696 if (item == NULL)
7697 return FAIL;
7698 item->di_tv.v_lock = 0;
7699 if (str == NULL)
7700 {
7701 item->di_tv.v_type = VAR_NUMBER;
7702 item->di_tv.vval.v_number = nr;
7703 }
7704 else
7705 {
7706 item->di_tv.v_type = VAR_STRING;
7707 item->di_tv.vval.v_string = vim_strsave(str);
7708 }
7709 if (dict_add(d, item) == FAIL)
7710 {
7711 dictitem_free(item);
7712 return FAIL;
7713 }
7714 return OK;
7715}
7716
7717/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007718 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007719 * Returns FAIL when out of memory and when key already exists.
7720 */
7721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007722dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007723{
7724 dictitem_T *item;
7725
7726 item = dictitem_alloc((char_u *)key);
7727 if (item == NULL)
7728 return FAIL;
7729 item->di_tv.v_lock = 0;
7730 item->di_tv.v_type = VAR_LIST;
7731 item->di_tv.vval.v_list = list;
7732 if (dict_add(d, item) == FAIL)
7733 {
7734 dictitem_free(item);
7735 return FAIL;
7736 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007737 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007738 return OK;
7739}
7740
7741/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 * Get the number of items in a Dictionary.
7743 */
7744 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007745dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007746{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007747 if (d == NULL)
7748 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007749 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007750}
7751
7752/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 * Find item "key[len]" in Dictionary "d".
7754 * If "len" is negative use strlen(key).
7755 * Returns NULL when not found.
7756 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007757 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007758dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007760#define AKEYLEN 200
7761 char_u buf[AKEYLEN];
7762 char_u *akey;
7763 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007766 if (len < 0)
7767 akey = key;
7768 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007769 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007770 tofree = akey = vim_strnsave(key, len);
7771 if (akey == NULL)
7772 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007773 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007774 else
7775 {
7776 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007777 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007778 akey = buf;
7779 }
7780
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007782 vim_free(tofree);
7783 if (HASHITEM_EMPTY(hi))
7784 return NULL;
7785 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786}
7787
7788/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007789 * Get a string item from a dictionary.
7790 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007791 * Returns NULL if the entry doesn't exist or out of memory.
7792 */
7793 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007795{
7796 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007797 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007798
7799 di = dict_find(d, key, -1);
7800 if (di == NULL)
7801 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007802 s = get_tv_string(&di->di_tv);
7803 if (save && s != NULL)
7804 s = vim_strsave(s);
7805 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007806}
7807
7808/*
7809 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007810 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007811 */
7812 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007813get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007814{
7815 dictitem_T *di;
7816
7817 di = dict_find(d, key, -1);
7818 if (di == NULL)
7819 return 0;
7820 return get_tv_number(&di->di_tv);
7821}
7822
7823/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007824 * Return an allocated string with the string representation of a Dictionary.
7825 * May return NULL.
7826 */
7827 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007829{
7830 garray_T ga;
7831 int first = TRUE;
7832 char_u *tofree;
7833 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007836 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007837 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007838
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007839 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007840 return NULL;
7841 ga_init2(&ga, (int)sizeof(char), 80);
7842 ga_append(&ga, '{');
7843
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007844 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007845 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007847 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007849 --todo;
7850
7851 if (first)
7852 first = FALSE;
7853 else
7854 ga_concat(&ga, (char_u *)", ");
7855
7856 tofree = string_quote(hi->hi_key, FALSE);
7857 if (tofree != NULL)
7858 {
7859 ga_concat(&ga, tofree);
7860 vim_free(tofree);
7861 }
7862 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007863 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007864 if (s != NULL)
7865 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007866 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007867 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007868 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007869 line_breakcheck();
7870
Bram Moolenaar8c711452005-01-14 21:53:12 +00007871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007872 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007873 if (todo > 0)
7874 {
7875 vim_free(ga.ga_data);
7876 return NULL;
7877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878
7879 ga_append(&ga, '}');
7880 ga_append(&ga, NUL);
7881 return (char_u *)ga.ga_data;
7882}
7883
7884/*
7885 * Allocate a variable for a Dictionary and fill it from "*arg".
7886 * Return OK or FAIL. Returns NOTDONE for {expr}.
7887 */
7888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007889get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007890{
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 dict_T *d = NULL;
7892 typval_T tvkey;
7893 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007894 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007896 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007897 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007898
7899 /*
7900 * First check if it's not a curly-braces thing: {expr}.
7901 * Must do this without evaluating, otherwise a function may be called
7902 * twice. Unfortunately this means we need to call eval1() twice for the
7903 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007904 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007905 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 if (*start != '}')
7907 {
7908 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7909 return FAIL;
7910 if (*start == '}')
7911 return NOTDONE;
7912 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913
7914 if (evaluate)
7915 {
7916 d = dict_alloc();
7917 if (d == NULL)
7918 return FAIL;
7919 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007920 tvkey.v_type = VAR_UNKNOWN;
7921 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007922
7923 *arg = skipwhite(*arg + 1);
7924 while (**arg != '}' && **arg != NUL)
7925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007926 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007927 goto failret;
7928 if (**arg != ':')
7929 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007930 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007931 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 goto failret;
7933 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007934 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007936 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007937 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007938 {
7939 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007940 clear_tv(&tvkey);
7941 goto failret;
7942 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944
7945 *arg = skipwhite(*arg + 1);
7946 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7947 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007948 if (evaluate)
7949 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 goto failret;
7951 }
7952 if (evaluate)
7953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007954 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 if (item != NULL)
7956 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007957 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007958 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007959 clear_tv(&tv);
7960 goto failret;
7961 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007962 item = dictitem_alloc(key);
7963 clear_tv(&tvkey);
7964 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007967 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007968 if (dict_add(d, item) == FAIL)
7969 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 }
7971 }
7972
7973 if (**arg == '}')
7974 break;
7975 if (**arg != ',')
7976 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007977 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 goto failret;
7979 }
7980 *arg = skipwhite(*arg + 1);
7981 }
7982
7983 if (**arg != '}')
7984 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007985 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007986failret:
7987 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007988 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 return FAIL;
7990 }
7991
7992 *arg = skipwhite(*arg + 1);
7993 if (evaluate)
7994 {
7995 rettv->v_type = VAR_DICT;
7996 rettv->vval.v_dict = d;
7997 ++d->dv_refcount;
7998 }
7999
8000 return OK;
8001}
8002
Bram Moolenaar17a13432016-01-24 14:22:10 +01008003 static char *
8004get_var_special_name(int nr)
8005{
8006 switch (nr)
8007 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008008 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008009 case VVAL_TRUE: return "v:true";
8010 case VVAL_NONE: return "v:none";
8011 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008012 }
8013 EMSG2(_(e_intern2), "get_var_special_name()");
8014 return "42";
8015}
8016
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008018 * Return a string with the string representation of a variable.
8019 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008020 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008021 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008022 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008023 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008024 */
8025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008026echo_string(
8027 typval_T *tv,
8028 char_u **tofree,
8029 char_u *numbuf,
8030 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008032 static int recurse = 0;
8033 char_u *r = NULL;
8034
Bram Moolenaar33570922005-01-25 22:26:29 +00008035 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008037 if (!did_echo_string_emsg)
8038 {
8039 /* Only give this message once for a recursive call to avoid
8040 * flooding the user with errors. And stop iterating over lists
8041 * and dicts. */
8042 did_echo_string_emsg = TRUE;
8043 EMSG(_("E724: variable nested too deep for displaying"));
8044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008045 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008046 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047 }
8048 ++recurse;
8049
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050 switch (tv->v_type)
8051 {
8052 case VAR_FUNC:
8053 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008054 r = tv->vval.v_string;
8055 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008056
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008057 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008058 {
8059 partial_T *pt = tv->vval.v_partial;
8060 char_u *fname = string_quote(pt == NULL ? NULL
8061 : pt->pt_name, FALSE);
8062 garray_T ga;
8063 int i;
8064 char_u *tf;
8065
8066 ga_init2(&ga, 1, 100);
8067 ga_concat(&ga, (char_u *)"function(");
8068 if (fname != NULL)
8069 {
8070 ga_concat(&ga, fname);
8071 vim_free(fname);
8072 }
8073 if (pt != NULL && pt->pt_argc > 0)
8074 {
8075 ga_concat(&ga, (char_u *)", [");
8076 for (i = 0; i < pt->pt_argc; ++i)
8077 {
8078 if (i > 0)
8079 ga_concat(&ga, (char_u *)", ");
8080 ga_concat(&ga,
8081 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8082 vim_free(tf);
8083 }
8084 ga_concat(&ga, (char_u *)"]");
8085 }
8086 if (pt != NULL && pt->pt_dict != NULL)
8087 {
8088 typval_T dtv;
8089
8090 ga_concat(&ga, (char_u *)", ");
8091 dtv.v_type = VAR_DICT;
8092 dtv.vval.v_dict = pt->pt_dict;
8093 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8094 vim_free(tf);
8095 }
8096 ga_concat(&ga, (char_u *)")");
8097
8098 *tofree = ga.ga_data;
8099 r = *tofree;
8100 break;
8101 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008102
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008103 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008104 if (tv->vval.v_list == NULL)
8105 {
8106 *tofree = NULL;
8107 r = NULL;
8108 }
8109 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8110 {
8111 *tofree = NULL;
8112 r = (char_u *)"[...]";
8113 }
8114 else
8115 {
8116 tv->vval.v_list->lv_copyID = copyID;
8117 *tofree = list2string(tv, copyID);
8118 r = *tofree;
8119 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008120 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008121
Bram Moolenaar8c711452005-01-14 21:53:12 +00008122 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008123 if (tv->vval.v_dict == NULL)
8124 {
8125 *tofree = NULL;
8126 r = NULL;
8127 }
8128 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8129 {
8130 *tofree = NULL;
8131 r = (char_u *)"{...}";
8132 }
8133 else
8134 {
8135 tv->vval.v_dict->dv_copyID = copyID;
8136 *tofree = dict2string(tv, copyID);
8137 r = *tofree;
8138 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008139 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008140
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141 case VAR_STRING:
8142 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008143 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008144 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008145 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008146 *tofree = NULL;
8147 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008149
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008150 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008151#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008152 *tofree = NULL;
8153 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8154 r = numbuf;
8155 break;
8156#endif
8157
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008158 case VAR_SPECIAL:
8159 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008160 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008161 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008162 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163
Bram Moolenaar8502c702014-06-17 12:51:16 +02008164 if (--recurse == 0)
8165 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008166 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008167}
8168
8169/*
8170 * Return a string with the string representation of a variable.
8171 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8172 * "numbuf" is used for a number.
8173 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008174 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008175 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008176 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008177tv2string(
8178 typval_T *tv,
8179 char_u **tofree,
8180 char_u *numbuf,
8181 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008182{
8183 switch (tv->v_type)
8184 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008185 case VAR_FUNC:
8186 *tofree = string_quote(tv->vval.v_string, TRUE);
8187 return *tofree;
8188 case VAR_STRING:
8189 *tofree = string_quote(tv->vval.v_string, FALSE);
8190 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008191 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008192#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193 *tofree = NULL;
8194 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8195 return numbuf;
8196#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008197 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008198 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008199 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008200 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008201 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008202 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008203 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008204 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008205 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008206 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008207 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008208}
8209
8210/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008211 * Return string "str" in ' quotes, doubling ' characters.
8212 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008213 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008214 */
8215 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008216string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008217{
Bram Moolenaar33570922005-01-25 22:26:29 +00008218 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008219 char_u *p, *r, *s;
8220
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 len = (function ? 13 : 3);
8222 if (str != NULL)
8223 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008224 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 for (p = str; *p != NUL; mb_ptr_adv(p))
8226 if (*p == '\'')
8227 ++len;
8228 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 s = r = alloc(len);
8230 if (r != NULL)
8231 {
8232 if (function)
8233 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008234 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008235 r += 10;
8236 }
8237 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008238 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008239 if (str != NULL)
8240 for (p = str; *p != NUL; )
8241 {
8242 if (*p == '\'')
8243 *r++ = '\'';
8244 MB_COPY_CHAR(p, r);
8245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008246 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008247 if (function)
8248 *r++ = ')';
8249 *r++ = NUL;
8250 }
8251 return s;
8252}
8253
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008254#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255/*
8256 * Convert the string "text" to a floating point number.
8257 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8258 * this always uses a decimal point.
8259 * Returns the length of the text that was consumed.
8260 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008262string2float(
8263 char_u *text,
8264 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265{
8266 char *s = (char *)text;
8267 float_T f;
8268
8269 f = strtod(s, &s);
8270 *value = f;
8271 return (int)((char_u *)s - text);
8272}
8273#endif
8274
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 * Get the value of an environment variable.
8277 * "arg" is pointing to the '$'. It is advanced to after the name.
8278 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008279 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 */
8281 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008282get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284 char_u *string = NULL;
8285 int len;
8286 int cc;
8287 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008288 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
8290 ++*arg;
8291 name = *arg;
8292 len = get_env_len(arg);
8293 if (evaluate)
8294 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008295 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008296 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008297
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008298 cc = name[len];
8299 name[len] = NUL;
8300 /* first try vim_getenv(), fast for normal environment vars */
8301 string = vim_getenv(name, &mustfree);
8302 if (string != NULL && *string != NUL)
8303 {
8304 if (!mustfree)
8305 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008307 else
8308 {
8309 if (mustfree)
8310 vim_free(string);
8311
8312 /* next try expanding things like $VIM and ${HOME} */
8313 string = expand_env_save(name - 1);
8314 if (string != NULL && *string == '$')
8315 {
8316 vim_free(string);
8317 string = NULL;
8318 }
8319 }
8320 name[len] = cc;
8321
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 rettv->v_type = VAR_STRING;
8323 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325
8326 return OK;
8327}
8328
8329/*
8330 * Array with names and number of arguments of all internal functions
8331 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8332 */
8333static struct fst
8334{
8335 char *f_name; /* function name */
8336 char f_min_argc; /* minimal number of arguments */
8337 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008338 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008339 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340} functions[] =
8341{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008342#ifdef FEAT_FLOAT
8343 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008344 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008345#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008346 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008347 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"append", 2, 2, f_append},
8349 {"argc", 0, 0, f_argc},
8350 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008351 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008352 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008353#ifdef FEAT_FLOAT
8354 {"asin", 1, 1, f_asin}, /* WJMc */
8355#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008356 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008357 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008358 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008359 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008360 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008361 {"assert_notequal", 2, 3, f_assert_notequal},
8362 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008363 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008364#ifdef FEAT_FLOAT
8365 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008366 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008369 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {"bufexists", 1, 1, f_bufexists},
8371 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8372 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8373 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8374 {"buflisted", 1, 1, f_buflisted},
8375 {"bufloaded", 1, 1, f_bufloaded},
8376 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008377 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"bufwinnr", 1, 1, f_bufwinnr},
8379 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008380 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008381 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008382 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008383#ifdef FEAT_FLOAT
8384 {"ceil", 1, 1, f_ceil},
8385#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008386#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008387 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008388 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8389 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008390 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008391 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008392 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008393 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008394 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008395 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008396 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008397 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008398 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8399 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008400 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008401 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008402#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008403 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008404 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008406 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008408#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008409 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008410 {"complete_add", 1, 1, f_complete_add},
8411 {"complete_check", 0, 0, f_complete_check},
8412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008414 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008415#ifdef FEAT_FLOAT
8416 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008417 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008418#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008419 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008421 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008422 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008423 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008425 {"diff_filler", 1, 1, f_diff_filler},
8426 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008427 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008429 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"eventhandler", 0, 0, f_eventhandler},
8431 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008432 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008434#ifdef FEAT_FLOAT
8435 {"exp", 1, 1, f_exp},
8436#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008437 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008438 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008439 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8441 {"filereadable", 1, 1, f_filereadable},
8442 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008443 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008444 {"finddir", 1, 3, f_finddir},
8445 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008446#ifdef FEAT_FLOAT
8447 {"float2nr", 1, 1, f_float2nr},
8448 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008449 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008450#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008451 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {"fnamemodify", 2, 2, f_fnamemodify},
8453 {"foldclosed", 1, 1, f_foldclosed},
8454 {"foldclosedend", 1, 1, f_foldclosedend},
8455 {"foldlevel", 1, 1, f_foldlevel},
8456 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008457 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008459 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008460 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008461 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008462 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008463 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {"getchar", 0, 1, f_getchar},
8465 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008466 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {"getcmdline", 0, 0, f_getcmdline},
8468 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008469 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008470 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008471 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008472 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008473 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008474 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {"getfsize", 1, 1, f_getfsize},
8476 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008477 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008478 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008479 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008480 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008481 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008482 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008483 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008484 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008486 {"gettabvar", 2, 3, f_gettabvar},
8487 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {"getwinposx", 0, 0, f_getwinposx},
8489 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008490 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008491 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008492 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008493 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008496 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008497 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8499 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8500 {"histadd", 2, 2, f_histadd},
8501 {"histdel", 1, 2, f_histdel},
8502 {"histget", 1, 2, f_histget},
8503 {"histnr", 1, 1, f_histnr},
8504 {"hlID", 1, 1, f_hlID},
8505 {"hlexists", 1, 1, f_hlexists},
8506 {"hostname", 0, 0, f_hostname},
8507 {"iconv", 3, 3, f_iconv},
8508 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008509 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008510 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008512 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 {"inputrestore", 0, 0, f_inputrestore},
8514 {"inputsave", 0, 0, f_inputsave},
8515 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008517 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008519 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008520#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8521 {"isnan", 1, 1, f_isnan},
8522#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008523 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008524#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008525 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008526 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008527 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008528 {"job_start", 1, 2, f_job_start},
8529 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008530 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008531#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008532 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008533 {"js_decode", 1, 1, f_js_decode},
8534 {"js_encode", 1, 1, f_js_encode},
8535 {"json_decode", 1, 1, f_json_decode},
8536 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008537 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008539 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"libcall", 3, 3, f_libcall},
8541 {"libcallnr", 3, 3, f_libcallnr},
8542 {"line", 1, 1, f_line},
8543 {"line2byte", 1, 1, f_line2byte},
8544 {"lispindent", 1, 1, f_lispindent},
8545 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008546#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008547 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008548 {"log10", 1, 1, f_log10},
8549#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008550#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008551 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008552#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008553 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008554 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008555 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008556 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008557 {"matchadd", 2, 5, f_matchadd},
8558 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008559 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008560 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008561 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008562 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008563 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008564 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008565 {"max", 1, 1, f_max},
8566 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008567#ifdef vim_mkdir
8568 {"mkdir", 1, 3, f_mkdir},
8569#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008570 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008571#ifdef FEAT_MZSCHEME
8572 {"mzeval", 1, 1, f_mzeval},
8573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008575 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008576 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008577 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008578#ifdef FEAT_PERL
8579 {"perleval", 1, 1, f_perleval},
8580#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008581#ifdef FEAT_FLOAT
8582 {"pow", 2, 2, f_pow},
8583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008585 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008586 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008587#ifdef FEAT_PYTHON3
8588 {"py3eval", 1, 1, f_py3eval},
8589#endif
8590#ifdef FEAT_PYTHON
8591 {"pyeval", 1, 1, f_pyeval},
8592#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008593 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008594 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008595 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008596#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008597 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008598#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008599 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {"remote_expr", 2, 3, f_remote_expr},
8601 {"remote_foreground", 1, 1, f_remote_foreground},
8602 {"remote_peek", 1, 2, f_remote_peek},
8603 {"remote_read", 1, 1, f_remote_read},
8604 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008605 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008607 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008609 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008610#ifdef FEAT_FLOAT
8611 {"round", 1, 1, f_round},
8612#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008613 {"screenattr", 2, 2, f_screenattr},
8614 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008615 {"screencol", 0, 0, f_screencol},
8616 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008617 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008618 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008619 {"searchpair", 3, 7, f_searchpair},
8620 {"searchpairpos", 3, 7, f_searchpairpos},
8621 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {"server2client", 2, 2, f_server2client},
8623 {"serverlist", 0, 0, f_serverlist},
8624 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008625 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008627 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008629 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008630 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008631 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008632 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008634 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008635 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008637#ifdef FEAT_CRYPT
8638 {"sha256", 1, 1, f_sha256},
8639#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008640 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008641 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008643#ifdef FEAT_FLOAT
8644 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008645 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008646#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008647 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008648 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008649 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008650 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008651 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008652#ifdef FEAT_FLOAT
8653 {"sqrt", 1, 1, f_sqrt},
8654 {"str2float", 1, 1, f_str2float},
8655#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008656 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008657 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008658 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008659 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660#ifdef HAVE_STRFTIME
8661 {"strftime", 1, 2, f_strftime},
8662#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008663 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008665 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 {"strlen", 1, 1, f_strlen},
8667 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008668 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008670 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008671 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 {"substitute", 4, 4, f_substitute},
8673 {"synID", 3, 3, f_synID},
8674 {"synIDattr", 2, 3, f_synIDattr},
8675 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008676 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008677 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008678 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008679 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008680 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008681 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008682 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008683 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008684 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008685#ifdef FEAT_FLOAT
8686 {"tan", 1, 1, f_tan},
8687 {"tanh", 1, 1, f_tanh},
8688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008690 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8691 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008692 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8693#ifdef FEAT_JOB_CHANNEL
8694 {"test_null_channel", 0, 0, f_test_null_channel},
8695#endif
8696 {"test_null_dict", 0, 0, f_test_null_dict},
8697#ifdef FEAT_JOB_CHANNEL
8698 {"test_null_job", 0, 0, f_test_null_job},
8699#endif
8700 {"test_null_list", 0, 0, f_test_null_list},
8701 {"test_null_partial", 0, 0, f_test_null_partial},
8702 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008703#ifdef FEAT_TIMERS
8704 {"timer_start", 2, 3, f_timer_start},
8705 {"timer_stop", 1, 1, f_timer_stop},
8706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {"tolower", 1, 1, f_tolower},
8708 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008709 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008710#ifdef FEAT_FLOAT
8711 {"trunc", 1, 1, f_trunc},
8712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008714 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008715 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008716 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008717 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {"virtcol", 1, 1, f_virtcol},
8719 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008720 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008721 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008722 {"win_getid", 0, 2, f_win_getid},
8723 {"win_gotoid", 1, 1, f_win_gotoid},
8724 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8725 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 {"winbufnr", 1, 1, f_winbufnr},
8727 {"wincol", 0, 0, f_wincol},
8728 {"winheight", 1, 1, f_winheight},
8729 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008730 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008732 {"winrestview", 1, 1, f_winrestview},
8733 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008735 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008736 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008737 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738};
8739
8740#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8741
8742/*
8743 * Function given to ExpandGeneric() to obtain the list of internal
8744 * or user defined function names.
8745 */
8746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008747get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749 static int intidx = -1;
8750 char_u *name;
8751
8752 if (idx == 0)
8753 intidx = -1;
8754 if (intidx < 0)
8755 {
8756 name = get_user_func_name(xp, idx);
8757 if (name != NULL)
8758 return name;
8759 }
8760 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8761 {
8762 STRCPY(IObuff, functions[intidx].f_name);
8763 STRCAT(IObuff, "(");
8764 if (functions[intidx].f_max_argc == 0)
8765 STRCAT(IObuff, ")");
8766 return IObuff;
8767 }
8768
8769 return NULL;
8770}
8771
8772/*
8773 * Function given to ExpandGeneric() to obtain the list of internal or
8774 * user defined variable or function names.
8775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008777get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 static int intidx = -1;
8780 char_u *name;
8781
8782 if (idx == 0)
8783 intidx = -1;
8784 if (intidx < 0)
8785 {
8786 name = get_function_name(xp, idx);
8787 if (name != NULL)
8788 return name;
8789 }
8790 return get_user_var_name(xp, ++intidx);
8791}
8792
8793#endif /* FEAT_CMDL_COMPL */
8794
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008795#if defined(EBCDIC) || defined(PROTO)
8796/*
8797 * Compare struct fst by function name.
8798 */
8799 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008800compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008801{
8802 struct fst *p1 = (struct fst *)s1;
8803 struct fst *p2 = (struct fst *)s2;
8804
8805 return STRCMP(p1->f_name, p2->f_name);
8806}
8807
8808/*
8809 * Sort the function table by function name.
8810 * The sorting of the table above is ASCII dependant.
8811 * On machines using EBCDIC we have to sort it.
8812 */
8813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008814sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008815{
8816 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8817
8818 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8819}
8820#endif
8821
8822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823/*
8824 * Find internal function in table above.
8825 * Return index, or -1 if not found
8826 */
8827 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008828find_internal_func(
8829 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
8831 int first = 0;
8832 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8833 int cmp;
8834 int x;
8835
8836 /*
8837 * Find the function name in the table. Binary search.
8838 */
8839 while (first <= last)
8840 {
8841 x = first + ((unsigned)(last - first) >> 1);
8842 cmp = STRCMP(name, functions[x].f_name);
8843 if (cmp < 0)
8844 last = x - 1;
8845 else if (cmp > 0)
8846 first = x + 1;
8847 else
8848 return x;
8849 }
8850 return -1;
8851}
8852
8853/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008854 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8855 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008856 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8857 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008858 */
8859 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008860deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008861{
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008863 int cc;
8864
Bram Moolenaar65639032016-03-16 21:40:30 +01008865 if (partialp != NULL)
8866 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008867
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008868 cc = name[*lenp];
8869 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008870 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008871 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008873 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008874 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008875 {
8876 *lenp = 0;
8877 return (char_u *)""; /* just in case */
8878 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008879 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008881 }
8882
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008883 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8884 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008885 partial_T *pt = v->di_tv.vval.v_partial;
8886
8887 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008888 {
8889 *lenp = 0;
8890 return (char_u *)""; /* just in case */
8891 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008892 if (partialp != NULL)
8893 *partialp = pt;
8894 *lenp = (int)STRLEN(pt->pt_name);
8895 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008896 }
8897
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008898 return name;
8899}
8900
8901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 * Allocate a variable for the result of a function.
8903 * Return OK or FAIL.
8904 */
8905 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008906get_func_tv(
8907 char_u *name, /* name of the function */
8908 int len, /* length of "name" */
8909 typval_T *rettv,
8910 char_u **arg, /* argument, pointing to the '(' */
8911 linenr_T firstline, /* first line of range */
8912 linenr_T lastline, /* last line of range */
8913 int *doesrange, /* return: function handled range */
8914 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008915 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008916 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 char_u *argp;
8919 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008920 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 int argcount = 0; /* number of arguments found */
8922
8923 /*
8924 * Get the arguments.
8925 */
8926 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008927 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 {
8929 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8930 if (*argp == ')' || *argp == ',' || *argp == NUL)
8931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8933 {
8934 ret = FAIL;
8935 break;
8936 }
8937 ++argcount;
8938 if (*argp != ',')
8939 break;
8940 }
8941 if (*argp == ')')
8942 ++argp;
8943 else
8944 ret = FAIL;
8945
8946 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008947 {
8948 int i = 0;
8949
8950 if (get_vim_var_nr(VV_TESTING))
8951 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008952 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008953 * what variables are used on the call stack. */
8954 if (funcargs.ga_itemsize == 0)
8955 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8956 for (i = 0; i < argcount; ++i)
8957 if (ga_grow(&funcargs, 1) == OK)
8958 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8959 &argvars[i];
8960 }
8961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008963 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008964
8965 funcargs.ga_len -= i;
8966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 {
8969 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008970 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008972 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
8975 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
8978 *arg = skipwhite(argp);
8979 return ret;
8980}
8981
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008982#define ERROR_UNKNOWN 0
8983#define ERROR_TOOMANY 1
8984#define ERROR_TOOFEW 2
8985#define ERROR_SCRIPT 3
8986#define ERROR_DICT 4
8987#define ERROR_NONE 5
8988#define ERROR_OTHER 6
8989#define FLEN_FIXED 40
8990
8991/*
8992 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8993 * Change <SNR>123_name() to K_SNR 123_name().
8994 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8995 * (slow).
8996 */
8997 static char_u *
8998fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8999{
9000 int llen;
9001 char_u *fname;
9002 int i;
9003
9004 llen = eval_fname_script(name);
9005 if (llen > 0)
9006 {
9007 fname_buf[0] = K_SPECIAL;
9008 fname_buf[1] = KS_EXTRA;
9009 fname_buf[2] = (int)KE_SNR;
9010 i = 3;
9011 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9012 {
9013 if (current_SID <= 0)
9014 *error = ERROR_SCRIPT;
9015 else
9016 {
9017 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9018 i = (int)STRLEN(fname_buf);
9019 }
9020 }
9021 if (i + STRLEN(name + llen) < FLEN_FIXED)
9022 {
9023 STRCPY(fname_buf + i, name + llen);
9024 fname = fname_buf;
9025 }
9026 else
9027 {
9028 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9029 if (fname == NULL)
9030 *error = ERROR_OTHER;
9031 else
9032 {
9033 *tofree = fname;
9034 mch_memmove(fname, fname_buf, (size_t)i);
9035 STRCPY(fname + i, name + llen);
9036 }
9037 }
9038 }
9039 else
9040 fname = name;
9041 return fname;
9042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
9044/*
9045 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009046 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009047 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050call_func(
9051 char_u *funcname, /* name of the function */
9052 int len, /* length of "name" */
9053 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009054 int argcount_in, /* number of "argvars" */
9055 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009056 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009057 linenr_T firstline, /* first line of range */
9058 linenr_T lastline, /* last line of range */
9059 int *doesrange, /* return: function handled range */
9060 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009061 partial_T *partial, /* optional, can be NULL */
9062 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 int error = ERROR_NONE;
9066 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009069 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009071 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009072 int argcount = argcount_in;
9073 typval_T *argvars = argvars_in;
9074 dict_T *selfdict = selfdict_in;
9075 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9076 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009077
9078 /* Make a copy of the name, if it comes from a funcref variable it could
9079 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009080 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009081 if (name == NULL)
9082 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009084 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
9086 *doesrange = FALSE;
9087
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009088 if (partial != NULL)
9089 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009090 /* When the function has a partial with a dict and there is a dict
9091 * argument, use the dict argument. That is backwards compatible.
9092 * When the dict was bound explicitly use the one from the partial. */
9093 if (partial->pt_dict != NULL
9094 && (selfdict_in == NULL || !partial->pt_auto))
9095 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009096 if (error == ERROR_NONE && partial->pt_argc > 0)
9097 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009098 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9099 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9100 for (i = 0; i < argcount_in; ++i)
9101 argv[i + argv_clear] = argvars_in[i];
9102 argvars = argv;
9103 argcount = partial->pt_argc + argcount_in;
9104 }
9105 }
9106
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107
9108 /* execute the function if no errors detected and executing */
9109 if (evaluate && error == ERROR_NONE)
9110 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009111 char_u *rfname = fname;
9112
9113 /* Ignore "g:" before a function name. */
9114 if (fname[0] == 'g' && fname[1] == ':')
9115 rfname = fname + 2;
9116
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009117 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9118 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 error = ERROR_UNKNOWN;
9120
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009121 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 {
9123 /*
9124 * User defined function.
9125 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009126 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009127
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009129 /* Trigger FuncUndefined event, may load the function. */
9130 if (fp == NULL
9131 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009132 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009133 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009135 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009136 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 }
9138#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009139 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009140 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009141 {
9142 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009143 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009144 }
9145
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 if (fp != NULL)
9147 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009148 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009150 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009152 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009154 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009155 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 else
9157 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009158 int did_save_redo = FALSE;
9159
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 /*
9161 * Call the user function.
9162 * Save and restore search patterns, script variables and
9163 * redo buffer.
9164 */
9165 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009166#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009167 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009168#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009169 {
9170 saveRedobuff();
9171 did_save_redo = TRUE;
9172 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009173 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009175 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009176 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9177 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9178 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009179 /* Function was unreferenced while being used, free it
9180 * now. */
9181 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009182 if (did_save_redo)
9183 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 restore_search_patterns();
9185 error = ERROR_NONE;
9186 }
9187 }
9188 }
9189 else
9190 {
9191 /*
9192 * Find the function name in the table, call its implementation.
9193 */
9194 i = find_internal_func(fname);
9195 if (i >= 0)
9196 {
9197 if (argcount < functions[i].f_min_argc)
9198 error = ERROR_TOOFEW;
9199 else if (argcount > functions[i].f_max_argc)
9200 error = ERROR_TOOMANY;
9201 else
9202 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009203 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 error = ERROR_NONE;
9206 }
9207 }
9208 }
9209 /*
9210 * The function call (or "FuncUndefined" autocommand sequence) might
9211 * have been aborted by an error, an interrupt, or an explicitly thrown
9212 * exception that has not been caught so far. This situation can be
9213 * tested for by calling aborting(). For an error in an internal
9214 * function or for the "E132" error in call_user_func(), however, the
9215 * throw point at which the "force_abort" flag (temporarily reset by
9216 * emsg()) is normally updated has not been reached yet. We need to
9217 * update that flag first to make aborting() reliable.
9218 */
9219 update_force_abort();
9220 }
9221 if (error == ERROR_NONE)
9222 ret = OK;
9223
9224 /*
9225 * Report an error unless the argument evaluation or function call has been
9226 * cancelled due to an aborting error, an interrupt, or an exception.
9227 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009228 if (!aborting())
9229 {
9230 switch (error)
9231 {
9232 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009233 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009234 break;
9235 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009236 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009237 break;
9238 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009239 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009240 name);
9241 break;
9242 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009243 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009244 name);
9245 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009246 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009247 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009248 name);
9249 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009250 }
9251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009253 while (argv_clear > 0)
9254 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009255 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009256 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257
9258 return ret;
9259}
9260
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009261/*
9262 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009263 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009264 */
9265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009266emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009267{
9268 char_u *p;
9269
9270 if (*name == K_SPECIAL)
9271 p = concat_str((char_u *)"<SNR>", name + 3);
9272 else
9273 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009274 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009275 if (p != name)
9276 vim_free(p);
9277}
9278
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009279/*
9280 * Return TRUE for a non-zero Number and a non-empty String.
9281 */
9282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009283non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009284{
9285 return ((argvars[0].v_type == VAR_NUMBER
9286 && argvars[0].vval.v_number != 0)
9287 || (argvars[0].v_type == VAR_STRING
9288 && argvars[0].vval.v_string != NULL
9289 && *argvars[0].vval.v_string != NUL));
9290}
9291
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292/*********************************************
9293 * Implementation of the built-in functions
9294 */
9295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009296#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009297static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009298
9299/*
9300 * Get the float value of "argvars[0]" into "f".
9301 * Returns FAIL when the argument is not a Number or Float.
9302 */
9303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009305{
9306 if (argvars[0].v_type == VAR_FLOAT)
9307 {
9308 *f = argvars[0].vval.v_float;
9309 return OK;
9310 }
9311 if (argvars[0].v_type == VAR_NUMBER)
9312 {
9313 *f = (float_T)argvars[0].vval.v_number;
9314 return OK;
9315 }
9316 EMSG(_("E808: Number or Float required"));
9317 return FAIL;
9318}
9319
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009320/*
9321 * "abs(expr)" function
9322 */
9323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009324f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009325{
9326 if (argvars[0].v_type == VAR_FLOAT)
9327 {
9328 rettv->v_type = VAR_FLOAT;
9329 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9330 }
9331 else
9332 {
9333 varnumber_T n;
9334 int error = FALSE;
9335
9336 n = get_tv_number_chk(&argvars[0], &error);
9337 if (error)
9338 rettv->vval.v_number = -1;
9339 else if (n > 0)
9340 rettv->vval.v_number = n;
9341 else
9342 rettv->vval.v_number = -n;
9343 }
9344}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009345
9346/*
9347 * "acos()" function
9348 */
9349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009350f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009351{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009352 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009353
9354 rettv->v_type = VAR_FLOAT;
9355 if (get_float_arg(argvars, &f) == OK)
9356 rettv->vval.v_float = acos(f);
9357 else
9358 rettv->vval.v_float = 0.0;
9359}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009360#endif
9361
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009363 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 */
9365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009366f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
Bram Moolenaar33570922005-01-25 22:26:29 +00009368 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009371 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009373 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009374 && !tv_check_lock(l->lv_lock,
9375 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009376 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009378 }
9379 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009380 EMSG(_(e_listreq));
9381}
9382
9383/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009384 * "and(expr, expr)" function
9385 */
9386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009387f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009388{
9389 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9390 & get_tv_number_chk(&argvars[1], NULL);
9391}
9392
9393/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009394 * "append(lnum, string/list)" function
9395 */
9396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009397f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009398{
9399 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009400 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009401 list_T *l = NULL;
9402 listitem_T *li = NULL;
9403 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009404 long added = 0;
9405
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009406 /* When coming here from Insert mode, sync undo, so that this can be
9407 * undone separately from what was previously inserted. */
9408 if (u_sync_once == 2)
9409 {
9410 u_sync_once = 1; /* notify that u_sync() was called */
9411 u_sync(TRUE);
9412 }
9413
Bram Moolenaar0d660222005-01-07 21:51:51 +00009414 lnum = get_tv_lnum(argvars);
9415 if (lnum >= 0
9416 && lnum <= curbuf->b_ml.ml_line_count
9417 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009418 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009419 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009420 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009421 l = argvars[1].vval.v_list;
9422 if (l == NULL)
9423 return;
9424 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009425 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009426 for (;;)
9427 {
9428 if (l == NULL)
9429 tv = &argvars[1]; /* append a string */
9430 else if (li == NULL)
9431 break; /* end of list */
9432 else
9433 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009434 line = get_tv_string_chk(tv);
9435 if (line == NULL) /* type error */
9436 {
9437 rettv->vval.v_number = 1; /* Failed */
9438 break;
9439 }
9440 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009441 ++added;
9442 if (l == NULL)
9443 break;
9444 li = li->li_next;
9445 }
9446
9447 appended_lines_mark(lnum, added);
9448 if (curwin->w_cursor.lnum > lnum)
9449 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009451 else
9452 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453}
9454
9455/*
9456 * "argc()" function
9457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009459f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462}
9463
9464/*
9465 * "argidx()" function
9466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009468f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471}
9472
9473/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009474 * "arglistid()" function
9475 */
9476 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009477f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009478{
9479 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009480
9481 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009482 wp = find_tabwin(&argvars[0], &argvars[1]);
9483 if (wp != NULL)
9484 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009485}
9486
9487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 * "argv(nr)" function
9489 */
9490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
9493 int idx;
9494
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009495 if (argvars[0].v_type != VAR_UNKNOWN)
9496 {
9497 idx = get_tv_number_chk(&argvars[0], NULL);
9498 if (idx >= 0 && idx < ARGCOUNT)
9499 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9500 else
9501 rettv->vval.v_string = NULL;
9502 rettv->v_type = VAR_STRING;
9503 }
9504 else if (rettv_list_alloc(rettv) == OK)
9505 for (idx = 0; idx < ARGCOUNT; ++idx)
9506 list_append_string(rettv->vval.v_list,
9507 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508}
9509
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009510typedef enum
9511{
9512 ASSERT_EQUAL,
9513 ASSERT_NOTEQUAL,
9514 ASSERT_MATCH,
9515 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009516 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009517} assert_type_T;
9518
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009519static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009520static 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 +01009521static void assert_error(garray_T *gap);
9522static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009523
9524/*
9525 * Prepare "gap" for an assert error and add the sourcing position.
9526 */
9527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009529{
9530 char buf[NUMBUFLEN];
9531
9532 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009533 if (sourcing_name != NULL)
9534 {
9535 ga_concat(gap, sourcing_name);
9536 if (sourcing_lnum > 0)
9537 ga_concat(gap, (char_u *)" ");
9538 }
9539 if (sourcing_lnum > 0)
9540 {
9541 sprintf(buf, "line %ld", (long)sourcing_lnum);
9542 ga_concat(gap, (char_u *)buf);
9543 }
9544 if (sourcing_name != NULL || sourcing_lnum > 0)
9545 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009546}
9547
9548/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009549 * Append "str" to "gap", escaping unprintable characters.
9550 * Changes NL to \n, CR to \r, etc.
9551 */
9552 static void
9553ga_concat_esc(garray_T *gap, char_u *str)
9554{
9555 char_u *p;
9556 char_u buf[NUMBUFLEN];
9557
Bram Moolenaarf1551962016-03-15 12:55:58 +01009558 if (str == NULL)
9559 {
9560 ga_concat(gap, (char_u *)"NULL");
9561 return;
9562 }
9563
Bram Moolenaar23689172016-02-15 22:37:37 +01009564 for (p = str; *p != NUL; ++p)
9565 switch (*p)
9566 {
9567 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9568 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9569 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9570 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9571 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9572 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9573 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9574 default:
9575 if (*p < ' ')
9576 {
9577 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9578 ga_concat(gap, buf);
9579 }
9580 else
9581 ga_append(gap, *p);
9582 break;
9583 }
9584}
9585
9586/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009587 * Fill "gap" with information about an assert error.
9588 */
9589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009590fill_assert_error(
9591 garray_T *gap,
9592 typval_T *opt_msg_tv,
9593 char_u *exp_str,
9594 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009595 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009596 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009597{
9598 char_u numbuf[NUMBUFLEN];
9599 char_u *tofree;
9600
9601 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9602 {
9603 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9604 vim_free(tofree);
9605 }
9606 else
9607 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009608 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009609 ga_concat(gap, (char_u *)"Pattern ");
9610 else
9611 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009612 if (exp_str == NULL)
9613 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009614 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009615 vim_free(tofree);
9616 }
9617 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009618 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009619 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009620 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009621 else if (atype == ASSERT_NOTMATCH)
9622 ga_concat(gap, (char_u *)" does match ");
9623 else if (atype == ASSERT_NOTEQUAL)
9624 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009625 else
9626 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009627 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009628 vim_free(tofree);
9629 }
9630}
Bram Moolenaar43345542015-11-29 17:35:35 +01009631
9632/*
9633 * Add an assert error to v:errors.
9634 */
9635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009636assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009637{
9638 struct vimvar *vp = &vimvars[VV_ERRORS];
9639
9640 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9641 /* Make sure v:errors is a list. */
9642 set_vim_var_list(VV_ERRORS, list_alloc());
9643 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9644}
9645
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009646 static void
9647assert_equal_common(typval_T *argvars, assert_type_T atype)
9648{
9649 garray_T ga;
9650
9651 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9652 != (atype == ASSERT_EQUAL))
9653 {
9654 prepare_assert_error(&ga);
9655 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9656 atype);
9657 assert_error(&ga);
9658 ga_clear(&ga);
9659 }
9660}
9661
Bram Moolenaar43345542015-11-29 17:35:35 +01009662/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009663 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009664 */
9665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009666f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009667{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009668 assert_equal_common(argvars, ASSERT_EQUAL);
9669}
Bram Moolenaar43345542015-11-29 17:35:35 +01009670
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009671/*
9672 * "assert_notequal(expected, actual[, msg])" function
9673 */
9674 static void
9675f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9676{
9677 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009678}
9679
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009680/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009681 * "assert_exception(string[, msg])" function
9682 */
9683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009684f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009685{
9686 garray_T ga;
9687 char *error;
9688
9689 error = (char *)get_tv_string_chk(&argvars[0]);
9690 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9691 {
9692 prepare_assert_error(&ga);
9693 ga_concat(&ga, (char_u *)"v:exception is not set");
9694 assert_error(&ga);
9695 ga_clear(&ga);
9696 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009697 else if (error != NULL
9698 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009699 {
9700 prepare_assert_error(&ga);
9701 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009702 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009703 assert_error(&ga);
9704 ga_clear(&ga);
9705 }
9706}
9707
9708/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009709 * "assert_fails(cmd [, error])" function
9710 */
9711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009712f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009713{
9714 char_u *cmd = get_tv_string_chk(&argvars[0]);
9715 garray_T ga;
9716
9717 called_emsg = FALSE;
9718 suppress_errthrow = TRUE;
9719 emsg_silent = TRUE;
9720 do_cmdline_cmd(cmd);
9721 if (!called_emsg)
9722 {
9723 prepare_assert_error(&ga);
9724 ga_concat(&ga, (char_u *)"command did not fail: ");
9725 ga_concat(&ga, cmd);
9726 assert_error(&ga);
9727 ga_clear(&ga);
9728 }
9729 else if (argvars[1].v_type != VAR_UNKNOWN)
9730 {
9731 char_u buf[NUMBUFLEN];
9732 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9733
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009734 if (error == NULL
9735 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009736 {
9737 prepare_assert_error(&ga);
9738 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009739 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009740 assert_error(&ga);
9741 ga_clear(&ga);
9742 }
9743 }
9744
9745 called_emsg = FALSE;
9746 suppress_errthrow = FALSE;
9747 emsg_silent = FALSE;
9748 emsg_on_display = FALSE;
9749 set_vim_var_string(VV_ERRMSG, NULL, 0);
9750}
9751
9752/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009753 * Common for assert_true() and assert_false().
9754 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009756assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009757{
9758 int error = FALSE;
9759 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009760
Bram Moolenaar37127922016-02-06 20:29:28 +01009761 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009762 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009763 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009764 if (argvars[0].v_type != VAR_NUMBER
9765 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9766 || error)
9767 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009768 prepare_assert_error(&ga);
9769 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009770 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009771 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009772 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009773 ga_clear(&ga);
9774 }
9775}
9776
9777/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009778 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009779 */
9780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009781f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009782{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009783 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009784}
9785
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009786 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009787assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009788{
9789 garray_T ga;
9790 char_u buf1[NUMBUFLEN];
9791 char_u buf2[NUMBUFLEN];
9792 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9793 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9794
Bram Moolenaar72188e92016-03-28 22:48:29 +02009795 if (pat == NULL || text == NULL)
9796 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009797 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009798 {
9799 prepare_assert_error(&ga);
9800 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009801 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009802 assert_error(&ga);
9803 ga_clear(&ga);
9804 }
9805}
9806
9807/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009808 * "assert_match(pattern, actual[, msg])" function
9809 */
9810 static void
9811f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9812{
9813 assert_match_common(argvars, ASSERT_MATCH);
9814}
9815
9816/*
9817 * "assert_notmatch(pattern, actual[, msg])" function
9818 */
9819 static void
9820f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9821{
9822 assert_match_common(argvars, ASSERT_NOTMATCH);
9823}
9824
9825/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009826 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009827 */
9828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009829f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009830{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009831 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009832}
9833
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009834#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009835/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009836 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009837 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009839f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009840{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009841 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009842
9843 rettv->v_type = VAR_FLOAT;
9844 if (get_float_arg(argvars, &f) == OK)
9845 rettv->vval.v_float = asin(f);
9846 else
9847 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009848}
9849
9850/*
9851 * "atan()" function
9852 */
9853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009854f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009855{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009856 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009857
9858 rettv->v_type = VAR_FLOAT;
9859 if (get_float_arg(argvars, &f) == OK)
9860 rettv->vval.v_float = atan(f);
9861 else
9862 rettv->vval.v_float = 0.0;
9863}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009864
9865/*
9866 * "atan2()" function
9867 */
9868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009869f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009870{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009871 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009872
9873 rettv->v_type = VAR_FLOAT;
9874 if (get_float_arg(argvars, &fx) == OK
9875 && get_float_arg(&argvars[1], &fy) == OK)
9876 rettv->vval.v_float = atan2(fx, fy);
9877 else
9878 rettv->vval.v_float = 0.0;
9879}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009880#endif
9881
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882/*
9883 * "browse(save, title, initdir, default)" function
9884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009886f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887{
9888#ifdef FEAT_BROWSE
9889 int save;
9890 char_u *title;
9891 char_u *initdir;
9892 char_u *defname;
9893 char_u buf[NUMBUFLEN];
9894 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009895 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009897 save = get_tv_number_chk(&argvars[0], &error);
9898 title = get_tv_string_chk(&argvars[1]);
9899 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9900 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009902 if (error || title == NULL || initdir == NULL || defname == NULL)
9903 rettv->vval.v_string = NULL;
9904 else
9905 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009906 do_browse(save ? BROWSE_SAVE : 0,
9907 title, defname, NULL, initdir, NULL, curbuf);
9908#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009910#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009912}
9913
9914/*
9915 * "browsedir(title, initdir)" function
9916 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009918f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009919{
9920#ifdef FEAT_BROWSE
9921 char_u *title;
9922 char_u *initdir;
9923 char_u buf[NUMBUFLEN];
9924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 title = get_tv_string_chk(&argvars[0]);
9926 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 if (title == NULL || initdir == NULL)
9929 rettv->vval.v_string = NULL;
9930 else
9931 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009932 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937}
9938
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009939static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009940
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941/*
9942 * Find a buffer by number or exact name.
9943 */
9944 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009945find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
9947 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009949 if (avar->v_type == VAR_NUMBER)
9950 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009951 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009953 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009954 if (buf == NULL)
9955 {
9956 /* No full path name match, try a match with a URL or a "nofile"
9957 * buffer, these don't use the full path. */
9958 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9959 if (buf->b_fname != NULL
9960 && (path_with_url(buf->b_fname)
9961#ifdef FEAT_QUICKFIX
9962 || bt_nofile(buf)
9963#endif
9964 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009965 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009966 break;
9967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 }
9969 return buf;
9970}
9971
9972/*
9973 * "bufexists(expr)" function
9974 */
9975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009976f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
9981/*
9982 * "buflisted(expr)" function
9983 */
9984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009985f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
9987 buf_T *buf;
9988
9989 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991}
9992
9993/*
9994 * "bufloaded(expr)" function
9995 */
9996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009997f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998{
9999 buf_T *buf;
10000
10001 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003}
10004
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010005 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010006buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 int save_magic;
10009 char_u *save_cpo;
10010 buf_T *buf;
10011
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10013 save_magic = p_magic;
10014 p_magic = TRUE;
10015 save_cpo = p_cpo;
10016 p_cpo = (char_u *)"";
10017
10018 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010019 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020
10021 p_magic = save_magic;
10022 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010023 return buf;
10024}
10025
10026/*
10027 * Get buffer by number or pattern.
10028 */
10029 static buf_T *
10030get_buf_tv(typval_T *tv, int curtab_only)
10031{
10032 char_u *name = tv->vval.v_string;
10033 buf_T *buf;
10034
10035 if (tv->v_type == VAR_NUMBER)
10036 return buflist_findnr((int)tv->vval.v_number);
10037 if (tv->v_type != VAR_STRING)
10038 return NULL;
10039 if (name == NULL || *name == NUL)
10040 return curbuf;
10041 if (name[0] == '$' && name[1] == NUL)
10042 return lastbuf;
10043
10044 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
10046 /* If not found, try expanding the name, like done for bufexists(). */
10047 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
10050 return buf;
10051}
10052
10053/*
10054 * "bufname(expr)" function
10055 */
10056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010057f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
10059 buf_T *buf;
10060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010061 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010063 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 --emsg_off;
10070}
10071
10072/*
10073 * "bufnr(expr)" function
10074 */
10075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010076f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077{
10078 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010079 int error = FALSE;
10080 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010082 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010084 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010085 --emsg_off;
10086
10087 /* If the buffer isn't found and the second argument is not zero create a
10088 * new buffer. */
10089 if (buf == NULL
10090 && argvars[1].v_type != VAR_UNKNOWN
10091 && get_tv_number_chk(&argvars[1], &error) != 0
10092 && !error
10093 && (name = get_tv_string_chk(&argvars[0])) != NULL
10094 && !error)
10095 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10096
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101}
10102
10103/*
10104 * "bufwinnr(nr)" function
10105 */
10106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010107f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108{
10109#ifdef FEAT_WINDOWS
10110 win_T *wp;
10111 int winnr = 0;
10112#endif
10113 buf_T *buf;
10114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010115 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010117 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118#ifdef FEAT_WINDOWS
10119 for (wp = firstwin; wp; wp = wp->w_next)
10120 {
10121 ++winnr;
10122 if (wp->w_buffer == buf)
10123 break;
10124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128#endif
10129 --emsg_off;
10130}
10131
10132/*
10133 * "byte2line(byte)" function
10134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010136f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137{
10138#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140#else
10141 long boff = 0;
10142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010143 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 (linenr_T)0, &boff);
10149#endif
10150}
10151
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010153byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010154{
10155#ifdef FEAT_MBYTE
10156 char_u *t;
10157#endif
10158 char_u *str;
10159 long idx;
10160
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010161 str = get_tv_string_chk(&argvars[0]);
10162 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010163 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010164 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010165 return;
10166
10167#ifdef FEAT_MBYTE
10168 t = str;
10169 for ( ; idx > 0; idx--)
10170 {
10171 if (*t == NUL) /* EOL reached */
10172 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010173 if (enc_utf8 && comp)
10174 t += utf_ptr2len(t);
10175 else
10176 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010177 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010178 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010179#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010180 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010182#endif
10183}
10184
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010185/*
10186 * "byteidx()" function
10187 */
10188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010189f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010190{
10191 byteidx(argvars, rettv, FALSE);
10192}
10193
10194/*
10195 * "byteidxcomp()" function
10196 */
10197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010198f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010199{
10200 byteidx(argvars, rettv, TRUE);
10201}
10202
Bram Moolenaardb913952012-06-29 12:54:53 +020010203 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010204func_call(
10205 char_u *name,
10206 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010207 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010208 dict_T *selfdict,
10209 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010210{
10211 listitem_T *item;
10212 typval_T argv[MAX_FUNC_ARGS + 1];
10213 int argc = 0;
10214 int dummy;
10215 int r = 0;
10216
10217 for (item = args->vval.v_list->lv_first; item != NULL;
10218 item = item->li_next)
10219 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010220 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010221 {
10222 EMSG(_("E699: Too many arguments"));
10223 break;
10224 }
10225 /* Make a copy of each argument. This is needed to be able to set
10226 * v_lock to VAR_FIXED in the copy without changing the original list.
10227 */
10228 copy_tv(&item->li_tv, &argv[argc++]);
10229 }
10230
10231 if (item == NULL)
10232 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10233 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010234 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010235
10236 /* Free the arguments. */
10237 while (argc > 0)
10238 clear_tv(&argv[--argc]);
10239
10240 return r;
10241}
10242
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010243/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010244 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010245 */
10246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010247f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010248{
10249 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010250 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010251 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010252
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010253 if (argvars[1].v_type != VAR_LIST)
10254 {
10255 EMSG(_(e_listreq));
10256 return;
10257 }
10258 if (argvars[1].vval.v_list == NULL)
10259 return;
10260
10261 if (argvars[0].v_type == VAR_FUNC)
10262 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010263 else if (argvars[0].v_type == VAR_PARTIAL)
10264 {
10265 partial = argvars[0].vval.v_partial;
10266 func = partial->pt_name;
10267 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010268 else
10269 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010270 if (*func == NUL)
10271 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010272
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 if (argvars[2].v_type != VAR_UNKNOWN)
10274 {
10275 if (argvars[2].v_type != VAR_DICT)
10276 {
10277 EMSG(_(e_dictreq));
10278 return;
10279 }
10280 selfdict = argvars[2].vval.v_dict;
10281 }
10282
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010283 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010284}
10285
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010286#ifdef FEAT_FLOAT
10287/*
10288 * "ceil({float})" function
10289 */
10290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010291f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010292{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010293 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010294
10295 rettv->v_type = VAR_FLOAT;
10296 if (get_float_arg(argvars, &f) == OK)
10297 rettv->vval.v_float = ceil(f);
10298 else
10299 rettv->vval.v_float = 0.0;
10300}
10301#endif
10302
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010303#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010304/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010305 * "ch_close()" function
10306 */
10307 static void
10308f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10309{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010310 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010311
10312 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010313 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010314 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010315 channel_clear(channel);
10316 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010317}
10318
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010319/*
10320 * "ch_getbufnr()" function
10321 */
10322 static void
10323f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10324{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010325 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010326
10327 rettv->vval.v_number = -1;
10328 if (channel != NULL)
10329 {
10330 char_u *what = get_tv_string(&argvars[1]);
10331 int part;
10332
10333 if (STRCMP(what, "err") == 0)
10334 part = PART_ERR;
10335 else if (STRCMP(what, "out") == 0)
10336 part = PART_OUT;
10337 else if (STRCMP(what, "in") == 0)
10338 part = PART_IN;
10339 else
10340 part = PART_SOCK;
10341 if (channel->ch_part[part].ch_buffer != NULL)
10342 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10343 }
10344}
10345
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010346/*
10347 * "ch_getjob()" function
10348 */
10349 static void
10350f_ch_getjob(typval_T *argvars, typval_T *rettv)
10351{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010352 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010353
10354 if (channel != NULL)
10355 {
10356 rettv->v_type = VAR_JOB;
10357 rettv->vval.v_job = channel->ch_job;
10358 if (channel->ch_job != NULL)
10359 ++channel->ch_job->jv_refcount;
10360 }
10361}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010362
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010363/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010364 * "ch_info()" function
10365 */
10366 static void
10367f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10368{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010369 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010370
10371 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10372 channel_info(channel, rettv->vval.v_dict);
10373}
10374
10375/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010376 * "ch_log()" function
10377 */
10378 static void
10379f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10380{
10381 char_u *msg = get_tv_string(&argvars[0]);
10382 channel_T *channel = NULL;
10383
10384 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010385 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010386
10387 ch_log(channel, (char *)msg);
10388}
10389
10390/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010391 * "ch_logfile()" function
10392 */
10393 static void
10394f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10395{
10396 char_u *fname;
10397 char_u *opt = (char_u *)"";
10398 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010399
10400 fname = get_tv_string(&argvars[0]);
10401 if (argvars[1].v_type == VAR_STRING)
10402 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010403 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010404}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010405
10406/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010407 * "ch_open()" function
10408 */
10409 static void
10410f_ch_open(typval_T *argvars, typval_T *rettv)
10411{
Bram Moolenaar77073442016-02-13 23:23:53 +010010412 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010413 if (check_restricted() || check_secure())
10414 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010415 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010416}
10417
10418/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010419 * "ch_read()" function
10420 */
10421 static void
10422f_ch_read(typval_T *argvars, typval_T *rettv)
10423{
10424 common_channel_read(argvars, rettv, FALSE);
10425}
10426
10427/*
10428 * "ch_readraw()" function
10429 */
10430 static void
10431f_ch_readraw(typval_T *argvars, typval_T *rettv)
10432{
10433 common_channel_read(argvars, rettv, TRUE);
10434}
10435
10436/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010437 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010438 */
10439 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010440f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10441{
10442 ch_expr_common(argvars, rettv, TRUE);
10443}
10444
10445/*
10446 * "ch_sendexpr()" function
10447 */
10448 static void
10449f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10450{
10451 ch_expr_common(argvars, rettv, FALSE);
10452}
10453
10454/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010455 * "ch_evalraw()" function
10456 */
10457 static void
10458f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10459{
10460 ch_raw_common(argvars, rettv, TRUE);
10461}
10462
10463/*
10464 * "ch_sendraw()" function
10465 */
10466 static void
10467f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10468{
10469 ch_raw_common(argvars, rettv, FALSE);
10470}
10471
10472/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010473 * "ch_setoptions()" function
10474 */
10475 static void
10476f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10477{
10478 channel_T *channel;
10479 jobopt_T opt;
10480
Bram Moolenaar437905c2016-04-26 19:01:05 +020010481 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010482 if (channel == NULL)
10483 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010484 clear_job_options(&opt);
10485 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010486 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10487 channel_set_options(channel, &opt);
10488 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010489}
10490
10491/*
10492 * "ch_status()" function
10493 */
10494 static void
10495f_ch_status(typval_T *argvars, typval_T *rettv)
10496{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010497 channel_T *channel;
10498
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010499 /* return an empty string by default */
10500 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010501 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010502
Bram Moolenaar437905c2016-04-26 19:01:05 +020010503 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010504 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010505}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010506#endif
10507
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010508/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010509 * "changenr()" function
10510 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010512f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010513{
10514 rettv->vval.v_number = curbuf->b_u_seq_cur;
10515}
10516
10517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 * "char2nr(string)" function
10519 */
10520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010521f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522{
10523#ifdef FEAT_MBYTE
10524 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010525 {
10526 int utf8 = 0;
10527
10528 if (argvars[1].v_type != VAR_UNKNOWN)
10529 utf8 = get_tv_number_chk(&argvars[1], NULL);
10530
10531 if (utf8)
10532 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10533 else
10534 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 else
10537#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539}
10540
10541/*
10542 * "cindent(lnum)" function
10543 */
10544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010545f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
10547#ifdef FEAT_CINDENT
10548 pos_T pos;
10549 linenr_T lnum;
10550
10551 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10554 {
10555 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 curwin->w_cursor = pos;
10558 }
10559 else
10560#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562}
10563
10564/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010565 * "clearmatches()" function
10566 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010568f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010569{
10570#ifdef FEAT_SEARCH_EXTRA
10571 clear_matches(curwin);
10572#endif
10573}
10574
10575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 * "col(string)" function
10577 */
10578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010579f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580{
10581 colnr_T col = 0;
10582 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010583 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010585 fp = var2fpos(&argvars[0], FALSE, &fnum);
10586 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 {
10588 if (fp->col == MAXCOL)
10589 {
10590 /* '> can be MAXCOL, get the length of the line then */
10591 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010592 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 else
10594 col = MAXCOL;
10595 }
10596 else
10597 {
10598 col = fp->col + 1;
10599#ifdef FEAT_VIRTUALEDIT
10600 /* col(".") when the cursor is on the NUL at the end of the line
10601 * because of "coladd" can be seen as an extra column. */
10602 if (virtual_active() && fp == &curwin->w_cursor)
10603 {
10604 char_u *p = ml_get_cursor();
10605
10606 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10607 curwin->w_virtcol - curwin->w_cursor.coladd))
10608 {
10609# ifdef FEAT_MBYTE
10610 int l;
10611
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010612 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 col += l;
10614# else
10615 if (*p != NUL && p[1] == NUL)
10616 ++col;
10617# endif
10618 }
10619 }
10620#endif
10621 }
10622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010623 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624}
10625
Bram Moolenaar572cb562005-08-05 21:35:02 +000010626#if defined(FEAT_INS_EXPAND)
10627/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010628 * "complete()" function
10629 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010631f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010632{
10633 int startcol;
10634
10635 if ((State & INSERT) == 0)
10636 {
10637 EMSG(_("E785: complete() can only be used in Insert mode"));
10638 return;
10639 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010640
10641 /* Check for undo allowed here, because if something was already inserted
10642 * the line was already saved for undo and this check isn't done. */
10643 if (!undo_allowed())
10644 return;
10645
Bram Moolenaarade00832006-03-10 21:46:58 +000010646 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10647 {
10648 EMSG(_(e_invarg));
10649 return;
10650 }
10651
10652 startcol = get_tv_number_chk(&argvars[0], NULL);
10653 if (startcol <= 0)
10654 return;
10655
10656 set_completion(startcol - 1, argvars[1].vval.v_list);
10657}
10658
10659/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010660 * "complete_add()" function
10661 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010663f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010664{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010665 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010666}
10667
10668/*
10669 * "complete_check()" function
10670 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010672f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010673{
10674 int saved = RedrawingDisabled;
10675
10676 RedrawingDisabled = 0;
10677 ins_compl_check_keys(0);
10678 rettv->vval.v_number = compl_interrupted;
10679 RedrawingDisabled = saved;
10680}
10681#endif
10682
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683/*
10684 * "confirm(message, buttons[, default [, type]])" function
10685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010687f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
10689#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10690 char_u *message;
10691 char_u *buttons = NULL;
10692 char_u buf[NUMBUFLEN];
10693 char_u buf2[NUMBUFLEN];
10694 int def = 1;
10695 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010696 char_u *typestr;
10697 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 message = get_tv_string_chk(&argvars[0]);
10700 if (message == NULL)
10701 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010702 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010704 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10705 if (buttons == NULL)
10706 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010707 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010709 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010710 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010712 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10713 if (typestr == NULL)
10714 error = TRUE;
10715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010717 switch (TOUPPER_ASC(*typestr))
10718 {
10719 case 'E': type = VIM_ERROR; break;
10720 case 'Q': type = VIM_QUESTION; break;
10721 case 'I': type = VIM_INFO; break;
10722 case 'W': type = VIM_WARNING; break;
10723 case 'G': type = VIM_GENERIC; break;
10724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 }
10726 }
10727 }
10728 }
10729
10730 if (buttons == NULL || *buttons == NUL)
10731 buttons = (char_u *)_("&Ok");
10732
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010733 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010735 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736#endif
10737}
10738
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010739/*
10740 * "copy()" function
10741 */
10742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010743f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010744{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010745 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010746}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010748#ifdef FEAT_FLOAT
10749/*
10750 * "cos()" function
10751 */
10752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010753f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010754{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010755 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010756
10757 rettv->v_type = VAR_FLOAT;
10758 if (get_float_arg(argvars, &f) == OK)
10759 rettv->vval.v_float = cos(f);
10760 else
10761 rettv->vval.v_float = 0.0;
10762}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010763
10764/*
10765 * "cosh()" function
10766 */
10767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010768f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010769{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010770 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010771
10772 rettv->v_type = VAR_FLOAT;
10773 if (get_float_arg(argvars, &f) == OK)
10774 rettv->vval.v_float = cosh(f);
10775 else
10776 rettv->vval.v_float = 0.0;
10777}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010778#endif
10779
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010781 * "count()" function
10782 */
10783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010784f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010785{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010786 long n = 0;
10787 int ic = FALSE;
10788
Bram Moolenaare9a41262005-01-15 22:18:47 +000010789 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010790 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010791 listitem_T *li;
10792 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010793 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010794
Bram Moolenaare9a41262005-01-15 22:18:47 +000010795 if ((l = argvars[0].vval.v_list) != NULL)
10796 {
10797 li = l->lv_first;
10798 if (argvars[2].v_type != VAR_UNKNOWN)
10799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010800 int error = FALSE;
10801
10802 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010803 if (argvars[3].v_type != VAR_UNKNOWN)
10804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 idx = get_tv_number_chk(&argvars[3], &error);
10806 if (!error)
10807 {
10808 li = list_find(l, idx);
10809 if (li == NULL)
10810 EMSGN(_(e_listidx), idx);
10811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010812 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010813 if (error)
10814 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010815 }
10816
10817 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010818 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010819 ++n;
10820 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010821 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010822 else if (argvars[0].v_type == VAR_DICT)
10823 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010824 int todo;
10825 dict_T *d;
10826 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010827
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010828 if ((d = argvars[0].vval.v_dict) != NULL)
10829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010830 int error = FALSE;
10831
Bram Moolenaare9a41262005-01-15 22:18:47 +000010832 if (argvars[2].v_type != VAR_UNKNOWN)
10833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010834 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835 if (argvars[3].v_type != VAR_UNKNOWN)
10836 EMSG(_(e_invarg));
10837 }
10838
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010839 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010840 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010841 {
10842 if (!HASHITEM_EMPTY(hi))
10843 {
10844 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010845 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010846 ++n;
10847 }
10848 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010849 }
10850 }
10851 else
10852 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010853 rettv->vval.v_number = n;
10854}
10855
10856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10858 *
10859 * Checks the existence of a cscope connection.
10860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010862f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863{
10864#ifdef FEAT_CSCOPE
10865 int num = 0;
10866 char_u *dbpath = NULL;
10867 char_u *prepend = NULL;
10868 char_u buf[NUMBUFLEN];
10869
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010870 if (argvars[0].v_type != VAR_UNKNOWN
10871 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 num = (int)get_tv_number(&argvars[0]);
10874 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 }
10878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880#endif
10881}
10882
10883/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010884 * "cursor(lnum, col)" function, or
10885 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010887 * Moves the cursor to the specified line and column.
10888 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010891f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892{
10893 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010894#ifdef FEAT_VIRTUALEDIT
10895 long coladd = 0;
10896#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010897 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010899 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010900 if (argvars[1].v_type == VAR_UNKNOWN)
10901 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010902 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010903 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010904
Bram Moolenaar493c1782014-05-28 14:34:46 +020010905 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010906 {
10907 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010908 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010909 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010910 line = pos.lnum;
10911 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010912#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010913 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010914#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010915 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010916 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010917 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010918 set_curswant = FALSE;
10919 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010920 }
10921 else
10922 {
10923 line = get_tv_lnum(argvars);
10924 col = get_tv_number_chk(&argvars[1], NULL);
10925#ifdef FEAT_VIRTUALEDIT
10926 if (argvars[2].v_type != VAR_UNKNOWN)
10927 coladd = get_tv_number_chk(&argvars[2], NULL);
10928#endif
10929 }
10930 if (line < 0 || col < 0
10931#ifdef FEAT_VIRTUALEDIT
10932 || coladd < 0
10933#endif
10934 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010935 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 if (line > 0)
10937 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 if (col > 0)
10939 curwin->w_cursor.col = col - 1;
10940#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010941 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942#endif
10943
10944 /* Make sure the cursor is in a valid position. */
10945 check_cursor();
10946#ifdef FEAT_MBYTE
10947 /* Correct cursor for multi-byte character. */
10948 if (has_mbyte)
10949 mb_adjust_cursor();
10950#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010951
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010952 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010953 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954}
10955
10956/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 */
10959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010960f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010962 int noref = 0;
10963
10964 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010966 if (noref < 0 || noref > 1)
10967 EMSG(_(e_invarg));
10968 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010969 {
10970 current_copyID += COPYID_INC;
10971 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973}
10974
10975/*
10976 * "delete()" function
10977 */
10978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010979f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010981 char_u nbuf[NUMBUFLEN];
10982 char_u *name;
10983 char_u *flags;
10984
10985 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010987 return;
10988
10989 name = get_tv_string(&argvars[0]);
10990 if (name == NULL || *name == NUL)
10991 {
10992 EMSG(_(e_invarg));
10993 return;
10994 }
10995
10996 if (argvars[1].v_type != VAR_UNKNOWN)
10997 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010999 flags = (char_u *)"";
11000
11001 if (*flags == NUL)
11002 /* delete a file */
11003 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11004 else if (STRCMP(flags, "d") == 0)
11005 /* delete an empty directory */
11006 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11007 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011008 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011009 rettv->vval.v_number = delete_recursive(name);
11010 else
11011 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012}
11013
11014/*
11015 * "did_filetype()" function
11016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011018f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019{
11020#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022#endif
11023}
11024
11025/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011026 * "diff_filler()" function
11027 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011029f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011030{
11031#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011033#endif
11034}
11035
11036/*
11037 * "diff_hlID()" function
11038 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011040f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011041{
11042#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011044 static linenr_T prev_lnum = 0;
11045 static int changedtick = 0;
11046 static int fnum = 0;
11047 static int change_start = 0;
11048 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011049 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011050 int filler_lines;
11051 int col;
11052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011053 if (lnum < 0) /* ignore type error in {lnum} arg */
11054 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011055 if (lnum != prev_lnum
11056 || changedtick != curbuf->b_changedtick
11057 || fnum != curbuf->b_fnum)
11058 {
11059 /* New line, buffer, change: need to get the values. */
11060 filler_lines = diff_check(curwin, lnum);
11061 if (filler_lines < 0)
11062 {
11063 if (filler_lines == -1)
11064 {
11065 change_start = MAXCOL;
11066 change_end = -1;
11067 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11068 hlID = HLF_ADD; /* added line */
11069 else
11070 hlID = HLF_CHD; /* changed line */
11071 }
11072 else
11073 hlID = HLF_ADD; /* added line */
11074 }
11075 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011076 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011077 prev_lnum = lnum;
11078 changedtick = curbuf->b_changedtick;
11079 fnum = curbuf->b_fnum;
11080 }
11081
11082 if (hlID == HLF_CHD || hlID == HLF_TXD)
11083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011085 if (col >= change_start && col <= change_end)
11086 hlID = HLF_TXD; /* changed text */
11087 else
11088 hlID = HLF_CHD; /* changed line */
11089 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011090 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011091#endif
11092}
11093
11094/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011095 * "empty({expr})" function
11096 */
11097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011098f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011099{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011100 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011101
11102 switch (argvars[0].v_type)
11103 {
11104 case VAR_STRING:
11105 case VAR_FUNC:
11106 n = argvars[0].vval.v_string == NULL
11107 || *argvars[0].vval.v_string == NUL;
11108 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011109 case VAR_PARTIAL:
11110 n = FALSE;
11111 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011112 case VAR_NUMBER:
11113 n = argvars[0].vval.v_number == 0;
11114 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011115 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011116#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011117 n = argvars[0].vval.v_float == 0.0;
11118 break;
11119#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011120 case VAR_LIST:
11121 n = argvars[0].vval.v_list == NULL
11122 || argvars[0].vval.v_list->lv_first == NULL;
11123 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 case VAR_DICT:
11125 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011126 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011128 case VAR_SPECIAL:
11129 n = argvars[0].vval.v_number != VVAL_TRUE;
11130 break;
11131
Bram Moolenaar835dc632016-02-07 14:27:38 +010011132 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011133#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011134 n = argvars[0].vval.v_job == NULL
11135 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11136 break;
11137#endif
11138 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011140 n = argvars[0].vval.v_channel == NULL
11141 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011142 break;
11143#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011144 case VAR_UNKNOWN:
11145 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11146 n = TRUE;
11147 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011148 }
11149
11150 rettv->vval.v_number = n;
11151}
11152
11153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 * "escape({string}, {chars})" function
11155 */
11156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011157f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158{
11159 char_u buf[NUMBUFLEN];
11160
Bram Moolenaar758711c2005-02-02 23:11:38 +000011161 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11162 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164}
11165
11166/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011167 * "eval()" function
11168 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011170f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011171{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011172 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 s = get_tv_string_chk(&argvars[0]);
11175 if (s != NULL)
11176 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011177
Bram Moolenaar615b9972015-01-14 17:15:05 +010011178 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11180 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011181 if (p != NULL && !aborting())
11182 EMSG2(_(e_invexpr2), p);
11183 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011185 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011187 else if (*s != NUL)
11188 EMSG(_(e_trailing));
11189}
11190
11191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 * "eventhandler()" function
11193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011195f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011197 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198}
11199
11200/*
11201 * "executable()" function
11202 */
11203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011204f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011206 char_u *name = get_tv_string(&argvars[0]);
11207
11208 /* Check in $PATH and also check directly if there is a directory name. */
11209 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11210 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011211}
11212
11213/*
11214 * "exepath()" function
11215 */
11216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011217f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011218{
11219 char_u *p = NULL;
11220
Bram Moolenaarb5971142015-03-21 17:32:19 +010011221 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011222 rettv->v_type = VAR_STRING;
11223 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224}
11225
11226/*
11227 * "exists()" function
11228 */
11229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011230f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231{
11232 char_u *p;
11233 char_u *name;
11234 int n = FALSE;
11235 int len = 0;
11236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011237 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 if (*p == '$') /* environment variable */
11239 {
11240 /* first try "normal" environment variables (fast) */
11241 if (mch_getenv(p + 1) != NULL)
11242 n = TRUE;
11243 else
11244 {
11245 /* try expanding things like $VIM and ${HOME} */
11246 p = expand_env_save(p);
11247 if (p != NULL && *p != '$')
11248 n = TRUE;
11249 vim_free(p);
11250 }
11251 }
11252 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011255 if (*skipwhite(p) != NUL)
11256 n = FALSE; /* trailing garbage */
11257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 else if (*p == '*') /* internal or user defined function */
11259 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011260 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 }
11262 else if (*p == ':')
11263 {
11264 n = cmd_exists(p + 1);
11265 }
11266 else if (*p == '#')
11267 {
11268#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011269 if (p[1] == '#')
11270 n = autocmd_supported(p + 2);
11271 else
11272 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273#endif
11274 }
11275 else /* internal variable */
11276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011277 char_u *tofree;
11278 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011280 /* get_name_len() takes care of expanding curly braces */
11281 name = p;
11282 len = get_name_len(&p, &tofree, TRUE, FALSE);
11283 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011285 if (tofree != NULL)
11286 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011287 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011288 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011290 /* handle d.key, l[idx], f(expr) */
11291 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11292 if (n)
11293 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 }
11295 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011296 if (*p != NUL)
11297 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011299 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 }
11301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011302 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303}
11304
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011305#ifdef FEAT_FLOAT
11306/*
11307 * "exp()" function
11308 */
11309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011310f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011311{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011312 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011313
11314 rettv->v_type = VAR_FLOAT;
11315 if (get_float_arg(argvars, &f) == OK)
11316 rettv->vval.v_float = exp(f);
11317 else
11318 rettv->vval.v_float = 0.0;
11319}
11320#endif
11321
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322/*
11323 * "expand()" function
11324 */
11325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011326f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
11328 char_u *s;
11329 int len;
11330 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011331 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011333 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011334 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011337 if (argvars[1].v_type != VAR_UNKNOWN
11338 && argvars[2].v_type != VAR_UNKNOWN
11339 && get_tv_number_chk(&argvars[2], &error)
11340 && !error)
11341 {
11342 rettv->v_type = VAR_LIST;
11343 rettv->vval.v_list = NULL;
11344 }
11345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 if (*s == '%' || *s == '#' || *s == '<')
11348 {
11349 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011350 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011352 if (rettv->v_type == VAR_LIST)
11353 {
11354 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11355 list_append_string(rettv->vval.v_list, result, -1);
11356 else
11357 vim_free(result);
11358 }
11359 else
11360 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 }
11362 else
11363 {
11364 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011365 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 if (argvars[1].v_type != VAR_UNKNOWN
11367 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011368 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369 if (!error)
11370 {
11371 ExpandInit(&xpc);
11372 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011373 if (p_wic)
11374 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011375 if (rettv->v_type == VAR_STRING)
11376 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11377 options, WILD_ALL);
11378 else if (rettv_list_alloc(rettv) != FAIL)
11379 {
11380 int i;
11381
11382 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11383 for (i = 0; i < xpc.xp_numfiles; i++)
11384 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11385 ExpandCleanup(&xpc);
11386 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011387 }
11388 else
11389 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 }
11391}
11392
11393/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011394 * Go over all entries in "d2" and add them to "d1".
11395 * When "action" is "error" then a duplicate key is an error.
11396 * When "action" is "force" then a duplicate key is overwritten.
11397 * Otherwise duplicate keys are ignored ("action" is "keep").
11398 */
11399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011400dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011401{
11402 dictitem_T *di1;
11403 hashitem_T *hi2;
11404 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011405 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011406
11407 todo = (int)d2->dv_hashtab.ht_used;
11408 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11409 {
11410 if (!HASHITEM_EMPTY(hi2))
11411 {
11412 --todo;
11413 di1 = dict_find(d1, hi2->hi_key, -1);
11414 if (d1->dv_scope != 0)
11415 {
11416 /* Disallow replacing a builtin function in l: and g:.
11417 * Check the key to be valid when adding to any
11418 * scope. */
11419 if (d1->dv_scope == VAR_DEF_SCOPE
11420 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11421 && var_check_func_name(hi2->hi_key,
11422 di1 == NULL))
11423 break;
11424 if (!valid_varname(hi2->hi_key))
11425 break;
11426 }
11427 if (di1 == NULL)
11428 {
11429 di1 = dictitem_copy(HI2DI(hi2));
11430 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11431 dictitem_free(di1);
11432 }
11433 else if (*action == 'e')
11434 {
11435 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11436 break;
11437 }
11438 else if (*action == 'f' && HI2DI(hi2) != di1)
11439 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011440 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11441 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011442 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011443 clear_tv(&di1->di_tv);
11444 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11445 }
11446 }
11447 }
11448}
11449
11450/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011451 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011452 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011453 */
11454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011455f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011456{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011457 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011458
Bram Moolenaare9a41262005-01-15 22:18:47 +000011459 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011460 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 list_T *l1, *l2;
11462 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011463 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011465
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 l1 = argvars[0].vval.v_list;
11467 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011468 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011469 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011470 {
11471 if (argvars[2].v_type != VAR_UNKNOWN)
11472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011473 before = get_tv_number_chk(&argvars[2], &error);
11474 if (error)
11475 return; /* type error; errmsg already given */
11476
Bram Moolenaar758711c2005-02-02 23:11:38 +000011477 if (before == l1->lv_len)
11478 item = NULL;
11479 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011480 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011481 item = list_find(l1, before);
11482 if (item == NULL)
11483 {
11484 EMSGN(_(e_listidx), before);
11485 return;
11486 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011487 }
11488 }
11489 else
11490 item = NULL;
11491 list_extend(l1, l2, item);
11492
Bram Moolenaare9a41262005-01-15 22:18:47 +000011493 copy_tv(&argvars[0], rettv);
11494 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011496 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11497 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011498 dict_T *d1, *d2;
11499 char_u *action;
11500 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011501
11502 d1 = argvars[0].vval.v_dict;
11503 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011504 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011505 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506 {
11507 /* Check the third argument. */
11508 if (argvars[2].v_type != VAR_UNKNOWN)
11509 {
11510 static char *(av[]) = {"keep", "force", "error"};
11511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011512 action = get_tv_string_chk(&argvars[2]);
11513 if (action == NULL)
11514 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011515 for (i = 0; i < 3; ++i)
11516 if (STRCMP(action, av[i]) == 0)
11517 break;
11518 if (i == 3)
11519 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011520 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011521 return;
11522 }
11523 }
11524 else
11525 action = (char_u *)"force";
11526
Bram Moolenaara9922d62013-05-30 13:01:18 +020011527 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528
Bram Moolenaare9a41262005-01-15 22:18:47 +000011529 copy_tv(&argvars[0], rettv);
11530 }
11531 }
11532 else
11533 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011534}
11535
11536/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011537 * "feedkeys()" function
11538 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011540f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011541{
11542 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011543 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011544 char_u *keys, *flags;
11545 char_u nbuf[NUMBUFLEN];
11546 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011547 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011548 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011549 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011550
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011551 /* This is not allowed in the sandbox. If the commands would still be
11552 * executed in the sandbox it would be OK, but it probably happens later,
11553 * when "sandbox" is no longer set. */
11554 if (check_secure())
11555 return;
11556
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011557 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011558
11559 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011560 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011561 flags = get_tv_string_buf(&argvars[1], nbuf);
11562 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011563 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011564 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011565 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011566 case 'n': remap = FALSE; break;
11567 case 'm': remap = TRUE; break;
11568 case 't': typed = TRUE; break;
11569 case 'i': insert = TRUE; break;
11570 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011571 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011572 }
11573 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011574 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011575
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011576 if (*keys != NUL || execute)
11577 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011578 /* Need to escape K_SPECIAL and CSI before putting the string in the
11579 * typeahead buffer. */
11580 keys_esc = vim_strsave_escape_csi(keys);
11581 if (keys_esc != NULL)
11582 {
11583 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011584 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011585 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011586 if (vgetc_busy)
11587 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011588 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011589 {
11590 int save_msg_scroll = msg_scroll;
11591
11592 /* Avoid a 1 second delay when the keys start Insert mode. */
11593 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011594
Bram Moolenaar245c4102016-04-20 17:37:41 +020011595 if (!dangerous)
11596 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011597 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011598 if (!dangerous)
11599 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011600 msg_scroll |= save_msg_scroll;
11601 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011602 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011603 }
11604}
11605
11606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 * "filereadable()" function
11608 */
11609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011610f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011612 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 char_u *p;
11614 int n;
11615
Bram Moolenaarc236c162008-07-13 17:41:49 +000011616#ifndef O_NONBLOCK
11617# define O_NONBLOCK 0
11618#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011620 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11621 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622 {
11623 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011624 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 }
11626 else
11627 n = FALSE;
11628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630}
11631
11632/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011633 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 * rights to write into.
11635 */
11636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011637f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011639 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640}
11641
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011642 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011643findfilendir(
11644 typval_T *argvars UNUSED,
11645 typval_T *rettv,
11646 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011647{
11648#ifdef FEAT_SEARCHPATH
11649 char_u *fname;
11650 char_u *fresult = NULL;
11651 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11652 char_u *p;
11653 char_u pathbuf[NUMBUFLEN];
11654 int count = 1;
11655 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011656 int error = FALSE;
11657#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011658
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011659 rettv->vval.v_string = NULL;
11660 rettv->v_type = VAR_STRING;
11661
11662#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011664
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011665 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11668 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011669 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670 else
11671 {
11672 if (*p != NUL)
11673 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011676 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011678 }
11679
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011680 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11681 error = TRUE;
11682
11683 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011685 do
11686 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011687 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011688 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 fresult = find_file_in_path_option(first ? fname : NULL,
11690 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011691 0, first, path,
11692 find_what,
11693 curbuf->b_ffname,
11694 find_what == FINDFILE_DIR
11695 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011696 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011697
11698 if (fresult != NULL && rettv->v_type == VAR_LIST)
11699 list_append_string(rettv->vval.v_list, fresult, -1);
11700
11701 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011702 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011703
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011704 if (rettv->v_type == VAR_STRING)
11705 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011707}
11708
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011709static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11710static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011711
11712/*
11713 * Implementation of map() and filter().
11714 */
11715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011716filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011717{
11718 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011719 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011720 listitem_T *li, *nli;
11721 list_T *l = NULL;
11722 dictitem_T *di;
11723 hashtab_T *ht;
11724 hashitem_T *hi;
11725 dict_T *d = NULL;
11726 typval_T save_val;
11727 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011728 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011729 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011730 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011731 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011732 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011733 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011734 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011735
Bram Moolenaare9a41262005-01-15 22:18:47 +000011736 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011737 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011738 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011739 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740 return;
11741 }
11742 else if (argvars[0].v_type == VAR_DICT)
11743 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011744 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011745 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 return;
11747 }
11748 else
11749 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011750 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 return;
11752 }
11753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 expr = get_tv_string_buf_chk(&argvars[1], buf);
11755 /* On type errors, the preceding call has already displayed an error
11756 * message. Avoid a misleading error message for an empty string that
11757 * was not passed as argument. */
11758 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011760 prepare_vimvar(VV_VAL, &save_val);
11761 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011762
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011763 /* We reset "did_emsg" to be able to detect whether an error
11764 * occurred during evaluation of the expression. */
11765 save_did_emsg = did_emsg;
11766 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011767
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011768 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011769 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 vimvars[VV_KEY].vv_type = VAR_STRING;
11772
11773 ht = &d->dv_hashtab;
11774 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011775 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011778 if (!HASHITEM_EMPTY(hi))
11779 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011780 int r;
11781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 --todo;
11783 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011784 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011785 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11786 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 break;
11788 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011789 r = filter_map_one(&di->di_tv, expr, map, &rem);
11790 clear_tv(&vimvars[VV_KEY].vv_tv);
11791 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792 break;
11793 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011794 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011795 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11796 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011797 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011799 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011800 }
11801 }
11802 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 }
11804 else
11805 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011806 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 for (li = l->lv_first; li != NULL; li = nli)
11809 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011810 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011811 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011813 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011814 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011815 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011816 break;
11817 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011819 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011820 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011821 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011822
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011823 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011825
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011826 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011827 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011828
11829 copy_tv(&argvars[0], rettv);
11830}
11831
11832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011833filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011834{
Bram Moolenaar33570922005-01-25 22:26:29 +000011835 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011836 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011837 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011838
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011840 s = expr;
11841 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011842 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 if (*s != NUL) /* check for trailing chars after expr */
11844 {
11845 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011846 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011847 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848 }
11849 if (map)
11850 {
11851 /* map(): replace the list item value */
11852 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011853 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011854 *tv = rettv;
11855 }
11856 else
11857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011858 int error = FALSE;
11859
Bram Moolenaare9a41262005-01-15 22:18:47 +000011860 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011861 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011862 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011863 /* On type error, nothing has been removed; return FAIL to stop the
11864 * loop. The error message was given by get_tv_number_chk(). */
11865 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011866 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011868 retval = OK;
11869theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011870 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011871 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011872}
11873
11874/*
11875 * "filter()" function
11876 */
11877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011878f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011879{
11880 filter_map(argvars, rettv, FALSE);
11881}
11882
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011883/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011884 * "finddir({fname}[, {path}[, {count}]])" function
11885 */
11886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011887f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011889 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011890}
11891
11892/*
11893 * "findfile({fname}[, {path}[, {count}]])" function
11894 */
11895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011896f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011898 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011899}
11900
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011901#ifdef FEAT_FLOAT
11902/*
11903 * "float2nr({float})" function
11904 */
11905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011906f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011907{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011908 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011909
11910 if (get_float_arg(argvars, &f) == OK)
11911 {
11912 if (f < -0x7fffffff)
11913 rettv->vval.v_number = -0x7fffffff;
11914 else if (f > 0x7fffffff)
11915 rettv->vval.v_number = 0x7fffffff;
11916 else
11917 rettv->vval.v_number = (varnumber_T)f;
11918 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011919}
11920
11921/*
11922 * "floor({float})" function
11923 */
11924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011925f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011926{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011927 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011928
11929 rettv->v_type = VAR_FLOAT;
11930 if (get_float_arg(argvars, &f) == OK)
11931 rettv->vval.v_float = floor(f);
11932 else
11933 rettv->vval.v_float = 0.0;
11934}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011935
11936/*
11937 * "fmod()" function
11938 */
11939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011940f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011941{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011942 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011943
11944 rettv->v_type = VAR_FLOAT;
11945 if (get_float_arg(argvars, &fx) == OK
11946 && get_float_arg(&argvars[1], &fy) == OK)
11947 rettv->vval.v_float = fmod(fx, fy);
11948 else
11949 rettv->vval.v_float = 0.0;
11950}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011951#endif
11952
Bram Moolenaar0d660222005-01-07 21:51:51 +000011953/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011954 * "fnameescape({string})" function
11955 */
11956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011957f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011958{
11959 rettv->vval.v_string = vim_strsave_fnameescape(
11960 get_tv_string(&argvars[0]), FALSE);
11961 rettv->v_type = VAR_STRING;
11962}
11963
11964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 * "fnamemodify({fname}, {mods})" function
11966 */
11967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011968f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
11970 char_u *fname;
11971 char_u *mods;
11972 int usedlen = 0;
11973 int len;
11974 char_u *fbuf = NULL;
11975 char_u buf[NUMBUFLEN];
11976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011977 fname = get_tv_string_chk(&argvars[0]);
11978 mods = get_tv_string_buf_chk(&argvars[1], buf);
11979 if (fname == NULL || mods == NULL)
11980 fname = NULL;
11981 else
11982 {
11983 len = (int)STRLEN(fname);
11984 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 vim_free(fbuf);
11993}
11994
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011995static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996
11997/*
11998 * "foldclosed()" function
11999 */
12000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012001foldclosed_both(
12002 typval_T *argvars UNUSED,
12003 typval_T *rettv,
12004 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005{
12006#ifdef FEAT_FOLDING
12007 linenr_T lnum;
12008 linenr_T first, last;
12009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12012 {
12013 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12014 {
12015 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 return;
12020 }
12021 }
12022#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024}
12025
12026/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012027 * "foldclosed()" function
12028 */
12029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012030f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031{
12032 foldclosed_both(argvars, rettv, FALSE);
12033}
12034
12035/*
12036 * "foldclosedend()" function
12037 */
12038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012039f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012040{
12041 foldclosed_both(argvars, rettv, TRUE);
12042}
12043
12044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 * "foldlevel()" function
12046 */
12047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012048f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049{
12050#ifdef FEAT_FOLDING
12051 linenr_T lnum;
12052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057}
12058
12059/*
12060 * "foldtext()" function
12061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012063f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064{
12065#ifdef FEAT_FOLDING
12066 linenr_T lnum;
12067 char_u *s;
12068 char_u *r;
12069 int len;
12070 char *txt;
12071#endif
12072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 rettv->v_type = VAR_STRING;
12074 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012076 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12077 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12078 <= curbuf->b_ml.ml_line_count
12079 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 {
12081 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012082 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12083 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 {
12085 if (!linewhite(lnum))
12086 break;
12087 ++lnum;
12088 }
12089
12090 /* Find interesting text in this line. */
12091 s = skipwhite(ml_get(lnum));
12092 /* skip C comment-start */
12093 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012096 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012097 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012098 {
12099 s = skipwhite(ml_get(lnum + 1));
12100 if (*s == '*')
12101 s = skipwhite(s + 1);
12102 }
12103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 txt = _("+-%s%3ld lines: ");
12105 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012106 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 + 20 /* for %3ld */
12108 + STRLEN(s))); /* concatenated */
12109 if (r != NULL)
12110 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012111 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12112 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12113 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 len = (int)STRLEN(r);
12115 STRCAT(r, s);
12116 /* remove 'foldmarker' and 'commentstring' */
12117 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 }
12120 }
12121#endif
12122}
12123
12124/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012125 * "foldtextresult(lnum)" function
12126 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012128f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012129{
12130#ifdef FEAT_FOLDING
12131 linenr_T lnum;
12132 char_u *text;
12133 char_u buf[51];
12134 foldinfo_T foldinfo;
12135 int fold_count;
12136#endif
12137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 rettv->v_type = VAR_STRING;
12139 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012140#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012142 /* treat illegal types and illegal string values for {lnum} the same */
12143 if (lnum < 0)
12144 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012145 fold_count = foldedCount(curwin, lnum, &foldinfo);
12146 if (fold_count > 0)
12147 {
12148 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12149 &foldinfo, buf);
12150 if (text == buf)
12151 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012153 }
12154#endif
12155}
12156
12157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 * "foreground()" function
12159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012161f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163#ifdef FEAT_GUI
12164 if (gui.in_use)
12165 gui_mch_set_foreground();
12166#else
12167# ifdef WIN32
12168 win32_set_foreground();
12169# endif
12170#endif
12171}
12172
12173/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012174 * "function()" function
12175 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012177f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012178{
12179 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012180 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012181 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012182 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012183
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012184 if (argvars[0].v_type == VAR_FUNC)
12185 {
12186 /* function(MyFunc, [arg], dict) */
12187 s = argvars[0].vval.v_string;
12188 }
12189 else if (argvars[0].v_type == VAR_PARTIAL
12190 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012191 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012192 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012193 arg_pt = argvars[0].vval.v_partial;
12194 s = arg_pt->pt_name;
12195 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012196 else
12197 {
12198 /* function('MyFunc', [arg], dict) */
12199 s = get_tv_string(&argvars[0]);
12200 use_string = TRUE;
12201 }
12202
12203 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012204 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012205 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012206 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12207 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012208 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012209 else
12210 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012211 int dict_idx = 0;
12212 int arg_idx = 0;
12213 list_T *list = NULL;
12214
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012215 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012216 {
12217 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012218 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012219
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012220 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12221 * also be called from another script. Using trans_function_name()
12222 * would also work, but some plugins depend on the name being
12223 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012224 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012225 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12226 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012227 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012228 STRCPY(name, sid_buf);
12229 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012230 }
12231 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012232 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012233 name = vim_strsave(s);
12234
12235 if (argvars[1].v_type != VAR_UNKNOWN)
12236 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012237 if (argvars[2].v_type != VAR_UNKNOWN)
12238 {
12239 /* function(name, [args], dict) */
12240 arg_idx = 1;
12241 dict_idx = 2;
12242 }
12243 else if (argvars[1].v_type == VAR_DICT)
12244 /* function(name, dict) */
12245 dict_idx = 1;
12246 else
12247 /* function(name, [args]) */
12248 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012249 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012250 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012251 if (argvars[dict_idx].v_type != VAR_DICT)
12252 {
12253 EMSG(_("E922: expected a dict"));
12254 vim_free(name);
12255 return;
12256 }
12257 if (argvars[dict_idx].vval.v_dict == NULL)
12258 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012259 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012260 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012261 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012262 if (argvars[arg_idx].v_type != VAR_LIST)
12263 {
12264 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12265 vim_free(name);
12266 return;
12267 }
12268 list = argvars[arg_idx].vval.v_list;
12269 if (list == NULL || list->lv_len == 0)
12270 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012271 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012272 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012273 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012274 {
12275 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012276
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012277 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012278 if (pt == NULL)
12279 vim_free(name);
12280 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012281 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012282 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012283 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012284 listitem_T *li;
12285 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012286 int arg_len = 0;
12287 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012288
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012289 if (arg_pt != NULL)
12290 arg_len = arg_pt->pt_argc;
12291 if (list != NULL)
12292 lv_len = list->lv_len;
12293 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012294 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012295 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012296 if (pt->pt_argv == NULL)
12297 {
12298 vim_free(pt);
12299 vim_free(name);
12300 return;
12301 }
12302 else
12303 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012304 for (i = 0; i < arg_len; i++)
12305 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12306 if (lv_len > 0)
12307 for (li = list->lv_first; li != NULL;
12308 li = li->li_next)
12309 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012310 }
12311 }
12312
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012313 /* For "function(dict.func, [], dict)" and "func" is a partial
12314 * use "dict". That is backwards compatible. */
12315 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012316 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012317 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012318 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12319 ++pt->pt_dict->dv_refcount;
12320 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012321 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012322 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012323 /* If the dict was bound automatically the result is also
12324 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012325 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012326 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012327 if (pt->pt_dict != NULL)
12328 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012329 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012330
12331 pt->pt_refcount = 1;
12332 pt->pt_name = name;
12333 func_ref(pt->pt_name);
12334 }
12335 rettv->v_type = VAR_PARTIAL;
12336 rettv->vval.v_partial = pt;
12337 }
12338 else
12339 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012340 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012341 rettv->v_type = VAR_FUNC;
12342 rettv->vval.v_string = name;
12343 func_ref(name);
12344 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012345 }
12346}
12347
12348/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012349 * "garbagecollect()" function
12350 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012352f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012353{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012354 /* This is postponed until we are back at the toplevel, because we may be
12355 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12356 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012357
12358 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12359 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012360}
12361
12362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012363 * "get()" function
12364 */
12365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012366f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012367{
Bram Moolenaar33570922005-01-25 22:26:29 +000012368 listitem_T *li;
12369 list_T *l;
12370 dictitem_T *di;
12371 dict_T *d;
12372 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012373
Bram Moolenaare9a41262005-01-15 22:18:47 +000012374 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012375 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012376 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012378 int error = FALSE;
12379
12380 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12381 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012382 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012383 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012385 else if (argvars[0].v_type == VAR_DICT)
12386 {
12387 if ((d = argvars[0].vval.v_dict) != NULL)
12388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012389 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012390 if (di != NULL)
12391 tv = &di->di_tv;
12392 }
12393 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012394 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012395 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012396 partial_T *pt;
12397 partial_T fref_pt;
12398
12399 if (argvars[0].v_type == VAR_PARTIAL)
12400 pt = argvars[0].vval.v_partial;
12401 else
12402 {
12403 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12404 fref_pt.pt_name = argvars[0].vval.v_string;
12405 pt = &fref_pt;
12406 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012407
12408 if (pt != NULL)
12409 {
12410 char_u *what = get_tv_string(&argvars[1]);
12411
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012412 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012413 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012414 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012415 if (pt->pt_name == NULL)
12416 rettv->vval.v_string = NULL;
12417 else
12418 rettv->vval.v_string = vim_strsave(pt->pt_name);
12419 }
12420 else if (STRCMP(what, "dict") == 0)
12421 {
12422 rettv->v_type = VAR_DICT;
12423 rettv->vval.v_dict = pt->pt_dict;
12424 if (pt->pt_dict != NULL)
12425 ++pt->pt_dict->dv_refcount;
12426 }
12427 else if (STRCMP(what, "args") == 0)
12428 {
12429 rettv->v_type = VAR_LIST;
12430 if (rettv_list_alloc(rettv) == OK)
12431 {
12432 int i;
12433
12434 for (i = 0; i < pt->pt_argc; ++i)
12435 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12436 }
12437 }
12438 else
12439 EMSG2(_(e_invarg2), what);
12440 return;
12441 }
12442 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012443 else
12444 EMSG2(_(e_listdictarg), "get()");
12445
12446 if (tv == NULL)
12447 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012448 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012449 copy_tv(&argvars[2], rettv);
12450 }
12451 else
12452 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012453}
12454
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012455static 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 +000012456
12457/*
12458 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012459 * Return a range (from start to end) of lines in rettv from the specified
12460 * buffer.
12461 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012462 */
12463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012464get_buffer_lines(
12465 buf_T *buf,
12466 linenr_T start,
12467 linenr_T end,
12468 int retlist,
12469 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012470{
12471 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012472
Bram Moolenaar959a1432013-12-14 12:17:38 +010012473 rettv->v_type = VAR_STRING;
12474 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012475 if (retlist && rettv_list_alloc(rettv) == FAIL)
12476 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012477
12478 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12479 return;
12480
12481 if (!retlist)
12482 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012483 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12484 p = ml_get_buf(buf, start, FALSE);
12485 else
12486 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012487 rettv->vval.v_string = vim_strsave(p);
12488 }
12489 else
12490 {
12491 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012492 return;
12493
12494 if (start < 1)
12495 start = 1;
12496 if (end > buf->b_ml.ml_line_count)
12497 end = buf->b_ml.ml_line_count;
12498 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012499 if (list_append_string(rettv->vval.v_list,
12500 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012501 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012502 }
12503}
12504
12505/*
12506 * "getbufline()" function
12507 */
12508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012509f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012510{
12511 linenr_T lnum;
12512 linenr_T end;
12513 buf_T *buf;
12514
12515 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12516 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012517 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012518 --emsg_off;
12519
Bram Moolenaar661b1822005-07-28 22:36:45 +000012520 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012521 if (argvars[2].v_type == VAR_UNKNOWN)
12522 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012523 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012524 end = get_tv_lnum_buf(&argvars[2], buf);
12525
Bram Moolenaar342337a2005-07-21 21:11:17 +000012526 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012527}
12528
Bram Moolenaar0d660222005-01-07 21:51:51 +000012529/*
12530 * "getbufvar()" function
12531 */
12532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012533f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012534{
12535 buf_T *buf;
12536 buf_T *save_curbuf;
12537 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012538 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012539 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12542 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012543 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012544 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012546 rettv->v_type = VAR_STRING;
12547 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548
12549 if (buf != NULL && varname != NULL)
12550 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012551 /* set curbuf to be our buf, temporarily */
12552 save_curbuf = curbuf;
12553 curbuf = buf;
12554
Bram Moolenaar0d660222005-01-07 21:51:51 +000012555 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012556 {
12557 if (get_option_tv(&varname, rettv, TRUE) == OK)
12558 done = TRUE;
12559 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012560 else if (STRCMP(varname, "changedtick") == 0)
12561 {
12562 rettv->v_type = VAR_NUMBER;
12563 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012564 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012565 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566 else
12567 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012568 /* Look up the variable. */
12569 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12570 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12571 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012572 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012573 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012574 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012575 done = TRUE;
12576 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012577 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012578
12579 /* restore previous notion of curbuf */
12580 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581 }
12582
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012583 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12584 /* use the default value */
12585 copy_tv(&argvars[2], rettv);
12586
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587 --emsg_off;
12588}
12589
12590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 * "getchar()" function
12592 */
12593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012594f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595{
12596 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012597 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012599 /* Position the cursor. Needed after a message that ends in a space. */
12600 windgoto(msg_row, msg_col);
12601
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602 ++no_mapping;
12603 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012604 for (;;)
12605 {
12606 if (argvars[0].v_type == VAR_UNKNOWN)
12607 /* getchar(): blocking wait. */
12608 n = safe_vgetc();
12609 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12610 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012611 n = vpeekc_any();
12612 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012613 /* illegal argument or getchar(0) and no char avail: return zero */
12614 n = 0;
12615 else
12616 /* getchar(0) and char avail: return char */
12617 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012618
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012619 if (n == K_IGNORE)
12620 continue;
12621 break;
12622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623 --no_mapping;
12624 --allow_keys;
12625
Bram Moolenaar219b8702006-11-01 14:32:36 +000012626 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12627 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12628 vimvars[VV_MOUSE_COL].vv_nr = 0;
12629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 if (IS_SPECIAL(n) || mod_mask != 0)
12632 {
12633 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12634 int i = 0;
12635
12636 /* Turn a special key into three bytes, plus modifier. */
12637 if (mod_mask != 0)
12638 {
12639 temp[i++] = K_SPECIAL;
12640 temp[i++] = KS_MODIFIER;
12641 temp[i++] = mod_mask;
12642 }
12643 if (IS_SPECIAL(n))
12644 {
12645 temp[i++] = K_SPECIAL;
12646 temp[i++] = K_SECOND(n);
12647 temp[i++] = K_THIRD(n);
12648 }
12649#ifdef FEAT_MBYTE
12650 else if (has_mbyte)
12651 i += (*mb_char2bytes)(n, temp + i);
12652#endif
12653 else
12654 temp[i++] = n;
12655 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656 rettv->v_type = VAR_STRING;
12657 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012658
12659#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012660 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012661 {
12662 int row = mouse_row;
12663 int col = mouse_col;
12664 win_T *win;
12665 linenr_T lnum;
12666# ifdef FEAT_WINDOWS
12667 win_T *wp;
12668# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012669 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012670
12671 if (row >= 0 && col >= 0)
12672 {
12673 /* Find the window at the mouse coordinates and compute the
12674 * text position. */
12675 win = mouse_find_win(&row, &col);
12676 (void)mouse_comp_pos(win, &row, &col, &lnum);
12677# ifdef FEAT_WINDOWS
12678 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012679 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012680# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012681 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012682 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12683 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12684 }
12685 }
12686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 }
12688}
12689
12690/*
12691 * "getcharmod()" function
12692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012694f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697}
12698
12699/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012700 * "getcharsearch()" function
12701 */
12702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012703f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012704{
12705 if (rettv_dict_alloc(rettv) != FAIL)
12706 {
12707 dict_T *dict = rettv->vval.v_dict;
12708
12709 dict_add_nr_str(dict, "char", 0L, last_csearch());
12710 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12711 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12712 }
12713}
12714
12715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 * "getcmdline()" function
12717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012719f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721 rettv->v_type = VAR_STRING;
12722 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723}
12724
12725/*
12726 * "getcmdpos()" function
12727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012729f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732}
12733
12734/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012735 * "getcmdtype()" function
12736 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012738f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012739{
12740 rettv->v_type = VAR_STRING;
12741 rettv->vval.v_string = alloc(2);
12742 if (rettv->vval.v_string != NULL)
12743 {
12744 rettv->vval.v_string[0] = get_cmdline_type();
12745 rettv->vval.v_string[1] = NUL;
12746 }
12747}
12748
12749/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012750 * "getcmdwintype()" function
12751 */
12752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012753f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012754{
12755 rettv->v_type = VAR_STRING;
12756 rettv->vval.v_string = NULL;
12757#ifdef FEAT_CMDWIN
12758 rettv->vval.v_string = alloc(2);
12759 if (rettv->vval.v_string != NULL)
12760 {
12761 rettv->vval.v_string[0] = cmdwin_type;
12762 rettv->vval.v_string[1] = NUL;
12763 }
12764#endif
12765}
12766
12767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 * "getcwd()" function
12769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012771f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012773 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012774 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012777 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012778
12779 wp = find_tabwin(&argvars[0], &argvars[1]);
12780 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012782 if (wp->w_localdir != NULL)
12783 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12784 else if(globaldir != NULL)
12785 rettv->vval.v_string = vim_strsave(globaldir);
12786 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012787 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012788 cwd = alloc(MAXPATHL);
12789 if (cwd != NULL)
12790 {
12791 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12792 rettv->vval.v_string = vim_strsave(cwd);
12793 vim_free(cwd);
12794 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012795 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012796#ifdef BACKSLASH_IN_FILENAME
12797 if (rettv->vval.v_string != NULL)
12798 slash_adjust(rettv->vval.v_string);
12799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 }
12801}
12802
12803/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012804 * "getfontname()" function
12805 */
12806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012807f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->v_type = VAR_STRING;
12810 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012811#ifdef FEAT_GUI
12812 if (gui.in_use)
12813 {
12814 GuiFont font;
12815 char_u *name = NULL;
12816
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012817 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012818 {
12819 /* Get the "Normal" font. Either the name saved by
12820 * hl_set_font_name() or from the font ID. */
12821 font = gui.norm_font;
12822 name = hl_get_font_name();
12823 }
12824 else
12825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012827 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12828 return;
12829 font = gui_mch_get_font(name, FALSE);
12830 if (font == NOFONT)
12831 return; /* Invalid font name, return empty string. */
12832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012834 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012835 gui_mch_free_font(font);
12836 }
12837#endif
12838}
12839
12840/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012841 * "getfperm({fname})" function
12842 */
12843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012844f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012845{
12846 char_u *fname;
12847 struct stat st;
12848 char_u *perm = NULL;
12849 char_u flags[] = "rwx";
12850 int i;
12851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012855 if (mch_stat((char *)fname, &st) >= 0)
12856 {
12857 perm = vim_strsave((char_u *)"---------");
12858 if (perm != NULL)
12859 {
12860 for (i = 0; i < 9; i++)
12861 {
12862 if (st.st_mode & (1 << (8 - i)))
12863 perm[i] = flags[i % 3];
12864 }
12865 }
12866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012868}
12869
12870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 * "getfsize({fname})" function
12872 */
12873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012874f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875{
12876 char_u *fname;
12877 struct stat st;
12878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882
12883 if (mch_stat((char *)fname, &st) >= 0)
12884 {
12885 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012890
12891 /* non-perfect check for overflow */
12892 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12893 rettv->vval.v_number = -2;
12894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 }
12896 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898}
12899
12900/*
12901 * "getftime({fname})" function
12902 */
12903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012904f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905{
12906 char_u *fname;
12907 struct stat st;
12908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910
12911 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915}
12916
12917/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012918 * "getftype({fname})" function
12919 */
12920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012921f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012922{
12923 char_u *fname;
12924 struct stat st;
12925 char_u *type = NULL;
12926 char *t;
12927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012929
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012931 if (mch_lstat((char *)fname, &st) >= 0)
12932 {
12933#ifdef S_ISREG
12934 if (S_ISREG(st.st_mode))
12935 t = "file";
12936 else if (S_ISDIR(st.st_mode))
12937 t = "dir";
12938# ifdef S_ISLNK
12939 else if (S_ISLNK(st.st_mode))
12940 t = "link";
12941# endif
12942# ifdef S_ISBLK
12943 else if (S_ISBLK(st.st_mode))
12944 t = "bdev";
12945# endif
12946# ifdef S_ISCHR
12947 else if (S_ISCHR(st.st_mode))
12948 t = "cdev";
12949# endif
12950# ifdef S_ISFIFO
12951 else if (S_ISFIFO(st.st_mode))
12952 t = "fifo";
12953# endif
12954# ifdef S_ISSOCK
12955 else if (S_ISSOCK(st.st_mode))
12956 t = "fifo";
12957# endif
12958 else
12959 t = "other";
12960#else
12961# ifdef S_IFMT
12962 switch (st.st_mode & S_IFMT)
12963 {
12964 case S_IFREG: t = "file"; break;
12965 case S_IFDIR: t = "dir"; break;
12966# ifdef S_IFLNK
12967 case S_IFLNK: t = "link"; break;
12968# endif
12969# ifdef S_IFBLK
12970 case S_IFBLK: t = "bdev"; break;
12971# endif
12972# ifdef S_IFCHR
12973 case S_IFCHR: t = "cdev"; break;
12974# endif
12975# ifdef S_IFIFO
12976 case S_IFIFO: t = "fifo"; break;
12977# endif
12978# ifdef S_IFSOCK
12979 case S_IFSOCK: t = "socket"; break;
12980# endif
12981 default: t = "other";
12982 }
12983# else
12984 if (mch_isdir(fname))
12985 t = "dir";
12986 else
12987 t = "file";
12988# endif
12989#endif
12990 type = vim_strsave((char_u *)t);
12991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012993}
12994
12995/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012996 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012997 */
12998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012999f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013000{
13001 linenr_T lnum;
13002 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013003 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013004
13005 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013006 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013007 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013008 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013009 retlist = FALSE;
13010 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013011 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013012 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013013 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013014 retlist = TRUE;
13015 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013016
Bram Moolenaar342337a2005-07-21 21:11:17 +000013017 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013018}
13019
13020/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013021 * "getmatches()" function
13022 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013024f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013025{
13026#ifdef FEAT_SEARCH_EXTRA
13027 dict_T *dict;
13028 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013029 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013030
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013031 if (rettv_list_alloc(rettv) == OK)
13032 {
13033 while (cur != NULL)
13034 {
13035 dict = dict_alloc();
13036 if (dict == NULL)
13037 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013038 if (cur->match.regprog == NULL)
13039 {
13040 /* match added with matchaddpos() */
13041 for (i = 0; i < MAXPOSMATCH; ++i)
13042 {
13043 llpos_T *llpos;
13044 char buf[6];
13045 list_T *l;
13046
13047 llpos = &cur->pos.pos[i];
13048 if (llpos->lnum == 0)
13049 break;
13050 l = list_alloc();
13051 if (l == NULL)
13052 break;
13053 list_append_number(l, (varnumber_T)llpos->lnum);
13054 if (llpos->col > 0)
13055 {
13056 list_append_number(l, (varnumber_T)llpos->col);
13057 list_append_number(l, (varnumber_T)llpos->len);
13058 }
13059 sprintf(buf, "pos%d", i + 1);
13060 dict_add_list(dict, buf, l);
13061 }
13062 }
13063 else
13064 {
13065 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13066 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013067 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013068 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13069 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013070# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013071 if (cur->conceal_char)
13072 {
13073 char_u buf[MB_MAXBYTES + 1];
13074
13075 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13076 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13077 }
13078# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013079 list_append_dict(rettv->vval.v_list, dict);
13080 cur = cur->next;
13081 }
13082 }
13083#endif
13084}
13085
13086/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013087 * "getpid()" function
13088 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013090f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013091{
13092 rettv->vval.v_number = mch_get_pid();
13093}
13094
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013096getpos_both(
13097 typval_T *argvars,
13098 typval_T *rettv,
13099 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013100{
Bram Moolenaara5525202006-03-02 22:52:09 +000013101 pos_T *fp;
13102 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013103 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013104
13105 if (rettv_list_alloc(rettv) == OK)
13106 {
13107 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013108 if (getcurpos)
13109 fp = &curwin->w_cursor;
13110 else
13111 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013112 if (fnum != -1)
13113 list_append_number(l, (varnumber_T)fnum);
13114 else
13115 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013116 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13117 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013118 list_append_number(l, (fp != NULL)
13119 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013120 : (varnumber_T)0);
13121 list_append_number(l,
13122#ifdef FEAT_VIRTUALEDIT
13123 (fp != NULL) ? (varnumber_T)fp->coladd :
13124#endif
13125 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013126 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013127 {
13128 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013129 list_append_number(l, curwin->w_curswant == MAXCOL ?
13130 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013131 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013132 }
13133 else
13134 rettv->vval.v_number = FALSE;
13135}
13136
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013137
13138/*
13139 * "getcurpos()" function
13140 */
13141 static void
13142f_getcurpos(typval_T *argvars, typval_T *rettv)
13143{
13144 getpos_both(argvars, rettv, TRUE);
13145}
13146
13147/*
13148 * "getpos(string)" function
13149 */
13150 static void
13151f_getpos(typval_T *argvars, typval_T *rettv)
13152{
13153 getpos_both(argvars, rettv, FALSE);
13154}
13155
Bram Moolenaara5525202006-03-02 22:52:09 +000013156/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013157 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013158 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013160f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013161{
13162#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013163 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013164#endif
13165
Bram Moolenaar2641f772005-03-25 21:58:17 +000013166#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013167 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013168 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013169 wp = NULL;
13170 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13171 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013172 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013173 if (wp == NULL)
13174 return;
13175 }
13176
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013177 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013178 }
13179#endif
13180}
13181
13182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183 * "getreg()" function
13184 */
13185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013186f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187{
13188 char_u *strregname;
13189 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013190 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013191 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013192 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013194 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013196 strregname = get_tv_string_chk(&argvars[0]);
13197 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013198 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013200 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013201 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13202 return_list = get_tv_number_chk(&argvars[2], &error);
13203 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013206 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013207
13208 if (error)
13209 return;
13210
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 regname = (strregname == NULL ? '"' : *strregname);
13212 if (regname == 0)
13213 regname = '"';
13214
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013215 if (return_list)
13216 {
13217 rettv->v_type = VAR_LIST;
13218 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13219 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013220 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013221 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013222 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013223 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013224 }
13225 else
13226 {
13227 rettv->v_type = VAR_STRING;
13228 rettv->vval.v_string = get_reg_contents(regname,
13229 arg2 ? GREG_EXPR_SRC : 0);
13230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231}
13232
13233/*
13234 * "getregtype()" function
13235 */
13236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013237f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238{
13239 char_u *strregname;
13240 int regname;
13241 char_u buf[NUMBUFLEN + 2];
13242 long reglen = 0;
13243
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013244 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013245 {
13246 strregname = get_tv_string_chk(&argvars[0]);
13247 if (strregname == NULL) /* type error; errmsg already given */
13248 {
13249 rettv->v_type = VAR_STRING;
13250 rettv->vval.v_string = NULL;
13251 return;
13252 }
13253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 else
13255 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013256 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257
13258 regname = (strregname == NULL ? '"' : *strregname);
13259 if (regname == 0)
13260 regname = '"';
13261
13262 buf[0] = NUL;
13263 buf[1] = NUL;
13264 switch (get_reg_type(regname, &reglen))
13265 {
13266 case MLINE: buf[0] = 'V'; break;
13267 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268 case MBLOCK:
13269 buf[0] = Ctrl_V;
13270 sprintf((char *)buf + 1, "%ld", reglen + 1);
13271 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273 rettv->v_type = VAR_STRING;
13274 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275}
13276
13277/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013278 * "gettabvar()" function
13279 */
13280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013281f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013282{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013283 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013284 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013285 dictitem_T *v;
13286 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013287 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013288
13289 rettv->v_type = VAR_STRING;
13290 rettv->vval.v_string = NULL;
13291
13292 varname = get_tv_string_chk(&argvars[1]);
13293 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13294 if (tp != NULL && varname != NULL)
13295 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013296 /* Set tp to be our tabpage, temporarily. Also set the window to the
13297 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013298 if (switch_win(&oldcurwin, &oldtabpage,
13299 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013300 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013301 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013302 /* look up the variable */
13303 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13304 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13305 if (v != NULL)
13306 {
13307 copy_tv(&v->di_tv, rettv);
13308 done = TRUE;
13309 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013310 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013311
13312 /* restore previous notion of curwin */
13313 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013314 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013315
13316 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013317 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013318}
13319
13320/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013321 * "gettabwinvar()" function
13322 */
13323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013324f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013325{
13326 getwinvar(argvars, rettv, 1);
13327}
13328
13329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 * "getwinposx()" function
13331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013333f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013335 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336#ifdef FEAT_GUI
13337 if (gui.in_use)
13338 {
13339 int x, y;
13340
13341 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013342 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 }
13344#endif
13345}
13346
13347/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013348 * "win_findbuf()" function
13349 */
13350 static void
13351f_win_findbuf(typval_T *argvars, typval_T *rettv)
13352{
13353 if (rettv_list_alloc(rettv) != FAIL)
13354 win_findbuf(argvars, rettv->vval.v_list);
13355}
13356
13357/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013358 * "win_getid()" function
13359 */
13360 static void
13361f_win_getid(typval_T *argvars, typval_T *rettv)
13362{
13363 rettv->vval.v_number = win_getid(argvars);
13364}
13365
13366/*
13367 * "win_gotoid()" function
13368 */
13369 static void
13370f_win_gotoid(typval_T *argvars, typval_T *rettv)
13371{
13372 rettv->vval.v_number = win_gotoid(argvars);
13373}
13374
13375/*
13376 * "win_id2tabwin()" function
13377 */
13378 static void
13379f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13380{
13381 if (rettv_list_alloc(rettv) != FAIL)
13382 win_id2tabwin(argvars, rettv->vval.v_list);
13383}
13384
13385/*
13386 * "win_id2win()" function
13387 */
13388 static void
13389f_win_id2win(typval_T *argvars, typval_T *rettv)
13390{
13391 rettv->vval.v_number = win_id2win(argvars);
13392}
13393
13394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 * "getwinposy()" function
13396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013398f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013400 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401#ifdef FEAT_GUI
13402 if (gui.in_use)
13403 {
13404 int x, y;
13405
13406 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 }
13409#endif
13410}
13411
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013412/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013413 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013414 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013415 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013416find_win_by_nr(
13417 typval_T *vp,
13418 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013419{
13420#ifdef FEAT_WINDOWS
13421 win_T *wp;
13422#endif
13423 int nr;
13424
13425 nr = get_tv_number_chk(vp, NULL);
13426
13427#ifdef FEAT_WINDOWS
13428 if (nr < 0)
13429 return NULL;
13430 if (nr == 0)
13431 return curwin;
13432
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013433 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13434 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013435 if (--nr <= 0)
13436 break;
13437 return wp;
13438#else
13439 if (nr == 0 || nr == 1)
13440 return curwin;
13441 return NULL;
13442#endif
13443}
13444
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013446 * Find window specified by "wvp" in tabpage "tvp".
13447 */
13448 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013449find_tabwin(
13450 typval_T *wvp, /* VAR_UNKNOWN for current window */
13451 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013452{
13453 win_T *wp = NULL;
13454 tabpage_T *tp = NULL;
13455 long n;
13456
13457 if (wvp->v_type != VAR_UNKNOWN)
13458 {
13459 if (tvp->v_type != VAR_UNKNOWN)
13460 {
13461 n = get_tv_number(tvp);
13462 if (n >= 0)
13463 tp = find_tabpage(n);
13464 }
13465 else
13466 tp = curtab;
13467
13468 if (tp != NULL)
13469 wp = find_win_by_nr(wvp, tp);
13470 }
13471 else
13472 wp = curwin;
13473
13474 return wp;
13475}
13476
13477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 * "getwinvar()" function
13479 */
13480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013481f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013483 getwinvar(argvars, rettv, 0);
13484}
13485
13486/*
13487 * getwinvar() and gettabwinvar()
13488 */
13489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013490getwinvar(
13491 typval_T *argvars,
13492 typval_T *rettv,
13493 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013494{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013495 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013497 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013498 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013499 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013500#ifdef FEAT_WINDOWS
13501 win_T *oldcurwin;
13502 tabpage_T *oldtabpage;
13503 int need_switch_win;
13504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013506#ifdef FEAT_WINDOWS
13507 if (off == 1)
13508 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13509 else
13510 tp = curtab;
13511#endif
13512 win = find_win_by_nr(&argvars[off], tp);
13513 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013514 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013516 rettv->v_type = VAR_STRING;
13517 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518
13519 if (win != NULL && varname != NULL)
13520 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013521#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013522 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013523 * otherwise the window is not valid. Only do this when needed,
13524 * autocommands get blocked. */
13525 need_switch_win = !(tp == curtab && win == curwin);
13526 if (!need_switch_win
13527 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13528#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013529 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013530 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013531 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013532 if (get_option_tv(&varname, rettv, 1) == OK)
13533 done = TRUE;
13534 }
13535 else
13536 {
13537 /* Look up the variable. */
13538 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13539 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13540 varname, FALSE);
13541 if (v != NULL)
13542 {
13543 copy_tv(&v->di_tv, rettv);
13544 done = TRUE;
13545 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013548
Bram Moolenaarba117c22015-09-29 16:53:22 +020013549#ifdef FEAT_WINDOWS
13550 if (need_switch_win)
13551 /* restore previous notion of curwin */
13552 restore_win(oldcurwin, oldtabpage, TRUE);
13553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 }
13555
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013556 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13557 /* use the default return value */
13558 copy_tv(&argvars[off + 2], rettv);
13559
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 --emsg_off;
13561}
13562
13563/*
13564 * "glob()" function
13565 */
13566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013567f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013569 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013571 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013573 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013574 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013575 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013576 if (argvars[1].v_type != VAR_UNKNOWN)
13577 {
13578 if (get_tv_number_chk(&argvars[1], &error))
13579 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013580 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013581 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013582 if (get_tv_number_chk(&argvars[2], &error))
13583 {
13584 rettv->v_type = VAR_LIST;
13585 rettv->vval.v_list = NULL;
13586 }
13587 if (argvars[3].v_type != VAR_UNKNOWN
13588 && get_tv_number_chk(&argvars[3], &error))
13589 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013590 }
13591 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013592 if (!error)
13593 {
13594 ExpandInit(&xpc);
13595 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013596 if (p_wic)
13597 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013598 if (rettv->v_type == VAR_STRING)
13599 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013600 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013601 else if (rettv_list_alloc(rettv) != FAIL)
13602 {
13603 int i;
13604
13605 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13606 NULL, options, WILD_ALL_KEEP);
13607 for (i = 0; i < xpc.xp_numfiles; i++)
13608 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13609
13610 ExpandCleanup(&xpc);
13611 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013612 }
13613 else
13614 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615}
13616
13617/*
13618 * "globpath()" function
13619 */
13620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013621f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013623 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013626 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013627 garray_T ga;
13628 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013630 /* When the optional second argument is non-zero, don't remove matches
13631 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013633 if (argvars[2].v_type != VAR_UNKNOWN)
13634 {
13635 if (get_tv_number_chk(&argvars[2], &error))
13636 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013637 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013638 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013639 if (get_tv_number_chk(&argvars[3], &error))
13640 {
13641 rettv->v_type = VAR_LIST;
13642 rettv->vval.v_list = NULL;
13643 }
13644 if (argvars[4].v_type != VAR_UNKNOWN
13645 && get_tv_number_chk(&argvars[4], &error))
13646 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013647 }
13648 }
13649 if (file != NULL && !error)
13650 {
13651 ga_init2(&ga, (int)sizeof(char_u *), 10);
13652 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13653 if (rettv->v_type == VAR_STRING)
13654 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13655 else if (rettv_list_alloc(rettv) != FAIL)
13656 for (i = 0; i < ga.ga_len; ++i)
13657 list_append_string(rettv->vval.v_list,
13658 ((char_u **)(ga.ga_data))[i], -1);
13659 ga_clear_strings(&ga);
13660 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013661 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013662 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663}
13664
13665/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013666 * "glob2regpat()" function
13667 */
13668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013669f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013670{
13671 char_u *pat = get_tv_string_chk(&argvars[0]);
13672
13673 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013674 rettv->vval.v_string = (pat == NULL)
13675 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013676}
13677
13678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 * "has()" function
13680 */
13681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013682f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683{
13684 int i;
13685 char_u *name;
13686 int n = FALSE;
13687 static char *(has_list[]) =
13688 {
13689#ifdef AMIGA
13690 "amiga",
13691# ifdef FEAT_ARP
13692 "arp",
13693# endif
13694#endif
13695#ifdef __BEOS__
13696 "beos",
13697#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013698#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 "mac",
13700#endif
13701#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013702 "macunix", /* built with 'darwin' enabled */
13703#endif
13704#if defined(__APPLE__) && __APPLE__ == 1
13705 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707#ifdef __QNX__
13708 "qnx",
13709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710#ifdef UNIX
13711 "unix",
13712#endif
13713#ifdef VMS
13714 "vms",
13715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716#ifdef WIN32
13717 "win32",
13718#endif
13719#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13720 "win32unix",
13721#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013722#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 "win64",
13724#endif
13725#ifdef EBCDIC
13726 "ebcdic",
13727#endif
13728#ifndef CASE_INSENSITIVE_FILENAME
13729 "fname_case",
13730#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013731#ifdef HAVE_ACL
13732 "acl",
13733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734#ifdef FEAT_ARABIC
13735 "arabic",
13736#endif
13737#ifdef FEAT_AUTOCMD
13738 "autocmd",
13739#endif
13740#ifdef FEAT_BEVAL
13741 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013742# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13743 "balloon_multiline",
13744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745#endif
13746#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13747 "builtin_terms",
13748# ifdef ALL_BUILTIN_TCAPS
13749 "all_builtin_terms",
13750# endif
13751#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013752#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13753 || defined(FEAT_GUI_W32) \
13754 || defined(FEAT_GUI_MOTIF))
13755 "browsefilter",
13756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757#ifdef FEAT_BYTEOFF
13758 "byte_offset",
13759#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013760#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013761 "channel",
13762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763#ifdef FEAT_CINDENT
13764 "cindent",
13765#endif
13766#ifdef FEAT_CLIENTSERVER
13767 "clientserver",
13768#endif
13769#ifdef FEAT_CLIPBOARD
13770 "clipboard",
13771#endif
13772#ifdef FEAT_CMDL_COMPL
13773 "cmdline_compl",
13774#endif
13775#ifdef FEAT_CMDHIST
13776 "cmdline_hist",
13777#endif
13778#ifdef FEAT_COMMENTS
13779 "comments",
13780#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013781#ifdef FEAT_CONCEAL
13782 "conceal",
13783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784#ifdef FEAT_CRYPT
13785 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013786 "crypt-blowfish",
13787 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788#endif
13789#ifdef FEAT_CSCOPE
13790 "cscope",
13791#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013792#ifdef FEAT_CURSORBIND
13793 "cursorbind",
13794#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013795#ifdef CURSOR_SHAPE
13796 "cursorshape",
13797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798#ifdef DEBUG
13799 "debug",
13800#endif
13801#ifdef FEAT_CON_DIALOG
13802 "dialog_con",
13803#endif
13804#ifdef FEAT_GUI_DIALOG
13805 "dialog_gui",
13806#endif
13807#ifdef FEAT_DIFF
13808 "diff",
13809#endif
13810#ifdef FEAT_DIGRAPHS
13811 "digraphs",
13812#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013813#ifdef FEAT_DIRECTX
13814 "directx",
13815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816#ifdef FEAT_DND
13817 "dnd",
13818#endif
13819#ifdef FEAT_EMACS_TAGS
13820 "emacs_tags",
13821#endif
13822 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013823 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824#ifdef FEAT_SEARCH_EXTRA
13825 "extra_search",
13826#endif
13827#ifdef FEAT_FKMAP
13828 "farsi",
13829#endif
13830#ifdef FEAT_SEARCHPATH
13831 "file_in_path",
13832#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013833#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013834 "filterpipe",
13835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836#ifdef FEAT_FIND_ID
13837 "find_in_path",
13838#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013839#ifdef FEAT_FLOAT
13840 "float",
13841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842#ifdef FEAT_FOLDING
13843 "folding",
13844#endif
13845#ifdef FEAT_FOOTER
13846 "footer",
13847#endif
13848#if !defined(USE_SYSTEM) && defined(UNIX)
13849 "fork",
13850#endif
13851#ifdef FEAT_GETTEXT
13852 "gettext",
13853#endif
13854#ifdef FEAT_GUI
13855 "gui",
13856#endif
13857#ifdef FEAT_GUI_ATHENA
13858# ifdef FEAT_GUI_NEXTAW
13859 "gui_neXtaw",
13860# else
13861 "gui_athena",
13862# endif
13863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864#ifdef FEAT_GUI_GTK
13865 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013866# ifdef USE_GTK3
13867 "gui_gtk3",
13868# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013872#ifdef FEAT_GUI_GNOME
13873 "gui_gnome",
13874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875#ifdef FEAT_GUI_MAC
13876 "gui_mac",
13877#endif
13878#ifdef FEAT_GUI_MOTIF
13879 "gui_motif",
13880#endif
13881#ifdef FEAT_GUI_PHOTON
13882 "gui_photon",
13883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884#ifdef FEAT_GUI_W32
13885 "gui_win32",
13886#endif
13887#ifdef FEAT_HANGULIN
13888 "hangul_input",
13889#endif
13890#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13891 "iconv",
13892#endif
13893#ifdef FEAT_INS_EXPAND
13894 "insert_expand",
13895#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013896#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013897 "job",
13898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899#ifdef FEAT_JUMPLIST
13900 "jumplist",
13901#endif
13902#ifdef FEAT_KEYMAP
13903 "keymap",
13904#endif
13905#ifdef FEAT_LANGMAP
13906 "langmap",
13907#endif
13908#ifdef FEAT_LIBCALL
13909 "libcall",
13910#endif
13911#ifdef FEAT_LINEBREAK
13912 "linebreak",
13913#endif
13914#ifdef FEAT_LISP
13915 "lispindent",
13916#endif
13917#ifdef FEAT_LISTCMDS
13918 "listcmds",
13919#endif
13920#ifdef FEAT_LOCALMAP
13921 "localmap",
13922#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013923#ifdef FEAT_LUA
13924# ifndef DYNAMIC_LUA
13925 "lua",
13926# endif
13927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928#ifdef FEAT_MENU
13929 "menu",
13930#endif
13931#ifdef FEAT_SESSION
13932 "mksession",
13933#endif
13934#ifdef FEAT_MODIFY_FNAME
13935 "modify_fname",
13936#endif
13937#ifdef FEAT_MOUSE
13938 "mouse",
13939#endif
13940#ifdef FEAT_MOUSESHAPE
13941 "mouseshape",
13942#endif
13943#if defined(UNIX) || defined(VMS)
13944# ifdef FEAT_MOUSE_DEC
13945 "mouse_dec",
13946# endif
13947# ifdef FEAT_MOUSE_GPM
13948 "mouse_gpm",
13949# endif
13950# ifdef FEAT_MOUSE_JSB
13951 "mouse_jsbterm",
13952# endif
13953# ifdef FEAT_MOUSE_NET
13954 "mouse_netterm",
13955# endif
13956# ifdef FEAT_MOUSE_PTERM
13957 "mouse_pterm",
13958# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013959# ifdef FEAT_MOUSE_SGR
13960 "mouse_sgr",
13961# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013962# ifdef FEAT_SYSMOUSE
13963 "mouse_sysmouse",
13964# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013965# ifdef FEAT_MOUSE_URXVT
13966 "mouse_urxvt",
13967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968# ifdef FEAT_MOUSE_XTERM
13969 "mouse_xterm",
13970# endif
13971#endif
13972#ifdef FEAT_MBYTE
13973 "multi_byte",
13974#endif
13975#ifdef FEAT_MBYTE_IME
13976 "multi_byte_ime",
13977#endif
13978#ifdef FEAT_MULTI_LANG
13979 "multi_lang",
13980#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013981#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013982#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013983 "mzscheme",
13984#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986#ifdef FEAT_OLE
13987 "ole",
13988#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013989 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990#ifdef FEAT_PATH_EXTRA
13991 "path_extra",
13992#endif
13993#ifdef FEAT_PERL
13994#ifndef DYNAMIC_PERL
13995 "perl",
13996#endif
13997#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013998#ifdef FEAT_PERSISTENT_UNDO
13999 "persistent_undo",
14000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001#ifdef FEAT_PYTHON
14002#ifndef DYNAMIC_PYTHON
14003 "python",
14004#endif
14005#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014006#ifdef FEAT_PYTHON3
14007#ifndef DYNAMIC_PYTHON3
14008 "python3",
14009#endif
14010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011#ifdef FEAT_POSTSCRIPT
14012 "postscript",
14013#endif
14014#ifdef FEAT_PRINTER
14015 "printer",
14016#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014017#ifdef FEAT_PROFILE
14018 "profile",
14019#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014020#ifdef FEAT_RELTIME
14021 "reltime",
14022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023#ifdef FEAT_QUICKFIX
14024 "quickfix",
14025#endif
14026#ifdef FEAT_RIGHTLEFT
14027 "rightleft",
14028#endif
14029#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14030 "ruby",
14031#endif
14032#ifdef FEAT_SCROLLBIND
14033 "scrollbind",
14034#endif
14035#ifdef FEAT_CMDL_INFO
14036 "showcmd",
14037 "cmdline_info",
14038#endif
14039#ifdef FEAT_SIGNS
14040 "signs",
14041#endif
14042#ifdef FEAT_SMARTINDENT
14043 "smartindent",
14044#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014045#ifdef STARTUPTIME
14046 "startuptime",
14047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048#ifdef FEAT_STL_OPT
14049 "statusline",
14050#endif
14051#ifdef FEAT_SUN_WORKSHOP
14052 "sun_workshop",
14053#endif
14054#ifdef FEAT_NETBEANS_INTG
14055 "netbeans_intg",
14056#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014057#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014058 "spell",
14059#endif
14060#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 "syntax",
14062#endif
14063#if defined(USE_SYSTEM) || !defined(UNIX)
14064 "system",
14065#endif
14066#ifdef FEAT_TAG_BINS
14067 "tag_binary",
14068#endif
14069#ifdef FEAT_TAG_OLDSTATIC
14070 "tag_old_static",
14071#endif
14072#ifdef FEAT_TAG_ANYWHITE
14073 "tag_any_white",
14074#endif
14075#ifdef FEAT_TCL
14076# ifndef DYNAMIC_TCL
14077 "tcl",
14078# endif
14079#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014080#ifdef FEAT_TERMGUICOLORS
14081 "termguicolors",
14082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083#ifdef TERMINFO
14084 "terminfo",
14085#endif
14086#ifdef FEAT_TERMRESPONSE
14087 "termresponse",
14088#endif
14089#ifdef FEAT_TEXTOBJ
14090 "textobjects",
14091#endif
14092#ifdef HAVE_TGETENT
14093 "tgetent",
14094#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014095#ifdef FEAT_TIMERS
14096 "timers",
14097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098#ifdef FEAT_TITLE
14099 "title",
14100#endif
14101#ifdef FEAT_TOOLBAR
14102 "toolbar",
14103#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014104#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14105 "unnamedplus",
14106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107#ifdef FEAT_USR_CMDS
14108 "user-commands", /* was accidentally included in 5.4 */
14109 "user_commands",
14110#endif
14111#ifdef FEAT_VIMINFO
14112 "viminfo",
14113#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014114#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115 "vertsplit",
14116#endif
14117#ifdef FEAT_VIRTUALEDIT
14118 "virtualedit",
14119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121#ifdef FEAT_VISUALEXTRA
14122 "visualextra",
14123#endif
14124#ifdef FEAT_VREPLACE
14125 "vreplace",
14126#endif
14127#ifdef FEAT_WILDIGN
14128 "wildignore",
14129#endif
14130#ifdef FEAT_WILDMENU
14131 "wildmenu",
14132#endif
14133#ifdef FEAT_WINDOWS
14134 "windows",
14135#endif
14136#ifdef FEAT_WAK
14137 "winaltkeys",
14138#endif
14139#ifdef FEAT_WRITEBACKUP
14140 "writebackup",
14141#endif
14142#ifdef FEAT_XIM
14143 "xim",
14144#endif
14145#ifdef FEAT_XFONTSET
14146 "xfontset",
14147#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014148#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014149 "xpm",
14150 "xpm_w32", /* for backward compatibility */
14151#else
14152# if defined(HAVE_XPM)
14153 "xpm",
14154# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156#ifdef USE_XSMP
14157 "xsmp",
14158#endif
14159#ifdef USE_XSMP_INTERACT
14160 "xsmp_interact",
14161#endif
14162#ifdef FEAT_XCLIPBOARD
14163 "xterm_clipboard",
14164#endif
14165#ifdef FEAT_XTERM_SAVE
14166 "xterm_save",
14167#endif
14168#if defined(UNIX) && defined(FEAT_X11)
14169 "X11",
14170#endif
14171 NULL
14172 };
14173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014174 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 for (i = 0; has_list[i] != NULL; ++i)
14176 if (STRICMP(name, has_list[i]) == 0)
14177 {
14178 n = TRUE;
14179 break;
14180 }
14181
14182 if (n == FALSE)
14183 {
14184 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014185 {
14186 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014187 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014188 && vim_isdigit(name[6])
14189 && vim_isdigit(name[8])
14190 && vim_isdigit(name[10]))
14191 {
14192 int major = atoi((char *)name + 6);
14193 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014194
14195 /* Expect "patch-9.9.01234". */
14196 n = (major < VIM_VERSION_MAJOR
14197 || (major == VIM_VERSION_MAJOR
14198 && (minor < VIM_VERSION_MINOR
14199 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014200 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014201 }
14202 else
14203 n = has_patch(atoi((char *)name + 5));
14204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 else if (STRICMP(name, "vim_starting") == 0)
14206 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014207#ifdef FEAT_MBYTE
14208 else if (STRICMP(name, "multi_byte_encoding") == 0)
14209 n = has_mbyte;
14210#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014211#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14212 else if (STRICMP(name, "balloon_multiline") == 0)
14213 n = multiline_balloon_available();
14214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215#ifdef DYNAMIC_TCL
14216 else if (STRICMP(name, "tcl") == 0)
14217 n = tcl_enabled(FALSE);
14218#endif
14219#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14220 else if (STRICMP(name, "iconv") == 0)
14221 n = iconv_enabled(FALSE);
14222#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014223#ifdef DYNAMIC_LUA
14224 else if (STRICMP(name, "lua") == 0)
14225 n = lua_enabled(FALSE);
14226#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014227#ifdef DYNAMIC_MZSCHEME
14228 else if (STRICMP(name, "mzscheme") == 0)
14229 n = mzscheme_enabled(FALSE);
14230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231#ifdef DYNAMIC_RUBY
14232 else if (STRICMP(name, "ruby") == 0)
14233 n = ruby_enabled(FALSE);
14234#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014235#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236#ifdef DYNAMIC_PYTHON
14237 else if (STRICMP(name, "python") == 0)
14238 n = python_enabled(FALSE);
14239#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014240#endif
14241#ifdef FEAT_PYTHON3
14242#ifdef DYNAMIC_PYTHON3
14243 else if (STRICMP(name, "python3") == 0)
14244 n = python3_enabled(FALSE);
14245#endif
14246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247#ifdef DYNAMIC_PERL
14248 else if (STRICMP(name, "perl") == 0)
14249 n = perl_enabled(FALSE);
14250#endif
14251#ifdef FEAT_GUI
14252 else if (STRICMP(name, "gui_running") == 0)
14253 n = (gui.in_use || gui.starting);
14254# ifdef FEAT_GUI_W32
14255 else if (STRICMP(name, "gui_win32s") == 0)
14256 n = gui_is_win32s();
14257# endif
14258# ifdef FEAT_BROWSE
14259 else if (STRICMP(name, "browse") == 0)
14260 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14261# endif
14262#endif
14263#ifdef FEAT_SYN_HL
14264 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014265 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266#endif
14267#if defined(WIN3264)
14268 else if (STRICMP(name, "win95") == 0)
14269 n = mch_windows95();
14270#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014271#ifdef FEAT_NETBEANS_INTG
14272 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014273 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275 }
14276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014277 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278}
14279
14280/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014281 * "has_key()" function
14282 */
14283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014284f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014285{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014286 if (argvars[0].v_type != VAR_DICT)
14287 {
14288 EMSG(_(e_dictreq));
14289 return;
14290 }
14291 if (argvars[0].vval.v_dict == NULL)
14292 return;
14293
14294 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014295 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014296}
14297
14298/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014299 * "haslocaldir()" function
14300 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014302f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014303{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014304 win_T *wp = NULL;
14305
14306 wp = find_tabwin(&argvars[0], &argvars[1]);
14307 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014308}
14309
14310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311 * "hasmapto()" function
14312 */
14313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014314f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315{
14316 char_u *name;
14317 char_u *mode;
14318 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014319 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014321 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014322 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 mode = (char_u *)"nvo";
14324 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014326 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014327 if (argvars[2].v_type != VAR_UNKNOWN)
14328 abbr = get_tv_number(&argvars[2]);
14329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330
Bram Moolenaar2c932302006-03-18 21:42:09 +000014331 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014332 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014334 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335}
14336
14337/*
14338 * "histadd()" function
14339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014341f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342{
14343#ifdef FEAT_CMDHIST
14344 int histype;
14345 char_u *str;
14346 char_u buf[NUMBUFLEN];
14347#endif
14348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014349 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 if (check_restricted() || check_secure())
14351 return;
14352#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014353 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14354 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 if (histype >= 0)
14356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 if (*str != NUL)
14359 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014360 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014362 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 return;
14364 }
14365 }
14366#endif
14367}
14368
14369/*
14370 * "histdel()" function
14371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014373f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374{
14375#ifdef FEAT_CMDHIST
14376 int n;
14377 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14381 if (str == NULL)
14382 n = 0;
14383 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014386 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 else
14391 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014392 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 get_tv_string_buf(&argvars[1], buf));
14394 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395#endif
14396}
14397
14398/*
14399 * "histget()" function
14400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014402f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403{
14404#ifdef FEAT_CMDHIST
14405 int type;
14406 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014407 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14410 if (str == NULL)
14411 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 {
14414 type = get_histtype(str);
14415 if (argvars[1].v_type == VAR_UNKNOWN)
14416 idx = get_history_idx(type);
14417 else
14418 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14419 /* -1 on type error */
14420 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014423 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426}
14427
14428/*
14429 * "histnr()" function
14430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014432f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433{
14434 int i;
14435
14436#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014437 char_u *history = get_tv_string_chk(&argvars[0]);
14438
14439 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 if (i >= HIST_CMD && i < HIST_COUNT)
14441 i = get_history_idx(i);
14442 else
14443#endif
14444 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446}
14447
14448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 * "highlightID(name)" function
14450 */
14451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014452f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455}
14456
14457/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014458 * "highlight_exists()" function
14459 */
14460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014461f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462{
14463 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14464}
14465
14466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 * "hostname()" function
14468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014470f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471{
14472 char_u hostname[256];
14473
14474 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->v_type = VAR_STRING;
14476 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477}
14478
14479/*
14480 * iconv() function
14481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014483f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484{
14485#ifdef FEAT_MBYTE
14486 char_u buf1[NUMBUFLEN];
14487 char_u buf2[NUMBUFLEN];
14488 char_u *from, *to, *str;
14489 vimconv_T vimconv;
14490#endif
14491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014492 rettv->v_type = VAR_STRING;
14493 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494
14495#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 str = get_tv_string(&argvars[0]);
14497 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14498 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 vimconv.vc_type = CONV_NONE;
14500 convert_setup(&vimconv, from, to);
14501
14502 /* If the encodings are equal, no conversion needed. */
14503 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507
14508 convert_setup(&vimconv, NULL, NULL);
14509 vim_free(from);
14510 vim_free(to);
14511#endif
14512}
14513
14514/*
14515 * "indent()" function
14516 */
14517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014518f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519{
14520 linenr_T lnum;
14521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527}
14528
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014529/*
14530 * "index()" function
14531 */
14532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014533f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014534{
Bram Moolenaar33570922005-01-25 22:26:29 +000014535 list_T *l;
14536 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014537 long idx = 0;
14538 int ic = FALSE;
14539
14540 rettv->vval.v_number = -1;
14541 if (argvars[0].v_type != VAR_LIST)
14542 {
14543 EMSG(_(e_listreq));
14544 return;
14545 }
14546 l = argvars[0].vval.v_list;
14547 if (l != NULL)
14548 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014549 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014550 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 int error = FALSE;
14553
Bram Moolenaar758711c2005-02-02 23:11:38 +000014554 /* Start at specified item. Use the cached index that list_find()
14555 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014556 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014557 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014558 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 ic = get_tv_number_chk(&argvars[3], &error);
14560 if (error)
14561 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014562 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014563
Bram Moolenaar758711c2005-02-02 23:11:38 +000014564 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014565 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014566 {
14567 rettv->vval.v_number = idx;
14568 break;
14569 }
14570 }
14571}
14572
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573static int inputsecret_flag = 0;
14574
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014575static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014576
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014578 * This function is used by f_input() and f_inputdialog() functions. The third
14579 * argument to f_input() specifies the type of completion to use at the
14580 * prompt. The third argument to f_inputdialog() specifies the value to return
14581 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 */
14583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014584get_user_input(
14585 typval_T *argvars,
14586 typval_T *rettv,
14587 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014589 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 char_u *p = NULL;
14591 int c;
14592 char_u buf[NUMBUFLEN];
14593 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014595 int xp_type = EXPAND_NOTHING;
14596 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014598 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014599 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600
14601#ifdef NO_CONSOLE_INPUT
14602 /* While starting up, there is no place to enter text. */
14603 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605#endif
14606
14607 cmd_silent = FALSE; /* Want to see the prompt. */
14608 if (prompt != NULL)
14609 {
14610 /* Only the part of the message after the last NL is considered as
14611 * prompt for the command line */
14612 p = vim_strrchr(prompt, '\n');
14613 if (p == NULL)
14614 p = prompt;
14615 else
14616 {
14617 ++p;
14618 c = *p;
14619 *p = NUL;
14620 msg_start();
14621 msg_clr_eos();
14622 msg_puts_attr(prompt, echo_attr);
14623 msg_didout = FALSE;
14624 msg_starthere();
14625 *p = c;
14626 }
14627 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 if (argvars[1].v_type != VAR_UNKNOWN)
14630 {
14631 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14632 if (defstr != NULL)
14633 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014635 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014636 {
14637 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014638 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014639 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014640
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014641 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014642 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014643
Bram Moolenaar4463f292005-09-25 22:20:24 +000014644 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14645 if (xp_name == NULL)
14646 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014647
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014648 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014649
Bram Moolenaar4463f292005-09-25 22:20:24 +000014650 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14651 &xp_arg) == FAIL)
14652 return;
14653 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014654 }
14655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014657 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014658 int save_ex_normal_busy = ex_normal_busy;
14659 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014661 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14662 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014663 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014664 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014665 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014666 && argvars[1].v_type != VAR_UNKNOWN
14667 && argvars[2].v_type != VAR_UNKNOWN)
14668 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14669 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014670
14671 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014673 /* since the user typed this, no need to wait for return */
14674 need_wait_return = FALSE;
14675 msg_didout = FALSE;
14676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 cmd_silent = cmd_silent_save;
14678}
14679
14680/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014681 * "input()" function
14682 * Also handles inputsecret() when inputsecret is set.
14683 */
14684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014685f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014686{
14687 get_user_input(argvars, rettv, FALSE);
14688}
14689
14690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 * "inputdialog()" function
14692 */
14693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014694f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695{
14696#if defined(FEAT_GUI_TEXTDIALOG)
14697 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14698 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14699 {
14700 char_u *message;
14701 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014704 message = get_tv_string_chk(&argvars[0]);
14705 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014706 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014707 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 else
14709 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014710 if (message != NULL && defstr != NULL
14711 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014712 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014713 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714 else
14715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014716 if (message != NULL && defstr != NULL
14717 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014718 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014719 rettv->vval.v_string = vim_strsave(
14720 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014722 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 }
14726 else
14727#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014728 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729}
14730
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014731/*
14732 * "inputlist()" function
14733 */
14734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014735f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014736{
14737 listitem_T *li;
14738 int selected;
14739 int mouse_used;
14740
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014741#ifdef NO_CONSOLE_INPUT
14742 /* While starting up, there is no place to enter text. */
14743 if (no_console_input())
14744 return;
14745#endif
14746 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14747 {
14748 EMSG2(_(e_listarg), "inputlist()");
14749 return;
14750 }
14751
14752 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014753 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014754 lines_left = Rows; /* avoid more prompt */
14755 msg_scroll = TRUE;
14756 msg_clr_eos();
14757
14758 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14759 {
14760 msg_puts(get_tv_string(&li->li_tv));
14761 msg_putchar('\n');
14762 }
14763
14764 /* Ask for choice. */
14765 selected = prompt_for_number(&mouse_used);
14766 if (mouse_used)
14767 selected -= lines_left;
14768
14769 rettv->vval.v_number = selected;
14770}
14771
14772
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14774
14775/*
14776 * "inputrestore()" function
14777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014779f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780{
14781 if (ga_userinput.ga_len > 0)
14782 {
14783 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14785 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014786 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 }
14788 else if (p_verbose > 1)
14789 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014790 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792 }
14793}
14794
14795/*
14796 * "inputsave()" function
14797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014799f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014801 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802 if (ga_grow(&ga_userinput, 1) == OK)
14803 {
14804 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14805 + ga_userinput.ga_len);
14806 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014807 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808 }
14809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014810 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811}
14812
14813/*
14814 * "inputsecret()" function
14815 */
14816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014817f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818{
14819 ++cmdline_star;
14820 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014821 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822 --cmdline_star;
14823 --inputsecret_flag;
14824}
14825
14826/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827 * "insert()" function
14828 */
14829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014830f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014831{
14832 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014833 listitem_T *item;
14834 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014835 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014836
14837 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014838 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014839 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014840 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014841 {
14842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014843 before = get_tv_number_chk(&argvars[2], &error);
14844 if (error)
14845 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014846
Bram Moolenaar758711c2005-02-02 23:11:38 +000014847 if (before == l->lv_len)
14848 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014849 else
14850 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014851 item = list_find(l, before);
14852 if (item == NULL)
14853 {
14854 EMSGN(_(e_listidx), before);
14855 l = NULL;
14856 }
14857 }
14858 if (l != NULL)
14859 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014860 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014861 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014862 }
14863 }
14864}
14865
14866/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014867 * "invert(expr)" function
14868 */
14869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014870f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014871{
14872 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14873}
14874
14875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 * "isdirectory()" function
14877 */
14878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014879f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014881 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882}
14883
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014884/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014885 * "islocked()" function
14886 */
14887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014888f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014889{
14890 lval_T lv;
14891 char_u *end;
14892 dictitem_T *di;
14893
14894 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014895 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14896 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014897 if (end != NULL && lv.ll_name != NULL)
14898 {
14899 if (*end != NUL)
14900 EMSG(_(e_trailing));
14901 else
14902 {
14903 if (lv.ll_tv == NULL)
14904 {
14905 if (check_changedtick(lv.ll_name))
14906 rettv->vval.v_number = 1; /* always locked */
14907 else
14908 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014909 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014910 if (di != NULL)
14911 {
14912 /* Consider a variable locked when:
14913 * 1. the variable itself is locked
14914 * 2. the value of the variable is locked.
14915 * 3. the List or Dict value is locked.
14916 */
14917 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14918 || tv_islocked(&di->di_tv));
14919 }
14920 }
14921 }
14922 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014923 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014924 else if (lv.ll_newkey != NULL)
14925 EMSG2(_(e_dictkey), lv.ll_newkey);
14926 else if (lv.ll_list != NULL)
14927 /* List item. */
14928 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14929 else
14930 /* Dictionary item. */
14931 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14932 }
14933 }
14934
14935 clear_lval(&lv);
14936}
14937
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014938#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14939/*
14940 * "isnan()" function
14941 */
14942 static void
14943f_isnan(typval_T *argvars, typval_T *rettv)
14944{
14945 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14946 && isnan(argvars[0].vval.v_float);
14947}
14948#endif
14949
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014950static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014951
14952/*
14953 * Turn a dict into a list:
14954 * "what" == 0: list of keys
14955 * "what" == 1: list of values
14956 * "what" == 2: list of items
14957 */
14958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014959dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014960{
Bram Moolenaar33570922005-01-25 22:26:29 +000014961 list_T *l2;
14962 dictitem_T *di;
14963 hashitem_T *hi;
14964 listitem_T *li;
14965 listitem_T *li2;
14966 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014967 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014968
Bram Moolenaar8c711452005-01-14 21:53:12 +000014969 if (argvars[0].v_type != VAR_DICT)
14970 {
14971 EMSG(_(e_dictreq));
14972 return;
14973 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014974 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014975 return;
14976
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014977 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014978 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014979
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014980 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014981 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014982 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014983 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014985 --todo;
14986 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014987
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014988 li = listitem_alloc();
14989 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014990 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014991 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014992
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014993 if (what == 0)
14994 {
14995 /* keys() */
14996 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014997 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014998 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14999 }
15000 else if (what == 1)
15001 {
15002 /* values() */
15003 copy_tv(&di->di_tv, &li->li_tv);
15004 }
15005 else
15006 {
15007 /* items() */
15008 l2 = list_alloc();
15009 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015010 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015011 li->li_tv.vval.v_list = l2;
15012 if (l2 == NULL)
15013 break;
15014 ++l2->lv_refcount;
15015
15016 li2 = listitem_alloc();
15017 if (li2 == NULL)
15018 break;
15019 list_append(l2, li2);
15020 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015021 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015022 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15023
15024 li2 = listitem_alloc();
15025 if (li2 == NULL)
15026 break;
15027 list_append(l2, li2);
15028 copy_tv(&di->di_tv, &li2->li_tv);
15029 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015030 }
15031 }
15032}
15033
15034/*
15035 * "items(dict)" function
15036 */
15037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015038f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015039{
15040 dict_list(argvars, rettv, 2);
15041}
15042
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015043#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015044/*
15045 * Get the job from the argument.
15046 * Returns NULL if the job is invalid.
15047 */
15048 static job_T *
15049get_job_arg(typval_T *tv)
15050{
15051 job_T *job;
15052
15053 if (tv->v_type != VAR_JOB)
15054 {
15055 EMSG2(_(e_invarg2), get_tv_string(tv));
15056 return NULL;
15057 }
15058 job = tv->vval.v_job;
15059
15060 if (job == NULL)
15061 EMSG(_("E916: not a valid job"));
15062 return job;
15063}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015064
Bram Moolenaar835dc632016-02-07 14:27:38 +010015065/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015066 * "job_getchannel()" function
15067 */
15068 static void
15069f_job_getchannel(typval_T *argvars, typval_T *rettv)
15070{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015071 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015072
Bram Moolenaar65edff82016-02-21 16:40:11 +010015073 if (job != NULL)
15074 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015075 rettv->v_type = VAR_CHANNEL;
15076 rettv->vval.v_channel = job->jv_channel;
15077 if (job->jv_channel != NULL)
15078 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015079 }
15080}
15081
15082/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015083 * "job_info()" function
15084 */
15085 static void
15086f_job_info(typval_T *argvars, typval_T *rettv)
15087{
15088 job_T *job = get_job_arg(&argvars[0]);
15089
15090 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15091 job_info(job, rettv->vval.v_dict);
15092}
15093
15094/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015095 * "job_setoptions()" function
15096 */
15097 static void
15098f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15099{
15100 job_T *job = get_job_arg(&argvars[0]);
15101 jobopt_T opt;
15102
15103 if (job == NULL)
15104 return;
15105 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015106 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15107 job_set_options(job, &opt);
15108 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015109}
15110
15111/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015112 * "job_start()" function
15113 */
15114 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015115f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015116{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015117 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015118 if (check_restricted() || check_secure())
15119 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015120 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015121}
15122
15123/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015124 * "job_status()" function
15125 */
15126 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015127f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015128{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015129 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015130
Bram Moolenaar65edff82016-02-21 16:40:11 +010015131 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015132 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015133 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015134 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015135 }
15136}
15137
15138/*
15139 * "job_stop()" function
15140 */
15141 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015142f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015143{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015144 job_T *job = get_job_arg(&argvars[0]);
15145
15146 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015147 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015148}
15149#endif
15150
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015152 * "join()" function
15153 */
15154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015155f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015156{
15157 garray_T ga;
15158 char_u *sep;
15159
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015160 if (argvars[0].v_type != VAR_LIST)
15161 {
15162 EMSG(_(e_listreq));
15163 return;
15164 }
15165 if (argvars[0].vval.v_list == NULL)
15166 return;
15167 if (argvars[1].v_type == VAR_UNKNOWN)
15168 sep = (char_u *)" ";
15169 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015171
15172 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015173
15174 if (sep != NULL)
15175 {
15176 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015177 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015178 ga_append(&ga, NUL);
15179 rettv->vval.v_string = (char_u *)ga.ga_data;
15180 }
15181 else
15182 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015183}
15184
15185/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015186 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015187 */
15188 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015189f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015190{
15191 js_read_T reader;
15192
15193 reader.js_buf = get_tv_string(&argvars[0]);
15194 reader.js_fill = NULL;
15195 reader.js_used = 0;
15196 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15197 EMSG(_(e_invarg));
15198}
15199
15200/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015201 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015202 */
15203 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015204f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015205{
15206 rettv->v_type = VAR_STRING;
15207 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15208}
15209
15210/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015211 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015212 */
15213 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015214f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015215{
15216 js_read_T reader;
15217
15218 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015219 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015220 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015221 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015222 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015223}
15224
15225/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015226 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015227 */
15228 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015229f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015230{
15231 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015232 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015233}
15234
15235/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015236 * "keys()" function
15237 */
15238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015239f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015240{
15241 dict_list(argvars, rettv, 0);
15242}
15243
15244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015245 * "last_buffer_nr()" function.
15246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015248f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249{
15250 int n = 0;
15251 buf_T *buf;
15252
15253 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15254 if (n < buf->b_fnum)
15255 n = buf->b_fnum;
15256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015258}
15259
15260/*
15261 * "len()" function
15262 */
15263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015264f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015265{
15266 switch (argvars[0].v_type)
15267 {
15268 case VAR_STRING:
15269 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015270 rettv->vval.v_number = (varnumber_T)STRLEN(
15271 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015272 break;
15273 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015274 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015275 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015276 case VAR_DICT:
15277 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15278 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015279 case VAR_UNKNOWN:
15280 case VAR_SPECIAL:
15281 case VAR_FLOAT:
15282 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015283 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015284 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015285 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015286 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015287 break;
15288 }
15289}
15290
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015291static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015292
15293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015294libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015295{
15296#ifdef FEAT_LIBCALL
15297 char_u *string_in;
15298 char_u **string_result;
15299 int nr_result;
15300#endif
15301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015302 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015303 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015305
15306 if (check_restricted() || check_secure())
15307 return;
15308
15309#ifdef FEAT_LIBCALL
15310 /* The first two args must be strings, otherwise its meaningless */
15311 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15312 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015313 string_in = NULL;
15314 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015315 string_in = argvars[2].vval.v_string;
15316 if (type == VAR_NUMBER)
15317 string_result = NULL;
15318 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015319 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015320 if (mch_libcall(argvars[0].vval.v_string,
15321 argvars[1].vval.v_string,
15322 string_in,
15323 argvars[2].vval.v_number,
15324 string_result,
15325 &nr_result) == OK
15326 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015327 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015328 }
15329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330}
15331
15332/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015333 * "libcall()" function
15334 */
15335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015336f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015337{
15338 libcall_common(argvars, rettv, VAR_STRING);
15339}
15340
15341/*
15342 * "libcallnr()" function
15343 */
15344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015345f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015346{
15347 libcall_common(argvars, rettv, VAR_NUMBER);
15348}
15349
15350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 * "line(string)" function
15352 */
15353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015354f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355{
15356 linenr_T lnum = 0;
15357 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015358 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015360 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 if (fp != NULL)
15362 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364}
15365
15366/*
15367 * "line2byte(lnum)" function
15368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015370f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371{
15372#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374#else
15375 linenr_T lnum;
15376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015377 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015379 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15382 if (rettv->vval.v_number >= 0)
15383 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384#endif
15385}
15386
15387/*
15388 * "lispindent(lnum)" function
15389 */
15390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015391f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392{
15393#ifdef FEAT_LISP
15394 pos_T pos;
15395 linenr_T lnum;
15396
15397 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015398 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15400 {
15401 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015402 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 curwin->w_cursor = pos;
15404 }
15405 else
15406#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408}
15409
15410/*
15411 * "localtime()" function
15412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015414f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417}
15418
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015419static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420
15421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015422get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423{
15424 char_u *keys;
15425 char_u *which;
15426 char_u buf[NUMBUFLEN];
15427 char_u *keys_buf = NULL;
15428 char_u *rhs;
15429 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015430 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015431 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015432 mapblock_T *mp;
15433 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434
15435 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015436 rettv->v_type = VAR_STRING;
15437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 if (*keys == NUL)
15441 return;
15442
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015443 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015445 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015446 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015447 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015448 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015449 if (argvars[3].v_type != VAR_UNKNOWN)
15450 get_dict = get_tv_number(&argvars[3]);
15451 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 else
15454 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015455 if (which == NULL)
15456 return;
15457
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 mode = get_map_mode(&which, 0);
15459
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015460 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015461 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015463
15464 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015466 /* Return a string. */
15467 if (rhs != NULL)
15468 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469
Bram Moolenaarbd743252010-10-20 21:23:33 +020015470 }
15471 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15472 {
15473 /* Return a dictionary. */
15474 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15475 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15476 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477
Bram Moolenaarbd743252010-10-20 21:23:33 +020015478 dict_add_nr_str(dict, "lhs", 0L, lhs);
15479 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15480 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15481 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15482 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15483 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15484 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015485 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015486 dict_add_nr_str(dict, "mode", 0L, mapmode);
15487
15488 vim_free(lhs);
15489 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 }
15491}
15492
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015493#ifdef FEAT_FLOAT
15494/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015495 * "log()" function
15496 */
15497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015498f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015499{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015500 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015501
15502 rettv->v_type = VAR_FLOAT;
15503 if (get_float_arg(argvars, &f) == OK)
15504 rettv->vval.v_float = log(f);
15505 else
15506 rettv->vval.v_float = 0.0;
15507}
15508
15509/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015510 * "log10()" function
15511 */
15512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015513f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015514{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015515 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015516
15517 rettv->v_type = VAR_FLOAT;
15518 if (get_float_arg(argvars, &f) == OK)
15519 rettv->vval.v_float = log10(f);
15520 else
15521 rettv->vval.v_float = 0.0;
15522}
15523#endif
15524
Bram Moolenaar1dced572012-04-05 16:54:08 +020015525#ifdef FEAT_LUA
15526/*
15527 * "luaeval()" function
15528 */
15529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015530f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015531{
15532 char_u *str;
15533 char_u buf[NUMBUFLEN];
15534
15535 str = get_tv_string_buf(&argvars[0], buf);
15536 do_luaeval(str, argvars + 1, rettv);
15537}
15538#endif
15539
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015541 * "map()" function
15542 */
15543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015544f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015545{
15546 filter_map(argvars, rettv, TRUE);
15547}
15548
15549/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015550 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 */
15552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015553f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015555 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556}
15557
15558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015559 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 */
15561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015562f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015564 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565}
15566
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015567static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568
15569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015570find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015572 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015573 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015574 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 char_u *pat;
15576 regmatch_T regmatch;
15577 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015578 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 char_u *save_cpo;
15580 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015581 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015582 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015583 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015584 list_T *l = NULL;
15585 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015586 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015587 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588
15589 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15590 save_cpo = p_cpo;
15591 p_cpo = (char_u *)"";
15592
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015593 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015594 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015595 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015596 /* type 3: return empty list when there are no matches.
15597 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015598 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015599 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015600 if (type == 4
15601 && (list_append_string(rettv->vval.v_list,
15602 (char_u *)"", 0) == FAIL
15603 || list_append_number(rettv->vval.v_list,
15604 (varnumber_T)-1) == FAIL
15605 || list_append_number(rettv->vval.v_list,
15606 (varnumber_T)-1) == FAIL
15607 || list_append_number(rettv->vval.v_list,
15608 (varnumber_T)-1) == FAIL))
15609 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015610 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015611 rettv->vval.v_list = NULL;
15612 goto theend;
15613 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015614 }
15615 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015617 rettv->v_type = VAR_STRING;
15618 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015621 if (argvars[0].v_type == VAR_LIST)
15622 {
15623 if ((l = argvars[0].vval.v_list) == NULL)
15624 goto theend;
15625 li = l->lv_first;
15626 }
15627 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015628 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015629 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015630 len = (long)STRLEN(str);
15631 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015633 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15634 if (pat == NULL)
15635 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015636
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015637 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015639 int error = FALSE;
15640
15641 start = get_tv_number_chk(&argvars[2], &error);
15642 if (error)
15643 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015644 if (l != NULL)
15645 {
15646 li = list_find(l, start);
15647 if (li == NULL)
15648 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015649 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015650 }
15651 else
15652 {
15653 if (start < 0)
15654 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015655 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015656 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015657 /* When "count" argument is there ignore matches before "start",
15658 * otherwise skip part of the string. Differs when pattern is "^"
15659 * or "\<". */
15660 if (argvars[3].v_type != VAR_UNKNOWN)
15661 startcol = start;
15662 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015663 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015664 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015665 len -= start;
15666 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015667 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015668
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015669 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015670 nth = get_tv_number_chk(&argvars[3], &error);
15671 if (error)
15672 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 }
15674
15675 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15676 if (regmatch.regprog != NULL)
15677 {
15678 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015679
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015680 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015681 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015682 if (l != NULL)
15683 {
15684 if (li == NULL)
15685 {
15686 match = FALSE;
15687 break;
15688 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015689 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015690 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015691 if (str == NULL)
15692 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015693 }
15694
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015695 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015696
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015697 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015698 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015699 if (l == NULL && !match)
15700 break;
15701
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015702 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015703 if (l != NULL)
15704 {
15705 li = li->li_next;
15706 ++idx;
15707 }
15708 else
15709 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015710#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015711 startcol = (colnr_T)(regmatch.startp[0]
15712 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015713#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015714 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015715#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015716 if (startcol > (colnr_T)len
15717 || str + startcol <= regmatch.startp[0])
15718 {
15719 match = FALSE;
15720 break;
15721 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015722 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015723 }
15724
15725 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015727 if (type == 4)
15728 {
15729 listitem_T *li1 = rettv->vval.v_list->lv_first;
15730 listitem_T *li2 = li1->li_next;
15731 listitem_T *li3 = li2->li_next;
15732 listitem_T *li4 = li3->li_next;
15733
15734 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15735 (int)(regmatch.endp[0] - regmatch.startp[0]));
15736 li3->li_tv.vval.v_number =
15737 (varnumber_T)(regmatch.startp[0] - expr);
15738 li4->li_tv.vval.v_number =
15739 (varnumber_T)(regmatch.endp[0] - expr);
15740 if (l != NULL)
15741 li2->li_tv.vval.v_number = (varnumber_T)idx;
15742 }
15743 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015744 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015745 int i;
15746
15747 /* return list with matched string and submatches */
15748 for (i = 0; i < NSUBEXP; ++i)
15749 {
15750 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015751 {
15752 if (list_append_string(rettv->vval.v_list,
15753 (char_u *)"", 0) == FAIL)
15754 break;
15755 }
15756 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015757 regmatch.startp[i],
15758 (int)(regmatch.endp[i] - regmatch.startp[i]))
15759 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015760 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015761 }
15762 }
15763 else if (type == 2)
15764 {
15765 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015766 if (l != NULL)
15767 copy_tv(&li->li_tv, rettv);
15768 else
15769 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015771 }
15772 else if (l != NULL)
15773 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774 else
15775 {
15776 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015777 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 (varnumber_T)(regmatch.startp[0] - str);
15779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015780 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015782 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 }
15784 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015785 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 }
15787
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015788 if (type == 4 && l == NULL)
15789 /* matchstrpos() without a list: drop the second item. */
15790 listitem_remove(rettv->vval.v_list,
15791 rettv->vval.v_list->lv_first->li_next);
15792
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015794 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795 p_cpo = save_cpo;
15796}
15797
15798/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015799 * "match()" function
15800 */
15801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015802f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015803{
15804 find_some_match(argvars, rettv, 1);
15805}
15806
15807/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015808 * "matchadd()" function
15809 */
15810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015811f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015812{
15813#ifdef FEAT_SEARCH_EXTRA
15814 char_u buf[NUMBUFLEN];
15815 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15816 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15817 int prio = 10; /* default priority */
15818 int id = -1;
15819 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015820 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015821
15822 rettv->vval.v_number = -1;
15823
15824 if (grp == NULL || pat == NULL)
15825 return;
15826 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015827 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015828 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015829 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015830 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015831 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015832 if (argvars[4].v_type != VAR_UNKNOWN)
15833 {
15834 if (argvars[4].v_type != VAR_DICT)
15835 {
15836 EMSG(_(e_dictreq));
15837 return;
15838 }
15839 if (dict_find(argvars[4].vval.v_dict,
15840 (char_u *)"conceal", -1) != NULL)
15841 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15842 (char_u *)"conceal", FALSE);
15843 }
15844 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015845 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015846 if (error == TRUE)
15847 return;
15848 if (id >= 1 && id <= 3)
15849 {
15850 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15851 return;
15852 }
15853
Bram Moolenaar6561d522015-07-21 15:48:27 +020015854 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15855 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015856#endif
15857}
15858
15859/*
15860 * "matchaddpos()" function
15861 */
15862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015863f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015864{
15865#ifdef FEAT_SEARCH_EXTRA
15866 char_u buf[NUMBUFLEN];
15867 char_u *group;
15868 int prio = 10;
15869 int id = -1;
15870 int error = FALSE;
15871 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015872 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015873
15874 rettv->vval.v_number = -1;
15875
15876 group = get_tv_string_buf_chk(&argvars[0], buf);
15877 if (group == NULL)
15878 return;
15879
15880 if (argvars[1].v_type != VAR_LIST)
15881 {
15882 EMSG2(_(e_listarg), "matchaddpos()");
15883 return;
15884 }
15885 l = argvars[1].vval.v_list;
15886 if (l == NULL)
15887 return;
15888
15889 if (argvars[2].v_type != VAR_UNKNOWN)
15890 {
15891 prio = get_tv_number_chk(&argvars[2], &error);
15892 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015893 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015894 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015895 if (argvars[4].v_type != VAR_UNKNOWN)
15896 {
15897 if (argvars[4].v_type != VAR_DICT)
15898 {
15899 EMSG(_(e_dictreq));
15900 return;
15901 }
15902 if (dict_find(argvars[4].vval.v_dict,
15903 (char_u *)"conceal", -1) != NULL)
15904 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15905 (char_u *)"conceal", FALSE);
15906 }
15907 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015908 }
15909 if (error == TRUE)
15910 return;
15911
15912 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15913 if (id == 1 || id == 2)
15914 {
15915 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15916 return;
15917 }
15918
Bram Moolenaar6561d522015-07-21 15:48:27 +020015919 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15920 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015921#endif
15922}
15923
15924/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015925 * "matcharg()" function
15926 */
15927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015928f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015929{
15930 if (rettv_list_alloc(rettv) == OK)
15931 {
15932#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015933 int id = get_tv_number(&argvars[0]);
15934 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015935
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015936 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015937 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015938 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15939 {
15940 list_append_string(rettv->vval.v_list,
15941 syn_id2name(m->hlg_id), -1);
15942 list_append_string(rettv->vval.v_list, m->pattern, -1);
15943 }
15944 else
15945 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015946 list_append_string(rettv->vval.v_list, NULL, -1);
15947 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015948 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015949 }
15950#endif
15951 }
15952}
15953
15954/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015955 * "matchdelete()" function
15956 */
15957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015958f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015959{
15960#ifdef FEAT_SEARCH_EXTRA
15961 rettv->vval.v_number = match_delete(curwin,
15962 (int)get_tv_number(&argvars[0]), TRUE);
15963#endif
15964}
15965
15966/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015967 * "matchend()" function
15968 */
15969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015970f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015971{
15972 find_some_match(argvars, rettv, 0);
15973}
15974
15975/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015976 * "matchlist()" function
15977 */
15978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015979f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015980{
15981 find_some_match(argvars, rettv, 3);
15982}
15983
15984/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015985 * "matchstr()" function
15986 */
15987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015988f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015989{
15990 find_some_match(argvars, rettv, 2);
15991}
15992
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015993/*
15994 * "matchstrpos()" function
15995 */
15996 static void
15997f_matchstrpos(typval_T *argvars, typval_T *rettv)
15998{
15999 find_some_match(argvars, rettv, 4);
16000}
16001
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016002static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016003
16004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016005max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016006{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016007 long n = 0;
16008 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016009 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016010
16011 if (argvars[0].v_type == VAR_LIST)
16012 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016013 list_T *l;
16014 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016015
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016016 l = argvars[0].vval.v_list;
16017 if (l != NULL)
16018 {
16019 li = l->lv_first;
16020 if (li != NULL)
16021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016022 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016023 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016024 {
16025 li = li->li_next;
16026 if (li == NULL)
16027 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016029 if (domax ? i > n : i < n)
16030 n = i;
16031 }
16032 }
16033 }
16034 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016035 else if (argvars[0].v_type == VAR_DICT)
16036 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016037 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016038 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016039 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016040 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016041
16042 d = argvars[0].vval.v_dict;
16043 if (d != NULL)
16044 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016045 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016046 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016048 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016050 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016051 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016052 if (first)
16053 {
16054 n = i;
16055 first = FALSE;
16056 }
16057 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016058 n = i;
16059 }
16060 }
16061 }
16062 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016063 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016064 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016065 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016066}
16067
16068/*
16069 * "max()" function
16070 */
16071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016072f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016073{
16074 max_min(argvars, rettv, TRUE);
16075}
16076
16077/*
16078 * "min()" function
16079 */
16080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016081f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016082{
16083 max_min(argvars, rettv, FALSE);
16084}
16085
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016086static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016087
16088/*
16089 * Create the directory in which "dir" is located, and higher levels when
16090 * needed.
16091 */
16092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016093mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016094{
16095 char_u *p;
16096 char_u *updir;
16097 int r = FAIL;
16098
16099 /* Get end of directory name in "dir".
16100 * We're done when it's "/" or "c:/". */
16101 p = gettail_sep(dir);
16102 if (p <= get_past_head(dir))
16103 return OK;
16104
16105 /* If the directory exists we're done. Otherwise: create it.*/
16106 updir = vim_strnsave(dir, (int)(p - dir));
16107 if (updir == NULL)
16108 return FAIL;
16109 if (mch_isdir(updir))
16110 r = OK;
16111 else if (mkdir_recurse(updir, prot) == OK)
16112 r = vim_mkdir_emsg(updir, prot);
16113 vim_free(updir);
16114 return r;
16115}
16116
16117#ifdef vim_mkdir
16118/*
16119 * "mkdir()" function
16120 */
16121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016122f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016123{
16124 char_u *dir;
16125 char_u buf[NUMBUFLEN];
16126 int prot = 0755;
16127
16128 rettv->vval.v_number = FAIL;
16129 if (check_restricted() || check_secure())
16130 return;
16131
16132 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016133 if (*dir == NUL)
16134 rettv->vval.v_number = FAIL;
16135 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016136 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016137 if (*gettail(dir) == NUL)
16138 /* remove trailing slashes */
16139 *gettail_sep(dir) = NUL;
16140
16141 if (argvars[1].v_type != VAR_UNKNOWN)
16142 {
16143 if (argvars[2].v_type != VAR_UNKNOWN)
16144 prot = get_tv_number_chk(&argvars[2], NULL);
16145 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16146 mkdir_recurse(dir, prot);
16147 }
16148 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016150}
16151#endif
16152
Bram Moolenaar0d660222005-01-07 21:51:51 +000016153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 * "mode()" function
16155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016157f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016159 char_u buf[3];
16160
16161 buf[1] = NUL;
16162 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 if (VIsual_active)
16165 {
16166 if (VIsual_select)
16167 buf[0] = VIsual_mode + 's' - 'v';
16168 else
16169 buf[0] = VIsual_mode;
16170 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016171 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016172 || State == CONFIRM)
16173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016175 if (State == ASKMORE)
16176 buf[1] = 'm';
16177 else if (State == CONFIRM)
16178 buf[1] = '?';
16179 }
16180 else if (State == EXTERNCMD)
16181 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 else if (State & INSERT)
16183 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016184#ifdef FEAT_VREPLACE
16185 if (State & VREPLACE_FLAG)
16186 {
16187 buf[0] = 'R';
16188 buf[1] = 'v';
16189 }
16190 else
16191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192 if (State & REPLACE_FLAG)
16193 buf[0] = 'R';
16194 else
16195 buf[0] = 'i';
16196 }
16197 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016200 if (exmode_active)
16201 buf[1] = 'v';
16202 }
16203 else if (exmode_active)
16204 {
16205 buf[0] = 'c';
16206 buf[1] = 'e';
16207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016211 if (finish_op)
16212 buf[1] = 'o';
16213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016215 /* Clear out the minor mode when the argument is not a non-zero number or
16216 * non-empty string. */
16217 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016218 buf[1] = NUL;
16219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016220 rettv->vval.v_string = vim_strsave(buf);
16221 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222}
16223
Bram Moolenaar429fa852013-04-15 12:27:36 +020016224#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016225/*
16226 * "mzeval()" function
16227 */
16228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016229f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016230{
16231 char_u *str;
16232 char_u buf[NUMBUFLEN];
16233
16234 str = get_tv_string_buf(&argvars[0], buf);
16235 do_mzeval(str, rettv);
16236}
Bram Moolenaar75676462013-01-30 14:55:42 +010016237
16238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016239mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016240{
16241 typval_T argvars[3];
16242
16243 argvars[0].v_type = VAR_STRING;
16244 argvars[0].vval.v_string = name;
16245 copy_tv(args, &argvars[1]);
16246 argvars[2].v_type = VAR_UNKNOWN;
16247 f_call(argvars, rettv);
16248 clear_tv(&argvars[1]);
16249}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016250#endif
16251
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016253 * "nextnonblank()" function
16254 */
16255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016256f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257{
16258 linenr_T lnum;
16259
16260 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016263 {
16264 lnum = 0;
16265 break;
16266 }
16267 if (*skipwhite(ml_get(lnum)) != NUL)
16268 break;
16269 }
16270 rettv->vval.v_number = lnum;
16271}
16272
16273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274 * "nr2char()" function
16275 */
16276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016277f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278{
16279 char_u buf[NUMBUFLEN];
16280
16281#ifdef FEAT_MBYTE
16282 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016283 {
16284 int utf8 = 0;
16285
16286 if (argvars[1].v_type != VAR_UNKNOWN)
16287 utf8 = get_tv_number_chk(&argvars[1], NULL);
16288 if (utf8)
16289 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16290 else
16291 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293 else
16294#endif
16295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016296 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 buf[1] = NUL;
16298 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016299 rettv->v_type = VAR_STRING;
16300 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016301}
16302
16303/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016304 * "or(expr, expr)" function
16305 */
16306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016307f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016308{
16309 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16310 | get_tv_number_chk(&argvars[1], NULL);
16311}
16312
16313/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016314 * "pathshorten()" function
16315 */
16316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016317f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016318{
16319 char_u *p;
16320
16321 rettv->v_type = VAR_STRING;
16322 p = get_tv_string_chk(&argvars[0]);
16323 if (p == NULL)
16324 rettv->vval.v_string = NULL;
16325 else
16326 {
16327 p = vim_strsave(p);
16328 rettv->vval.v_string = p;
16329 if (p != NULL)
16330 shorten_dir(p);
16331 }
16332}
16333
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016334#ifdef FEAT_PERL
16335/*
16336 * "perleval()" function
16337 */
16338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016339f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016340{
16341 char_u *str;
16342 char_u buf[NUMBUFLEN];
16343
16344 str = get_tv_string_buf(&argvars[0], buf);
16345 do_perleval(str, rettv);
16346}
16347#endif
16348
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016349#ifdef FEAT_FLOAT
16350/*
16351 * "pow()" function
16352 */
16353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016354f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016355{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016356 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016357
16358 rettv->v_type = VAR_FLOAT;
16359 if (get_float_arg(argvars, &fx) == OK
16360 && get_float_arg(&argvars[1], &fy) == OK)
16361 rettv->vval.v_float = pow(fx, fy);
16362 else
16363 rettv->vval.v_float = 0.0;
16364}
16365#endif
16366
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016367/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016368 * "prevnonblank()" function
16369 */
16370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016371f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372{
16373 linenr_T lnum;
16374
16375 lnum = get_tv_lnum(argvars);
16376 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16377 lnum = 0;
16378 else
16379 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16380 --lnum;
16381 rettv->vval.v_number = lnum;
16382}
16383
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016384/* This dummy va_list is here because:
16385 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16386 * - locally in the function results in a "used before set" warning
16387 * - using va_start() to initialize it gives "function with fixed args" error */
16388static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016389
Bram Moolenaar8c711452005-01-14 21:53:12 +000016390/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016391 * "printf()" function
16392 */
16393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016394f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016395{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016396 char_u buf[NUMBUFLEN];
16397 int len;
16398 char_u *s;
16399 int saved_did_emsg = did_emsg;
16400 char *fmt;
16401
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016402 rettv->v_type = VAR_STRING;
16403 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016404
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016405 /* Get the required length, allocate the buffer and do it for real. */
16406 did_emsg = FALSE;
16407 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16408 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16409 if (!did_emsg)
16410 {
16411 s = alloc(len + 1);
16412 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016413 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016414 rettv->vval.v_string = s;
16415 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016416 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016417 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016418 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016419}
16420
16421/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016422 * "pumvisible()" function
16423 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016425f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016426{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016427#ifdef FEAT_INS_EXPAND
16428 if (pum_visible())
16429 rettv->vval.v_number = 1;
16430#endif
16431}
16432
Bram Moolenaardb913952012-06-29 12:54:53 +020016433#ifdef FEAT_PYTHON3
16434/*
16435 * "py3eval()" function
16436 */
16437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016438f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016439{
16440 char_u *str;
16441 char_u buf[NUMBUFLEN];
16442
16443 str = get_tv_string_buf(&argvars[0], buf);
16444 do_py3eval(str, rettv);
16445}
16446#endif
16447
16448#ifdef FEAT_PYTHON
16449/*
16450 * "pyeval()" function
16451 */
16452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016453f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016454{
16455 char_u *str;
16456 char_u buf[NUMBUFLEN];
16457
16458 str = get_tv_string_buf(&argvars[0], buf);
16459 do_pyeval(str, rettv);
16460}
16461#endif
16462
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016463/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016464 * "range()" function
16465 */
16466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016467f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016468{
16469 long start;
16470 long end;
16471 long stride = 1;
16472 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016473 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016475 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016476 if (argvars[1].v_type == VAR_UNKNOWN)
16477 {
16478 end = start - 1;
16479 start = 0;
16480 }
16481 else
16482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016483 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016484 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016486 }
16487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016488 if (error)
16489 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016490 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016491 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016492 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016493 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016494 else
16495 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016496 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016497 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016498 if (list_append_number(rettv->vval.v_list,
16499 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016500 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016501 }
16502}
16503
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016504/*
16505 * "readfile()" function
16506 */
16507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016508f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016509{
16510 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016511 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016512 char_u *fname;
16513 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016514 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16515 int io_size = sizeof(buf);
16516 int readlen; /* size of last fread() */
16517 char_u *prev = NULL; /* previously read bytes, if any */
16518 long prevlen = 0; /* length of data in prev */
16519 long prevsize = 0; /* size of prev buffer */
16520 long maxline = MAXLNUM;
16521 long cnt = 0;
16522 char_u *p; /* position in buf */
16523 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016524
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016525 if (argvars[1].v_type != VAR_UNKNOWN)
16526 {
16527 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16528 binary = TRUE;
16529 if (argvars[2].v_type != VAR_UNKNOWN)
16530 maxline = get_tv_number(&argvars[2]);
16531 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016532
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016533 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016534 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016535
16536 /* Always open the file in binary mode, library functions have a mind of
16537 * their own about CR-LF conversion. */
16538 fname = get_tv_string(&argvars[0]);
16539 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16540 {
16541 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16542 return;
16543 }
16544
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016545 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016546 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016547 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016548
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016549 /* This for loop processes what was read, but is also entered at end
16550 * of file so that either:
16551 * - an incomplete line gets written
16552 * - a "binary" file gets an empty line at the end if it ends in a
16553 * newline. */
16554 for (p = buf, start = buf;
16555 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16556 ++p)
16557 {
16558 if (*p == '\n' || readlen <= 0)
16559 {
16560 listitem_T *li;
16561 char_u *s = NULL;
16562 long_u len = p - start;
16563
16564 /* Finished a line. Remove CRs before NL. */
16565 if (readlen > 0 && !binary)
16566 {
16567 while (len > 0 && start[len - 1] == '\r')
16568 --len;
16569 /* removal may cross back to the "prev" string */
16570 if (len == 0)
16571 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16572 --prevlen;
16573 }
16574 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016575 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016576 else
16577 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016578 /* Change "prev" buffer to be the right size. This way
16579 * the bytes are only copied once, and very long lines are
16580 * allocated only once. */
16581 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016582 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016583 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016584 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016585 prev = NULL; /* the list will own the string */
16586 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016587 }
16588 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016589 if (s == NULL)
16590 {
16591 do_outofmem_msg((long_u) prevlen + len + 1);
16592 failed = TRUE;
16593 break;
16594 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016595
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016596 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016597 {
16598 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016599 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016600 break;
16601 }
16602 li->li_tv.v_type = VAR_STRING;
16603 li->li_tv.v_lock = 0;
16604 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016605 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016606
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016607 start = p + 1; /* step over newline */
16608 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016609 break;
16610 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016611 else if (*p == NUL)
16612 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016613#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016614 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16615 * when finding the BF and check the previous two bytes. */
16616 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016617 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016618 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16619 * + 1, these may be in the "prev" string. */
16620 char_u back1 = p >= buf + 1 ? p[-1]
16621 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16622 char_u back2 = p >= buf + 2 ? p[-2]
16623 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16624 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016625
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016626 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016627 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016628 char_u *dest = p - 2;
16629
16630 /* Usually a BOM is at the beginning of a file, and so at
16631 * the beginning of a line; then we can just step over it.
16632 */
16633 if (start == dest)
16634 start = p + 1;
16635 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016636 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016637 /* have to shuffle buf to close gap */
16638 int adjust_prevlen = 0;
16639
16640 if (dest < buf)
16641 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016642 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016643 dest = buf;
16644 }
16645 if (readlen > p - buf + 1)
16646 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16647 readlen -= 3 - adjust_prevlen;
16648 prevlen -= adjust_prevlen;
16649 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016650 }
16651 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016652 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016653#endif
16654 } /* for */
16655
16656 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16657 break;
16658 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016659 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016660 /* There's part of a line in buf, store it in "prev". */
16661 if (p - start + prevlen >= prevsize)
16662 {
16663 /* need bigger "prev" buffer */
16664 char_u *newprev;
16665
16666 /* A common use case is ordinary text files and "prev" gets a
16667 * fragment of a line, so the first allocation is made
16668 * small, to avoid repeatedly 'allocing' large and
16669 * 'reallocing' small. */
16670 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016671 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016672 else
16673 {
16674 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016675 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016676 prevsize = grow50pc > growmin ? grow50pc : growmin;
16677 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016678 newprev = prev == NULL ? alloc(prevsize)
16679 : vim_realloc(prev, prevsize);
16680 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016681 {
16682 do_outofmem_msg((long_u)prevsize);
16683 failed = TRUE;
16684 break;
16685 }
16686 prev = newprev;
16687 }
16688 /* Add the line part to end of "prev". */
16689 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016690 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016691 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016692 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016693
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016694 /*
16695 * For a negative line count use only the lines at the end of the file,
16696 * free the rest.
16697 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016698 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016699 while (cnt > -maxline)
16700 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016701 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016702 --cnt;
16703 }
16704
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016705 if (failed)
16706 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016707 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016708 /* readfile doc says an empty list is returned on error */
16709 rettv->vval.v_list = list_alloc();
16710 }
16711
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016712 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016713 fclose(fd);
16714}
16715
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016716#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016717static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016718
16719/*
16720 * Convert a List to proftime_T.
16721 * Return FAIL when there is something wrong.
16722 */
16723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016724list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016725{
16726 long n1, n2;
16727 int error = FALSE;
16728
16729 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16730 || arg->vval.v_list->lv_len != 2)
16731 return FAIL;
16732 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16733 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16734# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016735 tm->HighPart = n1;
16736 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016737# else
16738 tm->tv_sec = n1;
16739 tm->tv_usec = n2;
16740# endif
16741 return error ? FAIL : OK;
16742}
16743#endif /* FEAT_RELTIME */
16744
16745/*
16746 * "reltime()" function
16747 */
16748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016749f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016750{
16751#ifdef FEAT_RELTIME
16752 proftime_T res;
16753 proftime_T start;
16754
16755 if (argvars[0].v_type == VAR_UNKNOWN)
16756 {
16757 /* No arguments: get current time. */
16758 profile_start(&res);
16759 }
16760 else if (argvars[1].v_type == VAR_UNKNOWN)
16761 {
16762 if (list2proftime(&argvars[0], &res) == FAIL)
16763 return;
16764 profile_end(&res);
16765 }
16766 else
16767 {
16768 /* Two arguments: compute the difference. */
16769 if (list2proftime(&argvars[0], &start) == FAIL
16770 || list2proftime(&argvars[1], &res) == FAIL)
16771 return;
16772 profile_sub(&res, &start);
16773 }
16774
16775 if (rettv_list_alloc(rettv) == OK)
16776 {
16777 long n1, n2;
16778
16779# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016780 n1 = res.HighPart;
16781 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016782# else
16783 n1 = res.tv_sec;
16784 n2 = res.tv_usec;
16785# endif
16786 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16787 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16788 }
16789#endif
16790}
16791
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016792#ifdef FEAT_FLOAT
16793/*
16794 * "reltimefloat()" function
16795 */
16796 static void
16797f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16798{
16799# ifdef FEAT_RELTIME
16800 proftime_T tm;
16801# endif
16802
16803 rettv->v_type = VAR_FLOAT;
16804 rettv->vval.v_float = 0;
16805# ifdef FEAT_RELTIME
16806 if (list2proftime(&argvars[0], &tm) == OK)
16807 rettv->vval.v_float = profile_float(&tm);
16808# endif
16809}
16810#endif
16811
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016812/*
16813 * "reltimestr()" function
16814 */
16815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016816f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016817{
16818#ifdef FEAT_RELTIME
16819 proftime_T tm;
16820#endif
16821
16822 rettv->v_type = VAR_STRING;
16823 rettv->vval.v_string = NULL;
16824#ifdef FEAT_RELTIME
16825 if (list2proftime(&argvars[0], &tm) == OK)
16826 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16827#endif
16828}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016829
Bram Moolenaar0d660222005-01-07 21:51:51 +000016830#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016831static void make_connection(void);
16832static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016833
16834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016835make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016836{
16837 if (X_DISPLAY == NULL
16838# ifdef FEAT_GUI
16839 && !gui.in_use
16840# endif
16841 )
16842 {
16843 x_force_connect = TRUE;
16844 setup_term_clip();
16845 x_force_connect = FALSE;
16846 }
16847}
16848
16849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016850check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016851{
16852 make_connection();
16853 if (X_DISPLAY == NULL)
16854 {
16855 EMSG(_("E240: No connection to Vim server"));
16856 return FAIL;
16857 }
16858 return OK;
16859}
16860#endif
16861
16862#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016864remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865{
16866 char_u *server_name;
16867 char_u *keys;
16868 char_u *r = NULL;
16869 char_u buf[NUMBUFLEN];
16870# ifdef WIN32
16871 HWND w;
16872# else
16873 Window w;
16874# endif
16875
16876 if (check_restricted() || check_secure())
16877 return;
16878
16879# ifdef FEAT_X11
16880 if (check_connection() == FAIL)
16881 return;
16882# endif
16883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016884 server_name = get_tv_string_chk(&argvars[0]);
16885 if (server_name == NULL)
16886 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887 keys = get_tv_string_buf(&argvars[1], buf);
16888# ifdef WIN32
16889 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16890# else
16891 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16892 < 0)
16893# endif
16894 {
16895 if (r != NULL)
16896 EMSG(r); /* sending worked but evaluation failed */
16897 else
16898 EMSG2(_("E241: Unable to send to %s"), server_name);
16899 return;
16900 }
16901
16902 rettv->vval.v_string = r;
16903
16904 if (argvars[2].v_type != VAR_UNKNOWN)
16905 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016907 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016908 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016910 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016911 v.di_tv.v_type = VAR_STRING;
16912 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016913 idvar = get_tv_string_chk(&argvars[2]);
16914 if (idvar != NULL)
16915 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917 }
16918}
16919#endif
16920
16921/*
16922 * "remote_expr()" function
16923 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016925f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926{
16927 rettv->v_type = VAR_STRING;
16928 rettv->vval.v_string = NULL;
16929#ifdef FEAT_CLIENTSERVER
16930 remote_common(argvars, rettv, TRUE);
16931#endif
16932}
16933
16934/*
16935 * "remote_foreground()" function
16936 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016938f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940#ifdef FEAT_CLIENTSERVER
16941# ifdef WIN32
16942 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016943 {
16944 char_u *server_name = get_tv_string_chk(&argvars[0]);
16945
16946 if (server_name != NULL)
16947 serverForeground(server_name);
16948 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949# else
16950 /* Send a foreground() expression to the server. */
16951 argvars[1].v_type = VAR_STRING;
16952 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16953 argvars[2].v_type = VAR_UNKNOWN;
16954 remote_common(argvars, rettv, TRUE);
16955 vim_free(argvars[1].vval.v_string);
16956# endif
16957#endif
16958}
16959
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016961f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962{
16963#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016964 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 char_u *s = NULL;
16966# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016967 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016969 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970
16971 if (check_restricted() || check_secure())
16972 {
16973 rettv->vval.v_number = -1;
16974 return;
16975 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016976 serverid = get_tv_string_chk(&argvars[0]);
16977 if (serverid == NULL)
16978 {
16979 rettv->vval.v_number = -1;
16980 return; /* type error; errmsg already given */
16981 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016983 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 if (n == 0)
16985 rettv->vval.v_number = -1;
16986 else
16987 {
16988 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16989 rettv->vval.v_number = (s != NULL);
16990 }
16991# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 if (check_connection() == FAIL)
16993 return;
16994
16995 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016996 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997# endif
16998
16999 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017001 char_u *retvar;
17002
Bram Moolenaar33570922005-01-25 22:26:29 +000017003 v.di_tv.v_type = VAR_STRING;
17004 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017005 retvar = get_tv_string_chk(&argvars[1]);
17006 if (retvar != NULL)
17007 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017008 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009 }
17010#else
17011 rettv->vval.v_number = -1;
17012#endif
17013}
17014
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017016f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017{
17018 char_u *r = NULL;
17019
17020#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 char_u *serverid = get_tv_string_chk(&argvars[0]);
17022
17023 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024 {
17025# ifdef WIN32
17026 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017027 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017028
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017029 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030 if (n != 0)
17031 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17032 if (r == NULL)
17033# else
17034 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017035 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017036# endif
17037 EMSG(_("E277: Unable to read a server reply"));
17038 }
17039#endif
17040 rettv->v_type = VAR_STRING;
17041 rettv->vval.v_string = r;
17042}
17043
17044/*
17045 * "remote_send()" function
17046 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017048f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049{
17050 rettv->v_type = VAR_STRING;
17051 rettv->vval.v_string = NULL;
17052#ifdef FEAT_CLIENTSERVER
17053 remote_common(argvars, rettv, FALSE);
17054#endif
17055}
17056
17057/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017058 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017059 */
17060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017061f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017062{
Bram Moolenaar33570922005-01-25 22:26:29 +000017063 list_T *l;
17064 listitem_T *item, *item2;
17065 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017066 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017067 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017068 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017069 dict_T *d;
17070 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017071 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017072
Bram Moolenaar8c711452005-01-14 21:53:12 +000017073 if (argvars[0].v_type == VAR_DICT)
17074 {
17075 if (argvars[2].v_type != VAR_UNKNOWN)
17076 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017077 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017078 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 key = get_tv_string_chk(&argvars[1]);
17081 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017083 di = dict_find(d, key, -1);
17084 if (di == NULL)
17085 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017086 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17087 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 {
17089 *rettv = di->di_tv;
17090 init_tv(&di->di_tv);
17091 dictitem_remove(d, di);
17092 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017094 }
17095 }
17096 else if (argvars[0].v_type != VAR_LIST)
17097 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017098 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017099 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017101 int error = FALSE;
17102
17103 idx = get_tv_number_chk(&argvars[1], &error);
17104 if (error)
17105 ; /* type error: do nothing, errmsg already given */
17106 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017107 EMSGN(_(e_listidx), idx);
17108 else
17109 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017110 if (argvars[2].v_type == VAR_UNKNOWN)
17111 {
17112 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017113 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017114 *rettv = item->li_tv;
17115 vim_free(item);
17116 }
17117 else
17118 {
17119 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 end = get_tv_number_chk(&argvars[2], &error);
17121 if (error)
17122 ; /* type error: do nothing */
17123 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017124 EMSGN(_(e_listidx), end);
17125 else
17126 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017127 int cnt = 0;
17128
17129 for (li = item; li != NULL; li = li->li_next)
17130 {
17131 ++cnt;
17132 if (li == item2)
17133 break;
17134 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017135 if (li == NULL) /* didn't find "item2" after "item" */
17136 EMSG(_(e_invrange));
17137 else
17138 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017139 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017140 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017141 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017142 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017143 l->lv_first = item;
17144 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017145 item->li_prev = NULL;
17146 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017147 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017148 }
17149 }
17150 }
17151 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017152 }
17153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154}
17155
17156/*
17157 * "rename({from}, {to})" function
17158 */
17159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017160f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161{
17162 char_u buf[NUMBUFLEN];
17163
17164 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017167 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17168 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169}
17170
17171/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017172 * "repeat()" function
17173 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017175f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017176{
17177 char_u *p;
17178 int n;
17179 int slen;
17180 int len;
17181 char_u *r;
17182 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017183
17184 n = get_tv_number(&argvars[1]);
17185 if (argvars[0].v_type == VAR_LIST)
17186 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017187 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017188 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017189 if (list_extend(rettv->vval.v_list,
17190 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017191 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017192 }
17193 else
17194 {
17195 p = get_tv_string(&argvars[0]);
17196 rettv->v_type = VAR_STRING;
17197 rettv->vval.v_string = NULL;
17198
17199 slen = (int)STRLEN(p);
17200 len = slen * n;
17201 if (len <= 0)
17202 return;
17203
17204 r = alloc(len + 1);
17205 if (r != NULL)
17206 {
17207 for (i = 0; i < n; i++)
17208 mch_memmove(r + i * slen, p, (size_t)slen);
17209 r[len] = NUL;
17210 }
17211
17212 rettv->vval.v_string = r;
17213 }
17214}
17215
17216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217 * "resolve()" function
17218 */
17219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017220f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221{
17222 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017223#ifdef HAVE_READLINK
17224 char_u *buf = NULL;
17225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017227 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228#ifdef FEAT_SHORTCUT
17229 {
17230 char_u *v = NULL;
17231
17232 v = mch_resolve_shortcut(p);
17233 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017234 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017236 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 }
17238#else
17239# ifdef HAVE_READLINK
17240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 char_u *cpy;
17242 int len;
17243 char_u *remain = NULL;
17244 char_u *q;
17245 int is_relative_to_current = FALSE;
17246 int has_trailing_pathsep = FALSE;
17247 int limit = 100;
17248
17249 p = vim_strsave(p);
17250
17251 if (p[0] == '.' && (vim_ispathsep(p[1])
17252 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17253 is_relative_to_current = TRUE;
17254
17255 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017256 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017259 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261
17262 q = getnextcomp(p);
17263 if (*q != NUL)
17264 {
17265 /* Separate the first path component in "p", and keep the
17266 * remainder (beginning with the path separator). */
17267 remain = vim_strsave(q - 1);
17268 q[-1] = NUL;
17269 }
17270
Bram Moolenaard9462e32011-04-11 21:35:11 +020017271 buf = alloc(MAXPATHL + 1);
17272 if (buf == NULL)
17273 goto fail;
17274
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 for (;;)
17276 {
17277 for (;;)
17278 {
17279 len = readlink((char *)p, (char *)buf, MAXPATHL);
17280 if (len <= 0)
17281 break;
17282 buf[len] = NUL;
17283
17284 if (limit-- == 0)
17285 {
17286 vim_free(p);
17287 vim_free(remain);
17288 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017289 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 goto fail;
17291 }
17292
17293 /* Ensure that the result will have a trailing path separator
17294 * if the argument has one. */
17295 if (remain == NULL && has_trailing_pathsep)
17296 add_pathsep(buf);
17297
17298 /* Separate the first path component in the link value and
17299 * concatenate the remainders. */
17300 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17301 if (*q != NUL)
17302 {
17303 if (remain == NULL)
17304 remain = vim_strsave(q - 1);
17305 else
17306 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017307 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 if (cpy != NULL)
17309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 vim_free(remain);
17311 remain = cpy;
17312 }
17313 }
17314 q[-1] = NUL;
17315 }
17316
17317 q = gettail(p);
17318 if (q > p && *q == NUL)
17319 {
17320 /* Ignore trailing path separator. */
17321 q[-1] = NUL;
17322 q = gettail(p);
17323 }
17324 if (q > p && !mch_isFullName(buf))
17325 {
17326 /* symlink is relative to directory of argument */
17327 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17328 if (cpy != NULL)
17329 {
17330 STRCPY(cpy, p);
17331 STRCPY(gettail(cpy), buf);
17332 vim_free(p);
17333 p = cpy;
17334 }
17335 }
17336 else
17337 {
17338 vim_free(p);
17339 p = vim_strsave(buf);
17340 }
17341 }
17342
17343 if (remain == NULL)
17344 break;
17345
17346 /* Append the first path component of "remain" to "p". */
17347 q = getnextcomp(remain + 1);
17348 len = q - remain - (*q != NUL);
17349 cpy = vim_strnsave(p, STRLEN(p) + len);
17350 if (cpy != NULL)
17351 {
17352 STRNCAT(cpy, remain, len);
17353 vim_free(p);
17354 p = cpy;
17355 }
17356 /* Shorten "remain". */
17357 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017358 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 else
17360 {
17361 vim_free(remain);
17362 remain = NULL;
17363 }
17364 }
17365
17366 /* If the result is a relative path name, make it explicitly relative to
17367 * the current directory if and only if the argument had this form. */
17368 if (!vim_ispathsep(*p))
17369 {
17370 if (is_relative_to_current
17371 && *p != NUL
17372 && !(p[0] == '.'
17373 && (p[1] == NUL
17374 || vim_ispathsep(p[1])
17375 || (p[1] == '.'
17376 && (p[2] == NUL
17377 || vim_ispathsep(p[2]))))))
17378 {
17379 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017380 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 if (cpy != NULL)
17382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 vim_free(p);
17384 p = cpy;
17385 }
17386 }
17387 else if (!is_relative_to_current)
17388 {
17389 /* Strip leading "./". */
17390 q = p;
17391 while (q[0] == '.' && vim_ispathsep(q[1]))
17392 q += 2;
17393 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017394 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 }
17396 }
17397
17398 /* Ensure that the result will have no trailing path separator
17399 * if the argument had none. But keep "/" or "//". */
17400 if (!has_trailing_pathsep)
17401 {
17402 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017403 if (after_pathsep(p, q))
17404 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 }
17406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017407 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 }
17409# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411# endif
17412#endif
17413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017414 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415
17416#ifdef HAVE_READLINK
17417fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017418 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017420 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421}
17422
17423/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017424 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 */
17426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017427f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428{
Bram Moolenaar33570922005-01-25 22:26:29 +000017429 list_T *l;
17430 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431
Bram Moolenaar0d660222005-01-07 21:51:51 +000017432 if (argvars[0].v_type != VAR_LIST)
17433 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017434 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017435 && !tv_check_lock(l->lv_lock,
17436 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017437 {
17438 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017439 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017440 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017441 while (li != NULL)
17442 {
17443 ni = li->li_prev;
17444 list_append(l, li);
17445 li = ni;
17446 }
17447 rettv->vval.v_list = l;
17448 rettv->v_type = VAR_LIST;
17449 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017450 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452}
17453
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017454#define SP_NOMOVE 0x01 /* don't move cursor */
17455#define SP_REPEAT 0x02 /* repeat to find outer pair */
17456#define SP_RETCOUNT 0x04 /* return matchcount */
17457#define SP_SETPCMARK 0x08 /* set previous context mark */
17458#define SP_START 0x10 /* accept match at start position */
17459#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17460#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017461#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017462
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017463static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017464
17465/*
17466 * Get flags for a search function.
17467 * Possibly sets "p_ws".
17468 * Returns BACKWARD, FORWARD or zero (for an error).
17469 */
17470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017471get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017472{
17473 int dir = FORWARD;
17474 char_u *flags;
17475 char_u nbuf[NUMBUFLEN];
17476 int mask;
17477
17478 if (varp->v_type != VAR_UNKNOWN)
17479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017480 flags = get_tv_string_buf_chk(varp, nbuf);
17481 if (flags == NULL)
17482 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017483 while (*flags != NUL)
17484 {
17485 switch (*flags)
17486 {
17487 case 'b': dir = BACKWARD; break;
17488 case 'w': p_ws = TRUE; break;
17489 case 'W': p_ws = FALSE; break;
17490 default: mask = 0;
17491 if (flagsp != NULL)
17492 switch (*flags)
17493 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017494 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017495 case 'e': mask = SP_END; break;
17496 case 'm': mask = SP_RETCOUNT; break;
17497 case 'n': mask = SP_NOMOVE; break;
17498 case 'p': mask = SP_SUBPAT; break;
17499 case 'r': mask = SP_REPEAT; break;
17500 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017501 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017502 }
17503 if (mask == 0)
17504 {
17505 EMSG2(_(e_invarg2), flags);
17506 dir = 0;
17507 }
17508 else
17509 *flagsp |= mask;
17510 }
17511 if (dir == 0)
17512 break;
17513 ++flags;
17514 }
17515 }
17516 return dir;
17517}
17518
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017520 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017522 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017523search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017525 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 char_u *pat;
17527 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017528 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529 int save_p_ws = p_ws;
17530 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017531 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017532 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017533 proftime_T tm;
17534#ifdef FEAT_RELTIME
17535 long time_limit = 0;
17536#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017537 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017538 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017540 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017541 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017542 if (dir == 0)
17543 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017544 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017545 if (flags & SP_START)
17546 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017547 if (flags & SP_END)
17548 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017549 if (flags & SP_COLUMN)
17550 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017551
Bram Moolenaar76929292008-01-06 19:07:36 +000017552 /* Optional arguments: line number to stop searching and timeout. */
17553 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017554 {
17555 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17556 if (lnum_stop < 0)
17557 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017558#ifdef FEAT_RELTIME
17559 if (argvars[3].v_type != VAR_UNKNOWN)
17560 {
17561 time_limit = get_tv_number_chk(&argvars[3], NULL);
17562 if (time_limit < 0)
17563 goto theend;
17564 }
17565#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017566 }
17567
Bram Moolenaar76929292008-01-06 19:07:36 +000017568#ifdef FEAT_RELTIME
17569 /* Set the time limit, if there is one. */
17570 profile_setlimit(time_limit, &tm);
17571#endif
17572
Bram Moolenaar231334e2005-07-25 20:46:57 +000017573 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017574 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017575 * Check to make sure only those flags are set.
17576 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17577 * flags cannot be set. Check for that condition also.
17578 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017579 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017580 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017582 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017583 goto theend;
17584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017586 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017587 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017588 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017589 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017591 if (flags & SP_SUBPAT)
17592 retval = subpatnum;
17593 else
17594 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017595 if (flags & SP_SETPCMARK)
17596 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017598 if (match_pos != NULL)
17599 {
17600 /* Store the match cursor position */
17601 match_pos->lnum = pos.lnum;
17602 match_pos->col = pos.col + 1;
17603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 /* "/$" will put the cursor after the end of the line, may need to
17605 * correct that here */
17606 check_cursor();
17607 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017608
17609 /* If 'n' flag is used: restore cursor position. */
17610 if (flags & SP_NOMOVE)
17611 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017612 else
17613 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017614theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017616
17617 return retval;
17618}
17619
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017620#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017621
17622/*
17623 * round() is not in C90, use ceil() or floor() instead.
17624 */
17625 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017626vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017627{
17628 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17629}
17630
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017631/*
17632 * "round({float})" function
17633 */
17634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017635f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017636{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017637 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017638
17639 rettv->v_type = VAR_FLOAT;
17640 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017641 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017642 else
17643 rettv->vval.v_float = 0.0;
17644}
17645#endif
17646
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017647/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017648 * "screenattr()" function
17649 */
17650 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017651f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017652{
17653 int row;
17654 int col;
17655 int c;
17656
17657 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17658 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17659 if (row < 0 || row >= screen_Rows
17660 || col < 0 || col >= screen_Columns)
17661 c = -1;
17662 else
17663 c = ScreenAttrs[LineOffset[row] + col];
17664 rettv->vval.v_number = c;
17665}
17666
17667/*
17668 * "screenchar()" function
17669 */
17670 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017671f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017672{
17673 int row;
17674 int col;
17675 int off;
17676 int c;
17677
17678 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17679 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17680 if (row < 0 || row >= screen_Rows
17681 || col < 0 || col >= screen_Columns)
17682 c = -1;
17683 else
17684 {
17685 off = LineOffset[row] + col;
17686#ifdef FEAT_MBYTE
17687 if (enc_utf8 && ScreenLinesUC[off] != 0)
17688 c = ScreenLinesUC[off];
17689 else
17690#endif
17691 c = ScreenLines[off];
17692 }
17693 rettv->vval.v_number = c;
17694}
17695
17696/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017697 * "screencol()" function
17698 *
17699 * First column is 1 to be consistent with virtcol().
17700 */
17701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017702f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017703{
17704 rettv->vval.v_number = screen_screencol() + 1;
17705}
17706
17707/*
17708 * "screenrow()" function
17709 */
17710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017711f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017712{
17713 rettv->vval.v_number = screen_screenrow() + 1;
17714}
17715
17716/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017717 * "search()" function
17718 */
17719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017720f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017721{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017722 int flags = 0;
17723
17724 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725}
17726
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017728 * "searchdecl()" function
17729 */
17730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017731f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017732{
17733 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017734 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017735 int error = FALSE;
17736 char_u *name;
17737
17738 rettv->vval.v_number = 1; /* default: FAIL */
17739
17740 name = get_tv_string_chk(&argvars[0]);
17741 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017742 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017743 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017744 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17745 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17746 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017747 if (!error && name != NULL)
17748 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017749 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017750}
17751
17752/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017753 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017755 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017756searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757{
17758 char_u *spat, *mpat, *epat;
17759 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 int dir;
17762 int flags = 0;
17763 char_u nbuf1[NUMBUFLEN];
17764 char_u nbuf2[NUMBUFLEN];
17765 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017766 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017767 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017768 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017771 spat = get_tv_string_chk(&argvars[0]);
17772 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17773 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17774 if (spat == NULL || mpat == NULL || epat == NULL)
17775 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 /* Handle the optional fourth argument: flags */
17778 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017779 if (dir == 0)
17780 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017781
17782 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017783 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17784 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017785 if ((flags & (SP_END | SP_SUBPAT)) != 0
17786 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017787 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017788 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017789 goto theend;
17790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017792 /* Using 'r' implies 'W', otherwise it doesn't work. */
17793 if (flags & SP_REPEAT)
17794 p_ws = FALSE;
17795
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017796 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017797 if (argvars[3].v_type == VAR_UNKNOWN
17798 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 skip = (char_u *)"";
17800 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017802 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017803 if (argvars[5].v_type != VAR_UNKNOWN)
17804 {
17805 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17806 if (lnum_stop < 0)
17807 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017808#ifdef FEAT_RELTIME
17809 if (argvars[6].v_type != VAR_UNKNOWN)
17810 {
17811 time_limit = get_tv_number_chk(&argvars[6], NULL);
17812 if (time_limit < 0)
17813 goto theend;
17814 }
17815#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017816 }
17817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017818 if (skip == NULL)
17819 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017821 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017822 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017823
17824theend:
17825 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017826
17827 return retval;
17828}
17829
17830/*
17831 * "searchpair()" function
17832 */
17833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017834f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017835{
17836 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17837}
17838
17839/*
17840 * "searchpairpos()" function
17841 */
17842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017843f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017844{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017845 pos_T match_pos;
17846 int lnum = 0;
17847 int col = 0;
17848
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017849 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017850 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017851
17852 if (searchpair_cmn(argvars, &match_pos) > 0)
17853 {
17854 lnum = match_pos.lnum;
17855 col = match_pos.col;
17856 }
17857
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017858 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17859 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017860}
17861
17862/*
17863 * Search for a start/middle/end thing.
17864 * Used by searchpair(), see its documentation for the details.
17865 * Returns 0 or -1 for no match,
17866 */
17867 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017868do_searchpair(
17869 char_u *spat, /* start pattern */
17870 char_u *mpat, /* middle pattern */
17871 char_u *epat, /* end pattern */
17872 int dir, /* BACKWARD or FORWARD */
17873 char_u *skip, /* skip expression */
17874 int flags, /* SP_SETPCMARK and other SP_ values */
17875 pos_T *match_pos,
17876 linenr_T lnum_stop, /* stop at this line if not zero */
17877 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017878{
17879 char_u *save_cpo;
17880 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17881 long retval = 0;
17882 pos_T pos;
17883 pos_T firstpos;
17884 pos_T foundpos;
17885 pos_T save_cursor;
17886 pos_T save_pos;
17887 int n;
17888 int r;
17889 int nest = 1;
17890 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017891 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017892 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017893
17894 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17895 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017896 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017897
Bram Moolenaar76929292008-01-06 19:07:36 +000017898#ifdef FEAT_RELTIME
17899 /* Set the time limit, if there is one. */
17900 profile_setlimit(time_limit, &tm);
17901#endif
17902
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017903 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17904 * start/middle/end (pat3, for the top pair). */
17905 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17906 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17907 if (pat2 == NULL || pat3 == NULL)
17908 goto theend;
17909 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17910 if (*mpat == NUL)
17911 STRCPY(pat3, pat2);
17912 else
17913 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17914 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017915 if (flags & SP_START)
17916 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017917
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 save_cursor = curwin->w_cursor;
17919 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017920 clearpos(&firstpos);
17921 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 pat = pat3;
17923 for (;;)
17924 {
17925 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017926 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17928 /* didn't find it or found the first match again: FAIL */
17929 break;
17930
17931 if (firstpos.lnum == 0)
17932 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017933 if (equalpos(pos, foundpos))
17934 {
17935 /* Found the same position again. Can happen with a pattern that
17936 * has "\zs" at the end and searching backwards. Advance one
17937 * character and try again. */
17938 if (dir == BACKWARD)
17939 decl(&pos);
17940 else
17941 incl(&pos);
17942 }
17943 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017945 /* clear the start flag to avoid getting stuck here */
17946 options &= ~SEARCH_START;
17947
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 /* If the skip pattern matches, ignore this match. */
17949 if (*skip != NUL)
17950 {
17951 save_pos = curwin->w_cursor;
17952 curwin->w_cursor = pos;
17953 r = eval_to_bool(skip, &err, NULL, FALSE);
17954 curwin->w_cursor = save_pos;
17955 if (err)
17956 {
17957 /* Evaluating {skip} caused an error, break here. */
17958 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017959 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 break;
17961 }
17962 if (r)
17963 continue;
17964 }
17965
17966 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17967 {
17968 /* Found end when searching backwards or start when searching
17969 * forward: nested pair. */
17970 ++nest;
17971 pat = pat2; /* nested, don't search for middle */
17972 }
17973 else
17974 {
17975 /* Found end when searching forward or start when searching
17976 * backward: end of (nested) pair; or found middle in outer pair. */
17977 if (--nest == 1)
17978 pat = pat3; /* outer level, search for middle */
17979 }
17980
17981 if (nest == 0)
17982 {
17983 /* Found the match: return matchcount or line number. */
17984 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017985 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017987 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017988 if (flags & SP_SETPCMARK)
17989 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 curwin->w_cursor = pos;
17991 if (!(flags & SP_REPEAT))
17992 break;
17993 nest = 1; /* search for next unmatched */
17994 }
17995 }
17996
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017997 if (match_pos != NULL)
17998 {
17999 /* Store the match cursor position */
18000 match_pos->lnum = curwin->w_cursor.lnum;
18001 match_pos->col = curwin->w_cursor.col + 1;
18002 }
18003
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018005 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 curwin->w_cursor = save_cursor;
18007
18008theend:
18009 vim_free(pat2);
18010 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018011 if (p_cpo == empty_option)
18012 p_cpo = save_cpo;
18013 else
18014 /* Darn, evaluating the {skip} expression changed the value. */
18015 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018016
18017 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018}
18019
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018020/*
18021 * "searchpos()" function
18022 */
18023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018024f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018025{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018026 pos_T match_pos;
18027 int lnum = 0;
18028 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018029 int n;
18030 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018031
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018032 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018033 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018034
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018035 n = search_cmn(argvars, &match_pos, &flags);
18036 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018037 {
18038 lnum = match_pos.lnum;
18039 col = match_pos.col;
18040 }
18041
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018042 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18043 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018044 if (flags & SP_SUBPAT)
18045 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018046}
18047
Bram Moolenaar0d660222005-01-07 21:51:51 +000018048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018049f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018051#ifdef FEAT_CLIENTSERVER
18052 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018053 char_u *server = get_tv_string_chk(&argvars[0]);
18054 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055
Bram Moolenaar0d660222005-01-07 21:51:51 +000018056 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018057 if (server == NULL || reply == NULL)
18058 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018059 if (check_restricted() || check_secure())
18060 return;
18061# ifdef FEAT_X11
18062 if (check_connection() == FAIL)
18063 return;
18064# endif
18065
18066 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018068 EMSG(_("E258: Unable to send to client"));
18069 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018071 rettv->vval.v_number = 0;
18072#else
18073 rettv->vval.v_number = -1;
18074#endif
18075}
18076
Bram Moolenaar0d660222005-01-07 21:51:51 +000018077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018078f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018079{
18080 char_u *r = NULL;
18081
18082#ifdef FEAT_CLIENTSERVER
18083# ifdef WIN32
18084 r = serverGetVimNames();
18085# else
18086 make_connection();
18087 if (X_DISPLAY != NULL)
18088 r = serverGetVimNames(X_DISPLAY);
18089# endif
18090#endif
18091 rettv->v_type = VAR_STRING;
18092 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093}
18094
18095/*
18096 * "setbufvar()" function
18097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018099f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100{
18101 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018104 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105 char_u nbuf[NUMBUFLEN];
18106
18107 if (check_restricted() || check_secure())
18108 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018109 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18110 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018111 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 varp = &argvars[2];
18113
18114 if (buf != NULL && varname != NULL && varp != NULL)
18115 {
18116 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118
18119 if (*varname == '&')
18120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018121 long numval;
18122 char_u *strval;
18123 int error = FALSE;
18124
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018126 numval = get_tv_number_chk(varp, &error);
18127 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018128 if (!error && strval != NULL)
18129 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 }
18131 else
18132 {
18133 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18134 if (bufvarname != NULL)
18135 {
18136 STRCPY(bufvarname, "b:");
18137 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018138 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 vim_free(bufvarname);
18140 }
18141 }
18142
18143 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146}
18147
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018149f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018150{
18151 dict_T *d;
18152 dictitem_T *di;
18153 char_u *csearch;
18154
18155 if (argvars[0].v_type != VAR_DICT)
18156 {
18157 EMSG(_(e_dictreq));
18158 return;
18159 }
18160
18161 if ((d = argvars[0].vval.v_dict) != NULL)
18162 {
18163 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18164 if (csearch != NULL)
18165 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018166#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018167 if (enc_utf8)
18168 {
18169 int pcc[MAX_MCO];
18170 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018171
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018172 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18173 }
18174 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018175#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018176 set_last_csearch(PTR2CHAR(csearch),
18177 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018178 }
18179
18180 di = dict_find(d, (char_u *)"forward", -1);
18181 if (di != NULL)
18182 set_csearch_direction(get_tv_number(&di->di_tv)
18183 ? FORWARD : BACKWARD);
18184
18185 di = dict_find(d, (char_u *)"until", -1);
18186 if (di != NULL)
18187 set_csearch_until(!!get_tv_number(&di->di_tv));
18188 }
18189}
18190
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191/*
18192 * "setcmdpos()" function
18193 */
18194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018195f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018197 int pos = (int)get_tv_number(&argvars[0]) - 1;
18198
18199 if (pos >= 0)
18200 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201}
18202
18203/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018204 * "setfperm({fname}, {mode})" function
18205 */
18206 static void
18207f_setfperm(typval_T *argvars, typval_T *rettv)
18208{
18209 char_u *fname;
18210 char_u modebuf[NUMBUFLEN];
18211 char_u *mode_str;
18212 int i;
18213 int mask;
18214 int mode = 0;
18215
18216 rettv->vval.v_number = 0;
18217 fname = get_tv_string_chk(&argvars[0]);
18218 if (fname == NULL)
18219 return;
18220 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18221 if (mode_str == NULL)
18222 return;
18223 if (STRLEN(mode_str) != 9)
18224 {
18225 EMSG2(_(e_invarg2), mode_str);
18226 return;
18227 }
18228
18229 mask = 1;
18230 for (i = 8; i >= 0; --i)
18231 {
18232 if (mode_str[i] != '-')
18233 mode |= mask;
18234 mask = mask << 1;
18235 }
18236 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18237}
18238
18239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 * "setline()" function
18241 */
18242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018243f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244{
18245 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018246 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018247 list_T *l = NULL;
18248 listitem_T *li = NULL;
18249 long added = 0;
18250 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018252 lnum = get_tv_lnum(&argvars[0]);
18253 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018255 l = argvars[1].vval.v_list;
18256 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018258 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018259 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018260
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018261 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018262 for (;;)
18263 {
18264 if (l != NULL)
18265 {
18266 /* list argument, get next string */
18267 if (li == NULL)
18268 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018269 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018270 li = li->li_next;
18271 }
18272
18273 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018274 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018275 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018276
18277 /* When coming here from Insert mode, sync undo, so that this can be
18278 * undone separately from what was previously inserted. */
18279 if (u_sync_once == 2)
18280 {
18281 u_sync_once = 1; /* notify that u_sync() was called */
18282 u_sync(TRUE);
18283 }
18284
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018285 if (lnum <= curbuf->b_ml.ml_line_count)
18286 {
18287 /* existing line, replace it */
18288 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18289 {
18290 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018291 if (lnum == curwin->w_cursor.lnum)
18292 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018293 rettv->vval.v_number = 0; /* OK */
18294 }
18295 }
18296 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18297 {
18298 /* lnum is one past the last line, append the line */
18299 ++added;
18300 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18301 rettv->vval.v_number = 0; /* OK */
18302 }
18303
18304 if (l == NULL) /* only one string argument */
18305 break;
18306 ++lnum;
18307 }
18308
18309 if (added > 0)
18310 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311}
18312
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018313static 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 +000018314
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018316 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018317 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018319set_qf_ll_list(
18320 win_T *wp UNUSED,
18321 typval_T *list_arg UNUSED,
18322 typval_T *action_arg UNUSED,
18323 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018324{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018325#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018326 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018327 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018328 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018329#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018330
Bram Moolenaar2641f772005-03-25 21:58:17 +000018331 rettv->vval.v_number = -1;
18332
18333#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018334 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018335 EMSG(_(e_listreq));
18336 else
18337 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018338 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018339
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018340 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018341 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018342 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018343 if (act == NULL)
18344 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018345 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018346 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018347 else
18348 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018349 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018350 else if (action_arg->v_type == VAR_UNKNOWN)
18351 action = ' ';
18352 else
18353 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018354
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018355 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018356 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018357 rettv->vval.v_number = 0;
18358 }
18359#endif
18360}
18361
18362/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018363 * "setloclist()" function
18364 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018366f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018367{
18368 win_T *win;
18369
18370 rettv->vval.v_number = -1;
18371
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018372 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018373 if (win != NULL)
18374 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18375}
18376
18377/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018378 * "setmatches()" function
18379 */
18380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018381f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018382{
18383#ifdef FEAT_SEARCH_EXTRA
18384 list_T *l;
18385 listitem_T *li;
18386 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018387 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018388
18389 rettv->vval.v_number = -1;
18390 if (argvars[0].v_type != VAR_LIST)
18391 {
18392 EMSG(_(e_listreq));
18393 return;
18394 }
18395 if ((l = argvars[0].vval.v_list) != NULL)
18396 {
18397
18398 /* To some extent make sure that we are dealing with a list from
18399 * "getmatches()". */
18400 li = l->lv_first;
18401 while (li != NULL)
18402 {
18403 if (li->li_tv.v_type != VAR_DICT
18404 || (d = li->li_tv.vval.v_dict) == NULL)
18405 {
18406 EMSG(_(e_invarg));
18407 return;
18408 }
18409 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018410 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18411 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018412 && dict_find(d, (char_u *)"priority", -1) != NULL
18413 && dict_find(d, (char_u *)"id", -1) != NULL))
18414 {
18415 EMSG(_(e_invarg));
18416 return;
18417 }
18418 li = li->li_next;
18419 }
18420
18421 clear_matches(curwin);
18422 li = l->lv_first;
18423 while (li != NULL)
18424 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018425 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018426 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018427 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018428 char_u *group;
18429 int priority;
18430 int id;
18431 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018432
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018433 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018434 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18435 {
18436 if (s == NULL)
18437 {
18438 s = list_alloc();
18439 if (s == NULL)
18440 return;
18441 }
18442
18443 /* match from matchaddpos() */
18444 for (i = 1; i < 9; i++)
18445 {
18446 sprintf((char *)buf, (char *)"pos%d", i);
18447 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18448 {
18449 if (di->di_tv.v_type != VAR_LIST)
18450 return;
18451
18452 list_append_tv(s, &di->di_tv);
18453 s->lv_refcount++;
18454 }
18455 else
18456 break;
18457 }
18458 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018459
18460 group = get_dict_string(d, (char_u *)"group", FALSE);
18461 priority = (int)get_dict_number(d, (char_u *)"priority");
18462 id = (int)get_dict_number(d, (char_u *)"id");
18463 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18464 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18465 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018466 if (i == 0)
18467 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018468 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018469 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018470 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018471 }
18472 else
18473 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018474 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018475 list_unref(s);
18476 s = NULL;
18477 }
18478
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018479 li = li->li_next;
18480 }
18481 rettv->vval.v_number = 0;
18482 }
18483#endif
18484}
18485
18486/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018487 * "setpos()" function
18488 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018490f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018491{
18492 pos_T pos;
18493 int fnum;
18494 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018495 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018496
Bram Moolenaar08250432008-02-13 11:42:46 +000018497 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018498 name = get_tv_string_chk(argvars);
18499 if (name != NULL)
18500 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018501 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018502 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018503 if (--pos.col < 0)
18504 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018505 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018506 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018507 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018508 if (fnum == curbuf->b_fnum)
18509 {
18510 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018511 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018512 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018513 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018514 curwin->w_set_curswant = FALSE;
18515 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018516 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018517 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018518 }
18519 else
18520 EMSG(_(e_invarg));
18521 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018522 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18523 {
18524 /* set mark */
18525 if (setmark_pos(name[1], &pos, fnum) == OK)
18526 rettv->vval.v_number = 0;
18527 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018528 else
18529 EMSG(_(e_invarg));
18530 }
18531 }
18532}
18533
18534/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018535 * "setqflist()" function
18536 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018538f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018539{
18540 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18541}
18542
18543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 * "setreg()" function
18545 */
18546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018547f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548{
18549 int regname;
18550 char_u *strregname;
18551 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018552 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 int append;
18554 char_u yank_type;
18555 long block_len;
18556
18557 block_len = -1;
18558 yank_type = MAUTO;
18559 append = FALSE;
18560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018561 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018562 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018564 if (strregname == NULL)
18565 return; /* type error; errmsg already given */
18566 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 if (regname == 0 || regname == '@')
18568 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018570 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018572 stropt = get_tv_string_chk(&argvars[2]);
18573 if (stropt == NULL)
18574 return; /* type error */
18575 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 switch (*stropt)
18577 {
18578 case 'a': case 'A': /* append */
18579 append = TRUE;
18580 break;
18581 case 'v': case 'c': /* character-wise selection */
18582 yank_type = MCHAR;
18583 break;
18584 case 'V': case 'l': /* line-wise selection */
18585 yank_type = MLINE;
18586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 case 'b': case Ctrl_V: /* block-wise selection */
18588 yank_type = MBLOCK;
18589 if (VIM_ISDIGIT(stropt[1]))
18590 {
18591 ++stropt;
18592 block_len = getdigits(&stropt) - 1;
18593 --stropt;
18594 }
18595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 }
18597 }
18598
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018599 if (argvars[1].v_type == VAR_LIST)
18600 {
18601 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018602 char_u **allocval;
18603 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018604 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018605 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018606 int len = argvars[1].vval.v_list->lv_len;
18607 listitem_T *li;
18608
Bram Moolenaar7d647822014-04-05 21:28:56 +020018609 /* First half: use for pointers to result lines; second half: use for
18610 * pointers to allocated copies. */
18611 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018612 if (lstval == NULL)
18613 return;
18614 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018615 allocval = lstval + len + 2;
18616 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018617
18618 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18619 li = li->li_next)
18620 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018621 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018622 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018623 goto free_lstval;
18624 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018625 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018626 /* Need to make a copy, next get_tv_string_buf_chk() will
18627 * overwrite the string. */
18628 strval = vim_strsave(buf);
18629 if (strval == NULL)
18630 goto free_lstval;
18631 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018632 }
18633 *curval++ = strval;
18634 }
18635 *curval++ = NULL;
18636
18637 write_reg_contents_lst(regname, lstval, -1,
18638 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018639free_lstval:
18640 while (curallocval > allocval)
18641 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018642 vim_free(lstval);
18643 }
18644 else
18645 {
18646 strval = get_tv_string_chk(&argvars[1]);
18647 if (strval == NULL)
18648 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018649 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653}
18654
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018655/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018656 * "settabvar()" function
18657 */
18658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018659f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018660{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018661#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018662 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018663 tabpage_T *tp;
18664#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018665 char_u *varname, *tabvarname;
18666 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018667
18668 rettv->vval.v_number = 0;
18669
18670 if (check_restricted() || check_secure())
18671 return;
18672
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018673#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018674 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018675#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018676 varname = get_tv_string_chk(&argvars[1]);
18677 varp = &argvars[2];
18678
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018679 if (varname != NULL && varp != NULL
18680#ifdef FEAT_WINDOWS
18681 && tp != NULL
18682#endif
18683 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018684 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018685#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018686 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018687 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018688#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018689
18690 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18691 if (tabvarname != NULL)
18692 {
18693 STRCPY(tabvarname, "t:");
18694 STRCPY(tabvarname + 2, varname);
18695 set_var(tabvarname, varp, TRUE);
18696 vim_free(tabvarname);
18697 }
18698
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018699#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018700 /* Restore current tabpage */
18701 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018702 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018703#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018704 }
18705}
18706
18707/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018708 * "settabwinvar()" function
18709 */
18710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018711f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018712{
18713 setwinvar(argvars, rettv, 1);
18714}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715
18716/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018717 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018720f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018722 setwinvar(argvars, rettv, 0);
18723}
18724
18725/*
18726 * "setwinvar()" and "settabwinvar()" functions
18727 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018728
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018730setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018731{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 win_T *win;
18733#ifdef FEAT_WINDOWS
18734 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018735 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018736 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737#endif
18738 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018739 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018741 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742
18743 if (check_restricted() || check_secure())
18744 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018745
18746#ifdef FEAT_WINDOWS
18747 if (off == 1)
18748 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18749 else
18750 tp = curtab;
18751#endif
18752 win = find_win_by_nr(&argvars[off], tp);
18753 varname = get_tv_string_chk(&argvars[off + 1]);
18754 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755
18756 if (win != NULL && varname != NULL && varp != NULL)
18757 {
18758#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018759 need_switch_win = !(tp == curtab && win == curwin);
18760 if (!need_switch_win
18761 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018764 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018766 long numval;
18767 char_u *strval;
18768 int error = FALSE;
18769
18770 ++varname;
18771 numval = get_tv_number_chk(varp, &error);
18772 strval = get_tv_string_buf_chk(varp, nbuf);
18773 if (!error && strval != NULL)
18774 set_option_value(varname, numval, strval, OPT_LOCAL);
18775 }
18776 else
18777 {
18778 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18779 if (winvarname != NULL)
18780 {
18781 STRCPY(winvarname, "w:");
18782 STRCPY(winvarname + 2, varname);
18783 set_var(winvarname, varp, TRUE);
18784 vim_free(winvarname);
18785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 }
18787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018789 if (need_switch_win)
18790 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791#endif
18792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793}
18794
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018795#ifdef FEAT_CRYPT
18796/*
18797 * "sha256({string})" function
18798 */
18799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018800f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018801{
18802 char_u *p;
18803
18804 p = get_tv_string(&argvars[0]);
18805 rettv->vval.v_string = vim_strsave(
18806 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18807 rettv->v_type = VAR_STRING;
18808}
18809#endif /* FEAT_CRYPT */
18810
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018812 * "shellescape({string})" function
18813 */
18814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018815f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018816{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018817 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018818 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018819 rettv->v_type = VAR_STRING;
18820}
18821
18822/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018823 * shiftwidth() function
18824 */
18825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018826f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018827{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018828 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018829}
18830
18831/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018832 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 */
18834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018835f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018837 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838
Bram Moolenaar0d660222005-01-07 21:51:51 +000018839 p = get_tv_string(&argvars[0]);
18840 rettv->vval.v_string = vim_strsave(p);
18841 simplify_filename(rettv->vval.v_string); /* simplify in place */
18842 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843}
18844
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018845#ifdef FEAT_FLOAT
18846/*
18847 * "sin()" function
18848 */
18849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018850f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018851{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018852 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018853
18854 rettv->v_type = VAR_FLOAT;
18855 if (get_float_arg(argvars, &f) == OK)
18856 rettv->vval.v_float = sin(f);
18857 else
18858 rettv->vval.v_float = 0.0;
18859}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018860
18861/*
18862 * "sinh()" function
18863 */
18864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018865f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018866{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018867 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018868
18869 rettv->v_type = VAR_FLOAT;
18870 if (get_float_arg(argvars, &f) == OK)
18871 rettv->vval.v_float = sinh(f);
18872 else
18873 rettv->vval.v_float = 0.0;
18874}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018875#endif
18876
Bram Moolenaar0d660222005-01-07 21:51:51 +000018877static int
18878#ifdef __BORLANDC__
18879 _RTLENTRYF
18880#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018881 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018882static int
18883#ifdef __BORLANDC__
18884 _RTLENTRYF
18885#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018886 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018887
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018888/* struct used in the array that's given to qsort() */
18889typedef struct
18890{
18891 listitem_T *item;
18892 int idx;
18893} sortItem_T;
18894
Bram Moolenaar0b962472016-02-22 22:51:33 +010018895/* struct storing information about current sort */
18896typedef struct
18897{
18898 int item_compare_ic;
18899 int item_compare_numeric;
18900 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018901#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018902 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018903#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018904 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018905 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018906 dict_T *item_compare_selfdict;
18907 int item_compare_func_err;
18908 int item_compare_keep_zero;
18909} sortinfo_T;
18910static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018911static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018912#define ITEM_COMPARE_FAIL 999
18913
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018915 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018917 static int
18918#ifdef __BORLANDC__
18919_RTLENTRYF
18920#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018921item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018923 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018924 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018925 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018926 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018927 int res;
18928 char_u numbuf1[NUMBUFLEN];
18929 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018931 si1 = (sortItem_T *)s1;
18932 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018933 tv1 = &si1->item->li_tv;
18934 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018935
Bram Moolenaar0b962472016-02-22 22:51:33 +010018936 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018937 {
18938 long v1 = get_tv_number(tv1);
18939 long v2 = get_tv_number(tv2);
18940
18941 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18942 }
18943
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018944#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018945 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018946 {
18947 float_T v1 = get_tv_float(tv1);
18948 float_T v2 = get_tv_float(tv2);
18949
18950 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18951 }
18952#endif
18953
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018954 /* tv2string() puts quotes around a string and allocates memory. Don't do
18955 * that for string variables. Use a single quote when comparing with a
18956 * non-string to do what the docs promise. */
18957 if (tv1->v_type == VAR_STRING)
18958 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018959 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018960 p1 = (char_u *)"'";
18961 else
18962 p1 = tv1->vval.v_string;
18963 }
18964 else
18965 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18966 if (tv2->v_type == VAR_STRING)
18967 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018968 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018969 p2 = (char_u *)"'";
18970 else
18971 p2 = tv2->vval.v_string;
18972 }
18973 else
18974 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018975 if (p1 == NULL)
18976 p1 = (char_u *)"";
18977 if (p2 == NULL)
18978 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018979 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018980 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018981 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018982 res = STRICMP(p1, p2);
18983 else
18984 res = STRCMP(p1, p2);
18985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018987 {
18988 double n1, n2;
18989 n1 = strtod((char *)p1, (char **)&p1);
18990 n2 = strtod((char *)p2, (char **)&p2);
18991 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18992 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018993
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018994 /* When the result would be zero, compare the item indexes. Makes the
18995 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018996 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018997 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018998
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999 vim_free(tofree1);
19000 vim_free(tofree2);
19001 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002}
19003
19004 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005#ifdef __BORLANDC__
19006_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019008item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019009{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019010 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019011 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019012 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019013 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019015 char_u *func_name;
19016 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019018 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019019 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019020 return 0;
19021
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019022 si1 = (sortItem_T *)s1;
19023 si2 = (sortItem_T *)s2;
19024
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019025 if (partial == NULL)
19026 func_name = sortinfo->item_compare_func;
19027 else
19028 func_name = partial->pt_name;
19029
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019030 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019031 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019032 copy_tv(&si1->item->li_tv, &argv[0]);
19033 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019034
19035 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019036 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019037 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019038 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019039 clear_tv(&argv[0]);
19040 clear_tv(&argv[1]);
19041
19042 if (res == FAIL)
19043 res = ITEM_COMPARE_FAIL;
19044 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019045 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19046 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019047 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019048 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019049
19050 /* When the result would be zero, compare the pointers themselves. Makes
19051 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019052 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019053 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019054
Bram Moolenaar0d660222005-01-07 21:51:51 +000019055 return res;
19056}
19057
19058/*
19059 * "sort({list})" function
19060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019062do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063{
Bram Moolenaar33570922005-01-25 22:26:29 +000019064 list_T *l;
19065 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019066 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019067 sortinfo_T *old_sortinfo;
19068 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019069 long len;
19070 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071
Bram Moolenaar0b962472016-02-22 22:51:33 +010019072 /* Pointer to current info struct used in compare function. Save and
19073 * restore the current one for nested calls. */
19074 old_sortinfo = sortinfo;
19075 sortinfo = &info;
19076
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019078 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 else
19080 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019082 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019083 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19084 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019085 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019086 rettv->vval.v_list = l;
19087 rettv->v_type = VAR_LIST;
19088 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089
Bram Moolenaar0d660222005-01-07 21:51:51 +000019090 len = list_len(l);
19091 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019092 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 info.item_compare_ic = FALSE;
19095 info.item_compare_numeric = FALSE;
19096 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019097#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019098 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019099#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019100 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019101 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019102 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019103 if (argvars[1].v_type != VAR_UNKNOWN)
19104 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019105 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019106 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019107 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019108 else if (argvars[1].v_type == VAR_PARTIAL)
19109 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019110 else
19111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019112 int error = FALSE;
19113
19114 i = get_tv_number_chk(&argvars[1], &error);
19115 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019117 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019118 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019119 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019120 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019121 else if (i != 0)
19122 {
19123 EMSG(_(e_invarg));
19124 goto theend;
19125 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019126 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019127 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019128 if (*info.item_compare_func == NUL)
19129 {
19130 /* empty string means default sort */
19131 info.item_compare_func = NULL;
19132 }
19133 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019134 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019135 info.item_compare_func = NULL;
19136 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019137 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019138 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019139 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019140 info.item_compare_func = NULL;
19141 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019142 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019143#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019144 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019145 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019146 info.item_compare_func = NULL;
19147 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019148 }
19149#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019150 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019151 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019152 info.item_compare_func = NULL;
19153 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019154 }
19155 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019156 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019157
19158 if (argvars[2].v_type != VAR_UNKNOWN)
19159 {
19160 /* optional third argument: {dict} */
19161 if (argvars[2].v_type != VAR_DICT)
19162 {
19163 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019164 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019165 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019166 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019167 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169
Bram Moolenaar0d660222005-01-07 21:51:51 +000019170 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019171 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019172 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019173 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174
Bram Moolenaar327aa022014-03-25 18:24:23 +010019175 i = 0;
19176 if (sort)
19177 {
19178 /* sort(): ptrs will be the list to sort */
19179 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019180 {
19181 ptrs[i].item = li;
19182 ptrs[i].idx = i;
19183 ++i;
19184 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019185
Bram Moolenaar0b962472016-02-22 22:51:33 +010019186 info.item_compare_func_err = FALSE;
19187 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019188 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019189 if ((info.item_compare_func != NULL
19190 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019191 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019192 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019193 EMSG(_("E702: Sort compare function failed"));
19194 else
19195 {
19196 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019197 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019198 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019199 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019200 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019201
Bram Moolenaar0b962472016-02-22 22:51:33 +010019202 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019203 {
19204 /* Clear the List and append the items in sorted order. */
19205 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19206 l->lv_len = 0;
19207 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019208 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019209 }
19210 }
19211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019213 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019214 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019215
19216 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019217 info.item_compare_func_err = FALSE;
19218 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019219 item_compare_func_ptr = info.item_compare_func != NULL
19220 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019221 ? item_compare2 : item_compare;
19222
19223 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19224 li = li->li_next)
19225 {
19226 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19227 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019228 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019229 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019230 {
19231 EMSG(_("E882: Uniq compare function failed"));
19232 break;
19233 }
19234 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019235
Bram Moolenaar0b962472016-02-22 22:51:33 +010019236 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019237 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019238 while (--i >= 0)
19239 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019240 li = ptrs[i].item->li_next;
19241 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019242 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019243 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019244 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019245 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019246 list_fix_watch(l, li);
19247 listitem_free(li);
19248 l->lv_len--;
19249 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019250 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019251 }
19252
19253 vim_free(ptrs);
19254 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019255theend:
19256 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019257}
19258
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019259/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019260 * "sort({list})" function
19261 */
19262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019263f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019264{
19265 do_sort_uniq(argvars, rettv, TRUE);
19266}
19267
19268/*
19269 * "uniq({list})" function
19270 */
19271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019272f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019273{
19274 do_sort_uniq(argvars, rettv, FALSE);
19275}
19276
19277/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019278 * "soundfold({word})" function
19279 */
19280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019281f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019282{
19283 char_u *s;
19284
19285 rettv->v_type = VAR_STRING;
19286 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019287#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019288 rettv->vval.v_string = eval_soundfold(s);
19289#else
19290 rettv->vval.v_string = vim_strsave(s);
19291#endif
19292}
19293
19294/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019295 * "spellbadword()" function
19296 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019298f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019299{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019300 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019301 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019302 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019304 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019305 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019306
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019307#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019308 if (argvars[0].v_type == VAR_UNKNOWN)
19309 {
19310 /* Find the start and length of the badly spelled word. */
19311 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19312 if (len != 0)
19313 word = ml_get_cursor();
19314 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019315 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019316 {
19317 char_u *str = get_tv_string_chk(&argvars[0]);
19318 int capcol = -1;
19319
19320 if (str != NULL)
19321 {
19322 /* Check the argument for spelling. */
19323 while (*str != NUL)
19324 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019325 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019326 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019327 {
19328 word = str;
19329 break;
19330 }
19331 str += len;
19332 }
19333 }
19334 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019335#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019336
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019337 list_append_string(rettv->vval.v_list, word, len);
19338 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019339 attr == HLF_SPB ? "bad" :
19340 attr == HLF_SPR ? "rare" :
19341 attr == HLF_SPL ? "local" :
19342 attr == HLF_SPC ? "caps" :
19343 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019344}
19345
19346/*
19347 * "spellsuggest()" function
19348 */
19349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019350f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019351{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019352#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019353 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019354 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019355 int maxcount;
19356 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019357 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019358 listitem_T *li;
19359 int need_capital = FALSE;
19360#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019361
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019362 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019363 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019364
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019365#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019366 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019367 {
19368 str = get_tv_string(&argvars[0]);
19369 if (argvars[1].v_type != VAR_UNKNOWN)
19370 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019371 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019372 if (maxcount <= 0)
19373 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019374 if (argvars[2].v_type != VAR_UNKNOWN)
19375 {
19376 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19377 if (typeerr)
19378 return;
19379 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019380 }
19381 else
19382 maxcount = 25;
19383
Bram Moolenaar4770d092006-01-12 23:22:24 +000019384 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019385
19386 for (i = 0; i < ga.ga_len; ++i)
19387 {
19388 str = ((char_u **)ga.ga_data)[i];
19389
19390 li = listitem_alloc();
19391 if (li == NULL)
19392 vim_free(str);
19393 else
19394 {
19395 li->li_tv.v_type = VAR_STRING;
19396 li->li_tv.v_lock = 0;
19397 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019398 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019399 }
19400 }
19401 ga_clear(&ga);
19402 }
19403#endif
19404}
19405
Bram Moolenaar0d660222005-01-07 21:51:51 +000019406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019407f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019408{
19409 char_u *str;
19410 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019411 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019412 regmatch_T regmatch;
19413 char_u patbuf[NUMBUFLEN];
19414 char_u *save_cpo;
19415 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019416 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019417 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019418 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019419
19420 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19421 save_cpo = p_cpo;
19422 p_cpo = (char_u *)"";
19423
19424 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019425 if (argvars[1].v_type != VAR_UNKNOWN)
19426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019427 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19428 if (pat == NULL)
19429 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019430 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019431 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019432 }
19433 if (pat == NULL || *pat == NUL)
19434 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019435
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019436 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019438 if (typeerr)
19439 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440
Bram Moolenaar0d660222005-01-07 21:51:51 +000019441 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19442 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019444 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019445 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019446 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019447 if (*str == NUL)
19448 match = FALSE; /* empty item at the end */
19449 else
19450 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019451 if (match)
19452 end = regmatch.startp[0];
19453 else
19454 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019455 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19456 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019457 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019458 if (list_append_string(rettv->vval.v_list, str,
19459 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019460 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019461 }
19462 if (!match)
19463 break;
19464 /* Advance to just after the match. */
19465 if (regmatch.endp[0] > str)
19466 col = 0;
19467 else
19468 {
19469 /* Don't get stuck at the same match. */
19470#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019471 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019472#else
19473 col = 1;
19474#endif
19475 }
19476 str = regmatch.endp[0];
19477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478
Bram Moolenaar473de612013-06-08 18:19:48 +020019479 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481
Bram Moolenaar0d660222005-01-07 21:51:51 +000019482 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483}
19484
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019485#ifdef FEAT_FLOAT
19486/*
19487 * "sqrt()" function
19488 */
19489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019490f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019491{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019492 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019493
19494 rettv->v_type = VAR_FLOAT;
19495 if (get_float_arg(argvars, &f) == OK)
19496 rettv->vval.v_float = sqrt(f);
19497 else
19498 rettv->vval.v_float = 0.0;
19499}
19500
19501/*
19502 * "str2float()" function
19503 */
19504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019505f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019506{
19507 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19508
19509 if (*p == '+')
19510 p = skipwhite(p + 1);
19511 (void)string2float(p, &rettv->vval.v_float);
19512 rettv->v_type = VAR_FLOAT;
19513}
19514#endif
19515
Bram Moolenaar2c932302006-03-18 21:42:09 +000019516/*
19517 * "str2nr()" function
19518 */
19519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019520f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019521{
19522 int base = 10;
19523 char_u *p;
19524 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019525 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019526
19527 if (argvars[1].v_type != VAR_UNKNOWN)
19528 {
19529 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019530 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019531 {
19532 EMSG(_(e_invarg));
19533 return;
19534 }
19535 }
19536
19537 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019538 if (*p == '+')
19539 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019540 switch (base)
19541 {
19542 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19543 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19544 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19545 default: what = 0;
19546 }
19547 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019548 rettv->vval.v_number = n;
19549}
19550
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551#ifdef HAVE_STRFTIME
19552/*
19553 * "strftime({format}[, {time}])" function
19554 */
19555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019556f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557{
19558 char_u result_buf[256];
19559 struct tm *curtime;
19560 time_t seconds;
19561 char_u *p;
19562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019563 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019565 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019566 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 seconds = time(NULL);
19568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019569 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 curtime = localtime(&seconds);
19571 /* MSVC returns NULL for an invalid value of seconds. */
19572 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019573 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 else
19575 {
19576# ifdef FEAT_MBYTE
19577 vimconv_T conv;
19578 char_u *enc;
19579
19580 conv.vc_type = CONV_NONE;
19581 enc = enc_locale();
19582 convert_setup(&conv, p_enc, enc);
19583 if (conv.vc_type != CONV_NONE)
19584 p = string_convert(&conv, p, NULL);
19585# endif
19586 if (p != NULL)
19587 (void)strftime((char *)result_buf, sizeof(result_buf),
19588 (char *)p, curtime);
19589 else
19590 result_buf[0] = NUL;
19591
19592# ifdef FEAT_MBYTE
19593 if (conv.vc_type != CONV_NONE)
19594 vim_free(p);
19595 convert_setup(&conv, enc, p_enc);
19596 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 else
19599# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019600 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601
19602# ifdef FEAT_MBYTE
19603 /* Release conversion descriptors */
19604 convert_setup(&conv, NULL, NULL);
19605 vim_free(enc);
19606# endif
19607 }
19608}
19609#endif
19610
19611/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019612 * "strgetchar()" function
19613 */
19614 static void
19615f_strgetchar(typval_T *argvars, typval_T *rettv)
19616{
19617 char_u *str;
19618 int len;
19619 int error = FALSE;
19620 int charidx;
19621
19622 rettv->vval.v_number = -1;
19623 str = get_tv_string_chk(&argvars[0]);
19624 if (str == NULL)
19625 return;
19626 len = (int)STRLEN(str);
19627 charidx = get_tv_number_chk(&argvars[1], &error);
19628 if (error)
19629 return;
19630#ifdef FEAT_MBYTE
19631 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019632 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019633
19634 while (charidx >= 0 && byteidx < len)
19635 {
19636 if (charidx == 0)
19637 {
19638 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19639 break;
19640 }
19641 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019642 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019643 }
19644 }
19645#else
19646 if (charidx < len)
19647 rettv->vval.v_number = str[charidx];
19648#endif
19649}
19650
19651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 * "stridx()" function
19653 */
19654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019655f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656{
19657 char_u buf[NUMBUFLEN];
19658 char_u *needle;
19659 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019660 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019664 needle = get_tv_string_chk(&argvars[1]);
19665 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019666 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019667 if (needle == NULL || haystack == NULL)
19668 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 if (argvars[2].v_type != VAR_UNKNOWN)
19671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019672 int error = FALSE;
19673
19674 start_idx = get_tv_number_chk(&argvars[2], &error);
19675 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019676 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019677 if (start_idx >= 0)
19678 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019679 }
19680
19681 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19682 if (pos != NULL)
19683 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684}
19685
19686/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019687 * "string()" function
19688 */
19689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019690f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019691{
19692 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019693 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019695 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019696 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19697 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019698 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019699 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019700 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701}
19702
19703/*
19704 * "strlen()" function
19705 */
19706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019707f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019709 rettv->vval.v_number = (varnumber_T)(STRLEN(
19710 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711}
19712
19713/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019714 * "strchars()" function
19715 */
19716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019717f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019718{
19719 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019720 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019721#ifdef FEAT_MBYTE
19722 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019723 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019724#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019725
19726 if (argvars[1].v_type != VAR_UNKNOWN)
19727 skipcc = get_tv_number_chk(&argvars[1], NULL);
19728 if (skipcc < 0 || skipcc > 1)
19729 EMSG(_(e_invarg));
19730 else
19731 {
19732#ifdef FEAT_MBYTE
19733 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19734 while (*s != NUL)
19735 {
19736 func_mb_ptr2char_adv(&s);
19737 ++len;
19738 }
19739 rettv->vval.v_number = len;
19740#else
19741 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19742#endif
19743 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019744}
19745
19746/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019747 * "strdisplaywidth()" function
19748 */
19749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019750f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019751{
19752 char_u *s = get_tv_string(&argvars[0]);
19753 int col = 0;
19754
19755 if (argvars[1].v_type != VAR_UNKNOWN)
19756 col = get_tv_number(&argvars[1]);
19757
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019758 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019759}
19760
19761/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019762 * "strwidth()" function
19763 */
19764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019765f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019766{
19767 char_u *s = get_tv_string(&argvars[0]);
19768
19769 rettv->vval.v_number = (varnumber_T)(
19770#ifdef FEAT_MBYTE
19771 mb_string2cells(s, -1)
19772#else
19773 STRLEN(s)
19774#endif
19775 );
19776}
19777
19778/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019779 * "strcharpart()" function
19780 */
19781 static void
19782f_strcharpart(typval_T *argvars, typval_T *rettv)
19783{
19784#ifdef FEAT_MBYTE
19785 char_u *p;
19786 int nchar;
19787 int nbyte = 0;
19788 int charlen;
19789 int len = 0;
19790 int slen;
19791 int error = FALSE;
19792
19793 p = get_tv_string(&argvars[0]);
19794 slen = (int)STRLEN(p);
19795
19796 nchar = get_tv_number_chk(&argvars[1], &error);
19797 if (!error)
19798 {
19799 if (nchar > 0)
19800 while (nchar > 0 && nbyte < slen)
19801 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019802 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019803 --nchar;
19804 }
19805 else
19806 nbyte = nchar;
19807 if (argvars[2].v_type != VAR_UNKNOWN)
19808 {
19809 charlen = get_tv_number(&argvars[2]);
19810 while (charlen > 0 && nbyte + len < slen)
19811 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019812 int off = nbyte + len;
19813
19814 if (off < 0)
19815 len += 1;
19816 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019817 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019818 --charlen;
19819 }
19820 }
19821 else
19822 len = slen - nbyte; /* default: all bytes that are available. */
19823 }
19824
19825 /*
19826 * Only return the overlap between the specified part and the actual
19827 * string.
19828 */
19829 if (nbyte < 0)
19830 {
19831 len += nbyte;
19832 nbyte = 0;
19833 }
19834 else if (nbyte > slen)
19835 nbyte = slen;
19836 if (len < 0)
19837 len = 0;
19838 else if (nbyte + len > slen)
19839 len = slen - nbyte;
19840
19841 rettv->v_type = VAR_STRING;
19842 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19843#else
19844 f_strpart(argvars, rettv);
19845#endif
19846}
19847
19848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 * "strpart()" function
19850 */
19851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019852f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853{
19854 char_u *p;
19855 int n;
19856 int len;
19857 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019858 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019860 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 slen = (int)STRLEN(p);
19862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019863 n = get_tv_number_chk(&argvars[1], &error);
19864 if (error)
19865 len = 0;
19866 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019867 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 else
19869 len = slen - n; /* default len: all bytes that are available. */
19870
19871 /*
19872 * Only return the overlap between the specified part and the actual
19873 * string.
19874 */
19875 if (n < 0)
19876 {
19877 len += n;
19878 n = 0;
19879 }
19880 else if (n > slen)
19881 n = slen;
19882 if (len < 0)
19883 len = 0;
19884 else if (n + len > slen)
19885 len = slen - n;
19886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019887 rettv->v_type = VAR_STRING;
19888 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889}
19890
19891/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019892 * "strridx()" function
19893 */
19894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019895f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019896{
19897 char_u buf[NUMBUFLEN];
19898 char_u *needle;
19899 char_u *haystack;
19900 char_u *rest;
19901 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019902 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019904 needle = get_tv_string_chk(&argvars[1]);
19905 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019906
19907 rettv->vval.v_number = -1;
19908 if (needle == NULL || haystack == NULL)
19909 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019910
19911 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019912 if (argvars[2].v_type != VAR_UNKNOWN)
19913 {
19914 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019915 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019916 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019917 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019918 }
19919 else
19920 end_idx = haystack_len;
19921
Bram Moolenaar0d660222005-01-07 21:51:51 +000019922 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019923 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019924 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019925 lastmatch = haystack + end_idx;
19926 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019927 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019928 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019929 for (rest = haystack; *rest != '\0'; ++rest)
19930 {
19931 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019932 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019933 break;
19934 lastmatch = rest;
19935 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019936 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019937
19938 if (lastmatch == NULL)
19939 rettv->vval.v_number = -1;
19940 else
19941 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19942}
19943
19944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 * "strtrans()" function
19946 */
19947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019948f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019950 rettv->v_type = VAR_STRING;
19951 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952}
19953
19954/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019955 * "submatch()" function
19956 */
19957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019958f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019959{
Bram Moolenaar41571762014-04-02 19:00:58 +020019960 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019961 int no;
19962 int retList = 0;
19963
19964 no = (int)get_tv_number_chk(&argvars[0], &error);
19965 if (error)
19966 return;
19967 error = FALSE;
19968 if (argvars[1].v_type != VAR_UNKNOWN)
19969 retList = get_tv_number_chk(&argvars[1], &error);
19970 if (error)
19971 return;
19972
19973 if (retList == 0)
19974 {
19975 rettv->v_type = VAR_STRING;
19976 rettv->vval.v_string = reg_submatch(no);
19977 }
19978 else
19979 {
19980 rettv->v_type = VAR_LIST;
19981 rettv->vval.v_list = reg_submatch_list(no);
19982 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019983}
19984
19985/*
19986 * "substitute()" function
19987 */
19988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019989f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019990{
19991 char_u patbuf[NUMBUFLEN];
19992 char_u subbuf[NUMBUFLEN];
19993 char_u flagsbuf[NUMBUFLEN];
19994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019995 char_u *str = get_tv_string_chk(&argvars[0]);
19996 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19997 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19998 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19999
Bram Moolenaar0d660222005-01-07 21:51:51 +000020000 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020001 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20002 rettv->vval.v_string = NULL;
20003 else
20004 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020005}
20006
20007/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020008 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020011f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012{
20013 int id = 0;
20014#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020015 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 long col;
20017 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020018 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020020 lnum = get_tv_lnum(argvars); /* -1 on type error */
20021 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20022 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020024 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020025 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020026 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027#endif
20028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030}
20031
20032/*
20033 * "synIDattr(id, what [, mode])" function
20034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020036f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037{
20038 char_u *p = NULL;
20039#ifdef FEAT_SYN_HL
20040 int id;
20041 char_u *what;
20042 char_u *mode;
20043 char_u modebuf[NUMBUFLEN];
20044 int modec;
20045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020046 id = get_tv_number(&argvars[0]);
20047 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020048 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020050 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020052 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 modec = 0; /* replace invalid with current */
20054 }
20055 else
20056 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020057#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020058 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 modec = 'g';
20060 else
20061#endif
20062 if (t_colors > 1)
20063 modec = 'c';
20064 else
20065 modec = 't';
20066 }
20067
20068
20069 switch (TOLOWER_ASC(what[0]))
20070 {
20071 case 'b':
20072 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20073 p = highlight_color(id, what, modec);
20074 else /* bold */
20075 p = highlight_has_attr(id, HL_BOLD, modec);
20076 break;
20077
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020078 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 p = highlight_color(id, what, modec);
20080 break;
20081
20082 case 'i':
20083 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20084 p = highlight_has_attr(id, HL_INVERSE, modec);
20085 else /* italic */
20086 p = highlight_has_attr(id, HL_ITALIC, modec);
20087 break;
20088
20089 case 'n': /* name */
20090 p = get_highlight_name(NULL, id - 1);
20091 break;
20092
20093 case 'r': /* reverse */
20094 p = highlight_has_attr(id, HL_INVERSE, modec);
20095 break;
20096
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020097 case 's':
20098 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20099 p = highlight_color(id, what, modec);
20100 else /* standout */
20101 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 break;
20103
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020104 case 'u':
20105 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20106 /* underline */
20107 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20108 else
20109 /* undercurl */
20110 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 break;
20112 }
20113
20114 if (p != NULL)
20115 p = vim_strsave(p);
20116#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020117 rettv->v_type = VAR_STRING;
20118 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119}
20120
20121/*
20122 * "synIDtrans(id)" function
20123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020125f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126{
20127 int id;
20128
20129#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020130 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131
20132 if (id > 0)
20133 id = syn_get_final_id(id);
20134 else
20135#endif
20136 id = 0;
20137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139}
20140
20141/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020142 * "synconcealed(lnum, col)" function
20143 */
20144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020145f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020146{
20147#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20148 long lnum;
20149 long col;
20150 int syntax_flags = 0;
20151 int cchar;
20152 int matchid = 0;
20153 char_u str[NUMBUFLEN];
20154#endif
20155
20156 rettv->v_type = VAR_LIST;
20157 rettv->vval.v_list = NULL;
20158
20159#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20160 lnum = get_tv_lnum(argvars); /* -1 on type error */
20161 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20162
20163 vim_memset(str, NUL, sizeof(str));
20164
20165 if (rettv_list_alloc(rettv) != FAIL)
20166 {
20167 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20168 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20169 && curwin->w_p_cole > 0)
20170 {
20171 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20172 syntax_flags = get_syntax_info(&matchid);
20173
20174 /* get the conceal character */
20175 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20176 {
20177 cchar = syn_get_sub_char();
20178 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20179 cchar = lcs_conceal;
20180 if (cchar != NUL)
20181 {
20182# ifdef FEAT_MBYTE
20183 if (has_mbyte)
20184 (*mb_char2bytes)(cchar, str);
20185 else
20186# endif
20187 str[0] = cchar;
20188 }
20189 }
20190 }
20191
20192 list_append_number(rettv->vval.v_list,
20193 (syntax_flags & HL_CONCEAL) != 0);
20194 /* -1 to auto-determine strlen */
20195 list_append_string(rettv->vval.v_list, str, -1);
20196 list_append_number(rettv->vval.v_list, matchid);
20197 }
20198#endif
20199}
20200
20201/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020202 * "synstack(lnum, col)" function
20203 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020205f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020206{
20207#ifdef FEAT_SYN_HL
20208 long lnum;
20209 long col;
20210 int i;
20211 int id;
20212#endif
20213
20214 rettv->v_type = VAR_LIST;
20215 rettv->vval.v_list = NULL;
20216
20217#ifdef FEAT_SYN_HL
20218 lnum = get_tv_lnum(argvars); /* -1 on type error */
20219 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20220
20221 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020222 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020223 && rettv_list_alloc(rettv) != FAIL)
20224 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020225 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020226 for (i = 0; ; ++i)
20227 {
20228 id = syn_get_stack_item(i);
20229 if (id < 0)
20230 break;
20231 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20232 break;
20233 }
20234 }
20235#endif
20236}
20237
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020239get_cmd_output_as_rettv(
20240 typval_T *argvars,
20241 typval_T *rettv,
20242 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020244 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020246 char_u *infile = NULL;
20247 char_u buf[NUMBUFLEN];
20248 int err = FALSE;
20249 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020250 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020251 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020253 rettv->v_type = VAR_STRING;
20254 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020255 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020256 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020257
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020258 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020259 {
20260 /*
20261 * Write the string to a temp file, to be used for input of the shell
20262 * command.
20263 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020264 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020265 {
20266 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020267 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020268 }
20269
20270 fd = mch_fopen((char *)infile, WRITEBIN);
20271 if (fd == NULL)
20272 {
20273 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020274 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020275 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020276 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020277 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020278 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20279 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020280 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020281 else
20282 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020283 size_t len;
20284
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020285 p = get_tv_string_buf_chk(&argvars[1], buf);
20286 if (p == NULL)
20287 {
20288 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020289 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020290 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020291 len = STRLEN(p);
20292 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020293 err = TRUE;
20294 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020295 if (fclose(fd) != 0)
20296 err = TRUE;
20297 if (err)
20298 {
20299 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020300 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020301 }
20302 }
20303
Bram Moolenaar52a72462014-08-29 15:53:52 +020020304 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20305 * echoes typeahead, that messes up the display. */
20306 if (!msg_silent)
20307 flags += SHELL_COOKED;
20308
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020309 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020311 int len;
20312 listitem_T *li;
20313 char_u *s = NULL;
20314 char_u *start;
20315 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020316 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317
Bram Moolenaar52a72462014-08-29 15:53:52 +020020318 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020319 if (res == NULL)
20320 goto errret;
20321
20322 list = list_alloc();
20323 if (list == NULL)
20324 goto errret;
20325
20326 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020328 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020329 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020330 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020331 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020332
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020333 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020334 if (s == NULL)
20335 goto errret;
20336
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020337 for (p = s; start < end; ++p, ++start)
20338 *p = *start == NUL ? NL : *start;
20339 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020340
20341 li = listitem_alloc();
20342 if (li == NULL)
20343 {
20344 vim_free(s);
20345 goto errret;
20346 }
20347 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020348 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020349 li->li_tv.vval.v_string = s;
20350 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020352
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020353 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020354 rettv->v_type = VAR_LIST;
20355 rettv->vval.v_list = list;
20356 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020358 else
20359 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020360 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020361#ifdef USE_CR
20362 /* translate <CR> into <NL> */
20363 if (res != NULL)
20364 {
20365 char_u *s;
20366
20367 for (s = res; *s; ++s)
20368 {
20369 if (*s == CAR)
20370 *s = NL;
20371 }
20372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373#else
20374# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020375 /* translate <CR><NL> into <NL> */
20376 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020378 char_u *s, *d;
20379
20380 d = res;
20381 for (s = res; *s; ++s)
20382 {
20383 if (s[0] == CAR && s[1] == NL)
20384 ++s;
20385 *d++ = *s;
20386 }
20387 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389# endif
20390#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020391 rettv->vval.v_string = res;
20392 res = NULL;
20393 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020394
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020395errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020396 if (infile != NULL)
20397 {
20398 mch_remove(infile);
20399 vim_free(infile);
20400 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020401 if (res != NULL)
20402 vim_free(res);
20403 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020404 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020405}
20406
20407/*
20408 * "system()" function
20409 */
20410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020411f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020412{
20413 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20414}
20415
20416/*
20417 * "systemlist()" function
20418 */
20419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020420f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020421{
20422 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423}
20424
20425/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020426 * "tabpagebuflist()" function
20427 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020429f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020430{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020431#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020432 tabpage_T *tp;
20433 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020434
20435 if (argvars[0].v_type == VAR_UNKNOWN)
20436 wp = firstwin;
20437 else
20438 {
20439 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20440 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020441 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020442 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020443 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020444 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020445 for (; wp != NULL; wp = wp->w_next)
20446 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020447 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020448 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020449 }
20450#endif
20451}
20452
20453
20454/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020455 * "tabpagenr()" function
20456 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020458f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020459{
20460 int nr = 1;
20461#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020462 char_u *arg;
20463
20464 if (argvars[0].v_type != VAR_UNKNOWN)
20465 {
20466 arg = get_tv_string_chk(&argvars[0]);
20467 nr = 0;
20468 if (arg != NULL)
20469 {
20470 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020471 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020472 else
20473 EMSG2(_(e_invexpr2), arg);
20474 }
20475 }
20476 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020477 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020478#endif
20479 rettv->vval.v_number = nr;
20480}
20481
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020482
20483#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020484static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020485
20486/*
20487 * Common code for tabpagewinnr() and winnr().
20488 */
20489 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020490get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020491{
20492 win_T *twin;
20493 int nr = 1;
20494 win_T *wp;
20495 char_u *arg;
20496
20497 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20498 if (argvar->v_type != VAR_UNKNOWN)
20499 {
20500 arg = get_tv_string_chk(argvar);
20501 if (arg == NULL)
20502 nr = 0; /* type error; errmsg already given */
20503 else if (STRCMP(arg, "$") == 0)
20504 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20505 else if (STRCMP(arg, "#") == 0)
20506 {
20507 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20508 if (twin == NULL)
20509 nr = 0;
20510 }
20511 else
20512 {
20513 EMSG2(_(e_invexpr2), arg);
20514 nr = 0;
20515 }
20516 }
20517
20518 if (nr > 0)
20519 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20520 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020521 {
20522 if (wp == NULL)
20523 {
20524 /* didn't find it in this tabpage */
20525 nr = 0;
20526 break;
20527 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020528 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020529 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020530 return nr;
20531}
20532#endif
20533
20534/*
20535 * "tabpagewinnr()" function
20536 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020538f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020539{
20540 int nr = 1;
20541#ifdef FEAT_WINDOWS
20542 tabpage_T *tp;
20543
20544 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20545 if (tp == NULL)
20546 nr = 0;
20547 else
20548 nr = get_winnr(tp, &argvars[1]);
20549#endif
20550 rettv->vval.v_number = nr;
20551}
20552
20553
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020554/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020555 * "tagfiles()" function
20556 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020558f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020559{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020560 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020561 tagname_T tn;
20562 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020564 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020565 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020566 fname = alloc(MAXPATHL);
20567 if (fname == NULL)
20568 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020569
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020570 for (first = TRUE; ; first = FALSE)
20571 if (get_tagfname(&tn, first, fname) == FAIL
20572 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020573 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020574 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020575 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020576}
20577
20578/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020579 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020580 */
20581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020582f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020583{
20584 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020585
20586 tag_pattern = get_tv_string(&argvars[0]);
20587
20588 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020589 if (*tag_pattern == NUL)
20590 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020591
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020592 if (rettv_list_alloc(rettv) == OK)
20593 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020594}
20595
20596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 * "tempname()" function
20598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020600f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601{
20602 static int x = 'A';
20603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020604 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020605 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606
20607 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20608 * names. Skip 'I' and 'O', they are used for shell redirection. */
20609 do
20610 {
20611 if (x == 'Z')
20612 x = '0';
20613 else if (x == '9')
20614 x = 'A';
20615 else
20616 {
20617#ifdef EBCDIC
20618 if (x == 'I')
20619 x = 'J';
20620 else if (x == 'R')
20621 x = 'S';
20622 else
20623#endif
20624 ++x;
20625 }
20626 } while (x == 'I' || x == 'O');
20627}
20628
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020629#ifdef FEAT_FLOAT
20630/*
20631 * "tan()" function
20632 */
20633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020634f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020635{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020636 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020637
20638 rettv->v_type = VAR_FLOAT;
20639 if (get_float_arg(argvars, &f) == OK)
20640 rettv->vval.v_float = tan(f);
20641 else
20642 rettv->vval.v_float = 0.0;
20643}
20644
20645/*
20646 * "tanh()" function
20647 */
20648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020649f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020650{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020651 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020652
20653 rettv->v_type = VAR_FLOAT;
20654 if (get_float_arg(argvars, &f) == OK)
20655 rettv->vval.v_float = tanh(f);
20656 else
20657 rettv->vval.v_float = 0.0;
20658}
20659#endif
20660
Bram Moolenaar574860b2016-05-24 17:33:34 +020020661/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020662 * "test_alloc_fail(id, countdown, repeat)" function
20663 */
20664 static void
20665f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20666{
20667 if (argvars[0].v_type != VAR_NUMBER
20668 || argvars[0].vval.v_number <= 0
20669 || argvars[1].v_type != VAR_NUMBER
20670 || argvars[1].vval.v_number < 0
20671 || argvars[2].v_type != VAR_NUMBER)
20672 EMSG(_(e_invarg));
20673 else
20674 {
20675 alloc_fail_id = argvars[0].vval.v_number;
20676 if (alloc_fail_id >= aid_last)
20677 EMSG(_(e_invarg));
20678 alloc_fail_countdown = argvars[1].vval.v_number;
20679 alloc_fail_repeat = argvars[2].vval.v_number;
20680 did_outofmem_msg = FALSE;
20681 }
20682}
20683
20684/*
20685 * "test_disable_char_avail({expr})" function
20686 */
20687 static void
20688f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20689{
20690 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20691}
20692
20693/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020694 * "test_garbagecollect_now()" function
20695 */
20696 static void
20697f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20698{
20699 /* This is dangerous, any Lists and Dicts used internally may be freed
20700 * while still in use. */
20701 garbage_collect(TRUE);
20702}
20703
20704#ifdef FEAT_JOB_CHANNEL
20705 static void
20706f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20707{
20708 rettv->v_type = VAR_CHANNEL;
20709 rettv->vval.v_channel = NULL;
20710}
20711#endif
20712
20713 static void
20714f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20715{
20716 rettv->v_type = VAR_DICT;
20717 rettv->vval.v_dict = NULL;
20718}
20719
20720#ifdef FEAT_JOB_CHANNEL
20721 static void
20722f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20723{
20724 rettv->v_type = VAR_JOB;
20725 rettv->vval.v_job = NULL;
20726}
20727#endif
20728
20729 static void
20730f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20731{
20732 rettv->v_type = VAR_LIST;
20733 rettv->vval.v_list = NULL;
20734}
20735
20736 static void
20737f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20738{
20739 rettv->v_type = VAR_PARTIAL;
20740 rettv->vval.v_partial = NULL;
20741}
20742
20743 static void
20744f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20745{
20746 rettv->v_type = VAR_STRING;
20747 rettv->vval.v_string = NULL;
20748}
20749
Bram Moolenaar975b5272016-03-15 23:10:59 +010020750#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20751/*
20752 * Get a callback from "arg". It can be a Funcref or a function name.
20753 * When "arg" is zero return an empty string.
20754 * Return NULL for an invalid argument.
20755 */
20756 char_u *
20757get_callback(typval_T *arg, partial_T **pp)
20758{
20759 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20760 {
20761 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020762 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020763 return (*pp)->pt_name;
20764 }
20765 *pp = NULL;
20766 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20767 return arg->vval.v_string;
20768 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20769 return (char_u *)"";
20770 EMSG(_("E921: Invalid callback argument"));
20771 return NULL;
20772}
20773#endif
20774
20775#ifdef FEAT_TIMERS
20776/*
20777 * "timer_start(time, callback [, options])" function
20778 */
20779 static void
20780f_timer_start(typval_T *argvars, typval_T *rettv)
20781{
20782 long msec = get_tv_number(&argvars[0]);
20783 timer_T *timer;
20784 int repeat = 0;
20785 char_u *callback;
20786 dict_T *dict;
20787
Bram Moolenaar38499922016-04-22 20:46:52 +020020788 if (check_secure())
20789 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020790 if (argvars[2].v_type != VAR_UNKNOWN)
20791 {
20792 if (argvars[2].v_type != VAR_DICT
20793 || (dict = argvars[2].vval.v_dict) == NULL)
20794 {
20795 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20796 return;
20797 }
20798 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20799 repeat = get_dict_number(dict, (char_u *)"repeat");
20800 }
20801
20802 timer = create_timer(msec, repeat);
20803 callback = get_callback(&argvars[1], &timer->tr_partial);
20804 if (callback == NULL)
20805 {
20806 stop_timer(timer);
20807 rettv->vval.v_number = -1;
20808 }
20809 else
20810 {
20811 timer->tr_callback = vim_strsave(callback);
20812 rettv->vval.v_number = timer->tr_id;
20813 }
20814}
20815
20816/*
20817 * "timer_stop(timer)" function
20818 */
20819 static void
20820f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20821{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020822 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020823
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020824 if (argvars[0].v_type != VAR_NUMBER)
20825 {
20826 EMSG(_(e_number_exp));
20827 return;
20828 }
20829 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020830 if (timer != NULL)
20831 stop_timer(timer);
20832}
20833#endif
20834
Bram Moolenaard52d9742005-08-21 22:20:28 +000020835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 * "tolower(string)" function
20837 */
20838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020839f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840{
20841 char_u *p;
20842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843 p = vim_strsave(get_tv_string(&argvars[0]));
20844 rettv->v_type = VAR_STRING;
20845 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846
20847 if (p != NULL)
20848 while (*p != NUL)
20849 {
20850#ifdef FEAT_MBYTE
20851 int l;
20852
20853 if (enc_utf8)
20854 {
20855 int c, lc;
20856
20857 c = utf_ptr2char(p);
20858 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020859 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 /* TODO: reallocate string when byte count changes. */
20861 if (utf_char2len(lc) == l)
20862 utf_char2bytes(lc, p);
20863 p += l;
20864 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020865 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 p += l; /* skip multi-byte character */
20867 else
20868#endif
20869 {
20870 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20871 ++p;
20872 }
20873 }
20874}
20875
20876/*
20877 * "toupper(string)" function
20878 */
20879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020880f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020882 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020883 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884}
20885
20886/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020887 * "tr(string, fromstr, tostr)" function
20888 */
20889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020890f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020891{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020892 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020893 char_u *fromstr;
20894 char_u *tostr;
20895 char_u *p;
20896#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020897 int inlen;
20898 int fromlen;
20899 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020900 int idx;
20901 char_u *cpstr;
20902 int cplen;
20903 int first = TRUE;
20904#endif
20905 char_u buf[NUMBUFLEN];
20906 char_u buf2[NUMBUFLEN];
20907 garray_T ga;
20908
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020909 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020910 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20911 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020912
20913 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020914 rettv->v_type = VAR_STRING;
20915 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020916 if (fromstr == NULL || tostr == NULL)
20917 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020918 ga_init2(&ga, (int)sizeof(char), 80);
20919
20920#ifdef FEAT_MBYTE
20921 if (!has_mbyte)
20922#endif
20923 /* not multi-byte: fromstr and tostr must be the same length */
20924 if (STRLEN(fromstr) != STRLEN(tostr))
20925 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020926#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020927error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020928#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020929 EMSG2(_(e_invarg2), fromstr);
20930 ga_clear(&ga);
20931 return;
20932 }
20933
20934 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020935 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020936 {
20937#ifdef FEAT_MBYTE
20938 if (has_mbyte)
20939 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020940 inlen = (*mb_ptr2len)(in_str);
20941 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020942 cplen = inlen;
20943 idx = 0;
20944 for (p = fromstr; *p != NUL; p += fromlen)
20945 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020946 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020947 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020948 {
20949 for (p = tostr; *p != NUL; p += tolen)
20950 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020951 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020952 if (idx-- == 0)
20953 {
20954 cplen = tolen;
20955 cpstr = p;
20956 break;
20957 }
20958 }
20959 if (*p == NUL) /* tostr is shorter than fromstr */
20960 goto error;
20961 break;
20962 }
20963 ++idx;
20964 }
20965
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020966 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020967 {
20968 /* Check that fromstr and tostr have the same number of
20969 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020970 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020971 first = FALSE;
20972 for (p = tostr; *p != NUL; p += tolen)
20973 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020974 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020975 --idx;
20976 }
20977 if (idx != 0)
20978 goto error;
20979 }
20980
Bram Moolenaarcde88542015-08-11 19:14:00 +020020981 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020982 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020983 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020984
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020985 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020986 }
20987 else
20988#endif
20989 {
20990 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020991 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020992 if (p != NULL)
20993 ga_append(&ga, tostr[p - fromstr]);
20994 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020995 ga_append(&ga, *in_str);
20996 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020997 }
20998 }
20999
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021000 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021001 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021002 ga_append(&ga, NUL);
21003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021005}
21006
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021007#ifdef FEAT_FLOAT
21008/*
21009 * "trunc({float})" function
21010 */
21011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021012f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021013{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021014 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021015
21016 rettv->v_type = VAR_FLOAT;
21017 if (get_float_arg(argvars, &f) == OK)
21018 /* trunc() is not in C90, use floor() or ceil() instead. */
21019 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21020 else
21021 rettv->vval.v_float = 0.0;
21022}
21023#endif
21024
Bram Moolenaar8299df92004-07-10 09:47:34 +000021025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 * "type(expr)" function
21027 */
21028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021031 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021032
21033 switch (argvars[0].v_type)
21034 {
21035 case VAR_NUMBER: n = 0; break;
21036 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021037 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021038 case VAR_FUNC: n = 2; break;
21039 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021040 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021041 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021042 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021043 if (argvars[0].vval.v_number == VVAL_FALSE
21044 || argvars[0].vval.v_number == VVAL_TRUE)
21045 n = 6;
21046 else
21047 n = 7;
21048 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021049 case VAR_JOB: n = 8; break;
21050 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021051 case VAR_UNKNOWN:
21052 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21053 n = -1;
21054 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021055 }
21056 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057}
21058
21059/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021060 * "undofile(name)" function
21061 */
21062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021063f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021064{
21065 rettv->v_type = VAR_STRING;
21066#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021067 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021068 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021069
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021070 if (*fname == NUL)
21071 {
21072 /* If there is no file name there will be no undo file. */
21073 rettv->vval.v_string = NULL;
21074 }
21075 else
21076 {
21077 char_u *ffname = FullName_save(fname, FALSE);
21078
21079 if (ffname != NULL)
21080 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21081 vim_free(ffname);
21082 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021083 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021084#else
21085 rettv->vval.v_string = NULL;
21086#endif
21087}
21088
21089/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021090 * "undotree()" function
21091 */
21092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021093f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021094{
21095 if (rettv_dict_alloc(rettv) == OK)
21096 {
21097 dict_T *dict = rettv->vval.v_dict;
21098 list_T *list;
21099
Bram Moolenaar730cde92010-06-27 05:18:54 +020021100 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021101 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021102 dict_add_nr_str(dict, "save_last",
21103 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021104 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21105 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021106 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021107
21108 list = list_alloc();
21109 if (list != NULL)
21110 {
21111 u_eval_tree(curbuf->b_u_oldhead, list);
21112 dict_add_list(dict, "entries", list);
21113 }
21114 }
21115}
21116
21117/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021118 * "values(dict)" function
21119 */
21120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021121f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021122{
21123 dict_list(argvars, rettv, 1);
21124}
21125
21126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 * "virtcol(string)" function
21128 */
21129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021130f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131{
21132 colnr_T vcol = 0;
21133 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021134 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021136 fp = var2fpos(&argvars[0], FALSE, &fnum);
21137 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21138 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 {
21140 getvvcol(curwin, fp, NULL, NULL, &vcol);
21141 ++vcol;
21142 }
21143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021144 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145}
21146
21147/*
21148 * "visualmode()" function
21149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021151f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 char_u str[2];
21154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021155 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 str[0] = curbuf->b_visual_mode_eval;
21157 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021158 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159
21160 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021161 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163}
21164
21165/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021166 * "wildmenumode()" function
21167 */
21168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021169f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021170{
21171#ifdef FEAT_WILDMENU
21172 if (wild_menu_showing)
21173 rettv->vval.v_number = 1;
21174#endif
21175}
21176
21177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 * "winbufnr(nr)" function
21179 */
21180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021181f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182{
21183 win_T *wp;
21184
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021185 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021189 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190}
21191
21192/*
21193 * "wincol()" function
21194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021196f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197{
21198 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021199 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200}
21201
21202/*
21203 * "winheight(nr)" function
21204 */
21205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021206f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207{
21208 win_T *wp;
21209
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021210 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021212 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215}
21216
21217/*
21218 * "winline()" function
21219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021221f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222{
21223 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021224 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225}
21226
21227/*
21228 * "winnr()" function
21229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021231f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232{
21233 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021234
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021236 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021238 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239}
21240
21241/*
21242 * "winrestcmd()" function
21243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021245f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246{
21247#ifdef FEAT_WINDOWS
21248 win_T *wp;
21249 int winnr = 1;
21250 garray_T ga;
21251 char_u buf[50];
21252
21253 ga_init2(&ga, (int)sizeof(char), 70);
21254 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21255 {
21256 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21257 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21259 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 ++winnr;
21261 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021262 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021264 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021266 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021268 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269}
21270
21271/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021272 * "winrestview()" function
21273 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021275f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021276{
21277 dict_T *dict;
21278
21279 if (argvars[0].v_type != VAR_DICT
21280 || (dict = argvars[0].vval.v_dict) == NULL)
21281 EMSG(_(e_invarg));
21282 else
21283 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021284 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21285 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21286 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21287 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021288#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021289 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21290 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021291#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021292 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21293 {
21294 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21295 curwin->w_set_curswant = FALSE;
21296 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021297
Bram Moolenaar82c25852014-05-28 16:47:16 +020021298 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21299 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021300#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021301 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21302 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021303#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021304 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21305 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21306 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21307 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021308
21309 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021310 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021311# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021312 win_new_width(curwin, W_WIDTH(curwin));
21313# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021314 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021315
Bram Moolenaarb851a962014-10-31 15:45:52 +010021316 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021317 curwin->w_topline = 1;
21318 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21319 curwin->w_topline = curbuf->b_ml.ml_line_count;
21320#ifdef FEAT_DIFF
21321 check_topfill(curwin, TRUE);
21322#endif
21323 }
21324}
21325
21326/*
21327 * "winsaveview()" function
21328 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021330f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021331{
21332 dict_T *dict;
21333
Bram Moolenaara800b422010-06-27 01:15:55 +020021334 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021335 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021336 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021337
21338 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21339 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21340#ifdef FEAT_VIRTUALEDIT
21341 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21342#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021343 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021344 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21345
21346 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21347#ifdef FEAT_DIFF
21348 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21349#endif
21350 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21351 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21352}
21353
21354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 * "winwidth(nr)" function
21356 */
21357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021358f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359{
21360 win_T *wp;
21361
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021362 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021364 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021366#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021369 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370#endif
21371}
21372
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021374 * "wordcount()" function
21375 */
21376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021377f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021378{
21379 if (rettv_dict_alloc(rettv) == FAIL)
21380 return;
21381 cursor_pos_info(rettv->vval.v_dict);
21382}
21383
21384/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021385 * Write list of strings to file
21386 */
21387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021388write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021389{
21390 listitem_T *li;
21391 int c;
21392 int ret = OK;
21393 char_u *s;
21394
21395 for (li = list->lv_first; li != NULL; li = li->li_next)
21396 {
21397 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21398 {
21399 if (*s == '\n')
21400 c = putc(NUL, fd);
21401 else
21402 c = putc(*s, fd);
21403 if (c == EOF)
21404 {
21405 ret = FAIL;
21406 break;
21407 }
21408 }
21409 if (!binary || li->li_next != NULL)
21410 if (putc('\n', fd) == EOF)
21411 {
21412 ret = FAIL;
21413 break;
21414 }
21415 if (ret == FAIL)
21416 {
21417 EMSG(_(e_write));
21418 break;
21419 }
21420 }
21421 return ret;
21422}
21423
21424/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021425 * "writefile()" function
21426 */
21427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021428f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021429{
21430 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021431 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021432 char_u *fname;
21433 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021434 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021435
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021436 if (check_restricted() || check_secure())
21437 return;
21438
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021439 if (argvars[0].v_type != VAR_LIST)
21440 {
21441 EMSG2(_(e_listarg), "writefile()");
21442 return;
21443 }
21444 if (argvars[0].vval.v_list == NULL)
21445 return;
21446
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021447 if (argvars[2].v_type != VAR_UNKNOWN)
21448 {
21449 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21450 binary = TRUE;
21451 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21452 append = TRUE;
21453 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021454
21455 /* Always open the file in binary mode, library functions have a mind of
21456 * their own about CR-LF conversion. */
21457 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021458 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21459 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021460 {
21461 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21462 ret = -1;
21463 }
21464 else
21465 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021466 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21467 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021468 fclose(fd);
21469 }
21470
21471 rettv->vval.v_number = ret;
21472}
21473
21474/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021475 * "xor(expr, expr)" function
21476 */
21477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021478f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021479{
21480 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21481 ^ get_tv_number_chk(&argvars[1], NULL);
21482}
21483
21484
21485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021487 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488 */
21489 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021490var2fpos(
21491 typval_T *varp,
21492 int dollar_lnum, /* TRUE when $ is last line */
21493 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021495 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021497 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498
Bram Moolenaara5525202006-03-02 22:52:09 +000021499 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021500 if (varp->v_type == VAR_LIST)
21501 {
21502 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021503 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021504 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021505 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021506
21507 l = varp->vval.v_list;
21508 if (l == NULL)
21509 return NULL;
21510
21511 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021512 pos.lnum = list_find_nr(l, 0L, &error);
21513 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021514 return NULL; /* invalid line number */
21515
21516 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021517 pos.col = list_find_nr(l, 1L, &error);
21518 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021519 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021520 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021521
21522 /* We accept "$" for the column number: last column. */
21523 li = list_find(l, 1L);
21524 if (li != NULL && li->li_tv.v_type == VAR_STRING
21525 && li->li_tv.vval.v_string != NULL
21526 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21527 pos.col = len + 1;
21528
Bram Moolenaara5525202006-03-02 22:52:09 +000021529 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021530 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021531 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021532 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021533
Bram Moolenaara5525202006-03-02 22:52:09 +000021534#ifdef FEAT_VIRTUALEDIT
21535 /* Get the virtual offset. Defaults to zero. */
21536 pos.coladd = list_find_nr(l, 2L, &error);
21537 if (error)
21538 pos.coladd = 0;
21539#endif
21540
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021541 return &pos;
21542 }
21543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021544 name = get_tv_string_chk(varp);
21545 if (name == NULL)
21546 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021547 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021549 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21550 {
21551 if (VIsual_active)
21552 return &VIsual;
21553 return &curwin->w_cursor;
21554 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021555 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021557 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21559 return NULL;
21560 return pp;
21561 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021562
21563#ifdef FEAT_VIRTUALEDIT
21564 pos.coladd = 0;
21565#endif
21566
Bram Moolenaar477933c2007-07-17 14:32:23 +000021567 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021568 {
21569 pos.col = 0;
21570 if (name[1] == '0') /* "w0": first visible line */
21571 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021572 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021573 pos.lnum = curwin->w_topline;
21574 return &pos;
21575 }
21576 else if (name[1] == '$') /* "w$": last visible line */
21577 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021578 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021579 pos.lnum = curwin->w_botline - 1;
21580 return &pos;
21581 }
21582 }
21583 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021585 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 {
21587 pos.lnum = curbuf->b_ml.ml_line_count;
21588 pos.col = 0;
21589 }
21590 else
21591 {
21592 pos.lnum = curwin->w_cursor.lnum;
21593 pos.col = (colnr_T)STRLEN(ml_get_curline());
21594 }
21595 return &pos;
21596 }
21597 return NULL;
21598}
21599
21600/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021601 * Convert list in "arg" into a position and optional file number.
21602 * When "fnump" is NULL there is no file number, only 3 items.
21603 * Note that the column is passed on as-is, the caller may want to decrement
21604 * it to use 1 for the first column.
21605 * Return FAIL when conversion is not possible, doesn't check the position for
21606 * validity.
21607 */
21608 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021609list2fpos(
21610 typval_T *arg,
21611 pos_T *posp,
21612 int *fnump,
21613 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021614{
21615 list_T *l = arg->vval.v_list;
21616 long i = 0;
21617 long n;
21618
Bram Moolenaar493c1782014-05-28 14:34:46 +020021619 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21620 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021621 if (arg->v_type != VAR_LIST
21622 || l == NULL
21623 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021624 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021625 return FAIL;
21626
21627 if (fnump != NULL)
21628 {
21629 n = list_find_nr(l, i++, NULL); /* fnum */
21630 if (n < 0)
21631 return FAIL;
21632 if (n == 0)
21633 n = curbuf->b_fnum; /* current buffer */
21634 *fnump = n;
21635 }
21636
21637 n = list_find_nr(l, i++, NULL); /* lnum */
21638 if (n < 0)
21639 return FAIL;
21640 posp->lnum = n;
21641
21642 n = list_find_nr(l, i++, NULL); /* col */
21643 if (n < 0)
21644 return FAIL;
21645 posp->col = n;
21646
21647#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021648 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021649 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021650 posp->coladd = 0;
21651 else
21652 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021653#endif
21654
Bram Moolenaar493c1782014-05-28 14:34:46 +020021655 if (curswantp != NULL)
21656 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21657
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021658 return OK;
21659}
21660
21661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 * Get the length of an environment variable name.
21663 * Advance "arg" to the first character after the name.
21664 * Return 0 for error.
21665 */
21666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021667get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668{
21669 char_u *p;
21670 int len;
21671
21672 for (p = *arg; vim_isIDc(*p); ++p)
21673 ;
21674 if (p == *arg) /* no name found */
21675 return 0;
21676
21677 len = (int)(p - *arg);
21678 *arg = p;
21679 return len;
21680}
21681
21682/*
21683 * Get the length of the name of a function or internal variable.
21684 * "arg" is advanced to the first non-white character after the name.
21685 * Return 0 if something is wrong.
21686 */
21687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021688get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689{
21690 char_u *p;
21691 int len;
21692
21693 /* Find the end of the name. */
21694 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021695 {
21696 if (*p == ':')
21697 {
21698 /* "s:" is start of "s:var", but "n:" is not and can be used in
21699 * slice "[n:]". Also "xx:" is not a namespace. */
21700 len = (int)(p - *arg);
21701 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21702 || len > 1)
21703 break;
21704 }
21705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 if (p == *arg) /* no name found */
21707 return 0;
21708
21709 len = (int)(p - *arg);
21710 *arg = skipwhite(p);
21711
21712 return len;
21713}
21714
21715/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021716 * Get the length of the name of a variable or function.
21717 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021719 * Return -1 if curly braces expansion failed.
21720 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 * If the name contains 'magic' {}'s, expand them and return the
21722 * expanded name in an allocated string via 'alias' - caller must free.
21723 */
21724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021725get_name_len(
21726 char_u **arg,
21727 char_u **alias,
21728 int evaluate,
21729 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730{
21731 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 char_u *p;
21733 char_u *expr_start;
21734 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735
21736 *alias = NULL; /* default to no alias */
21737
21738 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21739 && (*arg)[2] == (int)KE_SNR)
21740 {
21741 /* hard coded <SNR>, already translated */
21742 *arg += 3;
21743 return get_id_len(arg) + 3;
21744 }
21745 len = eval_fname_script(*arg);
21746 if (len > 0)
21747 {
21748 /* literal "<SID>", "s:" or "<SNR>" */
21749 *arg += len;
21750 }
21751
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021753 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021755 p = find_name_end(*arg, &expr_start, &expr_end,
21756 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 if (expr_start != NULL)
21758 {
21759 char_u *temp_string;
21760
21761 if (!evaluate)
21762 {
21763 len += (int)(p - *arg);
21764 *arg = skipwhite(p);
21765 return len;
21766 }
21767
21768 /*
21769 * Include any <SID> etc in the expanded string:
21770 * Thus the -len here.
21771 */
21772 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21773 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021774 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 *alias = temp_string;
21776 *arg = skipwhite(p);
21777 return (int)STRLEN(temp_string);
21778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779
21780 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021781 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 EMSG2(_(e_invexpr2), *arg);
21783
21784 return len;
21785}
21786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021787/*
21788 * Find the end of a variable or function name, taking care of magic braces.
21789 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21790 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021791 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021792 * Return a pointer to just after the name. Equal to "arg" if there is no
21793 * valid name.
21794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021796find_name_end(
21797 char_u *arg,
21798 char_u **expr_start,
21799 char_u **expr_end,
21800 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021802 int mb_nest = 0;
21803 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021805 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021807 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021809 *expr_start = NULL;
21810 *expr_end = NULL;
21811 }
21812
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021813 /* Quick check for valid starting character. */
21814 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21815 return arg;
21816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021817 for (p = arg; *p != NUL
21818 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021819 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021820 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021821 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021822 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021823 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021824 if (*p == '\'')
21825 {
21826 /* skip over 'string' to avoid counting [ and ] inside it. */
21827 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21828 ;
21829 if (*p == NUL)
21830 break;
21831 }
21832 else if (*p == '"')
21833 {
21834 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21835 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21836 if (*p == '\\' && p[1] != NUL)
21837 ++p;
21838 if (*p == NUL)
21839 break;
21840 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021841 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21842 {
21843 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021844 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021845 len = (int)(p - arg);
21846 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021847 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021848 break;
21849 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021851 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021853 if (*p == '[')
21854 ++br_nest;
21855 else if (*p == ']')
21856 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021859 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021861 if (*p == '{')
21862 {
21863 mb_nest++;
21864 if (expr_start != NULL && *expr_start == NULL)
21865 *expr_start = p;
21866 }
21867 else if (*p == '}')
21868 {
21869 mb_nest--;
21870 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21871 *expr_end = p;
21872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 }
21875
21876 return p;
21877}
21878
21879/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021880 * Expands out the 'magic' {}'s in a variable/function name.
21881 * Note that this can call itself recursively, to deal with
21882 * constructs like foo{bar}{baz}{bam}
21883 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21884 * "in_start" ^
21885 * "expr_start" ^
21886 * "expr_end" ^
21887 * "in_end" ^
21888 *
21889 * Returns a new allocated string, which the caller must free.
21890 * Returns NULL for failure.
21891 */
21892 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021893make_expanded_name(
21894 char_u *in_start,
21895 char_u *expr_start,
21896 char_u *expr_end,
21897 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021898{
21899 char_u c1;
21900 char_u *retval = NULL;
21901 char_u *temp_result;
21902 char_u *nextcmd = NULL;
21903
21904 if (expr_end == NULL || in_end == NULL)
21905 return NULL;
21906 *expr_start = NUL;
21907 *expr_end = NUL;
21908 c1 = *in_end;
21909 *in_end = NUL;
21910
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021911 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021912 if (temp_result != NULL && nextcmd == NULL)
21913 {
21914 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21915 + (in_end - expr_end) + 1));
21916 if (retval != NULL)
21917 {
21918 STRCPY(retval, in_start);
21919 STRCAT(retval, temp_result);
21920 STRCAT(retval, expr_end + 1);
21921 }
21922 }
21923 vim_free(temp_result);
21924
21925 *in_end = c1; /* put char back for error messages */
21926 *expr_start = '{';
21927 *expr_end = '}';
21928
21929 if (retval != NULL)
21930 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021931 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021932 if (expr_start != NULL)
21933 {
21934 /* Further expansion! */
21935 temp_result = make_expanded_name(retval, expr_start,
21936 expr_end, temp_result);
21937 vim_free(retval);
21938 retval = temp_result;
21939 }
21940 }
21941
21942 return retval;
21943}
21944
21945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021947 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 */
21949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021950eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021952 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21953}
21954
21955/*
21956 * Return TRUE if character "c" can be used as the first character in a
21957 * variable or function name (excluding '{' and '}').
21958 */
21959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021960eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021961{
21962 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963}
21964
21965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 * Set number v: variable to "val".
21967 */
21968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021969set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021971 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972}
21973
21974/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021975 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 */
21977 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021978get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021980 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981}
21982
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021983/*
21984 * Get string v: variable value. Uses a static buffer, can only be used once.
21985 */
21986 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021987get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021988{
21989 return get_tv_string(&vimvars[idx].vv_tv);
21990}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021991
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021993 * Get List v: variable value. Caller must take care of reference count when
21994 * needed.
21995 */
21996 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021997get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021998{
21999 return vimvars[idx].vv_list;
22000}
22001
22002/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022003 * Set v:char to character "c".
22004 */
22005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022006set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022007{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022008 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022009
22010#ifdef FEAT_MBYTE
22011 if (has_mbyte)
22012 buf[(*mb_char2bytes)(c, buf)] = NUL;
22013 else
22014#endif
22015 {
22016 buf[0] = c;
22017 buf[1] = NUL;
22018 }
22019 set_vim_var_string(VV_CHAR, buf, -1);
22020}
22021
22022/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022023 * Set v:count to "count" and v:count1 to "count1".
22024 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 */
22026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022027set_vcount(
22028 long count,
22029 long count1,
22030 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022032 if (set_prevcount)
22033 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022034 vimvars[VV_COUNT].vv_nr = count;
22035 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036}
22037
22038/*
22039 * Set string v: variable to a copy of "val".
22040 */
22041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022042set_vim_var_string(
22043 int idx,
22044 char_u *val,
22045 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046{
Bram Moolenaara542c682016-01-31 16:28:04 +010022047 clear_tv(&vimvars[idx].vv_di.di_tv);
22048 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022050 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022052 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022054 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055}
22056
22057/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022058 * Set List v: variable to "val".
22059 */
22060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022061set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022062{
Bram Moolenaara542c682016-01-31 16:28:04 +010022063 clear_tv(&vimvars[idx].vv_di.di_tv);
22064 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022065 vimvars[idx].vv_list = val;
22066 if (val != NULL)
22067 ++val->lv_refcount;
22068}
22069
22070/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022071 * Set Dictionary v: variable to "val".
22072 */
22073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022074set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022075{
22076 int todo;
22077 hashitem_T *hi;
22078
Bram Moolenaara542c682016-01-31 16:28:04 +010022079 clear_tv(&vimvars[idx].vv_di.di_tv);
22080 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022081 vimvars[idx].vv_dict = val;
22082 if (val != NULL)
22083 {
22084 ++val->dv_refcount;
22085
22086 /* Set readonly */
22087 todo = (int)val->dv_hashtab.ht_used;
22088 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22089 {
22090 if (HASHITEM_EMPTY(hi))
22091 continue;
22092 --todo;
22093 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22094 }
22095 }
22096}
22097
22098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 * Set v:register if needed.
22100 */
22101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022102set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103{
22104 char_u regname;
22105
22106 if (c == 0 || c == ' ')
22107 regname = '"';
22108 else
22109 regname = c;
22110 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022111 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 set_vim_var_string(VV_REG, &regname, 1);
22113}
22114
22115/*
22116 * Get or set v:exception. If "oldval" == NULL, return the current value.
22117 * Otherwise, restore the value to "oldval" and return NULL.
22118 * Must always be called in pairs to save and restore v:exception! Does not
22119 * take care of memory allocations.
22120 */
22121 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022122v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123{
22124 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022125 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126
Bram Moolenaare9a41262005-01-15 22:18:47 +000022127 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 return NULL;
22129}
22130
22131/*
22132 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22133 * Otherwise, restore the value to "oldval" and return NULL.
22134 * Must always be called in pairs to save and restore v:throwpoint! Does not
22135 * take care of memory allocations.
22136 */
22137 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022138v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139{
22140 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022141 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142
Bram Moolenaare9a41262005-01-15 22:18:47 +000022143 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 return NULL;
22145}
22146
22147#if defined(FEAT_AUTOCMD) || defined(PROTO)
22148/*
22149 * Set v:cmdarg.
22150 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22151 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22152 * Must always be called in pairs!
22153 */
22154 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022155set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156{
22157 char_u *oldval;
22158 char_u *newval;
22159 unsigned len;
22160
Bram Moolenaare9a41262005-01-15 22:18:47 +000022161 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022162 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022164 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022165 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022166 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 }
22168
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022169 if (eap->force_bin == FORCE_BIN)
22170 len = 6;
22171 else if (eap->force_bin == FORCE_NOBIN)
22172 len = 8;
22173 else
22174 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022175
22176 if (eap->read_edit)
22177 len += 7;
22178
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022179 if (eap->force_ff != 0)
22180 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22181# ifdef FEAT_MBYTE
22182 if (eap->force_enc != 0)
22183 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022184 if (eap->bad_char != 0)
22185 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022186# endif
22187
22188 newval = alloc(len + 1);
22189 if (newval == NULL)
22190 return NULL;
22191
22192 if (eap->force_bin == FORCE_BIN)
22193 sprintf((char *)newval, " ++bin");
22194 else if (eap->force_bin == FORCE_NOBIN)
22195 sprintf((char *)newval, " ++nobin");
22196 else
22197 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022198
22199 if (eap->read_edit)
22200 STRCAT(newval, " ++edit");
22201
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022202 if (eap->force_ff != 0)
22203 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22204 eap->cmd + eap->force_ff);
22205# ifdef FEAT_MBYTE
22206 if (eap->force_enc != 0)
22207 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22208 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022209 if (eap->bad_char == BAD_KEEP)
22210 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22211 else if (eap->bad_char == BAD_DROP)
22212 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22213 else if (eap->bad_char != 0)
22214 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022215# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022216 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022217 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218}
22219#endif
22220
22221/*
22222 * Get the value of internal variable "name".
22223 * Return OK or FAIL.
22224 */
22225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022226get_var_tv(
22227 char_u *name,
22228 int len, /* length of "name" */
22229 typval_T *rettv, /* NULL when only checking existence */
22230 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22231 int verbose, /* may give error message */
22232 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233{
22234 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022235 typval_T *tv = NULL;
22236 typval_T atv;
22237 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239
22240 /* truncate the name, so that we can use strcmp() */
22241 cc = name[len];
22242 name[len] = NUL;
22243
22244 /*
22245 * Check for "b:changedtick".
22246 */
22247 if (STRCMP(name, "b:changedtick") == 0)
22248 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022249 atv.v_type = VAR_NUMBER;
22250 atv.vval.v_number = curbuf->b_changedtick;
22251 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252 }
22253
22254 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 * Check for user-defined variables.
22256 */
22257 else
22258 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022259 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022261 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022262 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022263 if (dip != NULL)
22264 *dip = v;
22265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266 }
22267
Bram Moolenaare9a41262005-01-15 22:18:47 +000022268 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022270 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022271 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 ret = FAIL;
22273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022274 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022275 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276
22277 name[len] = cc;
22278
22279 return ret;
22280}
22281
22282/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022283 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22284 * Also handle function call with Funcref variable: func(expr)
22285 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22286 */
22287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022288handle_subscript(
22289 char_u **arg,
22290 typval_T *rettv,
22291 int evaluate, /* do more than finding the end */
22292 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022293{
22294 int ret = OK;
22295 dict_T *selfdict = NULL;
22296 char_u *s;
22297 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022298 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022299
22300 while (ret == OK
22301 && (**arg == '['
22302 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022303 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22304 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022305 && !vim_iswhite(*(*arg - 1)))
22306 {
22307 if (**arg == '(')
22308 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022309 partial_T *pt = NULL;
22310
Bram Moolenaard9fba312005-06-26 22:34:35 +000022311 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022312 if (evaluate)
22313 {
22314 functv = *rettv;
22315 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022316
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022317 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022318 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022319 {
22320 pt = functv.vval.v_partial;
22321 s = pt->pt_name;
22322 }
22323 else
22324 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022325 }
22326 else
22327 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022328 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022329 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022330 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022331
22332 /* Clear the funcref afterwards, so that deleting it while
22333 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022334 if (evaluate)
22335 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022336
22337 /* Stop the expression evaluation when immediately aborting on
22338 * error, or when an interrupt occurred or an exception was thrown
22339 * but not caught. */
22340 if (aborting())
22341 {
22342 if (ret == OK)
22343 clear_tv(rettv);
22344 ret = FAIL;
22345 }
22346 dict_unref(selfdict);
22347 selfdict = NULL;
22348 }
22349 else /* **arg == '[' || **arg == '.' */
22350 {
22351 dict_unref(selfdict);
22352 if (rettv->v_type == VAR_DICT)
22353 {
22354 selfdict = rettv->vval.v_dict;
22355 if (selfdict != NULL)
22356 ++selfdict->dv_refcount;
22357 }
22358 else
22359 selfdict = NULL;
22360 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22361 {
22362 clear_tv(rettv);
22363 ret = FAIL;
22364 }
22365 }
22366 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022367
Bram Moolenaar1d429612016-05-24 15:44:17 +020022368 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22369 * Don't do this when "Func" is already a partial that was bound
22370 * explicitly (pt_auto is FALSE). */
22371 if (selfdict != NULL
22372 && (rettv->v_type == VAR_FUNC
22373 || (rettv->v_type == VAR_PARTIAL
22374 && (rettv->vval.v_partial->pt_auto
22375 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022376 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022377 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22378 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022379 char_u *tofree = NULL;
22380 ufunc_T *fp;
22381 char_u fname_buf[FLEN_FIXED + 1];
22382 int error;
22383
22384 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022385 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022386 fp = find_func(fname);
22387 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022388
Bram Moolenaar65639032016-03-16 21:40:30 +010022389 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022390 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022391 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22392
22393 if (pt != NULL)
22394 {
22395 pt->pt_refcount = 1;
22396 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022397 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022398 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022399 if (rettv->v_type == VAR_FUNC)
22400 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022401 /* Just a function: Take over the function name and use
22402 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022403 pt->pt_name = rettv->vval.v_string;
22404 }
22405 else
22406 {
22407 partial_T *ret_pt = rettv->vval.v_partial;
22408 int i;
22409
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022410 /* Partial: copy the function name, use selfdict and copy
22411 * args. Can't take over name or args, the partial might
22412 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022413 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022414 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022415 if (ret_pt->pt_argc > 0)
22416 {
22417 pt->pt_argv = (typval_T *)alloc(
22418 sizeof(typval_T) * ret_pt->pt_argc);
22419 if (pt->pt_argv == NULL)
22420 /* out of memory: drop the arguments */
22421 pt->pt_argc = 0;
22422 else
22423 {
22424 pt->pt_argc = ret_pt->pt_argc;
22425 for (i = 0; i < pt->pt_argc; i++)
22426 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22427 }
22428 }
22429 partial_unref(ret_pt);
22430 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022431 rettv->v_type = VAR_PARTIAL;
22432 rettv->vval.v_partial = pt;
22433 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022434 }
22435 }
22436
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022437 dict_unref(selfdict);
22438 return ret;
22439}
22440
22441/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022442 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022443 * value).
22444 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022445 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022446alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022447{
Bram Moolenaar33570922005-01-25 22:26:29 +000022448 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022449}
22450
22451/*
22452 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 * The string "s" must have been allocated, it is consumed.
22454 * Return NULL for out of memory, the variable otherwise.
22455 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022457alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458{
Bram Moolenaar33570922005-01-25 22:26:29 +000022459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022461 rettv = alloc_tv();
22462 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022464 rettv->v_type = VAR_STRING;
22465 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 }
22467 else
22468 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022469 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470}
22471
22472/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022473 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022476free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477{
22478 if (varp != NULL)
22479 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022480 switch (varp->v_type)
22481 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022482 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022483 func_unref(varp->vval.v_string);
22484 /*FALLTHROUGH*/
22485 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022486 vim_free(varp->vval.v_string);
22487 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022488 case VAR_PARTIAL:
22489 partial_unref(varp->vval.v_partial);
22490 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022491 case VAR_LIST:
22492 list_unref(varp->vval.v_list);
22493 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022494 case VAR_DICT:
22495 dict_unref(varp->vval.v_dict);
22496 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022497 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022499 job_unref(varp->vval.v_job);
22500 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022501#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022502 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022504 channel_unref(varp->vval.v_channel);
22505 break;
22506#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022507 case VAR_NUMBER:
22508 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022509 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022510 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022511 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 vim_free(varp);
22514 }
22515}
22516
22517/*
22518 * Free the memory for a variable value and set the value to NULL or 0.
22519 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022521clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522{
22523 if (varp != NULL)
22524 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022525 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022527 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022528 func_unref(varp->vval.v_string);
22529 /*FALLTHROUGH*/
22530 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022531 vim_free(varp->vval.v_string);
22532 varp->vval.v_string = NULL;
22533 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022534 case VAR_PARTIAL:
22535 partial_unref(varp->vval.v_partial);
22536 varp->vval.v_partial = NULL;
22537 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022538 case VAR_LIST:
22539 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022540 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022541 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022542 case VAR_DICT:
22543 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022544 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022545 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022546 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022547 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022548 varp->vval.v_number = 0;
22549 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022550 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022551#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022552 varp->vval.v_float = 0.0;
22553 break;
22554#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022555 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022556#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022557 job_unref(varp->vval.v_job);
22558 varp->vval.v_job = NULL;
22559#endif
22560 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022561 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022562#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022563 channel_unref(varp->vval.v_channel);
22564 varp->vval.v_channel = NULL;
22565#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022566 case VAR_UNKNOWN:
22567 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022569 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 }
22571}
22572
22573/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022574 * Set the value of a variable to NULL without freeing items.
22575 */
22576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022577init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022578{
22579 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022580 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022581}
22582
22583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 * Get the number value of a variable.
22585 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022586 * For incompatible types, return 0.
22587 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22588 * caller of incompatible types: it sets *denote to TRUE if "denote"
22589 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022591 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022592get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022594 int error = FALSE;
22595
22596 return get_tv_number_chk(varp, &error); /* return 0L on error */
22597}
22598
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022599 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022600get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022601{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022602 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022604 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022606 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022607 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022608 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022609#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022610 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022611 break;
22612#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022613 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022614 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022615 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022616 break;
22617 case VAR_STRING:
22618 if (varp->vval.v_string != NULL)
22619 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022620 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022621 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022622 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022623 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022624 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022625 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022626 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022627 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022628 case VAR_SPECIAL:
22629 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22630 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022631 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022632#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022633 EMSG(_("E910: Using a Job as a Number"));
22634 break;
22635#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022636 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022637#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022638 EMSG(_("E913: Using a Channel as a Number"));
22639 break;
22640#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022641 case VAR_UNKNOWN:
22642 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022643 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022645 if (denote == NULL) /* useful for values that must be unsigned */
22646 n = -1;
22647 else
22648 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022649 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650}
22651
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022652#ifdef FEAT_FLOAT
22653 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022654get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022655{
22656 switch (varp->v_type)
22657 {
22658 case VAR_NUMBER:
22659 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022660 case VAR_FLOAT:
22661 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022662 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022663 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022664 EMSG(_("E891: Using a Funcref as a Float"));
22665 break;
22666 case VAR_STRING:
22667 EMSG(_("E892: Using a String as a Float"));
22668 break;
22669 case VAR_LIST:
22670 EMSG(_("E893: Using a List as a Float"));
22671 break;
22672 case VAR_DICT:
22673 EMSG(_("E894: Using a Dictionary as a Float"));
22674 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022675 case VAR_SPECIAL:
22676 EMSG(_("E907: Using a special value as a Float"));
22677 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022678 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022679# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022680 EMSG(_("E911: Using a Job as a Float"));
22681 break;
22682# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022683 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022684# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022685 EMSG(_("E914: Using a Channel as a Float"));
22686 break;
22687# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022688 case VAR_UNKNOWN:
22689 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022690 break;
22691 }
22692 return 0;
22693}
22694#endif
22695
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022697 * Get the lnum from the first argument.
22698 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022699 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 */
22701 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022702get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703{
Bram Moolenaar33570922005-01-25 22:26:29 +000022704 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 linenr_T lnum;
22706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022707 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 if (lnum == 0) /* no valid number, try using line() */
22709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022710 rettv.v_type = VAR_NUMBER;
22711 f_line(argvars, &rettv);
22712 lnum = rettv.vval.v_number;
22713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 }
22715 return lnum;
22716}
22717
22718/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022719 * Get the lnum from the first argument.
22720 * Also accepts "$", then "buf" is used.
22721 * Returns 0 on error.
22722 */
22723 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022724get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022725{
22726 if (argvars[0].v_type == VAR_STRING
22727 && argvars[0].vval.v_string != NULL
22728 && argvars[0].vval.v_string[0] == '$'
22729 && buf != NULL)
22730 return buf->b_ml.ml_line_count;
22731 return get_tv_number_chk(&argvars[0], NULL);
22732}
22733
22734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 * Get the string value of a variable.
22736 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022737 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22738 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 * If the String variable has never been set, return an empty string.
22740 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022741 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22742 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022744 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022745get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746{
22747 static char_u mybuf[NUMBUFLEN];
22748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022749 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750}
22751
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022752 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022753get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022755 char_u *res = get_tv_string_buf_chk(varp, buf);
22756
22757 return res != NULL ? res : (char_u *)"";
22758}
22759
Bram Moolenaar7d647822014-04-05 21:28:56 +020022760/*
22761 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22762 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022763 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022764get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022765{
22766 static char_u mybuf[NUMBUFLEN];
22767
22768 return get_tv_string_buf_chk(varp, mybuf);
22769}
22770
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022771 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022772get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022773{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022774 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022776 case VAR_NUMBER:
22777 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22778 return buf;
22779 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022780 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022781 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022782 break;
22783 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022784 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022785 break;
22786 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022787 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022788 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022789 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022790#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022791 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022792 break;
22793#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022794 case VAR_STRING:
22795 if (varp->vval.v_string != NULL)
22796 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022797 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022798 case VAR_SPECIAL:
22799 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22800 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022801 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022802#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022803 {
22804 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022805 char *status;
22806
22807 if (job == NULL)
22808 return (char_u *)"no process";
22809 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022810 : job->jv_status == JOB_ENDED ? "dead"
22811 : "run";
22812# ifdef UNIX
22813 vim_snprintf((char *)buf, NUMBUFLEN,
22814 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022815# elif defined(WIN32)
22816 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022817 "process %ld %s",
22818 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022819 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022820# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022821 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022822 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22823# endif
22824 return buf;
22825 }
22826#endif
22827 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022828 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022829#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022830 {
22831 channel_T *channel = varp->vval.v_channel;
22832 char *status = channel_status(channel);
22833
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022834 if (channel == NULL)
22835 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22836 else
22837 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022838 "channel %d %s", channel->ch_id, status);
22839 return buf;
22840 }
22841#endif
22842 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022843 case VAR_UNKNOWN:
22844 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022847 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848}
22849
22850/*
22851 * Find variable "name" in the list of variables.
22852 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022853 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022854 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022855 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022857 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022858find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022861 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862
Bram Moolenaara7043832005-01-21 11:56:39 +000022863 ht = find_var_ht(name, &varname);
22864 if (htp != NULL)
22865 *htp = ht;
22866 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022868 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869}
22870
22871/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022872 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022873 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022875 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022876find_var_in_ht(
22877 hashtab_T *ht,
22878 int htname,
22879 char_u *varname,
22880 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022881{
Bram Moolenaar33570922005-01-25 22:26:29 +000022882 hashitem_T *hi;
22883
22884 if (*varname == NUL)
22885 {
22886 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022887 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022888 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022889 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 case 'g': return &globvars_var;
22891 case 'v': return &vimvars_var;
22892 case 'b': return &curbuf->b_bufvar;
22893 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022894#ifdef FEAT_WINDOWS
22895 case 't': return &curtab->tp_winvar;
22896#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022897 case 'l': return current_funccal == NULL
22898 ? NULL : &current_funccal->l_vars_var;
22899 case 'a': return current_funccal == NULL
22900 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022901 }
22902 return NULL;
22903 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022904
22905 hi = hash_find(ht, varname);
22906 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022907 {
22908 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022909 * worked find the variable again. Don't auto-load a script if it was
22910 * loaded already, otherwise it would be loaded every time when
22911 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022912 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022913 {
22914 /* Note: script_autoload() may make "hi" invalid. It must either
22915 * be obtained again or not used. */
22916 if (!script_autoload(varname, FALSE) || aborting())
22917 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022918 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022919 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022920 if (HASHITEM_EMPTY(hi))
22921 return NULL;
22922 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022923 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022924}
22925
22926/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022927 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022928 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022929 * Set "varname" to the start of name without ':'.
22930 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022931 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022932find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022934 hashitem_T *hi;
22935
Bram Moolenaar73627d02015-08-11 15:46:09 +020022936 if (name[0] == NUL)
22937 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 if (name[1] != ':')
22939 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022940 /* The name must not start with a colon or #. */
22941 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 return NULL;
22943 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022944
22945 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022946 hi = hash_find(&compat_hashtab, name);
22947 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022948 return &compat_hashtab;
22949
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022951 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022952 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 }
22954 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022955 if (*name == 'g') /* global variable */
22956 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022957 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22958 */
22959 if (vim_strchr(name + 2, ':') != NULL
22960 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022961 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022963 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022965 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022966#ifdef FEAT_WINDOWS
22967 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022968 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022969#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022970 if (*name == 'v') /* v: variable */
22971 return &vimvarht;
22972 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022973 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022974 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022975 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 if (*name == 's' /* script variable */
22977 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22978 return &SCRIPT_VARS(current_SID);
22979 return NULL;
22980}
22981
22982/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022983 * Get function call environment based on bactrace debug level
22984 */
22985 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022986get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022987{
22988 int i;
22989 funccall_T *funccal;
22990 funccall_T *temp_funccal;
22991
22992 funccal = current_funccal;
22993 if (debug_backtrace_level > 0)
22994 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022995 for (i = 0; i < debug_backtrace_level; i++)
22996 {
22997 temp_funccal = funccal->caller;
22998 if (temp_funccal)
22999 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023000 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023001 /* backtrace level overflow. reset to max */
23002 debug_backtrace_level = i;
23003 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023004 }
23005 return funccal;
23006}
23007
23008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023010 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011 * Returns NULL when it doesn't exist.
23012 */
23013 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023014get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015{
Bram Moolenaar33570922005-01-25 22:26:29 +000023016 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023018 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 if (v == NULL)
23020 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022}
23023
23024/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 * sourcing this script and when executing functions defined in the script.
23027 */
23028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023029new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030{
Bram Moolenaara7043832005-01-21 11:56:39 +000023031 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023032 hashtab_T *ht;
23033 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023034
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23036 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023037 /* Re-allocating ga_data means that an ht_array pointing to
23038 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023039 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023040 for (i = 1; i <= ga_scripts.ga_len; ++i)
23041 {
23042 ht = &SCRIPT_VARS(i);
23043 if (ht->ht_mask == HT_INIT_SIZE - 1)
23044 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023045 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023046 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023047 }
23048
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 while (ga_scripts.ga_len < id)
23050 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023051 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023052 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023053 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 }
23056 }
23057}
23058
23059/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023060 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23061 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 */
23063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023064init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065{
Bram Moolenaar33570922005-01-25 22:26:29 +000023066 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023067 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023068 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023069 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023070 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023071 dict_var->di_tv.vval.v_dict = dict;
23072 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023073 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023074 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23075 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076}
23077
23078/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023079 * Unreference a dictionary initialized by init_var_dict().
23080 */
23081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023082unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023083{
23084 /* Now the dict needs to be freed if no one else is using it, go back to
23085 * normal reference counting. */
23086 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23087 dict_unref(dict);
23088}
23089
23090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023092 * Frees all allocated variables and the value they contain.
23093 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 */
23095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023096vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023097{
23098 vars_clear_ext(ht, TRUE);
23099}
23100
23101/*
23102 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23103 */
23104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023105vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106{
Bram Moolenaara7043832005-01-21 11:56:39 +000023107 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023108 hashitem_T *hi;
23109 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110
Bram Moolenaar33570922005-01-25 22:26:29 +000023111 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023112 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023113 for (hi = ht->ht_array; todo > 0; ++hi)
23114 {
23115 if (!HASHITEM_EMPTY(hi))
23116 {
23117 --todo;
23118
Bram Moolenaar33570922005-01-25 22:26:29 +000023119 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023120 * ht_array might change then. hash_clear() takes care of it
23121 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023122 v = HI2DI(hi);
23123 if (free_val)
23124 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023125 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023126 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023127 }
23128 }
23129 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023130 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131}
23132
Bram Moolenaara7043832005-01-21 11:56:39 +000023133/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023134 * Delete a variable from hashtab "ht" at item "hi".
23135 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023138delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139{
Bram Moolenaar33570922005-01-25 22:26:29 +000023140 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023141
23142 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023143 clear_tv(&di->di_tv);
23144 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145}
23146
23147/*
23148 * List the value of one internal variable.
23149 */
23150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023151list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023153 char_u *tofree;
23154 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023155 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023156
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023157 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023158 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023159 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023160 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161}
23162
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023164list_one_var_a(
23165 char_u *prefix,
23166 char_u *name,
23167 int type,
23168 char_u *string,
23169 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170{
Bram Moolenaar31859182007-08-14 20:41:13 +000023171 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23172 msg_start();
23173 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 if (name != NULL) /* "a:" vars don't have a name stored */
23175 msg_puts(name);
23176 msg_putchar(' ');
23177 msg_advance(22);
23178 if (type == VAR_NUMBER)
23179 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023180 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023181 msg_putchar('*');
23182 else if (type == VAR_LIST)
23183 {
23184 msg_putchar('[');
23185 if (*string == '[')
23186 ++string;
23187 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023188 else if (type == VAR_DICT)
23189 {
23190 msg_putchar('{');
23191 if (*string == '{')
23192 ++string;
23193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 else
23195 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023196
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023198
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023199 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023200 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023201 if (*first)
23202 {
23203 msg_clr_eos();
23204 *first = FALSE;
23205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206}
23207
23208/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023209 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 * If the variable already exists, the value is updated.
23211 * Otherwise the variable is created.
23212 */
23213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023214set_var(
23215 char_u *name,
23216 typval_T *tv,
23217 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218{
Bram Moolenaar33570922005-01-25 22:26:29 +000023219 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023221 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023223 ht = find_var_ht(name, &varname);
23224 if (ht == NULL || *varname == NUL)
23225 {
23226 EMSG2(_(e_illvar), name);
23227 return;
23228 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023229 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023230
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023231 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23232 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023233 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023234
Bram Moolenaar33570922005-01-25 22:26:29 +000023235 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023237 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023238 if (var_check_ro(v->di_flags, name, FALSE)
23239 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023240 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023241
23242 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023243 * Handle setting internal v: variables separately where needed to
23244 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023245 */
23246 if (ht == &vimvarht)
23247 {
23248 if (v->di_tv.v_type == VAR_STRING)
23249 {
23250 vim_free(v->di_tv.vval.v_string);
23251 if (copy || tv->v_type != VAR_STRING)
23252 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23253 else
23254 {
23255 /* Take over the string to avoid an extra alloc/free. */
23256 v->di_tv.vval.v_string = tv->vval.v_string;
23257 tv->vval.v_string = NULL;
23258 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023259 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023260 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023261 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023262 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023263 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023264 if (STRCMP(varname, "searchforward") == 0)
23265 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023266#ifdef FEAT_SEARCH_EXTRA
23267 else if (STRCMP(varname, "hlsearch") == 0)
23268 {
23269 no_hlsearch = !v->di_tv.vval.v_number;
23270 redraw_all_later(SOME_VALID);
23271 }
23272#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023273 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023274 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023275 else if (v->di_tv.v_type != tv->v_type)
23276 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023277 }
23278
23279 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 }
23281 else /* add a new variable */
23282 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023283 /* Can't add "v:" variable. */
23284 if (ht == &vimvarht)
23285 {
23286 EMSG2(_(e_illvar), name);
23287 return;
23288 }
23289
Bram Moolenaar92124a32005-06-17 22:03:40 +000023290 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023291 if (!valid_varname(varname))
23292 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023293
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023294 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23295 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023296 if (v == NULL)
23297 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023298 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023299 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023301 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 return;
23303 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023304 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023306
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023307 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023308 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023309 else
23310 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023311 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023312 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023313 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315}
23316
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023317/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023318 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023319 * Also give an error message.
23320 */
23321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023322var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023323{
23324 if (flags & DI_FLAGS_RO)
23325 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023326 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023327 return TRUE;
23328 }
23329 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23330 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023331 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023332 return TRUE;
23333 }
23334 return FALSE;
23335}
23336
23337/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023338 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23339 * Also give an error message.
23340 */
23341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023342var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023343{
23344 if (flags & DI_FLAGS_FIX)
23345 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023346 EMSG2(_("E795: Cannot delete variable %s"),
23347 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023348 return TRUE;
23349 }
23350 return FALSE;
23351}
23352
23353/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023354 * Check if a funcref is assigned to a valid variable name.
23355 * Return TRUE and give an error if not.
23356 */
23357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023358var_check_func_name(
23359 char_u *name, /* points to start of variable name */
23360 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023361{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023362 /* Allow for w: b: s: and t:. */
23363 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023364 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23365 ? name[2] : name[0]))
23366 {
23367 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23368 name);
23369 return TRUE;
23370 }
23371 /* Don't allow hiding a function. When "v" is not NULL we might be
23372 * assigning another function to the same var, the type is checked
23373 * below. */
23374 if (new_var && function_exists(name))
23375 {
23376 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23377 name);
23378 return TRUE;
23379 }
23380 return FALSE;
23381}
23382
23383/*
23384 * Check if a variable name is valid.
23385 * Return FALSE and give an error if not.
23386 */
23387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023388valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023389{
23390 char_u *p;
23391
23392 for (p = varname; *p != NUL; ++p)
23393 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23394 && *p != AUTOLOAD_CHAR)
23395 {
23396 EMSG2(_(e_illvar), varname);
23397 return FALSE;
23398 }
23399 return TRUE;
23400}
23401
23402/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023403 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023404 * Also give an error message, using "name" or _("name") when use_gettext is
23405 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023406 */
23407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023408tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023409{
23410 if (lock & VAR_LOCKED)
23411 {
23412 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023413 name == NULL ? (char_u *)_("Unknown")
23414 : use_gettext ? (char_u *)_(name)
23415 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023416 return TRUE;
23417 }
23418 if (lock & VAR_FIXED)
23419 {
23420 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023421 name == NULL ? (char_u *)_("Unknown")
23422 : use_gettext ? (char_u *)_(name)
23423 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023424 return TRUE;
23425 }
23426 return FALSE;
23427}
23428
23429/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023430 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023431 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023432 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023433 * It is OK for "from" and "to" to point to the same item. This is used to
23434 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023435 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023437copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023439 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023440 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023441 switch (from->v_type)
23442 {
23443 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023444 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023445 to->vval.v_number = from->vval.v_number;
23446 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023447 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023448#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023449 to->vval.v_float = from->vval.v_float;
23450 break;
23451#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023452 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023453#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023454 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023455 if (to->vval.v_job != NULL)
23456 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023457 break;
23458#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023459 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023460#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023461 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023462 if (to->vval.v_channel != NULL)
23463 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023464 break;
23465#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023466 case VAR_STRING:
23467 case VAR_FUNC:
23468 if (from->vval.v_string == NULL)
23469 to->vval.v_string = NULL;
23470 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023472 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023473 if (from->v_type == VAR_FUNC)
23474 func_ref(to->vval.v_string);
23475 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023476 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023477 case VAR_PARTIAL:
23478 if (from->vval.v_partial == NULL)
23479 to->vval.v_partial = NULL;
23480 else
23481 {
23482 to->vval.v_partial = from->vval.v_partial;
23483 ++to->vval.v_partial->pt_refcount;
23484 }
23485 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023486 case VAR_LIST:
23487 if (from->vval.v_list == NULL)
23488 to->vval.v_list = NULL;
23489 else
23490 {
23491 to->vval.v_list = from->vval.v_list;
23492 ++to->vval.v_list->lv_refcount;
23493 }
23494 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023495 case VAR_DICT:
23496 if (from->vval.v_dict == NULL)
23497 to->vval.v_dict = NULL;
23498 else
23499 {
23500 to->vval.v_dict = from->vval.v_dict;
23501 ++to->vval.v_dict->dv_refcount;
23502 }
23503 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023504 case VAR_UNKNOWN:
23505 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023506 break;
23507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508}
23509
23510/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023511 * Make a copy of an item.
23512 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023513 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23514 * reference to an already copied list/dict can be used.
23515 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023516 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023518item_copy(
23519 typval_T *from,
23520 typval_T *to,
23521 int deep,
23522 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023523{
23524 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023525 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023526
Bram Moolenaar33570922005-01-25 22:26:29 +000023527 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023528 {
23529 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023530 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023531 }
23532 ++recurse;
23533
23534 switch (from->v_type)
23535 {
23536 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023537 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023538 case VAR_STRING:
23539 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023540 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023541 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023542 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023543 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023544 copy_tv(from, to);
23545 break;
23546 case VAR_LIST:
23547 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023548 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023549 if (from->vval.v_list == NULL)
23550 to->vval.v_list = NULL;
23551 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23552 {
23553 /* use the copy made earlier */
23554 to->vval.v_list = from->vval.v_list->lv_copylist;
23555 ++to->vval.v_list->lv_refcount;
23556 }
23557 else
23558 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23559 if (to->vval.v_list == NULL)
23560 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023561 break;
23562 case VAR_DICT:
23563 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023564 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023565 if (from->vval.v_dict == NULL)
23566 to->vval.v_dict = NULL;
23567 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23568 {
23569 /* use the copy made earlier */
23570 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23571 ++to->vval.v_dict->dv_refcount;
23572 }
23573 else
23574 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23575 if (to->vval.v_dict == NULL)
23576 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023577 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023578 case VAR_UNKNOWN:
23579 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023580 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023581 }
23582 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023583 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023584}
23585
23586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 * ":echo expr1 ..." print each argument separated with a space, add a
23588 * newline at the end.
23589 * ":echon expr1 ..." print each argument plain.
23590 */
23591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023592ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593{
23594 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023595 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023596 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 char_u *p;
23598 int needclr = TRUE;
23599 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023600 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601
23602 if (eap->skip)
23603 ++emsg_skip;
23604 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23605 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023606 /* If eval1() causes an error message the text from the command may
23607 * still need to be cleared. E.g., "echo 22,44". */
23608 need_clr_eos = needclr;
23609
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023611 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 {
23613 /*
23614 * Report the invalid expression unless the expression evaluation
23615 * has been cancelled due to an aborting error, an interrupt, or an
23616 * exception.
23617 */
23618 if (!aborting())
23619 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023620 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 break;
23622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023623 need_clr_eos = FALSE;
23624
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 if (!eap->skip)
23626 {
23627 if (atstart)
23628 {
23629 atstart = FALSE;
23630 /* Call msg_start() after eval1(), evaluating the expression
23631 * may cause a message to appear. */
23632 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023633 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023634 /* Mark the saved text as finishing the line, so that what
23635 * follows is displayed on a new line when scrolling back
23636 * at the more prompt. */
23637 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 }
23641 else if (eap->cmdidx == CMD_echo)
23642 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023643 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023644 if (p != NULL)
23645 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023647 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023649 if (*p != TAB && needclr)
23650 {
23651 /* remove any text still there from the command */
23652 msg_clr_eos();
23653 needclr = FALSE;
23654 }
23655 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 }
23657 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023658 {
23659#ifdef FEAT_MBYTE
23660 if (has_mbyte)
23661 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023662 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023663
23664 (void)msg_outtrans_len_attr(p, i, echo_attr);
23665 p += i - 1;
23666 }
23667 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023669 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023672 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 arg = skipwhite(arg);
23676 }
23677 eap->nextcmd = check_nextcmd(arg);
23678
23679 if (eap->skip)
23680 --emsg_skip;
23681 else
23682 {
23683 /* remove text that may still be there from the command */
23684 if (needclr)
23685 msg_clr_eos();
23686 if (eap->cmdidx == CMD_echo)
23687 msg_end();
23688 }
23689}
23690
23691/*
23692 * ":echohl {name}".
23693 */
23694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023695ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696{
23697 int id;
23698
23699 id = syn_name2id(eap->arg);
23700 if (id == 0)
23701 echo_attr = 0;
23702 else
23703 echo_attr = syn_id2attr(id);
23704}
23705
23706/*
23707 * ":execute expr1 ..." execute the result of an expression.
23708 * ":echomsg expr1 ..." Print a message
23709 * ":echoerr expr1 ..." Print an error
23710 * Each gets spaces around each argument and a newline at the end for
23711 * echo commands
23712 */
23713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023714ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715{
23716 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023717 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 int ret = OK;
23719 char_u *p;
23720 garray_T ga;
23721 int len;
23722 int save_did_emsg;
23723
23724 ga_init2(&ga, 1, 80);
23725
23726 if (eap->skip)
23727 ++emsg_skip;
23728 while (*arg != NUL && *arg != '|' && *arg != '\n')
23729 {
23730 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023731 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 {
23733 /*
23734 * Report the invalid expression unless the expression evaluation
23735 * has been cancelled due to an aborting error, an interrupt, or an
23736 * exception.
23737 */
23738 if (!aborting())
23739 EMSG2(_(e_invexpr2), p);
23740 ret = FAIL;
23741 break;
23742 }
23743
23744 if (!eap->skip)
23745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023746 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 len = (int)STRLEN(p);
23748 if (ga_grow(&ga, len + 2) == FAIL)
23749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 ret = FAIL;
23752 break;
23753 }
23754 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 ga.ga_len += len;
23758 }
23759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023760 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761 arg = skipwhite(arg);
23762 }
23763
23764 if (ret != FAIL && ga.ga_data != NULL)
23765 {
23766 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023767 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023769 out_flush();
23770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 else if (eap->cmdidx == CMD_echoerr)
23772 {
23773 /* We don't want to abort following commands, restore did_emsg. */
23774 save_did_emsg = did_emsg;
23775 EMSG((char_u *)ga.ga_data);
23776 if (!force_abort)
23777 did_emsg = save_did_emsg;
23778 }
23779 else if (eap->cmdidx == CMD_execute)
23780 do_cmdline((char_u *)ga.ga_data,
23781 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23782 }
23783
23784 ga_clear(&ga);
23785
23786 if (eap->skip)
23787 --emsg_skip;
23788
23789 eap->nextcmd = check_nextcmd(arg);
23790}
23791
23792/*
23793 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23794 * "arg" points to the "&" or '+' when called, to "option" when returning.
23795 * Returns NULL when no option name found. Otherwise pointer to the char
23796 * after the option name.
23797 */
23798 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023799find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800{
23801 char_u *p = *arg;
23802
23803 ++p;
23804 if (*p == 'g' && p[1] == ':')
23805 {
23806 *opt_flags = OPT_GLOBAL;
23807 p += 2;
23808 }
23809 else if (*p == 'l' && p[1] == ':')
23810 {
23811 *opt_flags = OPT_LOCAL;
23812 p += 2;
23813 }
23814 else
23815 *opt_flags = 0;
23816
23817 if (!ASCII_ISALPHA(*p))
23818 return NULL;
23819 *arg = p;
23820
23821 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23822 p += 4; /* termcap option */
23823 else
23824 while (ASCII_ISALPHA(*p))
23825 ++p;
23826 return p;
23827}
23828
23829/*
23830 * ":function"
23831 */
23832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023833ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834{
23835 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023836 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 int j;
23838 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023840 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 char_u *name = NULL;
23842 char_u *p;
23843 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023844 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 garray_T newargs;
23846 garray_T newlines;
23847 int varargs = FALSE;
23848 int mustend = FALSE;
23849 int flags = 0;
23850 ufunc_T *fp;
23851 int indent;
23852 int nesting;
23853 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023854 dictitem_T *v;
23855 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023856 static int func_nr = 0; /* number for nameless function */
23857 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023858 hashtab_T *ht;
23859 int todo;
23860 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023861 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862
23863 /*
23864 * ":function" without argument: list functions.
23865 */
23866 if (ends_excmd(*eap->arg))
23867 {
23868 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023869 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023870 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023871 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023872 {
23873 if (!HASHITEM_EMPTY(hi))
23874 {
23875 --todo;
23876 fp = HI2UF(hi);
23877 if (!isdigit(*fp->uf_name))
23878 list_func_head(fp, FALSE);
23879 }
23880 }
23881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 eap->nextcmd = check_nextcmd(eap->arg);
23883 return;
23884 }
23885
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023886 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023887 * ":function /pat": list functions matching pattern.
23888 */
23889 if (*eap->arg == '/')
23890 {
23891 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23892 if (!eap->skip)
23893 {
23894 regmatch_T regmatch;
23895
23896 c = *p;
23897 *p = NUL;
23898 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23899 *p = c;
23900 if (regmatch.regprog != NULL)
23901 {
23902 regmatch.rm_ic = p_ic;
23903
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023904 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023905 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23906 {
23907 if (!HASHITEM_EMPTY(hi))
23908 {
23909 --todo;
23910 fp = HI2UF(hi);
23911 if (!isdigit(*fp->uf_name)
23912 && vim_regexec(&regmatch, fp->uf_name, 0))
23913 list_func_head(fp, FALSE);
23914 }
23915 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023916 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023917 }
23918 }
23919 if (*p == '/')
23920 ++p;
23921 eap->nextcmd = check_nextcmd(p);
23922 return;
23923 }
23924
23925 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023926 * Get the function name. There are these situations:
23927 * func normal function name
23928 * "name" == func, "fudi.fd_dict" == NULL
23929 * dict.func new dictionary entry
23930 * "name" == NULL, "fudi.fd_dict" set,
23931 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23932 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023933 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023934 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23935 * dict.func existing dict entry that's not a Funcref
23936 * "name" == NULL, "fudi.fd_dict" set,
23937 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023938 * s:func script-local function name
23939 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023942 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023943 paren = (vim_strchr(p, '(') != NULL);
23944 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945 {
23946 /*
23947 * Return on an invalid expression in braces, unless the expression
23948 * evaluation has been cancelled due to an aborting error, an
23949 * interrupt, or an exception.
23950 */
23951 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023953 if (!eap->skip && fudi.fd_newkey != NULL)
23954 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023955 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 else
23959 eap->skip = TRUE;
23960 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023961
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 /* An error in a function call during evaluation of an expression in magic
23963 * braces should not cause the function not to be defined. */
23964 saved_did_emsg = did_emsg;
23965 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966
23967 /*
23968 * ":function func" with only function name: list function.
23969 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023970 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971 {
23972 if (!ends_excmd(*skipwhite(p)))
23973 {
23974 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023975 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 }
23977 eap->nextcmd = check_nextcmd(p);
23978 if (eap->nextcmd != NULL)
23979 *p = NUL;
23980 if (!eap->skip && !got_int)
23981 {
23982 fp = find_func(name);
23983 if (fp != NULL)
23984 {
23985 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023986 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023988 if (FUNCLINE(fp, j) == NULL)
23989 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 msg_putchar('\n');
23991 msg_outnum((long)(j + 1));
23992 if (j < 9)
23993 msg_putchar(' ');
23994 if (j < 99)
23995 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023996 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 out_flush(); /* show a line at a time */
23998 ui_breakcheck();
23999 }
24000 if (!got_int)
24001 {
24002 msg_putchar('\n');
24003 msg_puts((char_u *)" endfunction");
24004 }
24005 }
24006 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024007 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024009 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 }
24011
24012 /*
24013 * ":function name(arg1, arg2)" Define function.
24014 */
24015 p = skipwhite(p);
24016 if (*p != '(')
24017 {
24018 if (!eap->skip)
24019 {
24020 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024021 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 }
24023 /* attempt to continue by skipping some text */
24024 if (vim_strchr(p, '(') != NULL)
24025 p = vim_strchr(p, '(');
24026 }
24027 p = skipwhite(p + 1);
24028
24029 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24030 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24031
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024032 if (!eap->skip)
24033 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024034 /* Check the name of the function. Unless it's a dictionary function
24035 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024036 if (name != NULL)
24037 arg = name;
24038 else
24039 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024040 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024041 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24042 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024043 {
24044 if (*arg == K_SPECIAL)
24045 j = 3;
24046 else
24047 j = 0;
24048 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24049 : eval_isnamec(arg[j])))
24050 ++j;
24051 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024052 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024053 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024054 /* Disallow using the g: dict. */
24055 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24056 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024057 }
24058
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 /*
24060 * Isolate the arguments: "arg1, arg2, ...)"
24061 */
24062 while (*p != ')')
24063 {
24064 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24065 {
24066 varargs = TRUE;
24067 p += 3;
24068 mustend = TRUE;
24069 }
24070 else
24071 {
24072 arg = p;
24073 while (ASCII_ISALNUM(*p) || *p == '_')
24074 ++p;
24075 if (arg == p || isdigit(*arg)
24076 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24077 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24078 {
24079 if (!eap->skip)
24080 EMSG2(_("E125: Illegal argument: %s"), arg);
24081 break;
24082 }
24083 if (ga_grow(&newargs, 1) == FAIL)
24084 goto erret;
24085 c = *p;
24086 *p = NUL;
24087 arg = vim_strsave(arg);
24088 if (arg == NULL)
24089 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024090
24091 /* Check for duplicate argument name. */
24092 for (i = 0; i < newargs.ga_len; ++i)
24093 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24094 {
24095 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024096 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024097 goto erret;
24098 }
24099
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24101 *p = c;
24102 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 if (*p == ',')
24104 ++p;
24105 else
24106 mustend = TRUE;
24107 }
24108 p = skipwhite(p);
24109 if (mustend && *p != ')')
24110 {
24111 if (!eap->skip)
24112 EMSG2(_(e_invarg2), eap->arg);
24113 break;
24114 }
24115 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024116 if (*p != ')')
24117 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 ++p; /* skip the ')' */
24119
Bram Moolenaare9a41262005-01-15 22:18:47 +000024120 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 for (;;)
24122 {
24123 p = skipwhite(p);
24124 if (STRNCMP(p, "range", 5) == 0)
24125 {
24126 flags |= FC_RANGE;
24127 p += 5;
24128 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024129 else if (STRNCMP(p, "dict", 4) == 0)
24130 {
24131 flags |= FC_DICT;
24132 p += 4;
24133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 else if (STRNCMP(p, "abort", 5) == 0)
24135 {
24136 flags |= FC_ABORT;
24137 p += 5;
24138 }
24139 else
24140 break;
24141 }
24142
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024143 /* When there is a line break use what follows for the function body.
24144 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24145 if (*p == '\n')
24146 line_arg = p + 1;
24147 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148 EMSG(_(e_trailing));
24149
24150 /*
24151 * Read the body of the function, until ":endfunction" is found.
24152 */
24153 if (KeyTyped)
24154 {
24155 /* Check if the function already exists, don't let the user type the
24156 * whole function before telling him it doesn't work! For a script we
24157 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024158 if (!eap->skip && !eap->forceit)
24159 {
24160 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24161 EMSG(_(e_funcdict));
24162 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024163 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024166 if (!eap->skip && did_emsg)
24167 goto erret;
24168
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169 msg_putchar('\n'); /* don't overwrite the function name */
24170 cmdline_row = msg_row;
24171 }
24172
24173 indent = 2;
24174 nesting = 0;
24175 for (;;)
24176 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024177 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024178 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024179 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024180 saved_wait_return = FALSE;
24181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024183 sourcing_lnum_off = sourcing_lnum;
24184
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024185 if (line_arg != NULL)
24186 {
24187 /* Use eap->arg, split up in parts by line breaks. */
24188 theline = line_arg;
24189 p = vim_strchr(theline, '\n');
24190 if (p == NULL)
24191 line_arg += STRLEN(line_arg);
24192 else
24193 {
24194 *p = NUL;
24195 line_arg = p + 1;
24196 }
24197 }
24198 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 theline = getcmdline(':', 0L, indent);
24200 else
24201 theline = eap->getline(':', eap->cookie, indent);
24202 if (KeyTyped)
24203 lines_left = Rows - 1;
24204 if (theline == NULL)
24205 {
24206 EMSG(_("E126: Missing :endfunction"));
24207 goto erret;
24208 }
24209
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024210 /* Detect line continuation: sourcing_lnum increased more than one. */
24211 if (sourcing_lnum > sourcing_lnum_off + 1)
24212 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24213 else
24214 sourcing_lnum_off = 0;
24215
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 if (skip_until != NULL)
24217 {
24218 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24219 * don't check for ":endfunc". */
24220 if (STRCMP(theline, skip_until) == 0)
24221 {
24222 vim_free(skip_until);
24223 skip_until = NULL;
24224 }
24225 }
24226 else
24227 {
24228 /* skip ':' and blanks*/
24229 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24230 ;
24231
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024232 /* Check for "endfunction". */
24233 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024235 if (line_arg == NULL)
24236 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 break;
24238 }
24239
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024240 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 * at "end". */
24242 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24243 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024244 else if (STRNCMP(p, "if", 2) == 0
24245 || STRNCMP(p, "wh", 2) == 0
24246 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 || STRNCMP(p, "try", 3) == 0)
24248 indent += 2;
24249
24250 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024251 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024253 if (*p == '!')
24254 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024256 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024257 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024259 ++nesting;
24260 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261 }
24262 }
24263
24264 /* Check for ":append" or ":insert". */
24265 p = skip_range(p, NULL);
24266 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24267 || (p[0] == 'i'
24268 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24269 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24270 skip_until = vim_strsave((char_u *)".");
24271
24272 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24273 arg = skipwhite(skiptowhite(p));
24274 if (arg[0] == '<' && arg[1] =='<'
24275 && ((p[0] == 'p' && p[1] == 'y'
24276 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24277 || (p[0] == 'p' && p[1] == 'e'
24278 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24279 || (p[0] == 't' && p[1] == 'c'
24280 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024281 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24282 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24284 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024285 || (p[0] == 'm' && p[1] == 'z'
24286 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 ))
24288 {
24289 /* ":python <<" continues until a dot, like ":append" */
24290 p = skipwhite(arg + 2);
24291 if (*p == NUL)
24292 skip_until = vim_strsave((char_u *)".");
24293 else
24294 skip_until = vim_strsave(p);
24295 }
24296 }
24297
24298 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024299 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024300 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024301 if (line_arg == NULL)
24302 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024304 }
24305
24306 /* Copy the line to newly allocated memory. get_one_sourceline()
24307 * allocates 250 bytes per line, this saves 80% on average. The cost
24308 * is an extra alloc/free. */
24309 p = vim_strsave(theline);
24310 if (p != NULL)
24311 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024312 if (line_arg == NULL)
24313 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024314 theline = p;
24315 }
24316
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024317 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24318
24319 /* Add NULL lines for continuation lines, so that the line count is
24320 * equal to the index in the growarray. */
24321 while (sourcing_lnum_off-- > 0)
24322 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024323
24324 /* Check for end of eap->arg. */
24325 if (line_arg != NULL && *line_arg == NUL)
24326 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327 }
24328
24329 /* Don't define the function when skipping commands or when an error was
24330 * detected. */
24331 if (eap->skip || did_emsg)
24332 goto erret;
24333
24334 /*
24335 * If there are no errors, add the function
24336 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024337 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024338 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024339 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024340 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024341 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024342 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024343 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024344 goto erret;
24345 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024346
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024347 fp = find_func(name);
24348 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024350 if (!eap->forceit)
24351 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024352 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024353 goto erret;
24354 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024355 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024356 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024357 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024358 name);
24359 goto erret;
24360 }
24361 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024362 ga_clear_strings(&(fp->uf_args));
24363 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024364 vim_free(name);
24365 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 }
24368 else
24369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024370 char numbuf[20];
24371
24372 fp = NULL;
24373 if (fudi.fd_newkey == NULL && !eap->forceit)
24374 {
24375 EMSG(_(e_funcdict));
24376 goto erret;
24377 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024378 if (fudi.fd_di == NULL)
24379 {
24380 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024381 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024382 goto erret;
24383 }
24384 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024385 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024386 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024387
24388 /* Give the function a sequential number. Can only be used with a
24389 * Funcref! */
24390 vim_free(name);
24391 sprintf(numbuf, "%d", ++func_nr);
24392 name = vim_strsave((char_u *)numbuf);
24393 if (name == NULL)
24394 goto erret;
24395 }
24396
24397 if (fp == NULL)
24398 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024399 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024400 {
24401 int slen, plen;
24402 char_u *scriptname;
24403
24404 /* Check that the autoload name matches the script name. */
24405 j = FAIL;
24406 if (sourcing_name != NULL)
24407 {
24408 scriptname = autoload_name(name);
24409 if (scriptname != NULL)
24410 {
24411 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024412 plen = (int)STRLEN(p);
24413 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024414 if (slen > plen && fnamecmp(p,
24415 sourcing_name + slen - plen) == 0)
24416 j = OK;
24417 vim_free(scriptname);
24418 }
24419 }
24420 if (j == FAIL)
24421 {
24422 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24423 goto erret;
24424 }
24425 }
24426
24427 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428 if (fp == NULL)
24429 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024430
24431 if (fudi.fd_dict != NULL)
24432 {
24433 if (fudi.fd_di == NULL)
24434 {
24435 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024436 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024437 if (fudi.fd_di == NULL)
24438 {
24439 vim_free(fp);
24440 goto erret;
24441 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024442 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24443 {
24444 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024445 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024446 goto erret;
24447 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024448 }
24449 else
24450 /* overwrite existing dict entry */
24451 clear_tv(&fudi.fd_di->di_tv);
24452 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024453 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024454 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024455 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024456
24457 /* behave like "dict" was used */
24458 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024459 }
24460
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024462 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024463 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24464 {
24465 vim_free(fp);
24466 goto erret;
24467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024469 fp->uf_args = newargs;
24470 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024471#ifdef FEAT_PROFILE
24472 fp->uf_tml_count = NULL;
24473 fp->uf_tml_total = NULL;
24474 fp->uf_tml_self = NULL;
24475 fp->uf_profiling = FALSE;
24476 if (prof_def_func())
24477 func_do_profile(fp);
24478#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024479 fp->uf_varargs = varargs;
24480 fp->uf_flags = flags;
24481 fp->uf_calls = 0;
24482 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024483 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484
24485erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486 ga_clear_strings(&newargs);
24487 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024488ret_free:
24489 vim_free(skip_until);
24490 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024491 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024493 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494}
24495
24496/*
24497 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024498 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024500 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024501 * TFN_INT: internal function name OK
24502 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024503 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504 * Advances "pp" to just after the function name (if no error).
24505 */
24506 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024507trans_function_name(
24508 char_u **pp,
24509 int skip, /* only find the end, don't evaluate */
24510 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024511 funcdict_T *fdp, /* return: info about dictionary used */
24512 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024514 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 char_u *start;
24516 char_u *end;
24517 int lead;
24518 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024520 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024521
24522 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024523 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024524 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024525
24526 /* Check for hard coded <SNR>: already translated function ID (from a user
24527 * command). */
24528 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24529 && (*pp)[2] == (int)KE_SNR)
24530 {
24531 *pp += 3;
24532 len = get_id_len(pp) + 3;
24533 return vim_strnsave(start, len);
24534 }
24535
24536 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24537 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024539 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024541
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024542 /* Note that TFN_ flags use the same values as GLV_ flags. */
24543 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024544 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024545 if (end == start)
24546 {
24547 if (!skip)
24548 EMSG(_("E129: Function name required"));
24549 goto theend;
24550 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024551 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024552 {
24553 /*
24554 * Report an invalid expression in braces, unless the expression
24555 * evaluation has been cancelled due to an aborting error, an
24556 * interrupt, or an exception.
24557 */
24558 if (!aborting())
24559 {
24560 if (end != NULL)
24561 EMSG2(_(e_invarg2), start);
24562 }
24563 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024564 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024565 goto theend;
24566 }
24567
24568 if (lv.ll_tv != NULL)
24569 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024570 if (fdp != NULL)
24571 {
24572 fdp->fd_dict = lv.ll_dict;
24573 fdp->fd_newkey = lv.ll_newkey;
24574 lv.ll_newkey = NULL;
24575 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024576 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024577 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24578 {
24579 name = vim_strsave(lv.ll_tv->vval.v_string);
24580 *pp = end;
24581 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024582 else if (lv.ll_tv->v_type == VAR_PARTIAL
24583 && lv.ll_tv->vval.v_partial != NULL)
24584 {
24585 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24586 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024587 if (partial != NULL)
24588 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024589 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024590 else
24591 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024592 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24593 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024594 EMSG(_(e_funcref));
24595 else
24596 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024597 name = NULL;
24598 }
24599 goto theend;
24600 }
24601
24602 if (lv.ll_name == NULL)
24603 {
24604 /* Error found, but continue after the function name. */
24605 *pp = end;
24606 goto theend;
24607 }
24608
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024609 /* Check if the name is a Funcref. If so, use the value. */
24610 if (lv.ll_exp_name != NULL)
24611 {
24612 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024613 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024614 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024615 if (name == lv.ll_exp_name)
24616 name = NULL;
24617 }
24618 else
24619 {
24620 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024621 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024622 if (name == *pp)
24623 name = NULL;
24624 }
24625 if (name != NULL)
24626 {
24627 name = vim_strsave(name);
24628 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024629 if (STRNCMP(name, "<SNR>", 5) == 0)
24630 {
24631 /* Change "<SNR>" to the byte sequence. */
24632 name[0] = K_SPECIAL;
24633 name[1] = KS_EXTRA;
24634 name[2] = (int)KE_SNR;
24635 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24636 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024637 goto theend;
24638 }
24639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024640 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024641 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024642 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024643 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24644 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24645 {
24646 /* When there was "s:" already or the name expanded to get a
24647 * leading "s:" then remove it. */
24648 lv.ll_name += 2;
24649 len -= 2;
24650 lead = 2;
24651 }
24652 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024653 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024654 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024655 /* skip over "s:" and "g:" */
24656 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024657 lv.ll_name += 2;
24658 len = (int)(end - lv.ll_name);
24659 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024660
24661 /*
24662 * Copy the function name to allocated memory.
24663 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24664 * Accept <SNR>123_name() outside a script.
24665 */
24666 if (skip)
24667 lead = 0; /* do nothing */
24668 else if (lead > 0)
24669 {
24670 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024671 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24672 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024673 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024674 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024675 if (current_SID <= 0)
24676 {
24677 EMSG(_(e_usingsid));
24678 goto theend;
24679 }
24680 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24681 lead += (int)STRLEN(sid_buf);
24682 }
24683 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024684 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024685 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024686 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024687 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024688 goto theend;
24689 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024690 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024691 {
24692 char_u *cp = vim_strchr(lv.ll_name, ':');
24693
24694 if (cp != NULL && cp < end)
24695 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024696 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024697 goto theend;
24698 }
24699 }
24700
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024701 name = alloc((unsigned)(len + lead + 1));
24702 if (name != NULL)
24703 {
24704 if (lead > 0)
24705 {
24706 name[0] = K_SPECIAL;
24707 name[1] = KS_EXTRA;
24708 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024709 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024710 STRCPY(name + 3, sid_buf);
24711 }
24712 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024713 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024714 }
24715 *pp = end;
24716
24717theend:
24718 clear_lval(&lv);
24719 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024720}
24721
24722/*
24723 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24724 * Return 2 if "p" starts with "s:".
24725 * Return 0 otherwise.
24726 */
24727 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024728eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024730 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24731 * the standard library function. */
24732 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24733 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024734 return 5;
24735 if (p[0] == 's' && p[1] == ':')
24736 return 2;
24737 return 0;
24738}
24739
24740/*
24741 * Return TRUE if "p" starts with "<SID>" or "s:".
24742 * Only works if eval_fname_script() returned non-zero for "p"!
24743 */
24744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024745eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024746{
24747 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24748}
24749
24750/*
24751 * List the head of the function: "name(arg1, arg2)".
24752 */
24753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024754list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755{
24756 int j;
24757
24758 msg_start();
24759 if (indent)
24760 MSG_PUTS(" ");
24761 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024762 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 {
24764 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024765 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766 }
24767 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024768 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024770 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771 {
24772 if (j)
24773 MSG_PUTS(", ");
24774 msg_puts(FUNCARG(fp, j));
24775 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024776 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 {
24778 if (j)
24779 MSG_PUTS(", ");
24780 MSG_PUTS("...");
24781 }
24782 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024783 if (fp->uf_flags & FC_ABORT)
24784 MSG_PUTS(" abort");
24785 if (fp->uf_flags & FC_RANGE)
24786 MSG_PUTS(" range");
24787 if (fp->uf_flags & FC_DICT)
24788 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024789 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024790 if (p_verbose > 0)
24791 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792}
24793
24794/*
24795 * Find a function by name, return pointer to it in ufuncs.
24796 * Return NULL for unknown function.
24797 */
24798 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024799find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024801 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024803 hi = hash_find(&func_hashtab, name);
24804 if (!HASHITEM_EMPTY(hi))
24805 return HI2UF(hi);
24806 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807}
24808
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024809#if defined(EXITFREE) || defined(PROTO)
24810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024811free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024812{
24813 hashitem_T *hi;
24814
24815 /* Need to start all over every time, because func_free() may change the
24816 * hash table. */
24817 while (func_hashtab.ht_used > 0)
24818 for (hi = func_hashtab.ht_array; ; ++hi)
24819 if (!HASHITEM_EMPTY(hi))
24820 {
24821 func_free(HI2UF(hi));
24822 break;
24823 }
24824}
24825#endif
24826
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024828translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024829{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024830 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024831 return find_internal_func(name) >= 0;
24832 return find_func(name) != NULL;
24833}
24834
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024835/*
24836 * Return TRUE if a function "name" exists.
24837 */
24838 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024839function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024840{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024841 char_u *nm = name;
24842 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024843 int n = FALSE;
24844
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024845 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024846 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024847 nm = skipwhite(nm);
24848
24849 /* Only accept "funcname", "funcname ", "funcname (..." and
24850 * "funcname(...", not "funcname!...". */
24851 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024852 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024853 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024854 return n;
24855}
24856
Bram Moolenaara1544c02013-05-30 12:35:52 +020024857 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024858get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024859{
24860 char_u *nm = name;
24861 char_u *p;
24862
Bram Moolenaar65639032016-03-16 21:40:30 +010024863 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024864
24865 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024866 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024867 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024868
Bram Moolenaara1544c02013-05-30 12:35:52 +020024869 vim_free(p);
24870 return NULL;
24871}
24872
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024873/*
24874 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024875 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24876 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024877 */
24878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024879builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024880{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024881 char_u *p;
24882
24883 if (!ASCII_ISLOWER(name[0]))
24884 return FALSE;
24885 p = vim_strchr(name, AUTOLOAD_CHAR);
24886 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024887}
24888
Bram Moolenaar05159a02005-02-26 23:04:13 +000024889#if defined(FEAT_PROFILE) || defined(PROTO)
24890/*
24891 * Start profiling function "fp".
24892 */
24893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024894func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024895{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024896 int len = fp->uf_lines.ga_len;
24897
24898 if (len == 0)
24899 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024900 fp->uf_tm_count = 0;
24901 profile_zero(&fp->uf_tm_self);
24902 profile_zero(&fp->uf_tm_total);
24903 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024904 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024905 if (fp->uf_tml_total == NULL)
24906 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024907 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024908 if (fp->uf_tml_self == NULL)
24909 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024910 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024911 fp->uf_tml_idx = -1;
24912 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24913 || fp->uf_tml_self == NULL)
24914 return; /* out of memory */
24915
24916 fp->uf_profiling = TRUE;
24917}
24918
24919/*
24920 * Dump the profiling results for all functions in file "fd".
24921 */
24922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024923func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024924{
24925 hashitem_T *hi;
24926 int todo;
24927 ufunc_T *fp;
24928 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024929 ufunc_T **sorttab;
24930 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024931
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024932 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024933 if (todo == 0)
24934 return; /* nothing to dump */
24935
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024936 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024937
Bram Moolenaar05159a02005-02-26 23:04:13 +000024938 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24939 {
24940 if (!HASHITEM_EMPTY(hi))
24941 {
24942 --todo;
24943 fp = HI2UF(hi);
24944 if (fp->uf_profiling)
24945 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024946 if (sorttab != NULL)
24947 sorttab[st_len++] = fp;
24948
Bram Moolenaar05159a02005-02-26 23:04:13 +000024949 if (fp->uf_name[0] == K_SPECIAL)
24950 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24951 else
24952 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24953 if (fp->uf_tm_count == 1)
24954 fprintf(fd, "Called 1 time\n");
24955 else
24956 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24957 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24958 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24959 fprintf(fd, "\n");
24960 fprintf(fd, "count total (s) self (s)\n");
24961
24962 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24963 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024964 if (FUNCLINE(fp, i) == NULL)
24965 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024966 prof_func_line(fd, fp->uf_tml_count[i],
24967 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024968 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24969 }
24970 fprintf(fd, "\n");
24971 }
24972 }
24973 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024974
24975 if (sorttab != NULL && st_len > 0)
24976 {
24977 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24978 prof_total_cmp);
24979 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24980 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24981 prof_self_cmp);
24982 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24983 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024984
24985 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024986}
Bram Moolenaar73830342005-02-28 22:48:19 +000024987
24988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024989prof_sort_list(
24990 FILE *fd,
24991 ufunc_T **sorttab,
24992 int st_len,
24993 char *title,
24994 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024995{
24996 int i;
24997 ufunc_T *fp;
24998
24999 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25000 fprintf(fd, "count total (s) self (s) function\n");
25001 for (i = 0; i < 20 && i < st_len; ++i)
25002 {
25003 fp = sorttab[i];
25004 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25005 prefer_self);
25006 if (fp->uf_name[0] == K_SPECIAL)
25007 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25008 else
25009 fprintf(fd, " %s()\n", fp->uf_name);
25010 }
25011 fprintf(fd, "\n");
25012}
25013
25014/*
25015 * Print the count and times for one function or function line.
25016 */
25017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025018prof_func_line(
25019 FILE *fd,
25020 int count,
25021 proftime_T *total,
25022 proftime_T *self,
25023 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025024{
25025 if (count > 0)
25026 {
25027 fprintf(fd, "%5d ", count);
25028 if (prefer_self && profile_equal(total, self))
25029 fprintf(fd, " ");
25030 else
25031 fprintf(fd, "%s ", profile_msg(total));
25032 if (!prefer_self && profile_equal(total, self))
25033 fprintf(fd, " ");
25034 else
25035 fprintf(fd, "%s ", profile_msg(self));
25036 }
25037 else
25038 fprintf(fd, " ");
25039}
25040
25041/*
25042 * Compare function for total time sorting.
25043 */
25044 static int
25045#ifdef __BORLANDC__
25046_RTLENTRYF
25047#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025048prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025049{
25050 ufunc_T *p1, *p2;
25051
25052 p1 = *(ufunc_T **)s1;
25053 p2 = *(ufunc_T **)s2;
25054 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25055}
25056
25057/*
25058 * Compare function for self time sorting.
25059 */
25060 static int
25061#ifdef __BORLANDC__
25062_RTLENTRYF
25063#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025064prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025065{
25066 ufunc_T *p1, *p2;
25067
25068 p1 = *(ufunc_T **)s1;
25069 p2 = *(ufunc_T **)s2;
25070 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25071}
25072
Bram Moolenaar05159a02005-02-26 23:04:13 +000025073#endif
25074
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025075/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025076 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025077 * Return TRUE if a package was loaded.
25078 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025080script_autoload(
25081 char_u *name,
25082 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025083{
25084 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025085 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025086 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025087 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025088
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025089 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025090 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025091 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025092 return FALSE;
25093
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025094 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025095
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025096 /* Find the name in the list of previously loaded package names. Skip
25097 * "autoload/", it's always the same. */
25098 for (i = 0; i < ga_loaded.ga_len; ++i)
25099 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25100 break;
25101 if (!reload && i < ga_loaded.ga_len)
25102 ret = FALSE; /* was loaded already */
25103 else
25104 {
25105 /* Remember the name if it wasn't loaded already. */
25106 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25107 {
25108 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25109 tofree = NULL;
25110 }
25111
25112 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025113 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025114 ret = TRUE;
25115 }
25116
25117 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025118 return ret;
25119}
25120
25121/*
25122 * Return the autoload script name for a function or variable name.
25123 * Returns NULL when out of memory.
25124 */
25125 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025126autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025127{
25128 char_u *p;
25129 char_u *scriptname;
25130
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025131 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025132 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25133 if (scriptname == NULL)
25134 return FALSE;
25135 STRCPY(scriptname, "autoload/");
25136 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025137 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025138 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025139 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025140 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025141 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025142}
25143
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25145
25146/*
25147 * Function given to ExpandGeneric() to obtain the list of user defined
25148 * function names.
25149 */
25150 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025151get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025152{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025153 static long_u done;
25154 static hashitem_T *hi;
25155 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156
25157 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025158 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025159 done = 0;
25160 hi = func_hashtab.ht_array;
25161 }
25162 if (done < func_hashtab.ht_used)
25163 {
25164 if (done++ > 0)
25165 ++hi;
25166 while (HASHITEM_EMPTY(hi))
25167 ++hi;
25168 fp = HI2UF(hi);
25169
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025170 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025171 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025172
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025173 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25174 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175
25176 cat_func_name(IObuff, fp);
25177 if (xp->xp_context != EXPAND_USER_FUNC)
25178 {
25179 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025180 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 STRCAT(IObuff, ")");
25182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 return IObuff;
25184 }
25185 return NULL;
25186}
25187
25188#endif /* FEAT_CMDL_COMPL */
25189
25190/*
25191 * Copy the function name of "fp" to buffer "buf".
25192 * "buf" must be able to hold the function name plus three bytes.
25193 * Takes care of script-local function names.
25194 */
25195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025196cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025198 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199 {
25200 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025201 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202 }
25203 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025204 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205}
25206
25207/*
25208 * ":delfunction {name}"
25209 */
25210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025211ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025213 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 char_u *p;
25215 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025216 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217
25218 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025219 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025220 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025221 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025222 {
25223 if (fudi.fd_dict != NULL && !eap->skip)
25224 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 if (!ends_excmd(*skipwhite(p)))
25228 {
25229 vim_free(name);
25230 EMSG(_(e_trailing));
25231 return;
25232 }
25233 eap->nextcmd = check_nextcmd(p);
25234 if (eap->nextcmd != NULL)
25235 *p = NUL;
25236
25237 if (!eap->skip)
25238 fp = find_func(name);
25239 vim_free(name);
25240
25241 if (!eap->skip)
25242 {
25243 if (fp == NULL)
25244 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025245 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246 return;
25247 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025248 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 {
25250 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25251 return;
25252 }
25253
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025254 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025256 /* Delete the dict item that refers to the function, it will
25257 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025258 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025259 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025260 else
25261 func_free(fp);
25262 }
25263}
25264
25265/*
25266 * Free a function and remove it from the list of functions.
25267 */
25268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025269func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025270{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025271 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025272
25273 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025274 ga_clear_strings(&(fp->uf_args));
25275 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025276#ifdef FEAT_PROFILE
25277 vim_free(fp->uf_tml_count);
25278 vim_free(fp->uf_tml_total);
25279 vim_free(fp->uf_tml_self);
25280#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025281
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025282 /* remove the function from the function hashtable */
25283 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25284 if (HASHITEM_EMPTY(hi))
25285 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025286 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025287 hash_remove(&func_hashtab, hi);
25288
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025289 vim_free(fp);
25290}
25291
25292/*
25293 * Unreference a Function: decrement the reference count and free it when it
25294 * becomes zero. Only for numbered functions.
25295 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025297func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025298{
25299 ufunc_T *fp;
25300
25301 if (name != NULL && isdigit(*name))
25302 {
25303 fp = find_func(name);
25304 if (fp == NULL)
25305 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025306 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025307 {
25308 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025309 * when "uf_calls" becomes zero. */
25310 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025311 func_free(fp);
25312 }
25313 }
25314}
25315
25316/*
25317 * Count a reference to a Function.
25318 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025320func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025321{
25322 ufunc_T *fp;
25323
25324 if (name != NULL && isdigit(*name))
25325 {
25326 fp = find_func(name);
25327 if (fp == NULL)
25328 EMSG2(_(e_intern2), "func_ref()");
25329 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025330 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 }
25332}
25333
25334/*
25335 * Call a user function.
25336 */
25337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025338call_user_func(
25339 ufunc_T *fp, /* pointer to function */
25340 int argcount, /* nr of args */
25341 typval_T *argvars, /* arguments */
25342 typval_T *rettv, /* return value */
25343 linenr_T firstline, /* first line of range */
25344 linenr_T lastline, /* last line of range */
25345 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346{
Bram Moolenaar33570922005-01-25 22:26:29 +000025347 char_u *save_sourcing_name;
25348 linenr_T save_sourcing_lnum;
25349 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025350 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025351 int save_did_emsg;
25352 static int depth = 0;
25353 dictitem_T *v;
25354 int fixvar_idx = 0; /* index in fixvar[] */
25355 int i;
25356 int ai;
25357 char_u numbuf[NUMBUFLEN];
25358 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025359 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025360#ifdef FEAT_PROFILE
25361 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025362 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364
25365 /* If depth of calling is getting too high, don't execute the function */
25366 if (depth >= p_mfd)
25367 {
25368 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025369 rettv->v_type = VAR_NUMBER;
25370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371 return;
25372 }
25373 ++depth;
25374
25375 line_breakcheck(); /* check for CTRL-C hit */
25376
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025377 fc = (funccall_T *)alloc(sizeof(funccall_T));
25378 fc->caller = current_funccal;
25379 current_funccal = fc;
25380 fc->func = fp;
25381 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025382 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025383 fc->linenr = 0;
25384 fc->returned = FALSE;
25385 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025386 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025387 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25388 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389
Bram Moolenaar33570922005-01-25 22:26:29 +000025390 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025391 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025392 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25393 * each argument variable and saves a lot of time.
25394 */
25395 /*
25396 * Init l: variables.
25397 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025398 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025399 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025400 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025401 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25402 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025403 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025404 name = v->di_key;
25405 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025406 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025407 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025408 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025409 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025410 v->di_tv.vval.v_dict = selfdict;
25411 ++selfdict->dv_refcount;
25412 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025413
Bram Moolenaar33570922005-01-25 22:26:29 +000025414 /*
25415 * Init a: variables.
25416 * Set a:0 to "argcount".
25417 * Set a:000 to a list with room for the "..." arguments.
25418 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025419 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025420 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025421 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025422 /* Use "name" to avoid a warning from some compiler that checks the
25423 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025424 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025425 name = v->di_key;
25426 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025427 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025428 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025429 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025430 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025431 v->di_tv.vval.v_list = &fc->l_varlist;
25432 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25433 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25434 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025435
25436 /*
25437 * Set a:firstline to "firstline" and a:lastline to "lastline".
25438 * Set a:name to named arguments.
25439 * Set a:N to the "..." arguments.
25440 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025441 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025442 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025443 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025444 (varnumber_T)lastline);
25445 for (i = 0; i < argcount; ++i)
25446 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025447 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025448 if (ai < 0)
25449 /* named argument a:name */
25450 name = FUNCARG(fp, i);
25451 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025452 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025453 /* "..." argument a:1, a:2, etc. */
25454 sprintf((char *)numbuf, "%d", ai + 1);
25455 name = numbuf;
25456 }
25457 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25458 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025459 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025460 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25461 }
25462 else
25463 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025464 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25465 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025466 if (v == NULL)
25467 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025468 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025469 }
25470 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025471 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025472
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025473 /* Note: the values are copied directly to avoid alloc/free.
25474 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025475 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025476 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025477
25478 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25479 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025480 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25481 fc->l_listitems[ai].li_tv = argvars[i];
25482 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025483 }
25484 }
25485
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 /* Don't redraw while executing the function. */
25487 ++RedrawingDisabled;
25488 save_sourcing_name = sourcing_name;
25489 save_sourcing_lnum = sourcing_lnum;
25490 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025491 /* need space for function name + ("function " + 3) or "[number]" */
25492 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25493 + STRLEN(fp->uf_name) + 20;
25494 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495 if (sourcing_name != NULL)
25496 {
25497 if (save_sourcing_name != NULL
25498 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025499 sprintf((char *)sourcing_name, "%s[%d]..",
25500 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 else
25502 STRCPY(sourcing_name, "function ");
25503 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25504
25505 if (p_verbose >= 12)
25506 {
25507 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025508 verbose_enter_scroll();
25509
Bram Moolenaar555b2802005-05-19 21:08:39 +000025510 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 if (p_verbose >= 14)
25512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025514 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025515 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025516 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517
25518 msg_puts((char_u *)"(");
25519 for (i = 0; i < argcount; ++i)
25520 {
25521 if (i > 0)
25522 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025523 if (argvars[i].v_type == VAR_NUMBER)
25524 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 else
25526 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025527 /* Do not want errors such as E724 here. */
25528 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025529 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025530 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025531 if (s != NULL)
25532 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025533 if (vim_strsize(s) > MSG_BUF_CLEN)
25534 {
25535 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25536 s = buf;
25537 }
25538 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025539 vim_free(tofree);
25540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025541 }
25542 }
25543 msg_puts((char_u *)")");
25544 }
25545 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025546
25547 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548 --no_wait_return;
25549 }
25550 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025551#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025552 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025553 {
25554 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25555 func_do_profile(fp);
25556 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025557 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025558 {
25559 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025560 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025561 profile_zero(&fp->uf_tm_children);
25562 }
25563 script_prof_save(&wait_start);
25564 }
25565#endif
25566
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025568 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025569 save_did_emsg = did_emsg;
25570 did_emsg = FALSE;
25571
25572 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025573 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25575
25576 --RedrawingDisabled;
25577
25578 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025579 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025581 clear_tv(rettv);
25582 rettv->v_type = VAR_NUMBER;
25583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584 }
25585
Bram Moolenaar05159a02005-02-26 23:04:13 +000025586#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025587 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025588 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025589 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025590 profile_end(&call_start);
25591 profile_sub_wait(&wait_start, &call_start);
25592 profile_add(&fp->uf_tm_total, &call_start);
25593 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025594 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025595 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025596 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25597 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025598 }
25599 }
25600#endif
25601
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602 /* when being verbose, mention the return value */
25603 if (p_verbose >= 12)
25604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025605 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025606 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025609 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025610 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025611 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025612 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025613 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025615 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025616 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025617 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025618 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025619
Bram Moolenaar555b2802005-05-19 21:08:39 +000025620 /* The value may be very long. Skip the middle part, so that we
25621 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025622 * truncate it at the end. Don't want errors such as E724 here. */
25623 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025624 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025625 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025626 if (s != NULL)
25627 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025628 if (vim_strsize(s) > MSG_BUF_CLEN)
25629 {
25630 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25631 s = buf;
25632 }
25633 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025634 vim_free(tofree);
25635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 }
25637 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025638
25639 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 --no_wait_return;
25641 }
25642
25643 vim_free(sourcing_name);
25644 sourcing_name = save_sourcing_name;
25645 sourcing_lnum = save_sourcing_lnum;
25646 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025647#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025648 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025649 script_prof_restore(&wait_start);
25650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651
25652 if (p_verbose >= 12 && sourcing_name != NULL)
25653 {
25654 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025655 verbose_enter_scroll();
25656
Bram Moolenaar555b2802005-05-19 21:08:39 +000025657 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025659
25660 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 --no_wait_return;
25662 }
25663
25664 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025665 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025667
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025668 /* If the a:000 list and the l: and a: dicts are not referenced we can
25669 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025670 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25671 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25672 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25673 {
25674 free_funccal(fc, FALSE);
25675 }
25676 else
25677 {
25678 hashitem_T *hi;
25679 listitem_T *li;
25680 int todo;
25681
25682 /* "fc" is still in use. This can happen when returning "a:000" or
25683 * assigning "l:" to a global variable.
25684 * Link "fc" in the list for garbage collection later. */
25685 fc->caller = previous_funccal;
25686 previous_funccal = fc;
25687
25688 /* Make a copy of the a: variables, since we didn't do that above. */
25689 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25690 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25691 {
25692 if (!HASHITEM_EMPTY(hi))
25693 {
25694 --todo;
25695 v = HI2DI(hi);
25696 copy_tv(&v->di_tv, &v->di_tv);
25697 }
25698 }
25699
25700 /* Make a copy of the a:000 items, since we didn't do that above. */
25701 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25702 copy_tv(&li->li_tv, &li->li_tv);
25703 }
25704}
25705
25706/*
25707 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025708 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025709 */
25710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025711can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025712{
25713 return (fc->l_varlist.lv_copyID != copyID
25714 && fc->l_vars.dv_copyID != copyID
25715 && fc->l_avars.dv_copyID != copyID);
25716}
25717
25718/*
25719 * Free "fc" and what it contains.
25720 */
25721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025722free_funccal(
25723 funccall_T *fc,
25724 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025725{
25726 listitem_T *li;
25727
25728 /* The a: variables typevals may not have been allocated, only free the
25729 * allocated variables. */
25730 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25731
25732 /* free all l: variables */
25733 vars_clear(&fc->l_vars.dv_hashtab);
25734
25735 /* Free the a:000 variables if they were allocated. */
25736 if (free_val)
25737 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25738 clear_tv(&li->li_tv);
25739
25740 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741}
25742
25743/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025744 * Add a number variable "name" to dict "dp" with value "nr".
25745 */
25746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025747add_nr_var(
25748 dict_T *dp,
25749 dictitem_T *v,
25750 char *name,
25751 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025752{
25753 STRCPY(v->di_key, name);
25754 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25755 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25756 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025757 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025758 v->di_tv.vval.v_number = nr;
25759}
25760
25761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 * ":return [expr]"
25763 */
25764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025765ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766{
25767 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025768 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 int returning = FALSE;
25770
25771 if (current_funccal == NULL)
25772 {
25773 EMSG(_("E133: :return not inside a function"));
25774 return;
25775 }
25776
25777 if (eap->skip)
25778 ++emsg_skip;
25779
25780 eap->nextcmd = NULL;
25781 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025782 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783 {
25784 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025785 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025787 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788 }
25789 /* It's safer to return also on error. */
25790 else if (!eap->skip)
25791 {
25792 /*
25793 * Return unless the expression evaluation has been cancelled due to an
25794 * aborting error, an interrupt, or an exception.
25795 */
25796 if (!aborting())
25797 returning = do_return(eap, FALSE, TRUE, NULL);
25798 }
25799
25800 /* When skipping or the return gets pending, advance to the next command
25801 * in this line (!returning). Otherwise, ignore the rest of the line.
25802 * Following lines will be ignored by get_func_line(). */
25803 if (returning)
25804 eap->nextcmd = NULL;
25805 else if (eap->nextcmd == NULL) /* no argument */
25806 eap->nextcmd = check_nextcmd(arg);
25807
25808 if (eap->skip)
25809 --emsg_skip;
25810}
25811
25812/*
25813 * Return from a function. Possibly makes the return pending. Also called
25814 * for a pending return at the ":endtry" or after returning from an extra
25815 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025816 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025817 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818 * FALSE when the return gets pending.
25819 */
25820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025821do_return(
25822 exarg_T *eap,
25823 int reanimate,
25824 int is_cmd,
25825 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826{
25827 int idx;
25828 struct condstack *cstack = eap->cstack;
25829
25830 if (reanimate)
25831 /* Undo the return. */
25832 current_funccal->returned = FALSE;
25833
25834 /*
25835 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25836 * not in its finally clause (which then is to be executed next) is found.
25837 * In this case, make the ":return" pending for execution at the ":endtry".
25838 * Otherwise, return normally.
25839 */
25840 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25841 if (idx >= 0)
25842 {
25843 cstack->cs_pending[idx] = CSTP_RETURN;
25844
25845 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025846 /* A pending return again gets pending. "rettv" points to an
25847 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025849 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025850 else
25851 {
25852 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025853 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025855 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025857 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 {
25859 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025860 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025861 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862 else
25863 EMSG(_(e_outofmem));
25864 }
25865 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025866 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867
25868 if (reanimate)
25869 {
25870 /* The pending return value could be overwritten by a ":return"
25871 * without argument in a finally clause; reset the default
25872 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025873 current_funccal->rettv->v_type = VAR_NUMBER;
25874 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875 }
25876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025877 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878 }
25879 else
25880 {
25881 current_funccal->returned = TRUE;
25882
25883 /* If the return is carried out now, store the return value. For
25884 * a return immediately after reanimation, the value is already
25885 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025886 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025888 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025889 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025890 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025891 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892 }
25893 }
25894
25895 return idx < 0;
25896}
25897
25898/*
25899 * Free the variable with a pending return value.
25900 */
25901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025902discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903{
Bram Moolenaar33570922005-01-25 22:26:29 +000025904 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025905}
25906
25907/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025908 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025909 * is an allocated string. Used by report_pending() for verbose messages.
25910 */
25911 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025912get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025914 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025915 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025916 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025918 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025919 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025920 if (s == NULL)
25921 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025922
25923 STRCPY(IObuff, ":return ");
25924 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25925 if (STRLEN(s) + 8 >= IOSIZE)
25926 STRCPY(IObuff + IOSIZE - 4, "...");
25927 vim_free(tofree);
25928 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929}
25930
25931/*
25932 * Get next function line.
25933 * Called by do_cmdline() to get the next line.
25934 * Returns allocated string, or NULL for end of function.
25935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025937get_func_line(
25938 int c UNUSED,
25939 void *cookie,
25940 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941{
Bram Moolenaar33570922005-01-25 22:26:29 +000025942 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025943 ufunc_T *fp = fcp->func;
25944 char_u *retval;
25945 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946
25947 /* If breakpoints have been added/deleted need to check for it. */
25948 if (fcp->dbg_tick != debug_tick)
25949 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025950 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025951 sourcing_lnum);
25952 fcp->dbg_tick = debug_tick;
25953 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025954#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025955 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025956 func_line_end(cookie);
25957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025958
Bram Moolenaar05159a02005-02-26 23:04:13 +000025959 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025960 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25961 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025962 retval = NULL;
25963 else
25964 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025965 /* Skip NULL lines (continuation lines). */
25966 while (fcp->linenr < gap->ga_len
25967 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25968 ++fcp->linenr;
25969 if (fcp->linenr >= gap->ga_len)
25970 retval = NULL;
25971 else
25972 {
25973 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25974 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025975#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025976 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025977 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025978#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980 }
25981
25982 /* Did we encounter a breakpoint? */
25983 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25984 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025985 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025986 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025987 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025988 sourcing_lnum);
25989 fcp->dbg_tick = debug_tick;
25990 }
25991
25992 return retval;
25993}
25994
Bram Moolenaar05159a02005-02-26 23:04:13 +000025995#if defined(FEAT_PROFILE) || defined(PROTO)
25996/*
25997 * Called when starting to read a function line.
25998 * "sourcing_lnum" must be correct!
25999 * When skipping lines it may not actually be executed, but we won't find out
26000 * until later and we need to store the time now.
26001 */
26002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026003func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026004{
26005 funccall_T *fcp = (funccall_T *)cookie;
26006 ufunc_T *fp = fcp->func;
26007
26008 if (fp->uf_profiling && sourcing_lnum >= 1
26009 && sourcing_lnum <= fp->uf_lines.ga_len)
26010 {
26011 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026012 /* Skip continuation lines. */
26013 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26014 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026015 fp->uf_tml_execed = FALSE;
26016 profile_start(&fp->uf_tml_start);
26017 profile_zero(&fp->uf_tml_children);
26018 profile_get_wait(&fp->uf_tml_wait);
26019 }
26020}
26021
26022/*
26023 * Called when actually executing a function line.
26024 */
26025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026026func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026027{
26028 funccall_T *fcp = (funccall_T *)cookie;
26029 ufunc_T *fp = fcp->func;
26030
26031 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26032 fp->uf_tml_execed = TRUE;
26033}
26034
26035/*
26036 * Called when done with a function line.
26037 */
26038 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026039func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026040{
26041 funccall_T *fcp = (funccall_T *)cookie;
26042 ufunc_T *fp = fcp->func;
26043
26044 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26045 {
26046 if (fp->uf_tml_execed)
26047 {
26048 ++fp->uf_tml_count[fp->uf_tml_idx];
26049 profile_end(&fp->uf_tml_start);
26050 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026051 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026052 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26053 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026054 }
26055 fp->uf_tml_idx = -1;
26056 }
26057}
26058#endif
26059
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060/*
26061 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026062 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063 */
26064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026065func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026066{
Bram Moolenaar33570922005-01-25 22:26:29 +000026067 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068
26069 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26070 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026071 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026072 || fcp->returned);
26073}
26074
26075/*
26076 * return TRUE if cookie indicates a function which "abort"s on errors.
26077 */
26078 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026079func_has_abort(
26080 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026082 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026083}
26084
26085#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26086typedef enum
26087{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026088 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26089 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26090 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091} var_flavour_T;
26092
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026093static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094
26095 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026096var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026097{
26098 char_u *p = varname;
26099
26100 if (ASCII_ISUPPER(*p))
26101 {
26102 while (*(++p))
26103 if (ASCII_ISLOWER(*p))
26104 return VAR_FLAVOUR_SESSION;
26105 return VAR_FLAVOUR_VIMINFO;
26106 }
26107 else
26108 return VAR_FLAVOUR_DEFAULT;
26109}
26110#endif
26111
26112#if defined(FEAT_VIMINFO) || defined(PROTO)
26113/*
26114 * Restore global vars that start with a capital from the viminfo file
26115 */
26116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026117read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118{
26119 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026120 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026121 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026122 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026123
26124 if (!writing && (find_viminfo_parameter('!') != NULL))
26125 {
26126 tab = vim_strchr(virp->vir_line + 1, '\t');
26127 if (tab != NULL)
26128 {
26129 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026130 switch (*tab)
26131 {
26132 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026133#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026134 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026135#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026136 case 'D': type = VAR_DICT; break;
26137 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026138 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026140
26141 tab = vim_strchr(tab, '\t');
26142 if (tab != NULL)
26143 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026144 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026145 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026146 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026148#ifdef FEAT_FLOAT
26149 else if (type == VAR_FLOAT)
26150 (void)string2float(tab + 1, &tv.vval.v_float);
26151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026152 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026153 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026154 if (type == VAR_DICT || type == VAR_LIST)
26155 {
26156 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26157
26158 if (etv == NULL)
26159 /* Failed to parse back the dict or list, use it as a
26160 * string. */
26161 tv.v_type = VAR_STRING;
26162 else
26163 {
26164 vim_free(tv.vval.v_string);
26165 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026166 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026167 }
26168 }
26169
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026170 /* when in a function use global variables */
26171 save_funccal = current_funccal;
26172 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026173 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026174 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026175
26176 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026177 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026178 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26179 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026180 }
26181 }
26182 }
26183
26184 return viminfo_readline(virp);
26185}
26186
26187/*
26188 * Write global vars that start with a capital to the viminfo file
26189 */
26190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026191write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192{
Bram Moolenaar33570922005-01-25 22:26:29 +000026193 hashitem_T *hi;
26194 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026195 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026196 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026197 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026198 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026199 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200
26201 if (find_viminfo_parameter('!') == NULL)
26202 return;
26203
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026204 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026205
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026206 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026207 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026208 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026209 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026210 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026211 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026212 this_var = HI2DI(hi);
26213 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026214 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026215 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026216 {
26217 case VAR_STRING: s = "STR"; break;
26218 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026219 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026220 case VAR_DICT: s = "DIC"; break;
26221 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026222 case VAR_SPECIAL: s = "XPL"; break;
26223
26224 case VAR_UNKNOWN:
26225 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026226 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026227 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026228 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026229 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026230 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026231 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026232 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026233 if (p != NULL)
26234 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026235 vim_free(tofree);
26236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026237 }
26238 }
26239}
26240#endif
26241
26242#if defined(FEAT_SESSION) || defined(PROTO)
26243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026244store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026245{
Bram Moolenaar33570922005-01-25 22:26:29 +000026246 hashitem_T *hi;
26247 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026248 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249 char_u *p, *t;
26250
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026251 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026252 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026253 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026254 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026256 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026257 this_var = HI2DI(hi);
26258 if ((this_var->di_tv.v_type == VAR_NUMBER
26259 || this_var->di_tv.v_type == VAR_STRING)
26260 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026261 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026262 /* Escape special characters with a backslash. Turn a LF and
26263 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026264 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026265 (char_u *)"\\\"\n\r");
26266 if (p == NULL) /* out of memory */
26267 break;
26268 for (t = p; *t != NUL; ++t)
26269 if (*t == '\n')
26270 *t = 'n';
26271 else if (*t == '\r')
26272 *t = 'r';
26273 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026274 this_var->di_key,
26275 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26276 : ' ',
26277 p,
26278 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26279 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026280 || put_eol(fd) == FAIL)
26281 {
26282 vim_free(p);
26283 return FAIL;
26284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026285 vim_free(p);
26286 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026287#ifdef FEAT_FLOAT
26288 else if (this_var->di_tv.v_type == VAR_FLOAT
26289 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26290 {
26291 float_T f = this_var->di_tv.vval.v_float;
26292 int sign = ' ';
26293
26294 if (f < 0)
26295 {
26296 f = -f;
26297 sign = '-';
26298 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026299 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026300 this_var->di_key, sign, f) < 0)
26301 || put_eol(fd) == FAIL)
26302 return FAIL;
26303 }
26304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305 }
26306 }
26307 return OK;
26308}
26309#endif
26310
Bram Moolenaar661b1822005-07-28 22:36:45 +000026311/*
26312 * Display script name where an item was last set.
26313 * Should only be invoked when 'verbose' is non-zero.
26314 */
26315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026316last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026317{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026318 char_u *p;
26319
Bram Moolenaar661b1822005-07-28 22:36:45 +000026320 if (scriptID != 0)
26321 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026322 p = home_replace_save(NULL, get_scriptname(scriptID));
26323 if (p != NULL)
26324 {
26325 verbose_enter();
26326 MSG_PUTS(_("\n\tLast set from "));
26327 MSG_PUTS(p);
26328 vim_free(p);
26329 verbose_leave();
26330 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026331 }
26332}
26333
Bram Moolenaard812df62008-11-09 12:46:09 +000026334/*
26335 * List v:oldfiles in a nice way.
26336 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026337 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026338ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026339{
26340 list_T *l = vimvars[VV_OLDFILES].vv_list;
26341 listitem_T *li;
26342 int nr = 0;
26343
26344 if (l == NULL)
26345 msg((char_u *)_("No old files"));
26346 else
26347 {
26348 msg_start();
26349 msg_scroll = TRUE;
26350 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26351 {
26352 msg_outnum((long)++nr);
26353 MSG_PUTS(": ");
26354 msg_outtrans(get_tv_string(&li->li_tv));
26355 msg_putchar('\n');
26356 out_flush(); /* output one line at a time */
26357 ui_breakcheck();
26358 }
26359 /* Assume "got_int" was set to truncate the listing. */
26360 got_int = FALSE;
26361
26362#ifdef FEAT_BROWSE_CMD
26363 if (cmdmod.browse)
26364 {
26365 quit_more = FALSE;
26366 nr = prompt_for_number(FALSE);
26367 msg_starthere();
26368 if (nr > 0)
26369 {
26370 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26371 (long)nr);
26372
26373 if (p != NULL)
26374 {
26375 p = expand_env_save(p);
26376 eap->arg = p;
26377 eap->cmdidx = CMD_edit;
26378 cmdmod.browse = FALSE;
26379 do_exedit(eap, NULL);
26380 vim_free(p);
26381 }
26382 }
26383 }
26384#endif
26385 }
26386}
26387
Bram Moolenaar53744302015-07-17 17:38:22 +020026388/* reset v:option_new, v:option_old and v:option_type */
26389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026390reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026391{
26392 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26393 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26394 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26395}
26396
26397
Bram Moolenaar071d4272004-06-13 20:20:40 +000026398#endif /* FEAT_EVAL */
26399
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026401#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026402
26403#ifdef WIN3264
26404/*
26405 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26406 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026407static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26408static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26409static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026410
26411/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026412 * Get the short path (8.3) for the filename in "fnamep".
26413 * Only works for a valid file name.
26414 * When the path gets longer "fnamep" is changed and the allocated buffer
26415 * is put in "bufp".
26416 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26417 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418 */
26419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026420get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026422 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026423 char_u *newbuf;
26424
26425 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026426 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026427 if (l > len - 1)
26428 {
26429 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026430 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026431 newbuf = vim_strnsave(*fnamep, l);
26432 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026433 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026434
26435 vim_free(*bufp);
26436 *fnamep = *bufp = newbuf;
26437
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026438 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026439 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026440 }
26441
26442 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026443 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444}
26445
26446/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026447 * Get the short path (8.3) for the filename in "fname". The converted
26448 * path is returned in "bufp".
26449 *
26450 * Some of the directories specified in "fname" may not exist. This function
26451 * will shorten the existing directories at the beginning of the path and then
26452 * append the remaining non-existing path.
26453 *
26454 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026455 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026456 * bufp - Pointer to an allocated buffer for the filename.
26457 * fnamelen - Length of the filename pointed to by fname
26458 *
26459 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460 */
26461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026462shortpath_for_invalid_fname(
26463 char_u **fname,
26464 char_u **bufp,
26465 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026466{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026467 char_u *short_fname, *save_fname, *pbuf_unused;
26468 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026470 int old_len, len;
26471 int new_len, sfx_len;
26472 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026473
26474 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026475 old_len = *fnamelen;
26476 save_fname = vim_strnsave(*fname, old_len);
26477 pbuf_unused = NULL;
26478 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026479
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026480 endp = save_fname + old_len - 1; /* Find the end of the copy */
26481 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026482
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026483 /*
26484 * Try shortening the supplied path till it succeeds by removing one
26485 * directory at a time from the tail of the path.
26486 */
26487 len = 0;
26488 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026489 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026490 /* go back one path-separator */
26491 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26492 --endp;
26493 if (endp <= save_fname)
26494 break; /* processed the complete path */
26495
26496 /*
26497 * Replace the path separator with a NUL and try to shorten the
26498 * resulting path.
26499 */
26500 ch = *endp;
26501 *endp = 0;
26502 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026503 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026504 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26505 {
26506 retval = FAIL;
26507 goto theend;
26508 }
26509 *endp = ch; /* preserve the string */
26510
26511 if (len > 0)
26512 break; /* successfully shortened the path */
26513
26514 /* failed to shorten the path. Skip the path separator */
26515 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026516 }
26517
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026518 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026519 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026520 /*
26521 * Succeeded in shortening the path. Now concatenate the shortened
26522 * path with the remaining path at the tail.
26523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026525 /* Compute the length of the new path. */
26526 sfx_len = (int)(save_endp - endp) + 1;
26527 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026528
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026529 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026530 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026531 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026533 /* There is not enough space in the currently allocated string,
26534 * copy it to a buffer big enough. */
26535 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026536 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026537 {
26538 retval = FAIL;
26539 goto theend;
26540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026541 }
26542 else
26543 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026544 /* Transfer short_fname to the main buffer (it's big enough),
26545 * unless get_short_pathname() did its work in-place. */
26546 *fname = *bufp = save_fname;
26547 if (short_fname != save_fname)
26548 vim_strncpy(save_fname, short_fname, len);
26549 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026550 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026551
26552 /* concat the not-shortened part of the path */
26553 vim_strncpy(*fname + len, endp, sfx_len);
26554 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026555 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026556
26557theend:
26558 vim_free(pbuf_unused);
26559 vim_free(save_fname);
26560
26561 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562}
26563
26564/*
26565 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026566 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026567 */
26568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026569shortpath_for_partial(
26570 char_u **fnamep,
26571 char_u **bufp,
26572 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026573{
26574 int sepcount, len, tflen;
26575 char_u *p;
26576 char_u *pbuf, *tfname;
26577 int hasTilde;
26578
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026579 /* Count up the path separators from the RHS.. so we know which part
26580 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026581 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026582 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026583 if (vim_ispathsep(*p))
26584 ++sepcount;
26585
26586 /* Need full path first (use expand_env() to remove a "~/") */
26587 hasTilde = (**fnamep == '~');
26588 if (hasTilde)
26589 pbuf = tfname = expand_env_save(*fnamep);
26590 else
26591 pbuf = tfname = FullName_save(*fnamep, FALSE);
26592
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026593 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026594
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026595 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26596 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026597
26598 if (len == 0)
26599 {
26600 /* Don't have a valid filename, so shorten the rest of the
26601 * path if we can. This CAN give us invalid 8.3 filenames, but
26602 * there's not a lot of point in guessing what it might be.
26603 */
26604 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026605 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26606 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026607 }
26608
26609 /* Count the paths backward to find the beginning of the desired string. */
26610 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026611 {
26612#ifdef FEAT_MBYTE
26613 if (has_mbyte)
26614 p -= mb_head_off(tfname, p);
26615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026616 if (vim_ispathsep(*p))
26617 {
26618 if (sepcount == 0 || (hasTilde && sepcount == 1))
26619 break;
26620 else
26621 sepcount --;
26622 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026624 if (hasTilde)
26625 {
26626 --p;
26627 if (p >= tfname)
26628 *p = '~';
26629 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026630 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026631 }
26632 else
26633 ++p;
26634
26635 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26636 vim_free(*bufp);
26637 *fnamelen = (int)STRLEN(p);
26638 *bufp = pbuf;
26639 *fnamep = p;
26640
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026641 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026642}
26643#endif /* WIN3264 */
26644
26645/*
26646 * Adjust a filename, according to a string of modifiers.
26647 * *fnamep must be NUL terminated when called. When returning, the length is
26648 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026649 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026650 * When there is an error, *fnamep is set to NULL.
26651 */
26652 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026653modify_fname(
26654 char_u *src, /* string with modifiers */
26655 int *usedlen, /* characters after src that are used */
26656 char_u **fnamep, /* file name so far */
26657 char_u **bufp, /* buffer for allocated file name or NULL */
26658 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026659{
26660 int valid = 0;
26661 char_u *tail;
26662 char_u *s, *p, *pbuf;
26663 char_u dirname[MAXPATHL];
26664 int c;
26665 int has_fullname = 0;
26666#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026667 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026668 int has_shortname = 0;
26669#endif
26670
26671repeat:
26672 /* ":p" - full path/file_name */
26673 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26674 {
26675 has_fullname = 1;
26676
26677 valid |= VALID_PATH;
26678 *usedlen += 2;
26679
26680 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26681 if ((*fnamep)[0] == '~'
26682#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26683 && ((*fnamep)[1] == '/'
26684# ifdef BACKSLASH_IN_FILENAME
26685 || (*fnamep)[1] == '\\'
26686# endif
26687 || (*fnamep)[1] == NUL)
26688
26689#endif
26690 )
26691 {
26692 *fnamep = expand_env_save(*fnamep);
26693 vim_free(*bufp); /* free any allocated file name */
26694 *bufp = *fnamep;
26695 if (*fnamep == NULL)
26696 return -1;
26697 }
26698
26699 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026700 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026701 {
26702 if (vim_ispathsep(*p)
26703 && p[1] == '.'
26704 && (p[2] == NUL
26705 || vim_ispathsep(p[2])
26706 || (p[2] == '.'
26707 && (p[3] == NUL || vim_ispathsep(p[3])))))
26708 break;
26709 }
26710
26711 /* FullName_save() is slow, don't use it when not needed. */
26712 if (*p != NUL || !vim_isAbsName(*fnamep))
26713 {
26714 *fnamep = FullName_save(*fnamep, *p != NUL);
26715 vim_free(*bufp); /* free any allocated file name */
26716 *bufp = *fnamep;
26717 if (*fnamep == NULL)
26718 return -1;
26719 }
26720
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026721#ifdef WIN3264
26722# if _WIN32_WINNT >= 0x0500
26723 if (vim_strchr(*fnamep, '~') != NULL)
26724 {
26725 /* Expand 8.3 filename to full path. Needed to make sure the same
26726 * file does not have two different names.
26727 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26728 p = alloc(_MAX_PATH + 1);
26729 if (p != NULL)
26730 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026731 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026732 {
26733 vim_free(*bufp);
26734 *bufp = *fnamep = p;
26735 }
26736 else
26737 vim_free(p);
26738 }
26739 }
26740# endif
26741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026742 /* Append a path separator to a directory. */
26743 if (mch_isdir(*fnamep))
26744 {
26745 /* Make room for one or two extra characters. */
26746 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26747 vim_free(*bufp); /* free any allocated file name */
26748 *bufp = *fnamep;
26749 if (*fnamep == NULL)
26750 return -1;
26751 add_pathsep(*fnamep);
26752 }
26753 }
26754
26755 /* ":." - path relative to the current directory */
26756 /* ":~" - path relative to the home directory */
26757 /* ":8" - shortname path - postponed till after */
26758 while (src[*usedlen] == ':'
26759 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26760 {
26761 *usedlen += 2;
26762 if (c == '8')
26763 {
26764#ifdef WIN3264
26765 has_shortname = 1; /* Postpone this. */
26766#endif
26767 continue;
26768 }
26769 pbuf = NULL;
26770 /* Need full path first (use expand_env() to remove a "~/") */
26771 if (!has_fullname)
26772 {
26773 if (c == '.' && **fnamep == '~')
26774 p = pbuf = expand_env_save(*fnamep);
26775 else
26776 p = pbuf = FullName_save(*fnamep, FALSE);
26777 }
26778 else
26779 p = *fnamep;
26780
26781 has_fullname = 0;
26782
26783 if (p != NULL)
26784 {
26785 if (c == '.')
26786 {
26787 mch_dirname(dirname, MAXPATHL);
26788 s = shorten_fname(p, dirname);
26789 if (s != NULL)
26790 {
26791 *fnamep = s;
26792 if (pbuf != NULL)
26793 {
26794 vim_free(*bufp); /* free any allocated file name */
26795 *bufp = pbuf;
26796 pbuf = NULL;
26797 }
26798 }
26799 }
26800 else
26801 {
26802 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26803 /* Only replace it when it starts with '~' */
26804 if (*dirname == '~')
26805 {
26806 s = vim_strsave(dirname);
26807 if (s != NULL)
26808 {
26809 *fnamep = s;
26810 vim_free(*bufp);
26811 *bufp = s;
26812 }
26813 }
26814 }
26815 vim_free(pbuf);
26816 }
26817 }
26818
26819 tail = gettail(*fnamep);
26820 *fnamelen = (int)STRLEN(*fnamep);
26821
26822 /* ":h" - head, remove "/file_name", can be repeated */
26823 /* Don't remove the first "/" or "c:\" */
26824 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26825 {
26826 valid |= VALID_HEAD;
26827 *usedlen += 2;
26828 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026829 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026830 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026831 *fnamelen = (int)(tail - *fnamep);
26832#ifdef VMS
26833 if (*fnamelen > 0)
26834 *fnamelen += 1; /* the path separator is part of the path */
26835#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026836 if (*fnamelen == 0)
26837 {
26838 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26839 p = vim_strsave((char_u *)".");
26840 if (p == NULL)
26841 return -1;
26842 vim_free(*bufp);
26843 *bufp = *fnamep = tail = p;
26844 *fnamelen = 1;
26845 }
26846 else
26847 {
26848 while (tail > s && !after_pathsep(s, tail))
26849 mb_ptr_back(*fnamep, tail);
26850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026851 }
26852
26853 /* ":8" - shortname */
26854 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26855 {
26856 *usedlen += 2;
26857#ifdef WIN3264
26858 has_shortname = 1;
26859#endif
26860 }
26861
26862#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026863 /*
26864 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026865 */
26866 if (has_shortname)
26867 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026868 /* Copy the string if it is shortened by :h and when it wasn't copied
26869 * yet, because we are going to change it in place. Avoids changing
26870 * the buffer name for "%:8". */
26871 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026872 {
26873 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026874 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026875 return -1;
26876 vim_free(*bufp);
26877 *bufp = *fnamep = p;
26878 }
26879
26880 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026881 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026882 if (!has_fullname && !vim_isAbsName(*fnamep))
26883 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026884 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026885 return -1;
26886 }
26887 else
26888 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026889 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026890
Bram Moolenaardc935552011-08-17 15:23:23 +020026891 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026892 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026893 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026894 return -1;
26895
26896 if (l == 0)
26897 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026898 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026899 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026900 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026901 return -1;
26902 }
26903 *fnamelen = l;
26904 }
26905 }
26906#endif /* WIN3264 */
26907
26908 /* ":t" - tail, just the basename */
26909 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26910 {
26911 *usedlen += 2;
26912 *fnamelen -= (int)(tail - *fnamep);
26913 *fnamep = tail;
26914 }
26915
26916 /* ":e" - extension, can be repeated */
26917 /* ":r" - root, without extension, can be repeated */
26918 while (src[*usedlen] == ':'
26919 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26920 {
26921 /* find a '.' in the tail:
26922 * - for second :e: before the current fname
26923 * - otherwise: The last '.'
26924 */
26925 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26926 s = *fnamep - 2;
26927 else
26928 s = *fnamep + *fnamelen - 1;
26929 for ( ; s > tail; --s)
26930 if (s[0] == '.')
26931 break;
26932 if (src[*usedlen + 1] == 'e') /* :e */
26933 {
26934 if (s > tail)
26935 {
26936 *fnamelen += (int)(*fnamep - (s + 1));
26937 *fnamep = s + 1;
26938#ifdef VMS
26939 /* cut version from the extension */
26940 s = *fnamep + *fnamelen - 1;
26941 for ( ; s > *fnamep; --s)
26942 if (s[0] == ';')
26943 break;
26944 if (s > *fnamep)
26945 *fnamelen = s - *fnamep;
26946#endif
26947 }
26948 else if (*fnamep <= tail)
26949 *fnamelen = 0;
26950 }
26951 else /* :r */
26952 {
26953 if (s > tail) /* remove one extension */
26954 *fnamelen = (int)(s - *fnamep);
26955 }
26956 *usedlen += 2;
26957 }
26958
26959 /* ":s?pat?foo?" - substitute */
26960 /* ":gs?pat?foo?" - global substitute */
26961 if (src[*usedlen] == ':'
26962 && (src[*usedlen + 1] == 's'
26963 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26964 {
26965 char_u *str;
26966 char_u *pat;
26967 char_u *sub;
26968 int sep;
26969 char_u *flags;
26970 int didit = FALSE;
26971
26972 flags = (char_u *)"";
26973 s = src + *usedlen + 2;
26974 if (src[*usedlen + 1] == 'g')
26975 {
26976 flags = (char_u *)"g";
26977 ++s;
26978 }
26979
26980 sep = *s++;
26981 if (sep)
26982 {
26983 /* find end of pattern */
26984 p = vim_strchr(s, sep);
26985 if (p != NULL)
26986 {
26987 pat = vim_strnsave(s, (int)(p - s));
26988 if (pat != NULL)
26989 {
26990 s = p + 1;
26991 /* find end of substitution */
26992 p = vim_strchr(s, sep);
26993 if (p != NULL)
26994 {
26995 sub = vim_strnsave(s, (int)(p - s));
26996 str = vim_strnsave(*fnamep, *fnamelen);
26997 if (sub != NULL && str != NULL)
26998 {
26999 *usedlen = (int)(p + 1 - src);
27000 s = do_string_sub(str, pat, sub, flags);
27001 if (s != NULL)
27002 {
27003 *fnamep = s;
27004 *fnamelen = (int)STRLEN(s);
27005 vim_free(*bufp);
27006 *bufp = s;
27007 didit = TRUE;
27008 }
27009 }
27010 vim_free(sub);
27011 vim_free(str);
27012 }
27013 vim_free(pat);
27014 }
27015 }
27016 /* after using ":s", repeat all the modifiers */
27017 if (didit)
27018 goto repeat;
27019 }
27020 }
27021
Bram Moolenaar26df0922014-02-23 23:39:13 +010027022 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27023 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027024 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027025 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027026 if (c != NUL)
27027 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027028 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027029 if (c != NUL)
27030 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027031 if (p == NULL)
27032 return -1;
27033 vim_free(*bufp);
27034 *bufp = *fnamep = p;
27035 *fnamelen = (int)STRLEN(p);
27036 *usedlen += 2;
27037 }
27038
Bram Moolenaar071d4272004-06-13 20:20:40 +000027039 return valid;
27040}
27041
27042/*
27043 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27044 * "flags" can be "g" to do a global substitute.
27045 * Returns an allocated string, NULL for error.
27046 */
27047 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027048do_string_sub(
27049 char_u *str,
27050 char_u *pat,
27051 char_u *sub,
27052 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027053{
27054 int sublen;
27055 regmatch_T regmatch;
27056 int i;
27057 int do_all;
27058 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027059 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027060 garray_T ga;
27061 char_u *ret;
27062 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027063 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027064
27065 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27066 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027067 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027068
27069 ga_init2(&ga, 1, 200);
27070
27071 do_all = (flags[0] == 'g');
27072
27073 regmatch.rm_ic = p_ic;
27074 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27075 if (regmatch.regprog != NULL)
27076 {
27077 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027078 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027079 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27080 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027081 /* Skip empty match except for first match. */
27082 if (regmatch.startp[0] == regmatch.endp[0])
27083 {
27084 if (zero_width == regmatch.startp[0])
27085 {
27086 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027087 i = MB_PTR2LEN(tail);
27088 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27089 (size_t)i);
27090 ga.ga_len += i;
27091 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027092 continue;
27093 }
27094 zero_width = regmatch.startp[0];
27095 }
27096
Bram Moolenaar071d4272004-06-13 20:20:40 +000027097 /*
27098 * Get some space for a temporary buffer to do the substitution
27099 * into. It will contain:
27100 * - The text up to where the match is.
27101 * - The substituted text.
27102 * - The text after the match.
27103 */
27104 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027105 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027106 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27107 {
27108 ga_clear(&ga);
27109 break;
27110 }
27111
27112 /* copy the text up to where the match is */
27113 i = (int)(regmatch.startp[0] - tail);
27114 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27115 /* add the substituted text */
27116 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27117 + ga.ga_len + i, TRUE, TRUE, FALSE);
27118 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027119 tail = regmatch.endp[0];
27120 if (*tail == NUL)
27121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027122 if (!do_all)
27123 break;
27124 }
27125
27126 if (ga.ga_data != NULL)
27127 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27128
Bram Moolenaar473de612013-06-08 18:19:48 +020027129 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027130 }
27131
27132 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27133 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027134 if (p_cpo == empty_option)
27135 p_cpo = save_cpo;
27136 else
27137 /* Darn, evaluating {sub} expression changed the value. */
27138 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027139
27140 return ret;
27141}
27142
27143#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */