blob: 5ced88ef06fc6e45f63e3a0c1968024c60e96982 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000113
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100114#define NAMESPACE_CHAR (char_u *)"abglstvw"
115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000128 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 */
130static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131#define COPYID_INC 2
132#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133
Bram Moolenaar8502c702014-06-17 12:51:16 +0200134/* Abort conversion to string after a recursion error. */
135static int did_echo_string_emsg = FALSE;
136
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100212/* List heads for garbage collection. Although there can be a reference loop
213 * from partial to dict to partial, we don't need to keep track of the partial,
214 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100300 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
302} vimvars[VV_LEN] =
303{
304 /*
305 * The order here must match the VV_ defines in vim.h!
306 * Initializing a union does not work, leave tv.vval empty to get zero's.
307 */
308 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("count1", VAR_NUMBER), VV_RO},
310 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
311 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
312 {VV_NAME("warningmsg", VAR_STRING), 0},
313 {VV_NAME("statusmsg", VAR_STRING), 0},
314 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
316 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
318 {VV_NAME("termresponse", VAR_STRING), VV_RO},
319 {VV_NAME("fname", VAR_STRING), VV_RO},
320 {VV_NAME("lang", VAR_STRING), VV_RO},
321 {VV_NAME("lc_time", VAR_STRING), VV_RO},
322 {VV_NAME("ctype", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
325 {VV_NAME("fname_in", VAR_STRING), VV_RO},
326 {VV_NAME("fname_out", VAR_STRING), VV_RO},
327 {VV_NAME("fname_new", VAR_STRING), VV_RO},
328 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
329 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
330 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
333 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("progname", VAR_STRING), VV_RO},
335 {VV_NAME("servername", VAR_STRING), VV_RO},
336 {VV_NAME("dying", VAR_NUMBER), VV_RO},
337 {VV_NAME("exception", VAR_STRING), VV_RO},
338 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
339 {VV_NAME("register", VAR_STRING), VV_RO},
340 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
341 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000342 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
343 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000344 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000345 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
346 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000347 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000352 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000353 {VV_NAME("swapname", VAR_STRING), VV_RO},
354 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000355 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200356 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000357 {VV_NAME("mouse_win", VAR_NUMBER), 0},
358 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
359 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000360 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100362 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000363 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200364 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200365 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200366 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200367 {VV_NAME("option_new", VAR_STRING), VV_RO},
368 {VV_NAME("option_old", VAR_STRING), VV_RO},
369 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100370 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371 {VV_NAME("false", VAR_SPECIAL), VV_RO},
372 {VV_NAME("true", VAR_SPECIAL), VV_RO},
373 {VV_NAME("null", VAR_SPECIAL), VV_RO},
374 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100375 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200376 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200434static void list_free_contents(list_T *l);
435static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100436static long list_len(list_T *l);
437static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
438static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
439static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
440static long list_find_nr(list_T *l, long idx, int *errorp);
441static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100442static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
443static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
444static list_T *list_copy(list_T *orig, int deep, int copyID);
445static char_u *list2string(typval_T *tv, int copyID);
446static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
447static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
448static int free_unref_items(int copyID);
449static dictitem_T *dictitem_copy(dictitem_T *org);
450static void dictitem_remove(dict_T *dict, dictitem_T *item);
451static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
452static long dict_len(dict_T *d);
453static char_u *dict2string(typval_T *tv, int copyID);
454static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
455static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100459static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
460static 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 +0100461static void emsg_funcname(char *ermsg, char_u *name);
462static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464static void dict_free_contents(dict_T *d);
465static void dict_free_dict(dict_T *d);
466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200483static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200484static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
485static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100488static void f_asin(typval_T *argvars, typval_T *rettv);
489static void f_atan(typval_T *argvars, typval_T *rettv);
490static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100492static void f_browse(typval_T *argvars, typval_T *rettv);
493static void f_browsedir(typval_T *argvars, typval_T *rettv);
494static void f_bufexists(typval_T *argvars, typval_T *rettv);
495static void f_buflisted(typval_T *argvars, typval_T *rettv);
496static void f_bufloaded(typval_T *argvars, typval_T *rettv);
497static void f_bufname(typval_T *argvars, typval_T *rettv);
498static void f_bufnr(typval_T *argvars, typval_T *rettv);
499static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
500static void f_byte2line(typval_T *argvars, typval_T *rettv);
501static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
502static void f_byteidx(typval_T *argvars, typval_T *rettv);
503static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
504static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100506static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100508#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100509static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100510static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100512static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100513static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100514static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100515static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100516static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
517static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100518static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100520static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
521static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100522static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100523static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100524#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100525static void f_changenr(typval_T *argvars, typval_T *rettv);
526static void f_char2nr(typval_T *argvars, typval_T *rettv);
527static void f_cindent(typval_T *argvars, typval_T *rettv);
528static void f_clearmatches(typval_T *argvars, typval_T *rettv);
529static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000530#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_complete(typval_T *argvars, typval_T *rettv);
532static void f_complete_add(typval_T *argvars, typval_T *rettv);
533static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000534#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100535static void f_confirm(typval_T *argvars, typval_T *rettv);
536static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_cos(typval_T *argvars, typval_T *rettv);
539static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_count(typval_T *argvars, typval_T *rettv);
542static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
543static void f_cursor(typval_T *argsvars, typval_T *rettv);
544static void f_deepcopy(typval_T *argvars, typval_T *rettv);
545static void f_delete(typval_T *argvars, typval_T *rettv);
546static void f_did_filetype(typval_T *argvars, typval_T *rettv);
547static void f_diff_filler(typval_T *argvars, typval_T *rettv);
548static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100549static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_empty(typval_T *argvars, typval_T *rettv);
551static void f_escape(typval_T *argvars, typval_T *rettv);
552static void f_eval(typval_T *argvars, typval_T *rettv);
553static void f_eventhandler(typval_T *argvars, typval_T *rettv);
554static void f_executable(typval_T *argvars, typval_T *rettv);
555static void f_exepath(typval_T *argvars, typval_T *rettv);
556static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200559#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_expand(typval_T *argvars, typval_T *rettv);
561static void f_extend(typval_T *argvars, typval_T *rettv);
562static void f_feedkeys(typval_T *argvars, typval_T *rettv);
563static void f_filereadable(typval_T *argvars, typval_T *rettv);
564static void f_filewritable(typval_T *argvars, typval_T *rettv);
565static void f_filter(typval_T *argvars, typval_T *rettv);
566static void f_finddir(typval_T *argvars, typval_T *rettv);
567static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000568#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100569static void f_float2nr(typval_T *argvars, typval_T *rettv);
570static void f_floor(typval_T *argvars, typval_T *rettv);
571static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000572#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100573static void f_fnameescape(typval_T *argvars, typval_T *rettv);
574static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
575static void f_foldclosed(typval_T *argvars, typval_T *rettv);
576static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
577static void f_foldlevel(typval_T *argvars, typval_T *rettv);
578static void f_foldtext(typval_T *argvars, typval_T *rettv);
579static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
580static void f_foreground(typval_T *argvars, typval_T *rettv);
581static void f_function(typval_T *argvars, typval_T *rettv);
582static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200583static void f_garbagecollect_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100584static 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);
806static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200807#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100808static void f_tan(typval_T *argvars, typval_T *rettv);
809static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200810#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100811#ifdef FEAT_TIMERS
812static void f_timer_start(typval_T *argvars, typval_T *rettv);
813static void f_timer_stop(typval_T *argvars, typval_T *rettv);
814#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100815static void f_tolower(typval_T *argvars, typval_T *rettv);
816static void f_toupper(typval_T *argvars, typval_T *rettv);
817static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000818#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000820#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static void f_type(typval_T *argvars, typval_T *rettv);
822static void f_undofile(typval_T *argvars, typval_T *rettv);
823static void f_undotree(typval_T *argvars, typval_T *rettv);
824static void f_uniq(typval_T *argvars, typval_T *rettv);
825static void f_values(typval_T *argvars, typval_T *rettv);
826static void f_virtcol(typval_T *argvars, typval_T *rettv);
827static void f_visualmode(typval_T *argvars, typval_T *rettv);
828static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100829static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100830static void f_win_getid(typval_T *argvars, typval_T *rettv);
831static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
832static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
833static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100834static void f_winbufnr(typval_T *argvars, typval_T *rettv);
835static void f_wincol(typval_T *argvars, typval_T *rettv);
836static void f_winheight(typval_T *argvars, typval_T *rettv);
837static void f_winline(typval_T *argvars, typval_T *rettv);
838static void f_winnr(typval_T *argvars, typval_T *rettv);
839static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
840static void f_winrestview(typval_T *argvars, typval_T *rettv);
841static void f_winsaveview(typval_T *argvars, typval_T *rettv);
842static void f_winwidth(typval_T *argvars, typval_T *rettv);
843static void f_writefile(typval_T *argvars, typval_T *rettv);
844static void f_wordcount(typval_T *argvars, typval_T *rettv);
845static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000846
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
848static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
849static int get_env_len(char_u **arg);
850static int get_id_len(char_u **arg);
851static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
852static 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 +0000853#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
854#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
855 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100856static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
857static int eval_isnamec(int c);
858static int eval_isnamec1(int c);
859static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
860static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100861static typval_T *alloc_string_tv(char_u *string);
862static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100863#ifdef FEAT_FLOAT
864static float_T get_tv_float(typval_T *varp);
865#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100866static linenr_T get_tv_lnum(typval_T *argvars);
867static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
869static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
870static hashtab_T *find_var_ht(char_u *name, char_u **varname);
871static funccall_T *get_funccal(void);
872static void vars_clear_ext(hashtab_T *ht, int free_val);
873static void delete_var(hashtab_T *ht, hashitem_T *hi);
874static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
875static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
876static void set_var(char_u *name, typval_T *varp, int copy);
877static int var_check_ro(int flags, char_u *name, int use_gettext);
878static int var_check_fixed(int flags, char_u *name, int use_gettext);
879static int var_check_func_name(char_u *name, int new_var);
880static int valid_varname(char_u *varname);
881static int tv_check_lock(int lock, char_u *name, int use_gettext);
882static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
883static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100884static 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 +0100885static int eval_fname_script(char_u *p);
886static int eval_fname_sid(char_u *p);
887static void list_func_head(ufunc_T *fp, int indent);
888static ufunc_T *find_func(char_u *name);
889static int function_exists(char_u *name);
890static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100892static void func_do_profile(ufunc_T *fp);
893static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
894static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000895static int
896# ifdef __BORLANDC__
897 _RTLENTRYF
898# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100899 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000900static int
901# ifdef __BORLANDC__
902 _RTLENTRYF
903# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100904 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100906static int script_autoload(char_u *name, int reload);
907static char_u *autoload_name(char_u *name);
908static void cat_func_name(char_u *buf, ufunc_T *fp);
909static void func_free(ufunc_T *fp);
910static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
911static int can_free_funccal(funccall_T *fc, int copyID) ;
912static void free_funccal(funccall_T *fc, int free_val);
913static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
914static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
915static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
916static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
917static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
918static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
919static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
920static int write_list(FILE *fd, list_T *list, int binary);
921static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000922
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200923
924#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100925static int compare_func_name(const void *s1, const void *s2);
926static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200927#endif
928
Bram Moolenaar33570922005-01-25 22:26:29 +0000929/*
930 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000931 */
932 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100933eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000934{
Bram Moolenaar33570922005-01-25 22:26:29 +0000935 int i;
936 struct vimvar *p;
937
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200938 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
939 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200940 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000941 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943
944 for (i = 0; i < VV_LEN; ++i)
945 {
946 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200947 if (STRLEN(p->vv_name) > 16)
948 {
949 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
950 getout(1);
951 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 STRCPY(p->vv_di.di_key, p->vv_name);
953 if (p->vv_flags & VV_RO)
954 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
955 else if (p->vv_flags & VV_RO_SBX)
956 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
957 else
958 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000959
960 /* add to v: scope dict, unless the value is not always available */
961 if (p->vv_type != VAR_UNKNOWN)
962 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000963 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000964 /* add to compat scope dict */
965 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100967 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
968
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000969 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100970 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200971 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100972 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100973
974 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
975 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
976 set_vim_var_nr(VV_NONE, VVAL_NONE);
977 set_vim_var_nr(VV_NULL, VVAL_NULL);
978
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200979 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200980
981#ifdef EBCDIC
982 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100983 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200984 */
985 sortFunctions();
986#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000987}
988
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989#if defined(EXITFREE) || defined(PROTO)
990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100991eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992{
993 int i;
994 struct vimvar *p;
995
996 for (i = 0; i < VV_LEN; ++i)
997 {
998 p = &vimvars[i];
999 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001000 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001001 vim_free(p->vv_str);
1002 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001003 }
1004 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1005 {
1006 list_unref(p->vv_list);
1007 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001008 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001009 }
1010 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001011 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001012 hash_clear(&compat_hashtab);
1013
Bram Moolenaard9fba312005-06-26 22:34:35 +00001014 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001015# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001016 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001017# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001018
1019 /* global variables */
1020 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001021
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001022 /* autoloaded script names */
1023 ga_clear_strings(&ga_loaded);
1024
Bram Moolenaarcca74132013-09-25 21:00:28 +02001025 /* Script-local variables. First clear all the variables and in a second
1026 * loop free the scriptvar_T, because a variable in one script might hold
1027 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001028 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001029 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001030 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001031 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001032 ga_clear(&ga_scripts);
1033
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001034 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001035 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001036
1037 /* functions */
1038 free_all_functions();
1039 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001040}
1041#endif
1042
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 * Return the name of the executed function.
1045 */
1046 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001047func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050}
1051
1052/*
1053 * Return the address holding the next breakpoint line for a funccall cookie.
1054 */
1055 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001056func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
Bram Moolenaar33570922005-01-25 22:26:29 +00001058 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059}
1060
1061/*
1062 * Return the address holding the debug tick for a funccall cookie.
1063 */
1064 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001065func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaar33570922005-01-25 22:26:29 +00001067 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068}
1069
1070/*
1071 * Return the nesting level for a funccall cookie.
1072 */
1073 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001074func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077}
1078
1079/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001080funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001082/* pointer to list of previously used funccal, still around because some
1083 * item in it is still being used. */
1084funccall_T *previous_funccal = NULL;
1085
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086/*
1087 * Return TRUE when a function was ended by a ":return" command.
1088 */
1089 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001090current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 return current_funccal->returned;
1093}
1094
1095
1096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 * Set an internal variable to a string value. Creates the variable if it does
1098 * not already exist.
1099 */
1100 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001101set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001103 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001104 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
1106 val = vim_strsave(value);
1107 if (val != NULL)
1108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001109 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001110 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001112 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115 }
1116}
1117
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001119static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120static char_u *redir_endp = NULL;
1121static char_u *redir_varname = NULL;
1122
1123/*
1124 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001125 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 * Returns OK if successfully completed the setup. FAIL otherwise.
1127 */
1128 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001129var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130{
1131 int save_emsg;
1132 int err;
1133 typval_T tv;
1134
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001135 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001136 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 {
1138 EMSG(_(e_invarg));
1139 return FAIL;
1140 }
1141
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 redir_varname = vim_strsave(name);
1144 if (redir_varname == NULL)
1145 return FAIL;
1146
1147 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1148 if (redir_lval == NULL)
1149 {
1150 var_redir_stop();
1151 return FAIL;
1152 }
1153
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001154 /* The output is stored in growarray "redir_ga" until redirection ends. */
1155 ga_init2(&redir_ga, (int)sizeof(char), 500);
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001158 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001159 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1161 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001162 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (redir_endp != NULL && *redir_endp != NUL)
1164 /* Trailing characters are present after the variable name */
1165 EMSG(_(e_trailing));
1166 else
1167 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001168 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 var_redir_stop();
1170 return FAIL;
1171 }
1172
1173 /* check if we can write to the variable: set it to or append an empty
1174 * string */
1175 save_emsg = did_emsg;
1176 did_emsg = FALSE;
1177 tv.v_type = VAR_STRING;
1178 tv.vval.v_string = (char_u *)"";
1179 if (append)
1180 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1181 else
1182 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001183 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001185 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 if (err)
1187 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 var_redir_stop();
1190 return FAIL;
1191 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192
1193 return OK;
1194}
1195
1196/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197 * Append "value[value_len]" to the variable set by var_redir_start().
1198 * The actual appending is postponed until redirection ends, because the value
1199 * appended may in fact be the string we write to, changing it may cause freed
1200 * memory to be used:
1201 * :redir => foo
1202 * :let foo
1203 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 */
1205 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001206var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001208 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209
1210 if (redir_lval == NULL)
1211 return;
1212
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001213 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001214 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001215 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001216 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001218 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001219 {
1220 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001221 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001222 }
1223 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001224 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001225}
1226
1227/*
1228 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001229 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230 */
1231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001232var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001233{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234 typval_T tv;
1235
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236 if (redir_lval != NULL)
1237 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001238 /* If there was no error: assign the text to the variable. */
1239 if (redir_endp != NULL)
1240 {
1241 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1242 tv.v_type = VAR_STRING;
1243 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001244 /* Call get_lval() again, if it's inside a Dict or List it may
1245 * have changed. */
1246 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001247 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001248 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1249 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1250 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001251 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001252
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001253 /* free the collected output */
1254 vim_free(redir_ga.ga_data);
1255 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001256
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001257 vim_free(redir_lval);
1258 redir_lval = NULL;
1259 }
1260 vim_free(redir_varname);
1261 redir_varname = NULL;
1262}
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264# if defined(FEAT_MBYTE) || defined(PROTO)
1265 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001266eval_charconvert(
1267 char_u *enc_from,
1268 char_u *enc_to,
1269 char_u *fname_from,
1270 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272 int err = FALSE;
1273
1274 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1275 set_vim_var_string(VV_CC_TO, enc_to, -1);
1276 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1277 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1278 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1279 err = TRUE;
1280 set_vim_var_string(VV_CC_FROM, NULL, -1);
1281 set_vim_var_string(VV_CC_TO, NULL, -1);
1282 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1283 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1284
1285 if (err)
1286 return FAIL;
1287 return OK;
1288}
1289# endif
1290
1291# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001293eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 int err = FALSE;
1296
1297 set_vim_var_string(VV_FNAME_IN, fname, -1);
1298 set_vim_var_string(VV_CMDARG, args, -1);
1299 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1300 err = TRUE;
1301 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1302 set_vim_var_string(VV_CMDARG, NULL, -1);
1303
1304 if (err)
1305 {
1306 mch_remove(fname);
1307 return FAIL;
1308 }
1309 return OK;
1310}
1311# endif
1312
1313# if defined(FEAT_DIFF) || defined(PROTO)
1314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001315eval_diff(
1316 char_u *origfile,
1317 char_u *newfile,
1318 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int err = FALSE;
1321
1322 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1323 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1324 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1325 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1326 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1327 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1328 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1329}
1330
1331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332eval_patch(
1333 char_u *origfile,
1334 char_u *difffile,
1335 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 int err;
1338
1339 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1340 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1341 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1342 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1343 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1344 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1345 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1346}
1347# endif
1348
1349/*
1350 * Top level evaluation function, returning a boolean.
1351 * Sets "error" to TRUE if there was an error.
1352 * Return TRUE or FALSE.
1353 */
1354 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001355eval_to_bool(
1356 char_u *arg,
1357 int *error,
1358 char_u **nextcmd,
1359 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 int retval = FALSE;
1363
1364 if (skip)
1365 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 else
1369 {
1370 *error = FALSE;
1371 if (!skip)
1372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001373 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376 }
1377 if (skip)
1378 --emsg_skip;
1379
1380 return retval;
1381}
1382
1383/*
1384 * Top level evaluation function, returning a string. If "skip" is TRUE,
1385 * only parsing to "nextcmd" is done, without reporting errors. Return
1386 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1387 */
1388 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001389eval_to_string_skip(
1390 char_u *arg,
1391 char_u **nextcmd,
1392 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
Bram Moolenaar33570922005-01-25 22:26:29 +00001394 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 char_u *retval;
1396
1397 if (skip)
1398 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 retval = NULL;
1401 else
1402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403 retval = vim_strsave(get_tv_string(&tv));
1404 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 }
1406 if (skip)
1407 --emsg_skip;
1408
1409 return retval;
1410}
1411
1412/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001413 * Skip over an expression at "*pp".
1414 * Return FAIL for an error, OK otherwise.
1415 */
1416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001417skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001418{
Bram Moolenaar33570922005-01-25 22:26:29 +00001419 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001420
1421 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001423}
1424
1425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001427 * When "convert" is TRUE convert a List into a sequence of lines and convert
1428 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 * Return pointer to allocated memory, or NULL for failure.
1430 */
1431 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001432eval_to_string(
1433 char_u *arg,
1434 char_u **nextcmd,
1435 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
Bram Moolenaar33570922005-01-25 22:26:29 +00001437 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001439 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001440#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001441 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 retval = NULL;
1446 else
1447 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001448 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001449 {
1450 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001451 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001452 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001453 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001454 if (tv.vval.v_list->lv_len > 0)
1455 ga_append(&ga, NL);
1456 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001457 ga_append(&ga, NUL);
1458 retval = (char_u *)ga.ga_data;
1459 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001460#ifdef FEAT_FLOAT
1461 else if (convert && tv.v_type == VAR_FLOAT)
1462 {
1463 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1464 retval = vim_strsave(numbuf);
1465 }
1466#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001467 else
1468 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
1471
1472 return retval;
1473}
1474
1475/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001476 * Call eval_to_string() without using current local variables and using
1477 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 */
1479 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001480eval_to_string_safe(
1481 char_u *arg,
1482 char_u **nextcmd,
1483 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 char_u *retval;
1486 void *save_funccalp;
1487
1488 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001489 if (use_sandbox)
1490 ++sandbox;
1491 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001492 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001493 if (use_sandbox)
1494 --sandbox;
1495 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 restore_funccal(save_funccalp);
1497 return retval;
1498}
1499
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500/*
1501 * Top level evaluation function, returning a number.
1502 * Evaluates "expr" silently.
1503 * Returns -1 for an error.
1504 */
1505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001506eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
Bram Moolenaar33570922005-01-25 22:26:29 +00001508 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001510 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
1512 ++emsg_off;
1513
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 retval = -1;
1516 else
1517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001518 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001519 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 }
1521 --emsg_off;
1522
1523 return retval;
1524}
1525
Bram Moolenaara40058a2005-07-11 22:42:07 +00001526/*
1527 * Prepare v: variable "idx" to be used.
1528 * Save the current typeval in "save_tv".
1529 * When not used yet add the variable to the v: hashtable.
1530 */
1531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001532prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001533{
1534 *save_tv = vimvars[idx].vv_tv;
1535 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1536 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1537}
1538
1539/*
1540 * Restore v: variable "idx" to typeval "save_tv".
1541 * When no longer defined, remove the variable from the v: hashtable.
1542 */
1543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001545{
1546 hashitem_T *hi;
1547
Bram Moolenaara40058a2005-07-11 22:42:07 +00001548 vimvars[idx].vv_tv = *save_tv;
1549 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1550 {
1551 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1552 if (HASHITEM_EMPTY(hi))
1553 EMSG2(_(e_intern2), "restore_vimvar()");
1554 else
1555 hash_remove(&vimvarht, hi);
1556 }
1557}
1558
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001559#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001560/*
1561 * Evaluate an expression to a list with suggestions.
1562 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001563 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001564 */
1565 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001566eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567{
1568 typval_T save_val;
1569 typval_T rettv;
1570 list_T *list = NULL;
1571 char_u *p = skipwhite(expr);
1572
1573 /* Set "v:val" to the bad word. */
1574 prepare_vimvar(VV_VAL, &save_val);
1575 vimvars[VV_VAL].vv_type = VAR_STRING;
1576 vimvars[VV_VAL].vv_str = badword;
1577 if (p_verbose == 0)
1578 ++emsg_off;
1579
1580 if (eval1(&p, &rettv, TRUE) == OK)
1581 {
1582 if (rettv.v_type != VAR_LIST)
1583 clear_tv(&rettv);
1584 else
1585 list = rettv.vval.v_list;
1586 }
1587
1588 if (p_verbose == 0)
1589 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001590 restore_vimvar(VV_VAL, &save_val);
1591
1592 return list;
1593}
1594
1595/*
1596 * "list" is supposed to contain two items: a word and a number. Return the
1597 * word in "pp" and the number as the return value.
1598 * Return -1 if anything isn't right.
1599 * Used to get the good word and score from the eval_spell_expr() result.
1600 */
1601 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001602get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001603{
1604 listitem_T *li;
1605
1606 li = list->lv_first;
1607 if (li == NULL)
1608 return -1;
1609 *pp = get_tv_string(&li->li_tv);
1610
1611 li = li->li_next;
1612 if (li == NULL)
1613 return -1;
1614 return get_tv_number(&li->li_tv);
1615}
1616#endif
1617
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001618/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001619 * Top level evaluation function.
1620 * Returns an allocated typval_T with the result.
1621 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001622 */
1623 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001624eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001625{
1626 typval_T *tv;
1627
1628 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001629 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001630 {
1631 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001633 }
1634
1635 return tv;
1636}
1637
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641 * Uses argv[argc] for the function arguments. Only Number and String
1642 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001645 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646call_vim_function(
1647 char_u *func,
1648 int argc,
1649 char_u **argv,
1650 int safe, /* use the sandbox */
1651 int str_arg_only, /* all arguments are strings */
1652 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaar33570922005-01-25 22:26:29 +00001654 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 long n;
1656 int len;
1657 int i;
1658 int doesrange;
1659 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001662 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001664 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666 for (i = 0; i < argc; i++)
1667 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001668 /* Pass a NULL or empty argument as an empty string */
1669 if (argv[i] == NULL || *argv[i] == NUL)
1670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001671 argvars[i].v_type = VAR_STRING;
1672 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001673 continue;
1674 }
1675
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001676 if (str_arg_only)
1677 len = 0;
1678 else
1679 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001680 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (len != 0 && len == (int)STRLEN(argv[i]))
1682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001683 argvars[i].v_type = VAR_NUMBER;
1684 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
1686 else
1687 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001688 argvars[i].v_type = VAR_STRING;
1689 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 }
1691 }
1692
1693 if (safe)
1694 {
1695 save_funccalp = save_funccal();
1696 ++sandbox;
1697 }
1698
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1700 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001702 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (safe)
1704 {
1705 --sandbox;
1706 restore_funccal(save_funccalp);
1707 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 vim_free(argvars);
1709
1710 if (ret == FAIL)
1711 clear_tv(rettv);
1712
1713 return ret;
1714}
1715
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001716/*
1717 * Call vimL function "func" and return the result as a number.
1718 * Returns -1 when calling the function fails.
1719 * Uses argv[argc] for the function arguments.
1720 */
1721 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001722call_func_retnr(
1723 char_u *func,
1724 int argc,
1725 char_u **argv,
1726 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001727{
1728 typval_T rettv;
1729 long retval;
1730
1731 /* All arguments are passed as strings, no conversion to number. */
1732 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1733 return -1;
1734
1735 retval = get_tv_number_chk(&rettv, NULL);
1736 clear_tv(&rettv);
1737 return retval;
1738}
1739
1740#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1741 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1742
Bram Moolenaar4f688582007-07-24 12:34:30 +00001743# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001745 * Call vimL function "func" and return the result as a string.
1746 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747 * Uses argv[argc] for the function arguments.
1748 */
1749 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750call_func_retstr(
1751 char_u *func,
1752 int argc,
1753 char_u **argv,
1754 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755{
1756 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001757 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001759 /* All arguments are passed as strings, no conversion to number. */
1760 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761 return NULL;
1762
1763 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001764 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 return retval;
1766}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001767# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001768
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001769/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001770 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001771 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001772 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773 */
1774 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775call_func_retlist(
1776 char_u *func,
1777 int argc,
1778 char_u **argv,
1779 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780{
1781 typval_T rettv;
1782
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001783 /* All arguments are passed as strings, no conversion to number. */
1784 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785 return NULL;
1786
1787 if (rettv.v_type != VAR_LIST)
1788 {
1789 clear_tv(&rettv);
1790 return NULL;
1791 }
1792
1793 return rettv.vval.v_list;
1794}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#endif
1796
1797/*
1798 * Save the current function call pointer, and set it to NULL.
1799 * Used when executing autocommands and for ":source".
1800 */
1801 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001804 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 current_funccal = NULL;
1807 return (void *)fc;
1808}
1809
1810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001813 funccall_T *fc = (funccall_T *)vfc;
1814
1815 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816}
1817
Bram Moolenaar05159a02005-02-26 23:04:13 +00001818#if defined(FEAT_PROFILE) || defined(PROTO)
1819/*
1820 * Prepare profiling for entering a child or something else that is not
1821 * counted for the script/function itself.
1822 * Should always be called in pair with prof_child_exit().
1823 */
1824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001825prof_child_enter(
1826 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001827{
1828 funccall_T *fc = current_funccal;
1829
1830 if (fc != NULL && fc->func->uf_profiling)
1831 profile_start(&fc->prof_child);
1832 script_prof_save(tm);
1833}
1834
1835/*
1836 * Take care of time spent in a child.
1837 * Should always be called after prof_child_enter().
1838 */
1839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840prof_child_exit(
1841 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001842{
1843 funccall_T *fc = current_funccal;
1844
1845 if (fc != NULL && fc->func->uf_profiling)
1846 {
1847 profile_end(&fc->prof_child);
1848 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1849 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1850 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1851 }
1852 script_prof_restore(tm);
1853}
1854#endif
1855
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857#ifdef FEAT_FOLDING
1858/*
1859 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1860 * it in "*cp". Doesn't give error messages.
1861 */
1862 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001863eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
Bram Moolenaar33570922005-01-25 22:26:29 +00001865 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 int retval;
1867 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001868 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1869 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
1871 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001872 if (use_sandbox)
1873 ++sandbox;
1874 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 retval = 0;
1878 else
1879 {
1880 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 if (tv.v_type == VAR_NUMBER)
1882 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001883 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 retval = 0;
1885 else
1886 {
1887 /* If the result is a string, check if there is a non-digit before
1888 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (!VIM_ISDIGIT(*s) && *s != '-')
1891 *cp = *s++;
1892 retval = atol((char *)s);
1893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001897 if (use_sandbox)
1898 --sandbox;
1899 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 return retval;
1902}
1903#endif
1904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 * ":let" list all variable values
1907 * ":let var1 var2" list variable values
1908 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 * ":let var += expr" assignment command.
1910 * ":let var -= expr" assignment command.
1911 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 */
1914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001915ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 int var_count = 0;
1922 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001924 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaardb552d602006-03-23 22:59:57 +00001927 argend = skip_var_list(arg, &var_count, &semicolon);
1928 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001930 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1931 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001932 expr = skipwhite(argend);
1933 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1934 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001936 /*
1937 * ":let" without "=": list variables
1938 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 if (*arg == '[')
1940 EMSG(_(e_invarg));
1941 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001943 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001944 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001947 list_glob_vars(&first);
1948 list_buf_vars(&first);
1949 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001950#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001951 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001952#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001953 list_script_vars(&first);
1954 list_func_vars(&first);
1955 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 eap->nextcmd = check_nextcmd(arg);
1958 }
1959 else
1960 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 op[0] = '=';
1962 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001963 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001965 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1966 op[0] = *expr; /* +=, -= or .= */
1967 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001969 else
1970 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (eap->skip)
1973 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (eap->skip)
1976 {
1977 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 --emsg_skip;
1980 }
1981 else if (i != FAIL)
1982 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001984 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001985 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 }
1988}
1989
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990/*
1991 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1992 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 * When "nextchars" is not NULL it points to a string with characters that
1994 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1995 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 * Returns OK or FAIL;
1997 */
1998 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001999ex_let_vars(
2000 char_u *arg_start,
2001 typval_T *tv,
2002 int copy, /* copy values from "tv", don't move */
2003 int semicolon, /* from skip_var_list() */
2004 int var_count, /* from skip_var_list() */
2005 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006{
2007 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002008 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002010 listitem_T *item;
2011 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012
2013 if (*arg != '[')
2014 {
2015 /*
2016 * ":let var = expr" or ":for var in list"
2017 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002018 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 return FAIL;
2020 return OK;
2021 }
2022
2023 /*
2024 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2025 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002026 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 {
2028 EMSG(_(e_listreq));
2029 return FAIL;
2030 }
2031
2032 i = list_len(l);
2033 if (semicolon == 0 && var_count < i)
2034 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002035 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002036 return FAIL;
2037 }
2038 if (var_count - semicolon > i)
2039 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002040 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 return FAIL;
2042 }
2043
2044 item = l->lv_first;
2045 while (*arg != ']')
2046 {
2047 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002048 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002049 item = item->li_next;
2050 if (arg == NULL)
2051 return FAIL;
2052
2053 arg = skipwhite(arg);
2054 if (*arg == ';')
2055 {
2056 /* Put the rest of the list (may be empty) in the var after ';'.
2057 * Create a new list for this. */
2058 l = list_alloc();
2059 if (l == NULL)
2060 return FAIL;
2061 while (item != NULL)
2062 {
2063 list_append_tv(l, &item->li_tv);
2064 item = item->li_next;
2065 }
2066
2067 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002068 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069 ltv.vval.v_list = l;
2070 l->lv_refcount = 1;
2071
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002072 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2073 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002074 clear_tv(&ltv);
2075 if (arg == NULL)
2076 return FAIL;
2077 break;
2078 }
2079 else if (*arg != ',' && *arg != ']')
2080 {
2081 EMSG2(_(e_intern2), "ex_let_vars()");
2082 return FAIL;
2083 }
2084 }
2085
2086 return OK;
2087}
2088
2089/*
2090 * Skip over assignable variable "var" or list of variables "[var, var]".
2091 * Used for ":let varvar = expr" and ":for varvar in expr".
2092 * For "[var, var]" increment "*var_count" for each variable.
2093 * for "[var, var; var]" set "semicolon".
2094 * Return NULL for an error.
2095 */
2096 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002097skip_var_list(
2098 char_u *arg,
2099 int *var_count,
2100 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002101{
2102 char_u *p, *s;
2103
2104 if (*arg == '[')
2105 {
2106 /* "[var, var]": find the matching ']'. */
2107 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002108 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002109 {
2110 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2111 s = skip_var_one(p);
2112 if (s == p)
2113 {
2114 EMSG2(_(e_invarg2), p);
2115 return NULL;
2116 }
2117 ++*var_count;
2118
2119 p = skipwhite(s);
2120 if (*p == ']')
2121 break;
2122 else if (*p == ';')
2123 {
2124 if (*semicolon == 1)
2125 {
2126 EMSG(_("Double ; in list of variables"));
2127 return NULL;
2128 }
2129 *semicolon = 1;
2130 }
2131 else if (*p != ',')
2132 {
2133 EMSG2(_(e_invarg2), p);
2134 return NULL;
2135 }
2136 }
2137 return p + 1;
2138 }
2139 else
2140 return skip_var_one(arg);
2141}
2142
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002143/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002144 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002145 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002147 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002148skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002149{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002150 if (*arg == '@' && arg[1] != NUL)
2151 return arg + 2;
2152 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2153 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002154}
2155
Bram Moolenaara7043832005-01-21 11:56:39 +00002156/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002157 * List variables for hashtab "ht" with prefix "prefix".
2158 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161list_hashtable_vars(
2162 hashtab_T *ht,
2163 char_u *prefix,
2164 int empty,
2165 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002166{
Bram Moolenaar33570922005-01-25 22:26:29 +00002167 hashitem_T *hi;
2168 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002169 int todo;
2170
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002171 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002172 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2173 {
2174 if (!HASHITEM_EMPTY(hi))
2175 {
2176 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002177 di = HI2DI(hi);
2178 if (empty || di->di_tv.v_type != VAR_STRING
2179 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002181 }
2182 }
2183}
2184
2185/*
2186 * List global variables.
2187 */
2188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002190{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002192}
2193
2194/*
2195 * List buffer variables.
2196 */
2197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002198list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002199{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 char_u numbuf[NUMBUFLEN];
2201
Bram Moolenaar429fa852013-04-15 12:27:36 +02002202 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204
2205 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2207 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002208}
2209
2210/*
2211 * List window variables.
2212 */
2213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002215{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002216 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002218}
2219
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002220#ifdef FEAT_WINDOWS
2221/*
2222 * List tab page variables.
2223 */
2224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002225list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002226{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002227 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002229}
2230#endif
2231
Bram Moolenaara7043832005-01-21 11:56:39 +00002232/*
2233 * List Vim variables.
2234 */
2235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002238 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239}
2240
2241/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002242 * List script-local variables, if there is a script.
2243 */
2244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002245list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002246{
2247 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002248 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2249 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002250}
2251
2252/*
2253 * List function variables, if there is a function.
2254 */
2255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002256list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002257{
2258 if (current_funccal != NULL)
2259 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002261}
2262
2263/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 * List variables in "arg".
2265 */
2266 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002267list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268{
2269 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 char_u *name_start;
2273 char_u *arg_subsc;
2274 char_u *tofree;
2275 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276
2277 while (!ends_excmd(*arg) && !got_int)
2278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002281 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2283 {
2284 emsg_severe = TRUE;
2285 EMSG(_(e_trailing));
2286 break;
2287 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 /* get_name_len() takes care of expanding curly braces */
2292 name_start = name = arg;
2293 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2294 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 /* This is mainly to keep test 49 working: when expanding
2297 * curly braces fails overrule the exception error message. */
2298 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 emsg_severe = TRUE;
2301 EMSG2(_(e_invarg2), arg);
2302 break;
2303 }
2304 error = TRUE;
2305 }
2306 else
2307 {
2308 if (tofree != NULL)
2309 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002310 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 else
2313 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 /* handle d.key, l[idx], f(expr) */
2315 arg_subsc = arg;
2316 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002317 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002321 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002323 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002324 case 'g': list_glob_vars(first); break;
2325 case 'b': list_buf_vars(first); break;
2326 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002327#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002328 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002329#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002330 case 'v': list_vim_vars(first); break;
2331 case 's': list_script_vars(first); break;
2332 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333 default:
2334 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002335 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 }
2337 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002338 {
2339 char_u numbuf[NUMBUFLEN];
2340 char_u *tf;
2341 int c;
2342 char_u *s;
2343
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002344 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 c = *arg;
2346 *arg = NUL;
2347 list_one_var_a((char_u *)"",
2348 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002349 tv.v_type,
2350 s == NULL ? (char_u *)"" : s,
2351 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002352 *arg = c;
2353 vim_free(tf);
2354 }
2355 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 }
2358 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002359
2360 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002362
2363 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 }
2365
2366 return arg;
2367}
2368
2369/*
2370 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2371 * Returns a pointer to the char just after the var name.
2372 * Returns NULL if there is an error.
2373 */
2374 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002375ex_let_one(
2376 char_u *arg, /* points to variable name */
2377 typval_T *tv, /* value to assign to variable */
2378 int copy, /* copy value from "tv" */
2379 char_u *endchars, /* valid chars after variable name or NULL */
2380 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381{
2382 int c1;
2383 char_u *name;
2384 char_u *p;
2385 char_u *arg_end = NULL;
2386 int len;
2387 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389
2390 /*
2391 * ":let $VAR = expr": Set environment variable.
2392 */
2393 if (*arg == '$')
2394 {
2395 /* Find the end of the name. */
2396 ++arg;
2397 name = arg;
2398 len = get_env_len(&arg);
2399 if (len == 0)
2400 EMSG2(_(e_invarg2), name - 1);
2401 else
2402 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 if (op != NULL && (*op == '+' || *op == '-'))
2404 EMSG2(_(e_letwrong), op);
2405 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002406 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002408 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 {
2410 c1 = name[len];
2411 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002412 p = get_tv_string_chk(tv);
2413 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 {
2415 int mustfree = FALSE;
2416 char_u *s = vim_getenv(name, &mustfree);
2417
2418 if (s != NULL)
2419 {
2420 p = tofree = concat_str(s, p);
2421 if (mustfree)
2422 vim_free(s);
2423 }
2424 }
2425 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002428 if (STRICMP(name, "HOME") == 0)
2429 init_homedir();
2430 else if (didset_vim && STRICMP(name, "VIM") == 0)
2431 didset_vim = FALSE;
2432 else if (didset_vimruntime
2433 && STRICMP(name, "VIMRUNTIME") == 0)
2434 didset_vimruntime = FALSE;
2435 arg_end = arg;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 }
2440 }
2441 }
2442
2443 /*
2444 * ":let &option = expr": Set option value.
2445 * ":let &l:option = expr": Set local option value.
2446 * ":let &g:option = expr": Set global option value.
2447 */
2448 else if (*arg == '&')
2449 {
2450 /* Find the end of the name. */
2451 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002452 if (p == NULL || (endchars != NULL
2453 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 EMSG(_(e_letunexp));
2455 else
2456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 long n;
2458 int opt_type;
2459 long numval;
2460 char_u *stringval = NULL;
2461 char_u *s;
2462
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 c1 = *p;
2464 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465
2466 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 s = get_tv_string_chk(tv); /* != NULL if number or string */
2468 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 {
2470 opt_type = get_option_value(arg, &numval,
2471 &stringval, opt_flags);
2472 if ((opt_type == 1 && *op == '.')
2473 || (opt_type == 0 && *op != '.'))
2474 EMSG2(_(e_letwrong), op);
2475 else
2476 {
2477 if (opt_type == 1) /* number */
2478 {
2479 if (*op == '+')
2480 n = numval + n;
2481 else
2482 n = numval - n;
2483 }
2484 else if (opt_type == 0 && stringval != NULL) /* string */
2485 {
2486 s = concat_str(stringval, s);
2487 vim_free(stringval);
2488 stringval = s;
2489 }
2490 }
2491 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 if (s != NULL)
2493 {
2494 set_option_value(arg, n, s, opt_flags);
2495 arg_end = p;
2496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002499 }
2500 }
2501
2502 /*
2503 * ":let @r = expr": Set register contents.
2504 */
2505 else if (*arg == '@')
2506 {
2507 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 if (op != NULL && (*op == '+' || *op == '-'))
2509 EMSG2(_(e_letwrong), op);
2510 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002511 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 EMSG(_(e_letunexp));
2513 else
2514 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002515 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002516 char_u *s;
2517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002518 p = get_tv_string_chk(tv);
2519 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002520 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002521 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002522 if (s != NULL)
2523 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002524 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002525 vim_free(s);
2526 }
2527 }
2528 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002529 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002530 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002531 arg_end = arg + 1;
2532 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002533 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 }
2535 }
2536
2537 /*
2538 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002541 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002545 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2549 EMSG(_(e_letunexp));
2550 else
2551 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002552 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 arg_end = p;
2554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 }
2558
2559 else
2560 EMSG2(_(e_invarg2), arg);
2561
2562 return arg_end;
2563}
2564
2565/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2567 */
2568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002569check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570{
2571 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2572 {
2573 EMSG2(_(e_readonlyvar), arg);
2574 return TRUE;
2575 }
2576 return FALSE;
2577}
2578
2579/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 * Get an lval: variable, Dict item or List item that can be assigned a value
2581 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2582 * "name.key", "name.key[expr]" etc.
2583 * Indexing only works if "name" is an existing List or Dictionary.
2584 * "name" points to the start of the name.
2585 * If "rettv" is not NULL it points to the value to be assigned.
2586 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2587 * wrong; must end in space or cmd separator.
2588 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002589 * flags:
2590 * GLV_QUIET: do not give error messages
2591 * GLV_NO_AUTOLOAD: do not use script autoloading
2592 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002594 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 */
2597 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002598get_lval(
2599 char_u *name,
2600 typval_T *rettv,
2601 lval_T *lp,
2602 int unlet,
2603 int skip,
2604 int flags, /* GLV_ values */
2605 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002607 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 char_u *expr_start, *expr_end;
2609 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002610 dictitem_T *v;
2611 typval_T var1;
2612 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002614 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002618 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002621 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622
2623 if (skip)
2624 {
2625 /* When skipping just find the end of the name. */
2626 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002627 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 }
2629
2630 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002631 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (expr_start != NULL)
2633 {
2634 /* Don't expand the name when we already know there is an error. */
2635 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2636 && *p != '[' && *p != '.')
2637 {
2638 EMSG(_(e_trailing));
2639 return NULL;
2640 }
2641
2642 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2643 if (lp->ll_exp_name == NULL)
2644 {
2645 /* Report an invalid expression in braces, unless the
2646 * expression evaluation has been cancelled due to an
2647 * aborting error, an interrupt, or an exception. */
2648 if (!aborting() && !quiet)
2649 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002650 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 EMSG2(_(e_invarg2), name);
2652 return NULL;
2653 }
2654 }
2655 lp->ll_name = lp->ll_exp_name;
2656 }
2657 else
2658 lp->ll_name = name;
2659
2660 /* Without [idx] or .key we are done. */
2661 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2662 return p;
2663
2664 cc = *p;
2665 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002666 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (v == NULL && !quiet)
2668 EMSG2(_(e_undefvar), lp->ll_name);
2669 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 if (v == NULL)
2671 return NULL;
2672
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 /*
2674 * Loop until no more [idx] or .key is following.
2675 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002676 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2680 && !(lp->ll_tv->v_type == VAR_DICT
2681 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
2684 EMSG(_("E689: Can only index a List or Dictionary"));
2685 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (!quiet)
2690 EMSG(_("E708: [:] must come last"));
2691 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002692 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 len = -1;
2695 if (*p == '.')
2696 {
2697 key = p + 1;
2698 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2699 ;
2700 if (len == 0)
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!quiet)
2703 EMSG(_(e_emptykey));
2704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706 p = key + len;
2707 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708 else
2709 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (*p == ':')
2713 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 else
2715 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 empty1 = FALSE;
2717 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002719 if (get_tv_string_chk(&var1) == NULL)
2720 {
2721 /* not a number or string */
2722 clear_tv(&var1);
2723 return NULL;
2724 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 }
2726
2727 /* Optionally get the second index [ :expr]. */
2728 if (*p == ':')
2729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002733 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002734 if (!empty1)
2735 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (rettv != NULL && (rettv->v_type != VAR_LIST
2739 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (!quiet)
2742 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 if (!empty1)
2744 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747 p = skipwhite(p + 1);
2748 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 else
2751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2754 {
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 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002759 if (get_tv_string_chk(&var2) == NULL)
2760 {
2761 /* not a number or string */
2762 if (!empty1)
2763 clear_tv(&var1);
2764 clear_tv(&var2);
2765 return NULL;
2766 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002772
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002774 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (!quiet)
2776 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 if (!empty1)
2778 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 }
2783
2784 /* Skip to past ']'. */
2785 ++p;
2786 }
2787
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 {
2790 if (len == -1)
2791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002793 key = get_tv_string_chk(&var1); /* is number or string */
2794 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 }
2799 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002800 lp->ll_list = NULL;
2801 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002802 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002804 /* When assigning to a scope dictionary check that a function and
2805 * variable name is valid (only variable name unless it is l: or
2806 * g: dictionary). Disallow overwriting a builtin function. */
2807 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002809 int prevval;
2810 int wrong;
2811
2812 if (len != -1)
2813 {
2814 prevval = key[len];
2815 key[len] = NUL;
2816 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002817 else
2818 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002819 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2820 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002821 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002822 || !valid_varname(key);
2823 if (len != -1)
2824 key[len] = prevval;
2825 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002826 return NULL;
2827 }
2828
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002831 /* Can't add "v:" variable. */
2832 if (lp->ll_dict == &vimvardict)
2833 {
2834 EMSG2(_(e_illvar), name);
2835 return NULL;
2836 }
2837
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002838 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002842 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 if (len == -1)
2844 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 }
2847 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 if (len == -1)
2852 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 p = NULL;
2855 break;
2856 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002857 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002858 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002859 return NULL;
2860
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 if (len == -1)
2862 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 }
2865 else
2866 {
2867 /*
2868 * Get the number and item for the only or first index of the List.
2869 */
2870 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 else
2873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002874 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 clear_tv(&var1);
2876 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 lp->ll_list = lp->ll_tv->vval.v_list;
2879 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2880 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002882 if (lp->ll_n1 < 0)
2883 {
2884 lp->ll_n1 = 0;
2885 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2886 }
2887 }
2888 if (lp->ll_li == NULL)
2889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 if (!quiet)
2893 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 }
2896
2897 /*
2898 * May need to find the item or absolute index for the second
2899 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 * When no index given: "lp->ll_empty2" is TRUE.
2901 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002902 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002905 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002906 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002910 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002911 {
2912 if (!quiet)
2913 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002915 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917 }
2918
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2920 if (lp->ll_n1 < 0)
2921 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2922 if (lp->ll_n2 < lp->ll_n1)
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 Moolenaar6cc16192005-01-08 21:49:45 +00002928 }
2929
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002931 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002932 }
2933
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 return p;
2935}
2936
2937/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002938 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 */
2940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002941clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942{
2943 vim_free(lp->ll_exp_name);
2944 vim_free(lp->ll_newkey);
2945}
2946
2947/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 */
2952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002953set_var_lval(
2954 lval_T *lp,
2955 char_u *endp,
2956 typval_T *rettv,
2957 int copy,
2958 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959{
2960 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002961 listitem_T *ri;
2962 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963
2964 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002965 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 cc = *endp;
2969 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 if (op != NULL && *op != '=')
2971 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002972 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002974 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002975 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002976 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002977 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002979 if ((di == NULL
2980 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2981 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2982 FALSE)))
2983 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002984 set_var(lp->ll_name, &tv, FALSE);
2985 clear_tv(&tv);
2986 }
2987 }
2988 else
2989 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002991 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002993 else if (tv_check_lock(lp->ll_newkey == NULL
2994 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002995 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002996 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 else if (lp->ll_range)
2998 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002999 listitem_T *ll_li = lp->ll_li;
3000 int ll_n1 = lp->ll_n1;
3001
3002 /*
3003 * Check whether any of the list items is locked
3004 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003005 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003006 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003007 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003008 return;
3009 ri = ri->li_next;
3010 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3011 break;
3012 ll_li = ll_li->li_next;
3013 ++ll_n1;
3014 }
3015
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 /*
3017 * Assign the List values to the list items.
3018 */
3019 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003020 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003021 if (op != NULL && *op != '=')
3022 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3023 else
3024 {
3025 clear_tv(&lp->ll_li->li_tv);
3026 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3027 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 ri = ri->li_next;
3029 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3030 break;
3031 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003032 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003034 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 ri = NULL;
3037 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003038 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 lp->ll_li = lp->ll_li->li_next;
3041 ++lp->ll_n1;
3042 }
3043 if (ri != NULL)
3044 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003045 else if (lp->ll_empty2
3046 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 : lp->ll_n1 != lp->ll_n2)
3048 EMSG(_("E711: List value has not enough items"));
3049 }
3050 else
3051 {
3052 /*
3053 * Assign to a List or Dictionary item.
3054 */
3055 if (lp->ll_newkey != NULL)
3056 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 if (op != NULL && *op != '=')
3058 {
3059 EMSG2(_(e_letwrong), op);
3060 return;
3061 }
3062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003064 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003065 if (di == NULL)
3066 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003067 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3068 {
3069 vim_free(di);
3070 return;
3071 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003072 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 else if (op != NULL && *op != '=')
3075 {
3076 tv_op(lp->ll_tv, rettv, op);
3077 return;
3078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003080 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003081
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003082 /*
3083 * Assign the value to the variable or list item.
3084 */
3085 if (copy)
3086 copy_tv(rettv, lp->ll_tv);
3087 else
3088 {
3089 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003090 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003091 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003092 }
3093 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003094}
3095
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3098 * Returns OK or FAIL.
3099 */
3100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003101tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102{
3103 long n;
3104 char_u numbuf[NUMBUFLEN];
3105 char_u *s;
3106
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003107 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3108 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3109 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 {
3111 switch (tv1->v_type)
3112 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003113 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 case VAR_DICT:
3115 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003116 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003117 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003118 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003119 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003120 break;
3121
3122 case VAR_LIST:
3123 if (*op != '+' || tv2->v_type != VAR_LIST)
3124 break;
3125 /* List += List */
3126 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3127 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3128 return OK;
3129
3130 case VAR_NUMBER:
3131 case VAR_STRING:
3132 if (tv2->v_type == VAR_LIST)
3133 break;
3134 if (*op == '+' || *op == '-')
3135 {
3136 /* nr += nr or nr -= nr*/
3137 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138#ifdef FEAT_FLOAT
3139 if (tv2->v_type == VAR_FLOAT)
3140 {
3141 float_T f = n;
3142
3143 if (*op == '+')
3144 f += tv2->vval.v_float;
3145 else
3146 f -= tv2->vval.v_float;
3147 clear_tv(tv1);
3148 tv1->v_type = VAR_FLOAT;
3149 tv1->vval.v_float = f;
3150 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003151 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003152#endif
3153 {
3154 if (*op == '+')
3155 n += get_tv_number(tv2);
3156 else
3157 n -= get_tv_number(tv2);
3158 clear_tv(tv1);
3159 tv1->v_type = VAR_NUMBER;
3160 tv1->vval.v_number = n;
3161 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003162 }
3163 else
3164 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165 if (tv2->v_type == VAR_FLOAT)
3166 break;
3167
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003168 /* str .= str */
3169 s = get_tv_string(tv1);
3170 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3171 clear_tv(tv1);
3172 tv1->v_type = VAR_STRING;
3173 tv1->vval.v_string = s;
3174 }
3175 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003176
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003178#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179 {
3180 float_T f;
3181
3182 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3183 && tv2->v_type != VAR_NUMBER
3184 && tv2->v_type != VAR_STRING))
3185 break;
3186 if (tv2->v_type == VAR_FLOAT)
3187 f = tv2->vval.v_float;
3188 else
3189 f = get_tv_number(tv2);
3190 if (*op == '+')
3191 tv1->vval.v_float += f;
3192 else
3193 tv1->vval.v_float -= f;
3194 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003195#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003196 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003197 }
3198 }
3199
3200 EMSG2(_(e_letwrong), op);
3201 return FAIL;
3202}
3203
3204/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 * Add a watcher to a list.
3206 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003208list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209{
3210 lw->lw_next = l->lv_watch;
3211 l->lv_watch = lw;
3212}
3213
3214/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003215 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 * No warning when it isn't found...
3217 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220{
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222
3223 lwp = &l->lv_watch;
3224 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3225 {
3226 if (lw == lwrem)
3227 {
3228 *lwp = lw->lw_next;
3229 break;
3230 }
3231 lwp = &lw->lw_next;
3232 }
3233}
3234
3235/*
3236 * Just before removing an item from a list: advance watchers to the next
3237 * item.
3238 */
3239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241{
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243
3244 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3245 if (lw->lw_item == item)
3246 lw->lw_item = item->li_next;
3247}
3248
3249/*
3250 * Evaluate the expression used in a ":for var in expr" command.
3251 * "arg" points to "var".
3252 * Set "*errp" to TRUE for an error, FALSE otherwise;
3253 * Return a pointer that holds the info. Null when there is an error.
3254 */
3255 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003256eval_for_line(
3257 char_u *arg,
3258 int *errp,
3259 char_u **nextcmdp,
3260 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261{
Bram Moolenaar33570922005-01-25 22:26:29 +00003262 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 typval_T tv;
3265 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266
3267 *errp = TRUE; /* default: there is an error */
3268
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 if (fi == NULL)
3271 return NULL;
3272
3273 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3274 if (expr == NULL)
3275 return fi;
3276
3277 expr = skipwhite(expr);
3278 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3279 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003280 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 return fi;
3282 }
3283
3284 if (skip)
3285 ++emsg_skip;
3286 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3287 {
3288 *errp = FALSE;
3289 if (!skip)
3290 {
3291 l = tv.vval.v_list;
3292 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003293 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003295 clear_tv(&tv);
3296 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 else
3298 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003299 /* No need to increment the refcount, it's already set for the
3300 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 fi->fi_list = l;
3302 list_add_watch(l, &fi->fi_lw);
3303 fi->fi_lw.lw_item = l->lv_first;
3304 }
3305 }
3306 }
3307 if (skip)
3308 --emsg_skip;
3309
3310 return fi;
3311}
3312
3313/*
3314 * Use the first item in a ":for" list. Advance to the next.
3315 * Assign the values to the variable (list). "arg" points to the first one.
3316 * Return TRUE when a valid item was found, FALSE when at end of list or
3317 * something wrong.
3318 */
3319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003322 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325
3326 item = fi->fi_lw.lw_item;
3327 if (item == NULL)
3328 result = FALSE;
3329 else
3330 {
3331 fi->fi_lw.lw_item = item->li_next;
3332 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3333 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3334 }
3335 return result;
3336}
3337
3338/*
3339 * Free the structure used to store info used by ":for".
3340 */
3341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343{
Bram Moolenaar33570922005-01-25 22:26:29 +00003344 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003346 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003347 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003349 list_unref(fi->fi_list);
3350 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 vim_free(fi);
3352}
3353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3355
3356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003357set_context_for_expression(
3358 expand_T *xp,
3359 char_u *arg,
3360 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 int got_eq = FALSE;
3363 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003364 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003366 if (cmdidx == CMD_let)
3367 {
3368 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003369 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003370 {
3371 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003372 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003373 {
3374 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003375 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003376 if (vim_iswhite(*p))
3377 break;
3378 }
3379 return;
3380 }
3381 }
3382 else
3383 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3384 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 while ((xp->xp_pattern = vim_strpbrk(arg,
3386 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3387 {
3388 c = *xp->xp_pattern;
3389 if (c == '&')
3390 {
3391 c = xp->xp_pattern[1];
3392 if (c == '&')
3393 {
3394 ++xp->xp_pattern;
3395 xp->xp_context = cmdidx != CMD_let || got_eq
3396 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3397 }
3398 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003401 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3402 xp->xp_pattern += 2;
3403
3404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 else if (c == '$')
3407 {
3408 /* environment variable */
3409 xp->xp_context = EXPAND_ENV_VARS;
3410 }
3411 else if (c == '=')
3412 {
3413 got_eq = TRUE;
3414 xp->xp_context = EXPAND_EXPRESSION;
3415 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003416 else if (c == '#'
3417 && xp->xp_context == EXPAND_EXPRESSION)
3418 {
3419 /* Autoload function/variable contains '#'. */
3420 break;
3421 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003422 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 && xp->xp_context == EXPAND_FUNCTIONS
3424 && vim_strchr(xp->xp_pattern, '(') == NULL)
3425 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003426 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 break;
3428 }
3429 else if (cmdidx != CMD_let || got_eq)
3430 {
3431 if (c == '"') /* string */
3432 {
3433 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3434 if (c == '\\' && xp->xp_pattern[1] != NUL)
3435 ++xp->xp_pattern;
3436 xp->xp_context = EXPAND_NOTHING;
3437 }
3438 else if (c == '\'') /* literal string */
3439 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003440 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3442 /* skip */ ;
3443 xp->xp_context = EXPAND_NOTHING;
3444 }
3445 else if (c == '|')
3446 {
3447 if (xp->xp_pattern[1] == '|')
3448 {
3449 ++xp->xp_pattern;
3450 xp->xp_context = EXPAND_EXPRESSION;
3451 }
3452 else
3453 xp->xp_context = EXPAND_COMMANDS;
3454 }
3455 else
3456 xp->xp_context = EXPAND_EXPRESSION;
3457 }
3458 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003459 /* Doesn't look like something valid, expand as an expression
3460 * anyway. */
3461 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 arg = xp->xp_pattern;
3463 if (*arg != NUL)
3464 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3465 /* skip */ ;
3466 }
3467 xp->xp_pattern = arg;
3468}
3469
3470#endif /* FEAT_CMDL_COMPL */
3471
3472/*
3473 * ":1,25call func(arg1, arg2)" function call.
3474 */
3475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003476ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478 char_u *arg = eap->arg;
3479 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003483 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 linenr_T lnum;
3485 int doesrange;
3486 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003487 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003488 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003490 if (eap->skip)
3491 {
3492 /* trans_function_name() doesn't work well when skipping, use eval0()
3493 * instead to skip to any following command, e.g. for:
3494 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003495 ++emsg_skip;
3496 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3497 clear_tv(&rettv);
3498 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003499 return;
3500 }
3501
Bram Moolenaar65639032016-03-16 21:40:30 +01003502 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003503 if (fudi.fd_newkey != NULL)
3504 {
3505 /* Still need to give an error message for missing key. */
3506 EMSG2(_(e_dictkey), fudi.fd_newkey);
3507 vim_free(fudi.fd_newkey);
3508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003509 if (tofree == NULL)
3510 return;
3511
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003512 /* Increase refcount on dictionary, it could get deleted when evaluating
3513 * the arguments. */
3514 if (fudi.fd_dict != NULL)
3515 ++fudi.fd_dict->dv_refcount;
3516
Bram Moolenaar65639032016-03-16 21:40:30 +01003517 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3518 * contents. For VAR_PARTIAL get its partial, unless we already have one
3519 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003520 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003521 name = deref_func_name(tofree, &len,
3522 partial != NULL ? NULL : &partial, FALSE);
3523
Bram Moolenaar532c7802005-01-27 14:44:31 +00003524 /* Skip white space to allow ":call func ()". Not good, but required for
3525 * backward compatibility. */
3526 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003527 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 if (*startarg != '(')
3530 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003531 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 goto end;
3533 }
3534
3535 /*
3536 * When skipping, evaluate the function once, to find the end of the
3537 * arguments.
3538 * When the function takes a range, this is discovered after the first
3539 * call, and the loop is broken.
3540 */
3541 if (eap->skip)
3542 {
3543 ++emsg_skip;
3544 lnum = eap->line2; /* do it once, also with an invalid range */
3545 }
3546 else
3547 lnum = eap->line1;
3548 for ( ; lnum <= eap->line2; ++lnum)
3549 {
3550 if (!eap->skip && eap->addr_count > 0)
3551 {
3552 curwin->w_cursor.lnum = lnum;
3553 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003554#ifdef FEAT_VIRTUALEDIT
3555 curwin->w_cursor.coladd = 0;
3556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003559 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003560 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003561 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
3563 failed = TRUE;
3564 break;
3565 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003566
3567 /* Handle a function returning a Funcref, Dictionary or List. */
3568 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3569 {
3570 failed = TRUE;
3571 break;
3572 }
3573
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003574 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 if (doesrange || eap->skip)
3576 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003577
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003579 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003580 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003581 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (aborting())
3583 break;
3584 }
3585 if (eap->skip)
3586 --emsg_skip;
3587
3588 if (!failed)
3589 {
3590 /* Check for trailing illegal characters and a following command. */
3591 if (!ends_excmd(*arg))
3592 {
3593 emsg_severe = TRUE;
3594 EMSG(_(e_trailing));
3595 }
3596 else
3597 eap->nextcmd = check_nextcmd(arg);
3598 }
3599
3600end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003601 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003602 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603}
3604
3605/*
3606 * ":unlet[!] var1 ... " command.
3607 */
3608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003609ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 ex_unletlock(eap, eap->arg, 0);
3612}
3613
3614/*
3615 * ":lockvar" and ":unlockvar" commands
3616 */
3617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003618ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003621 int deep = 2;
3622
3623 if (eap->forceit)
3624 deep = -1;
3625 else if (vim_isdigit(*arg))
3626 {
3627 deep = getdigits(&arg);
3628 arg = skipwhite(arg);
3629 }
3630
3631 ex_unletlock(eap, arg, deep);
3632}
3633
3634/*
3635 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3636 */
3637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003638ex_unletlock(
3639 exarg_T *eap,
3640 char_u *argstart,
3641 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003642{
3643 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 do
3649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003651 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003652 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 if (lv.ll_name == NULL)
3654 error = TRUE; /* error but continue parsing */
3655 if (name_end == NULL || (!vim_iswhite(*name_end)
3656 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 if (name_end != NULL)
3659 {
3660 emsg_severe = TRUE;
3661 EMSG(_(e_trailing));
3662 }
3663 if (!(eap->skip || error))
3664 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 break;
3666 }
3667
3668 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003669 {
3670 if (eap->cmdidx == CMD_unlet)
3671 {
3672 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3673 error = TRUE;
3674 }
3675 else
3676 {
3677 if (do_lock_var(&lv, name_end, deep,
3678 eap->cmdidx == CMD_lockvar) == FAIL)
3679 error = TRUE;
3680 }
3681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 if (!eap->skip)
3684 clear_lval(&lv);
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 arg = skipwhite(name_end);
3687 } while (!ends_excmd(*arg));
3688
3689 eap->nextcmd = check_nextcmd(arg);
3690}
3691
Bram Moolenaar8c711452005-01-14 21:53:12 +00003692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003693do_unlet_var(
3694 lval_T *lp,
3695 char_u *name_end,
3696 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003697{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 int ret = OK;
3699 int cc;
3700
3701 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 cc = *name_end;
3704 *name_end = NUL;
3705
3706 /* Normal name or expanded name. */
3707 if (check_changedtick(lp->ll_name))
3708 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003709 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003712 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003713 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003714 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003715 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003716 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003717 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 else if (lp->ll_range)
3719 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003720 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003721 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003722 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003723
3724 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3725 {
3726 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003727 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003728 return FAIL;
3729 ll_li = li;
3730 ++ll_n1;
3731 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003732
3733 /* Delete a range of List items. */
3734 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3735 {
3736 li = lp->ll_li->li_next;
3737 listitem_remove(lp->ll_list, lp->ll_li);
3738 lp->ll_li = li;
3739 ++lp->ll_n1;
3740 }
3741 }
3742 else
3743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003744 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003745 /* unlet a List item. */
3746 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003747 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003748 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003749 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750 }
3751
3752 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003753}
3754
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755/*
3756 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003757 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 */
3759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003760do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
Bram Moolenaar33570922005-01-25 22:26:29 +00003762 hashtab_T *ht;
3763 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003765 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003766 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
Bram Moolenaar33570922005-01-25 22:26:29 +00003768 ht = find_var_ht(name, &varname);
3769 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003771 if (ht == &globvarht)
3772 d = &globvardict;
3773 else if (current_funccal != NULL
3774 && ht == &current_funccal->l_vars.dv_hashtab)
3775 d = &current_funccal->l_vars;
3776 else if (ht == &compat_hashtab)
3777 d = &vimvardict;
3778 else
3779 {
3780 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3781 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3782 }
3783 if (d == NULL)
3784 {
3785 EMSG2(_(e_intern2), "do_unlet()");
3786 return FAIL;
3787 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003788 hi = hash_find(ht, varname);
3789 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003790 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003791 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003792 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003793 || var_check_ro(di->di_flags, name, FALSE)
3794 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003795 return FAIL;
3796
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797 delete_var(ht, hi);
3798 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003801 if (forceit)
3802 return OK;
3803 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return FAIL;
3805}
3806
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003807/*
3808 * Lock or unlock variable indicated by "lp".
3809 * "deep" is the levels to go (-1 for unlimited);
3810 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3811 */
3812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003813do_lock_var(
3814 lval_T *lp,
3815 char_u *name_end,
3816 int deep,
3817 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818{
3819 int ret = OK;
3820 int cc;
3821 dictitem_T *di;
3822
3823 if (deep == 0) /* nothing to do */
3824 return OK;
3825
3826 if (lp->ll_tv == NULL)
3827 {
3828 cc = *name_end;
3829 *name_end = NUL;
3830
3831 /* Normal name or expanded name. */
3832 if (check_changedtick(lp->ll_name))
3833 ret = FAIL;
3834 else
3835 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003836 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003837 if (di == NULL)
3838 ret = FAIL;
3839 else
3840 {
3841 if (lock)
3842 di->di_flags |= DI_FLAGS_LOCK;
3843 else
3844 di->di_flags &= ~DI_FLAGS_LOCK;
3845 item_lock(&di->di_tv, deep, lock);
3846 }
3847 }
3848 *name_end = cc;
3849 }
3850 else if (lp->ll_range)
3851 {
3852 listitem_T *li = lp->ll_li;
3853
3854 /* (un)lock a range of List items. */
3855 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3856 {
3857 item_lock(&li->li_tv, deep, lock);
3858 li = li->li_next;
3859 ++lp->ll_n1;
3860 }
3861 }
3862 else if (lp->ll_list != NULL)
3863 /* (un)lock a List item. */
3864 item_lock(&lp->ll_li->li_tv, deep, lock);
3865 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003866 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003867 item_lock(&lp->ll_di->di_tv, deep, lock);
3868
3869 return ret;
3870}
3871
3872/*
3873 * Lock or unlock an item. "deep" is nr of levels to go.
3874 */
3875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003876item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003877{
3878 static int recurse = 0;
3879 list_T *l;
3880 listitem_T *li;
3881 dict_T *d;
3882 hashitem_T *hi;
3883 int todo;
3884
3885 if (recurse >= DICT_MAXNEST)
3886 {
3887 EMSG(_("E743: variable nested too deep for (un)lock"));
3888 return;
3889 }
3890 if (deep == 0)
3891 return;
3892 ++recurse;
3893
3894 /* lock/unlock the item itself */
3895 if (lock)
3896 tv->v_lock |= VAR_LOCKED;
3897 else
3898 tv->v_lock &= ~VAR_LOCKED;
3899
3900 switch (tv->v_type)
3901 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003902 case VAR_UNKNOWN:
3903 case VAR_NUMBER:
3904 case VAR_STRING:
3905 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003906 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003907 case VAR_FLOAT:
3908 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003909 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003910 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003911 break;
3912
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003913 case VAR_LIST:
3914 if ((l = tv->vval.v_list) != NULL)
3915 {
3916 if (lock)
3917 l->lv_lock |= VAR_LOCKED;
3918 else
3919 l->lv_lock &= ~VAR_LOCKED;
3920 if (deep < 0 || deep > 1)
3921 /* recursive: lock/unlock the items the List contains */
3922 for (li = l->lv_first; li != NULL; li = li->li_next)
3923 item_lock(&li->li_tv, deep - 1, lock);
3924 }
3925 break;
3926 case VAR_DICT:
3927 if ((d = tv->vval.v_dict) != NULL)
3928 {
3929 if (lock)
3930 d->dv_lock |= VAR_LOCKED;
3931 else
3932 d->dv_lock &= ~VAR_LOCKED;
3933 if (deep < 0 || deep > 1)
3934 {
3935 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003936 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003937 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3938 {
3939 if (!HASHITEM_EMPTY(hi))
3940 {
3941 --todo;
3942 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3943 }
3944 }
3945 }
3946 }
3947 }
3948 --recurse;
3949}
3950
Bram Moolenaara40058a2005-07-11 22:42:07 +00003951/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003952 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3953 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003954 */
3955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003956tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003957{
3958 return (tv->v_lock & VAR_LOCKED)
3959 || (tv->v_type == VAR_LIST
3960 && tv->vval.v_list != NULL
3961 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3962 || (tv->v_type == VAR_DICT
3963 && tv->vval.v_dict != NULL
3964 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3965}
3966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3968/*
3969 * Delete all "menutrans_" variables.
3970 */
3971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003972del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003975 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
Bram Moolenaar33570922005-01-25 22:26:29 +00003977 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003978 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003979 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003980 {
3981 if (!HASHITEM_EMPTY(hi))
3982 {
3983 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003984 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3985 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003986 }
3987 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003988 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989}
3990#endif
3991
3992#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3993
3994/*
3995 * Local string buffer for the next two functions to store a variable name
3996 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3997 * get_user_var_name().
3998 */
3999
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004000static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
4002static char_u *varnamebuf = NULL;
4003static int varnamebuflen = 0;
4004
4005/*
4006 * Function to concatenate a prefix and a variable name.
4007 */
4008 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004009cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010{
4011 int len;
4012
4013 len = (int)STRLEN(name) + 3;
4014 if (len > varnamebuflen)
4015 {
4016 vim_free(varnamebuf);
4017 len += 10; /* some additional space */
4018 varnamebuf = alloc(len);
4019 if (varnamebuf == NULL)
4020 {
4021 varnamebuflen = 0;
4022 return NULL;
4023 }
4024 varnamebuflen = len;
4025 }
4026 *varnamebuf = prefix;
4027 varnamebuf[1] = ':';
4028 STRCPY(varnamebuf + 2, name);
4029 return varnamebuf;
4030}
4031
4032/*
4033 * Function given to ExpandGeneric() to obtain the list of user defined
4034 * (global/buffer/window/built-in) variable names.
4035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004037get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 static long_u gdone;
4040 static long_u bdone;
4041 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004042#ifdef FEAT_WINDOWS
4043 static long_u tdone;
4044#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 static int vidx;
4046 static hashitem_T *hi;
4047 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
4049 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004050 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004051 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004052#ifdef FEAT_WINDOWS
4053 tdone = 0;
4054#endif
4055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* Global variables */
4058 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004060 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 else
4063 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004064 while (HASHITEM_EMPTY(hi))
4065 ++hi;
4066 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4067 return cat_prefix_varname('g', hi->hi_key);
4068 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004070
4071 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004075 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004077 else
4078 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004085 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return (char_u *)"b:changedtick";
4087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004088
4089 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004090 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004093 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004095 else
4096 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004097 while (HASHITEM_EMPTY(hi))
4098 ++hi;
4099 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004101
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004102#ifdef FEAT_WINDOWS
4103 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004104 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004105 if (tdone < ht->ht_used)
4106 {
4107 if (tdone++ == 0)
4108 hi = ht->ht_array;
4109 else
4110 ++hi;
4111 while (HASHITEM_EMPTY(hi))
4112 ++hi;
4113 return cat_prefix_varname('t', hi->hi_key);
4114 }
4115#endif
4116
Bram Moolenaar33570922005-01-25 22:26:29 +00004117 /* v: variables */
4118 if (vidx < VV_LEN)
4119 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 vim_free(varnamebuf);
4122 varnamebuf = NULL;
4123 varnamebuflen = 0;
4124 return NULL;
4125}
4126
4127#endif /* FEAT_CMDL_COMPL */
4128
4129/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004130 * Return TRUE if "pat" matches "text".
4131 * Does not use 'cpo' and always uses 'magic'.
4132 */
4133 static int
4134pattern_match(char_u *pat, char_u *text, int ic)
4135{
4136 int matches = FALSE;
4137 char_u *save_cpo;
4138 regmatch_T regmatch;
4139
4140 /* avoid 'l' flag in 'cpoptions' */
4141 save_cpo = p_cpo;
4142 p_cpo = (char_u *)"";
4143 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4144 if (regmatch.regprog != NULL)
4145 {
4146 regmatch.rm_ic = ic;
4147 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4148 vim_regfree(regmatch.regprog);
4149 }
4150 p_cpo = save_cpo;
4151 return matches;
4152}
4153
4154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * types for expressions.
4156 */
4157typedef enum
4158{
4159 TYPE_UNKNOWN = 0
4160 , TYPE_EQUAL /* == */
4161 , TYPE_NEQUAL /* != */
4162 , TYPE_GREATER /* > */
4163 , TYPE_GEQUAL /* >= */
4164 , TYPE_SMALLER /* < */
4165 , TYPE_SEQUAL /* <= */
4166 , TYPE_MATCH /* =~ */
4167 , TYPE_NOMATCH /* !~ */
4168} exptype_T;
4169
4170/*
4171 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4174 */
4175
4176/*
4177 * Handle zero level expression.
4178 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004180 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 * Return OK or FAIL.
4182 */
4183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004184eval0(
4185 char_u *arg,
4186 typval_T *rettv,
4187 char_u **nextcmd,
4188 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
4190 int ret;
4191 char_u *p;
4192
4193 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (ret == FAIL || !ends_excmd(*p))
4196 {
4197 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 /*
4200 * Report the invalid expression unless the expression evaluation has
4201 * been cancelled due to an aborting error, an interrupt, or an
4202 * exception.
4203 */
4204 if (!aborting())
4205 EMSG2(_(e_invexpr2), arg);
4206 ret = FAIL;
4207 }
4208 if (nextcmd != NULL)
4209 *nextcmd = check_nextcmd(p);
4210
4211 return ret;
4212}
4213
4214/*
4215 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004216 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004221 * Note: "rettv.v_lock" is not set.
4222 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 * Return OK or FAIL.
4224 */
4225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004226eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
4228 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 /*
4232 * Get the first variable.
4233 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 return FAIL;
4236
4237 if ((*arg)[0] == '?')
4238 {
4239 result = FALSE;
4240 if (evaluate)
4241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
4243
4244 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 if (error)
4248 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250
4251 /*
4252 * Get the second variable.
4253 */
4254 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return FAIL;
4257
4258 /*
4259 * Check for the ":".
4260 */
4261 if ((*arg)[0] != ':')
4262 {
4263 EMSG(_("E109: Missing ':' after '?'"));
4264 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 return FAIL;
4267 }
4268
4269 /*
4270 * Get the third variable.
4271 */
4272 *arg = skipwhite(*arg + 1);
4273 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4274 {
4275 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 return FAIL;
4278 }
4279 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
4283 return OK;
4284}
4285
4286/*
4287 * Handle first level expression:
4288 * expr2 || expr2 || expr2 logical OR
4289 *
4290 * "arg" must point to the first non-white of the expression.
4291 * "arg" is advanced to the next non-white after the recognized expression.
4292 *
4293 * Return OK or FAIL.
4294 */
4295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004296eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 long result;
4300 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004301 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 /*
4304 * Get the first variable.
4305 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004306 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 return FAIL;
4308
4309 /*
4310 * Repeat until there is no following "||".
4311 */
4312 first = TRUE;
4313 result = FALSE;
4314 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4315 {
4316 if (evaluate && first)
4317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (error)
4322 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 first = FALSE;
4324 }
4325
4326 /*
4327 * Get the second variable.
4328 */
4329 *arg = skipwhite(*arg + 2);
4330 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4331 return FAIL;
4332
4333 /*
4334 * Compute the result.
4335 */
4336 if (evaluate && !result)
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 if (evaluate)
4345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004346 rettv->v_type = VAR_NUMBER;
4347 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 }
4350
4351 return OK;
4352}
4353
4354/*
4355 * Handle second level expression:
4356 * expr3 && expr3 && expr3 logical AND
4357 *
4358 * "arg" must point to the first non-white of the expression.
4359 * "arg" is advanced to the next non-white after the recognized expression.
4360 *
4361 * Return OK or FAIL.
4362 */
4363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004364eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
Bram Moolenaar33570922005-01-25 22:26:29 +00004366 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 long result;
4368 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004369 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
4371 /*
4372 * Get the first variable.
4373 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 return FAIL;
4376
4377 /*
4378 * Repeat until there is no following "&&".
4379 */
4380 first = TRUE;
4381 result = TRUE;
4382 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4383 {
4384 if (evaluate && first)
4385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004389 if (error)
4390 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 first = FALSE;
4392 }
4393
4394 /*
4395 * Get the second variable.
4396 */
4397 *arg = skipwhite(*arg + 2);
4398 if (eval4(arg, &var2, evaluate && result) == FAIL)
4399 return FAIL;
4400
4401 /*
4402 * Compute the result.
4403 */
4404 if (evaluate && result)
4405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 if (error)
4410 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 if (evaluate)
4413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 rettv->v_type = VAR_NUMBER;
4415 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417 }
4418
4419 return OK;
4420}
4421
4422/*
4423 * Handle third level expression:
4424 * var1 == var2
4425 * var1 =~ var2
4426 * var1 != var2
4427 * var1 !~ var2
4428 * var1 > var2
4429 * var1 >= var2
4430 * var1 < var2
4431 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 * var1 is var2
4433 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 *
4435 * "arg" must point to the first non-white of the expression.
4436 * "arg" is advanced to the next non-white after the recognized expression.
4437 *
4438 * Return OK or FAIL.
4439 */
4440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004441eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
Bram Moolenaar33570922005-01-25 22:26:29 +00004443 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 char_u *p;
4445 int i;
4446 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004447 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 int len = 2;
4449 long n1, n2;
4450 char_u *s1, *s2;
4451 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 /*
4455 * Get the first variable.
4456 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 return FAIL;
4459
4460 p = *arg;
4461 switch (p[0])
4462 {
4463 case '=': if (p[1] == '=')
4464 type = TYPE_EQUAL;
4465 else if (p[1] == '~')
4466 type = TYPE_MATCH;
4467 break;
4468 case '!': if (p[1] == '=')
4469 type = TYPE_NEQUAL;
4470 else if (p[1] == '~')
4471 type = TYPE_NOMATCH;
4472 break;
4473 case '>': if (p[1] != '=')
4474 {
4475 type = TYPE_GREATER;
4476 len = 1;
4477 }
4478 else
4479 type = TYPE_GEQUAL;
4480 break;
4481 case '<': if (p[1] != '=')
4482 {
4483 type = TYPE_SMALLER;
4484 len = 1;
4485 }
4486 else
4487 type = TYPE_SEQUAL;
4488 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004489 case 'i': if (p[1] == 's')
4490 {
4491 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4492 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004493 i = p[len];
4494 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 {
4496 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4497 type_is = TRUE;
4498 }
4499 }
4500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502
4503 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004504 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 */
4506 if (type != TYPE_UNKNOWN)
4507 {
4508 /* extra question mark appended: ignore case */
4509 if (p[len] == '?')
4510 {
4511 ic = TRUE;
4512 ++len;
4513 }
4514 /* extra '#' appended: match case */
4515 else if (p[len] == '#')
4516 {
4517 ic = FALSE;
4518 ++len;
4519 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004520 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 else
4522 ic = p_ic;
4523
4524 /*
4525 * Get the second variable.
4526 */
4527 *arg = skipwhite(p + len);
4528 if (eval5(arg, &var2, evaluate) == FAIL)
4529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004530 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return FAIL;
4532 }
4533
4534 if (evaluate)
4535 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004536 if (type_is && rettv->v_type != var2.v_type)
4537 {
4538 /* For "is" a different type always means FALSE, for "notis"
4539 * it means TRUE. */
4540 n1 = (type == TYPE_NEQUAL);
4541 }
4542 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4543 {
4544 if (type_is)
4545 {
4546 n1 = (rettv->v_type == var2.v_type
4547 && rettv->vval.v_list == var2.vval.v_list);
4548 if (type == TYPE_NEQUAL)
4549 n1 = !n1;
4550 }
4551 else if (rettv->v_type != var2.v_type
4552 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4553 {
4554 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004555 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004557 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004558 clear_tv(rettv);
4559 clear_tv(&var2);
4560 return FAIL;
4561 }
4562 else
4563 {
4564 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004565 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4566 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004567 if (type == TYPE_NEQUAL)
4568 n1 = !n1;
4569 }
4570 }
4571
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004572 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4573 {
4574 if (type_is)
4575 {
4576 n1 = (rettv->v_type == var2.v_type
4577 && rettv->vval.v_dict == var2.vval.v_dict);
4578 if (type == TYPE_NEQUAL)
4579 n1 = !n1;
4580 }
4581 else if (rettv->v_type != var2.v_type
4582 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4583 {
4584 if (rettv->v_type != var2.v_type)
4585 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4586 else
4587 EMSG(_("E736: Invalid operation for Dictionary"));
4588 clear_tv(rettv);
4589 clear_tv(&var2);
4590 return FAIL;
4591 }
4592 else
4593 {
4594 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004595 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4596 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004597 if (type == TYPE_NEQUAL)
4598 n1 = !n1;
4599 }
4600 }
4601
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004602 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4603 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004604 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004605 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004606 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004607 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004608 clear_tv(rettv);
4609 clear_tv(&var2);
4610 return FAIL;
4611 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004612 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004613 if (type == TYPE_NEQUAL)
4614 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004615 }
4616
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004617#ifdef FEAT_FLOAT
4618 /*
4619 * If one of the two variables is a float, compare as a float.
4620 * When using "=~" or "!~", always compare as string.
4621 */
4622 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4623 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4624 {
4625 float_T f1, f2;
4626
4627 if (rettv->v_type == VAR_FLOAT)
4628 f1 = rettv->vval.v_float;
4629 else
4630 f1 = get_tv_number(rettv);
4631 if (var2.v_type == VAR_FLOAT)
4632 f2 = var2.vval.v_float;
4633 else
4634 f2 = get_tv_number(&var2);
4635 n1 = FALSE;
4636 switch (type)
4637 {
4638 case TYPE_EQUAL: n1 = (f1 == f2); break;
4639 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4640 case TYPE_GREATER: n1 = (f1 > f2); break;
4641 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4642 case TYPE_SMALLER: n1 = (f1 < f2); break;
4643 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4644 case TYPE_UNKNOWN:
4645 case TYPE_MATCH:
4646 case TYPE_NOMATCH: break; /* avoid gcc warning */
4647 }
4648 }
4649#endif
4650
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 /*
4652 * If one of the two variables is a number, compare as a number.
4653 * When using "=~" or "!~", always compare as string.
4654 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004655 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004658 n1 = get_tv_number(rettv);
4659 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 switch (type)
4661 {
4662 case TYPE_EQUAL: n1 = (n1 == n2); break;
4663 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4664 case TYPE_GREATER: n1 = (n1 > n2); break;
4665 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4666 case TYPE_SMALLER: n1 = (n1 < n2); break;
4667 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4668 case TYPE_UNKNOWN:
4669 case TYPE_MATCH:
4670 case TYPE_NOMATCH: break; /* avoid gcc warning */
4671 }
4672 }
4673 else
4674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 s1 = get_tv_string_buf(rettv, buf1);
4676 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4678 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4679 else
4680 i = 0;
4681 n1 = FALSE;
4682 switch (type)
4683 {
4684 case TYPE_EQUAL: n1 = (i == 0); break;
4685 case TYPE_NEQUAL: n1 = (i != 0); break;
4686 case TYPE_GREATER: n1 = (i > 0); break;
4687 case TYPE_GEQUAL: n1 = (i >= 0); break;
4688 case TYPE_SMALLER: n1 = (i < 0); break;
4689 case TYPE_SEQUAL: n1 = (i <= 0); break;
4690
4691 case TYPE_MATCH:
4692 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004693 n1 = pattern_match(s2, s1, ic);
4694 if (type == TYPE_NOMATCH)
4695 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 break;
4697
4698 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4699 }
4700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701 clear_tv(rettv);
4702 clear_tv(&var2);
4703 rettv->v_type = VAR_NUMBER;
4704 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 }
4706 }
4707
4708 return OK;
4709}
4710
4711/*
4712 * Handle fourth level expression:
4713 * + number addition
4714 * - number subtraction
4715 * . string concatenation
4716 *
4717 * "arg" must point to the first non-white of the expression.
4718 * "arg" is advanced to the next non-white after the recognized expression.
4719 *
4720 * Return OK or FAIL.
4721 */
4722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004723eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
Bram Moolenaar33570922005-01-25 22:26:29 +00004725 typval_T var2;
4726 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 int op;
4728 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004729#ifdef FEAT_FLOAT
4730 float_T f1 = 0, f2 = 0;
4731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 char_u *s1, *s2;
4733 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4734 char_u *p;
4735
4736 /*
4737 * Get the first variable.
4738 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004739 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 return FAIL;
4741
4742 /*
4743 * Repeat computing, until no '+', '-' or '.' is following.
4744 */
4745 for (;;)
4746 {
4747 op = **arg;
4748 if (op != '+' && op != '-' && op != '.')
4749 break;
4750
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751 if ((op != '+' || rettv->v_type != VAR_LIST)
4752#ifdef FEAT_FLOAT
4753 && (op == '.' || rettv->v_type != VAR_FLOAT)
4754#endif
4755 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 {
4757 /* For "list + ...", an illegal use of the first operand as
4758 * a number cannot be determined before evaluating the 2nd
4759 * operand: if this is also a list, all is ok.
4760 * For "something . ...", "something - ..." or "non-list + ...",
4761 * we know that the first operand needs to be a string or number
4762 * without evaluating the 2nd operand. So check before to avoid
4763 * side effects after an error. */
4764 if (evaluate && get_tv_string_chk(rettv) == NULL)
4765 {
4766 clear_tv(rettv);
4767 return FAIL;
4768 }
4769 }
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 /*
4772 * Get the second variable.
4773 */
4774 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004775 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 return FAIL;
4779 }
4780
4781 if (evaluate)
4782 {
4783 /*
4784 * Compute the result.
4785 */
4786 if (op == '.')
4787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4789 s2 = get_tv_string_buf_chk(&var2, buf2);
4790 if (s2 == NULL) /* type error ? */
4791 {
4792 clear_tv(rettv);
4793 clear_tv(&var2);
4794 return FAIL;
4795 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004796 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 clear_tv(rettv);
4798 rettv->v_type = VAR_STRING;
4799 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004801 else if (op == '+' && rettv->v_type == VAR_LIST
4802 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004803 {
4804 /* concatenate Lists */
4805 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4806 &var3) == FAIL)
4807 {
4808 clear_tv(rettv);
4809 clear_tv(&var2);
4810 return FAIL;
4811 }
4812 clear_tv(rettv);
4813 *rettv = var3;
4814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 else
4816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004817 int error = FALSE;
4818
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819#ifdef FEAT_FLOAT
4820 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822 f1 = rettv->vval.v_float;
4823 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004824 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004825 else
4826#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828 n1 = get_tv_number_chk(rettv, &error);
4829 if (error)
4830 {
4831 /* This can only happen for "list + non-list". For
4832 * "non-list + ..." or "something - ...", we returned
4833 * before evaluating the 2nd operand. */
4834 clear_tv(rettv);
4835 return FAIL;
4836 }
4837#ifdef FEAT_FLOAT
4838 if (var2.v_type == VAR_FLOAT)
4839 f1 = n1;
4840#endif
4841 }
4842#ifdef FEAT_FLOAT
4843 if (var2.v_type == VAR_FLOAT)
4844 {
4845 f2 = var2.vval.v_float;
4846 n2 = 0;
4847 }
4848 else
4849#endif
4850 {
4851 n2 = get_tv_number_chk(&var2, &error);
4852 if (error)
4853 {
4854 clear_tv(rettv);
4855 clear_tv(&var2);
4856 return FAIL;
4857 }
4858#ifdef FEAT_FLOAT
4859 if (rettv->v_type == VAR_FLOAT)
4860 f2 = n2;
4861#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004862 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004864
4865#ifdef FEAT_FLOAT
4866 /* If there is a float on either side the result is a float. */
4867 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4868 {
4869 if (op == '+')
4870 f1 = f1 + f2;
4871 else
4872 f1 = f1 - f2;
4873 rettv->v_type = VAR_FLOAT;
4874 rettv->vval.v_float = f1;
4875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004877#endif
4878 {
4879 if (op == '+')
4880 n1 = n1 + n2;
4881 else
4882 n1 = n1 - n2;
4883 rettv->v_type = VAR_NUMBER;
4884 rettv->vval.v_number = n1;
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 }
4890 return OK;
4891}
4892
4893/*
4894 * Handle fifth level expression:
4895 * * number multiplication
4896 * / number division
4897 * % number modulo
4898 *
4899 * "arg" must point to the first non-white of the expression.
4900 * "arg" is advanced to the next non-white after the recognized expression.
4901 *
4902 * Return OK or FAIL.
4903 */
4904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004905eval6(
4906 char_u **arg,
4907 typval_T *rettv,
4908 int evaluate,
4909 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
Bram Moolenaar33570922005-01-25 22:26:29 +00004911 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 int op;
4913 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914#ifdef FEAT_FLOAT
4915 int use_float = FALSE;
4916 float_T f1 = 0, f2;
4917#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004918 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919
4920 /*
4921 * Get the first variable.
4922 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004923 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 return FAIL;
4925
4926 /*
4927 * Repeat computing, until no '*', '/' or '%' is following.
4928 */
4929 for (;;)
4930 {
4931 op = **arg;
4932 if (op != '*' && op != '/' && op != '%')
4933 break;
4934
4935 if (evaluate)
4936 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004937#ifdef FEAT_FLOAT
4938 if (rettv->v_type == VAR_FLOAT)
4939 {
4940 f1 = rettv->vval.v_float;
4941 use_float = TRUE;
4942 n1 = 0;
4943 }
4944 else
4945#endif
4946 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004947 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004948 if (error)
4949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951 else
4952 n1 = 0;
4953
4954 /*
4955 * Get the second variable.
4956 */
4957 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004958 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 return FAIL;
4960
4961 if (evaluate)
4962 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004963#ifdef FEAT_FLOAT
4964 if (var2.v_type == VAR_FLOAT)
4965 {
4966 if (!use_float)
4967 {
4968 f1 = n1;
4969 use_float = TRUE;
4970 }
4971 f2 = var2.vval.v_float;
4972 n2 = 0;
4973 }
4974 else
4975#endif
4976 {
4977 n2 = get_tv_number_chk(&var2, &error);
4978 clear_tv(&var2);
4979 if (error)
4980 return FAIL;
4981#ifdef FEAT_FLOAT
4982 if (use_float)
4983 f2 = n2;
4984#endif
4985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
4987 /*
4988 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991#ifdef FEAT_FLOAT
4992 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 if (op == '*')
4995 f1 = f1 * f2;
4996 else if (op == '/')
4997 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998# ifdef VMS
4999 /* VMS crashes on divide by zero, work around it */
5000 if (f2 == 0.0)
5001 {
5002 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005003 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005004 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005005 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005006 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005007 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005008 }
5009 else
5010 f1 = f1 / f2;
5011# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 /* We rely on the floating point library to handle divide
5013 * by zero to result in "inf" and not a crash. */
5014 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005015# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005019 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 return FAIL;
5021 }
5022 rettv->v_type = VAR_FLOAT;
5023 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 if (op == '*')
5029 n1 = n1 * n2;
5030 else if (op == '/')
5031 {
5032 if (n2 == 0) /* give an error message? */
5033 {
5034 if (n1 == 0)
5035 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5036 else if (n1 < 0)
5037 n1 = -0x7fffffffL;
5038 else
5039 n1 = 0x7fffffffL;
5040 }
5041 else
5042 n1 = n1 / n2;
5043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 {
5046 if (n2 == 0) /* give an error message? */
5047 n1 = 0;
5048 else
5049 n1 = n1 % n2;
5050 }
5051 rettv->v_type = VAR_NUMBER;
5052 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055 }
5056
5057 return OK;
5058}
5059
5060/*
5061 * Handle sixth level expression:
5062 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005063 * "string" string constant
5064 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 * &option-name option value
5066 * @r register contents
5067 * identifier variable value
5068 * function() function call
5069 * $VAR environment variable
5070 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005071 * [expr, expr] List
5072 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 *
5074 * Also handle:
5075 * ! in front logical NOT
5076 * - in front unary minus
5077 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 * trailing [] subscript in String or List
5079 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 *
5081 * "arg" must point to the first non-white of the expression.
5082 * "arg" is advanced to the next non-white after the recognized expression.
5083 *
5084 * Return OK or FAIL.
5085 */
5086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005087eval7(
5088 char_u **arg,
5089 typval_T *rettv,
5090 int evaluate,
5091 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 long n;
5094 int len;
5095 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 char_u *start_leader, *end_leader;
5097 int ret = OK;
5098 char_u *alias;
5099
5100 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005102 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
5106 /*
5107 * Skip '!' and '-' characters. They are handled later.
5108 */
5109 start_leader = *arg;
5110 while (**arg == '!' || **arg == '-' || **arg == '+')
5111 *arg = skipwhite(*arg + 1);
5112 end_leader = *arg;
5113
5114 switch (**arg)
5115 {
5116 /*
5117 * Number constant.
5118 */
5119 case '0':
5120 case '1':
5121 case '2':
5122 case '3':
5123 case '4':
5124 case '5':
5125 case '6':
5126 case '7':
5127 case '8':
5128 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005129 {
5130#ifdef FEAT_FLOAT
5131 char_u *p = skipdigits(*arg + 1);
5132 int get_float = FALSE;
5133
5134 /* We accept a float when the format matches
5135 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005136 * strict to avoid backwards compatibility problems.
5137 * Don't look for a float after the "." operator, so that
5138 * ":let vers = 1.2.3" doesn't fail. */
5139 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005141 get_float = TRUE;
5142 p = skipdigits(p + 2);
5143 if (*p == 'e' || *p == 'E')
5144 {
5145 ++p;
5146 if (*p == '-' || *p == '+')
5147 ++p;
5148 if (!vim_isdigit(*p))
5149 get_float = FALSE;
5150 else
5151 p = skipdigits(p + 1);
5152 }
5153 if (ASCII_ISALPHA(*p) || *p == '.')
5154 get_float = FALSE;
5155 }
5156 if (get_float)
5157 {
5158 float_T f;
5159
5160 *arg += string2float(*arg, &f);
5161 if (evaluate)
5162 {
5163 rettv->v_type = VAR_FLOAT;
5164 rettv->vval.v_float = f;
5165 }
5166 }
5167 else
5168#endif
5169 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005170 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005171 *arg += len;
5172 if (evaluate)
5173 {
5174 rettv->v_type = VAR_NUMBER;
5175 rettv->vval.v_number = n;
5176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181 /*
5182 * String constant: "string".
5183 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005184 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 break;
5186
5187 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 break;
5192
5193 /*
5194 * List: [expr, expr]
5195 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 break;
5198
5199 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 * Dictionary: {key: val, key: val}
5201 */
5202 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5203 break;
5204
5205 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005206 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005208 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 break;
5210
5211 /*
5212 * Environment variable: $VAR.
5213 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216
5217 /*
5218 * Register contents: @r.
5219 */
5220 case '@': ++*arg;
5221 if (evaluate)
5222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005223 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005224 rettv->vval.v_string = get_reg_contents(**arg,
5225 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 if (**arg != NUL)
5228 ++*arg;
5229 break;
5230
5231 /*
5232 * nested expression: (expression).
5233 */
5234 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005235 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (**arg == ')')
5237 ++*arg;
5238 else if (ret == OK)
5239 {
5240 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005241 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 ret = FAIL;
5243 }
5244 break;
5245
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 break;
5248 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249
5250 if (ret == NOTDONE)
5251 {
5252 /*
5253 * Must be a variable or function name.
5254 * Can also be a curly-braces kind of name: {expr}.
5255 */
5256 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005257 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 if (alias != NULL)
5259 s = alias;
5260
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 ret = FAIL;
5263 else
5264 {
5265 if (**arg == '(') /* recursive! */
5266 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005267 partial_T *partial;
5268
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 /* If "s" is the name of a variable of type VAR_FUNC
5270 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005271 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272
5273 /* Invoke the function. */
5274 ret = get_func_tv(s, len, rettv, arg,
5275 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005276 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005277
5278 /* If evaluate is FALSE rettv->v_type was not set in
5279 * get_func_tv, but it's needed in handle_subscript() to parse
5280 * what follows. So set it here. */
5281 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5282 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005283 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005284 rettv->v_type = VAR_FUNC;
5285 }
5286
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 /* Stop the expression evaluation when immediately
5288 * aborting on error, or when an interrupt occurred or
5289 * an exception was thrown but not caught. */
5290 if (aborting())
5291 {
5292 if (ret == OK)
5293 clear_tv(rettv);
5294 ret = FAIL;
5295 }
5296 }
5297 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005298 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005299 else
5300 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005302 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 }
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 *arg = skipwhite(*arg);
5306
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005307 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5308 * expr(expr). */
5309 if (ret == OK)
5310 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
5312 /*
5313 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5314 */
5315 if (ret == OK && evaluate && end_leader > start_leader)
5316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318 int val = 0;
5319#ifdef FEAT_FLOAT
5320 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322 if (rettv->v_type == VAR_FLOAT)
5323 f = rettv->vval.v_float;
5324 else
5325#endif
5326 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 clear_tv(rettv);
5330 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 else
5333 {
5334 while (end_leader > start_leader)
5335 {
5336 --end_leader;
5337 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005338 {
5339#ifdef FEAT_FLOAT
5340 if (rettv->v_type == VAR_FLOAT)
5341 f = !f;
5342 else
5343#endif
5344 val = !val;
5345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005347 {
5348#ifdef FEAT_FLOAT
5349 if (rettv->v_type == VAR_FLOAT)
5350 f = -f;
5351 else
5352#endif
5353 val = -val;
5354 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005355 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005356#ifdef FEAT_FLOAT
5357 if (rettv->v_type == VAR_FLOAT)
5358 {
5359 clear_tv(rettv);
5360 rettv->vval.v_float = f;
5361 }
5362 else
5363#endif
5364 {
5365 clear_tv(rettv);
5366 rettv->v_type = VAR_NUMBER;
5367 rettv->vval.v_number = val;
5368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371
5372 return ret;
5373}
5374
5375/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005376 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5377 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5379 */
5380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005381eval_index(
5382 char_u **arg,
5383 typval_T *rettv,
5384 int evaluate,
5385 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386{
5387 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005388 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390 long len = -1;
5391 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394
Bram Moolenaara03f2332016-02-06 18:09:59 +01005395 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005397 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005398 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005399 if (verbose)
5400 EMSG(_("E695: Cannot index a Funcref"));
5401 return FAIL;
5402 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005403#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005404 if (verbose)
5405 EMSG(_(e_float_as_string));
5406 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005407#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005408 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005409 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005410 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005411 if (verbose)
5412 EMSG(_("E909: Cannot index a special variable"));
5413 return FAIL;
5414 case VAR_UNKNOWN:
5415 if (evaluate)
5416 return FAIL;
5417 /* FALLTHROUGH */
5418
5419 case VAR_STRING:
5420 case VAR_NUMBER:
5421 case VAR_LIST:
5422 case VAR_DICT:
5423 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005424 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005426 init_tv(&var1);
5427 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005428 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 /*
5431 * dict.name
5432 */
5433 key = *arg + 1;
5434 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5435 ;
5436 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 }
5440 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442 /*
5443 * something[idx]
5444 *
5445 * Get the (first) variable from inside the [].
5446 */
5447 *arg = skipwhite(*arg + 1);
5448 if (**arg == ':')
5449 empty1 = TRUE;
5450 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5451 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005452 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5453 {
5454 /* not a number or string */
5455 clear_tv(&var1);
5456 return FAIL;
5457 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458
5459 /*
5460 * Get the second variable from inside the [:].
5461 */
5462 if (**arg == ':')
5463 {
5464 range = TRUE;
5465 *arg = skipwhite(*arg + 1);
5466 if (**arg == ']')
5467 empty2 = TRUE;
5468 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005470 if (!empty1)
5471 clear_tv(&var1);
5472 return FAIL;
5473 }
5474 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5475 {
5476 /* not a number or string */
5477 if (!empty1)
5478 clear_tv(&var1);
5479 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 return FAIL;
5481 }
5482 }
5483
5484 /* Check for the ']'. */
5485 if (**arg != ']')
5486 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005487 if (verbose)
5488 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 clear_tv(&var1);
5490 if (range)
5491 clear_tv(&var2);
5492 return FAIL;
5493 }
5494 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 }
5496
5497 if (evaluate)
5498 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005499 n1 = 0;
5500 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 n1 = get_tv_number(&var1);
5503 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 }
5505 if (range)
5506 {
5507 if (empty2)
5508 n2 = -1;
5509 else
5510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511 n2 = get_tv_number(&var2);
5512 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 }
5514 }
5515
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005518 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005519 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005520 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005521 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005522 case VAR_SPECIAL:
5523 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005524 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005525 break; /* not evaluating, skipping over subscript */
5526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 case VAR_NUMBER:
5528 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 len = (long)STRLEN(s);
5531 if (range)
5532 {
5533 /* The resulting variable is a substring. If the indexes
5534 * are out of range the result is empty. */
5535 if (n1 < 0)
5536 {
5537 n1 = len + n1;
5538 if (n1 < 0)
5539 n1 = 0;
5540 }
5541 if (n2 < 0)
5542 n2 = len + n2;
5543 else if (n2 >= len)
5544 n2 = len;
5545 if (n1 >= len || n2 < 0 || n1 > n2)
5546 s = NULL;
5547 else
5548 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5549 }
5550 else
5551 {
5552 /* The resulting variable is a string of a single
5553 * character. If the index is too big or negative the
5554 * result is empty. */
5555 if (n1 >= len || n1 < 0)
5556 s = NULL;
5557 else
5558 s = vim_strnsave(s + n1, 1);
5559 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 clear_tv(rettv);
5561 rettv->v_type = VAR_STRING;
5562 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 break;
5564
5565 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 if (n1 < 0)
5568 n1 = len + n1;
5569 if (!empty1 && (n1 < 0 || n1 >= len))
5570 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005571 /* For a range we allow invalid values and return an empty
5572 * list. A list index out of range is an error. */
5573 if (!range)
5574 {
5575 if (verbose)
5576 EMSGN(_(e_listidx), n1);
5577 return FAIL;
5578 }
5579 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 }
5581 if (range)
5582 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005583 list_T *l;
5584 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585
5586 if (n2 < 0)
5587 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005588 else if (n2 >= len)
5589 n2 = len - 1;
5590 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005591 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005592 l = list_alloc();
5593 if (l == NULL)
5594 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 n1 <= n2; ++n1)
5597 {
5598 if (list_append_tv(l, &item->li_tv) == FAIL)
5599 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005600 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 return FAIL;
5602 }
5603 item = item->li_next;
5604 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 clear_tv(rettv);
5606 rettv->v_type = VAR_LIST;
5607 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005608 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 }
5610 else
5611 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005612 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 clear_tv(rettv);
5614 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 }
5616 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005617
5618 case VAR_DICT:
5619 if (range)
5620 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005621 if (verbose)
5622 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 if (len == -1)
5624 clear_tv(&var1);
5625 return FAIL;
5626 }
5627 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005628 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629
5630 if (len == -1)
5631 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005632 key = get_tv_string_chk(&var1);
5633 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 clear_tv(&var1);
5636 return FAIL;
5637 }
5638 }
5639
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005640 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005642 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005643 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 if (len == -1)
5645 clear_tv(&var1);
5646 if (item == NULL)
5647 return FAIL;
5648
5649 copy_tv(&item->di_tv, &var1);
5650 clear_tv(rettv);
5651 *rettv = var1;
5652 }
5653 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654 }
5655 }
5656
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005657 return OK;
5658}
5659
5660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 * Get an option value.
5662 * "arg" points to the '&' or '+' before the option name.
5663 * "arg" is advanced to character after the option name.
5664 * Return OK or FAIL.
5665 */
5666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005667get_option_tv(
5668 char_u **arg,
5669 typval_T *rettv, /* when NULL, only check if option exists */
5670 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
5672 char_u *option_end;
5673 long numval;
5674 char_u *stringval;
5675 int opt_type;
5676 int c;
5677 int working = (**arg == '+'); /* has("+option") */
5678 int ret = OK;
5679 int opt_flags;
5680
5681 /*
5682 * Isolate the option name and find its value.
5683 */
5684 option_end = find_option_end(arg, &opt_flags);
5685 if (option_end == NULL)
5686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 EMSG2(_("E112: Option name missing: %s"), *arg);
5689 return FAIL;
5690 }
5691
5692 if (!evaluate)
5693 {
5694 *arg = option_end;
5695 return OK;
5696 }
5697
5698 c = *option_end;
5699 *option_end = NUL;
5700 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703 if (opt_type == -3) /* invalid name */
5704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 EMSG2(_("E113: Unknown option: %s"), *arg);
5707 ret = FAIL;
5708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
5711 if (opt_type == -2) /* hidden string option */
5712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005713 rettv->v_type = VAR_STRING;
5714 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716 else if (opt_type == -1) /* hidden number option */
5717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005718 rettv->v_type = VAR_NUMBER;
5719 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
5721 else if (opt_type == 1) /* number option */
5722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005723 rettv->v_type = VAR_NUMBER;
5724 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726 else /* string option */
5727 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005728 rettv->v_type = VAR_STRING;
5729 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731 }
5732 else if (working && (opt_type == -2 || opt_type == -1))
5733 ret = FAIL;
5734
5735 *option_end = c; /* put back for error messages */
5736 *arg = option_end;
5737
5738 return ret;
5739}
5740
5741/*
5742 * Allocate a variable for a string constant.
5743 * Return OK or FAIL.
5744 */
5745 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005746get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 char_u *p;
5749 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 int extra = 0;
5751
5752 /*
5753 * Find the end of the string, skipping backslashed characters.
5754 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
5757 if (*p == '\\' && p[1] != NUL)
5758 {
5759 ++p;
5760 /* A "\<x>" form occupies at least 4 characters, and produces up
5761 * to 6 characters: reserve space for 2 extra */
5762 if (*p == '<')
5763 extra += 2;
5764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766
5767 if (*p != '"')
5768 {
5769 EMSG2(_("E114: Missing quote: %s"), *arg);
5770 return FAIL;
5771 }
5772
5773 /* If only parsing, set *arg and return here */
5774 if (!evaluate)
5775 {
5776 *arg = p + 1;
5777 return OK;
5778 }
5779
5780 /*
5781 * Copy the string into allocated memory, handling backslashed
5782 * characters.
5783 */
5784 name = alloc((unsigned)(p - *arg + extra));
5785 if (name == NULL)
5786 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 rettv->v_type = VAR_STRING;
5788 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 {
5792 if (*p == '\\')
5793 {
5794 switch (*++p)
5795 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 case 'b': *name++ = BS; ++p; break;
5797 case 'e': *name++ = ESC; ++p; break;
5798 case 'f': *name++ = FF; ++p; break;
5799 case 'n': *name++ = NL; ++p; break;
5800 case 'r': *name++ = CAR; ++p; break;
5801 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
5803 case 'X': /* hex: "\x1", "\x12" */
5804 case 'x':
5805 case 'u': /* Unicode: "\u0023" */
5806 case 'U':
5807 if (vim_isxdigit(p[1]))
5808 {
5809 int n, nr;
5810 int c = toupper(*p);
5811
5812 if (c == 'X')
5813 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005814 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005816 else
5817 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 nr = 0;
5819 while (--n >= 0 && vim_isxdigit(p[1]))
5820 {
5821 ++p;
5822 nr = (nr << 4) + hex2nr(*p);
5823 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825#ifdef FEAT_MBYTE
5826 /* For "\u" store the number according to
5827 * 'encoding'. */
5828 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 else
5831#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 break;
5835
5836 /* octal: "\1", "\12", "\123" */
5837 case '0':
5838 case '1':
5839 case '2':
5840 case '3':
5841 case '4':
5842 case '5':
5843 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 case '7': *name = *p++ - '0';
5845 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 *name = (*name << 3) + *p++ - '0';
5848 if (*p >= '0' && *p <= '7')
5849 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 break;
5853
5854 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 if (extra != 0)
5857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 break;
5860 }
5861 /* FALLTHROUGH */
5862
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 break;
5865 }
5866 }
5867 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 *arg = p + 1;
5873
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 return OK;
5875}
5876
5877/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 * Return OK or FAIL.
5880 */
5881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005882get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 int reduce = 0;
5887
5888 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 */
5891 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5892 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 break;
5897 ++reduce;
5898 ++p;
5899 }
5900 }
5901
Bram Moolenaar8c711452005-01-14 21:53:12 +00005902 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 return FAIL;
5906 }
5907
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005909 if (!evaluate)
5910 {
5911 *arg = p + 1;
5912 return OK;
5913 }
5914
5915 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005916 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 */
5918 str = alloc((unsigned)((p - *arg) - reduce));
5919 if (str == NULL)
5920 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 rettv->v_type = VAR_STRING;
5922 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 break;
5930 ++p;
5931 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005933 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935 *arg = p + 1;
5936
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005937 return OK;
5938}
5939
Bram Moolenaarddecc252016-04-06 22:59:37 +02005940 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005941partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005942{
5943 int i;
5944
5945 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005946 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005947 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005948 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005949 func_unref(pt->pt_name);
5950 vim_free(pt->pt_name);
5951 vim_free(pt);
5952}
5953
5954/*
5955 * Unreference a closure: decrement the reference count and free it when it
5956 * becomes zero.
5957 */
5958 void
5959partial_unref(partial_T *pt)
5960{
5961 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005962 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005963}
5964
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005965/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 * Allocate a variable for a List and fill it from "*arg".
5967 * Return OK or FAIL.
5968 */
5969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005970get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971{
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 list_T *l = NULL;
5973 typval_T tv;
5974 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975
5976 if (evaluate)
5977 {
5978 l = list_alloc();
5979 if (l == NULL)
5980 return FAIL;
5981 }
5982
5983 *arg = skipwhite(*arg + 1);
5984 while (**arg != ']' && **arg != NUL)
5985 {
5986 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5987 goto failret;
5988 if (evaluate)
5989 {
5990 item = listitem_alloc();
5991 if (item != NULL)
5992 {
5993 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005994 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995 list_append(l, item);
5996 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005997 else
5998 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 }
6000
6001 if (**arg == ']')
6002 break;
6003 if (**arg != ',')
6004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006005 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 goto failret;
6007 }
6008 *arg = skipwhite(*arg + 1);
6009 }
6010
6011 if (**arg != ']')
6012 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006013 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014failret:
6015 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006016 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 return FAIL;
6018 }
6019
6020 *arg = skipwhite(*arg + 1);
6021 if (evaluate)
6022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006023 rettv->v_type = VAR_LIST;
6024 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 ++l->lv_refcount;
6026 }
6027
6028 return OK;
6029}
6030
6031/*
6032 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006033 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006035 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006036list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006038 list_T *l;
6039
6040 l = (list_T *)alloc_clear(sizeof(list_T));
6041 if (l != NULL)
6042 {
6043 /* Prepend the list to the list of lists for garbage collection. */
6044 if (first_list != NULL)
6045 first_list->lv_used_prev = l;
6046 l->lv_used_prev = NULL;
6047 l->lv_used_next = first_list;
6048 first_list = l;
6049 }
6050 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051}
6052
6053/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006054 * Allocate an empty list for a return value.
6055 * Returns OK or FAIL.
6056 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006058rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006059{
6060 list_T *l = list_alloc();
6061
6062 if (l == NULL)
6063 return FAIL;
6064
6065 rettv->vval.v_list = l;
6066 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006067 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006068 ++l->lv_refcount;
6069 return OK;
6070}
6071
6072/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 * Unreference a list: decrement the reference count and free it when it
6074 * becomes zero.
6075 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006077list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006079 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006080 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081}
6082
6083/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006084 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 * Ignores the reference count.
6086 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006087 static void
6088list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089{
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006092 for (item = l->lv_first; item != NULL; item = l->lv_first)
6093 {
6094 /* Remove the item before deleting it. */
6095 l->lv_first = item->li_next;
6096 clear_tv(&item->li_tv);
6097 vim_free(item);
6098 }
6099}
6100
6101 static void
6102list_free_list(list_T *l)
6103{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006104 /* Remove the list from the list of lists for garbage collection. */
6105 if (l->lv_used_prev == NULL)
6106 first_list = l->lv_used_next;
6107 else
6108 l->lv_used_prev->lv_used_next = l->lv_used_next;
6109 if (l->lv_used_next != NULL)
6110 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6111
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112 vim_free(l);
6113}
6114
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006115 void
6116list_free(list_T *l)
6117{
6118 if (!in_free_unref_items)
6119 {
6120 list_free_contents(l);
6121 list_free_list(l);
6122 }
6123}
6124
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125/*
6126 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006127 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006129 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006130listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006131{
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133}
6134
6135/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006136 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006139listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006141 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142 vim_free(item);
6143}
6144
6145/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006146 * Remove a list item from a List and free it. Also clears the value.
6147 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006149listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006150{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006151 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006152 listitem_free(item);
6153}
6154
6155/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156 * Get the number of items in a list.
6157 */
6158 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006159list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161 if (l == NULL)
6162 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006163 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164}
6165
6166/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167 * Return TRUE when two lists have exactly the same values.
6168 */
6169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006170list_equal(
6171 list_T *l1,
6172 list_T *l2,
6173 int ic, /* ignore case for strings */
6174 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175{
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006177
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006178 if (l1 == NULL || l2 == NULL)
6179 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006180 if (l1 == l2)
6181 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006182 if (list_len(l1) != list_len(l2))
6183 return FALSE;
6184
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185 for (item1 = l1->lv_first, item2 = l2->lv_first;
6186 item1 != NULL && item2 != NULL;
6187 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006189 return FALSE;
6190 return item1 == NULL && item2 == NULL;
6191}
6192
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006193/*
6194 * Return the dictitem that an entry in a hashtable points to.
6195 */
6196 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006197dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006198{
6199 return HI2DI(hi);
6200}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006201
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006203 * Return TRUE when two dictionaries have exactly the same key/values.
6204 */
6205 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006206dict_equal(
6207 dict_T *d1,
6208 dict_T *d2,
6209 int ic, /* ignore case for strings */
6210 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006211{
Bram Moolenaar33570922005-01-25 22:26:29 +00006212 hashitem_T *hi;
6213 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006214 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006215
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006216 if (d1 == NULL || d2 == NULL)
6217 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006218 if (d1 == d2)
6219 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006220 if (dict_len(d1) != dict_len(d2))
6221 return FALSE;
6222
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006223 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006224 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006225 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006226 if (!HASHITEM_EMPTY(hi))
6227 {
6228 item2 = dict_find(d2, hi->hi_key, -1);
6229 if (item2 == NULL)
6230 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006231 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006232 return FALSE;
6233 --todo;
6234 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006235 }
6236 return TRUE;
6237}
6238
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006239static int tv_equal_recurse_limit;
6240
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006241/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006243 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006244 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 */
6246 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006247tv_equal(
6248 typval_T *tv1,
6249 typval_T *tv2,
6250 int ic, /* ignore case */
6251 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006252{
6253 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006254 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006255 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006256 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006257
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006258 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6259 if ((tv1->v_type == VAR_FUNC
6260 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6261 && (tv2->v_type == VAR_FUNC
6262 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6263 {
6264 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6265 : tv1->vval.v_partial->pt_name;
6266 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6267 : tv2->vval.v_partial->pt_name;
6268 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6269 }
6270
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006271 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006272 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006273
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006274 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006275 * recursiveness to a limit. We guess they are equal then.
6276 * A fixed limit has the problem of still taking an awful long time.
6277 * Reduce the limit every time running into it. That should work fine for
6278 * deeply linked structures that are not recursively linked and catch
6279 * recursiveness quickly. */
6280 if (!recursive)
6281 tv_equal_recurse_limit = 1000;
6282 if (recursive_cnt >= tv_equal_recurse_limit)
6283 {
6284 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006285 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006286 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006287
6288 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006289 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006290 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006291 ++recursive_cnt;
6292 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6293 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006294 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006295
6296 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006297 ++recursive_cnt;
6298 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6299 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006300 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006301
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006302 case VAR_NUMBER:
6303 return tv1->vval.v_number == tv2->vval.v_number;
6304
6305 case VAR_STRING:
6306 s1 = get_tv_string_buf(tv1, buf1);
6307 s2 = get_tv_string_buf(tv2, buf2);
6308 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006309
6310 case VAR_SPECIAL:
6311 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006312
6313 case VAR_FLOAT:
6314#ifdef FEAT_FLOAT
6315 return tv1->vval.v_float == tv2->vval.v_float;
6316#endif
6317 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006318#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006319 return tv1->vval.v_job == tv2->vval.v_job;
6320#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006321 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006322#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006323 return tv1->vval.v_channel == tv2->vval.v_channel;
6324#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006325 case VAR_FUNC:
6326 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006327 case VAR_UNKNOWN:
6328 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006329 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006330
Bram Moolenaara03f2332016-02-06 18:09:59 +01006331 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6332 * does not equal anything, not even itself. */
6333 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334}
6335
6336/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337 * Locate item with index "n" in list "l" and return it.
6338 * A negative index is counted from the end; -1 is the last item.
6339 * Returns NULL when "n" is out of range.
6340 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006341 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006342list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343{
Bram Moolenaar33570922005-01-25 22:26:29 +00006344 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 long idx;
6346
6347 if (l == NULL)
6348 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006349
6350 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006352 n = l->lv_len + n;
6353
6354 /* Check for index out of range. */
6355 if (n < 0 || n >= l->lv_len)
6356 return NULL;
6357
6358 /* When there is a cached index may start search from there. */
6359 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006360 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006361 if (n < l->lv_idx / 2)
6362 {
6363 /* closest to the start of the list */
6364 item = l->lv_first;
6365 idx = 0;
6366 }
6367 else if (n > (l->lv_idx + l->lv_len) / 2)
6368 {
6369 /* closest to the end of the list */
6370 item = l->lv_last;
6371 idx = l->lv_len - 1;
6372 }
6373 else
6374 {
6375 /* closest to the cached index */
6376 item = l->lv_idx_item;
6377 idx = l->lv_idx;
6378 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379 }
6380 else
6381 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006382 if (n < l->lv_len / 2)
6383 {
6384 /* closest to the start of the list */
6385 item = l->lv_first;
6386 idx = 0;
6387 }
6388 else
6389 {
6390 /* closest to the end of the list */
6391 item = l->lv_last;
6392 idx = l->lv_len - 1;
6393 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006395
6396 while (n > idx)
6397 {
6398 /* search forward */
6399 item = item->li_next;
6400 ++idx;
6401 }
6402 while (n < idx)
6403 {
6404 /* search backward */
6405 item = item->li_prev;
6406 --idx;
6407 }
6408
6409 /* cache the used index */
6410 l->lv_idx = idx;
6411 l->lv_idx_item = item;
6412
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 return item;
6414}
6415
6416/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006417 * Get list item "l[idx]" as a number.
6418 */
6419 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006420list_find_nr(
6421 list_T *l,
6422 long idx,
6423 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006424{
6425 listitem_T *li;
6426
6427 li = list_find(l, idx);
6428 if (li == NULL)
6429 {
6430 if (errorp != NULL)
6431 *errorp = TRUE;
6432 return -1L;
6433 }
6434 return get_tv_number_chk(&li->li_tv, errorp);
6435}
6436
6437/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006438 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6439 */
6440 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006441list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006442{
6443 listitem_T *li;
6444
6445 li = list_find(l, idx - 1);
6446 if (li == NULL)
6447 {
6448 EMSGN(_(e_listidx), idx);
6449 return NULL;
6450 }
6451 return get_tv_string(&li->li_tv);
6452}
6453
6454/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006455 * Locate "item" list "l" and return its index.
6456 * Returns -1 when "item" is not in the list.
6457 */
6458 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006459list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006460{
6461 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006463
6464 if (l == NULL)
6465 return -1;
6466 idx = 0;
6467 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6468 ++idx;
6469 if (li == NULL)
6470 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006471 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006472}
6473
6474/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006475 * Append item "item" to the end of list "l".
6476 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006477 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479{
6480 if (l->lv_last == NULL)
6481 {
6482 /* empty list */
6483 l->lv_first = item;
6484 l->lv_last = item;
6485 item->li_prev = NULL;
6486 }
6487 else
6488 {
6489 l->lv_last->li_next = item;
6490 item->li_prev = l->lv_last;
6491 l->lv_last = item;
6492 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006493 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494 item->li_next = NULL;
6495}
6496
6497/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006501 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006502list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006504 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006505
Bram Moolenaar05159a02005-02-26 23:04:13 +00006506 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006508 copy_tv(tv, &li->li_tv);
6509 list_append(l, li);
6510 return OK;
6511}
6512
6513/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006514 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006515 * Return FAIL when out of memory.
6516 */
6517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006518list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006519{
6520 listitem_T *li = listitem_alloc();
6521
6522 if (li == NULL)
6523 return FAIL;
6524 li->li_tv.v_type = VAR_DICT;
6525 li->li_tv.v_lock = 0;
6526 li->li_tv.vval.v_dict = dict;
6527 list_append(list, li);
6528 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529 return OK;
6530}
6531
6532/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006533 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006534 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006535 * Returns FAIL when out of memory.
6536 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006539{
6540 listitem_T *li = listitem_alloc();
6541
6542 if (li == NULL)
6543 return FAIL;
6544 list_append(l, li);
6545 li->li_tv.v_type = VAR_STRING;
6546 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006547 if (str == NULL)
6548 li->li_tv.vval.v_string = NULL;
6549 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006550 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006551 return FAIL;
6552 return OK;
6553}
6554
6555/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006556 * Append "n" to list "l".
6557 * Returns FAIL when out of memory.
6558 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006559 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006560list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006561{
6562 listitem_T *li;
6563
6564 li = listitem_alloc();
6565 if (li == NULL)
6566 return FAIL;
6567 li->li_tv.v_type = VAR_NUMBER;
6568 li->li_tv.v_lock = 0;
6569 li->li_tv.vval.v_number = n;
6570 list_append(l, li);
6571 return OK;
6572}
6573
6574/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 * If "item" is NULL append at the end.
6577 * Return FAIL when out of memory.
6578 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006579 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006580list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581{
Bram Moolenaar33570922005-01-25 22:26:29 +00006582 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583
6584 if (ni == NULL)
6585 return FAIL;
6586 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006587 list_insert(l, ni, item);
6588 return OK;
6589}
6590
6591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006592list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006593{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006594 if (item == NULL)
6595 /* Append new item at end of list. */
6596 list_append(l, ni);
6597 else
6598 {
6599 /* Insert new item before existing item. */
6600 ni->li_prev = item->li_prev;
6601 ni->li_next = item;
6602 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006603 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006604 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006605 ++l->lv_idx;
6606 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006607 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006608 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006609 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006610 l->lv_idx_item = NULL;
6611 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006613 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006614 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006615}
6616
6617/*
6618 * Extend "l1" with "l2".
6619 * If "bef" is NULL append at the end, otherwise insert before this item.
6620 * Returns FAIL when out of memory.
6621 */
6622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006623list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624{
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006626 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006627
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006628 /* We also quit the loop when we have inserted the original item count of
6629 * the list, avoid a hang when we extend a list with itself. */
6630 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6632 return FAIL;
6633 return OK;
6634}
6635
6636/*
6637 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6638 * Return FAIL when out of memory.
6639 */
6640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006641list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642{
Bram Moolenaar33570922005-01-25 22:26:29 +00006643 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006645 if (l1 == NULL || l2 == NULL)
6646 return FAIL;
6647
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006649 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650 if (l == NULL)
6651 return FAIL;
6652 tv->v_type = VAR_LIST;
6653 tv->vval.v_list = l;
6654
6655 /* append all items from the second list */
6656 return list_extend(l, l2, NULL);
6657}
6658
6659/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006660 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006662 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 * Returns NULL when out of memory.
6664 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006665 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006666list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667{
Bram Moolenaar33570922005-01-25 22:26:29 +00006668 list_T *copy;
6669 listitem_T *item;
6670 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671
6672 if (orig == NULL)
6673 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674
6675 copy = list_alloc();
6676 if (copy != NULL)
6677 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006678 if (copyID != 0)
6679 {
6680 /* Do this before adding the items, because one of the items may
6681 * refer back to this list. */
6682 orig->lv_copyID = copyID;
6683 orig->lv_copylist = copy;
6684 }
6685 for (item = orig->lv_first; item != NULL && !got_int;
6686 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 {
6688 ni = listitem_alloc();
6689 if (ni == NULL)
6690 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006691 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006692 {
6693 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6694 {
6695 vim_free(ni);
6696 break;
6697 }
6698 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006700 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006701 list_append(copy, ni);
6702 }
6703 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006704 if (item != NULL)
6705 {
6706 list_unref(copy);
6707 copy = NULL;
6708 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709 }
6710
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006711 return copy;
6712}
6713
6714/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006715 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006716 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006717 * This used to be called list_remove, but that conflicts with a Sun header
6718 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006721vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006722{
Bram Moolenaar33570922005-01-25 22:26:29 +00006723 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006724
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006725 /* notify watchers */
6726 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006728 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006729 list_fix_watch(l, ip);
6730 if (ip == item2)
6731 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006732 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733
6734 if (item2->li_next == NULL)
6735 l->lv_last = item->li_prev;
6736 else
6737 item2->li_next->li_prev = item->li_prev;
6738 if (item->li_prev == NULL)
6739 l->lv_first = item2->li_next;
6740 else
6741 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006742 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006743}
6744
6745/*
6746 * Return an allocated string with the string representation of a list.
6747 * May return NULL.
6748 */
6749 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006750list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751{
6752 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006753
6754 if (tv->vval.v_list == NULL)
6755 return NULL;
6756 ga_init2(&ga, (int)sizeof(char), 80);
6757 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006758 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006759 {
6760 vim_free(ga.ga_data);
6761 return NULL;
6762 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006763 ga_append(&ga, ']');
6764 ga_append(&ga, NUL);
6765 return (char_u *)ga.ga_data;
6766}
6767
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006768typedef struct join_S {
6769 char_u *s;
6770 char_u *tofree;
6771} join_T;
6772
6773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006774list_join_inner(
6775 garray_T *gap, /* to store the result in */
6776 list_T *l,
6777 char_u *sep,
6778 int echo_style,
6779 int copyID,
6780 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006781{
6782 int i;
6783 join_T *p;
6784 int len;
6785 int sumlen = 0;
6786 int first = TRUE;
6787 char_u *tofree;
6788 char_u numbuf[NUMBUFLEN];
6789 listitem_T *item;
6790 char_u *s;
6791
6792 /* Stringify each item in the list. */
6793 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6794 {
6795 if (echo_style)
6796 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6797 else
6798 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6799 if (s == NULL)
6800 return FAIL;
6801
6802 len = (int)STRLEN(s);
6803 sumlen += len;
6804
Bram Moolenaarcde88542015-08-11 19:14:00 +02006805 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006806 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6807 if (tofree != NULL || s != numbuf)
6808 {
6809 p->s = s;
6810 p->tofree = tofree;
6811 }
6812 else
6813 {
6814 p->s = vim_strnsave(s, len);
6815 p->tofree = p->s;
6816 }
6817
6818 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006819 if (did_echo_string_emsg) /* recursion error, bail out */
6820 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006821 }
6822
6823 /* Allocate result buffer with its total size, avoid re-allocation and
6824 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6825 if (join_gap->ga_len >= 2)
6826 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6827 if (ga_grow(gap, sumlen + 2) == FAIL)
6828 return FAIL;
6829
6830 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6831 {
6832 if (first)
6833 first = FALSE;
6834 else
6835 ga_concat(gap, sep);
6836 p = ((join_T *)join_gap->ga_data) + i;
6837
6838 if (p->s != NULL)
6839 ga_concat(gap, p->s);
6840 line_breakcheck();
6841 }
6842
6843 return OK;
6844}
6845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006847 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006848 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006849 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006850 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006851 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006852list_join(
6853 garray_T *gap,
6854 list_T *l,
6855 char_u *sep,
6856 int echo_style,
6857 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006858{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006859 garray_T join_ga;
6860 int retval;
6861 join_T *p;
6862 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006863
Bram Moolenaard39a7512015-04-16 22:51:22 +02006864 if (l->lv_len < 1)
6865 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006866 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6867 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6868
6869 /* Dispose each item in join_ga. */
6870 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006871 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006872 p = (join_T *)join_ga.ga_data;
6873 for (i = 0; i < join_ga.ga_len; ++i)
6874 {
6875 vim_free(p->tofree);
6876 ++p;
6877 }
6878 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006879 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006880
6881 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006882}
6883
6884/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006885 * Return the next (unique) copy ID.
6886 * Used for serializing nested structures.
6887 */
6888 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006889get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006890{
6891 current_copyID += COPYID_INC;
6892 return current_copyID;
6893}
6894
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006895/* Used by get_func_tv() */
6896static garray_T funcargs = GA_EMPTY;
6897
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006898/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899 * Garbage collection for lists and dictionaries.
6900 *
6901 * We use reference counts to be able to free most items right away when they
6902 * are no longer used. But for composite items it's possible that it becomes
6903 * unused while the reference count is > 0: When there is a recursive
6904 * reference. Example:
6905 * :let l = [1, 2, 3]
6906 * :let d = {9: l}
6907 * :let l[1] = d
6908 *
6909 * Since this is quite unusual we handle this with garbage collection: every
6910 * once in a while find out which lists and dicts are not referenced from any
6911 * variable.
6912 *
6913 * Here is a good reference text about garbage collection (refers to Python
6914 * but it applies to all reference-counting mechanisms):
6915 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006916 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006917
6918/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 * Do garbage collection for lists and dicts.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006920 * When "testing" is TRUE this is called from garbagecollect_for_testing().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006922 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006924garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006925{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006926 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 buf_T *buf;
6929 win_T *wp;
6930 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006931 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006932 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006934#ifdef FEAT_WINDOWS
6935 tabpage_T *tp;
6936#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006938 if (!testing)
6939 {
6940 /* Only do this once. */
6941 want_garbage_collect = FALSE;
6942 may_garbage_collect = FALSE;
6943 garbage_collect_at_exit = FALSE;
6944 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006945
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006946 /* We advance by two because we add one for items referenced through
6947 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006948 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 /*
6951 * 1. Go through all accessible variables and mark all lists and dicts
6952 * with copyID.
6953 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954
6955 /* Don't free variables in the previous_funccal list unless they are only
6956 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006957 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006958 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6959 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6961 NULL);
6962 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6963 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 }
6965
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 /* script-local variables */
6967 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969
6970 /* buffer-local variables */
6971 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006972 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6973 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974
6975 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006976 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6978 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006979#ifdef FEAT_AUTOCMD
6980 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006981 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6982 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006983#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006985#ifdef FEAT_WINDOWS
6986 /* tabpage-local variables */
6987 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006988 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6989 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006990#endif
6991
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006993 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994
6995 /* function-local variables */
6996 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6997 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006998 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6999 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 }
7001
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007002 /* function call arguments, if v:testing is set. */
7003 for (i = 0; i < funcargs.ga_len; ++i)
7004 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7005 copyID, NULL, NULL);
7006
Bram Moolenaard812df62008-11-09 12:46:09 +00007007 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007008 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007009
Bram Moolenaar1dced572012-04-05 16:54:08 +02007010#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007012#endif
7013
Bram Moolenaardb913952012-06-29 12:54:53 +02007014#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007016#endif
7017
7018#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007019 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007020#endif
7021
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007022#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007023 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007024#endif
7025
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007026 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007027 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 /*
7029 * 2. Free lists and dictionaries that are not referenced.
7030 */
7031 did_free = free_unref_items(copyID);
7032
7033 /*
7034 * 3. Check if any funccal can be freed now.
7035 */
7036 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007037 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 if (can_free_funccal(*pfc, copyID))
7039 {
7040 fc = *pfc;
7041 *pfc = fc->caller;
7042 free_funccal(fc, TRUE);
7043 did_free = TRUE;
7044 did_free_funccal = TRUE;
7045 }
7046 else
7047 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007048 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 if (did_free_funccal)
7050 /* When a funccal was freed some more items might be garbage
7051 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007052 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007053 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 else if (p_verbose > 0)
7055 {
7056 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7057 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007058
7059 return did_free;
7060}
7061
7062/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007063 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007064 */
7065 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007066free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007067{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007068 dict_T *dd, *dd_next;
7069 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007070 int did_free = FALSE;
7071
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007072 /* Let all "free" functions know that we are here. This means no
7073 * dictionaries, lists, channels or jobs are to be freed, because we will
7074 * do that here. */
7075 in_free_unref_items = TRUE;
7076
7077 /*
7078 * PASS 1: free the contents of the items. We don't free the items
7079 * themselves yet, so that it is possible to decrement refcount counters
7080 */
7081
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007083 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007085 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007086 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007088 /* Free the Dictionary and ordinary items it contains, but don't
7089 * recurse into Lists and Dictionaries, they will be in the list
7090 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007091 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094
7095 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007096 * Go through the list of lists and free items without the copyID.
7097 * But don't free a list that has a watcher (used in a for loop), these
7098 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007100 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7101 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7102 && ll->lv_watch == NULL)
7103 {
7104 /* Free the List and ordinary items it contains, but don't recurse
7105 * into Lists and Dictionaries, they will be in the list of dicts
7106 * or list of lists. */
7107 list_free_contents(ll);
7108 did_free = TRUE;
7109 }
7110
7111#ifdef FEAT_JOB_CHANNEL
7112 /* Go through the list of jobs and free items without the copyID. This
7113 * must happen before doing channels, because jobs refer to channels, but
7114 * the reference from the channel to the job isn't tracked. */
7115 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7116
7117 /* Go through the list of channels and free items without the copyID. */
7118 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7119#endif
7120
7121 /*
7122 * PASS 2: free the items themselves.
7123 */
7124 for (dd = first_dict; dd != NULL; dd = dd_next)
7125 {
7126 dd_next = dd->dv_used_next;
7127 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7128 dict_free_dict(dd);
7129 }
7130
7131 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007132 {
7133 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007134 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7135 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007136 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007137 /* Free the List and ordinary items it contains, but don't recurse
7138 * into Lists and Dictionaries, they will be in the list of dicts
7139 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007140 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007141 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007142 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007143
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007144#ifdef FEAT_JOB_CHANNEL
7145 /* Go through the list of jobs and free items without the copyID. This
7146 * must happen before doing channels, because jobs refer to channels, but
7147 * the reference from the channel to the job isn't tracked. */
7148 free_unused_jobs(copyID, COPYID_MASK);
7149
7150 /* Go through the list of channels and free items without the copyID. */
7151 free_unused_channels(copyID, COPYID_MASK);
7152#endif
7153
7154 in_free_unref_items = FALSE;
7155
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007156 return did_free;
7157}
7158
7159/*
7160 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 * "list_stack" is used to add lists to be marked. Can be NULL.
7162 *
7163 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007164 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007166set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007167{
7168 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007169 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007171 hashtab_T *cur_ht;
7172 ht_stack_T *ht_stack = NULL;
7173 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007174
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007175 cur_ht = ht;
7176 for (;;)
7177 {
7178 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007179 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007180 /* Mark each item in the hashtab. If the item contains a hashtab
7181 * it is added to ht_stack, if it contains a list it is added to
7182 * list_stack. */
7183 todo = (int)cur_ht->ht_used;
7184 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7185 if (!HASHITEM_EMPTY(hi))
7186 {
7187 --todo;
7188 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7189 &ht_stack, list_stack);
7190 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192
7193 if (ht_stack == NULL)
7194 break;
7195
7196 /* take an item from the stack */
7197 cur_ht = ht_stack->ht;
7198 tempitem = ht_stack;
7199 ht_stack = ht_stack->prev;
7200 free(tempitem);
7201 }
7202
7203 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007204}
7205
7206/*
7207 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007208 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7209 *
7210 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007211 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007212 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007213set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007214{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007215 listitem_T *li;
7216 int abort = FALSE;
7217 list_T *cur_l;
7218 list_stack_T *list_stack = NULL;
7219 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007220
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007221 cur_l = l;
7222 for (;;)
7223 {
7224 if (!abort)
7225 /* Mark each item in the list. If the item contains a hashtab
7226 * it is added to ht_stack, if it contains a list it is added to
7227 * list_stack. */
7228 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7229 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7230 ht_stack, &list_stack);
7231 if (list_stack == NULL)
7232 break;
7233
7234 /* take an item from the stack */
7235 cur_l = list_stack->list;
7236 tempitem = list_stack;
7237 list_stack = list_stack->prev;
7238 free(tempitem);
7239 }
7240
7241 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007242}
7243
7244/*
7245 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007246 * "list_stack" is used to add lists to be marked. Can be NULL.
7247 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7248 *
7249 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007250 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007252set_ref_in_item(
7253 typval_T *tv,
7254 int copyID,
7255 ht_stack_T **ht_stack,
7256 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007257{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007258 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007259
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007260 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007261 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007262 dict_T *dd = tv->vval.v_dict;
7263
Bram Moolenaara03f2332016-02-06 18:09:59 +01007264 if (dd != NULL && dd->dv_copyID != copyID)
7265 {
7266 /* Didn't see this dict yet. */
7267 dd->dv_copyID = copyID;
7268 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007269 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007270 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7271 }
7272 else
7273 {
7274 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7275 if (newitem == NULL)
7276 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007277 else
7278 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007279 newitem->ht = &dd->dv_hashtab;
7280 newitem->prev = *ht_stack;
7281 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007282 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007283 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007284 }
7285 }
7286 else if (tv->v_type == VAR_LIST)
7287 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007288 list_T *ll = tv->vval.v_list;
7289
Bram Moolenaara03f2332016-02-06 18:09:59 +01007290 if (ll != NULL && ll->lv_copyID != copyID)
7291 {
7292 /* Didn't see this list yet. */
7293 ll->lv_copyID = copyID;
7294 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007295 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007296 abort = set_ref_in_list(ll, copyID, ht_stack);
7297 }
7298 else
7299 {
7300 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007301 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007302 if (newitem == NULL)
7303 abort = TRUE;
7304 else
7305 {
7306 newitem->list = ll;
7307 newitem->prev = *list_stack;
7308 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007309 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007310 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007311 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007312 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007313 else if (tv->v_type == VAR_PARTIAL)
7314 {
7315 partial_T *pt = tv->vval.v_partial;
7316 int i;
7317
7318 /* A partial does not have a copyID, because it cannot contain itself.
7319 */
7320 if (pt != NULL)
7321 {
7322 if (pt->pt_dict != NULL)
7323 {
7324 typval_T dtv;
7325
7326 dtv.v_type = VAR_DICT;
7327 dtv.vval.v_dict = pt->pt_dict;
7328 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7329 }
7330
7331 for (i = 0; i < pt->pt_argc; ++i)
7332 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7333 ht_stack, list_stack);
7334 }
7335 }
7336#ifdef FEAT_JOB_CHANNEL
7337 else if (tv->v_type == VAR_JOB)
7338 {
7339 job_T *job = tv->vval.v_job;
7340 typval_T dtv;
7341
7342 if (job != NULL && job->jv_copyID != copyID)
7343 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007344 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007345 if (job->jv_channel != NULL)
7346 {
7347 dtv.v_type = VAR_CHANNEL;
7348 dtv.vval.v_channel = job->jv_channel;
7349 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7350 }
7351 if (job->jv_exit_partial != NULL)
7352 {
7353 dtv.v_type = VAR_PARTIAL;
7354 dtv.vval.v_partial = job->jv_exit_partial;
7355 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7356 }
7357 }
7358 }
7359 else if (tv->v_type == VAR_CHANNEL)
7360 {
7361 channel_T *ch =tv->vval.v_channel;
7362 int part;
7363 typval_T dtv;
7364 jsonq_T *jq;
7365 cbq_T *cq;
7366
7367 if (ch != NULL && ch->ch_copyID != copyID)
7368 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007369 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007370 for (part = PART_SOCK; part <= PART_IN; ++part)
7371 {
7372 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7373 jq = jq->jq_next)
7374 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7375 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7376 cq = cq->cq_next)
7377 if (cq->cq_partial != NULL)
7378 {
7379 dtv.v_type = VAR_PARTIAL;
7380 dtv.vval.v_partial = cq->cq_partial;
7381 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7382 }
7383 if (ch->ch_part[part].ch_partial != NULL)
7384 {
7385 dtv.v_type = VAR_PARTIAL;
7386 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7387 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7388 }
7389 }
7390 if (ch->ch_partial != NULL)
7391 {
7392 dtv.v_type = VAR_PARTIAL;
7393 dtv.vval.v_partial = ch->ch_partial;
7394 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7395 }
7396 if (ch->ch_close_partial != NULL)
7397 {
7398 dtv.v_type = VAR_PARTIAL;
7399 dtv.vval.v_partial = ch->ch_close_partial;
7400 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7401 }
7402 }
7403 }
7404#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007405 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007406}
7407
7408/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 * Allocate an empty header for a dictionary.
7410 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007411 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413{
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007415
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007417 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007418 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007419 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007420 if (first_dict != NULL)
7421 first_dict->dv_used_prev = d;
7422 d->dv_used_next = first_dict;
7423 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007424 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007425
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007427 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007428 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007429 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007430 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007431 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007432 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433}
7434
7435/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007436 * Allocate an empty dict for a return value.
7437 * Returns OK or FAIL.
7438 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007439 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007440rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007441{
7442 dict_T *d = dict_alloc();
7443
7444 if (d == NULL)
7445 return FAIL;
7446
7447 rettv->vval.v_dict = d;
7448 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007449 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007450 ++d->dv_refcount;
7451 return OK;
7452}
7453
7454
7455/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 * Unreference a Dictionary: decrement the reference count and free it when it
7457 * becomes zero.
7458 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007459 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007460dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007462 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007463 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464}
7465
7466/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007467 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468 * Ignores the reference count.
7469 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007470 static void
7471dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007473 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007475 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007477 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007478 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007479 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007482 if (!HASHITEM_EMPTY(hi))
7483 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007484 /* Remove the item before deleting it, just in case there is
7485 * something recursive causing trouble. */
7486 di = HI2DI(hi);
7487 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007488 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007489 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007490 --todo;
7491 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007494}
7495
7496 static void
7497dict_free_dict(dict_T *d)
7498{
7499 /* Remove the dict from the list of dicts for garbage collection. */
7500 if (d->dv_used_prev == NULL)
7501 first_dict = d->dv_used_next;
7502 else
7503 d->dv_used_prev->dv_used_next = d->dv_used_next;
7504 if (d->dv_used_next != NULL)
7505 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506 vim_free(d);
7507}
7508
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007509 void
7510dict_free(dict_T *d)
7511{
7512 if (!in_free_unref_items)
7513 {
7514 dict_free_contents(d);
7515 dict_free_dict(d);
7516 }
7517}
7518
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519/*
7520 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 * The "key" is copied to the new item.
7522 * Note that the value of the item "di_tv" still needs to be initialized!
7523 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007525 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007526dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527{
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007530 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007532 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007534 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007535 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007536 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537}
7538
7539/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007540 * Make a copy of a Dictionary item.
7541 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007543dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007544{
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007546
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007547 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7548 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007549 if (di != NULL)
7550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007552 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007553 copy_tv(&org->di_tv, &di->di_tv);
7554 }
7555 return di;
7556}
7557
7558/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 * Remove item "item" from Dictionary "dict" and free it.
7560 */
7561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007562dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563{
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007567 if (HASHITEM_EMPTY(hi))
7568 EMSG2(_(e_intern2), "dictitem_remove()");
7569 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007571 dictitem_free(item);
7572}
7573
7574/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 * Free a dict item. Also clears the value.
7576 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007578dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007581 if (item->di_flags & DI_FLAGS_ALLOC)
7582 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583}
7584
7585/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7587 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007588 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 * Returns NULL when out of memory.
7590 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007592dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007593{
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 dict_T *copy;
7595 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007598
7599 if (orig == NULL)
7600 return NULL;
7601
7602 copy = dict_alloc();
7603 if (copy != NULL)
7604 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007605 if (copyID != 0)
7606 {
7607 orig->dv_copyID = copyID;
7608 orig->dv_copydict = copy;
7609 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007610 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007611 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007612 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007613 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007615 --todo;
7616
7617 di = dictitem_alloc(hi->hi_key);
7618 if (di == NULL)
7619 break;
7620 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007621 {
7622 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7623 copyID) == FAIL)
7624 {
7625 vim_free(di);
7626 break;
7627 }
7628 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 else
7630 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7631 if (dict_add(copy, di) == FAIL)
7632 {
7633 dictitem_free(di);
7634 break;
7635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007640 if (todo > 0)
7641 {
7642 dict_unref(copy);
7643 copy = NULL;
7644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 }
7646
7647 return copy;
7648}
7649
7650/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007652 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007654 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007655dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656{
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658}
7659
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007661 * Add a number or string entry to dictionary "d".
7662 * When "str" is NULL use number "nr", otherwise use "str".
7663 * Returns FAIL when out of memory and when key already exists.
7664 */
7665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007666dict_add_nr_str(
7667 dict_T *d,
7668 char *key,
7669 long nr,
7670 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007671{
7672 dictitem_T *item;
7673
7674 item = dictitem_alloc((char_u *)key);
7675 if (item == NULL)
7676 return FAIL;
7677 item->di_tv.v_lock = 0;
7678 if (str == NULL)
7679 {
7680 item->di_tv.v_type = VAR_NUMBER;
7681 item->di_tv.vval.v_number = nr;
7682 }
7683 else
7684 {
7685 item->di_tv.v_type = VAR_STRING;
7686 item->di_tv.vval.v_string = vim_strsave(str);
7687 }
7688 if (dict_add(d, item) == FAIL)
7689 {
7690 dictitem_free(item);
7691 return FAIL;
7692 }
7693 return OK;
7694}
7695
7696/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007697 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007698 * Returns FAIL when out of memory and when key already exists.
7699 */
7700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007701dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007702{
7703 dictitem_T *item;
7704
7705 item = dictitem_alloc((char_u *)key);
7706 if (item == NULL)
7707 return FAIL;
7708 item->di_tv.v_lock = 0;
7709 item->di_tv.v_type = VAR_LIST;
7710 item->di_tv.vval.v_list = list;
7711 if (dict_add(d, item) == FAIL)
7712 {
7713 dictitem_free(item);
7714 return FAIL;
7715 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007716 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007717 return OK;
7718}
7719
7720/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007721 * Get the number of items in a Dictionary.
7722 */
7723 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007724dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007725{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007726 if (d == NULL)
7727 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007728 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007729}
7730
7731/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 * Find item "key[len]" in Dictionary "d".
7733 * If "len" is negative use strlen(key).
7734 * Returns NULL when not found.
7735 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007736 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007737dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739#define AKEYLEN 200
7740 char_u buf[AKEYLEN];
7741 char_u *akey;
7742 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007745 if (len < 0)
7746 akey = key;
7747 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007748 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007749 tofree = akey = vim_strnsave(key, len);
7750 if (akey == NULL)
7751 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007752 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007753 else
7754 {
7755 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007756 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 akey = buf;
7758 }
7759
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007761 vim_free(tofree);
7762 if (HASHITEM_EMPTY(hi))
7763 return NULL;
7764 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765}
7766
7767/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007768 * Get a string item from a dictionary.
7769 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007770 * Returns NULL if the entry doesn't exist or out of memory.
7771 */
7772 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007773get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007774{
7775 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007776 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007777
7778 di = dict_find(d, key, -1);
7779 if (di == NULL)
7780 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007781 s = get_tv_string(&di->di_tv);
7782 if (save && s != NULL)
7783 s = vim_strsave(s);
7784 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007785}
7786
7787/*
7788 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007789 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007790 */
7791 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007792get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007793{
7794 dictitem_T *di;
7795
7796 di = dict_find(d, key, -1);
7797 if (di == NULL)
7798 return 0;
7799 return get_tv_number(&di->di_tv);
7800}
7801
7802/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007803 * Return an allocated string with the string representation of a Dictionary.
7804 * May return NULL.
7805 */
7806 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007807dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007808{
7809 garray_T ga;
7810 int first = TRUE;
7811 char_u *tofree;
7812 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007813 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007814 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007816 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007817
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007818 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007819 return NULL;
7820 ga_init2(&ga, (int)sizeof(char), 80);
7821 ga_append(&ga, '{');
7822
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007823 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007824 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007825 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007826 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007827 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007828 --todo;
7829
7830 if (first)
7831 first = FALSE;
7832 else
7833 ga_concat(&ga, (char_u *)", ");
7834
7835 tofree = string_quote(hi->hi_key, FALSE);
7836 if (tofree != NULL)
7837 {
7838 ga_concat(&ga, tofree);
7839 vim_free(tofree);
7840 }
7841 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007842 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007843 if (s != NULL)
7844 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007845 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007846 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007847 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007848 line_breakcheck();
7849
Bram Moolenaar8c711452005-01-14 21:53:12 +00007850 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007852 if (todo > 0)
7853 {
7854 vim_free(ga.ga_data);
7855 return NULL;
7856 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857
7858 ga_append(&ga, '}');
7859 ga_append(&ga, NUL);
7860 return (char_u *)ga.ga_data;
7861}
7862
7863/*
7864 * Allocate a variable for a Dictionary and fill it from "*arg".
7865 * Return OK or FAIL. Returns NOTDONE for {expr}.
7866 */
7867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007868get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007869{
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 dict_T *d = NULL;
7871 typval_T tvkey;
7872 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007873 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007874 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007875 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007876 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007877
7878 /*
7879 * First check if it's not a curly-braces thing: {expr}.
7880 * Must do this without evaluating, otherwise a function may be called
7881 * twice. Unfortunately this means we need to call eval1() twice for the
7882 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007884 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885 if (*start != '}')
7886 {
7887 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7888 return FAIL;
7889 if (*start == '}')
7890 return NOTDONE;
7891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007892
7893 if (evaluate)
7894 {
7895 d = dict_alloc();
7896 if (d == NULL)
7897 return FAIL;
7898 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007899 tvkey.v_type = VAR_UNKNOWN;
7900 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007901
7902 *arg = skipwhite(*arg + 1);
7903 while (**arg != '}' && **arg != NUL)
7904 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007905 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007906 goto failret;
7907 if (**arg != ':')
7908 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007909 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007910 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007911 goto failret;
7912 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007913 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007914 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007915 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007916 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007917 {
7918 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007919 clear_tv(&tvkey);
7920 goto failret;
7921 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007922 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923
7924 *arg = skipwhite(*arg + 1);
7925 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7926 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007927 if (evaluate)
7928 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 goto failret;
7930 }
7931 if (evaluate)
7932 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007933 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 if (item != NULL)
7935 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007936 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007937 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938 clear_tv(&tv);
7939 goto failret;
7940 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007941 item = dictitem_alloc(key);
7942 clear_tv(&tvkey);
7943 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007946 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007947 if (dict_add(d, item) == FAIL)
7948 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007949 }
7950 }
7951
7952 if (**arg == '}')
7953 break;
7954 if (**arg != ',')
7955 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007956 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007957 goto failret;
7958 }
7959 *arg = skipwhite(*arg + 1);
7960 }
7961
7962 if (**arg != '}')
7963 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007964 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965failret:
7966 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007967 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968 return FAIL;
7969 }
7970
7971 *arg = skipwhite(*arg + 1);
7972 if (evaluate)
7973 {
7974 rettv->v_type = VAR_DICT;
7975 rettv->vval.v_dict = d;
7976 ++d->dv_refcount;
7977 }
7978
7979 return OK;
7980}
7981
Bram Moolenaar17a13432016-01-24 14:22:10 +01007982 static char *
7983get_var_special_name(int nr)
7984{
7985 switch (nr)
7986 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007987 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007988 case VVAL_TRUE: return "v:true";
7989 case VVAL_NONE: return "v:none";
7990 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007991 }
7992 EMSG2(_(e_intern2), "get_var_special_name()");
7993 return "42";
7994}
7995
Bram Moolenaar8c711452005-01-14 21:53:12 +00007996/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 * Return a string with the string representation of a variable.
7998 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007999 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008000 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008001 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008002 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008003 */
8004 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008005echo_string(
8006 typval_T *tv,
8007 char_u **tofree,
8008 char_u *numbuf,
8009 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008010{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008011 static int recurse = 0;
8012 char_u *r = NULL;
8013
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008015 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008016 if (!did_echo_string_emsg)
8017 {
8018 /* Only give this message once for a recursive call to avoid
8019 * flooding the user with errors. And stop iterating over lists
8020 * and dicts. */
8021 did_echo_string_emsg = TRUE;
8022 EMSG(_("E724: variable nested too deep for displaying"));
8023 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008024 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008025 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008026 }
8027 ++recurse;
8028
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008029 switch (tv->v_type)
8030 {
8031 case VAR_FUNC:
8032 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008033 r = tv->vval.v_string;
8034 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008035
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008036 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008037 {
8038 partial_T *pt = tv->vval.v_partial;
8039 char_u *fname = string_quote(pt == NULL ? NULL
8040 : pt->pt_name, FALSE);
8041 garray_T ga;
8042 int i;
8043 char_u *tf;
8044
8045 ga_init2(&ga, 1, 100);
8046 ga_concat(&ga, (char_u *)"function(");
8047 if (fname != NULL)
8048 {
8049 ga_concat(&ga, fname);
8050 vim_free(fname);
8051 }
8052 if (pt != NULL && pt->pt_argc > 0)
8053 {
8054 ga_concat(&ga, (char_u *)", [");
8055 for (i = 0; i < pt->pt_argc; ++i)
8056 {
8057 if (i > 0)
8058 ga_concat(&ga, (char_u *)", ");
8059 ga_concat(&ga,
8060 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8061 vim_free(tf);
8062 }
8063 ga_concat(&ga, (char_u *)"]");
8064 }
8065 if (pt != NULL && pt->pt_dict != NULL)
8066 {
8067 typval_T dtv;
8068
8069 ga_concat(&ga, (char_u *)", ");
8070 dtv.v_type = VAR_DICT;
8071 dtv.vval.v_dict = pt->pt_dict;
8072 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8073 vim_free(tf);
8074 }
8075 ga_concat(&ga, (char_u *)")");
8076
8077 *tofree = ga.ga_data;
8078 r = *tofree;
8079 break;
8080 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008081
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008082 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008083 if (tv->vval.v_list == NULL)
8084 {
8085 *tofree = NULL;
8086 r = NULL;
8087 }
8088 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8089 {
8090 *tofree = NULL;
8091 r = (char_u *)"[...]";
8092 }
8093 else
8094 {
8095 tv->vval.v_list->lv_copyID = copyID;
8096 *tofree = list2string(tv, copyID);
8097 r = *tofree;
8098 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008099 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008100
Bram Moolenaar8c711452005-01-14 21:53:12 +00008101 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008102 if (tv->vval.v_dict == NULL)
8103 {
8104 *tofree = NULL;
8105 r = NULL;
8106 }
8107 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8108 {
8109 *tofree = NULL;
8110 r = (char_u *)"{...}";
8111 }
8112 else
8113 {
8114 tv->vval.v_dict->dv_copyID = copyID;
8115 *tofree = dict2string(tv, copyID);
8116 r = *tofree;
8117 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008118 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008119
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008120 case VAR_STRING:
8121 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008122 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008123 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008124 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008125 *tofree = NULL;
8126 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008127 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008128
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008129 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008130#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008131 *tofree = NULL;
8132 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8133 r = numbuf;
8134 break;
8135#endif
8136
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008137 case VAR_SPECIAL:
8138 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008139 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008140 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008141 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008142
Bram Moolenaar8502c702014-06-17 12:51:16 +02008143 if (--recurse == 0)
8144 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008146}
8147
8148/*
8149 * Return a string with the string representation of a variable.
8150 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8151 * "numbuf" is used for a number.
8152 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008153 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008154 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008155 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008156tv2string(
8157 typval_T *tv,
8158 char_u **tofree,
8159 char_u *numbuf,
8160 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008161{
8162 switch (tv->v_type)
8163 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008164 case VAR_FUNC:
8165 *tofree = string_quote(tv->vval.v_string, TRUE);
8166 return *tofree;
8167 case VAR_STRING:
8168 *tofree = string_quote(tv->vval.v_string, FALSE);
8169 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008170 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008171#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008172 *tofree = NULL;
8173 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8174 return numbuf;
8175#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008176 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008177 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008178 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008179 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008180 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008181 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008182 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008183 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008185 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008186 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008187}
8188
8189/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008190 * Return string "str" in ' quotes, doubling ' characters.
8191 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008192 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008193 */
8194 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008195string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008196{
Bram Moolenaar33570922005-01-25 22:26:29 +00008197 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008198 char_u *p, *r, *s;
8199
Bram Moolenaar33570922005-01-25 22:26:29 +00008200 len = (function ? 13 : 3);
8201 if (str != NULL)
8202 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008203 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 for (p = str; *p != NUL; mb_ptr_adv(p))
8205 if (*p == '\'')
8206 ++len;
8207 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008208 s = r = alloc(len);
8209 if (r != NULL)
8210 {
8211 if (function)
8212 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008213 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008214 r += 10;
8215 }
8216 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008217 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008218 if (str != NULL)
8219 for (p = str; *p != NUL; )
8220 {
8221 if (*p == '\'')
8222 *r++ = '\'';
8223 MB_COPY_CHAR(p, r);
8224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008225 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008226 if (function)
8227 *r++ = ')';
8228 *r++ = NUL;
8229 }
8230 return s;
8231}
8232
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008233#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008234/*
8235 * Convert the string "text" to a floating point number.
8236 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8237 * this always uses a decimal point.
8238 * Returns the length of the text that was consumed.
8239 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008240 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008241string2float(
8242 char_u *text,
8243 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008244{
8245 char *s = (char *)text;
8246 float_T f;
8247
8248 f = strtod(s, &s);
8249 *value = f;
8250 return (int)((char_u *)s - text);
8251}
8252#endif
8253
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 * Get the value of an environment variable.
8256 * "arg" is pointing to the '$'. It is advanced to after the name.
8257 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008258 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 */
8260 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008261get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262{
8263 char_u *string = NULL;
8264 int len;
8265 int cc;
8266 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008267 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268
8269 ++*arg;
8270 name = *arg;
8271 len = get_env_len(arg);
8272 if (evaluate)
8273 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008274 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008275 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008276
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008277 cc = name[len];
8278 name[len] = NUL;
8279 /* first try vim_getenv(), fast for normal environment vars */
8280 string = vim_getenv(name, &mustfree);
8281 if (string != NULL && *string != NUL)
8282 {
8283 if (!mustfree)
8284 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008286 else
8287 {
8288 if (mustfree)
8289 vim_free(string);
8290
8291 /* next try expanding things like $VIM and ${HOME} */
8292 string = expand_env_save(name - 1);
8293 if (string != NULL && *string == '$')
8294 {
8295 vim_free(string);
8296 string = NULL;
8297 }
8298 }
8299 name[len] = cc;
8300
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008301 rettv->v_type = VAR_STRING;
8302 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 }
8304
8305 return OK;
8306}
8307
8308/*
8309 * Array with names and number of arguments of all internal functions
8310 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8311 */
8312static struct fst
8313{
8314 char *f_name; /* function name */
8315 char f_min_argc; /* minimal number of arguments */
8316 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008317 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008318 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319} functions[] =
8320{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008321#ifdef FEAT_FLOAT
8322 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008323 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008324#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008325 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008326 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008327 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"append", 2, 2, f_append},
8329 {"argc", 0, 0, f_argc},
8330 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008331 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008332 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008333#ifdef FEAT_FLOAT
8334 {"asin", 1, 1, f_asin}, /* WJMc */
8335#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008336 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008337 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008338 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008339 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008340 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008341 {"assert_notequal", 2, 3, f_assert_notequal},
8342 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008343 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008344#ifdef FEAT_FLOAT
8345 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008346 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008349 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"bufexists", 1, 1, f_bufexists},
8351 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8352 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8353 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8354 {"buflisted", 1, 1, f_buflisted},
8355 {"bufloaded", 1, 1, f_bufloaded},
8356 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008357 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"bufwinnr", 1, 1, f_bufwinnr},
8359 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008360 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008361 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008362 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008363#ifdef FEAT_FLOAT
8364 {"ceil", 1, 1, f_ceil},
8365#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008366#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008367 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008368 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8369 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008370 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008371 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008372 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008373 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008374 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008375 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008376 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008377 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008378 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8379 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008380 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008381 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008382#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008383 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008384 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008386 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008388#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008389 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008390 {"complete_add", 1, 1, f_complete_add},
8391 {"complete_check", 0, 0, f_complete_check},
8392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008394 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008395#ifdef FEAT_FLOAT
8396 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008397 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008398#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008399 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008401 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008402 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008403 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008405 {"diff_filler", 1, 1, f_diff_filler},
8406 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008407 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008408 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008410 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 {"eventhandler", 0, 0, f_eventhandler},
8412 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008413 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008415#ifdef FEAT_FLOAT
8416 {"exp", 1, 1, f_exp},
8417#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008418 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008419 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008420 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8422 {"filereadable", 1, 1, f_filereadable},
8423 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008424 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008425 {"finddir", 1, 3, f_finddir},
8426 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008427#ifdef FEAT_FLOAT
8428 {"float2nr", 1, 1, f_float2nr},
8429 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008430 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008431#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008432 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"fnamemodify", 2, 2, f_fnamemodify},
8434 {"foldclosed", 1, 1, f_foldclosed},
8435 {"foldclosedend", 1, 1, f_foldclosedend},
8436 {"foldlevel", 1, 1, f_foldlevel},
8437 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008438 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008440 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008441 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008442 {"garbagecollect_for_testing", 0, 0, f_garbagecollect_for_testing},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008443 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008444 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008445 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {"getchar", 0, 1, f_getchar},
8447 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008448 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"getcmdline", 0, 0, f_getcmdline},
8450 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008451 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008452 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008453 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008454 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008455 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008456 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {"getfsize", 1, 1, f_getfsize},
8458 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008459 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008460 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008461 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008462 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008463 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008464 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008465 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008466 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008468 {"gettabvar", 2, 3, f_gettabvar},
8469 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {"getwinposx", 0, 0, f_getwinposx},
8471 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008472 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008473 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008474 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008475 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008477 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008478 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008479 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8481 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8482 {"histadd", 2, 2, f_histadd},
8483 {"histdel", 1, 2, f_histdel},
8484 {"histget", 1, 2, f_histget},
8485 {"histnr", 1, 1, f_histnr},
8486 {"hlID", 1, 1, f_hlID},
8487 {"hlexists", 1, 1, f_hlexists},
8488 {"hostname", 0, 0, f_hostname},
8489 {"iconv", 3, 3, f_iconv},
8490 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008491 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008492 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008494 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 {"inputrestore", 0, 0, f_inputrestore},
8496 {"inputsave", 0, 0, f_inputsave},
8497 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008498 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008499 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008501 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008502#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8503 {"isnan", 1, 1, f_isnan},
8504#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008505 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008506#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008507 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008508 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008509 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008510 {"job_start", 1, 2, f_job_start},
8511 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008512 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008513#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008514 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008515 {"js_decode", 1, 1, f_js_decode},
8516 {"js_encode", 1, 1, f_js_encode},
8517 {"json_decode", 1, 1, f_json_decode},
8518 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008519 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {"libcall", 3, 3, f_libcall},
8523 {"libcallnr", 3, 3, f_libcallnr},
8524 {"line", 1, 1, f_line},
8525 {"line2byte", 1, 1, f_line2byte},
8526 {"lispindent", 1, 1, f_lispindent},
8527 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008528#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008529 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008530 {"log10", 1, 1, f_log10},
8531#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008532#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008533 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008534#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008535 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008536 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008537 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008538 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008539 {"matchadd", 2, 5, f_matchadd},
8540 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008541 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008542 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008543 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008544 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008545 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008546 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008547 {"max", 1, 1, f_max},
8548 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008549#ifdef vim_mkdir
8550 {"mkdir", 1, 3, f_mkdir},
8551#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008552 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008553#ifdef FEAT_MZSCHEME
8554 {"mzeval", 1, 1, f_mzeval},
8555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008557 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008558 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008559 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008560#ifdef FEAT_PERL
8561 {"perleval", 1, 1, f_perleval},
8562#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008563#ifdef FEAT_FLOAT
8564 {"pow", 2, 2, f_pow},
8565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008567 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008568 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008569#ifdef FEAT_PYTHON3
8570 {"py3eval", 1, 1, f_py3eval},
8571#endif
8572#ifdef FEAT_PYTHON
8573 {"pyeval", 1, 1, f_pyeval},
8574#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008575 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008576 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008577 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008578#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008579 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008580#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008581 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {"remote_expr", 2, 3, f_remote_expr},
8583 {"remote_foreground", 1, 1, f_remote_foreground},
8584 {"remote_peek", 1, 2, f_remote_peek},
8585 {"remote_read", 1, 1, f_remote_read},
8586 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008587 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008589 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008591 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008592#ifdef FEAT_FLOAT
8593 {"round", 1, 1, f_round},
8594#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008595 {"screenattr", 2, 2, f_screenattr},
8596 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008597 {"screencol", 0, 0, f_screencol},
8598 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008599 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008600 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008601 {"searchpair", 3, 7, f_searchpair},
8602 {"searchpairpos", 3, 7, f_searchpairpos},
8603 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 {"server2client", 2, 2, f_server2client},
8605 {"serverlist", 0, 0, f_serverlist},
8606 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008607 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008609 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008611 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008612 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008613 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008614 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008616 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008617 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008619#ifdef FEAT_CRYPT
8620 {"sha256", 1, 1, f_sha256},
8621#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008622 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008623 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008625#ifdef FEAT_FLOAT
8626 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008627 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008628#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008629 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008630 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008631 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008632 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008633 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008634#ifdef FEAT_FLOAT
8635 {"sqrt", 1, 1, f_sqrt},
8636 {"str2float", 1, 1, f_str2float},
8637#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008638 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008639 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008640 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008641 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#ifdef HAVE_STRFTIME
8643 {"strftime", 1, 2, f_strftime},
8644#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008645 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008646 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008647 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 {"strlen", 1, 1, f_strlen},
8649 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008650 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008652 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008653 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 {"substitute", 4, 4, f_substitute},
8655 {"synID", 3, 3, f_synID},
8656 {"synIDattr", 2, 3, f_synIDattr},
8657 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008658 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008659 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008660 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008661 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008662 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008663 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008664 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008665 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008666 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008667#ifdef FEAT_FLOAT
8668 {"tan", 1, 1, f_tan},
8669 {"tanh", 1, 1, f_tanh},
8670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008672 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008673#ifdef FEAT_TIMERS
8674 {"timer_start", 2, 3, f_timer_start},
8675 {"timer_stop", 1, 1, f_timer_stop},
8676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 {"tolower", 1, 1, f_tolower},
8678 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008679 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008680#ifdef FEAT_FLOAT
8681 {"trunc", 1, 1, f_trunc},
8682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008684 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008685 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008686 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008687 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 {"virtcol", 1, 1, f_virtcol},
8689 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008690 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008691 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008692 {"win_getid", 0, 2, f_win_getid},
8693 {"win_gotoid", 1, 1, f_win_gotoid},
8694 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8695 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 {"winbufnr", 1, 1, f_winbufnr},
8697 {"wincol", 0, 0, f_wincol},
8698 {"winheight", 1, 1, f_winheight},
8699 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008700 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008702 {"winrestview", 1, 1, f_winrestview},
8703 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008705 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008706 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008707 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708};
8709
8710#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8711
8712/*
8713 * Function given to ExpandGeneric() to obtain the list of internal
8714 * or user defined function names.
8715 */
8716 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008717get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718{
8719 static int intidx = -1;
8720 char_u *name;
8721
8722 if (idx == 0)
8723 intidx = -1;
8724 if (intidx < 0)
8725 {
8726 name = get_user_func_name(xp, idx);
8727 if (name != NULL)
8728 return name;
8729 }
8730 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8731 {
8732 STRCPY(IObuff, functions[intidx].f_name);
8733 STRCAT(IObuff, "(");
8734 if (functions[intidx].f_max_argc == 0)
8735 STRCAT(IObuff, ")");
8736 return IObuff;
8737 }
8738
8739 return NULL;
8740}
8741
8742/*
8743 * Function given to ExpandGeneric() to obtain the list of internal or
8744 * user defined variable or function names.
8745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008747get_expr_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_function_name(xp, idx);
8757 if (name != NULL)
8758 return name;
8759 }
8760 return get_user_var_name(xp, ++intidx);
8761}
8762
8763#endif /* FEAT_CMDL_COMPL */
8764
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008765#if defined(EBCDIC) || defined(PROTO)
8766/*
8767 * Compare struct fst by function name.
8768 */
8769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008770compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008771{
8772 struct fst *p1 = (struct fst *)s1;
8773 struct fst *p2 = (struct fst *)s2;
8774
8775 return STRCMP(p1->f_name, p2->f_name);
8776}
8777
8778/*
8779 * Sort the function table by function name.
8780 * The sorting of the table above is ASCII dependant.
8781 * On machines using EBCDIC we have to sort it.
8782 */
8783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008784sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008785{
8786 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8787
8788 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8789}
8790#endif
8791
8792
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793/*
8794 * Find internal function in table above.
8795 * Return index, or -1 if not found
8796 */
8797 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008798find_internal_func(
8799 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
8801 int first = 0;
8802 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8803 int cmp;
8804 int x;
8805
8806 /*
8807 * Find the function name in the table. Binary search.
8808 */
8809 while (first <= last)
8810 {
8811 x = first + ((unsigned)(last - first) >> 1);
8812 cmp = STRCMP(name, functions[x].f_name);
8813 if (cmp < 0)
8814 last = x - 1;
8815 else if (cmp > 0)
8816 first = x + 1;
8817 else
8818 return x;
8819 }
8820 return -1;
8821}
8822
8823/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008824 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8825 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008826 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8827 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008828 */
8829 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008830deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008831{
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008833 int cc;
8834
Bram Moolenaar65639032016-03-16 21:40:30 +01008835 if (partialp != NULL)
8836 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008837
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008838 cc = name[*lenp];
8839 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008840 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008841 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008843 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008845 {
8846 *lenp = 0;
8847 return (char_u *)""; /* just in case */
8848 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008849 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008851 }
8852
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008853 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8854 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008855 partial_T *pt = v->di_tv.vval.v_partial;
8856
8857 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008858 {
8859 *lenp = 0;
8860 return (char_u *)""; /* just in case */
8861 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008862 if (partialp != NULL)
8863 *partialp = pt;
8864 *lenp = (int)STRLEN(pt->pt_name);
8865 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008866 }
8867
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008868 return name;
8869}
8870
8871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 * Allocate a variable for the result of a function.
8873 * Return OK or FAIL.
8874 */
8875 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008876get_func_tv(
8877 char_u *name, /* name of the function */
8878 int len, /* length of "name" */
8879 typval_T *rettv,
8880 char_u **arg, /* argument, pointing to the '(' */
8881 linenr_T firstline, /* first line of range */
8882 linenr_T lastline, /* last line of range */
8883 int *doesrange, /* return: function handled range */
8884 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008885 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008886 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 char_u *argp;
8889 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008890 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 int argcount = 0; /* number of arguments found */
8892
8893 /*
8894 * Get the arguments.
8895 */
8896 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008897 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 {
8899 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8900 if (*argp == ')' || *argp == ',' || *argp == NUL)
8901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8903 {
8904 ret = FAIL;
8905 break;
8906 }
8907 ++argcount;
8908 if (*argp != ',')
8909 break;
8910 }
8911 if (*argp == ')')
8912 ++argp;
8913 else
8914 ret = FAIL;
8915
8916 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008917 {
8918 int i = 0;
8919
8920 if (get_vim_var_nr(VV_TESTING))
8921 {
8922 /* Prepare for calling garbagecollect_for_testing(), need to know
8923 * what variables are used on the call stack. */
8924 if (funcargs.ga_itemsize == 0)
8925 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8926 for (i = 0; i < argcount; ++i)
8927 if (ga_grow(&funcargs, 1) == OK)
8928 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8929 &argvars[i];
8930 }
8931
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008933 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008934
8935 funcargs.ga_len -= i;
8936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008938 {
8939 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008940 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008942 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
8945 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
8948 *arg = skipwhite(argp);
8949 return ret;
8950}
8951
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008952#define ERROR_UNKNOWN 0
8953#define ERROR_TOOMANY 1
8954#define ERROR_TOOFEW 2
8955#define ERROR_SCRIPT 3
8956#define ERROR_DICT 4
8957#define ERROR_NONE 5
8958#define ERROR_OTHER 6
8959#define FLEN_FIXED 40
8960
8961/*
8962 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8963 * Change <SNR>123_name() to K_SNR 123_name().
8964 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8965 * (slow).
8966 */
8967 static char_u *
8968fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8969{
8970 int llen;
8971 char_u *fname;
8972 int i;
8973
8974 llen = eval_fname_script(name);
8975 if (llen > 0)
8976 {
8977 fname_buf[0] = K_SPECIAL;
8978 fname_buf[1] = KS_EXTRA;
8979 fname_buf[2] = (int)KE_SNR;
8980 i = 3;
8981 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8982 {
8983 if (current_SID <= 0)
8984 *error = ERROR_SCRIPT;
8985 else
8986 {
8987 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8988 i = (int)STRLEN(fname_buf);
8989 }
8990 }
8991 if (i + STRLEN(name + llen) < FLEN_FIXED)
8992 {
8993 STRCPY(fname_buf + i, name + llen);
8994 fname = fname_buf;
8995 }
8996 else
8997 {
8998 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8999 if (fname == NULL)
9000 *error = ERROR_OTHER;
9001 else
9002 {
9003 *tofree = fname;
9004 mch_memmove(fname, fname_buf, (size_t)i);
9005 STRCPY(fname + i, name + llen);
9006 }
9007 }
9008 }
9009 else
9010 fname = name;
9011 return fname;
9012}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013
9014/*
9015 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009016 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009017 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009020call_func(
9021 char_u *funcname, /* name of the function */
9022 int len, /* length of "name" */
9023 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009024 int argcount_in, /* number of "argvars" */
9025 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009026 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009027 linenr_T firstline, /* first line of range */
9028 linenr_T lastline, /* last line of range */
9029 int *doesrange, /* return: function handled range */
9030 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009031 partial_T *partial, /* optional, can be NULL */
9032 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
9034 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 int error = ERROR_NONE;
9036 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009039 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009041 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009042 int argcount = argcount_in;
9043 typval_T *argvars = argvars_in;
9044 dict_T *selfdict = selfdict_in;
9045 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9046 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009047
9048 /* Make a copy of the name, if it comes from a funcref variable it could
9049 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009050 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009051 if (name == NULL)
9052 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009054 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055
9056 *doesrange = FALSE;
9057
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009058 if (partial != NULL)
9059 {
9060 if (partial->pt_dict != NULL)
9061 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009062 /* When the function has a partial with a dict and there is a dict
9063 * argument, use the dict argument. That is backwards compatible.
9064 */
9065 if (selfdict_in == NULL)
9066 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009067 }
9068 if (error == ERROR_NONE && partial->pt_argc > 0)
9069 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009070 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9071 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9072 for (i = 0; i < argcount_in; ++i)
9073 argv[i + argv_clear] = argvars_in[i];
9074 argvars = argv;
9075 argcount = partial->pt_argc + argcount_in;
9076 }
9077 }
9078
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
9080 /* execute the function if no errors detected and executing */
9081 if (evaluate && error == ERROR_NONE)
9082 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009083 char_u *rfname = fname;
9084
9085 /* Ignore "g:" before a function name. */
9086 if (fname[0] == 'g' && fname[1] == ':')
9087 rfname = fname + 2;
9088
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009089 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9090 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 error = ERROR_UNKNOWN;
9092
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009093 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 {
9095 /*
9096 * User defined function.
9097 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009098 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009099
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009101 /* Trigger FuncUndefined event, may load the function. */
9102 if (fp == NULL
9103 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009104 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009105 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009107 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009108 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 }
9110#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009111 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009112 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009113 {
9114 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009115 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009116 }
9117
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 if (fp != NULL)
9119 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009120 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009122 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009124 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009126 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009127 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 else
9129 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009130 int did_save_redo = FALSE;
9131
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 /*
9133 * Call the user function.
9134 * Save and restore search patterns, script variables and
9135 * redo buffer.
9136 */
9137 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009138#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009139 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009140#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009141 {
9142 saveRedobuff();
9143 did_save_redo = TRUE;
9144 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009145 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009147 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009148 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9149 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9150 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009151 /* Function was unreferenced while being used, free it
9152 * now. */
9153 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009154 if (did_save_redo)
9155 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 restore_search_patterns();
9157 error = ERROR_NONE;
9158 }
9159 }
9160 }
9161 else
9162 {
9163 /*
9164 * Find the function name in the table, call its implementation.
9165 */
9166 i = find_internal_func(fname);
9167 if (i >= 0)
9168 {
9169 if (argcount < functions[i].f_min_argc)
9170 error = ERROR_TOOFEW;
9171 else if (argcount > functions[i].f_max_argc)
9172 error = ERROR_TOOMANY;
9173 else
9174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009175 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 error = ERROR_NONE;
9178 }
9179 }
9180 }
9181 /*
9182 * The function call (or "FuncUndefined" autocommand sequence) might
9183 * have been aborted by an error, an interrupt, or an explicitly thrown
9184 * exception that has not been caught so far. This situation can be
9185 * tested for by calling aborting(). For an error in an internal
9186 * function or for the "E132" error in call_user_func(), however, the
9187 * throw point at which the "force_abort" flag (temporarily reset by
9188 * emsg()) is normally updated has not been reached yet. We need to
9189 * update that flag first to make aborting() reliable.
9190 */
9191 update_force_abort();
9192 }
9193 if (error == ERROR_NONE)
9194 ret = OK;
9195
9196 /*
9197 * Report an error unless the argument evaluation or function call has been
9198 * cancelled due to an aborting error, an interrupt, or an exception.
9199 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009200 if (!aborting())
9201 {
9202 switch (error)
9203 {
9204 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009205 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009206 break;
9207 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009208 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009209 break;
9210 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009211 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009212 name);
9213 break;
9214 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009215 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009216 name);
9217 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009218 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009219 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009220 name);
9221 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009222 }
9223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009225 while (argv_clear > 0)
9226 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009227 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009228 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229
9230 return ret;
9231}
9232
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009233/*
9234 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009235 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009236 */
9237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009238emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009239{
9240 char_u *p;
9241
9242 if (*name == K_SPECIAL)
9243 p = concat_str((char_u *)"<SNR>", name + 3);
9244 else
9245 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009246 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009247 if (p != name)
9248 vim_free(p);
9249}
9250
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009251/*
9252 * Return TRUE for a non-zero Number and a non-empty String.
9253 */
9254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009255non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009256{
9257 return ((argvars[0].v_type == VAR_NUMBER
9258 && argvars[0].vval.v_number != 0)
9259 || (argvars[0].v_type == VAR_STRING
9260 && argvars[0].vval.v_string != NULL
9261 && *argvars[0].vval.v_string != NUL));
9262}
9263
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264/*********************************************
9265 * Implementation of the built-in functions
9266 */
9267
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009268#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009269static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009270
9271/*
9272 * Get the float value of "argvars[0]" into "f".
9273 * Returns FAIL when the argument is not a Number or Float.
9274 */
9275 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009276get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009277{
9278 if (argvars[0].v_type == VAR_FLOAT)
9279 {
9280 *f = argvars[0].vval.v_float;
9281 return OK;
9282 }
9283 if (argvars[0].v_type == VAR_NUMBER)
9284 {
9285 *f = (float_T)argvars[0].vval.v_number;
9286 return OK;
9287 }
9288 EMSG(_("E808: Number or Float required"));
9289 return FAIL;
9290}
9291
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009292/*
9293 * "abs(expr)" function
9294 */
9295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009296f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009297{
9298 if (argvars[0].v_type == VAR_FLOAT)
9299 {
9300 rettv->v_type = VAR_FLOAT;
9301 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9302 }
9303 else
9304 {
9305 varnumber_T n;
9306 int error = FALSE;
9307
9308 n = get_tv_number_chk(&argvars[0], &error);
9309 if (error)
9310 rettv->vval.v_number = -1;
9311 else if (n > 0)
9312 rettv->vval.v_number = n;
9313 else
9314 rettv->vval.v_number = -n;
9315 }
9316}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009317
9318/*
9319 * "acos()" function
9320 */
9321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009322f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009323{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009324 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009325
9326 rettv->v_type = VAR_FLOAT;
9327 if (get_float_arg(argvars, &f) == OK)
9328 rettv->vval.v_float = acos(f);
9329 else
9330 rettv->vval.v_float = 0.0;
9331}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009332#endif
9333
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009335 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 */
9337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009338f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339{
Bram Moolenaar33570922005-01-25 22:26:29 +00009340 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009343 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009345 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009346 && !tv_check_lock(l->lv_lock,
9347 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009348 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009350 }
9351 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009352 EMSG(_(e_listreq));
9353}
9354
9355/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009356 * "alloc_fail(id, countdown, repeat)" function
9357 */
9358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009359f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009360{
9361 if (argvars[0].v_type != VAR_NUMBER
9362 || argvars[0].vval.v_number <= 0
9363 || argvars[1].v_type != VAR_NUMBER
9364 || argvars[1].vval.v_number < 0
9365 || argvars[2].v_type != VAR_NUMBER)
9366 EMSG(_(e_invarg));
9367 else
9368 {
9369 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009370 if (alloc_fail_id >= aid_last)
9371 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009372 alloc_fail_countdown = argvars[1].vval.v_number;
9373 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009374 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009375 }
9376}
9377
9378/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009379 * "and(expr, expr)" function
9380 */
9381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009382f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009383{
9384 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9385 & get_tv_number_chk(&argvars[1], NULL);
9386}
9387
9388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009389 * "append(lnum, string/list)" function
9390 */
9391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009393{
9394 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009395 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 list_T *l = NULL;
9397 listitem_T *li = NULL;
9398 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009399 long added = 0;
9400
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009401 /* When coming here from Insert mode, sync undo, so that this can be
9402 * undone separately from what was previously inserted. */
9403 if (u_sync_once == 2)
9404 {
9405 u_sync_once = 1; /* notify that u_sync() was called */
9406 u_sync(TRUE);
9407 }
9408
Bram Moolenaar0d660222005-01-07 21:51:51 +00009409 lnum = get_tv_lnum(argvars);
9410 if (lnum >= 0
9411 && lnum <= curbuf->b_ml.ml_line_count
9412 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009413 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009414 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009415 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009416 l = argvars[1].vval.v_list;
9417 if (l == NULL)
9418 return;
9419 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009420 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009421 for (;;)
9422 {
9423 if (l == NULL)
9424 tv = &argvars[1]; /* append a string */
9425 else if (li == NULL)
9426 break; /* end of list */
9427 else
9428 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 line = get_tv_string_chk(tv);
9430 if (line == NULL) /* type error */
9431 {
9432 rettv->vval.v_number = 1; /* Failed */
9433 break;
9434 }
9435 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009436 ++added;
9437 if (l == NULL)
9438 break;
9439 li = li->li_next;
9440 }
9441
9442 appended_lines_mark(lnum, added);
9443 if (curwin->w_cursor.lnum > lnum)
9444 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009446 else
9447 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448}
9449
9450/*
9451 * "argc()" function
9452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009454f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457}
9458
9459/*
9460 * "argidx()" function
9461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009463f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466}
9467
9468/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009469 * "arglistid()" function
9470 */
9471 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009472f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009473{
9474 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009475
9476 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009477 wp = find_tabwin(&argvars[0], &argvars[1]);
9478 if (wp != NULL)
9479 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009480}
9481
9482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 * "argv(nr)" function
9484 */
9485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009486f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487{
9488 int idx;
9489
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009490 if (argvars[0].v_type != VAR_UNKNOWN)
9491 {
9492 idx = get_tv_number_chk(&argvars[0], NULL);
9493 if (idx >= 0 && idx < ARGCOUNT)
9494 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9495 else
9496 rettv->vval.v_string = NULL;
9497 rettv->v_type = VAR_STRING;
9498 }
9499 else if (rettv_list_alloc(rettv) == OK)
9500 for (idx = 0; idx < ARGCOUNT; ++idx)
9501 list_append_string(rettv->vval.v_list,
9502 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503}
9504
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009505typedef enum
9506{
9507 ASSERT_EQUAL,
9508 ASSERT_NOTEQUAL,
9509 ASSERT_MATCH,
9510 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009511 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009512} assert_type_T;
9513
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009514static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009515static 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 +01009516static void assert_error(garray_T *gap);
9517static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009518
9519/*
9520 * Prepare "gap" for an assert error and add the sourcing position.
9521 */
9522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009523prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009524{
9525 char buf[NUMBUFLEN];
9526
9527 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009528 if (sourcing_name != NULL)
9529 {
9530 ga_concat(gap, sourcing_name);
9531 if (sourcing_lnum > 0)
9532 ga_concat(gap, (char_u *)" ");
9533 }
9534 if (sourcing_lnum > 0)
9535 {
9536 sprintf(buf, "line %ld", (long)sourcing_lnum);
9537 ga_concat(gap, (char_u *)buf);
9538 }
9539 if (sourcing_name != NULL || sourcing_lnum > 0)
9540 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009541}
9542
9543/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009544 * Append "str" to "gap", escaping unprintable characters.
9545 * Changes NL to \n, CR to \r, etc.
9546 */
9547 static void
9548ga_concat_esc(garray_T *gap, char_u *str)
9549{
9550 char_u *p;
9551 char_u buf[NUMBUFLEN];
9552
Bram Moolenaarf1551962016-03-15 12:55:58 +01009553 if (str == NULL)
9554 {
9555 ga_concat(gap, (char_u *)"NULL");
9556 return;
9557 }
9558
Bram Moolenaar23689172016-02-15 22:37:37 +01009559 for (p = str; *p != NUL; ++p)
9560 switch (*p)
9561 {
9562 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9563 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9564 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9565 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9566 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9567 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9568 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9569 default:
9570 if (*p < ' ')
9571 {
9572 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9573 ga_concat(gap, buf);
9574 }
9575 else
9576 ga_append(gap, *p);
9577 break;
9578 }
9579}
9580
9581/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009582 * Fill "gap" with information about an assert error.
9583 */
9584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009585fill_assert_error(
9586 garray_T *gap,
9587 typval_T *opt_msg_tv,
9588 char_u *exp_str,
9589 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009590 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009591 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009592{
9593 char_u numbuf[NUMBUFLEN];
9594 char_u *tofree;
9595
9596 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9597 {
9598 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9599 vim_free(tofree);
9600 }
9601 else
9602 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009603 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009604 ga_concat(gap, (char_u *)"Pattern ");
9605 else
9606 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009607 if (exp_str == NULL)
9608 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009609 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009610 vim_free(tofree);
9611 }
9612 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009613 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009614 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009615 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009616 else if (atype == ASSERT_NOTMATCH)
9617 ga_concat(gap, (char_u *)" does match ");
9618 else if (atype == ASSERT_NOTEQUAL)
9619 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009620 else
9621 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009622 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009623 vim_free(tofree);
9624 }
9625}
Bram Moolenaar43345542015-11-29 17:35:35 +01009626
9627/*
9628 * Add an assert error to v:errors.
9629 */
9630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009631assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009632{
9633 struct vimvar *vp = &vimvars[VV_ERRORS];
9634
9635 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9636 /* Make sure v:errors is a list. */
9637 set_vim_var_list(VV_ERRORS, list_alloc());
9638 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9639}
9640
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009641 static void
9642assert_equal_common(typval_T *argvars, assert_type_T atype)
9643{
9644 garray_T ga;
9645
9646 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9647 != (atype == ASSERT_EQUAL))
9648 {
9649 prepare_assert_error(&ga);
9650 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9651 atype);
9652 assert_error(&ga);
9653 ga_clear(&ga);
9654 }
9655}
9656
Bram Moolenaar43345542015-11-29 17:35:35 +01009657/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009658 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009659 */
9660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009661f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009662{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009663 assert_equal_common(argvars, ASSERT_EQUAL);
9664}
Bram Moolenaar43345542015-11-29 17:35:35 +01009665
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009666/*
9667 * "assert_notequal(expected, actual[, msg])" function
9668 */
9669 static void
9670f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9671{
9672 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009673}
9674
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009675/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009676 * "assert_exception(string[, msg])" function
9677 */
9678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009679f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009680{
9681 garray_T ga;
9682 char *error;
9683
9684 error = (char *)get_tv_string_chk(&argvars[0]);
9685 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9686 {
9687 prepare_assert_error(&ga);
9688 ga_concat(&ga, (char_u *)"v:exception is not set");
9689 assert_error(&ga);
9690 ga_clear(&ga);
9691 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009692 else if (error != NULL
9693 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009694 {
9695 prepare_assert_error(&ga);
9696 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009697 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009698 assert_error(&ga);
9699 ga_clear(&ga);
9700 }
9701}
9702
9703/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009704 * "assert_fails(cmd [, error])" function
9705 */
9706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009707f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009708{
9709 char_u *cmd = get_tv_string_chk(&argvars[0]);
9710 garray_T ga;
9711
9712 called_emsg = FALSE;
9713 suppress_errthrow = TRUE;
9714 emsg_silent = TRUE;
9715 do_cmdline_cmd(cmd);
9716 if (!called_emsg)
9717 {
9718 prepare_assert_error(&ga);
9719 ga_concat(&ga, (char_u *)"command did not fail: ");
9720 ga_concat(&ga, cmd);
9721 assert_error(&ga);
9722 ga_clear(&ga);
9723 }
9724 else if (argvars[1].v_type != VAR_UNKNOWN)
9725 {
9726 char_u buf[NUMBUFLEN];
9727 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9728
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009729 if (error == NULL
9730 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009731 {
9732 prepare_assert_error(&ga);
9733 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009734 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009735 assert_error(&ga);
9736 ga_clear(&ga);
9737 }
9738 }
9739
9740 called_emsg = FALSE;
9741 suppress_errthrow = FALSE;
9742 emsg_silent = FALSE;
9743 emsg_on_display = FALSE;
9744 set_vim_var_string(VV_ERRMSG, NULL, 0);
9745}
9746
9747/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009748 * Common for assert_true() and assert_false().
9749 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009751assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009752{
9753 int error = FALSE;
9754 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009755
Bram Moolenaar37127922016-02-06 20:29:28 +01009756 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009757 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009758 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009759 if (argvars[0].v_type != VAR_NUMBER
9760 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9761 || error)
9762 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009763 prepare_assert_error(&ga);
9764 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009765 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009766 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009767 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009768 ga_clear(&ga);
9769 }
9770}
9771
9772/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009773 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009774 */
9775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009776f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009777{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009778 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009779}
9780
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009781 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009782assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009783{
9784 garray_T ga;
9785 char_u buf1[NUMBUFLEN];
9786 char_u buf2[NUMBUFLEN];
9787 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9788 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9789
Bram Moolenaar72188e92016-03-28 22:48:29 +02009790 if (pat == NULL || text == NULL)
9791 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009792 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009793 {
9794 prepare_assert_error(&ga);
9795 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009796 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009797 assert_error(&ga);
9798 ga_clear(&ga);
9799 }
9800}
9801
9802/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009803 * "assert_match(pattern, actual[, msg])" function
9804 */
9805 static void
9806f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9807{
9808 assert_match_common(argvars, ASSERT_MATCH);
9809}
9810
9811/*
9812 * "assert_notmatch(pattern, actual[, msg])" function
9813 */
9814 static void
9815f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9816{
9817 assert_match_common(argvars, ASSERT_NOTMATCH);
9818}
9819
9820/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009821 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009822 */
9823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009824f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009825{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009826 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009827}
9828
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009829#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009830/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009831 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009832 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009834f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009835{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009836 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009837
9838 rettv->v_type = VAR_FLOAT;
9839 if (get_float_arg(argvars, &f) == OK)
9840 rettv->vval.v_float = asin(f);
9841 else
9842 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009843}
9844
9845/*
9846 * "atan()" function
9847 */
9848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009849f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009850{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009851 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009852
9853 rettv->v_type = VAR_FLOAT;
9854 if (get_float_arg(argvars, &f) == OK)
9855 rettv->vval.v_float = atan(f);
9856 else
9857 rettv->vval.v_float = 0.0;
9858}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009859
9860/*
9861 * "atan2()" function
9862 */
9863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009864f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009865{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009866 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009867
9868 rettv->v_type = VAR_FLOAT;
9869 if (get_float_arg(argvars, &fx) == OK
9870 && get_float_arg(&argvars[1], &fy) == OK)
9871 rettv->vval.v_float = atan2(fx, fy);
9872 else
9873 rettv->vval.v_float = 0.0;
9874}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009875#endif
9876
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877/*
9878 * "browse(save, title, initdir, default)" function
9879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009881f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882{
9883#ifdef FEAT_BROWSE
9884 int save;
9885 char_u *title;
9886 char_u *initdir;
9887 char_u *defname;
9888 char_u buf[NUMBUFLEN];
9889 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009890 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009892 save = get_tv_number_chk(&argvars[0], &error);
9893 title = get_tv_string_chk(&argvars[1]);
9894 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9895 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009897 if (error || title == NULL || initdir == NULL || defname == NULL)
9898 rettv->vval.v_string = NULL;
9899 else
9900 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009901 do_browse(save ? BROWSE_SAVE : 0,
9902 title, defname, NULL, initdir, NULL, curbuf);
9903#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009904 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009905#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009906 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009907}
9908
9909/*
9910 * "browsedir(title, initdir)" function
9911 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009913f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009914{
9915#ifdef FEAT_BROWSE
9916 char_u *title;
9917 char_u *initdir;
9918 char_u buf[NUMBUFLEN];
9919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009920 title = get_tv_string_chk(&argvars[0]);
9921 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 if (title == NULL || initdir == NULL)
9924 rettv->vval.v_string = NULL;
9925 else
9926 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009927 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932}
9933
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009934static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009935
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936/*
9937 * Find a buffer by number or exact name.
9938 */
9939 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009940find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941{
9942 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009944 if (avar->v_type == VAR_NUMBER)
9945 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009946 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009948 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009949 if (buf == NULL)
9950 {
9951 /* No full path name match, try a match with a URL or a "nofile"
9952 * buffer, these don't use the full path. */
9953 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9954 if (buf->b_fname != NULL
9955 && (path_with_url(buf->b_fname)
9956#ifdef FEAT_QUICKFIX
9957 || bt_nofile(buf)
9958#endif
9959 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009960 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009961 break;
9962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 }
9964 return buf;
9965}
9966
9967/*
9968 * "bufexists(expr)" function
9969 */
9970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009971f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974}
9975
9976/*
9977 * "buflisted(expr)" function
9978 */
9979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009980f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981{
9982 buf_T *buf;
9983
9984 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986}
9987
9988/*
9989 * "bufloaded(expr)" function
9990 */
9991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009992f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994 buf_T *buf;
9995
9996 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998}
9999
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010000 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010001buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 int save_magic;
10004 char_u *save_cpo;
10005 buf_T *buf;
10006
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10008 save_magic = p_magic;
10009 p_magic = TRUE;
10010 save_cpo = p_cpo;
10011 p_cpo = (char_u *)"";
10012
10013 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010014 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015
10016 p_magic = save_magic;
10017 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010018 return buf;
10019}
10020
10021/*
10022 * Get buffer by number or pattern.
10023 */
10024 static buf_T *
10025get_buf_tv(typval_T *tv, int curtab_only)
10026{
10027 char_u *name = tv->vval.v_string;
10028 buf_T *buf;
10029
10030 if (tv->v_type == VAR_NUMBER)
10031 return buflist_findnr((int)tv->vval.v_number);
10032 if (tv->v_type != VAR_STRING)
10033 return NULL;
10034 if (name == NULL || *name == NUL)
10035 return curbuf;
10036 if (name[0] == '$' && name[1] == NUL)
10037 return lastbuf;
10038
10039 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040
10041 /* If not found, try expanding the name, like done for bufexists(). */
10042 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044
10045 return buf;
10046}
10047
10048/*
10049 * "bufname(expr)" function
10050 */
10051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010052f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 buf_T *buf;
10055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010056 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010058 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 --emsg_off;
10065}
10066
10067/*
10068 * "bufnr(expr)" function
10069 */
10070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010071f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072{
10073 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010074 int error = FALSE;
10075 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010077 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010079 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010080 --emsg_off;
10081
10082 /* If the buffer isn't found and the second argument is not zero create a
10083 * new buffer. */
10084 if (buf == NULL
10085 && argvars[1].v_type != VAR_UNKNOWN
10086 && get_tv_number_chk(&argvars[1], &error) != 0
10087 && !error
10088 && (name = get_tv_string_chk(&argvars[0])) != NULL
10089 && !error)
10090 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10091
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096}
10097
10098/*
10099 * "bufwinnr(nr)" function
10100 */
10101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010102f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103{
10104#ifdef FEAT_WINDOWS
10105 win_T *wp;
10106 int winnr = 0;
10107#endif
10108 buf_T *buf;
10109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010112 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113#ifdef FEAT_WINDOWS
10114 for (wp = firstwin; wp; wp = wp->w_next)
10115 {
10116 ++winnr;
10117 if (wp->w_buffer == buf)
10118 break;
10119 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123#endif
10124 --emsg_off;
10125}
10126
10127/*
10128 * "byte2line(byte)" function
10129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010131f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132{
10133#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135#else
10136 long boff = 0;
10137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 (linenr_T)0, &boff);
10144#endif
10145}
10146
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010148byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010149{
10150#ifdef FEAT_MBYTE
10151 char_u *t;
10152#endif
10153 char_u *str;
10154 long idx;
10155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010156 str = get_tv_string_chk(&argvars[0]);
10157 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010160 return;
10161
10162#ifdef FEAT_MBYTE
10163 t = str;
10164 for ( ; idx > 0; idx--)
10165 {
10166 if (*t == NUL) /* EOL reached */
10167 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010168 if (enc_utf8 && comp)
10169 t += utf_ptr2len(t);
10170 else
10171 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010172 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010173 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010174#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010175 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010176 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010177#endif
10178}
10179
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010180/*
10181 * "byteidx()" function
10182 */
10183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010184f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010185{
10186 byteidx(argvars, rettv, FALSE);
10187}
10188
10189/*
10190 * "byteidxcomp()" function
10191 */
10192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010193f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010194{
10195 byteidx(argvars, rettv, TRUE);
10196}
10197
Bram Moolenaardb913952012-06-29 12:54:53 +020010198 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010199func_call(
10200 char_u *name,
10201 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010202 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010203 dict_T *selfdict,
10204 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010205{
10206 listitem_T *item;
10207 typval_T argv[MAX_FUNC_ARGS + 1];
10208 int argc = 0;
10209 int dummy;
10210 int r = 0;
10211
10212 for (item = args->vval.v_list->lv_first; item != NULL;
10213 item = item->li_next)
10214 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010215 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010216 {
10217 EMSG(_("E699: Too many arguments"));
10218 break;
10219 }
10220 /* Make a copy of each argument. This is needed to be able to set
10221 * v_lock to VAR_FIXED in the copy without changing the original list.
10222 */
10223 copy_tv(&item->li_tv, &argv[argc++]);
10224 }
10225
10226 if (item == NULL)
10227 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10228 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010229 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010230
10231 /* Free the arguments. */
10232 while (argc > 0)
10233 clear_tv(&argv[--argc]);
10234
10235 return r;
10236}
10237
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010238/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010239 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010240 */
10241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010242f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010243{
10244 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010245 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010246 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010247
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010248 if (argvars[1].v_type != VAR_LIST)
10249 {
10250 EMSG(_(e_listreq));
10251 return;
10252 }
10253 if (argvars[1].vval.v_list == NULL)
10254 return;
10255
10256 if (argvars[0].v_type == VAR_FUNC)
10257 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010258 else if (argvars[0].v_type == VAR_PARTIAL)
10259 {
10260 partial = argvars[0].vval.v_partial;
10261 func = partial->pt_name;
10262 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010263 else
10264 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010265 if (*func == NUL)
10266 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010267
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268 if (argvars[2].v_type != VAR_UNKNOWN)
10269 {
10270 if (argvars[2].v_type != VAR_DICT)
10271 {
10272 EMSG(_(e_dictreq));
10273 return;
10274 }
10275 selfdict = argvars[2].vval.v_dict;
10276 }
10277
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010278 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010279}
10280
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010281#ifdef FEAT_FLOAT
10282/*
10283 * "ceil({float})" function
10284 */
10285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010286f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010287{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010288 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010289
10290 rettv->v_type = VAR_FLOAT;
10291 if (get_float_arg(argvars, &f) == OK)
10292 rettv->vval.v_float = ceil(f);
10293 else
10294 rettv->vval.v_float = 0.0;
10295}
10296#endif
10297
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010298#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010299/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010300 * "ch_close()" function
10301 */
10302 static void
10303f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10304{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010305 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010306
10307 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010308 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010309 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010310 channel_clear(channel);
10311 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010312}
10313
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010314/*
10315 * "ch_getbufnr()" function
10316 */
10317 static void
10318f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10319{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010320 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010321
10322 rettv->vval.v_number = -1;
10323 if (channel != NULL)
10324 {
10325 char_u *what = get_tv_string(&argvars[1]);
10326 int part;
10327
10328 if (STRCMP(what, "err") == 0)
10329 part = PART_ERR;
10330 else if (STRCMP(what, "out") == 0)
10331 part = PART_OUT;
10332 else if (STRCMP(what, "in") == 0)
10333 part = PART_IN;
10334 else
10335 part = PART_SOCK;
10336 if (channel->ch_part[part].ch_buffer != NULL)
10337 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10338 }
10339}
10340
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010341/*
10342 * "ch_getjob()" function
10343 */
10344 static void
10345f_ch_getjob(typval_T *argvars, typval_T *rettv)
10346{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010347 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010348
10349 if (channel != NULL)
10350 {
10351 rettv->v_type = VAR_JOB;
10352 rettv->vval.v_job = channel->ch_job;
10353 if (channel->ch_job != NULL)
10354 ++channel->ch_job->jv_refcount;
10355 }
10356}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010357
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010358/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010359 * "ch_info()" function
10360 */
10361 static void
10362f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10363{
10364 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10365
10366 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10367 channel_info(channel, rettv->vval.v_dict);
10368}
10369
10370/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010371 * "ch_log()" function
10372 */
10373 static void
10374f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10375{
10376 char_u *msg = get_tv_string(&argvars[0]);
10377 channel_T *channel = NULL;
10378
10379 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010380 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010381
10382 ch_log(channel, (char *)msg);
10383}
10384
10385/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010386 * "ch_logfile()" function
10387 */
10388 static void
10389f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10390{
10391 char_u *fname;
10392 char_u *opt = (char_u *)"";
10393 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010394
10395 fname = get_tv_string(&argvars[0]);
10396 if (argvars[1].v_type == VAR_STRING)
10397 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010398 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010399}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010400
10401/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010402 * "ch_open()" function
10403 */
10404 static void
10405f_ch_open(typval_T *argvars, typval_T *rettv)
10406{
Bram Moolenaar77073442016-02-13 23:23:53 +010010407 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010408 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010409}
10410
10411/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010412 * "ch_read()" function
10413 */
10414 static void
10415f_ch_read(typval_T *argvars, typval_T *rettv)
10416{
10417 common_channel_read(argvars, rettv, FALSE);
10418}
10419
10420/*
10421 * "ch_readraw()" function
10422 */
10423 static void
10424f_ch_readraw(typval_T *argvars, typval_T *rettv)
10425{
10426 common_channel_read(argvars, rettv, TRUE);
10427}
10428
10429/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010430 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010431 */
10432 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010433f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10434{
10435 ch_expr_common(argvars, rettv, TRUE);
10436}
10437
10438/*
10439 * "ch_sendexpr()" function
10440 */
10441 static void
10442f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10443{
10444 ch_expr_common(argvars, rettv, FALSE);
10445}
10446
10447/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010448 * "ch_evalraw()" function
10449 */
10450 static void
10451f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10452{
10453 ch_raw_common(argvars, rettv, TRUE);
10454}
10455
10456/*
10457 * "ch_sendraw()" function
10458 */
10459 static void
10460f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10461{
10462 ch_raw_common(argvars, rettv, FALSE);
10463}
10464
10465/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010466 * "ch_setoptions()" function
10467 */
10468 static void
10469f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10470{
10471 channel_T *channel;
10472 jobopt_T opt;
10473
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010474 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010475 if (channel == NULL)
10476 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010477 clear_job_options(&opt);
10478 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010479 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10480 channel_set_options(channel, &opt);
10481 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010482}
10483
10484/*
10485 * "ch_status()" function
10486 */
10487 static void
10488f_ch_status(typval_T *argvars, typval_T *rettv)
10489{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010490 channel_T *channel;
10491
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010492 /* return an empty string by default */
10493 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010494 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010495
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010496 channel = get_channel_arg(&argvars[0], FALSE);
10497 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010498}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010499#endif
10500
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010501/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010502 * "changenr()" function
10503 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010505f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010506{
10507 rettv->vval.v_number = curbuf->b_u_seq_cur;
10508}
10509
10510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 * "char2nr(string)" function
10512 */
10513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010514f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515{
10516#ifdef FEAT_MBYTE
10517 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010518 {
10519 int utf8 = 0;
10520
10521 if (argvars[1].v_type != VAR_UNKNOWN)
10522 utf8 = get_tv_number_chk(&argvars[1], NULL);
10523
10524 if (utf8)
10525 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10526 else
10527 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 else
10530#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532}
10533
10534/*
10535 * "cindent(lnum)" function
10536 */
10537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010538f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
10540#ifdef FEAT_CINDENT
10541 pos_T pos;
10542 linenr_T lnum;
10543
10544 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10547 {
10548 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 curwin->w_cursor = pos;
10551 }
10552 else
10553#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555}
10556
10557/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010558 * "clearmatches()" function
10559 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010561f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010562{
10563#ifdef FEAT_SEARCH_EXTRA
10564 clear_matches(curwin);
10565#endif
10566}
10567
10568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 * "col(string)" function
10570 */
10571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010572f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573{
10574 colnr_T col = 0;
10575 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010576 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010578 fp = var2fpos(&argvars[0], FALSE, &fnum);
10579 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 {
10581 if (fp->col == MAXCOL)
10582 {
10583 /* '> can be MAXCOL, get the length of the line then */
10584 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010585 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 else
10587 col = MAXCOL;
10588 }
10589 else
10590 {
10591 col = fp->col + 1;
10592#ifdef FEAT_VIRTUALEDIT
10593 /* col(".") when the cursor is on the NUL at the end of the line
10594 * because of "coladd" can be seen as an extra column. */
10595 if (virtual_active() && fp == &curwin->w_cursor)
10596 {
10597 char_u *p = ml_get_cursor();
10598
10599 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10600 curwin->w_virtcol - curwin->w_cursor.coladd))
10601 {
10602# ifdef FEAT_MBYTE
10603 int l;
10604
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010605 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 col += l;
10607# else
10608 if (*p != NUL && p[1] == NUL)
10609 ++col;
10610# endif
10611 }
10612 }
10613#endif
10614 }
10615 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010616 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617}
10618
Bram Moolenaar572cb562005-08-05 21:35:02 +000010619#if defined(FEAT_INS_EXPAND)
10620/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010621 * "complete()" function
10622 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010624f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010625{
10626 int startcol;
10627
10628 if ((State & INSERT) == 0)
10629 {
10630 EMSG(_("E785: complete() can only be used in Insert mode"));
10631 return;
10632 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010633
10634 /* Check for undo allowed here, because if something was already inserted
10635 * the line was already saved for undo and this check isn't done. */
10636 if (!undo_allowed())
10637 return;
10638
Bram Moolenaarade00832006-03-10 21:46:58 +000010639 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10640 {
10641 EMSG(_(e_invarg));
10642 return;
10643 }
10644
10645 startcol = get_tv_number_chk(&argvars[0], NULL);
10646 if (startcol <= 0)
10647 return;
10648
10649 set_completion(startcol - 1, argvars[1].vval.v_list);
10650}
10651
10652/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010653 * "complete_add()" function
10654 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010656f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010657{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010658 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010659}
10660
10661/*
10662 * "complete_check()" function
10663 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010665f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010666{
10667 int saved = RedrawingDisabled;
10668
10669 RedrawingDisabled = 0;
10670 ins_compl_check_keys(0);
10671 rettv->vval.v_number = compl_interrupted;
10672 RedrawingDisabled = saved;
10673}
10674#endif
10675
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676/*
10677 * "confirm(message, buttons[, default [, type]])" function
10678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010680f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681{
10682#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10683 char_u *message;
10684 char_u *buttons = NULL;
10685 char_u buf[NUMBUFLEN];
10686 char_u buf2[NUMBUFLEN];
10687 int def = 1;
10688 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 char_u *typestr;
10690 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 message = get_tv_string_chk(&argvars[0]);
10693 if (message == NULL)
10694 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010695 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010697 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10698 if (buttons == NULL)
10699 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010700 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010702 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010703 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10706 if (typestr == NULL)
10707 error = TRUE;
10708 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 switch (TOUPPER_ASC(*typestr))
10711 {
10712 case 'E': type = VIM_ERROR; break;
10713 case 'Q': type = VIM_QUESTION; break;
10714 case 'I': type = VIM_INFO; break;
10715 case 'W': type = VIM_WARNING; break;
10716 case 'G': type = VIM_GENERIC; break;
10717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 }
10719 }
10720 }
10721 }
10722
10723 if (buttons == NULL || *buttons == NUL)
10724 buttons = (char_u *)_("&Ok");
10725
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010726 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010727 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010728 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729#endif
10730}
10731
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010732/*
10733 * "copy()" function
10734 */
10735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010736f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010737{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010738 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010739}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010741#ifdef FEAT_FLOAT
10742/*
10743 * "cos()" function
10744 */
10745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010746f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010747{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010748 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010749
10750 rettv->v_type = VAR_FLOAT;
10751 if (get_float_arg(argvars, &f) == OK)
10752 rettv->vval.v_float = cos(f);
10753 else
10754 rettv->vval.v_float = 0.0;
10755}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010756
10757/*
10758 * "cosh()" function
10759 */
10760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010761f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010762{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010763 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010764
10765 rettv->v_type = VAR_FLOAT;
10766 if (get_float_arg(argvars, &f) == OK)
10767 rettv->vval.v_float = cosh(f);
10768 else
10769 rettv->vval.v_float = 0.0;
10770}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010771#endif
10772
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010774 * "count()" function
10775 */
10776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010777f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010778{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010779 long n = 0;
10780 int ic = FALSE;
10781
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010783 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 listitem_T *li;
10785 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010786 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010787
Bram Moolenaare9a41262005-01-15 22:18:47 +000010788 if ((l = argvars[0].vval.v_list) != NULL)
10789 {
10790 li = l->lv_first;
10791 if (argvars[2].v_type != VAR_UNKNOWN)
10792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010793 int error = FALSE;
10794
10795 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010796 if (argvars[3].v_type != VAR_UNKNOWN)
10797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 idx = get_tv_number_chk(&argvars[3], &error);
10799 if (!error)
10800 {
10801 li = list_find(l, idx);
10802 if (li == NULL)
10803 EMSGN(_(e_listidx), idx);
10804 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010805 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 if (error)
10807 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010808 }
10809
10810 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010811 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010812 ++n;
10813 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010814 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010815 else if (argvars[0].v_type == VAR_DICT)
10816 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010817 int todo;
10818 dict_T *d;
10819 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010821 if ((d = argvars[0].vval.v_dict) != NULL)
10822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010823 int error = FALSE;
10824
Bram Moolenaare9a41262005-01-15 22:18:47 +000010825 if (argvars[2].v_type != VAR_UNKNOWN)
10826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010827 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010828 if (argvars[3].v_type != VAR_UNKNOWN)
10829 EMSG(_(e_invarg));
10830 }
10831
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010832 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010834 {
10835 if (!HASHITEM_EMPTY(hi))
10836 {
10837 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010838 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010839 ++n;
10840 }
10841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 }
10843 }
10844 else
10845 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010846 rettv->vval.v_number = n;
10847}
10848
10849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10851 *
10852 * Checks the existence of a cscope connection.
10853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010855f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856{
10857#ifdef FEAT_CSCOPE
10858 int num = 0;
10859 char_u *dbpath = NULL;
10860 char_u *prepend = NULL;
10861 char_u buf[NUMBUFLEN];
10862
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010863 if (argvars[0].v_type != VAR_UNKNOWN
10864 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866 num = (int)get_tv_number(&argvars[0]);
10867 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010868 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 }
10871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873#endif
10874}
10875
10876/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010877 * "cursor(lnum, col)" function, or
10878 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010880 * Moves the cursor to the specified line and column.
10881 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010884f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885{
10886 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010887#ifdef FEAT_VIRTUALEDIT
10888 long coladd = 0;
10889#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010890 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010892 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010893 if (argvars[1].v_type == VAR_UNKNOWN)
10894 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010895 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010896 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010897
Bram Moolenaar493c1782014-05-28 14:34:46 +020010898 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010899 {
10900 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010901 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010902 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010903 line = pos.lnum;
10904 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010905#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010906 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010907#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010908 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010909 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010910 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010911 set_curswant = FALSE;
10912 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010913 }
10914 else
10915 {
10916 line = get_tv_lnum(argvars);
10917 col = get_tv_number_chk(&argvars[1], NULL);
10918#ifdef FEAT_VIRTUALEDIT
10919 if (argvars[2].v_type != VAR_UNKNOWN)
10920 coladd = get_tv_number_chk(&argvars[2], NULL);
10921#endif
10922 }
10923 if (line < 0 || col < 0
10924#ifdef FEAT_VIRTUALEDIT
10925 || coladd < 0
10926#endif
10927 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010928 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 if (line > 0)
10930 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 if (col > 0)
10932 curwin->w_cursor.col = col - 1;
10933#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010934 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935#endif
10936
10937 /* Make sure the cursor is in a valid position. */
10938 check_cursor();
10939#ifdef FEAT_MBYTE
10940 /* Correct cursor for multi-byte character. */
10941 if (has_mbyte)
10942 mb_adjust_cursor();
10943#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010944
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010945 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010946 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947}
10948
10949/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010950 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 */
10952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010953f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010955 int noref = 0;
10956
10957 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010959 if (noref < 0 || noref > 1)
10960 EMSG(_(e_invarg));
10961 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010962 {
10963 current_copyID += COPYID_INC;
10964 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966}
10967
10968/*
10969 * "delete()" function
10970 */
10971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010972f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010974 char_u nbuf[NUMBUFLEN];
10975 char_u *name;
10976 char_u *flags;
10977
10978 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010980 return;
10981
10982 name = get_tv_string(&argvars[0]);
10983 if (name == NULL || *name == NUL)
10984 {
10985 EMSG(_(e_invarg));
10986 return;
10987 }
10988
10989 if (argvars[1].v_type != VAR_UNKNOWN)
10990 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010992 flags = (char_u *)"";
10993
10994 if (*flags == NUL)
10995 /* delete a file */
10996 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10997 else if (STRCMP(flags, "d") == 0)
10998 /* delete an empty directory */
10999 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11000 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011001 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011002 rettv->vval.v_number = delete_recursive(name);
11003 else
11004 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005}
11006
11007/*
11008 * "did_filetype()" function
11009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011011f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012{
11013#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015#endif
11016}
11017
11018/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011019 * "diff_filler()" function
11020 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011022f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011023{
11024#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011026#endif
11027}
11028
11029/*
11030 * "diff_hlID()" function
11031 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011033f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011034{
11035#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011037 static linenr_T prev_lnum = 0;
11038 static int changedtick = 0;
11039 static int fnum = 0;
11040 static int change_start = 0;
11041 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011042 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011043 int filler_lines;
11044 int col;
11045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011046 if (lnum < 0) /* ignore type error in {lnum} arg */
11047 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011048 if (lnum != prev_lnum
11049 || changedtick != curbuf->b_changedtick
11050 || fnum != curbuf->b_fnum)
11051 {
11052 /* New line, buffer, change: need to get the values. */
11053 filler_lines = diff_check(curwin, lnum);
11054 if (filler_lines < 0)
11055 {
11056 if (filler_lines == -1)
11057 {
11058 change_start = MAXCOL;
11059 change_end = -1;
11060 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11061 hlID = HLF_ADD; /* added line */
11062 else
11063 hlID = HLF_CHD; /* changed line */
11064 }
11065 else
11066 hlID = HLF_ADD; /* added line */
11067 }
11068 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011069 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011070 prev_lnum = lnum;
11071 changedtick = curbuf->b_changedtick;
11072 fnum = curbuf->b_fnum;
11073 }
11074
11075 if (hlID == HLF_CHD || hlID == HLF_TXD)
11076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011078 if (col >= change_start && col <= change_end)
11079 hlID = HLF_TXD; /* changed text */
11080 else
11081 hlID = HLF_CHD; /* changed line */
11082 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011083 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011084#endif
11085}
11086
11087/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011088 * "disable_char_avail_for_testing({expr})" function
11089 */
11090 static void
11091f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11092{
11093 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11094}
11095
11096/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011097 * "empty({expr})" function
11098 */
11099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011100f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011101{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011102 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011103
11104 switch (argvars[0].v_type)
11105 {
11106 case VAR_STRING:
11107 case VAR_FUNC:
11108 n = argvars[0].vval.v_string == NULL
11109 || *argvars[0].vval.v_string == NUL;
11110 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011111 case VAR_PARTIAL:
11112 n = FALSE;
11113 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011114 case VAR_NUMBER:
11115 n = argvars[0].vval.v_number == 0;
11116 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011117 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011118#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011119 n = argvars[0].vval.v_float == 0.0;
11120 break;
11121#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011122 case VAR_LIST:
11123 n = argvars[0].vval.v_list == NULL
11124 || argvars[0].vval.v_list->lv_first == NULL;
11125 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 case VAR_DICT:
11127 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011130 case VAR_SPECIAL:
11131 n = argvars[0].vval.v_number != VVAL_TRUE;
11132 break;
11133
Bram Moolenaar835dc632016-02-07 14:27:38 +010011134 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011135#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011136 n = argvars[0].vval.v_job == NULL
11137 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11138 break;
11139#endif
11140 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011141#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011142 n = argvars[0].vval.v_channel == NULL
11143 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011144 break;
11145#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011146 case VAR_UNKNOWN:
11147 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11148 n = TRUE;
11149 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011150 }
11151
11152 rettv->vval.v_number = n;
11153}
11154
11155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 * "escape({string}, {chars})" function
11157 */
11158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011159f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160{
11161 char_u buf[NUMBUFLEN];
11162
Bram Moolenaar758711c2005-02-02 23:11:38 +000011163 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11164 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166}
11167
11168/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011169 * "eval()" function
11170 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011172f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011173{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011174 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011176 s = get_tv_string_chk(&argvars[0]);
11177 if (s != NULL)
11178 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011179
Bram Moolenaar615b9972015-01-14 17:15:05 +010011180 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011181 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11182 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011183 if (p != NULL && !aborting())
11184 EMSG2(_(e_invexpr2), p);
11185 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011187 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011188 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011189 else if (*s != NUL)
11190 EMSG(_(e_trailing));
11191}
11192
11193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 * "eventhandler()" function
11195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011197f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011199 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200}
11201
11202/*
11203 * "executable()" function
11204 */
11205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011206f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011208 char_u *name = get_tv_string(&argvars[0]);
11209
11210 /* Check in $PATH and also check directly if there is a directory name. */
11211 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11212 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011213}
11214
11215/*
11216 * "exepath()" function
11217 */
11218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011219f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011220{
11221 char_u *p = NULL;
11222
Bram Moolenaarb5971142015-03-21 17:32:19 +010011223 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011224 rettv->v_type = VAR_STRING;
11225 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226}
11227
11228/*
11229 * "exists()" function
11230 */
11231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011232f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233{
11234 char_u *p;
11235 char_u *name;
11236 int n = FALSE;
11237 int len = 0;
11238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 if (*p == '$') /* environment variable */
11241 {
11242 /* first try "normal" environment variables (fast) */
11243 if (mch_getenv(p + 1) != NULL)
11244 n = TRUE;
11245 else
11246 {
11247 /* try expanding things like $VIM and ${HOME} */
11248 p = expand_env_save(p);
11249 if (p != NULL && *p != '$')
11250 n = TRUE;
11251 vim_free(p);
11252 }
11253 }
11254 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011257 if (*skipwhite(p) != NUL)
11258 n = FALSE; /* trailing garbage */
11259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 else if (*p == '*') /* internal or user defined function */
11261 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011262 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 }
11264 else if (*p == ':')
11265 {
11266 n = cmd_exists(p + 1);
11267 }
11268 else if (*p == '#')
11269 {
11270#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011271 if (p[1] == '#')
11272 n = autocmd_supported(p + 2);
11273 else
11274 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275#endif
11276 }
11277 else /* internal variable */
11278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011279 char_u *tofree;
11280 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011282 /* get_name_len() takes care of expanding curly braces */
11283 name = p;
11284 len = get_name_len(&p, &tofree, TRUE, FALSE);
11285 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011287 if (tofree != NULL)
11288 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011289 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011290 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011292 /* handle d.key, l[idx], f(expr) */
11293 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11294 if (n)
11295 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 }
11297 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011298 if (*p != NUL)
11299 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011301 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 }
11303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011304 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305}
11306
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011307#ifdef FEAT_FLOAT
11308/*
11309 * "exp()" function
11310 */
11311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011312f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011313{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011314 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011315
11316 rettv->v_type = VAR_FLOAT;
11317 if (get_float_arg(argvars, &f) == OK)
11318 rettv->vval.v_float = exp(f);
11319 else
11320 rettv->vval.v_float = 0.0;
11321}
11322#endif
11323
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324/*
11325 * "expand()" function
11326 */
11327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011328f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329{
11330 char_u *s;
11331 int len;
11332 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011333 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011336 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011339 if (argvars[1].v_type != VAR_UNKNOWN
11340 && argvars[2].v_type != VAR_UNKNOWN
11341 && get_tv_number_chk(&argvars[2], &error)
11342 && !error)
11343 {
11344 rettv->v_type = VAR_LIST;
11345 rettv->vval.v_list = NULL;
11346 }
11347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 if (*s == '%' || *s == '#' || *s == '<')
11350 {
11351 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011352 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011354 if (rettv->v_type == VAR_LIST)
11355 {
11356 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11357 list_append_string(rettv->vval.v_list, result, -1);
11358 else
11359 vim_free(result);
11360 }
11361 else
11362 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 }
11364 else
11365 {
11366 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011367 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 if (argvars[1].v_type != VAR_UNKNOWN
11369 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011370 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 if (!error)
11372 {
11373 ExpandInit(&xpc);
11374 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011375 if (p_wic)
11376 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011377 if (rettv->v_type == VAR_STRING)
11378 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11379 options, WILD_ALL);
11380 else if (rettv_list_alloc(rettv) != FAIL)
11381 {
11382 int i;
11383
11384 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11385 for (i = 0; i < xpc.xp_numfiles; i++)
11386 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11387 ExpandCleanup(&xpc);
11388 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 }
11390 else
11391 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 }
11393}
11394
11395/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011396 * Go over all entries in "d2" and add them to "d1".
11397 * When "action" is "error" then a duplicate key is an error.
11398 * When "action" is "force" then a duplicate key is overwritten.
11399 * Otherwise duplicate keys are ignored ("action" is "keep").
11400 */
11401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011402dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011403{
11404 dictitem_T *di1;
11405 hashitem_T *hi2;
11406 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011407 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011408
11409 todo = (int)d2->dv_hashtab.ht_used;
11410 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11411 {
11412 if (!HASHITEM_EMPTY(hi2))
11413 {
11414 --todo;
11415 di1 = dict_find(d1, hi2->hi_key, -1);
11416 if (d1->dv_scope != 0)
11417 {
11418 /* Disallow replacing a builtin function in l: and g:.
11419 * Check the key to be valid when adding to any
11420 * scope. */
11421 if (d1->dv_scope == VAR_DEF_SCOPE
11422 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11423 && var_check_func_name(hi2->hi_key,
11424 di1 == NULL))
11425 break;
11426 if (!valid_varname(hi2->hi_key))
11427 break;
11428 }
11429 if (di1 == NULL)
11430 {
11431 di1 = dictitem_copy(HI2DI(hi2));
11432 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11433 dictitem_free(di1);
11434 }
11435 else if (*action == 'e')
11436 {
11437 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11438 break;
11439 }
11440 else if (*action == 'f' && HI2DI(hi2) != di1)
11441 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011442 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11443 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011444 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011445 clear_tv(&di1->di_tv);
11446 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11447 }
11448 }
11449 }
11450}
11451
11452/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011453 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011454 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011455 */
11456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011457f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011458{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011459 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011460
Bram Moolenaare9a41262005-01-15 22:18:47 +000011461 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011462 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011463 list_T *l1, *l2;
11464 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011465 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011466 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011467
Bram Moolenaare9a41262005-01-15 22:18:47 +000011468 l1 = argvars[0].vval.v_list;
11469 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011470 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011471 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011472 {
11473 if (argvars[2].v_type != VAR_UNKNOWN)
11474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011475 before = get_tv_number_chk(&argvars[2], &error);
11476 if (error)
11477 return; /* type error; errmsg already given */
11478
Bram Moolenaar758711c2005-02-02 23:11:38 +000011479 if (before == l1->lv_len)
11480 item = NULL;
11481 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011482 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011483 item = list_find(l1, before);
11484 if (item == NULL)
11485 {
11486 EMSGN(_(e_listidx), before);
11487 return;
11488 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011489 }
11490 }
11491 else
11492 item = NULL;
11493 list_extend(l1, l2, item);
11494
Bram Moolenaare9a41262005-01-15 22:18:47 +000011495 copy_tv(&argvars[0], rettv);
11496 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011497 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011498 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11499 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011500 dict_T *d1, *d2;
11501 char_u *action;
11502 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011503
11504 d1 = argvars[0].vval.v_dict;
11505 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011506 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011507 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011508 {
11509 /* Check the third argument. */
11510 if (argvars[2].v_type != VAR_UNKNOWN)
11511 {
11512 static char *(av[]) = {"keep", "force", "error"};
11513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 action = get_tv_string_chk(&argvars[2]);
11515 if (action == NULL)
11516 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011517 for (i = 0; i < 3; ++i)
11518 if (STRCMP(action, av[i]) == 0)
11519 break;
11520 if (i == 3)
11521 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011522 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011523 return;
11524 }
11525 }
11526 else
11527 action = (char_u *)"force";
11528
Bram Moolenaara9922d62013-05-30 13:01:18 +020011529 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011530
Bram Moolenaare9a41262005-01-15 22:18:47 +000011531 copy_tv(&argvars[0], rettv);
11532 }
11533 }
11534 else
11535 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011536}
11537
11538/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011539 * "feedkeys()" function
11540 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011542f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011543{
11544 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011545 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011546 char_u *keys, *flags;
11547 char_u nbuf[NUMBUFLEN];
11548 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011549 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011550 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011551
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011552 /* This is not allowed in the sandbox. If the commands would still be
11553 * executed in the sandbox it would be OK, but it probably happens later,
11554 * when "sandbox" is no longer set. */
11555 if (check_secure())
11556 return;
11557
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011558 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011559
11560 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011561 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011562 flags = get_tv_string_buf(&argvars[1], nbuf);
11563 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011564 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011565 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011566 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011567 case 'n': remap = FALSE; break;
11568 case 'm': remap = TRUE; break;
11569 case 't': typed = TRUE; break;
11570 case 'i': insert = TRUE; break;
11571 case 'x': execute = 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
11595 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011596 exec_normal(TRUE);
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011597 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011598 msg_scroll |= save_msg_scroll;
11599 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011600 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011601 }
11602}
11603
11604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 * "filereadable()" function
11606 */
11607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011608f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011610 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 char_u *p;
11612 int n;
11613
Bram Moolenaarc236c162008-07-13 17:41:49 +000011614#ifndef O_NONBLOCK
11615# define O_NONBLOCK 0
11616#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011618 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11619 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 {
11621 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011622 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 }
11624 else
11625 n = FALSE;
11626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628}
11629
11630/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011631 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 * rights to write into.
11633 */
11634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011635f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011637 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638}
11639
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011640 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011641findfilendir(
11642 typval_T *argvars UNUSED,
11643 typval_T *rettv,
11644 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011645{
11646#ifdef FEAT_SEARCHPATH
11647 char_u *fname;
11648 char_u *fresult = NULL;
11649 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11650 char_u *p;
11651 char_u pathbuf[NUMBUFLEN];
11652 int count = 1;
11653 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011654 int error = FALSE;
11655#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011656
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011657 rettv->vval.v_string = NULL;
11658 rettv->v_type = VAR_STRING;
11659
11660#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011662
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011663 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011665 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11666 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011667 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011668 else
11669 {
11670 if (*p != NUL)
11671 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011674 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011676 }
11677
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011678 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11679 error = TRUE;
11680
11681 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011683 do
11684 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011685 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011686 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 fresult = find_file_in_path_option(first ? fname : NULL,
11688 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011689 0, first, path,
11690 find_what,
11691 curbuf->b_ffname,
11692 find_what == FINDFILE_DIR
11693 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011694 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011695
11696 if (fresult != NULL && rettv->v_type == VAR_LIST)
11697 list_append_string(rettv->vval.v_list, fresult, -1);
11698
11699 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011700 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011701
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011702 if (rettv->v_type == VAR_STRING)
11703 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011704#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011705}
11706
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011707static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11708static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011709
11710/*
11711 * Implementation of map() and filter().
11712 */
11713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011714filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011715{
11716 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011717 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 listitem_T *li, *nli;
11719 list_T *l = NULL;
11720 dictitem_T *di;
11721 hashtab_T *ht;
11722 hashitem_T *hi;
11723 dict_T *d = NULL;
11724 typval_T save_val;
11725 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011726 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011727 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011728 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011729 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011730 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011731 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011732 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011733
Bram Moolenaare9a41262005-01-15 22:18:47 +000011734 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011735 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011736 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011737 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011738 return;
11739 }
11740 else if (argvars[0].v_type == VAR_DICT)
11741 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011742 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011743 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 return;
11745 }
11746 else
11747 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011748 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011749 return;
11750 }
11751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011752 expr = get_tv_string_buf_chk(&argvars[1], buf);
11753 /* On type errors, the preceding call has already displayed an error
11754 * message. Avoid a misleading error message for an empty string that
11755 * was not passed as argument. */
11756 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011758 prepare_vimvar(VV_VAL, &save_val);
11759 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011760
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011761 /* We reset "did_emsg" to be able to detect whether an error
11762 * occurred during evaluation of the expression. */
11763 save_did_emsg = did_emsg;
11764 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011765
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011766 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011767 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011769 vimvars[VV_KEY].vv_type = VAR_STRING;
11770
11771 ht = &d->dv_hashtab;
11772 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011773 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776 if (!HASHITEM_EMPTY(hi))
11777 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011778 int r;
11779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011780 --todo;
11781 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011782 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011783 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11784 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 break;
11786 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011787 r = filter_map_one(&di->di_tv, expr, map, &rem);
11788 clear_tv(&vimvars[VV_KEY].vv_tv);
11789 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 break;
11791 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011792 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011793 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11794 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011795 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011796 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 }
11799 }
11800 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 }
11802 else
11803 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011804 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 for (li = l->lv_first; li != NULL; li = nli)
11807 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011808 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011809 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011811 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011812 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011813 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011814 break;
11815 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011817 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011818 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011819 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011820
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011821 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011823
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011824 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011826
11827 copy_tv(&argvars[0], rettv);
11828}
11829
11830 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011831filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011832{
Bram Moolenaar33570922005-01-25 22:26:29 +000011833 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011834 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011835 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011836
Bram Moolenaar33570922005-01-25 22:26:29 +000011837 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011838 s = expr;
11839 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011840 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011841 if (*s != NUL) /* check for trailing chars after expr */
11842 {
11843 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011844 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011845 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011846 }
11847 if (map)
11848 {
11849 /* map(): replace the list item value */
11850 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011851 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011852 *tv = rettv;
11853 }
11854 else
11855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 int error = FALSE;
11857
Bram Moolenaare9a41262005-01-15 22:18:47 +000011858 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011859 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011860 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011861 /* On type error, nothing has been removed; return FAIL to stop the
11862 * loop. The error message was given by get_tv_number_chk(). */
11863 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011864 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011865 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011866 retval = OK;
11867theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011868 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011869 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011870}
11871
11872/*
11873 * "filter()" function
11874 */
11875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011876f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011877{
11878 filter_map(argvars, rettv, FALSE);
11879}
11880
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011881/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011882 * "finddir({fname}[, {path}[, {count}]])" function
11883 */
11884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011885f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011886{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011887 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888}
11889
11890/*
11891 * "findfile({fname}[, {path}[, {count}]])" function
11892 */
11893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011894f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011895{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011896 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897}
11898
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011899#ifdef FEAT_FLOAT
11900/*
11901 * "float2nr({float})" function
11902 */
11903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011904f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011905{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011906 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011907
11908 if (get_float_arg(argvars, &f) == OK)
11909 {
11910 if (f < -0x7fffffff)
11911 rettv->vval.v_number = -0x7fffffff;
11912 else if (f > 0x7fffffff)
11913 rettv->vval.v_number = 0x7fffffff;
11914 else
11915 rettv->vval.v_number = (varnumber_T)f;
11916 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011917}
11918
11919/*
11920 * "floor({float})" function
11921 */
11922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011923f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011924{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011925 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011926
11927 rettv->v_type = VAR_FLOAT;
11928 if (get_float_arg(argvars, &f) == OK)
11929 rettv->vval.v_float = floor(f);
11930 else
11931 rettv->vval.v_float = 0.0;
11932}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011933
11934/*
11935 * "fmod()" function
11936 */
11937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011938f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011939{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011940 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011941
11942 rettv->v_type = VAR_FLOAT;
11943 if (get_float_arg(argvars, &fx) == OK
11944 && get_float_arg(&argvars[1], &fy) == OK)
11945 rettv->vval.v_float = fmod(fx, fy);
11946 else
11947 rettv->vval.v_float = 0.0;
11948}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011949#endif
11950
Bram Moolenaar0d660222005-01-07 21:51:51 +000011951/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011952 * "fnameescape({string})" function
11953 */
11954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011955f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011956{
11957 rettv->vval.v_string = vim_strsave_fnameescape(
11958 get_tv_string(&argvars[0]), FALSE);
11959 rettv->v_type = VAR_STRING;
11960}
11961
11962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 * "fnamemodify({fname}, {mods})" function
11964 */
11965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011966f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967{
11968 char_u *fname;
11969 char_u *mods;
11970 int usedlen = 0;
11971 int len;
11972 char_u *fbuf = NULL;
11973 char_u buf[NUMBUFLEN];
11974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011975 fname = get_tv_string_chk(&argvars[0]);
11976 mods = get_tv_string_buf_chk(&argvars[1], buf);
11977 if (fname == NULL || mods == NULL)
11978 fname = NULL;
11979 else
11980 {
11981 len = (int)STRLEN(fname);
11982 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011985 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 vim_free(fbuf);
11991}
11992
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011993static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994
11995/*
11996 * "foldclosed()" function
11997 */
11998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011999foldclosed_both(
12000 typval_T *argvars UNUSED,
12001 typval_T *rettv,
12002 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003{
12004#ifdef FEAT_FOLDING
12005 linenr_T lnum;
12006 linenr_T first, last;
12007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012008 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12010 {
12011 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12012 {
12013 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 return;
12018 }
12019 }
12020#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022}
12023
12024/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012025 * "foldclosed()" function
12026 */
12027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012028f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012029{
12030 foldclosed_both(argvars, rettv, FALSE);
12031}
12032
12033/*
12034 * "foldclosedend()" function
12035 */
12036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012037f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012038{
12039 foldclosed_both(argvars, rettv, TRUE);
12040}
12041
12042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 * "foldlevel()" function
12044 */
12045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012046f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047{
12048#ifdef FEAT_FOLDING
12049 linenr_T lnum;
12050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055}
12056
12057/*
12058 * "foldtext()" function
12059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012061f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062{
12063#ifdef FEAT_FOLDING
12064 linenr_T lnum;
12065 char_u *s;
12066 char_u *r;
12067 int len;
12068 char *txt;
12069#endif
12070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012071 rettv->v_type = VAR_STRING;
12072 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012074 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12075 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12076 <= curbuf->b_ml.ml_line_count
12077 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 {
12079 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012080 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12081 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 {
12083 if (!linewhite(lnum))
12084 break;
12085 ++lnum;
12086 }
12087
12088 /* Find interesting text in this line. */
12089 s = skipwhite(ml_get(lnum));
12090 /* skip C comment-start */
12091 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012094 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012095 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012096 {
12097 s = skipwhite(ml_get(lnum + 1));
12098 if (*s == '*')
12099 s = skipwhite(s + 1);
12100 }
12101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 txt = _("+-%s%3ld lines: ");
12103 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012104 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 + 20 /* for %3ld */
12106 + STRLEN(s))); /* concatenated */
12107 if (r != NULL)
12108 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012109 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12110 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12111 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 len = (int)STRLEN(r);
12113 STRCAT(r, s);
12114 /* remove 'foldmarker' and 'commentstring' */
12115 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117 }
12118 }
12119#endif
12120}
12121
12122/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012123 * "foldtextresult(lnum)" function
12124 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012126f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012127{
12128#ifdef FEAT_FOLDING
12129 linenr_T lnum;
12130 char_u *text;
12131 char_u buf[51];
12132 foldinfo_T foldinfo;
12133 int fold_count;
12134#endif
12135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 rettv->v_type = VAR_STRING;
12137 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012138#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012140 /* treat illegal types and illegal string values for {lnum} the same */
12141 if (lnum < 0)
12142 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012143 fold_count = foldedCount(curwin, lnum, &foldinfo);
12144 if (fold_count > 0)
12145 {
12146 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12147 &foldinfo, buf);
12148 if (text == buf)
12149 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012151 }
12152#endif
12153}
12154
12155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 * "foreground()" function
12157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012159f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161#ifdef FEAT_GUI
12162 if (gui.in_use)
12163 gui_mch_set_foreground();
12164#else
12165# ifdef WIN32
12166 win32_set_foreground();
12167# endif
12168#endif
12169}
12170
12171/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012172 * "function()" function
12173 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012175f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012176{
12177 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012178 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012179 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012180 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012181
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012182 if (argvars[0].v_type == VAR_FUNC)
12183 {
12184 /* function(MyFunc, [arg], dict) */
12185 s = argvars[0].vval.v_string;
12186 }
12187 else if (argvars[0].v_type == VAR_PARTIAL
12188 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012189 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012190 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012191 arg_pt = argvars[0].vval.v_partial;
12192 s = arg_pt->pt_name;
12193 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012194 else
12195 {
12196 /* function('MyFunc', [arg], dict) */
12197 s = get_tv_string(&argvars[0]);
12198 use_string = TRUE;
12199 }
12200
12201 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012202 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012203 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012204 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12205 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012206 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012207 else
12208 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012209 int dict_idx = 0;
12210 int arg_idx = 0;
12211 list_T *list = NULL;
12212
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012213 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012214 {
12215 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012216 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012217
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012218 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12219 * also be called from another script. Using trans_function_name()
12220 * would also work, but some plugins depend on the name being
12221 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012222 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012223 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12224 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012225 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012226 STRCPY(name, sid_buf);
12227 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012228 }
12229 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012230 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012231 name = vim_strsave(s);
12232
12233 if (argvars[1].v_type != VAR_UNKNOWN)
12234 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012235 if (argvars[2].v_type != VAR_UNKNOWN)
12236 {
12237 /* function(name, [args], dict) */
12238 arg_idx = 1;
12239 dict_idx = 2;
12240 }
12241 else if (argvars[1].v_type == VAR_DICT)
12242 /* function(name, dict) */
12243 dict_idx = 1;
12244 else
12245 /* function(name, [args]) */
12246 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012247 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012248 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012249 if (argvars[dict_idx].v_type != VAR_DICT)
12250 {
12251 EMSG(_("E922: expected a dict"));
12252 vim_free(name);
12253 return;
12254 }
12255 if (argvars[dict_idx].vval.v_dict == NULL)
12256 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012257 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012258 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012259 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012260 if (argvars[arg_idx].v_type != VAR_LIST)
12261 {
12262 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12263 vim_free(name);
12264 return;
12265 }
12266 list = argvars[arg_idx].vval.v_list;
12267 if (list == NULL || list->lv_len == 0)
12268 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012269 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012270 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012271 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012272 {
12273 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012274
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012275 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012276 if (pt == NULL)
12277 vim_free(name);
12278 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012279 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012280 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012281 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012282 listitem_T *li;
12283 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012284 int arg_len = 0;
12285 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012286
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012287 if (arg_pt != NULL)
12288 arg_len = arg_pt->pt_argc;
12289 if (list != NULL)
12290 lv_len = list->lv_len;
12291 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012292 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012293 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012294 if (pt->pt_argv == NULL)
12295 {
12296 vim_free(pt);
12297 vim_free(name);
12298 return;
12299 }
12300 else
12301 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012302 for (i = 0; i < arg_len; i++)
12303 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12304 if (lv_len > 0)
12305 for (li = list->lv_first; li != NULL;
12306 li = li->li_next)
12307 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012308 }
12309 }
12310
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012311 /* For "function(dict.func, [], dict)" and "func" is a partial
12312 * use "dict". That is backwards compatible. */
12313 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012314 {
12315 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12316 ++pt->pt_dict->dv_refcount;
12317 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012318 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012319 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012320 pt->pt_dict = arg_pt->pt_dict;
12321 if (pt->pt_dict != NULL)
12322 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012323 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012324
12325 pt->pt_refcount = 1;
12326 pt->pt_name = name;
12327 func_ref(pt->pt_name);
12328 }
12329 rettv->v_type = VAR_PARTIAL;
12330 rettv->vval.v_partial = pt;
12331 }
12332 else
12333 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012334 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012335 rettv->v_type = VAR_FUNC;
12336 rettv->vval.v_string = name;
12337 func_ref(name);
12338 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012339 }
12340}
12341
12342/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012343 * "garbagecollect()" function
12344 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012346f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012347{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012348 /* This is postponed until we are back at the toplevel, because we may be
12349 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12350 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012351
12352 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12353 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012354}
12355
12356/*
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020012357 * "garbagecollect_for_testing()" function
12358 */
12359 static void
12360f_garbagecollect_for_testing(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12361{
12362 /* This is dangerous, any Lists and Dicts used internally may be freed
12363 * while still in use. */
12364 garbage_collect(TRUE);
12365}
12366
12367/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012368 * "get()" function
12369 */
12370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012371f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012372{
Bram Moolenaar33570922005-01-25 22:26:29 +000012373 listitem_T *li;
12374 list_T *l;
12375 dictitem_T *di;
12376 dict_T *d;
12377 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012378
Bram Moolenaare9a41262005-01-15 22:18:47 +000012379 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012380 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012381 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012383 int error = FALSE;
12384
12385 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12386 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012387 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012388 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012390 else if (argvars[0].v_type == VAR_DICT)
12391 {
12392 if ((d = argvars[0].vval.v_dict) != NULL)
12393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012394 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012395 if (di != NULL)
12396 tv = &di->di_tv;
12397 }
12398 }
12399 else
12400 EMSG2(_(e_listdictarg), "get()");
12401
12402 if (tv == NULL)
12403 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012404 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012405 copy_tv(&argvars[2], rettv);
12406 }
12407 else
12408 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012409}
12410
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012411static 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 +000012412
12413/*
12414 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012415 * Return a range (from start to end) of lines in rettv from the specified
12416 * buffer.
12417 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012418 */
12419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012420get_buffer_lines(
12421 buf_T *buf,
12422 linenr_T start,
12423 linenr_T end,
12424 int retlist,
12425 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012426{
12427 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012428
Bram Moolenaar959a1432013-12-14 12:17:38 +010012429 rettv->v_type = VAR_STRING;
12430 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012431 if (retlist && rettv_list_alloc(rettv) == FAIL)
12432 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012433
12434 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12435 return;
12436
12437 if (!retlist)
12438 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012439 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12440 p = ml_get_buf(buf, start, FALSE);
12441 else
12442 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012443 rettv->vval.v_string = vim_strsave(p);
12444 }
12445 else
12446 {
12447 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012448 return;
12449
12450 if (start < 1)
12451 start = 1;
12452 if (end > buf->b_ml.ml_line_count)
12453 end = buf->b_ml.ml_line_count;
12454 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012455 if (list_append_string(rettv->vval.v_list,
12456 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012457 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012458 }
12459}
12460
12461/*
12462 * "getbufline()" function
12463 */
12464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012465f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012466{
12467 linenr_T lnum;
12468 linenr_T end;
12469 buf_T *buf;
12470
12471 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12472 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012473 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012474 --emsg_off;
12475
Bram Moolenaar661b1822005-07-28 22:36:45 +000012476 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012477 if (argvars[2].v_type == VAR_UNKNOWN)
12478 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012479 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012480 end = get_tv_lnum_buf(&argvars[2], buf);
12481
Bram Moolenaar342337a2005-07-21 21:11:17 +000012482 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012483}
12484
Bram Moolenaar0d660222005-01-07 21:51:51 +000012485/*
12486 * "getbufvar()" function
12487 */
12488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012489f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012490{
12491 buf_T *buf;
12492 buf_T *save_curbuf;
12493 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012494 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012495 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012497 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12498 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012500 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012502 rettv->v_type = VAR_STRING;
12503 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012504
12505 if (buf != NULL && varname != NULL)
12506 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012507 /* set curbuf to be our buf, temporarily */
12508 save_curbuf = curbuf;
12509 curbuf = buf;
12510
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012512 {
12513 if (get_option_tv(&varname, rettv, TRUE) == OK)
12514 done = TRUE;
12515 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012516 else if (STRCMP(varname, "changedtick") == 0)
12517 {
12518 rettv->v_type = VAR_NUMBER;
12519 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012520 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012521 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012522 else
12523 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012524 /* Look up the variable. */
12525 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12526 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12527 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012528 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012529 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012530 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012531 done = TRUE;
12532 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012534
12535 /* restore previous notion of curbuf */
12536 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537 }
12538
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012539 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12540 /* use the default value */
12541 copy_tv(&argvars[2], rettv);
12542
Bram Moolenaar0d660222005-01-07 21:51:51 +000012543 --emsg_off;
12544}
12545
12546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547 * "getchar()" function
12548 */
12549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012550f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551{
12552 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012553 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012555 /* Position the cursor. Needed after a message that ends in a space. */
12556 windgoto(msg_row, msg_col);
12557
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558 ++no_mapping;
12559 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012560 for (;;)
12561 {
12562 if (argvars[0].v_type == VAR_UNKNOWN)
12563 /* getchar(): blocking wait. */
12564 n = safe_vgetc();
12565 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12566 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012567 n = vpeekc_any();
12568 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012569 /* illegal argument or getchar(0) and no char avail: return zero */
12570 n = 0;
12571 else
12572 /* getchar(0) and char avail: return char */
12573 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012574
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012575 if (n == K_IGNORE)
12576 continue;
12577 break;
12578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579 --no_mapping;
12580 --allow_keys;
12581
Bram Moolenaar219b8702006-11-01 14:32:36 +000012582 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12583 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12584 vimvars[VV_MOUSE_COL].vv_nr = 0;
12585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012586 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587 if (IS_SPECIAL(n) || mod_mask != 0)
12588 {
12589 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12590 int i = 0;
12591
12592 /* Turn a special key into three bytes, plus modifier. */
12593 if (mod_mask != 0)
12594 {
12595 temp[i++] = K_SPECIAL;
12596 temp[i++] = KS_MODIFIER;
12597 temp[i++] = mod_mask;
12598 }
12599 if (IS_SPECIAL(n))
12600 {
12601 temp[i++] = K_SPECIAL;
12602 temp[i++] = K_SECOND(n);
12603 temp[i++] = K_THIRD(n);
12604 }
12605#ifdef FEAT_MBYTE
12606 else if (has_mbyte)
12607 i += (*mb_char2bytes)(n, temp + i);
12608#endif
12609 else
12610 temp[i++] = n;
12611 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012612 rettv->v_type = VAR_STRING;
12613 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012614
12615#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012616 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012617 {
12618 int row = mouse_row;
12619 int col = mouse_col;
12620 win_T *win;
12621 linenr_T lnum;
12622# ifdef FEAT_WINDOWS
12623 win_T *wp;
12624# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012625 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012626
12627 if (row >= 0 && col >= 0)
12628 {
12629 /* Find the window at the mouse coordinates and compute the
12630 * text position. */
12631 win = mouse_find_win(&row, &col);
12632 (void)mouse_comp_pos(win, &row, &col, &lnum);
12633# ifdef FEAT_WINDOWS
12634 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012635 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012636# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012637 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012638 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12639 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12640 }
12641 }
12642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 }
12644}
12645
12646/*
12647 * "getcharmod()" function
12648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012650f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012652 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653}
12654
12655/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012656 * "getcharsearch()" function
12657 */
12658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012659f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012660{
12661 if (rettv_dict_alloc(rettv) != FAIL)
12662 {
12663 dict_T *dict = rettv->vval.v_dict;
12664
12665 dict_add_nr_str(dict, "char", 0L, last_csearch());
12666 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12667 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12668 }
12669}
12670
12671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 * "getcmdline()" function
12673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012675f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677 rettv->v_type = VAR_STRING;
12678 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679}
12680
12681/*
12682 * "getcmdpos()" function
12683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012685f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688}
12689
12690/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012691 * "getcmdtype()" function
12692 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012694f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012695{
12696 rettv->v_type = VAR_STRING;
12697 rettv->vval.v_string = alloc(2);
12698 if (rettv->vval.v_string != NULL)
12699 {
12700 rettv->vval.v_string[0] = get_cmdline_type();
12701 rettv->vval.v_string[1] = NUL;
12702 }
12703}
12704
12705/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012706 * "getcmdwintype()" function
12707 */
12708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012709f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012710{
12711 rettv->v_type = VAR_STRING;
12712 rettv->vval.v_string = NULL;
12713#ifdef FEAT_CMDWIN
12714 rettv->vval.v_string = alloc(2);
12715 if (rettv->vval.v_string != NULL)
12716 {
12717 rettv->vval.v_string[0] = cmdwin_type;
12718 rettv->vval.v_string[1] = NUL;
12719 }
12720#endif
12721}
12722
12723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 * "getcwd()" function
12725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012727f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012729 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012730 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012733 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012734
12735 wp = find_tabwin(&argvars[0], &argvars[1]);
12736 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012738 if (wp->w_localdir != NULL)
12739 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12740 else if(globaldir != NULL)
12741 rettv->vval.v_string = vim_strsave(globaldir);
12742 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012743 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012744 cwd = alloc(MAXPATHL);
12745 if (cwd != NULL)
12746 {
12747 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12748 rettv->vval.v_string = vim_strsave(cwd);
12749 vim_free(cwd);
12750 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012751 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012752#ifdef BACKSLASH_IN_FILENAME
12753 if (rettv->vval.v_string != NULL)
12754 slash_adjust(rettv->vval.v_string);
12755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756 }
12757}
12758
12759/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012760 * "getfontname()" function
12761 */
12762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012763f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012764{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765 rettv->v_type = VAR_STRING;
12766 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012767#ifdef FEAT_GUI
12768 if (gui.in_use)
12769 {
12770 GuiFont font;
12771 char_u *name = NULL;
12772
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012773 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012774 {
12775 /* Get the "Normal" font. Either the name saved by
12776 * hl_set_font_name() or from the font ID. */
12777 font = gui.norm_font;
12778 name = hl_get_font_name();
12779 }
12780 else
12781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012783 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12784 return;
12785 font = gui_mch_get_font(name, FALSE);
12786 if (font == NOFONT)
12787 return; /* Invalid font name, return empty string. */
12788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012790 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012791 gui_mch_free_font(font);
12792 }
12793#endif
12794}
12795
12796/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012797 * "getfperm({fname})" function
12798 */
12799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012800f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012801{
12802 char_u *fname;
12803 struct stat st;
12804 char_u *perm = NULL;
12805 char_u flags[] = "rwx";
12806 int i;
12807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012811 if (mch_stat((char *)fname, &st) >= 0)
12812 {
12813 perm = vim_strsave((char_u *)"---------");
12814 if (perm != NULL)
12815 {
12816 for (i = 0; i < 9; i++)
12817 {
12818 if (st.st_mode & (1 << (8 - i)))
12819 perm[i] = flags[i % 3];
12820 }
12821 }
12822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012824}
12825
12826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827 * "getfsize({fname})" function
12828 */
12829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012830f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831{
12832 char_u *fname;
12833 struct stat st;
12834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838
12839 if (mch_stat((char *)fname, &st) >= 0)
12840 {
12841 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012846
12847 /* non-perfect check for overflow */
12848 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12849 rettv->vval.v_number = -2;
12850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851 }
12852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854}
12855
12856/*
12857 * "getftime({fname})" function
12858 */
12859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012860f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861{
12862 char_u *fname;
12863 struct stat st;
12864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866
12867 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012870 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871}
12872
12873/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012874 * "getftype({fname})" function
12875 */
12876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012877f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012878{
12879 char_u *fname;
12880 struct stat st;
12881 char_u *type = NULL;
12882 char *t;
12883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012887 if (mch_lstat((char *)fname, &st) >= 0)
12888 {
12889#ifdef S_ISREG
12890 if (S_ISREG(st.st_mode))
12891 t = "file";
12892 else if (S_ISDIR(st.st_mode))
12893 t = "dir";
12894# ifdef S_ISLNK
12895 else if (S_ISLNK(st.st_mode))
12896 t = "link";
12897# endif
12898# ifdef S_ISBLK
12899 else if (S_ISBLK(st.st_mode))
12900 t = "bdev";
12901# endif
12902# ifdef S_ISCHR
12903 else if (S_ISCHR(st.st_mode))
12904 t = "cdev";
12905# endif
12906# ifdef S_ISFIFO
12907 else if (S_ISFIFO(st.st_mode))
12908 t = "fifo";
12909# endif
12910# ifdef S_ISSOCK
12911 else if (S_ISSOCK(st.st_mode))
12912 t = "fifo";
12913# endif
12914 else
12915 t = "other";
12916#else
12917# ifdef S_IFMT
12918 switch (st.st_mode & S_IFMT)
12919 {
12920 case S_IFREG: t = "file"; break;
12921 case S_IFDIR: t = "dir"; break;
12922# ifdef S_IFLNK
12923 case S_IFLNK: t = "link"; break;
12924# endif
12925# ifdef S_IFBLK
12926 case S_IFBLK: t = "bdev"; break;
12927# endif
12928# ifdef S_IFCHR
12929 case S_IFCHR: t = "cdev"; break;
12930# endif
12931# ifdef S_IFIFO
12932 case S_IFIFO: t = "fifo"; break;
12933# endif
12934# ifdef S_IFSOCK
12935 case S_IFSOCK: t = "socket"; break;
12936# endif
12937 default: t = "other";
12938 }
12939# else
12940 if (mch_isdir(fname))
12941 t = "dir";
12942 else
12943 t = "file";
12944# endif
12945#endif
12946 type = vim_strsave((char_u *)t);
12947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012949}
12950
12951/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012952 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012953 */
12954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012955f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956{
12957 linenr_T lnum;
12958 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012959 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012960
12961 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012962 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012963 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012964 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012965 retlist = FALSE;
12966 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012967 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012968 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012969 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012970 retlist = TRUE;
12971 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012972
Bram Moolenaar342337a2005-07-21 21:11:17 +000012973 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012974}
12975
12976/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012977 * "getmatches()" function
12978 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012980f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012981{
12982#ifdef FEAT_SEARCH_EXTRA
12983 dict_T *dict;
12984 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012985 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012986
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012987 if (rettv_list_alloc(rettv) == OK)
12988 {
12989 while (cur != NULL)
12990 {
12991 dict = dict_alloc();
12992 if (dict == NULL)
12993 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012994 if (cur->match.regprog == NULL)
12995 {
12996 /* match added with matchaddpos() */
12997 for (i = 0; i < MAXPOSMATCH; ++i)
12998 {
12999 llpos_T *llpos;
13000 char buf[6];
13001 list_T *l;
13002
13003 llpos = &cur->pos.pos[i];
13004 if (llpos->lnum == 0)
13005 break;
13006 l = list_alloc();
13007 if (l == NULL)
13008 break;
13009 list_append_number(l, (varnumber_T)llpos->lnum);
13010 if (llpos->col > 0)
13011 {
13012 list_append_number(l, (varnumber_T)llpos->col);
13013 list_append_number(l, (varnumber_T)llpos->len);
13014 }
13015 sprintf(buf, "pos%d", i + 1);
13016 dict_add_list(dict, buf, l);
13017 }
13018 }
13019 else
13020 {
13021 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13022 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013023 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013024 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13025 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013026# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013027 if (cur->conceal_char)
13028 {
13029 char_u buf[MB_MAXBYTES + 1];
13030
13031 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13032 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13033 }
13034# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013035 list_append_dict(rettv->vval.v_list, dict);
13036 cur = cur->next;
13037 }
13038 }
13039#endif
13040}
13041
13042/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013043 * "getpid()" function
13044 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013046f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013047{
13048 rettv->vval.v_number = mch_get_pid();
13049}
13050
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013051static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013052
13053/*
13054 * "getcurpos()" function
13055 */
13056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013057f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013058{
13059 getpos_both(argvars, rettv, TRUE);
13060}
13061
Bram Moolenaar18081e32008-02-20 19:11:07 +000013062/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013063 * "getpos(string)" function
13064 */
13065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013066f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013067{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013068 getpos_both(argvars, rettv, FALSE);
13069}
13070
13071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013072getpos_both(
13073 typval_T *argvars,
13074 typval_T *rettv,
13075 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013076{
Bram Moolenaara5525202006-03-02 22:52:09 +000013077 pos_T *fp;
13078 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013079 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013080
13081 if (rettv_list_alloc(rettv) == OK)
13082 {
13083 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013084 if (getcurpos)
13085 fp = &curwin->w_cursor;
13086 else
13087 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013088 if (fnum != -1)
13089 list_append_number(l, (varnumber_T)fnum);
13090 else
13091 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013092 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13093 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013094 list_append_number(l, (fp != NULL)
13095 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013096 : (varnumber_T)0);
13097 list_append_number(l,
13098#ifdef FEAT_VIRTUALEDIT
13099 (fp != NULL) ? (varnumber_T)fp->coladd :
13100#endif
13101 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013102 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013103 {
13104 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013105 list_append_number(l, curwin->w_curswant == MAXCOL ?
13106 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013107 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013108 }
13109 else
13110 rettv->vval.v_number = FALSE;
13111}
13112
13113/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013114 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013115 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013117f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013118{
13119#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013120 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013121#endif
13122
Bram Moolenaar2641f772005-03-25 21:58:17 +000013123#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013124 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013125 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013126 wp = NULL;
13127 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13128 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013129 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013130 if (wp == NULL)
13131 return;
13132 }
13133
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013134 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013135 }
13136#endif
13137}
13138
13139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 * "getreg()" function
13141 */
13142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013143f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144{
13145 char_u *strregname;
13146 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013147 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013148 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013149 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013151 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 strregname = get_tv_string_chk(&argvars[0]);
13154 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013155 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013157 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013158 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13159 return_list = get_tv_number_chk(&argvars[2], &error);
13160 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013163 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013164
13165 if (error)
13166 return;
13167
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 regname = (strregname == NULL ? '"' : *strregname);
13169 if (regname == 0)
13170 regname = '"';
13171
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013172 if (return_list)
13173 {
13174 rettv->v_type = VAR_LIST;
13175 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13176 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013177 if (rettv->vval.v_list != NULL)
13178 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013179 }
13180 else
13181 {
13182 rettv->v_type = VAR_STRING;
13183 rettv->vval.v_string = get_reg_contents(regname,
13184 arg2 ? GREG_EXPR_SRC : 0);
13185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186}
13187
13188/*
13189 * "getregtype()" function
13190 */
13191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013192f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193{
13194 char_u *strregname;
13195 int regname;
13196 char_u buf[NUMBUFLEN + 2];
13197 long reglen = 0;
13198
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013199 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013200 {
13201 strregname = get_tv_string_chk(&argvars[0]);
13202 if (strregname == NULL) /* type error; errmsg already given */
13203 {
13204 rettv->v_type = VAR_STRING;
13205 rettv->vval.v_string = NULL;
13206 return;
13207 }
13208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209 else
13210 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013211 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212
13213 regname = (strregname == NULL ? '"' : *strregname);
13214 if (regname == 0)
13215 regname = '"';
13216
13217 buf[0] = NUL;
13218 buf[1] = NUL;
13219 switch (get_reg_type(regname, &reglen))
13220 {
13221 case MLINE: buf[0] = 'V'; break;
13222 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223 case MBLOCK:
13224 buf[0] = Ctrl_V;
13225 sprintf((char *)buf + 1, "%ld", reglen + 1);
13226 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228 rettv->v_type = VAR_STRING;
13229 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230}
13231
13232/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013233 * "gettabvar()" function
13234 */
13235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013236f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013237{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013238 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013239 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013240 dictitem_T *v;
13241 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013242 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013243
13244 rettv->v_type = VAR_STRING;
13245 rettv->vval.v_string = NULL;
13246
13247 varname = get_tv_string_chk(&argvars[1]);
13248 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13249 if (tp != NULL && varname != NULL)
13250 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013251 /* Set tp to be our tabpage, temporarily. Also set the window to the
13252 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013253 if (switch_win(&oldcurwin, &oldtabpage,
13254 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013255 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013256 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013257 /* look up the variable */
13258 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13259 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13260 if (v != NULL)
13261 {
13262 copy_tv(&v->di_tv, rettv);
13263 done = TRUE;
13264 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013265 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013266
13267 /* restore previous notion of curwin */
13268 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013269 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013270
13271 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013272 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013273}
13274
13275/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013276 * "gettabwinvar()" function
13277 */
13278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013279f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013280{
13281 getwinvar(argvars, rettv, 1);
13282}
13283
13284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 * "getwinposx()" function
13286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013288f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291#ifdef FEAT_GUI
13292 if (gui.in_use)
13293 {
13294 int x, y;
13295
13296 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 }
13299#endif
13300}
13301
13302/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013303 * "win_findbuf()" function
13304 */
13305 static void
13306f_win_findbuf(typval_T *argvars, typval_T *rettv)
13307{
13308 if (rettv_list_alloc(rettv) != FAIL)
13309 win_findbuf(argvars, rettv->vval.v_list);
13310}
13311
13312/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013313 * "win_getid()" function
13314 */
13315 static void
13316f_win_getid(typval_T *argvars, typval_T *rettv)
13317{
13318 rettv->vval.v_number = win_getid(argvars);
13319}
13320
13321/*
13322 * "win_gotoid()" function
13323 */
13324 static void
13325f_win_gotoid(typval_T *argvars, typval_T *rettv)
13326{
13327 rettv->vval.v_number = win_gotoid(argvars);
13328}
13329
13330/*
13331 * "win_id2tabwin()" function
13332 */
13333 static void
13334f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13335{
13336 if (rettv_list_alloc(rettv) != FAIL)
13337 win_id2tabwin(argvars, rettv->vval.v_list);
13338}
13339
13340/*
13341 * "win_id2win()" function
13342 */
13343 static void
13344f_win_id2win(typval_T *argvars, typval_T *rettv)
13345{
13346 rettv->vval.v_number = win_id2win(argvars);
13347}
13348
13349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 * "getwinposy()" function
13351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013353f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356#ifdef FEAT_GUI
13357 if (gui.in_use)
13358 {
13359 int x, y;
13360
13361 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013362 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 }
13364#endif
13365}
13366
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013367/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013368 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013369 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013370 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013371find_win_by_nr(
13372 typval_T *vp,
13373 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013374{
13375#ifdef FEAT_WINDOWS
13376 win_T *wp;
13377#endif
13378 int nr;
13379
13380 nr = get_tv_number_chk(vp, NULL);
13381
13382#ifdef FEAT_WINDOWS
13383 if (nr < 0)
13384 return NULL;
13385 if (nr == 0)
13386 return curwin;
13387
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013388 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13389 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013390 if (--nr <= 0)
13391 break;
13392 return wp;
13393#else
13394 if (nr == 0 || nr == 1)
13395 return curwin;
13396 return NULL;
13397#endif
13398}
13399
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013401 * Find window specified by "wvp" in tabpage "tvp".
13402 */
13403 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013404find_tabwin(
13405 typval_T *wvp, /* VAR_UNKNOWN for current window */
13406 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013407{
13408 win_T *wp = NULL;
13409 tabpage_T *tp = NULL;
13410 long n;
13411
13412 if (wvp->v_type != VAR_UNKNOWN)
13413 {
13414 if (tvp->v_type != VAR_UNKNOWN)
13415 {
13416 n = get_tv_number(tvp);
13417 if (n >= 0)
13418 tp = find_tabpage(n);
13419 }
13420 else
13421 tp = curtab;
13422
13423 if (tp != NULL)
13424 wp = find_win_by_nr(wvp, tp);
13425 }
13426 else
13427 wp = curwin;
13428
13429 return wp;
13430}
13431
13432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 * "getwinvar()" function
13434 */
13435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013436f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013438 getwinvar(argvars, rettv, 0);
13439}
13440
13441/*
13442 * getwinvar() and gettabwinvar()
13443 */
13444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013445getwinvar(
13446 typval_T *argvars,
13447 typval_T *rettv,
13448 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013449{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013450 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013452 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013453 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013454 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013455#ifdef FEAT_WINDOWS
13456 win_T *oldcurwin;
13457 tabpage_T *oldtabpage;
13458 int need_switch_win;
13459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013461#ifdef FEAT_WINDOWS
13462 if (off == 1)
13463 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13464 else
13465 tp = curtab;
13466#endif
13467 win = find_win_by_nr(&argvars[off], tp);
13468 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013469 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013471 rettv->v_type = VAR_STRING;
13472 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473
13474 if (win != NULL && varname != NULL)
13475 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013476#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013477 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013478 * otherwise the window is not valid. Only do this when needed,
13479 * autocommands get blocked. */
13480 need_switch_win = !(tp == curtab && win == curwin);
13481 if (!need_switch_win
13482 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13483#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013484 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013485 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013486 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013487 if (get_option_tv(&varname, rettv, 1) == OK)
13488 done = TRUE;
13489 }
13490 else
13491 {
13492 /* Look up the variable. */
13493 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13494 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13495 varname, FALSE);
13496 if (v != NULL)
13497 {
13498 copy_tv(&v->di_tv, rettv);
13499 done = TRUE;
13500 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013503
Bram Moolenaarba117c22015-09-29 16:53:22 +020013504#ifdef FEAT_WINDOWS
13505 if (need_switch_win)
13506 /* restore previous notion of curwin */
13507 restore_win(oldcurwin, oldtabpage, TRUE);
13508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 }
13510
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013511 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13512 /* use the default return value */
13513 copy_tv(&argvars[off + 2], rettv);
13514
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 --emsg_off;
13516}
13517
13518/*
13519 * "glob()" function
13520 */
13521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013522f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013524 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013526 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013528 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013529 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013530 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013531 if (argvars[1].v_type != VAR_UNKNOWN)
13532 {
13533 if (get_tv_number_chk(&argvars[1], &error))
13534 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013535 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013536 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013537 if (get_tv_number_chk(&argvars[2], &error))
13538 {
13539 rettv->v_type = VAR_LIST;
13540 rettv->vval.v_list = NULL;
13541 }
13542 if (argvars[3].v_type != VAR_UNKNOWN
13543 && get_tv_number_chk(&argvars[3], &error))
13544 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013545 }
13546 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013547 if (!error)
13548 {
13549 ExpandInit(&xpc);
13550 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013551 if (p_wic)
13552 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013553 if (rettv->v_type == VAR_STRING)
13554 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013555 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013556 else if (rettv_list_alloc(rettv) != FAIL)
13557 {
13558 int i;
13559
13560 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13561 NULL, options, WILD_ALL_KEEP);
13562 for (i = 0; i < xpc.xp_numfiles; i++)
13563 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13564
13565 ExpandCleanup(&xpc);
13566 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013567 }
13568 else
13569 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570}
13571
13572/*
13573 * "globpath()" function
13574 */
13575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013576f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013578 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013580 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013581 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013582 garray_T ga;
13583 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013585 /* When the optional second argument is non-zero, don't remove matches
13586 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013588 if (argvars[2].v_type != VAR_UNKNOWN)
13589 {
13590 if (get_tv_number_chk(&argvars[2], &error))
13591 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013592 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013593 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013594 if (get_tv_number_chk(&argvars[3], &error))
13595 {
13596 rettv->v_type = VAR_LIST;
13597 rettv->vval.v_list = NULL;
13598 }
13599 if (argvars[4].v_type != VAR_UNKNOWN
13600 && get_tv_number_chk(&argvars[4], &error))
13601 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013602 }
13603 }
13604 if (file != NULL && !error)
13605 {
13606 ga_init2(&ga, (int)sizeof(char_u *), 10);
13607 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13608 if (rettv->v_type == VAR_STRING)
13609 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13610 else if (rettv_list_alloc(rettv) != FAIL)
13611 for (i = 0; i < ga.ga_len; ++i)
13612 list_append_string(rettv->vval.v_list,
13613 ((char_u **)(ga.ga_data))[i], -1);
13614 ga_clear_strings(&ga);
13615 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013616 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013617 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618}
13619
13620/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013621 * "glob2regpat()" function
13622 */
13623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013624f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013625{
13626 char_u *pat = get_tv_string_chk(&argvars[0]);
13627
13628 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013629 rettv->vval.v_string = (pat == NULL)
13630 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013631}
13632
13633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 * "has()" function
13635 */
13636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013637f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638{
13639 int i;
13640 char_u *name;
13641 int n = FALSE;
13642 static char *(has_list[]) =
13643 {
13644#ifdef AMIGA
13645 "amiga",
13646# ifdef FEAT_ARP
13647 "arp",
13648# endif
13649#endif
13650#ifdef __BEOS__
13651 "beos",
13652#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013653#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 "mac",
13655#endif
13656#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013657 "macunix", /* built with 'darwin' enabled */
13658#endif
13659#if defined(__APPLE__) && __APPLE__ == 1
13660 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662#ifdef __QNX__
13663 "qnx",
13664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665#ifdef UNIX
13666 "unix",
13667#endif
13668#ifdef VMS
13669 "vms",
13670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671#ifdef WIN32
13672 "win32",
13673#endif
13674#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13675 "win32unix",
13676#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013677#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 "win64",
13679#endif
13680#ifdef EBCDIC
13681 "ebcdic",
13682#endif
13683#ifndef CASE_INSENSITIVE_FILENAME
13684 "fname_case",
13685#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013686#ifdef HAVE_ACL
13687 "acl",
13688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689#ifdef FEAT_ARABIC
13690 "arabic",
13691#endif
13692#ifdef FEAT_AUTOCMD
13693 "autocmd",
13694#endif
13695#ifdef FEAT_BEVAL
13696 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013697# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13698 "balloon_multiline",
13699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700#endif
13701#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13702 "builtin_terms",
13703# ifdef ALL_BUILTIN_TCAPS
13704 "all_builtin_terms",
13705# endif
13706#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013707#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13708 || defined(FEAT_GUI_W32) \
13709 || defined(FEAT_GUI_MOTIF))
13710 "browsefilter",
13711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712#ifdef FEAT_BYTEOFF
13713 "byte_offset",
13714#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013715#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013716 "channel",
13717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718#ifdef FEAT_CINDENT
13719 "cindent",
13720#endif
13721#ifdef FEAT_CLIENTSERVER
13722 "clientserver",
13723#endif
13724#ifdef FEAT_CLIPBOARD
13725 "clipboard",
13726#endif
13727#ifdef FEAT_CMDL_COMPL
13728 "cmdline_compl",
13729#endif
13730#ifdef FEAT_CMDHIST
13731 "cmdline_hist",
13732#endif
13733#ifdef FEAT_COMMENTS
13734 "comments",
13735#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013736#ifdef FEAT_CONCEAL
13737 "conceal",
13738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739#ifdef FEAT_CRYPT
13740 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013741 "crypt-blowfish",
13742 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743#endif
13744#ifdef FEAT_CSCOPE
13745 "cscope",
13746#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013747#ifdef FEAT_CURSORBIND
13748 "cursorbind",
13749#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013750#ifdef CURSOR_SHAPE
13751 "cursorshape",
13752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753#ifdef DEBUG
13754 "debug",
13755#endif
13756#ifdef FEAT_CON_DIALOG
13757 "dialog_con",
13758#endif
13759#ifdef FEAT_GUI_DIALOG
13760 "dialog_gui",
13761#endif
13762#ifdef FEAT_DIFF
13763 "diff",
13764#endif
13765#ifdef FEAT_DIGRAPHS
13766 "digraphs",
13767#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013768#ifdef FEAT_DIRECTX
13769 "directx",
13770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771#ifdef FEAT_DND
13772 "dnd",
13773#endif
13774#ifdef FEAT_EMACS_TAGS
13775 "emacs_tags",
13776#endif
13777 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013778 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779#ifdef FEAT_SEARCH_EXTRA
13780 "extra_search",
13781#endif
13782#ifdef FEAT_FKMAP
13783 "farsi",
13784#endif
13785#ifdef FEAT_SEARCHPATH
13786 "file_in_path",
13787#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013788#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013789 "filterpipe",
13790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791#ifdef FEAT_FIND_ID
13792 "find_in_path",
13793#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013794#ifdef FEAT_FLOAT
13795 "float",
13796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797#ifdef FEAT_FOLDING
13798 "folding",
13799#endif
13800#ifdef FEAT_FOOTER
13801 "footer",
13802#endif
13803#if !defined(USE_SYSTEM) && defined(UNIX)
13804 "fork",
13805#endif
13806#ifdef FEAT_GETTEXT
13807 "gettext",
13808#endif
13809#ifdef FEAT_GUI
13810 "gui",
13811#endif
13812#ifdef FEAT_GUI_ATHENA
13813# ifdef FEAT_GUI_NEXTAW
13814 "gui_neXtaw",
13815# else
13816 "gui_athena",
13817# endif
13818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#ifdef FEAT_GUI_GTK
13820 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013821# ifdef USE_GTK3
13822 "gui_gtk3",
13823# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013827#ifdef FEAT_GUI_GNOME
13828 "gui_gnome",
13829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830#ifdef FEAT_GUI_MAC
13831 "gui_mac",
13832#endif
13833#ifdef FEAT_GUI_MOTIF
13834 "gui_motif",
13835#endif
13836#ifdef FEAT_GUI_PHOTON
13837 "gui_photon",
13838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839#ifdef FEAT_GUI_W32
13840 "gui_win32",
13841#endif
13842#ifdef FEAT_HANGULIN
13843 "hangul_input",
13844#endif
13845#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13846 "iconv",
13847#endif
13848#ifdef FEAT_INS_EXPAND
13849 "insert_expand",
13850#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013851#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013852 "job",
13853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854#ifdef FEAT_JUMPLIST
13855 "jumplist",
13856#endif
13857#ifdef FEAT_KEYMAP
13858 "keymap",
13859#endif
13860#ifdef FEAT_LANGMAP
13861 "langmap",
13862#endif
13863#ifdef FEAT_LIBCALL
13864 "libcall",
13865#endif
13866#ifdef FEAT_LINEBREAK
13867 "linebreak",
13868#endif
13869#ifdef FEAT_LISP
13870 "lispindent",
13871#endif
13872#ifdef FEAT_LISTCMDS
13873 "listcmds",
13874#endif
13875#ifdef FEAT_LOCALMAP
13876 "localmap",
13877#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013878#ifdef FEAT_LUA
13879# ifndef DYNAMIC_LUA
13880 "lua",
13881# endif
13882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883#ifdef FEAT_MENU
13884 "menu",
13885#endif
13886#ifdef FEAT_SESSION
13887 "mksession",
13888#endif
13889#ifdef FEAT_MODIFY_FNAME
13890 "modify_fname",
13891#endif
13892#ifdef FEAT_MOUSE
13893 "mouse",
13894#endif
13895#ifdef FEAT_MOUSESHAPE
13896 "mouseshape",
13897#endif
13898#if defined(UNIX) || defined(VMS)
13899# ifdef FEAT_MOUSE_DEC
13900 "mouse_dec",
13901# endif
13902# ifdef FEAT_MOUSE_GPM
13903 "mouse_gpm",
13904# endif
13905# ifdef FEAT_MOUSE_JSB
13906 "mouse_jsbterm",
13907# endif
13908# ifdef FEAT_MOUSE_NET
13909 "mouse_netterm",
13910# endif
13911# ifdef FEAT_MOUSE_PTERM
13912 "mouse_pterm",
13913# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013914# ifdef FEAT_MOUSE_SGR
13915 "mouse_sgr",
13916# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013917# ifdef FEAT_SYSMOUSE
13918 "mouse_sysmouse",
13919# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013920# ifdef FEAT_MOUSE_URXVT
13921 "mouse_urxvt",
13922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923# ifdef FEAT_MOUSE_XTERM
13924 "mouse_xterm",
13925# endif
13926#endif
13927#ifdef FEAT_MBYTE
13928 "multi_byte",
13929#endif
13930#ifdef FEAT_MBYTE_IME
13931 "multi_byte_ime",
13932#endif
13933#ifdef FEAT_MULTI_LANG
13934 "multi_lang",
13935#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013936#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013937#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013938 "mzscheme",
13939#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941#ifdef FEAT_OLE
13942 "ole",
13943#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013944 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945#ifdef FEAT_PATH_EXTRA
13946 "path_extra",
13947#endif
13948#ifdef FEAT_PERL
13949#ifndef DYNAMIC_PERL
13950 "perl",
13951#endif
13952#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013953#ifdef FEAT_PERSISTENT_UNDO
13954 "persistent_undo",
13955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956#ifdef FEAT_PYTHON
13957#ifndef DYNAMIC_PYTHON
13958 "python",
13959#endif
13960#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013961#ifdef FEAT_PYTHON3
13962#ifndef DYNAMIC_PYTHON3
13963 "python3",
13964#endif
13965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966#ifdef FEAT_POSTSCRIPT
13967 "postscript",
13968#endif
13969#ifdef FEAT_PRINTER
13970 "printer",
13971#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013972#ifdef FEAT_PROFILE
13973 "profile",
13974#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013975#ifdef FEAT_RELTIME
13976 "reltime",
13977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978#ifdef FEAT_QUICKFIX
13979 "quickfix",
13980#endif
13981#ifdef FEAT_RIGHTLEFT
13982 "rightleft",
13983#endif
13984#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13985 "ruby",
13986#endif
13987#ifdef FEAT_SCROLLBIND
13988 "scrollbind",
13989#endif
13990#ifdef FEAT_CMDL_INFO
13991 "showcmd",
13992 "cmdline_info",
13993#endif
13994#ifdef FEAT_SIGNS
13995 "signs",
13996#endif
13997#ifdef FEAT_SMARTINDENT
13998 "smartindent",
13999#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014000#ifdef STARTUPTIME
14001 "startuptime",
14002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003#ifdef FEAT_STL_OPT
14004 "statusline",
14005#endif
14006#ifdef FEAT_SUN_WORKSHOP
14007 "sun_workshop",
14008#endif
14009#ifdef FEAT_NETBEANS_INTG
14010 "netbeans_intg",
14011#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014012#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014013 "spell",
14014#endif
14015#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 "syntax",
14017#endif
14018#if defined(USE_SYSTEM) || !defined(UNIX)
14019 "system",
14020#endif
14021#ifdef FEAT_TAG_BINS
14022 "tag_binary",
14023#endif
14024#ifdef FEAT_TAG_OLDSTATIC
14025 "tag_old_static",
14026#endif
14027#ifdef FEAT_TAG_ANYWHITE
14028 "tag_any_white",
14029#endif
14030#ifdef FEAT_TCL
14031# ifndef DYNAMIC_TCL
14032 "tcl",
14033# endif
14034#endif
14035#ifdef TERMINFO
14036 "terminfo",
14037#endif
14038#ifdef FEAT_TERMRESPONSE
14039 "termresponse",
14040#endif
14041#ifdef FEAT_TEXTOBJ
14042 "textobjects",
14043#endif
14044#ifdef HAVE_TGETENT
14045 "tgetent",
14046#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014047#ifdef FEAT_TIMERS
14048 "timers",
14049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050#ifdef FEAT_TITLE
14051 "title",
14052#endif
14053#ifdef FEAT_TOOLBAR
14054 "toolbar",
14055#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014056#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14057 "unnamedplus",
14058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059#ifdef FEAT_USR_CMDS
14060 "user-commands", /* was accidentally included in 5.4 */
14061 "user_commands",
14062#endif
14063#ifdef FEAT_VIMINFO
14064 "viminfo",
14065#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014066#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 "vertsplit",
14068#endif
14069#ifdef FEAT_VIRTUALEDIT
14070 "virtualedit",
14071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073#ifdef FEAT_VISUALEXTRA
14074 "visualextra",
14075#endif
14076#ifdef FEAT_VREPLACE
14077 "vreplace",
14078#endif
14079#ifdef FEAT_WILDIGN
14080 "wildignore",
14081#endif
14082#ifdef FEAT_WILDMENU
14083 "wildmenu",
14084#endif
14085#ifdef FEAT_WINDOWS
14086 "windows",
14087#endif
14088#ifdef FEAT_WAK
14089 "winaltkeys",
14090#endif
14091#ifdef FEAT_WRITEBACKUP
14092 "writebackup",
14093#endif
14094#ifdef FEAT_XIM
14095 "xim",
14096#endif
14097#ifdef FEAT_XFONTSET
14098 "xfontset",
14099#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014100#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014101 "xpm",
14102 "xpm_w32", /* for backward compatibility */
14103#else
14104# if defined(HAVE_XPM)
14105 "xpm",
14106# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108#ifdef USE_XSMP
14109 "xsmp",
14110#endif
14111#ifdef USE_XSMP_INTERACT
14112 "xsmp_interact",
14113#endif
14114#ifdef FEAT_XCLIPBOARD
14115 "xterm_clipboard",
14116#endif
14117#ifdef FEAT_XTERM_SAVE
14118 "xterm_save",
14119#endif
14120#if defined(UNIX) && defined(FEAT_X11)
14121 "X11",
14122#endif
14123 NULL
14124 };
14125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014126 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 for (i = 0; has_list[i] != NULL; ++i)
14128 if (STRICMP(name, has_list[i]) == 0)
14129 {
14130 n = TRUE;
14131 break;
14132 }
14133
14134 if (n == FALSE)
14135 {
14136 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014137 {
14138 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014139 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014140 && vim_isdigit(name[6])
14141 && vim_isdigit(name[8])
14142 && vim_isdigit(name[10]))
14143 {
14144 int major = atoi((char *)name + 6);
14145 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014146
14147 /* Expect "patch-9.9.01234". */
14148 n = (major < VIM_VERSION_MAJOR
14149 || (major == VIM_VERSION_MAJOR
14150 && (minor < VIM_VERSION_MINOR
14151 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014152 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014153 }
14154 else
14155 n = has_patch(atoi((char *)name + 5));
14156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 else if (STRICMP(name, "vim_starting") == 0)
14158 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014159#ifdef FEAT_MBYTE
14160 else if (STRICMP(name, "multi_byte_encoding") == 0)
14161 n = has_mbyte;
14162#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014163#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14164 else if (STRICMP(name, "balloon_multiline") == 0)
14165 n = multiline_balloon_available();
14166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167#ifdef DYNAMIC_TCL
14168 else if (STRICMP(name, "tcl") == 0)
14169 n = tcl_enabled(FALSE);
14170#endif
14171#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14172 else if (STRICMP(name, "iconv") == 0)
14173 n = iconv_enabled(FALSE);
14174#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014175#ifdef DYNAMIC_LUA
14176 else if (STRICMP(name, "lua") == 0)
14177 n = lua_enabled(FALSE);
14178#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014179#ifdef DYNAMIC_MZSCHEME
14180 else if (STRICMP(name, "mzscheme") == 0)
14181 n = mzscheme_enabled(FALSE);
14182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183#ifdef DYNAMIC_RUBY
14184 else if (STRICMP(name, "ruby") == 0)
14185 n = ruby_enabled(FALSE);
14186#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014187#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188#ifdef DYNAMIC_PYTHON
14189 else if (STRICMP(name, "python") == 0)
14190 n = python_enabled(FALSE);
14191#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014192#endif
14193#ifdef FEAT_PYTHON3
14194#ifdef DYNAMIC_PYTHON3
14195 else if (STRICMP(name, "python3") == 0)
14196 n = python3_enabled(FALSE);
14197#endif
14198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199#ifdef DYNAMIC_PERL
14200 else if (STRICMP(name, "perl") == 0)
14201 n = perl_enabled(FALSE);
14202#endif
14203#ifdef FEAT_GUI
14204 else if (STRICMP(name, "gui_running") == 0)
14205 n = (gui.in_use || gui.starting);
14206# ifdef FEAT_GUI_W32
14207 else if (STRICMP(name, "gui_win32s") == 0)
14208 n = gui_is_win32s();
14209# endif
14210# ifdef FEAT_BROWSE
14211 else if (STRICMP(name, "browse") == 0)
14212 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14213# endif
14214#endif
14215#ifdef FEAT_SYN_HL
14216 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014217 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218#endif
14219#if defined(WIN3264)
14220 else if (STRICMP(name, "win95") == 0)
14221 n = mch_windows95();
14222#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014223#ifdef FEAT_NETBEANS_INTG
14224 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014225 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 }
14228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014229 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230}
14231
14232/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014233 * "has_key()" function
14234 */
14235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014236f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014237{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014238 if (argvars[0].v_type != VAR_DICT)
14239 {
14240 EMSG(_(e_dictreq));
14241 return;
14242 }
14243 if (argvars[0].vval.v_dict == NULL)
14244 return;
14245
14246 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014247 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014248}
14249
14250/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014251 * "haslocaldir()" function
14252 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014254f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014255{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014256 win_T *wp = NULL;
14257
14258 wp = find_tabwin(&argvars[0], &argvars[1]);
14259 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014260}
14261
14262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263 * "hasmapto()" function
14264 */
14265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014266f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267{
14268 char_u *name;
14269 char_u *mode;
14270 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014271 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014274 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275 mode = (char_u *)"nvo";
14276 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014278 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014279 if (argvars[2].v_type != VAR_UNKNOWN)
14280 abbr = get_tv_number(&argvars[2]);
14281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282
Bram Moolenaar2c932302006-03-18 21:42:09 +000014283 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014284 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287}
14288
14289/*
14290 * "histadd()" function
14291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014293f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294{
14295#ifdef FEAT_CMDHIST
14296 int histype;
14297 char_u *str;
14298 char_u buf[NUMBUFLEN];
14299#endif
14300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014301 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302 if (check_restricted() || check_secure())
14303 return;
14304#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014305 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14306 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 if (histype >= 0)
14308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 if (*str != NUL)
14311 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014312 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 return;
14316 }
14317 }
14318#endif
14319}
14320
14321/*
14322 * "histdel()" function
14323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014325f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326{
14327#ifdef FEAT_CMDHIST
14328 int n;
14329 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014332 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14333 if (str == NULL)
14334 n = 0;
14335 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014337 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014338 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 else
14343 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014344 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345 get_tv_string_buf(&argvars[1], buf));
14346 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347#endif
14348}
14349
14350/*
14351 * "histget()" function
14352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014354f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355{
14356#ifdef FEAT_CMDHIST
14357 int type;
14358 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014359 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14362 if (str == NULL)
14363 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014365 {
14366 type = get_histtype(str);
14367 if (argvars[1].v_type == VAR_UNKNOWN)
14368 idx = get_history_idx(type);
14369 else
14370 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14371 /* -1 on type error */
14372 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378}
14379
14380/*
14381 * "histnr()" function
14382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014384f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385{
14386 int i;
14387
14388#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 char_u *history = get_tv_string_chk(&argvars[0]);
14390
14391 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 if (i >= HIST_CMD && i < HIST_COUNT)
14393 i = get_history_idx(i);
14394 else
14395#endif
14396 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398}
14399
14400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 * "highlightID(name)" function
14402 */
14403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014404f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407}
14408
14409/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014410 * "highlight_exists()" function
14411 */
14412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014413f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014414{
14415 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14416}
14417
14418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 * "hostname()" function
14420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014422f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423{
14424 char_u hostname[256];
14425
14426 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427 rettv->v_type = VAR_STRING;
14428 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429}
14430
14431/*
14432 * iconv() function
14433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014435f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436{
14437#ifdef FEAT_MBYTE
14438 char_u buf1[NUMBUFLEN];
14439 char_u buf2[NUMBUFLEN];
14440 char_u *from, *to, *str;
14441 vimconv_T vimconv;
14442#endif
14443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014444 rettv->v_type = VAR_STRING;
14445 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446
14447#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448 str = get_tv_string(&argvars[0]);
14449 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14450 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 vimconv.vc_type = CONV_NONE;
14452 convert_setup(&vimconv, from, to);
14453
14454 /* If the encodings are equal, no conversion needed. */
14455 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459
14460 convert_setup(&vimconv, NULL, NULL);
14461 vim_free(from);
14462 vim_free(to);
14463#endif
14464}
14465
14466/*
14467 * "indent()" function
14468 */
14469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014470f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471{
14472 linenr_T lnum;
14473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479}
14480
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014481/*
14482 * "index()" function
14483 */
14484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014485f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014486{
Bram Moolenaar33570922005-01-25 22:26:29 +000014487 list_T *l;
14488 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014489 long idx = 0;
14490 int ic = FALSE;
14491
14492 rettv->vval.v_number = -1;
14493 if (argvars[0].v_type != VAR_LIST)
14494 {
14495 EMSG(_(e_listreq));
14496 return;
14497 }
14498 l = argvars[0].vval.v_list;
14499 if (l != NULL)
14500 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014501 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014502 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014504 int error = FALSE;
14505
Bram Moolenaar758711c2005-02-02 23:11:38 +000014506 /* Start at specified item. Use the cached index that list_find()
14507 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014508 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014509 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014510 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 ic = get_tv_number_chk(&argvars[3], &error);
14512 if (error)
14513 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014514 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014515
Bram Moolenaar758711c2005-02-02 23:11:38 +000014516 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014517 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014518 {
14519 rettv->vval.v_number = idx;
14520 break;
14521 }
14522 }
14523}
14524
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525static int inputsecret_flag = 0;
14526
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014527static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014528
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014530 * This function is used by f_input() and f_inputdialog() functions. The third
14531 * argument to f_input() specifies the type of completion to use at the
14532 * prompt. The third argument to f_inputdialog() specifies the value to return
14533 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 */
14535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014536get_user_input(
14537 typval_T *argvars,
14538 typval_T *rettv,
14539 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014541 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 char_u *p = NULL;
14543 int c;
14544 char_u buf[NUMBUFLEN];
14545 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014546 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014547 int xp_type = EXPAND_NOTHING;
14548 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014551 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552
14553#ifdef NO_CONSOLE_INPUT
14554 /* While starting up, there is no place to enter text. */
14555 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557#endif
14558
14559 cmd_silent = FALSE; /* Want to see the prompt. */
14560 if (prompt != NULL)
14561 {
14562 /* Only the part of the message after the last NL is considered as
14563 * prompt for the command line */
14564 p = vim_strrchr(prompt, '\n');
14565 if (p == NULL)
14566 p = prompt;
14567 else
14568 {
14569 ++p;
14570 c = *p;
14571 *p = NUL;
14572 msg_start();
14573 msg_clr_eos();
14574 msg_puts_attr(prompt, echo_attr);
14575 msg_didout = FALSE;
14576 msg_starthere();
14577 *p = c;
14578 }
14579 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014581 if (argvars[1].v_type != VAR_UNKNOWN)
14582 {
14583 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14584 if (defstr != NULL)
14585 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014587 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014588 {
14589 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014590 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014591 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014592
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014593 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014594 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014595
Bram Moolenaar4463f292005-09-25 22:20:24 +000014596 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14597 if (xp_name == NULL)
14598 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014599
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014600 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014601
Bram Moolenaar4463f292005-09-25 22:20:24 +000014602 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14603 &xp_arg) == FAIL)
14604 return;
14605 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014606 }
14607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014608 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014609 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014610 int save_ex_normal_busy = ex_normal_busy;
14611 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014612 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014613 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14614 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014615 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014616 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014617 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014618 && argvars[1].v_type != VAR_UNKNOWN
14619 && argvars[2].v_type != VAR_UNKNOWN)
14620 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14621 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014622
14623 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014625 /* since the user typed this, no need to wait for return */
14626 need_wait_return = FALSE;
14627 msg_didout = FALSE;
14628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629 cmd_silent = cmd_silent_save;
14630}
14631
14632/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014633 * "input()" function
14634 * Also handles inputsecret() when inputsecret is set.
14635 */
14636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014637f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014638{
14639 get_user_input(argvars, rettv, FALSE);
14640}
14641
14642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643 * "inputdialog()" function
14644 */
14645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014646f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647{
14648#if defined(FEAT_GUI_TEXTDIALOG)
14649 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14650 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14651 {
14652 char_u *message;
14653 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 message = get_tv_string_chk(&argvars[0]);
14657 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014658 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014659 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 else
14661 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 if (message != NULL && defstr != NULL
14663 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014664 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 else
14667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 if (message != NULL && defstr != NULL
14669 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014670 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014671 rettv->vval.v_string = vim_strsave(
14672 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014674 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 }
14678 else
14679#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014680 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681}
14682
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014683/*
14684 * "inputlist()" function
14685 */
14686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014687f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014688{
14689 listitem_T *li;
14690 int selected;
14691 int mouse_used;
14692
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014693#ifdef NO_CONSOLE_INPUT
14694 /* While starting up, there is no place to enter text. */
14695 if (no_console_input())
14696 return;
14697#endif
14698 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14699 {
14700 EMSG2(_(e_listarg), "inputlist()");
14701 return;
14702 }
14703
14704 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014705 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014706 lines_left = Rows; /* avoid more prompt */
14707 msg_scroll = TRUE;
14708 msg_clr_eos();
14709
14710 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14711 {
14712 msg_puts(get_tv_string(&li->li_tv));
14713 msg_putchar('\n');
14714 }
14715
14716 /* Ask for choice. */
14717 selected = prompt_for_number(&mouse_used);
14718 if (mouse_used)
14719 selected -= lines_left;
14720
14721 rettv->vval.v_number = selected;
14722}
14723
14724
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14726
14727/*
14728 * "inputrestore()" function
14729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014731f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732{
14733 if (ga_userinput.ga_len > 0)
14734 {
14735 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14737 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014738 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739 }
14740 else if (p_verbose > 1)
14741 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014742 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014743 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 }
14745}
14746
14747/*
14748 * "inputsave()" function
14749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014751f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014753 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 if (ga_grow(&ga_userinput, 1) == OK)
14755 {
14756 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14757 + ga_userinput.ga_len);
14758 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014759 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 }
14761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014762 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763}
14764
14765/*
14766 * "inputsecret()" function
14767 */
14768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014769f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770{
14771 ++cmdline_star;
14772 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014773 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774 --cmdline_star;
14775 --inputsecret_flag;
14776}
14777
14778/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014779 * "insert()" function
14780 */
14781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014782f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014783{
14784 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 listitem_T *item;
14786 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014787 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014788
14789 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014790 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014791 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014792 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014793 {
14794 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014795 before = get_tv_number_chk(&argvars[2], &error);
14796 if (error)
14797 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014798
Bram Moolenaar758711c2005-02-02 23:11:38 +000014799 if (before == l->lv_len)
14800 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014801 else
14802 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014803 item = list_find(l, before);
14804 if (item == NULL)
14805 {
14806 EMSGN(_(e_listidx), before);
14807 l = NULL;
14808 }
14809 }
14810 if (l != NULL)
14811 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014812 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014813 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014814 }
14815 }
14816}
14817
14818/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014819 * "invert(expr)" function
14820 */
14821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014822f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014823{
14824 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14825}
14826
14827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 * "isdirectory()" function
14829 */
14830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014831f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834}
14835
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014836/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014837 * "islocked()" function
14838 */
14839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014840f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014841{
14842 lval_T lv;
14843 char_u *end;
14844 dictitem_T *di;
14845
14846 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014847 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14848 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014849 if (end != NULL && lv.ll_name != NULL)
14850 {
14851 if (*end != NUL)
14852 EMSG(_(e_trailing));
14853 else
14854 {
14855 if (lv.ll_tv == NULL)
14856 {
14857 if (check_changedtick(lv.ll_name))
14858 rettv->vval.v_number = 1; /* always locked */
14859 else
14860 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014861 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014862 if (di != NULL)
14863 {
14864 /* Consider a variable locked when:
14865 * 1. the variable itself is locked
14866 * 2. the value of the variable is locked.
14867 * 3. the List or Dict value is locked.
14868 */
14869 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14870 || tv_islocked(&di->di_tv));
14871 }
14872 }
14873 }
14874 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014875 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014876 else if (lv.ll_newkey != NULL)
14877 EMSG2(_(e_dictkey), lv.ll_newkey);
14878 else if (lv.ll_list != NULL)
14879 /* List item. */
14880 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14881 else
14882 /* Dictionary item. */
14883 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14884 }
14885 }
14886
14887 clear_lval(&lv);
14888}
14889
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014890#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14891/*
14892 * "isnan()" function
14893 */
14894 static void
14895f_isnan(typval_T *argvars, typval_T *rettv)
14896{
14897 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14898 && isnan(argvars[0].vval.v_float);
14899}
14900#endif
14901
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014902static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014903
14904/*
14905 * Turn a dict into a list:
14906 * "what" == 0: list of keys
14907 * "what" == 1: list of values
14908 * "what" == 2: list of items
14909 */
14910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014911dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014912{
Bram Moolenaar33570922005-01-25 22:26:29 +000014913 list_T *l2;
14914 dictitem_T *di;
14915 hashitem_T *hi;
14916 listitem_T *li;
14917 listitem_T *li2;
14918 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014919 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014920
Bram Moolenaar8c711452005-01-14 21:53:12 +000014921 if (argvars[0].v_type != VAR_DICT)
14922 {
14923 EMSG(_(e_dictreq));
14924 return;
14925 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014926 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014927 return;
14928
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014929 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014930 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014931
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014932 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014933 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014934 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014935 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014936 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014937 --todo;
14938 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014939
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014940 li = listitem_alloc();
14941 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014942 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014943 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014944
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014945 if (what == 0)
14946 {
14947 /* keys() */
14948 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014949 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014950 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14951 }
14952 else if (what == 1)
14953 {
14954 /* values() */
14955 copy_tv(&di->di_tv, &li->li_tv);
14956 }
14957 else
14958 {
14959 /* items() */
14960 l2 = list_alloc();
14961 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014962 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014963 li->li_tv.vval.v_list = l2;
14964 if (l2 == NULL)
14965 break;
14966 ++l2->lv_refcount;
14967
14968 li2 = listitem_alloc();
14969 if (li2 == NULL)
14970 break;
14971 list_append(l2, li2);
14972 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014973 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014974 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14975
14976 li2 = listitem_alloc();
14977 if (li2 == NULL)
14978 break;
14979 list_append(l2, li2);
14980 copy_tv(&di->di_tv, &li2->li_tv);
14981 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014982 }
14983 }
14984}
14985
14986/*
14987 * "items(dict)" function
14988 */
14989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014990f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014991{
14992 dict_list(argvars, rettv, 2);
14993}
14994
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014995#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014996/*
14997 * Get the job from the argument.
14998 * Returns NULL if the job is invalid.
14999 */
15000 static job_T *
15001get_job_arg(typval_T *tv)
15002{
15003 job_T *job;
15004
15005 if (tv->v_type != VAR_JOB)
15006 {
15007 EMSG2(_(e_invarg2), get_tv_string(tv));
15008 return NULL;
15009 }
15010 job = tv->vval.v_job;
15011
15012 if (job == NULL)
15013 EMSG(_("E916: not a valid job"));
15014 return job;
15015}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015016
Bram Moolenaar835dc632016-02-07 14:27:38 +010015017/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015018 * "job_getchannel()" function
15019 */
15020 static void
15021f_job_getchannel(typval_T *argvars, typval_T *rettv)
15022{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015023 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015024
Bram Moolenaar65edff82016-02-21 16:40:11 +010015025 if (job != NULL)
15026 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015027 rettv->v_type = VAR_CHANNEL;
15028 rettv->vval.v_channel = job->jv_channel;
15029 if (job->jv_channel != NULL)
15030 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015031 }
15032}
15033
15034/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015035 * "job_info()" function
15036 */
15037 static void
15038f_job_info(typval_T *argvars, typval_T *rettv)
15039{
15040 job_T *job = get_job_arg(&argvars[0]);
15041
15042 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15043 job_info(job, rettv->vval.v_dict);
15044}
15045
15046/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015047 * "job_setoptions()" function
15048 */
15049 static void
15050f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15051{
15052 job_T *job = get_job_arg(&argvars[0]);
15053 jobopt_T opt;
15054
15055 if (job == NULL)
15056 return;
15057 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015058 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15059 job_set_options(job, &opt);
15060 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015061}
15062
15063/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015064 * "job_start()" function
15065 */
15066 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015067f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015068{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015069 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015070 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015071}
15072
15073/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015074 * "job_status()" function
15075 */
15076 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015077f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015078{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015079 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015080
Bram Moolenaar65edff82016-02-21 16:40:11 +010015081 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015082 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015083 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015084 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015085 }
15086}
15087
15088/*
15089 * "job_stop()" function
15090 */
15091 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015092f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015093{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015094 job_T *job = get_job_arg(&argvars[0]);
15095
15096 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015097 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098}
15099#endif
15100
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015102 * "join()" function
15103 */
15104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015105f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015106{
15107 garray_T ga;
15108 char_u *sep;
15109
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015110 if (argvars[0].v_type != VAR_LIST)
15111 {
15112 EMSG(_(e_listreq));
15113 return;
15114 }
15115 if (argvars[0].vval.v_list == NULL)
15116 return;
15117 if (argvars[1].v_type == VAR_UNKNOWN)
15118 sep = (char_u *)" ";
15119 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015120 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015121
15122 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015123
15124 if (sep != NULL)
15125 {
15126 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015127 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015128 ga_append(&ga, NUL);
15129 rettv->vval.v_string = (char_u *)ga.ga_data;
15130 }
15131 else
15132 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015133}
15134
15135/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015136 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015137 */
15138 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015139f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015140{
15141 js_read_T reader;
15142
15143 reader.js_buf = get_tv_string(&argvars[0]);
15144 reader.js_fill = NULL;
15145 reader.js_used = 0;
15146 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15147 EMSG(_(e_invarg));
15148}
15149
15150/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015151 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015152 */
15153 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015154f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015155{
15156 rettv->v_type = VAR_STRING;
15157 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15158}
15159
15160/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015161 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015162 */
15163 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015164f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015165{
15166 js_read_T reader;
15167
15168 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015169 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015170 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015171 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015172 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015173}
15174
15175/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015176 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015177 */
15178 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015179f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015180{
15181 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015182 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015183}
15184
15185/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015186 * "keys()" function
15187 */
15188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015189f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015190{
15191 dict_list(argvars, rettv, 0);
15192}
15193
15194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 * "last_buffer_nr()" function.
15196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015198f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199{
15200 int n = 0;
15201 buf_T *buf;
15202
15203 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15204 if (n < buf->b_fnum)
15205 n = buf->b_fnum;
15206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015208}
15209
15210/*
15211 * "len()" function
15212 */
15213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015214f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015215{
15216 switch (argvars[0].v_type)
15217 {
15218 case VAR_STRING:
15219 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015220 rettv->vval.v_number = (varnumber_T)STRLEN(
15221 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015222 break;
15223 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015224 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015225 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015226 case VAR_DICT:
15227 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15228 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015229 case VAR_UNKNOWN:
15230 case VAR_SPECIAL:
15231 case VAR_FLOAT:
15232 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015233 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015234 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015235 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015236 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015237 break;
15238 }
15239}
15240
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015241static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015242
15243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015244libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015245{
15246#ifdef FEAT_LIBCALL
15247 char_u *string_in;
15248 char_u **string_result;
15249 int nr_result;
15250#endif
15251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015252 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015253 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015254 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015255
15256 if (check_restricted() || check_secure())
15257 return;
15258
15259#ifdef FEAT_LIBCALL
15260 /* The first two args must be strings, otherwise its meaningless */
15261 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15262 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015263 string_in = NULL;
15264 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015265 string_in = argvars[2].vval.v_string;
15266 if (type == VAR_NUMBER)
15267 string_result = NULL;
15268 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270 if (mch_libcall(argvars[0].vval.v_string,
15271 argvars[1].vval.v_string,
15272 string_in,
15273 argvars[2].vval.v_number,
15274 string_result,
15275 &nr_result) == OK
15276 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015277 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015278 }
15279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280}
15281
15282/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015283 * "libcall()" function
15284 */
15285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015286f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015287{
15288 libcall_common(argvars, rettv, VAR_STRING);
15289}
15290
15291/*
15292 * "libcallnr()" function
15293 */
15294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015295f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015296{
15297 libcall_common(argvars, rettv, VAR_NUMBER);
15298}
15299
15300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 * "line(string)" function
15302 */
15303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015304f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305{
15306 linenr_T lnum = 0;
15307 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015308 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015310 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311 if (fp != NULL)
15312 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015313 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314}
15315
15316/*
15317 * "line2byte(lnum)" function
15318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015320f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321{
15322#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015323 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324#else
15325 linenr_T lnum;
15326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015327 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015329 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015331 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15332 if (rettv->vval.v_number >= 0)
15333 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334#endif
15335}
15336
15337/*
15338 * "lispindent(lnum)" function
15339 */
15340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015341f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342{
15343#ifdef FEAT_LISP
15344 pos_T pos;
15345 linenr_T lnum;
15346
15347 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015348 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15350 {
15351 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015352 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 curwin->w_cursor = pos;
15354 }
15355 else
15356#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358}
15359
15360/*
15361 * "localtime()" function
15362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015364f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367}
15368
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015369static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370
15371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015372get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373{
15374 char_u *keys;
15375 char_u *which;
15376 char_u buf[NUMBUFLEN];
15377 char_u *keys_buf = NULL;
15378 char_u *rhs;
15379 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015380 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015381 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015382 mapblock_T *mp;
15383 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384
15385 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386 rettv->v_type = VAR_STRING;
15387 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 if (*keys == NUL)
15391 return;
15392
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015393 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015395 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015396 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015397 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015398 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015399 if (argvars[3].v_type != VAR_UNKNOWN)
15400 get_dict = get_tv_number(&argvars[3]);
15401 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 else
15404 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015405 if (which == NULL)
15406 return;
15407
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 mode = get_map_mode(&which, 0);
15409
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015410 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015411 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015413
15414 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015416 /* Return a string. */
15417 if (rhs != NULL)
15418 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419
Bram Moolenaarbd743252010-10-20 21:23:33 +020015420 }
15421 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15422 {
15423 /* Return a dictionary. */
15424 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15425 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15426 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427
Bram Moolenaarbd743252010-10-20 21:23:33 +020015428 dict_add_nr_str(dict, "lhs", 0L, lhs);
15429 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15430 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15431 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15432 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15433 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15434 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015435 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015436 dict_add_nr_str(dict, "mode", 0L, mapmode);
15437
15438 vim_free(lhs);
15439 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 }
15441}
15442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015443#ifdef FEAT_FLOAT
15444/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015445 * "log()" function
15446 */
15447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015448f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015449{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015450 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015451
15452 rettv->v_type = VAR_FLOAT;
15453 if (get_float_arg(argvars, &f) == OK)
15454 rettv->vval.v_float = log(f);
15455 else
15456 rettv->vval.v_float = 0.0;
15457}
15458
15459/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015460 * "log10()" function
15461 */
15462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015463f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015464{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015465 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015466
15467 rettv->v_type = VAR_FLOAT;
15468 if (get_float_arg(argvars, &f) == OK)
15469 rettv->vval.v_float = log10(f);
15470 else
15471 rettv->vval.v_float = 0.0;
15472}
15473#endif
15474
Bram Moolenaar1dced572012-04-05 16:54:08 +020015475#ifdef FEAT_LUA
15476/*
15477 * "luaeval()" function
15478 */
15479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015480f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015481{
15482 char_u *str;
15483 char_u buf[NUMBUFLEN];
15484
15485 str = get_tv_string_buf(&argvars[0], buf);
15486 do_luaeval(str, argvars + 1, rettv);
15487}
15488#endif
15489
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015491 * "map()" function
15492 */
15493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015494f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015495{
15496 filter_map(argvars, rettv, TRUE);
15497}
15498
15499/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015500 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501 */
15502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015503f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015505 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506}
15507
15508/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015509 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 */
15511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015512f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015514 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515}
15516
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015517static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518
15519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015520find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015522 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015523 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015524 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 char_u *pat;
15526 regmatch_T regmatch;
15527 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015528 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 char_u *save_cpo;
15530 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015531 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015532 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015533 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015534 list_T *l = NULL;
15535 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015536 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015537 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538
15539 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15540 save_cpo = p_cpo;
15541 p_cpo = (char_u *)"";
15542
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015544 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015545 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015546 /* type 3: return empty list when there are no matches.
15547 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015548 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015549 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015550 if (type == 4
15551 && (list_append_string(rettv->vval.v_list,
15552 (char_u *)"", 0) == FAIL
15553 || list_append_number(rettv->vval.v_list,
15554 (varnumber_T)-1) == FAIL
15555 || list_append_number(rettv->vval.v_list,
15556 (varnumber_T)-1) == FAIL
15557 || list_append_number(rettv->vval.v_list,
15558 (varnumber_T)-1) == FAIL))
15559 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015560 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015561 rettv->vval.v_list = NULL;
15562 goto theend;
15563 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015564 }
15565 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567 rettv->v_type = VAR_STRING;
15568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015571 if (argvars[0].v_type == VAR_LIST)
15572 {
15573 if ((l = argvars[0].vval.v_list) == NULL)
15574 goto theend;
15575 li = l->lv_first;
15576 }
15577 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015578 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015579 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015580 len = (long)STRLEN(str);
15581 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15584 if (pat == NULL)
15585 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015586
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015587 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015589 int error = FALSE;
15590
15591 start = get_tv_number_chk(&argvars[2], &error);
15592 if (error)
15593 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015594 if (l != NULL)
15595 {
15596 li = list_find(l, start);
15597 if (li == NULL)
15598 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015599 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015600 }
15601 else
15602 {
15603 if (start < 0)
15604 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015605 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015606 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015607 /* When "count" argument is there ignore matches before "start",
15608 * otherwise skip part of the string. Differs when pattern is "^"
15609 * or "\<". */
15610 if (argvars[3].v_type != VAR_UNKNOWN)
15611 startcol = start;
15612 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015613 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015614 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015615 len -= start;
15616 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015617 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015618
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015619 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015620 nth = get_tv_number_chk(&argvars[3], &error);
15621 if (error)
15622 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 }
15624
15625 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15626 if (regmatch.regprog != NULL)
15627 {
15628 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015629
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015630 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015631 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015632 if (l != NULL)
15633 {
15634 if (li == NULL)
15635 {
15636 match = FALSE;
15637 break;
15638 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015639 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015640 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015641 if (str == NULL)
15642 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015643 }
15644
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015645 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015646
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015647 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015648 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015649 if (l == NULL && !match)
15650 break;
15651
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015652 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015653 if (l != NULL)
15654 {
15655 li = li->li_next;
15656 ++idx;
15657 }
15658 else
15659 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015660#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015661 startcol = (colnr_T)(regmatch.startp[0]
15662 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015663#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015664 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015665#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015666 if (startcol > (colnr_T)len
15667 || str + startcol <= regmatch.startp[0])
15668 {
15669 match = FALSE;
15670 break;
15671 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015672 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015673 }
15674
15675 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015677 if (type == 4)
15678 {
15679 listitem_T *li1 = rettv->vval.v_list->lv_first;
15680 listitem_T *li2 = li1->li_next;
15681 listitem_T *li3 = li2->li_next;
15682 listitem_T *li4 = li3->li_next;
15683
15684 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15685 (int)(regmatch.endp[0] - regmatch.startp[0]));
15686 li3->li_tv.vval.v_number =
15687 (varnumber_T)(regmatch.startp[0] - expr);
15688 li4->li_tv.vval.v_number =
15689 (varnumber_T)(regmatch.endp[0] - expr);
15690 if (l != NULL)
15691 li2->li_tv.vval.v_number = (varnumber_T)idx;
15692 }
15693 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015695 int i;
15696
15697 /* return list with matched string and submatches */
15698 for (i = 0; i < NSUBEXP; ++i)
15699 {
15700 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015701 {
15702 if (list_append_string(rettv->vval.v_list,
15703 (char_u *)"", 0) == FAIL)
15704 break;
15705 }
15706 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015707 regmatch.startp[i],
15708 (int)(regmatch.endp[i] - regmatch.startp[i]))
15709 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015710 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015711 }
15712 }
15713 else if (type == 2)
15714 {
15715 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015716 if (l != NULL)
15717 copy_tv(&li->li_tv, rettv);
15718 else
15719 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015721 }
15722 else if (l != NULL)
15723 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 else
15725 {
15726 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 (varnumber_T)(regmatch.startp[0] - str);
15729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015732 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 }
15734 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015735 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 }
15737
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015738 if (type == 4 && l == NULL)
15739 /* matchstrpos() without a list: drop the second item. */
15740 listitem_remove(rettv->vval.v_list,
15741 rettv->vval.v_list->lv_first->li_next);
15742
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015744 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 p_cpo = save_cpo;
15746}
15747
15748/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749 * "match()" function
15750 */
15751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015752f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753{
15754 find_some_match(argvars, rettv, 1);
15755}
15756
15757/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015758 * "matchadd()" function
15759 */
15760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015761f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015762{
15763#ifdef FEAT_SEARCH_EXTRA
15764 char_u buf[NUMBUFLEN];
15765 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15766 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15767 int prio = 10; /* default priority */
15768 int id = -1;
15769 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015770 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015771
15772 rettv->vval.v_number = -1;
15773
15774 if (grp == NULL || pat == NULL)
15775 return;
15776 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015777 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015778 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015779 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015780 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015781 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015782 if (argvars[4].v_type != VAR_UNKNOWN)
15783 {
15784 if (argvars[4].v_type != VAR_DICT)
15785 {
15786 EMSG(_(e_dictreq));
15787 return;
15788 }
15789 if (dict_find(argvars[4].vval.v_dict,
15790 (char_u *)"conceal", -1) != NULL)
15791 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15792 (char_u *)"conceal", FALSE);
15793 }
15794 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015795 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015796 if (error == TRUE)
15797 return;
15798 if (id >= 1 && id <= 3)
15799 {
15800 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15801 return;
15802 }
15803
Bram Moolenaar6561d522015-07-21 15:48:27 +020015804 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15805 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015806#endif
15807}
15808
15809/*
15810 * "matchaddpos()" function
15811 */
15812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015813f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015814{
15815#ifdef FEAT_SEARCH_EXTRA
15816 char_u buf[NUMBUFLEN];
15817 char_u *group;
15818 int prio = 10;
15819 int id = -1;
15820 int error = FALSE;
15821 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015822 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015823
15824 rettv->vval.v_number = -1;
15825
15826 group = get_tv_string_buf_chk(&argvars[0], buf);
15827 if (group == NULL)
15828 return;
15829
15830 if (argvars[1].v_type != VAR_LIST)
15831 {
15832 EMSG2(_(e_listarg), "matchaddpos()");
15833 return;
15834 }
15835 l = argvars[1].vval.v_list;
15836 if (l == NULL)
15837 return;
15838
15839 if (argvars[2].v_type != VAR_UNKNOWN)
15840 {
15841 prio = get_tv_number_chk(&argvars[2], &error);
15842 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015843 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015844 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015845 if (argvars[4].v_type != VAR_UNKNOWN)
15846 {
15847 if (argvars[4].v_type != VAR_DICT)
15848 {
15849 EMSG(_(e_dictreq));
15850 return;
15851 }
15852 if (dict_find(argvars[4].vval.v_dict,
15853 (char_u *)"conceal", -1) != NULL)
15854 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15855 (char_u *)"conceal", FALSE);
15856 }
15857 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015858 }
15859 if (error == TRUE)
15860 return;
15861
15862 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15863 if (id == 1 || id == 2)
15864 {
15865 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15866 return;
15867 }
15868
Bram Moolenaar6561d522015-07-21 15:48:27 +020015869 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15870 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015871#endif
15872}
15873
15874/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015875 * "matcharg()" function
15876 */
15877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015878f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015879{
15880 if (rettv_list_alloc(rettv) == OK)
15881 {
15882#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015883 int id = get_tv_number(&argvars[0]);
15884 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015885
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015886 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015887 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015888 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15889 {
15890 list_append_string(rettv->vval.v_list,
15891 syn_id2name(m->hlg_id), -1);
15892 list_append_string(rettv->vval.v_list, m->pattern, -1);
15893 }
15894 else
15895 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015896 list_append_string(rettv->vval.v_list, NULL, -1);
15897 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015898 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015899 }
15900#endif
15901 }
15902}
15903
15904/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015905 * "matchdelete()" function
15906 */
15907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015908f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015909{
15910#ifdef FEAT_SEARCH_EXTRA
15911 rettv->vval.v_number = match_delete(curwin,
15912 (int)get_tv_number(&argvars[0]), TRUE);
15913#endif
15914}
15915
15916/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015917 * "matchend()" function
15918 */
15919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015920f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015921{
15922 find_some_match(argvars, rettv, 0);
15923}
15924
15925/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015926 * "matchlist()" function
15927 */
15928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015929f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015930{
15931 find_some_match(argvars, rettv, 3);
15932}
15933
15934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015935 * "matchstr()" function
15936 */
15937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015938f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015939{
15940 find_some_match(argvars, rettv, 2);
15941}
15942
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015943/*
15944 * "matchstrpos()" function
15945 */
15946 static void
15947f_matchstrpos(typval_T *argvars, typval_T *rettv)
15948{
15949 find_some_match(argvars, rettv, 4);
15950}
15951
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015952static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015953
15954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015955max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015956{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015957 long n = 0;
15958 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015959 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015960
15961 if (argvars[0].v_type == VAR_LIST)
15962 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015963 list_T *l;
15964 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015965
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015966 l = argvars[0].vval.v_list;
15967 if (l != NULL)
15968 {
15969 li = l->lv_first;
15970 if (li != NULL)
15971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015972 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015973 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015974 {
15975 li = li->li_next;
15976 if (li == NULL)
15977 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015978 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015979 if (domax ? i > n : i < n)
15980 n = i;
15981 }
15982 }
15983 }
15984 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015985 else if (argvars[0].v_type == VAR_DICT)
15986 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015987 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015988 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015989 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015990 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015991
15992 d = argvars[0].vval.v_dict;
15993 if (d != NULL)
15994 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015995 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015996 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015997 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015998 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015999 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016000 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016001 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016002 if (first)
16003 {
16004 n = i;
16005 first = FALSE;
16006 }
16007 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016008 n = i;
16009 }
16010 }
16011 }
16012 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016013 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016014 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016015 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016016}
16017
16018/*
16019 * "max()" function
16020 */
16021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016022f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016023{
16024 max_min(argvars, rettv, TRUE);
16025}
16026
16027/*
16028 * "min()" function
16029 */
16030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016031f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016032{
16033 max_min(argvars, rettv, FALSE);
16034}
16035
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016036static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016037
16038/*
16039 * Create the directory in which "dir" is located, and higher levels when
16040 * needed.
16041 */
16042 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016043mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016044{
16045 char_u *p;
16046 char_u *updir;
16047 int r = FAIL;
16048
16049 /* Get end of directory name in "dir".
16050 * We're done when it's "/" or "c:/". */
16051 p = gettail_sep(dir);
16052 if (p <= get_past_head(dir))
16053 return OK;
16054
16055 /* If the directory exists we're done. Otherwise: create it.*/
16056 updir = vim_strnsave(dir, (int)(p - dir));
16057 if (updir == NULL)
16058 return FAIL;
16059 if (mch_isdir(updir))
16060 r = OK;
16061 else if (mkdir_recurse(updir, prot) == OK)
16062 r = vim_mkdir_emsg(updir, prot);
16063 vim_free(updir);
16064 return r;
16065}
16066
16067#ifdef vim_mkdir
16068/*
16069 * "mkdir()" function
16070 */
16071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016072f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016073{
16074 char_u *dir;
16075 char_u buf[NUMBUFLEN];
16076 int prot = 0755;
16077
16078 rettv->vval.v_number = FAIL;
16079 if (check_restricted() || check_secure())
16080 return;
16081
16082 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016083 if (*dir == NUL)
16084 rettv->vval.v_number = FAIL;
16085 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016086 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016087 if (*gettail(dir) == NUL)
16088 /* remove trailing slashes */
16089 *gettail_sep(dir) = NUL;
16090
16091 if (argvars[1].v_type != VAR_UNKNOWN)
16092 {
16093 if (argvars[2].v_type != VAR_UNKNOWN)
16094 prot = get_tv_number_chk(&argvars[2], NULL);
16095 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16096 mkdir_recurse(dir, prot);
16097 }
16098 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016099 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016100}
16101#endif
16102
Bram Moolenaar0d660222005-01-07 21:51:51 +000016103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 * "mode()" function
16105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016107f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016109 char_u buf[3];
16110
16111 buf[1] = NUL;
16112 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 if (VIsual_active)
16115 {
16116 if (VIsual_select)
16117 buf[0] = VIsual_mode + 's' - 'v';
16118 else
16119 buf[0] = VIsual_mode;
16120 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016121 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016122 || State == CONFIRM)
16123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016125 if (State == ASKMORE)
16126 buf[1] = 'm';
16127 else if (State == CONFIRM)
16128 buf[1] = '?';
16129 }
16130 else if (State == EXTERNCMD)
16131 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 else if (State & INSERT)
16133 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016134#ifdef FEAT_VREPLACE
16135 if (State & VREPLACE_FLAG)
16136 {
16137 buf[0] = 'R';
16138 buf[1] = 'v';
16139 }
16140 else
16141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 if (State & REPLACE_FLAG)
16143 buf[0] = 'R';
16144 else
16145 buf[0] = 'i';
16146 }
16147 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016150 if (exmode_active)
16151 buf[1] = 'v';
16152 }
16153 else if (exmode_active)
16154 {
16155 buf[0] = 'c';
16156 buf[1] = 'e';
16157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016161 if (finish_op)
16162 buf[1] = 'o';
16163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016165 /* Clear out the minor mode when the argument is not a non-zero number or
16166 * non-empty string. */
16167 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016168 buf[1] = NUL;
16169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170 rettv->vval.v_string = vim_strsave(buf);
16171 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172}
16173
Bram Moolenaar429fa852013-04-15 12:27:36 +020016174#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016175/*
16176 * "mzeval()" function
16177 */
16178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016179f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016180{
16181 char_u *str;
16182 char_u buf[NUMBUFLEN];
16183
16184 str = get_tv_string_buf(&argvars[0], buf);
16185 do_mzeval(str, rettv);
16186}
Bram Moolenaar75676462013-01-30 14:55:42 +010016187
16188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016189mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016190{
16191 typval_T argvars[3];
16192
16193 argvars[0].v_type = VAR_STRING;
16194 argvars[0].vval.v_string = name;
16195 copy_tv(args, &argvars[1]);
16196 argvars[2].v_type = VAR_UNKNOWN;
16197 f_call(argvars, rettv);
16198 clear_tv(&argvars[1]);
16199}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016200#endif
16201
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016203 * "nextnonblank()" function
16204 */
16205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016206f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016207{
16208 linenr_T lnum;
16209
16210 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016212 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016213 {
16214 lnum = 0;
16215 break;
16216 }
16217 if (*skipwhite(ml_get(lnum)) != NUL)
16218 break;
16219 }
16220 rettv->vval.v_number = lnum;
16221}
16222
16223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 * "nr2char()" function
16225 */
16226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016227f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228{
16229 char_u buf[NUMBUFLEN];
16230
16231#ifdef FEAT_MBYTE
16232 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016233 {
16234 int utf8 = 0;
16235
16236 if (argvars[1].v_type != VAR_UNKNOWN)
16237 utf8 = get_tv_number_chk(&argvars[1], NULL);
16238 if (utf8)
16239 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16240 else
16241 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 else
16244#endif
16245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 buf[1] = NUL;
16248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016249 rettv->v_type = VAR_STRING;
16250 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016251}
16252
16253/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016254 * "or(expr, expr)" function
16255 */
16256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016257f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016258{
16259 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16260 | get_tv_number_chk(&argvars[1], NULL);
16261}
16262
16263/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016264 * "pathshorten()" function
16265 */
16266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016267f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016268{
16269 char_u *p;
16270
16271 rettv->v_type = VAR_STRING;
16272 p = get_tv_string_chk(&argvars[0]);
16273 if (p == NULL)
16274 rettv->vval.v_string = NULL;
16275 else
16276 {
16277 p = vim_strsave(p);
16278 rettv->vval.v_string = p;
16279 if (p != NULL)
16280 shorten_dir(p);
16281 }
16282}
16283
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016284#ifdef FEAT_PERL
16285/*
16286 * "perleval()" function
16287 */
16288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016289f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016290{
16291 char_u *str;
16292 char_u buf[NUMBUFLEN];
16293
16294 str = get_tv_string_buf(&argvars[0], buf);
16295 do_perleval(str, rettv);
16296}
16297#endif
16298
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016299#ifdef FEAT_FLOAT
16300/*
16301 * "pow()" function
16302 */
16303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016304f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016305{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016306 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016307
16308 rettv->v_type = VAR_FLOAT;
16309 if (get_float_arg(argvars, &fx) == OK
16310 && get_float_arg(&argvars[1], &fy) == OK)
16311 rettv->vval.v_float = pow(fx, fy);
16312 else
16313 rettv->vval.v_float = 0.0;
16314}
16315#endif
16316
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016317/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016318 * "prevnonblank()" function
16319 */
16320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016321f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016322{
16323 linenr_T lnum;
16324
16325 lnum = get_tv_lnum(argvars);
16326 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16327 lnum = 0;
16328 else
16329 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16330 --lnum;
16331 rettv->vval.v_number = lnum;
16332}
16333
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016334/* This dummy va_list is here because:
16335 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16336 * - locally in the function results in a "used before set" warning
16337 * - using va_start() to initialize it gives "function with fixed args" error */
16338static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016339
Bram Moolenaar8c711452005-01-14 21:53:12 +000016340/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016341 * "printf()" function
16342 */
16343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016344f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016345{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016346 char_u buf[NUMBUFLEN];
16347 int len;
16348 char_u *s;
16349 int saved_did_emsg = did_emsg;
16350 char *fmt;
16351
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016352 rettv->v_type = VAR_STRING;
16353 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016354
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016355 /* Get the required length, allocate the buffer and do it for real. */
16356 did_emsg = FALSE;
16357 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16358 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16359 if (!did_emsg)
16360 {
16361 s = alloc(len + 1);
16362 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016363 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016364 rettv->vval.v_string = s;
16365 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016366 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016367 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016368 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016369}
16370
16371/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016372 * "pumvisible()" function
16373 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016375f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016376{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016377#ifdef FEAT_INS_EXPAND
16378 if (pum_visible())
16379 rettv->vval.v_number = 1;
16380#endif
16381}
16382
Bram Moolenaardb913952012-06-29 12:54:53 +020016383#ifdef FEAT_PYTHON3
16384/*
16385 * "py3eval()" function
16386 */
16387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016388f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016389{
16390 char_u *str;
16391 char_u buf[NUMBUFLEN];
16392
16393 str = get_tv_string_buf(&argvars[0], buf);
16394 do_py3eval(str, rettv);
16395}
16396#endif
16397
16398#ifdef FEAT_PYTHON
16399/*
16400 * "pyeval()" function
16401 */
16402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016403f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016404{
16405 char_u *str;
16406 char_u buf[NUMBUFLEN];
16407
16408 str = get_tv_string_buf(&argvars[0], buf);
16409 do_pyeval(str, rettv);
16410}
16411#endif
16412
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016413/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016414 * "range()" function
16415 */
16416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016417f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016418{
16419 long start;
16420 long end;
16421 long stride = 1;
16422 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016423 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016425 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016426 if (argvars[1].v_type == VAR_UNKNOWN)
16427 {
16428 end = start - 1;
16429 start = 0;
16430 }
16431 else
16432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016433 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016434 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016435 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016436 }
16437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016438 if (error)
16439 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016440 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016441 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016442 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016443 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016444 else
16445 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016446 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016447 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016448 if (list_append_number(rettv->vval.v_list,
16449 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016450 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016451 }
16452}
16453
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016454/*
16455 * "readfile()" function
16456 */
16457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016458f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016459{
16460 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016461 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016462 char_u *fname;
16463 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016464 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16465 int io_size = sizeof(buf);
16466 int readlen; /* size of last fread() */
16467 char_u *prev = NULL; /* previously read bytes, if any */
16468 long prevlen = 0; /* length of data in prev */
16469 long prevsize = 0; /* size of prev buffer */
16470 long maxline = MAXLNUM;
16471 long cnt = 0;
16472 char_u *p; /* position in buf */
16473 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016474
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016475 if (argvars[1].v_type != VAR_UNKNOWN)
16476 {
16477 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16478 binary = TRUE;
16479 if (argvars[2].v_type != VAR_UNKNOWN)
16480 maxline = get_tv_number(&argvars[2]);
16481 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016482
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016483 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016484 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016485
16486 /* Always open the file in binary mode, library functions have a mind of
16487 * their own about CR-LF conversion. */
16488 fname = get_tv_string(&argvars[0]);
16489 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16490 {
16491 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16492 return;
16493 }
16494
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016495 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016496 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016497 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016498
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016499 /* This for loop processes what was read, but is also entered at end
16500 * of file so that either:
16501 * - an incomplete line gets written
16502 * - a "binary" file gets an empty line at the end if it ends in a
16503 * newline. */
16504 for (p = buf, start = buf;
16505 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16506 ++p)
16507 {
16508 if (*p == '\n' || readlen <= 0)
16509 {
16510 listitem_T *li;
16511 char_u *s = NULL;
16512 long_u len = p - start;
16513
16514 /* Finished a line. Remove CRs before NL. */
16515 if (readlen > 0 && !binary)
16516 {
16517 while (len > 0 && start[len - 1] == '\r')
16518 --len;
16519 /* removal may cross back to the "prev" string */
16520 if (len == 0)
16521 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16522 --prevlen;
16523 }
16524 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016525 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016526 else
16527 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016528 /* Change "prev" buffer to be the right size. This way
16529 * the bytes are only copied once, and very long lines are
16530 * allocated only once. */
16531 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016532 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016533 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016534 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016535 prev = NULL; /* the list will own the string */
16536 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016537 }
16538 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016539 if (s == NULL)
16540 {
16541 do_outofmem_msg((long_u) prevlen + len + 1);
16542 failed = TRUE;
16543 break;
16544 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016545
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016546 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016547 {
16548 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016549 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016550 break;
16551 }
16552 li->li_tv.v_type = VAR_STRING;
16553 li->li_tv.v_lock = 0;
16554 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016555 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016557 start = p + 1; /* step over newline */
16558 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016559 break;
16560 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016561 else if (*p == NUL)
16562 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016563#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016564 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16565 * when finding the BF and check the previous two bytes. */
16566 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016567 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016568 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16569 * + 1, these may be in the "prev" string. */
16570 char_u back1 = p >= buf + 1 ? p[-1]
16571 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16572 char_u back2 = p >= buf + 2 ? p[-2]
16573 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16574 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016575
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016576 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016577 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016578 char_u *dest = p - 2;
16579
16580 /* Usually a BOM is at the beginning of a file, and so at
16581 * the beginning of a line; then we can just step over it.
16582 */
16583 if (start == dest)
16584 start = p + 1;
16585 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016586 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016587 /* have to shuffle buf to close gap */
16588 int adjust_prevlen = 0;
16589
16590 if (dest < buf)
16591 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016592 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016593 dest = buf;
16594 }
16595 if (readlen > p - buf + 1)
16596 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16597 readlen -= 3 - adjust_prevlen;
16598 prevlen -= adjust_prevlen;
16599 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016600 }
16601 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016602 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016603#endif
16604 } /* for */
16605
16606 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16607 break;
16608 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016609 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016610 /* There's part of a line in buf, store it in "prev". */
16611 if (p - start + prevlen >= prevsize)
16612 {
16613 /* need bigger "prev" buffer */
16614 char_u *newprev;
16615
16616 /* A common use case is ordinary text files and "prev" gets a
16617 * fragment of a line, so the first allocation is made
16618 * small, to avoid repeatedly 'allocing' large and
16619 * 'reallocing' small. */
16620 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016621 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016622 else
16623 {
16624 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016625 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016626 prevsize = grow50pc > growmin ? grow50pc : growmin;
16627 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016628 newprev = prev == NULL ? alloc(prevsize)
16629 : vim_realloc(prev, prevsize);
16630 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016631 {
16632 do_outofmem_msg((long_u)prevsize);
16633 failed = TRUE;
16634 break;
16635 }
16636 prev = newprev;
16637 }
16638 /* Add the line part to end of "prev". */
16639 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016640 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016641 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016642 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016643
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016644 /*
16645 * For a negative line count use only the lines at the end of the file,
16646 * free the rest.
16647 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016648 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016649 while (cnt > -maxline)
16650 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016651 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016652 --cnt;
16653 }
16654
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016655 if (failed)
16656 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016657 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016658 /* readfile doc says an empty list is returned on error */
16659 rettv->vval.v_list = list_alloc();
16660 }
16661
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016662 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016663 fclose(fd);
16664}
16665
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016666#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016667static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016668
16669/*
16670 * Convert a List to proftime_T.
16671 * Return FAIL when there is something wrong.
16672 */
16673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016674list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016675{
16676 long n1, n2;
16677 int error = FALSE;
16678
16679 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16680 || arg->vval.v_list->lv_len != 2)
16681 return FAIL;
16682 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16683 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16684# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016685 tm->HighPart = n1;
16686 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016687# else
16688 tm->tv_sec = n1;
16689 tm->tv_usec = n2;
16690# endif
16691 return error ? FAIL : OK;
16692}
16693#endif /* FEAT_RELTIME */
16694
16695/*
16696 * "reltime()" function
16697 */
16698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016699f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016700{
16701#ifdef FEAT_RELTIME
16702 proftime_T res;
16703 proftime_T start;
16704
16705 if (argvars[0].v_type == VAR_UNKNOWN)
16706 {
16707 /* No arguments: get current time. */
16708 profile_start(&res);
16709 }
16710 else if (argvars[1].v_type == VAR_UNKNOWN)
16711 {
16712 if (list2proftime(&argvars[0], &res) == FAIL)
16713 return;
16714 profile_end(&res);
16715 }
16716 else
16717 {
16718 /* Two arguments: compute the difference. */
16719 if (list2proftime(&argvars[0], &start) == FAIL
16720 || list2proftime(&argvars[1], &res) == FAIL)
16721 return;
16722 profile_sub(&res, &start);
16723 }
16724
16725 if (rettv_list_alloc(rettv) == OK)
16726 {
16727 long n1, n2;
16728
16729# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016730 n1 = res.HighPart;
16731 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016732# else
16733 n1 = res.tv_sec;
16734 n2 = res.tv_usec;
16735# endif
16736 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16737 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16738 }
16739#endif
16740}
16741
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016742#ifdef FEAT_FLOAT
16743/*
16744 * "reltimefloat()" function
16745 */
16746 static void
16747f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16748{
16749# ifdef FEAT_RELTIME
16750 proftime_T tm;
16751# endif
16752
16753 rettv->v_type = VAR_FLOAT;
16754 rettv->vval.v_float = 0;
16755# ifdef FEAT_RELTIME
16756 if (list2proftime(&argvars[0], &tm) == OK)
16757 rettv->vval.v_float = profile_float(&tm);
16758# endif
16759}
16760#endif
16761
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016762/*
16763 * "reltimestr()" function
16764 */
16765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016766f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016767{
16768#ifdef FEAT_RELTIME
16769 proftime_T tm;
16770#endif
16771
16772 rettv->v_type = VAR_STRING;
16773 rettv->vval.v_string = NULL;
16774#ifdef FEAT_RELTIME
16775 if (list2proftime(&argvars[0], &tm) == OK)
16776 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16777#endif
16778}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016779
Bram Moolenaar0d660222005-01-07 21:51:51 +000016780#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016781static void make_connection(void);
16782static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783
16784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016785make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016786{
16787 if (X_DISPLAY == NULL
16788# ifdef FEAT_GUI
16789 && !gui.in_use
16790# endif
16791 )
16792 {
16793 x_force_connect = TRUE;
16794 setup_term_clip();
16795 x_force_connect = FALSE;
16796 }
16797}
16798
16799 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016800check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801{
16802 make_connection();
16803 if (X_DISPLAY == NULL)
16804 {
16805 EMSG(_("E240: No connection to Vim server"));
16806 return FAIL;
16807 }
16808 return OK;
16809}
16810#endif
16811
16812#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016813static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016814
16815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016816remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016817{
16818 char_u *server_name;
16819 char_u *keys;
16820 char_u *r = NULL;
16821 char_u buf[NUMBUFLEN];
16822# ifdef WIN32
16823 HWND w;
16824# else
16825 Window w;
16826# endif
16827
16828 if (check_restricted() || check_secure())
16829 return;
16830
16831# ifdef FEAT_X11
16832 if (check_connection() == FAIL)
16833 return;
16834# endif
16835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016836 server_name = get_tv_string_chk(&argvars[0]);
16837 if (server_name == NULL)
16838 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016839 keys = get_tv_string_buf(&argvars[1], buf);
16840# ifdef WIN32
16841 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16842# else
16843 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16844 < 0)
16845# endif
16846 {
16847 if (r != NULL)
16848 EMSG(r); /* sending worked but evaluation failed */
16849 else
16850 EMSG2(_("E241: Unable to send to %s"), server_name);
16851 return;
16852 }
16853
16854 rettv->vval.v_string = r;
16855
16856 if (argvars[2].v_type != VAR_UNKNOWN)
16857 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016858 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016859 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016860 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016861
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016862 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 v.di_tv.v_type = VAR_STRING;
16864 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016865 idvar = get_tv_string_chk(&argvars[2]);
16866 if (idvar != NULL)
16867 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016868 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016869 }
16870}
16871#endif
16872
16873/*
16874 * "remote_expr()" function
16875 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016877f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878{
16879 rettv->v_type = VAR_STRING;
16880 rettv->vval.v_string = NULL;
16881#ifdef FEAT_CLIENTSERVER
16882 remote_common(argvars, rettv, TRUE);
16883#endif
16884}
16885
16886/*
16887 * "remote_foreground()" function
16888 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016890f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016892#ifdef FEAT_CLIENTSERVER
16893# ifdef WIN32
16894 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016895 {
16896 char_u *server_name = get_tv_string_chk(&argvars[0]);
16897
16898 if (server_name != NULL)
16899 serverForeground(server_name);
16900 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016901# else
16902 /* Send a foreground() expression to the server. */
16903 argvars[1].v_type = VAR_STRING;
16904 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16905 argvars[2].v_type = VAR_UNKNOWN;
16906 remote_common(argvars, rettv, TRUE);
16907 vim_free(argvars[1].vval.v_string);
16908# endif
16909#endif
16910}
16911
Bram Moolenaar0d660222005-01-07 21:51:51 +000016912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016913f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914{
16915#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917 char_u *s = NULL;
16918# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016919 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016921 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922
16923 if (check_restricted() || check_secure())
16924 {
16925 rettv->vval.v_number = -1;
16926 return;
16927 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016928 serverid = get_tv_string_chk(&argvars[0]);
16929 if (serverid == NULL)
16930 {
16931 rettv->vval.v_number = -1;
16932 return; /* type error; errmsg already given */
16933 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016935 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936 if (n == 0)
16937 rettv->vval.v_number = -1;
16938 else
16939 {
16940 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16941 rettv->vval.v_number = (s != NULL);
16942 }
16943# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944 if (check_connection() == FAIL)
16945 return;
16946
16947 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016948 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949# endif
16950
16951 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016953 char_u *retvar;
16954
Bram Moolenaar33570922005-01-25 22:26:29 +000016955 v.di_tv.v_type = VAR_STRING;
16956 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016957 retvar = get_tv_string_chk(&argvars[1]);
16958 if (retvar != NULL)
16959 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016960 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016961 }
16962#else
16963 rettv->vval.v_number = -1;
16964#endif
16965}
16966
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016968f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969{
16970 char_u *r = NULL;
16971
16972#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016973 char_u *serverid = get_tv_string_chk(&argvars[0]);
16974
16975 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976 {
16977# ifdef WIN32
16978 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016979 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016981 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 if (n != 0)
16983 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16984 if (r == NULL)
16985# else
16986 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016987 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988# endif
16989 EMSG(_("E277: Unable to read a server reply"));
16990 }
16991#endif
16992 rettv->v_type = VAR_STRING;
16993 rettv->vval.v_string = r;
16994}
16995
16996/*
16997 * "remote_send()" function
16998 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017000f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001{
17002 rettv->v_type = VAR_STRING;
17003 rettv->vval.v_string = NULL;
17004#ifdef FEAT_CLIENTSERVER
17005 remote_common(argvars, rettv, FALSE);
17006#endif
17007}
17008
17009/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017010 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017011 */
17012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017013f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017014{
Bram Moolenaar33570922005-01-25 22:26:29 +000017015 list_T *l;
17016 listitem_T *item, *item2;
17017 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017018 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017019 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017020 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 dict_T *d;
17022 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017023 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017024
Bram Moolenaar8c711452005-01-14 21:53:12 +000017025 if (argvars[0].v_type == VAR_DICT)
17026 {
17027 if (argvars[2].v_type != VAR_UNKNOWN)
17028 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017029 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017030 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017032 key = get_tv_string_chk(&argvars[1]);
17033 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017034 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017035 di = dict_find(d, key, -1);
17036 if (di == NULL)
17037 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017038 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17039 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017040 {
17041 *rettv = di->di_tv;
17042 init_tv(&di->di_tv);
17043 dictitem_remove(d, di);
17044 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017045 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017046 }
17047 }
17048 else if (argvars[0].v_type != VAR_LIST)
17049 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017050 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017051 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017052 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 int error = FALSE;
17054
17055 idx = get_tv_number_chk(&argvars[1], &error);
17056 if (error)
17057 ; /* type error: do nothing, errmsg already given */
17058 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017059 EMSGN(_(e_listidx), idx);
17060 else
17061 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017062 if (argvars[2].v_type == VAR_UNKNOWN)
17063 {
17064 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017065 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017066 *rettv = item->li_tv;
17067 vim_free(item);
17068 }
17069 else
17070 {
17071 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017072 end = get_tv_number_chk(&argvars[2], &error);
17073 if (error)
17074 ; /* type error: do nothing */
17075 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017076 EMSGN(_(e_listidx), end);
17077 else
17078 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017079 int cnt = 0;
17080
17081 for (li = item; li != NULL; li = li->li_next)
17082 {
17083 ++cnt;
17084 if (li == item2)
17085 break;
17086 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017087 if (li == NULL) /* didn't find "item2" after "item" */
17088 EMSG(_(e_invrange));
17089 else
17090 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017091 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017092 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017093 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017094 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017095 l->lv_first = item;
17096 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017097 item->li_prev = NULL;
17098 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017099 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017100 }
17101 }
17102 }
17103 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017104 }
17105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106}
17107
17108/*
17109 * "rename({from}, {to})" function
17110 */
17111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017112f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113{
17114 char_u buf[NUMBUFLEN];
17115
17116 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017117 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17120 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121}
17122
17123/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017124 * "repeat()" function
17125 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017127f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017128{
17129 char_u *p;
17130 int n;
17131 int slen;
17132 int len;
17133 char_u *r;
17134 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017135
17136 n = get_tv_number(&argvars[1]);
17137 if (argvars[0].v_type == VAR_LIST)
17138 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017139 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017140 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017141 if (list_extend(rettv->vval.v_list,
17142 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017143 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017144 }
17145 else
17146 {
17147 p = get_tv_string(&argvars[0]);
17148 rettv->v_type = VAR_STRING;
17149 rettv->vval.v_string = NULL;
17150
17151 slen = (int)STRLEN(p);
17152 len = slen * n;
17153 if (len <= 0)
17154 return;
17155
17156 r = alloc(len + 1);
17157 if (r != NULL)
17158 {
17159 for (i = 0; i < n; i++)
17160 mch_memmove(r + i * slen, p, (size_t)slen);
17161 r[len] = NUL;
17162 }
17163
17164 rettv->vval.v_string = r;
17165 }
17166}
17167
17168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 * "resolve()" function
17170 */
17171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017172f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173{
17174 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017175#ifdef HAVE_READLINK
17176 char_u *buf = NULL;
17177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017179 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180#ifdef FEAT_SHORTCUT
17181 {
17182 char_u *v = NULL;
17183
17184 v = mch_resolve_shortcut(p);
17185 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017186 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 }
17190#else
17191# ifdef HAVE_READLINK
17192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 char_u *cpy;
17194 int len;
17195 char_u *remain = NULL;
17196 char_u *q;
17197 int is_relative_to_current = FALSE;
17198 int has_trailing_pathsep = FALSE;
17199 int limit = 100;
17200
17201 p = vim_strsave(p);
17202
17203 if (p[0] == '.' && (vim_ispathsep(p[1])
17204 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17205 is_relative_to_current = TRUE;
17206
17207 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017208 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017211 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213
17214 q = getnextcomp(p);
17215 if (*q != NUL)
17216 {
17217 /* Separate the first path component in "p", and keep the
17218 * remainder (beginning with the path separator). */
17219 remain = vim_strsave(q - 1);
17220 q[-1] = NUL;
17221 }
17222
Bram Moolenaard9462e32011-04-11 21:35:11 +020017223 buf = alloc(MAXPATHL + 1);
17224 if (buf == NULL)
17225 goto fail;
17226
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227 for (;;)
17228 {
17229 for (;;)
17230 {
17231 len = readlink((char *)p, (char *)buf, MAXPATHL);
17232 if (len <= 0)
17233 break;
17234 buf[len] = NUL;
17235
17236 if (limit-- == 0)
17237 {
17238 vim_free(p);
17239 vim_free(remain);
17240 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 goto fail;
17243 }
17244
17245 /* Ensure that the result will have a trailing path separator
17246 * if the argument has one. */
17247 if (remain == NULL && has_trailing_pathsep)
17248 add_pathsep(buf);
17249
17250 /* Separate the first path component in the link value and
17251 * concatenate the remainders. */
17252 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17253 if (*q != NUL)
17254 {
17255 if (remain == NULL)
17256 remain = vim_strsave(q - 1);
17257 else
17258 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017259 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 if (cpy != NULL)
17261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 vim_free(remain);
17263 remain = cpy;
17264 }
17265 }
17266 q[-1] = NUL;
17267 }
17268
17269 q = gettail(p);
17270 if (q > p && *q == NUL)
17271 {
17272 /* Ignore trailing path separator. */
17273 q[-1] = NUL;
17274 q = gettail(p);
17275 }
17276 if (q > p && !mch_isFullName(buf))
17277 {
17278 /* symlink is relative to directory of argument */
17279 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17280 if (cpy != NULL)
17281 {
17282 STRCPY(cpy, p);
17283 STRCPY(gettail(cpy), buf);
17284 vim_free(p);
17285 p = cpy;
17286 }
17287 }
17288 else
17289 {
17290 vim_free(p);
17291 p = vim_strsave(buf);
17292 }
17293 }
17294
17295 if (remain == NULL)
17296 break;
17297
17298 /* Append the first path component of "remain" to "p". */
17299 q = getnextcomp(remain + 1);
17300 len = q - remain - (*q != NUL);
17301 cpy = vim_strnsave(p, STRLEN(p) + len);
17302 if (cpy != NULL)
17303 {
17304 STRNCAT(cpy, remain, len);
17305 vim_free(p);
17306 p = cpy;
17307 }
17308 /* Shorten "remain". */
17309 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017310 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 else
17312 {
17313 vim_free(remain);
17314 remain = NULL;
17315 }
17316 }
17317
17318 /* If the result is a relative path name, make it explicitly relative to
17319 * the current directory if and only if the argument had this form. */
17320 if (!vim_ispathsep(*p))
17321 {
17322 if (is_relative_to_current
17323 && *p != NUL
17324 && !(p[0] == '.'
17325 && (p[1] == NUL
17326 || vim_ispathsep(p[1])
17327 || (p[1] == '.'
17328 && (p[2] == NUL
17329 || vim_ispathsep(p[2]))))))
17330 {
17331 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017332 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 if (cpy != NULL)
17334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 vim_free(p);
17336 p = cpy;
17337 }
17338 }
17339 else if (!is_relative_to_current)
17340 {
17341 /* Strip leading "./". */
17342 q = p;
17343 while (q[0] == '.' && vim_ispathsep(q[1]))
17344 q += 2;
17345 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017346 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 }
17348 }
17349
17350 /* Ensure that the result will have no trailing path separator
17351 * if the argument had none. But keep "/" or "//". */
17352 if (!has_trailing_pathsep)
17353 {
17354 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017355 if (after_pathsep(p, q))
17356 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 }
17358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 }
17361# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017362 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363# endif
17364#endif
17365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367
17368#ifdef HAVE_READLINK
17369fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017370 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017372 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373}
17374
17375/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017376 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 */
17378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017379f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380{
Bram Moolenaar33570922005-01-25 22:26:29 +000017381 list_T *l;
17382 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383
Bram Moolenaar0d660222005-01-07 21:51:51 +000017384 if (argvars[0].v_type != VAR_LIST)
17385 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017386 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017387 && !tv_check_lock(l->lv_lock,
17388 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017389 {
17390 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017391 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017392 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017393 while (li != NULL)
17394 {
17395 ni = li->li_prev;
17396 list_append(l, li);
17397 li = ni;
17398 }
17399 rettv->vval.v_list = l;
17400 rettv->v_type = VAR_LIST;
17401 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017402 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404}
17405
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017406#define SP_NOMOVE 0x01 /* don't move cursor */
17407#define SP_REPEAT 0x02 /* repeat to find outer pair */
17408#define SP_RETCOUNT 0x04 /* return matchcount */
17409#define SP_SETPCMARK 0x08 /* set previous context mark */
17410#define SP_START 0x10 /* accept match at start position */
17411#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17412#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017413#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017414
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017415static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017416
17417/*
17418 * Get flags for a search function.
17419 * Possibly sets "p_ws".
17420 * Returns BACKWARD, FORWARD or zero (for an error).
17421 */
17422 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017423get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017424{
17425 int dir = FORWARD;
17426 char_u *flags;
17427 char_u nbuf[NUMBUFLEN];
17428 int mask;
17429
17430 if (varp->v_type != VAR_UNKNOWN)
17431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017432 flags = get_tv_string_buf_chk(varp, nbuf);
17433 if (flags == NULL)
17434 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017435 while (*flags != NUL)
17436 {
17437 switch (*flags)
17438 {
17439 case 'b': dir = BACKWARD; break;
17440 case 'w': p_ws = TRUE; break;
17441 case 'W': p_ws = FALSE; break;
17442 default: mask = 0;
17443 if (flagsp != NULL)
17444 switch (*flags)
17445 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017446 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017447 case 'e': mask = SP_END; break;
17448 case 'm': mask = SP_RETCOUNT; break;
17449 case 'n': mask = SP_NOMOVE; break;
17450 case 'p': mask = SP_SUBPAT; break;
17451 case 'r': mask = SP_REPEAT; break;
17452 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017453 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017454 }
17455 if (mask == 0)
17456 {
17457 EMSG2(_(e_invarg2), flags);
17458 dir = 0;
17459 }
17460 else
17461 *flagsp |= mask;
17462 }
17463 if (dir == 0)
17464 break;
17465 ++flags;
17466 }
17467 }
17468 return dir;
17469}
17470
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017472 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017475search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017477 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 char_u *pat;
17479 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017480 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 int save_p_ws = p_ws;
17482 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017483 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017484 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017485 proftime_T tm;
17486#ifdef FEAT_RELTIME
17487 long time_limit = 0;
17488#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017489 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017490 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017493 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017494 if (dir == 0)
17495 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017496 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017497 if (flags & SP_START)
17498 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017499 if (flags & SP_END)
17500 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017501 if (flags & SP_COLUMN)
17502 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017503
Bram Moolenaar76929292008-01-06 19:07:36 +000017504 /* Optional arguments: line number to stop searching and timeout. */
17505 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017506 {
17507 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17508 if (lnum_stop < 0)
17509 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017510#ifdef FEAT_RELTIME
17511 if (argvars[3].v_type != VAR_UNKNOWN)
17512 {
17513 time_limit = get_tv_number_chk(&argvars[3], NULL);
17514 if (time_limit < 0)
17515 goto theend;
17516 }
17517#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017518 }
17519
Bram Moolenaar76929292008-01-06 19:07:36 +000017520#ifdef FEAT_RELTIME
17521 /* Set the time limit, if there is one. */
17522 profile_setlimit(time_limit, &tm);
17523#endif
17524
Bram Moolenaar231334e2005-07-25 20:46:57 +000017525 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017526 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017527 * Check to make sure only those flags are set.
17528 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17529 * flags cannot be set. Check for that condition also.
17530 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017531 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017532 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017534 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017535 goto theend;
17536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017538 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017539 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017540 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017541 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017543 if (flags & SP_SUBPAT)
17544 retval = subpatnum;
17545 else
17546 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017547 if (flags & SP_SETPCMARK)
17548 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017550 if (match_pos != NULL)
17551 {
17552 /* Store the match cursor position */
17553 match_pos->lnum = pos.lnum;
17554 match_pos->col = pos.col + 1;
17555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 /* "/$" will put the cursor after the end of the line, may need to
17557 * correct that here */
17558 check_cursor();
17559 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017560
17561 /* If 'n' flag is used: restore cursor position. */
17562 if (flags & SP_NOMOVE)
17563 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017564 else
17565 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017566theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017568
17569 return retval;
17570}
17571
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017572#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017573
17574/*
17575 * round() is not in C90, use ceil() or floor() instead.
17576 */
17577 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017578vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017579{
17580 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17581}
17582
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017583/*
17584 * "round({float})" function
17585 */
17586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017587f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017588{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017589 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017590
17591 rettv->v_type = VAR_FLOAT;
17592 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017593 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017594 else
17595 rettv->vval.v_float = 0.0;
17596}
17597#endif
17598
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017599/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017600 * "screenattr()" function
17601 */
17602 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017603f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017604{
17605 int row;
17606 int col;
17607 int c;
17608
17609 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17610 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17611 if (row < 0 || row >= screen_Rows
17612 || col < 0 || col >= screen_Columns)
17613 c = -1;
17614 else
17615 c = ScreenAttrs[LineOffset[row] + col];
17616 rettv->vval.v_number = c;
17617}
17618
17619/*
17620 * "screenchar()" function
17621 */
17622 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017623f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017624{
17625 int row;
17626 int col;
17627 int off;
17628 int c;
17629
17630 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17631 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17632 if (row < 0 || row >= screen_Rows
17633 || col < 0 || col >= screen_Columns)
17634 c = -1;
17635 else
17636 {
17637 off = LineOffset[row] + col;
17638#ifdef FEAT_MBYTE
17639 if (enc_utf8 && ScreenLinesUC[off] != 0)
17640 c = ScreenLinesUC[off];
17641 else
17642#endif
17643 c = ScreenLines[off];
17644 }
17645 rettv->vval.v_number = c;
17646}
17647
17648/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017649 * "screencol()" function
17650 *
17651 * First column is 1 to be consistent with virtcol().
17652 */
17653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017654f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017655{
17656 rettv->vval.v_number = screen_screencol() + 1;
17657}
17658
17659/*
17660 * "screenrow()" function
17661 */
17662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017663f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017664{
17665 rettv->vval.v_number = screen_screenrow() + 1;
17666}
17667
17668/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017669 * "search()" function
17670 */
17671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017672f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017673{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017674 int flags = 0;
17675
17676 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677}
17678
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017680 * "searchdecl()" function
17681 */
17682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017683f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017684{
17685 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017686 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017687 int error = FALSE;
17688 char_u *name;
17689
17690 rettv->vval.v_number = 1; /* default: FAIL */
17691
17692 name = get_tv_string_chk(&argvars[0]);
17693 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017694 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017695 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017696 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17697 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17698 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017699 if (!error && name != NULL)
17700 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017701 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017702}
17703
17704/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017705 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017708searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709{
17710 char_u *spat, *mpat, *epat;
17711 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 int dir;
17714 int flags = 0;
17715 char_u nbuf1[NUMBUFLEN];
17716 char_u nbuf2[NUMBUFLEN];
17717 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017718 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017719 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017720 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017723 spat = get_tv_string_chk(&argvars[0]);
17724 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17725 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17726 if (spat == NULL || mpat == NULL || epat == NULL)
17727 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 /* Handle the optional fourth argument: flags */
17730 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017731 if (dir == 0)
17732 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017733
17734 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017735 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17736 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017737 if ((flags & (SP_END | SP_SUBPAT)) != 0
17738 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017739 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017740 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017741 goto theend;
17742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017744 /* Using 'r' implies 'W', otherwise it doesn't work. */
17745 if (flags & SP_REPEAT)
17746 p_ws = FALSE;
17747
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017748 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017749 if (argvars[3].v_type == VAR_UNKNOWN
17750 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 skip = (char_u *)"";
17752 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017754 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017755 if (argvars[5].v_type != VAR_UNKNOWN)
17756 {
17757 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17758 if (lnum_stop < 0)
17759 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017760#ifdef FEAT_RELTIME
17761 if (argvars[6].v_type != VAR_UNKNOWN)
17762 {
17763 time_limit = get_tv_number_chk(&argvars[6], NULL);
17764 if (time_limit < 0)
17765 goto theend;
17766 }
17767#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017768 }
17769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017770 if (skip == NULL)
17771 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017773 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017774 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017775
17776theend:
17777 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017778
17779 return retval;
17780}
17781
17782/*
17783 * "searchpair()" function
17784 */
17785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017786f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017787{
17788 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17789}
17790
17791/*
17792 * "searchpairpos()" function
17793 */
17794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017795f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017796{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017797 pos_T match_pos;
17798 int lnum = 0;
17799 int col = 0;
17800
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017801 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017802 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017803
17804 if (searchpair_cmn(argvars, &match_pos) > 0)
17805 {
17806 lnum = match_pos.lnum;
17807 col = match_pos.col;
17808 }
17809
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017810 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17811 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017812}
17813
17814/*
17815 * Search for a start/middle/end thing.
17816 * Used by searchpair(), see its documentation for the details.
17817 * Returns 0 or -1 for no match,
17818 */
17819 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017820do_searchpair(
17821 char_u *spat, /* start pattern */
17822 char_u *mpat, /* middle pattern */
17823 char_u *epat, /* end pattern */
17824 int dir, /* BACKWARD or FORWARD */
17825 char_u *skip, /* skip expression */
17826 int flags, /* SP_SETPCMARK and other SP_ values */
17827 pos_T *match_pos,
17828 linenr_T lnum_stop, /* stop at this line if not zero */
17829 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017830{
17831 char_u *save_cpo;
17832 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17833 long retval = 0;
17834 pos_T pos;
17835 pos_T firstpos;
17836 pos_T foundpos;
17837 pos_T save_cursor;
17838 pos_T save_pos;
17839 int n;
17840 int r;
17841 int nest = 1;
17842 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017843 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017844 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017845
17846 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17847 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017848 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017849
Bram Moolenaar76929292008-01-06 19:07:36 +000017850#ifdef FEAT_RELTIME
17851 /* Set the time limit, if there is one. */
17852 profile_setlimit(time_limit, &tm);
17853#endif
17854
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017855 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17856 * start/middle/end (pat3, for the top pair). */
17857 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17858 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17859 if (pat2 == NULL || pat3 == NULL)
17860 goto theend;
17861 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17862 if (*mpat == NUL)
17863 STRCPY(pat3, pat2);
17864 else
17865 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17866 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017867 if (flags & SP_START)
17868 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017869
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 save_cursor = curwin->w_cursor;
17871 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017872 clearpos(&firstpos);
17873 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 pat = pat3;
17875 for (;;)
17876 {
17877 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017878 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17880 /* didn't find it or found the first match again: FAIL */
17881 break;
17882
17883 if (firstpos.lnum == 0)
17884 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017885 if (equalpos(pos, foundpos))
17886 {
17887 /* Found the same position again. Can happen with a pattern that
17888 * has "\zs" at the end and searching backwards. Advance one
17889 * character and try again. */
17890 if (dir == BACKWARD)
17891 decl(&pos);
17892 else
17893 incl(&pos);
17894 }
17895 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017897 /* clear the start flag to avoid getting stuck here */
17898 options &= ~SEARCH_START;
17899
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900 /* If the skip pattern matches, ignore this match. */
17901 if (*skip != NUL)
17902 {
17903 save_pos = curwin->w_cursor;
17904 curwin->w_cursor = pos;
17905 r = eval_to_bool(skip, &err, NULL, FALSE);
17906 curwin->w_cursor = save_pos;
17907 if (err)
17908 {
17909 /* Evaluating {skip} caused an error, break here. */
17910 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017911 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 break;
17913 }
17914 if (r)
17915 continue;
17916 }
17917
17918 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17919 {
17920 /* Found end when searching backwards or start when searching
17921 * forward: nested pair. */
17922 ++nest;
17923 pat = pat2; /* nested, don't search for middle */
17924 }
17925 else
17926 {
17927 /* Found end when searching forward or start when searching
17928 * backward: end of (nested) pair; or found middle in outer pair. */
17929 if (--nest == 1)
17930 pat = pat3; /* outer level, search for middle */
17931 }
17932
17933 if (nest == 0)
17934 {
17935 /* Found the match: return matchcount or line number. */
17936 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017937 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017939 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017940 if (flags & SP_SETPCMARK)
17941 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 curwin->w_cursor = pos;
17943 if (!(flags & SP_REPEAT))
17944 break;
17945 nest = 1; /* search for next unmatched */
17946 }
17947 }
17948
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017949 if (match_pos != NULL)
17950 {
17951 /* Store the match cursor position */
17952 match_pos->lnum = curwin->w_cursor.lnum;
17953 match_pos->col = curwin->w_cursor.col + 1;
17954 }
17955
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017957 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 curwin->w_cursor = save_cursor;
17959
17960theend:
17961 vim_free(pat2);
17962 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017963 if (p_cpo == empty_option)
17964 p_cpo = save_cpo;
17965 else
17966 /* Darn, evaluating the {skip} expression changed the value. */
17967 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017968
17969 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970}
17971
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017972/*
17973 * "searchpos()" function
17974 */
17975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017976f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017977{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017978 pos_T match_pos;
17979 int lnum = 0;
17980 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017981 int n;
17982 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017984 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017985 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017986
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017987 n = search_cmn(argvars, &match_pos, &flags);
17988 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017989 {
17990 lnum = match_pos.lnum;
17991 col = match_pos.col;
17992 }
17993
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017994 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17995 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017996 if (flags & SP_SUBPAT)
17997 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017998}
17999
Bram Moolenaar0d660222005-01-07 21:51:51 +000018000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018001f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018003#ifdef FEAT_CLIENTSERVER
18004 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018005 char_u *server = get_tv_string_chk(&argvars[0]);
18006 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007
Bram Moolenaar0d660222005-01-07 21:51:51 +000018008 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018009 if (server == NULL || reply == NULL)
18010 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018011 if (check_restricted() || check_secure())
18012 return;
18013# ifdef FEAT_X11
18014 if (check_connection() == FAIL)
18015 return;
18016# endif
18017
18018 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018020 EMSG(_("E258: Unable to send to client"));
18021 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018023 rettv->vval.v_number = 0;
18024#else
18025 rettv->vval.v_number = -1;
18026#endif
18027}
18028
Bram Moolenaar0d660222005-01-07 21:51:51 +000018029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018030f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018031{
18032 char_u *r = NULL;
18033
18034#ifdef FEAT_CLIENTSERVER
18035# ifdef WIN32
18036 r = serverGetVimNames();
18037# else
18038 make_connection();
18039 if (X_DISPLAY != NULL)
18040 r = serverGetVimNames(X_DISPLAY);
18041# endif
18042#endif
18043 rettv->v_type = VAR_STRING;
18044 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045}
18046
18047/*
18048 * "setbufvar()" function
18049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018051f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052{
18053 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018056 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 char_u nbuf[NUMBUFLEN];
18058
18059 if (check_restricted() || check_secure())
18060 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018061 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18062 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018063 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 varp = &argvars[2];
18065
18066 if (buf != NULL && varname != NULL && varp != NULL)
18067 {
18068 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070
18071 if (*varname == '&')
18072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018073 long numval;
18074 char_u *strval;
18075 int error = FALSE;
18076
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018078 numval = get_tv_number_chk(varp, &error);
18079 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018080 if (!error && strval != NULL)
18081 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 }
18083 else
18084 {
18085 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18086 if (bufvarname != NULL)
18087 {
18088 STRCPY(bufvarname, "b:");
18089 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018090 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 vim_free(bufvarname);
18092 }
18093 }
18094
18095 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098}
18099
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018101f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018102{
18103 dict_T *d;
18104 dictitem_T *di;
18105 char_u *csearch;
18106
18107 if (argvars[0].v_type != VAR_DICT)
18108 {
18109 EMSG(_(e_dictreq));
18110 return;
18111 }
18112
18113 if ((d = argvars[0].vval.v_dict) != NULL)
18114 {
18115 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18116 if (csearch != NULL)
18117 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018118#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018119 if (enc_utf8)
18120 {
18121 int pcc[MAX_MCO];
18122 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018123
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018124 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18125 }
18126 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018127#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018128 set_last_csearch(PTR2CHAR(csearch),
18129 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018130 }
18131
18132 di = dict_find(d, (char_u *)"forward", -1);
18133 if (di != NULL)
18134 set_csearch_direction(get_tv_number(&di->di_tv)
18135 ? FORWARD : BACKWARD);
18136
18137 di = dict_find(d, (char_u *)"until", -1);
18138 if (di != NULL)
18139 set_csearch_until(!!get_tv_number(&di->di_tv));
18140 }
18141}
18142
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143/*
18144 * "setcmdpos()" function
18145 */
18146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018147f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018149 int pos = (int)get_tv_number(&argvars[0]) - 1;
18150
18151 if (pos >= 0)
18152 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153}
18154
18155/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018156 * "setfperm({fname}, {mode})" function
18157 */
18158 static void
18159f_setfperm(typval_T *argvars, typval_T *rettv)
18160{
18161 char_u *fname;
18162 char_u modebuf[NUMBUFLEN];
18163 char_u *mode_str;
18164 int i;
18165 int mask;
18166 int mode = 0;
18167
18168 rettv->vval.v_number = 0;
18169 fname = get_tv_string_chk(&argvars[0]);
18170 if (fname == NULL)
18171 return;
18172 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18173 if (mode_str == NULL)
18174 return;
18175 if (STRLEN(mode_str) != 9)
18176 {
18177 EMSG2(_(e_invarg2), mode_str);
18178 return;
18179 }
18180
18181 mask = 1;
18182 for (i = 8; i >= 0; --i)
18183 {
18184 if (mode_str[i] != '-')
18185 mode |= mask;
18186 mask = mask << 1;
18187 }
18188 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18189}
18190
18191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 * "setline()" function
18193 */
18194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018195f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196{
18197 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018198 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018199 list_T *l = NULL;
18200 listitem_T *li = NULL;
18201 long added = 0;
18202 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018204 lnum = get_tv_lnum(&argvars[0]);
18205 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018207 l = argvars[1].vval.v_list;
18208 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018210 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018211 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018212
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018213 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018214 for (;;)
18215 {
18216 if (l != NULL)
18217 {
18218 /* list argument, get next string */
18219 if (li == NULL)
18220 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018221 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018222 li = li->li_next;
18223 }
18224
18225 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018226 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018227 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018228
18229 /* When coming here from Insert mode, sync undo, so that this can be
18230 * undone separately from what was previously inserted. */
18231 if (u_sync_once == 2)
18232 {
18233 u_sync_once = 1; /* notify that u_sync() was called */
18234 u_sync(TRUE);
18235 }
18236
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018237 if (lnum <= curbuf->b_ml.ml_line_count)
18238 {
18239 /* existing line, replace it */
18240 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18241 {
18242 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018243 if (lnum == curwin->w_cursor.lnum)
18244 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018245 rettv->vval.v_number = 0; /* OK */
18246 }
18247 }
18248 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18249 {
18250 /* lnum is one past the last line, append the line */
18251 ++added;
18252 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18253 rettv->vval.v_number = 0; /* OK */
18254 }
18255
18256 if (l == NULL) /* only one string argument */
18257 break;
18258 ++lnum;
18259 }
18260
18261 if (added > 0)
18262 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263}
18264
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018265static 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 +000018266
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018268 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018269 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018271set_qf_ll_list(
18272 win_T *wp UNUSED,
18273 typval_T *list_arg UNUSED,
18274 typval_T *action_arg UNUSED,
18275 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018276{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018277#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018278 char_u *act;
18279 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018280#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018281
Bram Moolenaar2641f772005-03-25 21:58:17 +000018282 rettv->vval.v_number = -1;
18283
18284#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018285 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018286 EMSG(_(e_listreq));
18287 else
18288 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018289 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018290
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018291 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018292 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018293 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018294 if (act == NULL)
18295 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018296 if (*act == 'a' || *act == 'r')
18297 action = *act;
18298 }
18299
Bram Moolenaar81484f42012-12-05 15:16:47 +010018300 if (l != NULL && set_errorlist(wp, l, action,
18301 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018302 rettv->vval.v_number = 0;
18303 }
18304#endif
18305}
18306
18307/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018308 * "setloclist()" function
18309 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018311f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018312{
18313 win_T *win;
18314
18315 rettv->vval.v_number = -1;
18316
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018317 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018318 if (win != NULL)
18319 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18320}
18321
18322/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018323 * "setmatches()" function
18324 */
18325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018326f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018327{
18328#ifdef FEAT_SEARCH_EXTRA
18329 list_T *l;
18330 listitem_T *li;
18331 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018332 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018333
18334 rettv->vval.v_number = -1;
18335 if (argvars[0].v_type != VAR_LIST)
18336 {
18337 EMSG(_(e_listreq));
18338 return;
18339 }
18340 if ((l = argvars[0].vval.v_list) != NULL)
18341 {
18342
18343 /* To some extent make sure that we are dealing with a list from
18344 * "getmatches()". */
18345 li = l->lv_first;
18346 while (li != NULL)
18347 {
18348 if (li->li_tv.v_type != VAR_DICT
18349 || (d = li->li_tv.vval.v_dict) == NULL)
18350 {
18351 EMSG(_(e_invarg));
18352 return;
18353 }
18354 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018355 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18356 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018357 && dict_find(d, (char_u *)"priority", -1) != NULL
18358 && dict_find(d, (char_u *)"id", -1) != NULL))
18359 {
18360 EMSG(_(e_invarg));
18361 return;
18362 }
18363 li = li->li_next;
18364 }
18365
18366 clear_matches(curwin);
18367 li = l->lv_first;
18368 while (li != NULL)
18369 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018370 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018371 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018372 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018373 char_u *group;
18374 int priority;
18375 int id;
18376 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018377
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018378 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018379 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18380 {
18381 if (s == NULL)
18382 {
18383 s = list_alloc();
18384 if (s == NULL)
18385 return;
18386 }
18387
18388 /* match from matchaddpos() */
18389 for (i = 1; i < 9; i++)
18390 {
18391 sprintf((char *)buf, (char *)"pos%d", i);
18392 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18393 {
18394 if (di->di_tv.v_type != VAR_LIST)
18395 return;
18396
18397 list_append_tv(s, &di->di_tv);
18398 s->lv_refcount++;
18399 }
18400 else
18401 break;
18402 }
18403 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018404
18405 group = get_dict_string(d, (char_u *)"group", FALSE);
18406 priority = (int)get_dict_number(d, (char_u *)"priority");
18407 id = (int)get_dict_number(d, (char_u *)"id");
18408 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18409 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18410 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018411 if (i == 0)
18412 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018413 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018414 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018415 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018416 }
18417 else
18418 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018419 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018420 list_unref(s);
18421 s = NULL;
18422 }
18423
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018424 li = li->li_next;
18425 }
18426 rettv->vval.v_number = 0;
18427 }
18428#endif
18429}
18430
18431/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018432 * "setpos()" function
18433 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018435f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018436{
18437 pos_T pos;
18438 int fnum;
18439 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018440 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018441
Bram Moolenaar08250432008-02-13 11:42:46 +000018442 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018443 name = get_tv_string_chk(argvars);
18444 if (name != NULL)
18445 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018446 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018447 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018448 if (--pos.col < 0)
18449 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018450 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018451 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018452 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018453 if (fnum == curbuf->b_fnum)
18454 {
18455 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018456 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018457 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018458 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018459 curwin->w_set_curswant = FALSE;
18460 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018461 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018462 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018463 }
18464 else
18465 EMSG(_(e_invarg));
18466 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018467 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18468 {
18469 /* set mark */
18470 if (setmark_pos(name[1], &pos, fnum) == OK)
18471 rettv->vval.v_number = 0;
18472 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018473 else
18474 EMSG(_(e_invarg));
18475 }
18476 }
18477}
18478
18479/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018480 * "setqflist()" function
18481 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018483f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018484{
18485 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18486}
18487
18488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 * "setreg()" function
18490 */
18491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018492f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493{
18494 int regname;
18495 char_u *strregname;
18496 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018497 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 int append;
18499 char_u yank_type;
18500 long block_len;
18501
18502 block_len = -1;
18503 yank_type = MAUTO;
18504 append = FALSE;
18505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018506 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018507 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018509 if (strregname == NULL)
18510 return; /* type error; errmsg already given */
18511 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 if (regname == 0 || regname == '@')
18513 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018515 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018517 stropt = get_tv_string_chk(&argvars[2]);
18518 if (stropt == NULL)
18519 return; /* type error */
18520 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 switch (*stropt)
18522 {
18523 case 'a': case 'A': /* append */
18524 append = TRUE;
18525 break;
18526 case 'v': case 'c': /* character-wise selection */
18527 yank_type = MCHAR;
18528 break;
18529 case 'V': case 'l': /* line-wise selection */
18530 yank_type = MLINE;
18531 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 case 'b': case Ctrl_V: /* block-wise selection */
18533 yank_type = MBLOCK;
18534 if (VIM_ISDIGIT(stropt[1]))
18535 {
18536 ++stropt;
18537 block_len = getdigits(&stropt) - 1;
18538 --stropt;
18539 }
18540 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 }
18542 }
18543
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018544 if (argvars[1].v_type == VAR_LIST)
18545 {
18546 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018547 char_u **allocval;
18548 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018549 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018550 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018551 int len = argvars[1].vval.v_list->lv_len;
18552 listitem_T *li;
18553
Bram Moolenaar7d647822014-04-05 21:28:56 +020018554 /* First half: use for pointers to result lines; second half: use for
18555 * pointers to allocated copies. */
18556 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018557 if (lstval == NULL)
18558 return;
18559 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018560 allocval = lstval + len + 2;
18561 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018562
18563 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18564 li = li->li_next)
18565 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018566 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018567 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018568 goto free_lstval;
18569 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018570 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018571 /* Need to make a copy, next get_tv_string_buf_chk() will
18572 * overwrite the string. */
18573 strval = vim_strsave(buf);
18574 if (strval == NULL)
18575 goto free_lstval;
18576 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018577 }
18578 *curval++ = strval;
18579 }
18580 *curval++ = NULL;
18581
18582 write_reg_contents_lst(regname, lstval, -1,
18583 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018584free_lstval:
18585 while (curallocval > allocval)
18586 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018587 vim_free(lstval);
18588 }
18589 else
18590 {
18591 strval = get_tv_string_chk(&argvars[1]);
18592 if (strval == NULL)
18593 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018594 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598}
18599
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018600/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018601 * "settabvar()" function
18602 */
18603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018604f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018605{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018606#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018607 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018608 tabpage_T *tp;
18609#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018610 char_u *varname, *tabvarname;
18611 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018612
18613 rettv->vval.v_number = 0;
18614
18615 if (check_restricted() || check_secure())
18616 return;
18617
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018618#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018619 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018620#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018621 varname = get_tv_string_chk(&argvars[1]);
18622 varp = &argvars[2];
18623
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018624 if (varname != NULL && varp != NULL
18625#ifdef FEAT_WINDOWS
18626 && tp != NULL
18627#endif
18628 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018629 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018630#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018631 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018632 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018633#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018634
18635 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18636 if (tabvarname != NULL)
18637 {
18638 STRCPY(tabvarname, "t:");
18639 STRCPY(tabvarname + 2, varname);
18640 set_var(tabvarname, varp, TRUE);
18641 vim_free(tabvarname);
18642 }
18643
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018644#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018645 /* Restore current tabpage */
18646 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018647 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018648#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018649 }
18650}
18651
18652/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018653 * "settabwinvar()" function
18654 */
18655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018656f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018657{
18658 setwinvar(argvars, rettv, 1);
18659}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660
18661/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018662 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018665f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018667 setwinvar(argvars, rettv, 0);
18668}
18669
18670/*
18671 * "setwinvar()" and "settabwinvar()" functions
18672 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018673
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018675setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018676{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 win_T *win;
18678#ifdef FEAT_WINDOWS
18679 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018680 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018681 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682#endif
18683 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018684 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018686 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687
18688 if (check_restricted() || check_secure())
18689 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018690
18691#ifdef FEAT_WINDOWS
18692 if (off == 1)
18693 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18694 else
18695 tp = curtab;
18696#endif
18697 win = find_win_by_nr(&argvars[off], tp);
18698 varname = get_tv_string_chk(&argvars[off + 1]);
18699 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700
18701 if (win != NULL && varname != NULL && varp != NULL)
18702 {
18703#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018704 need_switch_win = !(tp == curtab && win == curwin);
18705 if (!need_switch_win
18706 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018709 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018711 long numval;
18712 char_u *strval;
18713 int error = FALSE;
18714
18715 ++varname;
18716 numval = get_tv_number_chk(varp, &error);
18717 strval = get_tv_string_buf_chk(varp, nbuf);
18718 if (!error && strval != NULL)
18719 set_option_value(varname, numval, strval, OPT_LOCAL);
18720 }
18721 else
18722 {
18723 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18724 if (winvarname != NULL)
18725 {
18726 STRCPY(winvarname, "w:");
18727 STRCPY(winvarname + 2, varname);
18728 set_var(winvarname, varp, TRUE);
18729 vim_free(winvarname);
18730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 }
18732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018734 if (need_switch_win)
18735 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736#endif
18737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738}
18739
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018740#ifdef FEAT_CRYPT
18741/*
18742 * "sha256({string})" function
18743 */
18744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018745f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018746{
18747 char_u *p;
18748
18749 p = get_tv_string(&argvars[0]);
18750 rettv->vval.v_string = vim_strsave(
18751 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18752 rettv->v_type = VAR_STRING;
18753}
18754#endif /* FEAT_CRYPT */
18755
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018757 * "shellescape({string})" function
18758 */
18759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018760f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018761{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018762 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018763 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018764 rettv->v_type = VAR_STRING;
18765}
18766
18767/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018768 * shiftwidth() function
18769 */
18770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018771f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018772{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018773 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018774}
18775
18776/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018777 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 */
18779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018780f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018782 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783
Bram Moolenaar0d660222005-01-07 21:51:51 +000018784 p = get_tv_string(&argvars[0]);
18785 rettv->vval.v_string = vim_strsave(p);
18786 simplify_filename(rettv->vval.v_string); /* simplify in place */
18787 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788}
18789
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018790#ifdef FEAT_FLOAT
18791/*
18792 * "sin()" function
18793 */
18794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018795f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018796{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018797 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018798
18799 rettv->v_type = VAR_FLOAT;
18800 if (get_float_arg(argvars, &f) == OK)
18801 rettv->vval.v_float = sin(f);
18802 else
18803 rettv->vval.v_float = 0.0;
18804}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018805
18806/*
18807 * "sinh()" function
18808 */
18809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018810f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018811{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018812 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018813
18814 rettv->v_type = VAR_FLOAT;
18815 if (get_float_arg(argvars, &f) == OK)
18816 rettv->vval.v_float = sinh(f);
18817 else
18818 rettv->vval.v_float = 0.0;
18819}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018820#endif
18821
Bram Moolenaar0d660222005-01-07 21:51:51 +000018822static int
18823#ifdef __BORLANDC__
18824 _RTLENTRYF
18825#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018826 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018827static int
18828#ifdef __BORLANDC__
18829 _RTLENTRYF
18830#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018831 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018832
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018833/* struct used in the array that's given to qsort() */
18834typedef struct
18835{
18836 listitem_T *item;
18837 int idx;
18838} sortItem_T;
18839
Bram Moolenaar0b962472016-02-22 22:51:33 +010018840/* struct storing information about current sort */
18841typedef struct
18842{
18843 int item_compare_ic;
18844 int item_compare_numeric;
18845 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018846#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018847 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018848#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018849 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018850 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018851 dict_T *item_compare_selfdict;
18852 int item_compare_func_err;
18853 int item_compare_keep_zero;
18854} sortinfo_T;
18855static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018856static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018857#define ITEM_COMPARE_FAIL 999
18858
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018860 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018862 static int
18863#ifdef __BORLANDC__
18864_RTLENTRYF
18865#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018866item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018868 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018869 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018870 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018871 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018872 int res;
18873 char_u numbuf1[NUMBUFLEN];
18874 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018876 si1 = (sortItem_T *)s1;
18877 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018878 tv1 = &si1->item->li_tv;
18879 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018880
Bram Moolenaar0b962472016-02-22 22:51:33 +010018881 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018882 {
18883 long v1 = get_tv_number(tv1);
18884 long v2 = get_tv_number(tv2);
18885
18886 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18887 }
18888
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018889#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018890 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018891 {
18892 float_T v1 = get_tv_float(tv1);
18893 float_T v2 = get_tv_float(tv2);
18894
18895 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18896 }
18897#endif
18898
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018899 /* tv2string() puts quotes around a string and allocates memory. Don't do
18900 * that for string variables. Use a single quote when comparing with a
18901 * non-string to do what the docs promise. */
18902 if (tv1->v_type == VAR_STRING)
18903 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018904 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018905 p1 = (char_u *)"'";
18906 else
18907 p1 = tv1->vval.v_string;
18908 }
18909 else
18910 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18911 if (tv2->v_type == VAR_STRING)
18912 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018913 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018914 p2 = (char_u *)"'";
18915 else
18916 p2 = tv2->vval.v_string;
18917 }
18918 else
18919 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018920 if (p1 == NULL)
18921 p1 = (char_u *)"";
18922 if (p2 == NULL)
18923 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018924 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018925 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018926 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018927 res = STRICMP(p1, p2);
18928 else
18929 res = STRCMP(p1, p2);
18930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018932 {
18933 double n1, n2;
18934 n1 = strtod((char *)p1, (char **)&p1);
18935 n2 = strtod((char *)p2, (char **)&p2);
18936 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18937 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018938
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018939 /* When the result would be zero, compare the item indexes. Makes the
18940 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018941 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018942 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018943
Bram Moolenaar0d660222005-01-07 21:51:51 +000018944 vim_free(tofree1);
18945 vim_free(tofree2);
18946 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947}
18948
18949 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018950#ifdef __BORLANDC__
18951_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018953item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018954{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018955 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018957 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018958 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018959 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018960 char_u *func_name;
18961 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018963 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018964 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018965 return 0;
18966
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018967 si1 = (sortItem_T *)s1;
18968 si2 = (sortItem_T *)s2;
18969
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018970 if (partial == NULL)
18971 func_name = sortinfo->item_compare_func;
18972 else
18973 func_name = partial->pt_name;
18974
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018975 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018976 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018977 copy_tv(&si1->item->li_tv, &argv[0]);
18978 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979
18980 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018981 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018982 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018983 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018984 clear_tv(&argv[0]);
18985 clear_tv(&argv[1]);
18986
18987 if (res == FAIL)
18988 res = ITEM_COMPARE_FAIL;
18989 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018990 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18991 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018992 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018993 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018994
18995 /* When the result would be zero, compare the pointers themselves. Makes
18996 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018997 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018998 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018999
Bram Moolenaar0d660222005-01-07 21:51:51 +000019000 return res;
19001}
19002
19003/*
19004 * "sort({list})" function
19005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019007do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008{
Bram Moolenaar33570922005-01-25 22:26:29 +000019009 list_T *l;
19010 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019011 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019012 sortinfo_T *old_sortinfo;
19013 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014 long len;
19015 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016
Bram Moolenaar0b962472016-02-22 22:51:33 +010019017 /* Pointer to current info struct used in compare function. Save and
19018 * restore the current one for nested calls. */
19019 old_sortinfo = sortinfo;
19020 sortinfo = &info;
19021
Bram Moolenaar0d660222005-01-07 21:51:51 +000019022 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019023 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 else
19025 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019026 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019027 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019028 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19029 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019030 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019031 rettv->vval.v_list = l;
19032 rettv->v_type = VAR_LIST;
19033 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034
Bram Moolenaar0d660222005-01-07 21:51:51 +000019035 len = list_len(l);
19036 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019037 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038
Bram Moolenaar0b962472016-02-22 22:51:33 +010019039 info.item_compare_ic = FALSE;
19040 info.item_compare_numeric = FALSE;
19041 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019042#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019043 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019044#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019045 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019046 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019047 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019048 if (argvars[1].v_type != VAR_UNKNOWN)
19049 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019050 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019052 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019053 else if (argvars[1].v_type == VAR_PARTIAL)
19054 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019055 else
19056 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019057 int error = FALSE;
19058
19059 i = get_tv_number_chk(&argvars[1], &error);
19060 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019061 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019062 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019063 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019064 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019065 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019066 else if (i != 0)
19067 {
19068 EMSG(_(e_invarg));
19069 goto theend;
19070 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019071 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019072 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019073 if (*info.item_compare_func == NUL)
19074 {
19075 /* empty string means default sort */
19076 info.item_compare_func = NULL;
19077 }
19078 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019079 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019080 info.item_compare_func = NULL;
19081 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019082 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019083 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019084 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019085 info.item_compare_func = NULL;
19086 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019087 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019088#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019089 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019090 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019091 info.item_compare_func = NULL;
19092 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019093 }
19094#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019095 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019096 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019097 info.item_compare_func = NULL;
19098 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019099 }
19100 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019101 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019102
19103 if (argvars[2].v_type != VAR_UNKNOWN)
19104 {
19105 /* optional third argument: {dict} */
19106 if (argvars[2].v_type != VAR_DICT)
19107 {
19108 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019109 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019110 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019111 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019112 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114
Bram Moolenaar0d660222005-01-07 21:51:51 +000019115 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019116 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019117 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019118 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119
Bram Moolenaar327aa022014-03-25 18:24:23 +010019120 i = 0;
19121 if (sort)
19122 {
19123 /* sort(): ptrs will be the list to sort */
19124 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019125 {
19126 ptrs[i].item = li;
19127 ptrs[i].idx = i;
19128 ++i;
19129 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019130
Bram Moolenaar0b962472016-02-22 22:51:33 +010019131 info.item_compare_func_err = FALSE;
19132 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019133 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019134 if ((info.item_compare_func != NULL
19135 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019136 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019138 EMSG(_("E702: Sort compare function failed"));
19139 else
19140 {
19141 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019142 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019143 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019144 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019145 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019146
Bram Moolenaar0b962472016-02-22 22:51:33 +010019147 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019148 {
19149 /* Clear the List and append the items in sorted order. */
19150 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19151 l->lv_len = 0;
19152 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019153 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019154 }
19155 }
19156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019158 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019159 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019160
19161 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019162 info.item_compare_func_err = FALSE;
19163 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019164 item_compare_func_ptr = info.item_compare_func != NULL
19165 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019166 ? item_compare2 : item_compare;
19167
19168 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19169 li = li->li_next)
19170 {
19171 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19172 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019173 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019174 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019175 {
19176 EMSG(_("E882: Uniq compare function failed"));
19177 break;
19178 }
19179 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019180
Bram Moolenaar0b962472016-02-22 22:51:33 +010019181 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019182 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019183 while (--i >= 0)
19184 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019185 li = ptrs[i].item->li_next;
19186 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019187 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019188 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019189 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019190 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019191 list_fix_watch(l, li);
19192 listitem_free(li);
19193 l->lv_len--;
19194 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019195 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019196 }
19197
19198 vim_free(ptrs);
19199 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019200theend:
19201 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019202}
19203
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019204/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019205 * "sort({list})" function
19206 */
19207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019208f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019209{
19210 do_sort_uniq(argvars, rettv, TRUE);
19211}
19212
19213/*
19214 * "uniq({list})" function
19215 */
19216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019217f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019218{
19219 do_sort_uniq(argvars, rettv, FALSE);
19220}
19221
19222/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019223 * "soundfold({word})" function
19224 */
19225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019226f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019227{
19228 char_u *s;
19229
19230 rettv->v_type = VAR_STRING;
19231 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019232#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019233 rettv->vval.v_string = eval_soundfold(s);
19234#else
19235 rettv->vval.v_string = vim_strsave(s);
19236#endif
19237}
19238
19239/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019240 * "spellbadword()" function
19241 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019243f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019244{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019245 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019246 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019247 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019248
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019249 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019250 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019251
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019252#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019253 if (argvars[0].v_type == VAR_UNKNOWN)
19254 {
19255 /* Find the start and length of the badly spelled word. */
19256 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19257 if (len != 0)
19258 word = ml_get_cursor();
19259 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019260 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019261 {
19262 char_u *str = get_tv_string_chk(&argvars[0]);
19263 int capcol = -1;
19264
19265 if (str != NULL)
19266 {
19267 /* Check the argument for spelling. */
19268 while (*str != NUL)
19269 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019270 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019271 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019272 {
19273 word = str;
19274 break;
19275 }
19276 str += len;
19277 }
19278 }
19279 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019280#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019281
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019282 list_append_string(rettv->vval.v_list, word, len);
19283 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019284 attr == HLF_SPB ? "bad" :
19285 attr == HLF_SPR ? "rare" :
19286 attr == HLF_SPL ? "local" :
19287 attr == HLF_SPC ? "caps" :
19288 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019289}
19290
19291/*
19292 * "spellsuggest()" function
19293 */
19294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019295f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019296{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019297#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019298 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019299 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019300 int maxcount;
19301 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019302 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019303 listitem_T *li;
19304 int need_capital = FALSE;
19305#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019306
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019307 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019308 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019309
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019310#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019311 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019312 {
19313 str = get_tv_string(&argvars[0]);
19314 if (argvars[1].v_type != VAR_UNKNOWN)
19315 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019316 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019317 if (maxcount <= 0)
19318 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019319 if (argvars[2].v_type != VAR_UNKNOWN)
19320 {
19321 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19322 if (typeerr)
19323 return;
19324 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019325 }
19326 else
19327 maxcount = 25;
19328
Bram Moolenaar4770d092006-01-12 23:22:24 +000019329 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019330
19331 for (i = 0; i < ga.ga_len; ++i)
19332 {
19333 str = ((char_u **)ga.ga_data)[i];
19334
19335 li = listitem_alloc();
19336 if (li == NULL)
19337 vim_free(str);
19338 else
19339 {
19340 li->li_tv.v_type = VAR_STRING;
19341 li->li_tv.v_lock = 0;
19342 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019343 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019344 }
19345 }
19346 ga_clear(&ga);
19347 }
19348#endif
19349}
19350
Bram Moolenaar0d660222005-01-07 21:51:51 +000019351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019352f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019353{
19354 char_u *str;
19355 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019356 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019357 regmatch_T regmatch;
19358 char_u patbuf[NUMBUFLEN];
19359 char_u *save_cpo;
19360 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019361 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019362 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019363 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019364
19365 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19366 save_cpo = p_cpo;
19367 p_cpo = (char_u *)"";
19368
19369 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019370 if (argvars[1].v_type != VAR_UNKNOWN)
19371 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019372 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19373 if (pat == NULL)
19374 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019375 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019376 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019377 }
19378 if (pat == NULL || *pat == NUL)
19379 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019380
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019381 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019383 if (typeerr)
19384 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385
Bram Moolenaar0d660222005-01-07 21:51:51 +000019386 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19387 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019389 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019390 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019391 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019392 if (*str == NUL)
19393 match = FALSE; /* empty item at the end */
19394 else
19395 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019396 if (match)
19397 end = regmatch.startp[0];
19398 else
19399 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019400 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19401 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019402 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019403 if (list_append_string(rettv->vval.v_list, str,
19404 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019405 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019406 }
19407 if (!match)
19408 break;
19409 /* Advance to just after the match. */
19410 if (regmatch.endp[0] > str)
19411 col = 0;
19412 else
19413 {
19414 /* Don't get stuck at the same match. */
19415#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019416 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019417#else
19418 col = 1;
19419#endif
19420 }
19421 str = regmatch.endp[0];
19422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423
Bram Moolenaar473de612013-06-08 18:19:48 +020019424 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426
Bram Moolenaar0d660222005-01-07 21:51:51 +000019427 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428}
19429
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019430#ifdef FEAT_FLOAT
19431/*
19432 * "sqrt()" function
19433 */
19434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019435f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019436{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019437 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019438
19439 rettv->v_type = VAR_FLOAT;
19440 if (get_float_arg(argvars, &f) == OK)
19441 rettv->vval.v_float = sqrt(f);
19442 else
19443 rettv->vval.v_float = 0.0;
19444}
19445
19446/*
19447 * "str2float()" function
19448 */
19449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019450f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019451{
19452 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19453
19454 if (*p == '+')
19455 p = skipwhite(p + 1);
19456 (void)string2float(p, &rettv->vval.v_float);
19457 rettv->v_type = VAR_FLOAT;
19458}
19459#endif
19460
Bram Moolenaar2c932302006-03-18 21:42:09 +000019461/*
19462 * "str2nr()" function
19463 */
19464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019465f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019466{
19467 int base = 10;
19468 char_u *p;
19469 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019470 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019471
19472 if (argvars[1].v_type != VAR_UNKNOWN)
19473 {
19474 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019475 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019476 {
19477 EMSG(_(e_invarg));
19478 return;
19479 }
19480 }
19481
19482 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019483 if (*p == '+')
19484 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019485 switch (base)
19486 {
19487 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19488 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19489 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19490 default: what = 0;
19491 }
19492 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019493 rettv->vval.v_number = n;
19494}
19495
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496#ifdef HAVE_STRFTIME
19497/*
19498 * "strftime({format}[, {time}])" function
19499 */
19500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019501f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502{
19503 char_u result_buf[256];
19504 struct tm *curtime;
19505 time_t seconds;
19506 char_u *p;
19507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019508 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019510 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019511 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 seconds = time(NULL);
19513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019514 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 curtime = localtime(&seconds);
19516 /* MSVC returns NULL for an invalid value of seconds. */
19517 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019518 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 else
19520 {
19521# ifdef FEAT_MBYTE
19522 vimconv_T conv;
19523 char_u *enc;
19524
19525 conv.vc_type = CONV_NONE;
19526 enc = enc_locale();
19527 convert_setup(&conv, p_enc, enc);
19528 if (conv.vc_type != CONV_NONE)
19529 p = string_convert(&conv, p, NULL);
19530# endif
19531 if (p != NULL)
19532 (void)strftime((char *)result_buf, sizeof(result_buf),
19533 (char *)p, curtime);
19534 else
19535 result_buf[0] = NUL;
19536
19537# ifdef FEAT_MBYTE
19538 if (conv.vc_type != CONV_NONE)
19539 vim_free(p);
19540 convert_setup(&conv, enc, p_enc);
19541 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019542 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 else
19544# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019545 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546
19547# ifdef FEAT_MBYTE
19548 /* Release conversion descriptors */
19549 convert_setup(&conv, NULL, NULL);
19550 vim_free(enc);
19551# endif
19552 }
19553}
19554#endif
19555
19556/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019557 * "strgetchar()" function
19558 */
19559 static void
19560f_strgetchar(typval_T *argvars, typval_T *rettv)
19561{
19562 char_u *str;
19563 int len;
19564 int error = FALSE;
19565 int charidx;
19566
19567 rettv->vval.v_number = -1;
19568 str = get_tv_string_chk(&argvars[0]);
19569 if (str == NULL)
19570 return;
19571 len = (int)STRLEN(str);
19572 charidx = get_tv_number_chk(&argvars[1], &error);
19573 if (error)
19574 return;
19575#ifdef FEAT_MBYTE
19576 {
19577 int byteidx = 0;
19578
19579 while (charidx >= 0 && byteidx < len)
19580 {
19581 if (charidx == 0)
19582 {
19583 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19584 break;
19585 }
19586 --charidx;
19587 byteidx += mb_char2len(str[byteidx]);
19588 }
19589 }
19590#else
19591 if (charidx < len)
19592 rettv->vval.v_number = str[charidx];
19593#endif
19594}
19595
19596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 * "stridx()" function
19598 */
19599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019600f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601{
19602 char_u buf[NUMBUFLEN];
19603 char_u *needle;
19604 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019605 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019607 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019609 needle = get_tv_string_chk(&argvars[1]);
19610 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019611 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019612 if (needle == NULL || haystack == NULL)
19613 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 if (argvars[2].v_type != VAR_UNKNOWN)
19616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019617 int error = FALSE;
19618
19619 start_idx = get_tv_number_chk(&argvars[2], &error);
19620 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019622 if (start_idx >= 0)
19623 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019624 }
19625
19626 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19627 if (pos != NULL)
19628 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629}
19630
19631/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019632 * "string()" function
19633 */
19634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019635f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019636{
19637 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019638 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019640 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019641 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19642 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019643 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019644 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019645 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646}
19647
19648/*
19649 * "strlen()" function
19650 */
19651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019652f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019654 rettv->vval.v_number = (varnumber_T)(STRLEN(
19655 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656}
19657
19658/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019659 * "strchars()" function
19660 */
19661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019662f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019663{
19664 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019665 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019666#ifdef FEAT_MBYTE
19667 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019668 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019669#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019670
19671 if (argvars[1].v_type != VAR_UNKNOWN)
19672 skipcc = get_tv_number_chk(&argvars[1], NULL);
19673 if (skipcc < 0 || skipcc > 1)
19674 EMSG(_(e_invarg));
19675 else
19676 {
19677#ifdef FEAT_MBYTE
19678 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19679 while (*s != NUL)
19680 {
19681 func_mb_ptr2char_adv(&s);
19682 ++len;
19683 }
19684 rettv->vval.v_number = len;
19685#else
19686 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19687#endif
19688 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019689}
19690
19691/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019692 * "strdisplaywidth()" function
19693 */
19694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019695f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019696{
19697 char_u *s = get_tv_string(&argvars[0]);
19698 int col = 0;
19699
19700 if (argvars[1].v_type != VAR_UNKNOWN)
19701 col = get_tv_number(&argvars[1]);
19702
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019703 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019704}
19705
19706/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019707 * "strwidth()" function
19708 */
19709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019710f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019711{
19712 char_u *s = get_tv_string(&argvars[0]);
19713
19714 rettv->vval.v_number = (varnumber_T)(
19715#ifdef FEAT_MBYTE
19716 mb_string2cells(s, -1)
19717#else
19718 STRLEN(s)
19719#endif
19720 );
19721}
19722
19723/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019724 * "strcharpart()" function
19725 */
19726 static void
19727f_strcharpart(typval_T *argvars, typval_T *rettv)
19728{
19729#ifdef FEAT_MBYTE
19730 char_u *p;
19731 int nchar;
19732 int nbyte = 0;
19733 int charlen;
19734 int len = 0;
19735 int slen;
19736 int error = FALSE;
19737
19738 p = get_tv_string(&argvars[0]);
19739 slen = (int)STRLEN(p);
19740
19741 nchar = get_tv_number_chk(&argvars[1], &error);
19742 if (!error)
19743 {
19744 if (nchar > 0)
19745 while (nchar > 0 && nbyte < slen)
19746 {
19747 nbyte += mb_char2len(p[nbyte]);
19748 --nchar;
19749 }
19750 else
19751 nbyte = nchar;
19752 if (argvars[2].v_type != VAR_UNKNOWN)
19753 {
19754 charlen = get_tv_number(&argvars[2]);
19755 while (charlen > 0 && nbyte + len < slen)
19756 {
19757 len += mb_char2len(p[nbyte + len]);
19758 --charlen;
19759 }
19760 }
19761 else
19762 len = slen - nbyte; /* default: all bytes that are available. */
19763 }
19764
19765 /*
19766 * Only return the overlap between the specified part and the actual
19767 * string.
19768 */
19769 if (nbyte < 0)
19770 {
19771 len += nbyte;
19772 nbyte = 0;
19773 }
19774 else if (nbyte > slen)
19775 nbyte = slen;
19776 if (len < 0)
19777 len = 0;
19778 else if (nbyte + len > slen)
19779 len = slen - nbyte;
19780
19781 rettv->v_type = VAR_STRING;
19782 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19783#else
19784 f_strpart(argvars, rettv);
19785#endif
19786}
19787
19788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 * "strpart()" function
19790 */
19791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019792f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793{
19794 char_u *p;
19795 int n;
19796 int len;
19797 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019798 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019800 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 slen = (int)STRLEN(p);
19802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019803 n = get_tv_number_chk(&argvars[1], &error);
19804 if (error)
19805 len = 0;
19806 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019807 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 else
19809 len = slen - n; /* default len: all bytes that are available. */
19810
19811 /*
19812 * Only return the overlap between the specified part and the actual
19813 * string.
19814 */
19815 if (n < 0)
19816 {
19817 len += n;
19818 n = 0;
19819 }
19820 else if (n > slen)
19821 n = slen;
19822 if (len < 0)
19823 len = 0;
19824 else if (n + len > slen)
19825 len = slen - n;
19826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019827 rettv->v_type = VAR_STRING;
19828 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829}
19830
19831/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019832 * "strridx()" function
19833 */
19834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019835f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019836{
19837 char_u buf[NUMBUFLEN];
19838 char_u *needle;
19839 char_u *haystack;
19840 char_u *rest;
19841 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019842 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019844 needle = get_tv_string_chk(&argvars[1]);
19845 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019846
19847 rettv->vval.v_number = -1;
19848 if (needle == NULL || haystack == NULL)
19849 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019850
19851 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019852 if (argvars[2].v_type != VAR_UNKNOWN)
19853 {
19854 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019855 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019856 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019857 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019858 }
19859 else
19860 end_idx = haystack_len;
19861
Bram Moolenaar0d660222005-01-07 21:51:51 +000019862 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019863 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019864 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019865 lastmatch = haystack + end_idx;
19866 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019867 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019868 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019869 for (rest = haystack; *rest != '\0'; ++rest)
19870 {
19871 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019872 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019873 break;
19874 lastmatch = rest;
19875 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019876 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019877
19878 if (lastmatch == NULL)
19879 rettv->vval.v_number = -1;
19880 else
19881 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19882}
19883
19884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 * "strtrans()" function
19886 */
19887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019888f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019890 rettv->v_type = VAR_STRING;
19891 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892}
19893
19894/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019895 * "submatch()" function
19896 */
19897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019898f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019899{
Bram Moolenaar41571762014-04-02 19:00:58 +020019900 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019901 int no;
19902 int retList = 0;
19903
19904 no = (int)get_tv_number_chk(&argvars[0], &error);
19905 if (error)
19906 return;
19907 error = FALSE;
19908 if (argvars[1].v_type != VAR_UNKNOWN)
19909 retList = get_tv_number_chk(&argvars[1], &error);
19910 if (error)
19911 return;
19912
19913 if (retList == 0)
19914 {
19915 rettv->v_type = VAR_STRING;
19916 rettv->vval.v_string = reg_submatch(no);
19917 }
19918 else
19919 {
19920 rettv->v_type = VAR_LIST;
19921 rettv->vval.v_list = reg_submatch_list(no);
19922 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019923}
19924
19925/*
19926 * "substitute()" function
19927 */
19928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019929f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019930{
19931 char_u patbuf[NUMBUFLEN];
19932 char_u subbuf[NUMBUFLEN];
19933 char_u flagsbuf[NUMBUFLEN];
19934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019935 char_u *str = get_tv_string_chk(&argvars[0]);
19936 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19937 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19938 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19939
Bram Moolenaar0d660222005-01-07 21:51:51 +000019940 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019941 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19942 rettv->vval.v_string = NULL;
19943 else
19944 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019945}
19946
19947/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019948 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019951f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952{
19953 int id = 0;
19954#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019955 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 long col;
19957 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019958 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019960 lnum = get_tv_lnum(argvars); /* -1 on type error */
19961 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19962 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019964 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019965 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019966 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967#endif
19968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970}
19971
19972/*
19973 * "synIDattr(id, what [, mode])" function
19974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019976f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977{
19978 char_u *p = NULL;
19979#ifdef FEAT_SYN_HL
19980 int id;
19981 char_u *what;
19982 char_u *mode;
19983 char_u modebuf[NUMBUFLEN];
19984 int modec;
19985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019986 id = get_tv_number(&argvars[0]);
19987 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019988 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019990 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019992 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 modec = 0; /* replace invalid with current */
19994 }
19995 else
19996 {
19997#ifdef FEAT_GUI
19998 if (gui.in_use)
19999 modec = 'g';
20000 else
20001#endif
20002 if (t_colors > 1)
20003 modec = 'c';
20004 else
20005 modec = 't';
20006 }
20007
20008
20009 switch (TOLOWER_ASC(what[0]))
20010 {
20011 case 'b':
20012 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20013 p = highlight_color(id, what, modec);
20014 else /* bold */
20015 p = highlight_has_attr(id, HL_BOLD, modec);
20016 break;
20017
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020018 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 p = highlight_color(id, what, modec);
20020 break;
20021
20022 case 'i':
20023 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20024 p = highlight_has_attr(id, HL_INVERSE, modec);
20025 else /* italic */
20026 p = highlight_has_attr(id, HL_ITALIC, modec);
20027 break;
20028
20029 case 'n': /* name */
20030 p = get_highlight_name(NULL, id - 1);
20031 break;
20032
20033 case 'r': /* reverse */
20034 p = highlight_has_attr(id, HL_INVERSE, modec);
20035 break;
20036
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020037 case 's':
20038 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20039 p = highlight_color(id, what, modec);
20040 else /* standout */
20041 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 break;
20043
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020044 case 'u':
20045 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20046 /* underline */
20047 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20048 else
20049 /* undercurl */
20050 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 break;
20052 }
20053
20054 if (p != NULL)
20055 p = vim_strsave(p);
20056#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020057 rettv->v_type = VAR_STRING;
20058 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059}
20060
20061/*
20062 * "synIDtrans(id)" function
20063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020065f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066{
20067 int id;
20068
20069#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020070 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071
20072 if (id > 0)
20073 id = syn_get_final_id(id);
20074 else
20075#endif
20076 id = 0;
20077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020078 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079}
20080
20081/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020082 * "synconcealed(lnum, col)" function
20083 */
20084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020085f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020086{
20087#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20088 long lnum;
20089 long col;
20090 int syntax_flags = 0;
20091 int cchar;
20092 int matchid = 0;
20093 char_u str[NUMBUFLEN];
20094#endif
20095
20096 rettv->v_type = VAR_LIST;
20097 rettv->vval.v_list = NULL;
20098
20099#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20100 lnum = get_tv_lnum(argvars); /* -1 on type error */
20101 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20102
20103 vim_memset(str, NUL, sizeof(str));
20104
20105 if (rettv_list_alloc(rettv) != FAIL)
20106 {
20107 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20108 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20109 && curwin->w_p_cole > 0)
20110 {
20111 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20112 syntax_flags = get_syntax_info(&matchid);
20113
20114 /* get the conceal character */
20115 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20116 {
20117 cchar = syn_get_sub_char();
20118 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20119 cchar = lcs_conceal;
20120 if (cchar != NUL)
20121 {
20122# ifdef FEAT_MBYTE
20123 if (has_mbyte)
20124 (*mb_char2bytes)(cchar, str);
20125 else
20126# endif
20127 str[0] = cchar;
20128 }
20129 }
20130 }
20131
20132 list_append_number(rettv->vval.v_list,
20133 (syntax_flags & HL_CONCEAL) != 0);
20134 /* -1 to auto-determine strlen */
20135 list_append_string(rettv->vval.v_list, str, -1);
20136 list_append_number(rettv->vval.v_list, matchid);
20137 }
20138#endif
20139}
20140
20141/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020142 * "synstack(lnum, col)" function
20143 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020145f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020146{
20147#ifdef FEAT_SYN_HL
20148 long lnum;
20149 long col;
20150 int i;
20151 int id;
20152#endif
20153
20154 rettv->v_type = VAR_LIST;
20155 rettv->vval.v_list = NULL;
20156
20157#ifdef FEAT_SYN_HL
20158 lnum = get_tv_lnum(argvars); /* -1 on type error */
20159 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20160
20161 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020162 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020163 && rettv_list_alloc(rettv) != FAIL)
20164 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020165 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020166 for (i = 0; ; ++i)
20167 {
20168 id = syn_get_stack_item(i);
20169 if (id < 0)
20170 break;
20171 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20172 break;
20173 }
20174 }
20175#endif
20176}
20177
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020179get_cmd_output_as_rettv(
20180 typval_T *argvars,
20181 typval_T *rettv,
20182 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020184 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020186 char_u *infile = NULL;
20187 char_u buf[NUMBUFLEN];
20188 int err = FALSE;
20189 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020190 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020191 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020193 rettv->v_type = VAR_STRING;
20194 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020195 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020196 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020197
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020198 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020199 {
20200 /*
20201 * Write the string to a temp file, to be used for input of the shell
20202 * command.
20203 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020204 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020205 {
20206 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020207 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020208 }
20209
20210 fd = mch_fopen((char *)infile, WRITEBIN);
20211 if (fd == NULL)
20212 {
20213 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020214 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020215 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020216 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020217 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020218 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20219 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020220 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020221 else
20222 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020223 size_t len;
20224
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020225 p = get_tv_string_buf_chk(&argvars[1], buf);
20226 if (p == NULL)
20227 {
20228 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020229 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020230 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020231 len = STRLEN(p);
20232 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020233 err = TRUE;
20234 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020235 if (fclose(fd) != 0)
20236 err = TRUE;
20237 if (err)
20238 {
20239 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020240 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020241 }
20242 }
20243
Bram Moolenaar52a72462014-08-29 15:53:52 +020020244 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20245 * echoes typeahead, that messes up the display. */
20246 if (!msg_silent)
20247 flags += SHELL_COOKED;
20248
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020249 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020251 int len;
20252 listitem_T *li;
20253 char_u *s = NULL;
20254 char_u *start;
20255 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020256 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257
Bram Moolenaar52a72462014-08-29 15:53:52 +020020258 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020259 if (res == NULL)
20260 goto errret;
20261
20262 list = list_alloc();
20263 if (list == NULL)
20264 goto errret;
20265
20266 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020268 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020269 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020270 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020271 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020272
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020273 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020274 if (s == NULL)
20275 goto errret;
20276
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020277 for (p = s; start < end; ++p, ++start)
20278 *p = *start == NUL ? NL : *start;
20279 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020280
20281 li = listitem_alloc();
20282 if (li == NULL)
20283 {
20284 vim_free(s);
20285 goto errret;
20286 }
20287 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020288 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020289 li->li_tv.vval.v_string = s;
20290 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020292
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020293 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020294 rettv->v_type = VAR_LIST;
20295 rettv->vval.v_list = list;
20296 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020298 else
20299 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020300 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020301#ifdef USE_CR
20302 /* translate <CR> into <NL> */
20303 if (res != NULL)
20304 {
20305 char_u *s;
20306
20307 for (s = res; *s; ++s)
20308 {
20309 if (*s == CAR)
20310 *s = NL;
20311 }
20312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313#else
20314# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020315 /* translate <CR><NL> into <NL> */
20316 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020318 char_u *s, *d;
20319
20320 d = res;
20321 for (s = res; *s; ++s)
20322 {
20323 if (s[0] == CAR && s[1] == NL)
20324 ++s;
20325 *d++ = *s;
20326 }
20327 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329# endif
20330#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020331 rettv->vval.v_string = res;
20332 res = NULL;
20333 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020334
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020335errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020336 if (infile != NULL)
20337 {
20338 mch_remove(infile);
20339 vim_free(infile);
20340 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020341 if (res != NULL)
20342 vim_free(res);
20343 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020344 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020345}
20346
20347/*
20348 * "system()" function
20349 */
20350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020351f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020352{
20353 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20354}
20355
20356/*
20357 * "systemlist()" function
20358 */
20359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020360f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020361{
20362 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363}
20364
20365/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020366 * "tabpagebuflist()" function
20367 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020369f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020370{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020371#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020372 tabpage_T *tp;
20373 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020374
20375 if (argvars[0].v_type == VAR_UNKNOWN)
20376 wp = firstwin;
20377 else
20378 {
20379 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20380 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020381 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020382 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020383 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020384 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020385 for (; wp != NULL; wp = wp->w_next)
20386 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020387 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020388 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020389 }
20390#endif
20391}
20392
20393
20394/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020395 * "tabpagenr()" function
20396 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020398f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020399{
20400 int nr = 1;
20401#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020402 char_u *arg;
20403
20404 if (argvars[0].v_type != VAR_UNKNOWN)
20405 {
20406 arg = get_tv_string_chk(&argvars[0]);
20407 nr = 0;
20408 if (arg != NULL)
20409 {
20410 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020411 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020412 else
20413 EMSG2(_(e_invexpr2), arg);
20414 }
20415 }
20416 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020417 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020418#endif
20419 rettv->vval.v_number = nr;
20420}
20421
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020422
20423#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020424static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020425
20426/*
20427 * Common code for tabpagewinnr() and winnr().
20428 */
20429 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020430get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020431{
20432 win_T *twin;
20433 int nr = 1;
20434 win_T *wp;
20435 char_u *arg;
20436
20437 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20438 if (argvar->v_type != VAR_UNKNOWN)
20439 {
20440 arg = get_tv_string_chk(argvar);
20441 if (arg == NULL)
20442 nr = 0; /* type error; errmsg already given */
20443 else if (STRCMP(arg, "$") == 0)
20444 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20445 else if (STRCMP(arg, "#") == 0)
20446 {
20447 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20448 if (twin == NULL)
20449 nr = 0;
20450 }
20451 else
20452 {
20453 EMSG2(_(e_invexpr2), arg);
20454 nr = 0;
20455 }
20456 }
20457
20458 if (nr > 0)
20459 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20460 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020461 {
20462 if (wp == NULL)
20463 {
20464 /* didn't find it in this tabpage */
20465 nr = 0;
20466 break;
20467 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020468 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020469 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020470 return nr;
20471}
20472#endif
20473
20474/*
20475 * "tabpagewinnr()" function
20476 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020478f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020479{
20480 int nr = 1;
20481#ifdef FEAT_WINDOWS
20482 tabpage_T *tp;
20483
20484 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20485 if (tp == NULL)
20486 nr = 0;
20487 else
20488 nr = get_winnr(tp, &argvars[1]);
20489#endif
20490 rettv->vval.v_number = nr;
20491}
20492
20493
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020494/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020495 * "tagfiles()" function
20496 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020498f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020499{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020500 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020501 tagname_T tn;
20502 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020503
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020504 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020505 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020506 fname = alloc(MAXPATHL);
20507 if (fname == NULL)
20508 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020509
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020510 for (first = TRUE; ; first = FALSE)
20511 if (get_tagfname(&tn, first, fname) == FAIL
20512 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020513 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020514 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020515 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020516}
20517
20518/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020519 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020520 */
20521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020522f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020523{
20524 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020525
20526 tag_pattern = get_tv_string(&argvars[0]);
20527
20528 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020529 if (*tag_pattern == NUL)
20530 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020531
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020532 if (rettv_list_alloc(rettv) == OK)
20533 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020534}
20535
20536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 * "tempname()" function
20538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020540f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541{
20542 static int x = 'A';
20543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020544 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020545 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546
20547 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20548 * names. Skip 'I' and 'O', they are used for shell redirection. */
20549 do
20550 {
20551 if (x == 'Z')
20552 x = '0';
20553 else if (x == '9')
20554 x = 'A';
20555 else
20556 {
20557#ifdef EBCDIC
20558 if (x == 'I')
20559 x = 'J';
20560 else if (x == 'R')
20561 x = 'S';
20562 else
20563#endif
20564 ++x;
20565 }
20566 } while (x == 'I' || x == 'O');
20567}
20568
20569/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020570 * "test(list)" function: Just checking the walls...
20571 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020573f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020574{
20575 /* Used for unit testing. Change the code below to your liking. */
20576#if 0
20577 listitem_T *li;
20578 list_T *l;
20579 char_u *bad, *good;
20580
20581 if (argvars[0].v_type != VAR_LIST)
20582 return;
20583 l = argvars[0].vval.v_list;
20584 if (l == NULL)
20585 return;
20586 li = l->lv_first;
20587 if (li == NULL)
20588 return;
20589 bad = get_tv_string(&li->li_tv);
20590 li = li->li_next;
20591 if (li == NULL)
20592 return;
20593 good = get_tv_string(&li->li_tv);
20594 rettv->vval.v_number = test_edit_score(bad, good);
20595#endif
20596}
20597
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020598#ifdef FEAT_FLOAT
20599/*
20600 * "tan()" function
20601 */
20602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020603f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020604{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020605 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020606
20607 rettv->v_type = VAR_FLOAT;
20608 if (get_float_arg(argvars, &f) == OK)
20609 rettv->vval.v_float = tan(f);
20610 else
20611 rettv->vval.v_float = 0.0;
20612}
20613
20614/*
20615 * "tanh()" function
20616 */
20617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020618f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020619{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020620 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020621
20622 rettv->v_type = VAR_FLOAT;
20623 if (get_float_arg(argvars, &f) == OK)
20624 rettv->vval.v_float = tanh(f);
20625 else
20626 rettv->vval.v_float = 0.0;
20627}
20628#endif
20629
Bram Moolenaar975b5272016-03-15 23:10:59 +010020630#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20631/*
20632 * Get a callback from "arg". It can be a Funcref or a function name.
20633 * When "arg" is zero return an empty string.
20634 * Return NULL for an invalid argument.
20635 */
20636 char_u *
20637get_callback(typval_T *arg, partial_T **pp)
20638{
20639 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20640 {
20641 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020642 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020643 return (*pp)->pt_name;
20644 }
20645 *pp = NULL;
20646 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20647 return arg->vval.v_string;
20648 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20649 return (char_u *)"";
20650 EMSG(_("E921: Invalid callback argument"));
20651 return NULL;
20652}
20653#endif
20654
20655#ifdef FEAT_TIMERS
20656/*
20657 * "timer_start(time, callback [, options])" function
20658 */
20659 static void
20660f_timer_start(typval_T *argvars, typval_T *rettv)
20661{
20662 long msec = get_tv_number(&argvars[0]);
20663 timer_T *timer;
20664 int repeat = 0;
20665 char_u *callback;
20666 dict_T *dict;
20667
20668 if (argvars[2].v_type != VAR_UNKNOWN)
20669 {
20670 if (argvars[2].v_type != VAR_DICT
20671 || (dict = argvars[2].vval.v_dict) == NULL)
20672 {
20673 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20674 return;
20675 }
20676 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20677 repeat = get_dict_number(dict, (char_u *)"repeat");
20678 }
20679
20680 timer = create_timer(msec, repeat);
20681 callback = get_callback(&argvars[1], &timer->tr_partial);
20682 if (callback == NULL)
20683 {
20684 stop_timer(timer);
20685 rettv->vval.v_number = -1;
20686 }
20687 else
20688 {
20689 timer->tr_callback = vim_strsave(callback);
20690 rettv->vval.v_number = timer->tr_id;
20691 }
20692}
20693
20694/*
20695 * "timer_stop(timer)" function
20696 */
20697 static void
20698f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20699{
20700 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20701
20702 if (timer != NULL)
20703 stop_timer(timer);
20704}
20705#endif
20706
Bram Moolenaard52d9742005-08-21 22:20:28 +000020707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 * "tolower(string)" function
20709 */
20710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020711f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712{
20713 char_u *p;
20714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020715 p = vim_strsave(get_tv_string(&argvars[0]));
20716 rettv->v_type = VAR_STRING;
20717 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718
20719 if (p != NULL)
20720 while (*p != NUL)
20721 {
20722#ifdef FEAT_MBYTE
20723 int l;
20724
20725 if (enc_utf8)
20726 {
20727 int c, lc;
20728
20729 c = utf_ptr2char(p);
20730 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020731 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 /* TODO: reallocate string when byte count changes. */
20733 if (utf_char2len(lc) == l)
20734 utf_char2bytes(lc, p);
20735 p += l;
20736 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020737 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 p += l; /* skip multi-byte character */
20739 else
20740#endif
20741 {
20742 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20743 ++p;
20744 }
20745 }
20746}
20747
20748/*
20749 * "toupper(string)" function
20750 */
20751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020752f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020754 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020755 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756}
20757
20758/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020759 * "tr(string, fromstr, tostr)" function
20760 */
20761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020762f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020763{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020764 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020765 char_u *fromstr;
20766 char_u *tostr;
20767 char_u *p;
20768#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020769 int inlen;
20770 int fromlen;
20771 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020772 int idx;
20773 char_u *cpstr;
20774 int cplen;
20775 int first = TRUE;
20776#endif
20777 char_u buf[NUMBUFLEN];
20778 char_u buf2[NUMBUFLEN];
20779 garray_T ga;
20780
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020781 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020782 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20783 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020784
20785 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020786 rettv->v_type = VAR_STRING;
20787 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020788 if (fromstr == NULL || tostr == NULL)
20789 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020790 ga_init2(&ga, (int)sizeof(char), 80);
20791
20792#ifdef FEAT_MBYTE
20793 if (!has_mbyte)
20794#endif
20795 /* not multi-byte: fromstr and tostr must be the same length */
20796 if (STRLEN(fromstr) != STRLEN(tostr))
20797 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020798#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020799error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020800#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020801 EMSG2(_(e_invarg2), fromstr);
20802 ga_clear(&ga);
20803 return;
20804 }
20805
20806 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020807 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020808 {
20809#ifdef FEAT_MBYTE
20810 if (has_mbyte)
20811 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020812 inlen = (*mb_ptr2len)(in_str);
20813 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020814 cplen = inlen;
20815 idx = 0;
20816 for (p = fromstr; *p != NUL; p += fromlen)
20817 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020818 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020819 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020820 {
20821 for (p = tostr; *p != NUL; p += tolen)
20822 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020823 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020824 if (idx-- == 0)
20825 {
20826 cplen = tolen;
20827 cpstr = p;
20828 break;
20829 }
20830 }
20831 if (*p == NUL) /* tostr is shorter than fromstr */
20832 goto error;
20833 break;
20834 }
20835 ++idx;
20836 }
20837
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020838 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020839 {
20840 /* Check that fromstr and tostr have the same number of
20841 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020842 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020843 first = FALSE;
20844 for (p = tostr; *p != NUL; p += tolen)
20845 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020846 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020847 --idx;
20848 }
20849 if (idx != 0)
20850 goto error;
20851 }
20852
Bram Moolenaarcde88542015-08-11 19:14:00 +020020853 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020854 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020855 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020856
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020857 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020858 }
20859 else
20860#endif
20861 {
20862 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020863 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020864 if (p != NULL)
20865 ga_append(&ga, tostr[p - fromstr]);
20866 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020867 ga_append(&ga, *in_str);
20868 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020869 }
20870 }
20871
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020872 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020873 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020874 ga_append(&ga, NUL);
20875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020876 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020877}
20878
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020879#ifdef FEAT_FLOAT
20880/*
20881 * "trunc({float})" function
20882 */
20883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020884f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020885{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020886 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020887
20888 rettv->v_type = VAR_FLOAT;
20889 if (get_float_arg(argvars, &f) == OK)
20890 /* trunc() is not in C90, use floor() or ceil() instead. */
20891 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20892 else
20893 rettv->vval.v_float = 0.0;
20894}
20895#endif
20896
Bram Moolenaar8299df92004-07-10 09:47:34 +000020897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 * "type(expr)" function
20899 */
20900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020901f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020903 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020904
20905 switch (argvars[0].v_type)
20906 {
20907 case VAR_NUMBER: n = 0; break;
20908 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020909 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020910 case VAR_FUNC: n = 2; break;
20911 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020912 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020913 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020914 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020915 if (argvars[0].vval.v_number == VVAL_FALSE
20916 || argvars[0].vval.v_number == VVAL_TRUE)
20917 n = 6;
20918 else
20919 n = 7;
20920 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020921 case VAR_JOB: n = 8; break;
20922 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020923 case VAR_UNKNOWN:
20924 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20925 n = -1;
20926 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020927 }
20928 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929}
20930
20931/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020932 * "undofile(name)" function
20933 */
20934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020935f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020936{
20937 rettv->v_type = VAR_STRING;
20938#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020939 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020940 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020941
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020942 if (*fname == NUL)
20943 {
20944 /* If there is no file name there will be no undo file. */
20945 rettv->vval.v_string = NULL;
20946 }
20947 else
20948 {
20949 char_u *ffname = FullName_save(fname, FALSE);
20950
20951 if (ffname != NULL)
20952 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20953 vim_free(ffname);
20954 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020955 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020956#else
20957 rettv->vval.v_string = NULL;
20958#endif
20959}
20960
20961/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020962 * "undotree()" function
20963 */
20964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020965f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020966{
20967 if (rettv_dict_alloc(rettv) == OK)
20968 {
20969 dict_T *dict = rettv->vval.v_dict;
20970 list_T *list;
20971
Bram Moolenaar730cde92010-06-27 05:18:54 +020020972 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020973 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020974 dict_add_nr_str(dict, "save_last",
20975 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020976 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20977 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020978 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020979
20980 list = list_alloc();
20981 if (list != NULL)
20982 {
20983 u_eval_tree(curbuf->b_u_oldhead, list);
20984 dict_add_list(dict, "entries", list);
20985 }
20986 }
20987}
20988
20989/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020990 * "values(dict)" function
20991 */
20992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020993f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020994{
20995 dict_list(argvars, rettv, 1);
20996}
20997
20998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 * "virtcol(string)" function
21000 */
21001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021002f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003{
21004 colnr_T vcol = 0;
21005 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021006 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021008 fp = var2fpos(&argvars[0], FALSE, &fnum);
21009 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21010 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 {
21012 getvvcol(curwin, fp, NULL, NULL, &vcol);
21013 ++vcol;
21014 }
21015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017}
21018
21019/*
21020 * "visualmode()" function
21021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021023f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 char_u str[2];
21026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021027 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 str[0] = curbuf->b_visual_mode_eval;
21029 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021030 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031
21032 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021033 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035}
21036
21037/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021038 * "wildmenumode()" function
21039 */
21040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021041f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021042{
21043#ifdef FEAT_WILDMENU
21044 if (wild_menu_showing)
21045 rettv->vval.v_number = 1;
21046#endif
21047}
21048
21049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 * "winbufnr(nr)" function
21051 */
21052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021053f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054{
21055 win_T *wp;
21056
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021057 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021059 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021061 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062}
21063
21064/*
21065 * "wincol()" function
21066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021068f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069{
21070 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021071 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072}
21073
21074/*
21075 * "winheight(nr)" function
21076 */
21077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021078f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079{
21080 win_T *wp;
21081
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021082 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021084 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021086 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087}
21088
21089/*
21090 * "winline()" function
21091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021093f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094{
21095 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021096 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097}
21098
21099/*
21100 * "winnr()" function
21101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021103f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104{
21105 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021106
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021108 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021110 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111}
21112
21113/*
21114 * "winrestcmd()" function
21115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021117f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118{
21119#ifdef FEAT_WINDOWS
21120 win_T *wp;
21121 int winnr = 1;
21122 garray_T ga;
21123 char_u buf[50];
21124
21125 ga_init2(&ga, (int)sizeof(char), 70);
21126 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21127 {
21128 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21129 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21131 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 ++winnr;
21133 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021134 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021136 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021138 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021140 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141}
21142
21143/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021144 * "winrestview()" function
21145 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021147f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021148{
21149 dict_T *dict;
21150
21151 if (argvars[0].v_type != VAR_DICT
21152 || (dict = argvars[0].vval.v_dict) == NULL)
21153 EMSG(_(e_invarg));
21154 else
21155 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021156 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21157 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21158 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21159 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021160#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021161 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21162 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021163#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021164 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21165 {
21166 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21167 curwin->w_set_curswant = FALSE;
21168 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021169
Bram Moolenaar82c25852014-05-28 16:47:16 +020021170 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21171 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021172#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021173 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21174 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021175#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021176 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21177 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21178 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21179 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021180
21181 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021182 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021183# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021184 win_new_width(curwin, W_WIDTH(curwin));
21185# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021186 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021187
Bram Moolenaarb851a962014-10-31 15:45:52 +010021188 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021189 curwin->w_topline = 1;
21190 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21191 curwin->w_topline = curbuf->b_ml.ml_line_count;
21192#ifdef FEAT_DIFF
21193 check_topfill(curwin, TRUE);
21194#endif
21195 }
21196}
21197
21198/*
21199 * "winsaveview()" function
21200 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021202f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021203{
21204 dict_T *dict;
21205
Bram Moolenaara800b422010-06-27 01:15:55 +020021206 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021207 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021208 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021209
21210 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21211 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21212#ifdef FEAT_VIRTUALEDIT
21213 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21214#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021215 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021216 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21217
21218 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21219#ifdef FEAT_DIFF
21220 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21221#endif
21222 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21223 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21224}
21225
21226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 * "winwidth(nr)" function
21228 */
21229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021230f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231{
21232 win_T *wp;
21233
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021234 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021236 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021238#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021239 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021241 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242#endif
21243}
21244
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021246 * "wordcount()" function
21247 */
21248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021249f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021250{
21251 if (rettv_dict_alloc(rettv) == FAIL)
21252 return;
21253 cursor_pos_info(rettv->vval.v_dict);
21254}
21255
21256/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021257 * Write list of strings to file
21258 */
21259 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021260write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021261{
21262 listitem_T *li;
21263 int c;
21264 int ret = OK;
21265 char_u *s;
21266
21267 for (li = list->lv_first; li != NULL; li = li->li_next)
21268 {
21269 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21270 {
21271 if (*s == '\n')
21272 c = putc(NUL, fd);
21273 else
21274 c = putc(*s, fd);
21275 if (c == EOF)
21276 {
21277 ret = FAIL;
21278 break;
21279 }
21280 }
21281 if (!binary || li->li_next != NULL)
21282 if (putc('\n', fd) == EOF)
21283 {
21284 ret = FAIL;
21285 break;
21286 }
21287 if (ret == FAIL)
21288 {
21289 EMSG(_(e_write));
21290 break;
21291 }
21292 }
21293 return ret;
21294}
21295
21296/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021297 * "writefile()" function
21298 */
21299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021300f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021301{
21302 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021303 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021304 char_u *fname;
21305 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021306 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021307
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021308 if (check_restricted() || check_secure())
21309 return;
21310
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021311 if (argvars[0].v_type != VAR_LIST)
21312 {
21313 EMSG2(_(e_listarg), "writefile()");
21314 return;
21315 }
21316 if (argvars[0].vval.v_list == NULL)
21317 return;
21318
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021319 if (argvars[2].v_type != VAR_UNKNOWN)
21320 {
21321 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21322 binary = TRUE;
21323 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21324 append = TRUE;
21325 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021326
21327 /* Always open the file in binary mode, library functions have a mind of
21328 * their own about CR-LF conversion. */
21329 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021330 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21331 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021332 {
21333 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21334 ret = -1;
21335 }
21336 else
21337 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021338 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21339 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021340 fclose(fd);
21341 }
21342
21343 rettv->vval.v_number = ret;
21344}
21345
21346/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021347 * "xor(expr, expr)" function
21348 */
21349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021350f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021351{
21352 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21353 ^ get_tv_number_chk(&argvars[1], NULL);
21354}
21355
21356
21357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021359 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 */
21361 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021362var2fpos(
21363 typval_T *varp,
21364 int dollar_lnum, /* TRUE when $ is last line */
21365 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021367 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021369 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370
Bram Moolenaara5525202006-03-02 22:52:09 +000021371 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021372 if (varp->v_type == VAR_LIST)
21373 {
21374 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021375 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021376 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021377 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021378
21379 l = varp->vval.v_list;
21380 if (l == NULL)
21381 return NULL;
21382
21383 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021384 pos.lnum = list_find_nr(l, 0L, &error);
21385 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021386 return NULL; /* invalid line number */
21387
21388 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021389 pos.col = list_find_nr(l, 1L, &error);
21390 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021391 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021392 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021393
21394 /* We accept "$" for the column number: last column. */
21395 li = list_find(l, 1L);
21396 if (li != NULL && li->li_tv.v_type == VAR_STRING
21397 && li->li_tv.vval.v_string != NULL
21398 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21399 pos.col = len + 1;
21400
Bram Moolenaara5525202006-03-02 22:52:09 +000021401 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021402 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021403 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021404 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021405
Bram Moolenaara5525202006-03-02 22:52:09 +000021406#ifdef FEAT_VIRTUALEDIT
21407 /* Get the virtual offset. Defaults to zero. */
21408 pos.coladd = list_find_nr(l, 2L, &error);
21409 if (error)
21410 pos.coladd = 0;
21411#endif
21412
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021413 return &pos;
21414 }
21415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021416 name = get_tv_string_chk(varp);
21417 if (name == NULL)
21418 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021419 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021421 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21422 {
21423 if (VIsual_active)
21424 return &VIsual;
21425 return &curwin->w_cursor;
21426 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021427 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021429 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21431 return NULL;
21432 return pp;
21433 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021434
21435#ifdef FEAT_VIRTUALEDIT
21436 pos.coladd = 0;
21437#endif
21438
Bram Moolenaar477933c2007-07-17 14:32:23 +000021439 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021440 {
21441 pos.col = 0;
21442 if (name[1] == '0') /* "w0": first visible line */
21443 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021444 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021445 pos.lnum = curwin->w_topline;
21446 return &pos;
21447 }
21448 else if (name[1] == '$') /* "w$": last visible line */
21449 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021450 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021451 pos.lnum = curwin->w_botline - 1;
21452 return &pos;
21453 }
21454 }
21455 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021457 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 {
21459 pos.lnum = curbuf->b_ml.ml_line_count;
21460 pos.col = 0;
21461 }
21462 else
21463 {
21464 pos.lnum = curwin->w_cursor.lnum;
21465 pos.col = (colnr_T)STRLEN(ml_get_curline());
21466 }
21467 return &pos;
21468 }
21469 return NULL;
21470}
21471
21472/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021473 * Convert list in "arg" into a position and optional file number.
21474 * When "fnump" is NULL there is no file number, only 3 items.
21475 * Note that the column is passed on as-is, the caller may want to decrement
21476 * it to use 1 for the first column.
21477 * Return FAIL when conversion is not possible, doesn't check the position for
21478 * validity.
21479 */
21480 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021481list2fpos(
21482 typval_T *arg,
21483 pos_T *posp,
21484 int *fnump,
21485 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021486{
21487 list_T *l = arg->vval.v_list;
21488 long i = 0;
21489 long n;
21490
Bram Moolenaar493c1782014-05-28 14:34:46 +020021491 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21492 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021493 if (arg->v_type != VAR_LIST
21494 || l == NULL
21495 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021496 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021497 return FAIL;
21498
21499 if (fnump != NULL)
21500 {
21501 n = list_find_nr(l, i++, NULL); /* fnum */
21502 if (n < 0)
21503 return FAIL;
21504 if (n == 0)
21505 n = curbuf->b_fnum; /* current buffer */
21506 *fnump = n;
21507 }
21508
21509 n = list_find_nr(l, i++, NULL); /* lnum */
21510 if (n < 0)
21511 return FAIL;
21512 posp->lnum = n;
21513
21514 n = list_find_nr(l, i++, NULL); /* col */
21515 if (n < 0)
21516 return FAIL;
21517 posp->col = n;
21518
21519#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021520 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021521 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021522 posp->coladd = 0;
21523 else
21524 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021525#endif
21526
Bram Moolenaar493c1782014-05-28 14:34:46 +020021527 if (curswantp != NULL)
21528 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21529
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021530 return OK;
21531}
21532
21533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 * Get the length of an environment variable name.
21535 * Advance "arg" to the first character after the name.
21536 * Return 0 for error.
21537 */
21538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021539get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540{
21541 char_u *p;
21542 int len;
21543
21544 for (p = *arg; vim_isIDc(*p); ++p)
21545 ;
21546 if (p == *arg) /* no name found */
21547 return 0;
21548
21549 len = (int)(p - *arg);
21550 *arg = p;
21551 return len;
21552}
21553
21554/*
21555 * Get the length of the name of a function or internal variable.
21556 * "arg" is advanced to the first non-white character after the name.
21557 * Return 0 if something is wrong.
21558 */
21559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021560get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561{
21562 char_u *p;
21563 int len;
21564
21565 /* Find the end of the name. */
21566 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021567 {
21568 if (*p == ':')
21569 {
21570 /* "s:" is start of "s:var", but "n:" is not and can be used in
21571 * slice "[n:]". Also "xx:" is not a namespace. */
21572 len = (int)(p - *arg);
21573 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21574 || len > 1)
21575 break;
21576 }
21577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 if (p == *arg) /* no name found */
21579 return 0;
21580
21581 len = (int)(p - *arg);
21582 *arg = skipwhite(p);
21583
21584 return len;
21585}
21586
21587/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021588 * Get the length of the name of a variable or function.
21589 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021591 * Return -1 if curly braces expansion failed.
21592 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 * If the name contains 'magic' {}'s, expand them and return the
21594 * expanded name in an allocated string via 'alias' - caller must free.
21595 */
21596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021597get_name_len(
21598 char_u **arg,
21599 char_u **alias,
21600 int evaluate,
21601 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602{
21603 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 char_u *p;
21605 char_u *expr_start;
21606 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607
21608 *alias = NULL; /* default to no alias */
21609
21610 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21611 && (*arg)[2] == (int)KE_SNR)
21612 {
21613 /* hard coded <SNR>, already translated */
21614 *arg += 3;
21615 return get_id_len(arg) + 3;
21616 }
21617 len = eval_fname_script(*arg);
21618 if (len > 0)
21619 {
21620 /* literal "<SID>", "s:" or "<SNR>" */
21621 *arg += len;
21622 }
21623
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021627 p = find_name_end(*arg, &expr_start, &expr_end,
21628 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 if (expr_start != NULL)
21630 {
21631 char_u *temp_string;
21632
21633 if (!evaluate)
21634 {
21635 len += (int)(p - *arg);
21636 *arg = skipwhite(p);
21637 return len;
21638 }
21639
21640 /*
21641 * Include any <SID> etc in the expanded string:
21642 * Thus the -len here.
21643 */
21644 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21645 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021646 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 *alias = temp_string;
21648 *arg = skipwhite(p);
21649 return (int)STRLEN(temp_string);
21650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651
21652 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021653 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 EMSG2(_(e_invexpr2), *arg);
21655
21656 return len;
21657}
21658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021659/*
21660 * Find the end of a variable or function name, taking care of magic braces.
21661 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21662 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021663 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021664 * Return a pointer to just after the name. Equal to "arg" if there is no
21665 * valid name.
21666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021668find_name_end(
21669 char_u *arg,
21670 char_u **expr_start,
21671 char_u **expr_end,
21672 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021674 int mb_nest = 0;
21675 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021677 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021679 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021681 *expr_start = NULL;
21682 *expr_end = NULL;
21683 }
21684
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021685 /* Quick check for valid starting character. */
21686 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21687 return arg;
21688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021689 for (p = arg; *p != NUL
21690 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021691 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021692 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021693 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021694 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021695 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021696 if (*p == '\'')
21697 {
21698 /* skip over 'string' to avoid counting [ and ] inside it. */
21699 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21700 ;
21701 if (*p == NUL)
21702 break;
21703 }
21704 else if (*p == '"')
21705 {
21706 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21707 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21708 if (*p == '\\' && p[1] != NUL)
21709 ++p;
21710 if (*p == NUL)
21711 break;
21712 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021713 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21714 {
21715 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021716 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021717 len = (int)(p - arg);
21718 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021719 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021720 break;
21721 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021723 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021725 if (*p == '[')
21726 ++br_nest;
21727 else if (*p == ']')
21728 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021731 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021733 if (*p == '{')
21734 {
21735 mb_nest++;
21736 if (expr_start != NULL && *expr_start == NULL)
21737 *expr_start = p;
21738 }
21739 else if (*p == '}')
21740 {
21741 mb_nest--;
21742 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21743 *expr_end = p;
21744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 }
21747
21748 return p;
21749}
21750
21751/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021752 * Expands out the 'magic' {}'s in a variable/function name.
21753 * Note that this can call itself recursively, to deal with
21754 * constructs like foo{bar}{baz}{bam}
21755 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21756 * "in_start" ^
21757 * "expr_start" ^
21758 * "expr_end" ^
21759 * "in_end" ^
21760 *
21761 * Returns a new allocated string, which the caller must free.
21762 * Returns NULL for failure.
21763 */
21764 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021765make_expanded_name(
21766 char_u *in_start,
21767 char_u *expr_start,
21768 char_u *expr_end,
21769 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021770{
21771 char_u c1;
21772 char_u *retval = NULL;
21773 char_u *temp_result;
21774 char_u *nextcmd = NULL;
21775
21776 if (expr_end == NULL || in_end == NULL)
21777 return NULL;
21778 *expr_start = NUL;
21779 *expr_end = NUL;
21780 c1 = *in_end;
21781 *in_end = NUL;
21782
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021783 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021784 if (temp_result != NULL && nextcmd == NULL)
21785 {
21786 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21787 + (in_end - expr_end) + 1));
21788 if (retval != NULL)
21789 {
21790 STRCPY(retval, in_start);
21791 STRCAT(retval, temp_result);
21792 STRCAT(retval, expr_end + 1);
21793 }
21794 }
21795 vim_free(temp_result);
21796
21797 *in_end = c1; /* put char back for error messages */
21798 *expr_start = '{';
21799 *expr_end = '}';
21800
21801 if (retval != NULL)
21802 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021803 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021804 if (expr_start != NULL)
21805 {
21806 /* Further expansion! */
21807 temp_result = make_expanded_name(retval, expr_start,
21808 expr_end, temp_result);
21809 vim_free(retval);
21810 retval = temp_result;
21811 }
21812 }
21813
21814 return retval;
21815}
21816
21817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021819 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 */
21821 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021822eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021824 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21825}
21826
21827/*
21828 * Return TRUE if character "c" can be used as the first character in a
21829 * variable or function name (excluding '{' and '}').
21830 */
21831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021832eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021833{
21834 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835}
21836
21837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 * Set number v: variable to "val".
21839 */
21840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021841set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021843 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844}
21845
21846/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021847 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 */
21849 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021850get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021852 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853}
21854
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021855/*
21856 * Get string v: variable value. Uses a static buffer, can only be used once.
21857 */
21858 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021859get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021860{
21861 return get_tv_string(&vimvars[idx].vv_tv);
21862}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021863
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021865 * Get List v: variable value. Caller must take care of reference count when
21866 * needed.
21867 */
21868 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021869get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021870{
21871 return vimvars[idx].vv_list;
21872}
21873
21874/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021875 * Set v:char to character "c".
21876 */
21877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021878set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021879{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021880 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021881
21882#ifdef FEAT_MBYTE
21883 if (has_mbyte)
21884 buf[(*mb_char2bytes)(c, buf)] = NUL;
21885 else
21886#endif
21887 {
21888 buf[0] = c;
21889 buf[1] = NUL;
21890 }
21891 set_vim_var_string(VV_CHAR, buf, -1);
21892}
21893
21894/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021895 * Set v:count to "count" and v:count1 to "count1".
21896 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 */
21898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021899set_vcount(
21900 long count,
21901 long count1,
21902 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021904 if (set_prevcount)
21905 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021906 vimvars[VV_COUNT].vv_nr = count;
21907 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908}
21909
21910/*
21911 * Set string v: variable to a copy of "val".
21912 */
21913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021914set_vim_var_string(
21915 int idx,
21916 char_u *val,
21917 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918{
Bram Moolenaara542c682016-01-31 16:28:04 +010021919 clear_tv(&vimvars[idx].vv_di.di_tv);
21920 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021922 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021924 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021926 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927}
21928
21929/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021930 * Set List v: variable to "val".
21931 */
21932 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021933set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021934{
Bram Moolenaara542c682016-01-31 16:28:04 +010021935 clear_tv(&vimvars[idx].vv_di.di_tv);
21936 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021937 vimvars[idx].vv_list = val;
21938 if (val != NULL)
21939 ++val->lv_refcount;
21940}
21941
21942/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021943 * Set Dictionary v: variable to "val".
21944 */
21945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021946set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021947{
21948 int todo;
21949 hashitem_T *hi;
21950
Bram Moolenaara542c682016-01-31 16:28:04 +010021951 clear_tv(&vimvars[idx].vv_di.di_tv);
21952 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021953 vimvars[idx].vv_dict = val;
21954 if (val != NULL)
21955 {
21956 ++val->dv_refcount;
21957
21958 /* Set readonly */
21959 todo = (int)val->dv_hashtab.ht_used;
21960 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21961 {
21962 if (HASHITEM_EMPTY(hi))
21963 continue;
21964 --todo;
21965 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21966 }
21967 }
21968}
21969
21970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 * Set v:register if needed.
21972 */
21973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021974set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975{
21976 char_u regname;
21977
21978 if (c == 0 || c == ' ')
21979 regname = '"';
21980 else
21981 regname = c;
21982 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021983 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 set_vim_var_string(VV_REG, &regname, 1);
21985}
21986
21987/*
21988 * Get or set v:exception. If "oldval" == NULL, return the current value.
21989 * Otherwise, restore the value to "oldval" and return NULL.
21990 * Must always be called in pairs to save and restore v:exception! Does not
21991 * take care of memory allocations.
21992 */
21993 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021994v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995{
21996 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021997 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998
Bram Moolenaare9a41262005-01-15 22:18:47 +000021999 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 return NULL;
22001}
22002
22003/*
22004 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22005 * Otherwise, restore the value to "oldval" and return NULL.
22006 * Must always be called in pairs to save and restore v:throwpoint! Does not
22007 * take care of memory allocations.
22008 */
22009 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022010v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011{
22012 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022013 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014
Bram Moolenaare9a41262005-01-15 22:18:47 +000022015 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 return NULL;
22017}
22018
22019#if defined(FEAT_AUTOCMD) || defined(PROTO)
22020/*
22021 * Set v:cmdarg.
22022 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22023 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22024 * Must always be called in pairs!
22025 */
22026 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022027set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028{
22029 char_u *oldval;
22030 char_u *newval;
22031 unsigned len;
22032
Bram Moolenaare9a41262005-01-15 22:18:47 +000022033 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022034 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022036 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022037 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022038 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 }
22040
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022041 if (eap->force_bin == FORCE_BIN)
22042 len = 6;
22043 else if (eap->force_bin == FORCE_NOBIN)
22044 len = 8;
22045 else
22046 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022047
22048 if (eap->read_edit)
22049 len += 7;
22050
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022051 if (eap->force_ff != 0)
22052 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22053# ifdef FEAT_MBYTE
22054 if (eap->force_enc != 0)
22055 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022056 if (eap->bad_char != 0)
22057 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022058# endif
22059
22060 newval = alloc(len + 1);
22061 if (newval == NULL)
22062 return NULL;
22063
22064 if (eap->force_bin == FORCE_BIN)
22065 sprintf((char *)newval, " ++bin");
22066 else if (eap->force_bin == FORCE_NOBIN)
22067 sprintf((char *)newval, " ++nobin");
22068 else
22069 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022070
22071 if (eap->read_edit)
22072 STRCAT(newval, " ++edit");
22073
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022074 if (eap->force_ff != 0)
22075 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22076 eap->cmd + eap->force_ff);
22077# ifdef FEAT_MBYTE
22078 if (eap->force_enc != 0)
22079 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22080 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022081 if (eap->bad_char == BAD_KEEP)
22082 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22083 else if (eap->bad_char == BAD_DROP)
22084 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22085 else if (eap->bad_char != 0)
22086 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022087# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022088 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022089 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090}
22091#endif
22092
22093/*
22094 * Get the value of internal variable "name".
22095 * Return OK or FAIL.
22096 */
22097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022098get_var_tv(
22099 char_u *name,
22100 int len, /* length of "name" */
22101 typval_T *rettv, /* NULL when only checking existence */
22102 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22103 int verbose, /* may give error message */
22104 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105{
22106 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022107 typval_T *tv = NULL;
22108 typval_T atv;
22109 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111
22112 /* truncate the name, so that we can use strcmp() */
22113 cc = name[len];
22114 name[len] = NUL;
22115
22116 /*
22117 * Check for "b:changedtick".
22118 */
22119 if (STRCMP(name, "b:changedtick") == 0)
22120 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022121 atv.v_type = VAR_NUMBER;
22122 atv.vval.v_number = curbuf->b_changedtick;
22123 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124 }
22125
22126 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 * Check for user-defined variables.
22128 */
22129 else
22130 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022131 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022133 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022134 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022135 if (dip != NULL)
22136 *dip = v;
22137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 }
22139
Bram Moolenaare9a41262005-01-15 22:18:47 +000022140 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022142 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022143 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 ret = FAIL;
22145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022146 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022147 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148
22149 name[len] = cc;
22150
22151 return ret;
22152}
22153
22154/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022155 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22156 * Also handle function call with Funcref variable: func(expr)
22157 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22158 */
22159 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022160handle_subscript(
22161 char_u **arg,
22162 typval_T *rettv,
22163 int evaluate, /* do more than finding the end */
22164 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022165{
22166 int ret = OK;
22167 dict_T *selfdict = NULL;
22168 char_u *s;
22169 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022170 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022171
22172 while (ret == OK
22173 && (**arg == '['
22174 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022175 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22176 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022177 && !vim_iswhite(*(*arg - 1)))
22178 {
22179 if (**arg == '(')
22180 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022181 partial_T *pt = NULL;
22182
Bram Moolenaard9fba312005-06-26 22:34:35 +000022183 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022184 if (evaluate)
22185 {
22186 functv = *rettv;
22187 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022188
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022189 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022190 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022191 {
22192 pt = functv.vval.v_partial;
22193 s = pt->pt_name;
22194 }
22195 else
22196 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022197 }
22198 else
22199 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022200 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022201 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022202 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022203
22204 /* Clear the funcref afterwards, so that deleting it while
22205 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022206 if (evaluate)
22207 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022208
22209 /* Stop the expression evaluation when immediately aborting on
22210 * error, or when an interrupt occurred or an exception was thrown
22211 * but not caught. */
22212 if (aborting())
22213 {
22214 if (ret == OK)
22215 clear_tv(rettv);
22216 ret = FAIL;
22217 }
22218 dict_unref(selfdict);
22219 selfdict = NULL;
22220 }
22221 else /* **arg == '[' || **arg == '.' */
22222 {
22223 dict_unref(selfdict);
22224 if (rettv->v_type == VAR_DICT)
22225 {
22226 selfdict = rettv->vval.v_dict;
22227 if (selfdict != NULL)
22228 ++selfdict->dv_refcount;
22229 }
22230 else
22231 selfdict = NULL;
22232 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22233 {
22234 clear_tv(rettv);
22235 ret = FAIL;
22236 }
22237 }
22238 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022239
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022240 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
22241 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022242 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022243 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22244 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022245 char_u *tofree = NULL;
22246 ufunc_T *fp;
22247 char_u fname_buf[FLEN_FIXED + 1];
22248 int error;
22249
22250 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022251 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022252 fp = find_func(fname);
22253 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022254
22255 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010022256 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022257 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022258 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22259
22260 if (pt != NULL)
22261 {
22262 pt->pt_refcount = 1;
22263 pt->pt_dict = selfdict;
22264 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022265 if (rettv->v_type == VAR_FUNC)
22266 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022267 /* Just a function: Take over the function name and use
22268 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022269 pt->pt_name = rettv->vval.v_string;
22270 }
22271 else
22272 {
22273 partial_T *ret_pt = rettv->vval.v_partial;
22274 int i;
22275
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022276 /* Partial: copy the function name, use selfdict and copy
22277 * args. Can't take over name or args, the partial might
22278 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022279 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022280 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022281 if (ret_pt->pt_argc > 0)
22282 {
22283 pt->pt_argv = (typval_T *)alloc(
22284 sizeof(typval_T) * ret_pt->pt_argc);
22285 if (pt->pt_argv == NULL)
22286 /* out of memory: drop the arguments */
22287 pt->pt_argc = 0;
22288 else
22289 {
22290 pt->pt_argc = ret_pt->pt_argc;
22291 for (i = 0; i < pt->pt_argc; i++)
22292 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22293 }
22294 }
22295 partial_unref(ret_pt);
22296 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022297 rettv->v_type = VAR_PARTIAL;
22298 rettv->vval.v_partial = pt;
22299 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022300 }
22301 }
22302
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022303 dict_unref(selfdict);
22304 return ret;
22305}
22306
22307/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022308 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022309 * value).
22310 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022311 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022312alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022313{
Bram Moolenaar33570922005-01-25 22:26:29 +000022314 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022315}
22316
22317/*
22318 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 * The string "s" must have been allocated, it is consumed.
22320 * Return NULL for out of memory, the variable otherwise.
22321 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022322 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022323alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324{
Bram Moolenaar33570922005-01-25 22:26:29 +000022325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022327 rettv = alloc_tv();
22328 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022330 rettv->v_type = VAR_STRING;
22331 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 }
22333 else
22334 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022335 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336}
22337
22338/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022339 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022342free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343{
22344 if (varp != NULL)
22345 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022346 switch (varp->v_type)
22347 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022348 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022349 func_unref(varp->vval.v_string);
22350 /*FALLTHROUGH*/
22351 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022352 vim_free(varp->vval.v_string);
22353 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022354 case VAR_PARTIAL:
22355 partial_unref(varp->vval.v_partial);
22356 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022357 case VAR_LIST:
22358 list_unref(varp->vval.v_list);
22359 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022360 case VAR_DICT:
22361 dict_unref(varp->vval.v_dict);
22362 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022363 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022364#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022365 job_unref(varp->vval.v_job);
22366 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022367#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022368 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022369#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022370 channel_unref(varp->vval.v_channel);
22371 break;
22372#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022373 case VAR_NUMBER:
22374 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022375 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022376 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022377 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 vim_free(varp);
22380 }
22381}
22382
22383/*
22384 * Free the memory for a variable value and set the value to NULL or 0.
22385 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022386 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022387clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388{
22389 if (varp != NULL)
22390 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022391 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022393 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022394 func_unref(varp->vval.v_string);
22395 /*FALLTHROUGH*/
22396 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022397 vim_free(varp->vval.v_string);
22398 varp->vval.v_string = NULL;
22399 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022400 case VAR_PARTIAL:
22401 partial_unref(varp->vval.v_partial);
22402 varp->vval.v_partial = NULL;
22403 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022404 case VAR_LIST:
22405 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022406 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022407 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022408 case VAR_DICT:
22409 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022410 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022411 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022412 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022413 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022414 varp->vval.v_number = 0;
22415 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022416 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022417#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022418 varp->vval.v_float = 0.0;
22419 break;
22420#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022421 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022422#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022423 job_unref(varp->vval.v_job);
22424 varp->vval.v_job = NULL;
22425#endif
22426 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022427 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022428#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022429 channel_unref(varp->vval.v_channel);
22430 varp->vval.v_channel = NULL;
22431#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022432 case VAR_UNKNOWN:
22433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022435 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 }
22437}
22438
22439/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022440 * Set the value of a variable to NULL without freeing items.
22441 */
22442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022443init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022444{
22445 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022446 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022447}
22448
22449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 * Get the number value of a variable.
22451 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022452 * For incompatible types, return 0.
22453 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22454 * caller of incompatible types: it sets *denote to TRUE if "denote"
22455 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022457 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022458get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022460 int error = FALSE;
22461
22462 return get_tv_number_chk(varp, &error); /* return 0L on error */
22463}
22464
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022465 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022466get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022467{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022468 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022470 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022472 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022473 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022474 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022475#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022476 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022477 break;
22478#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022479 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022480 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022481 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022482 break;
22483 case VAR_STRING:
22484 if (varp->vval.v_string != NULL)
22485 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022486 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022487 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022488 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022489 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022490 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022491 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022492 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022493 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022494 case VAR_SPECIAL:
22495 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
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 EMSG(_("E910: Using a Job as a Number"));
22500 break;
22501#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 EMSG(_("E913: Using a Channel as a Number"));
22505 break;
22506#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022507 case VAR_UNKNOWN:
22508 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022509 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022511 if (denote == NULL) /* useful for values that must be unsigned */
22512 n = -1;
22513 else
22514 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022515 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516}
22517
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022518#ifdef FEAT_FLOAT
22519 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022520get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022521{
22522 switch (varp->v_type)
22523 {
22524 case VAR_NUMBER:
22525 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022526 case VAR_FLOAT:
22527 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022528 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022529 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022530 EMSG(_("E891: Using a Funcref as a Float"));
22531 break;
22532 case VAR_STRING:
22533 EMSG(_("E892: Using a String as a Float"));
22534 break;
22535 case VAR_LIST:
22536 EMSG(_("E893: Using a List as a Float"));
22537 break;
22538 case VAR_DICT:
22539 EMSG(_("E894: Using a Dictionary as a Float"));
22540 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022541 case VAR_SPECIAL:
22542 EMSG(_("E907: Using a special value as a Float"));
22543 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022544 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022545# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022546 EMSG(_("E911: Using a Job as a Float"));
22547 break;
22548# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022549 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022550# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022551 EMSG(_("E914: Using a Channel as a Float"));
22552 break;
22553# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022554 case VAR_UNKNOWN:
22555 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022556 break;
22557 }
22558 return 0;
22559}
22560#endif
22561
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022563 * Get the lnum from the first argument.
22564 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022565 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 */
22567 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022568get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569{
Bram Moolenaar33570922005-01-25 22:26:29 +000022570 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 linenr_T lnum;
22572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022573 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 if (lnum == 0) /* no valid number, try using line() */
22575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022576 rettv.v_type = VAR_NUMBER;
22577 f_line(argvars, &rettv);
22578 lnum = rettv.vval.v_number;
22579 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 }
22581 return lnum;
22582}
22583
22584/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022585 * Get the lnum from the first argument.
22586 * Also accepts "$", then "buf" is used.
22587 * Returns 0 on error.
22588 */
22589 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022590get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022591{
22592 if (argvars[0].v_type == VAR_STRING
22593 && argvars[0].vval.v_string != NULL
22594 && argvars[0].vval.v_string[0] == '$'
22595 && buf != NULL)
22596 return buf->b_ml.ml_line_count;
22597 return get_tv_number_chk(&argvars[0], NULL);
22598}
22599
22600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 * Get the string value of a variable.
22602 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022603 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22604 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 * If the String variable has never been set, return an empty string.
22606 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022607 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22608 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022610 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022611get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612{
22613 static char_u mybuf[NUMBUFLEN];
22614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022615 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616}
22617
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022618 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022619get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022621 char_u *res = get_tv_string_buf_chk(varp, buf);
22622
22623 return res != NULL ? res : (char_u *)"";
22624}
22625
Bram Moolenaar7d647822014-04-05 21:28:56 +020022626/*
22627 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22628 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022629 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022630get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022631{
22632 static char_u mybuf[NUMBUFLEN];
22633
22634 return get_tv_string_buf_chk(varp, mybuf);
22635}
22636
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022637 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022638get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022639{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022640 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022642 case VAR_NUMBER:
22643 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22644 return buf;
22645 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022646 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022647 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022648 break;
22649 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022650 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022651 break;
22652 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022653 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022654 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022655 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022656#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022657 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022658 break;
22659#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022660 case VAR_STRING:
22661 if (varp->vval.v_string != NULL)
22662 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022663 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022664 case VAR_SPECIAL:
22665 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22666 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022667 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022668#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022669 {
22670 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022671 char *status;
22672
22673 if (job == NULL)
22674 return (char_u *)"no process";
22675 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022676 : job->jv_status == JOB_ENDED ? "dead"
22677 : "run";
22678# ifdef UNIX
22679 vim_snprintf((char *)buf, NUMBUFLEN,
22680 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022681# elif defined(WIN32)
22682 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022683 "process %ld %s",
22684 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022685 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022686# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022687 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022688 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22689# endif
22690 return buf;
22691 }
22692#endif
22693 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022694 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022695#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022696 {
22697 channel_T *channel = varp->vval.v_channel;
22698 char *status = channel_status(channel);
22699
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022700 if (channel == NULL)
22701 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22702 else
22703 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022704 "channel %d %s", channel->ch_id, status);
22705 return buf;
22706 }
22707#endif
22708 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022709 case VAR_UNKNOWN:
22710 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022711 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022713 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714}
22715
22716/*
22717 * Find variable "name" in the list of variables.
22718 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022719 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022720 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022721 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022723 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022724find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728
Bram Moolenaara7043832005-01-21 11:56:39 +000022729 ht = find_var_ht(name, &varname);
22730 if (htp != NULL)
22731 *htp = ht;
22732 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022734 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735}
22736
22737/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022738 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022739 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022741 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022742find_var_in_ht(
22743 hashtab_T *ht,
22744 int htname,
22745 char_u *varname,
22746 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022747{
Bram Moolenaar33570922005-01-25 22:26:29 +000022748 hashitem_T *hi;
22749
22750 if (*varname == NUL)
22751 {
22752 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022753 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022755 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022756 case 'g': return &globvars_var;
22757 case 'v': return &vimvars_var;
22758 case 'b': return &curbuf->b_bufvar;
22759 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022760#ifdef FEAT_WINDOWS
22761 case 't': return &curtab->tp_winvar;
22762#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022763 case 'l': return current_funccal == NULL
22764 ? NULL : &current_funccal->l_vars_var;
22765 case 'a': return current_funccal == NULL
22766 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022767 }
22768 return NULL;
22769 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022770
22771 hi = hash_find(ht, varname);
22772 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022773 {
22774 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022775 * worked find the variable again. Don't auto-load a script if it was
22776 * loaded already, otherwise it would be loaded every time when
22777 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022778 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022779 {
22780 /* Note: script_autoload() may make "hi" invalid. It must either
22781 * be obtained again or not used. */
22782 if (!script_autoload(varname, FALSE) || aborting())
22783 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022784 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022785 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022786 if (HASHITEM_EMPTY(hi))
22787 return NULL;
22788 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022789 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022790}
22791
22792/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022793 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022794 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022795 * Set "varname" to the start of name without ':'.
22796 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022797 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022798find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022800 hashitem_T *hi;
22801
Bram Moolenaar73627d02015-08-11 15:46:09 +020022802 if (name[0] == NUL)
22803 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 if (name[1] != ':')
22805 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022806 /* The name must not start with a colon or #. */
22807 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 return NULL;
22809 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022810
22811 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022812 hi = hash_find(&compat_hashtab, name);
22813 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022814 return &compat_hashtab;
22815
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022817 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022818 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 }
22820 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022821 if (*name == 'g') /* global variable */
22822 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022823 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22824 */
22825 if (vim_strchr(name + 2, ':') != NULL
22826 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022827 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022829 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022831 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022832#ifdef FEAT_WINDOWS
22833 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022834 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022835#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022836 if (*name == 'v') /* v: variable */
22837 return &vimvarht;
22838 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022839 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022840 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022841 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 if (*name == 's' /* script variable */
22843 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22844 return &SCRIPT_VARS(current_SID);
22845 return NULL;
22846}
22847
22848/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022849 * Get function call environment based on bactrace debug level
22850 */
22851 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022852get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022853{
22854 int i;
22855 funccall_T *funccal;
22856 funccall_T *temp_funccal;
22857
22858 funccal = current_funccal;
22859 if (debug_backtrace_level > 0)
22860 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022861 for (i = 0; i < debug_backtrace_level; i++)
22862 {
22863 temp_funccal = funccal->caller;
22864 if (temp_funccal)
22865 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022866 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022867 /* backtrace level overflow. reset to max */
22868 debug_backtrace_level = i;
22869 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022870 }
22871 return funccal;
22872}
22873
22874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022876 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 * Returns NULL when it doesn't exist.
22878 */
22879 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022880get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881{
Bram Moolenaar33570922005-01-25 22:26:29 +000022882 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022884 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 if (v == NULL)
22886 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022887 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888}
22889
22890/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022891 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 * sourcing this script and when executing functions defined in the script.
22893 */
22894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022895new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896{
Bram Moolenaara7043832005-01-21 11:56:39 +000022897 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022898 hashtab_T *ht;
22899 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022900
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22902 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022903 /* Re-allocating ga_data means that an ht_array pointing to
22904 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022905 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022906 for (i = 1; i <= ga_scripts.ga_len; ++i)
22907 {
22908 ht = &SCRIPT_VARS(i);
22909 if (ht->ht_mask == HT_INIT_SIZE - 1)
22910 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022911 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022912 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022913 }
22914
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 while (ga_scripts.ga_len < id)
22916 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022917 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022918 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022919 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 }
22922 }
22923}
22924
22925/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022926 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22927 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 */
22929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022930init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931{
Bram Moolenaar33570922005-01-25 22:26:29 +000022932 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022933 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022934 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022935 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022936 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022937 dict_var->di_tv.vval.v_dict = dict;
22938 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022939 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022940 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22941 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942}
22943
22944/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022945 * Unreference a dictionary initialized by init_var_dict().
22946 */
22947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022948unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022949{
22950 /* Now the dict needs to be freed if no one else is using it, go back to
22951 * normal reference counting. */
22952 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22953 dict_unref(dict);
22954}
22955
22956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022958 * Frees all allocated variables and the value they contain.
22959 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960 */
22961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022962vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022963{
22964 vars_clear_ext(ht, TRUE);
22965}
22966
22967/*
22968 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22969 */
22970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022971vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972{
Bram Moolenaara7043832005-01-21 11:56:39 +000022973 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022974 hashitem_T *hi;
22975 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976
Bram Moolenaar33570922005-01-25 22:26:29 +000022977 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022978 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022979 for (hi = ht->ht_array; todo > 0; ++hi)
22980 {
22981 if (!HASHITEM_EMPTY(hi))
22982 {
22983 --todo;
22984
Bram Moolenaar33570922005-01-25 22:26:29 +000022985 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022986 * ht_array might change then. hash_clear() takes care of it
22987 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022988 v = HI2DI(hi);
22989 if (free_val)
22990 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022991 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022992 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022993 }
22994 }
22995 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022996 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997}
22998
Bram Moolenaara7043832005-01-21 11:56:39 +000022999/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023000 * Delete a variable from hashtab "ht" at item "hi".
23001 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023004delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005{
Bram Moolenaar33570922005-01-25 22:26:29 +000023006 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023007
23008 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023009 clear_tv(&di->di_tv);
23010 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011}
23012
23013/*
23014 * List the value of one internal variable.
23015 */
23016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023017list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023019 char_u *tofree;
23020 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023021 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023022
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023023 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023025 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023026 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027}
23028
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023030list_one_var_a(
23031 char_u *prefix,
23032 char_u *name,
23033 int type,
23034 char_u *string,
23035 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023036{
Bram Moolenaar31859182007-08-14 20:41:13 +000023037 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23038 msg_start();
23039 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 if (name != NULL) /* "a:" vars don't have a name stored */
23041 msg_puts(name);
23042 msg_putchar(' ');
23043 msg_advance(22);
23044 if (type == VAR_NUMBER)
23045 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023046 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023047 msg_putchar('*');
23048 else if (type == VAR_LIST)
23049 {
23050 msg_putchar('[');
23051 if (*string == '[')
23052 ++string;
23053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023054 else if (type == VAR_DICT)
23055 {
23056 msg_putchar('{');
23057 if (*string == '{')
23058 ++string;
23059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 else
23061 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023062
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023064
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023065 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023066 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023067 if (*first)
23068 {
23069 msg_clr_eos();
23070 *first = FALSE;
23071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072}
23073
23074/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023075 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 * If the variable already exists, the value is updated.
23077 * Otherwise the variable is created.
23078 */
23079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023080set_var(
23081 char_u *name,
23082 typval_T *tv,
23083 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084{
Bram Moolenaar33570922005-01-25 22:26:29 +000023085 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023087 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023089 ht = find_var_ht(name, &varname);
23090 if (ht == NULL || *varname == NUL)
23091 {
23092 EMSG2(_(e_illvar), name);
23093 return;
23094 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023095 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023096
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023097 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23098 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023099 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023100
Bram Moolenaar33570922005-01-25 22:26:29 +000023101 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023103 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023104 if (var_check_ro(v->di_flags, name, FALSE)
23105 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023106 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023107
23108 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023109 * Handle setting internal v: variables separately where needed to
23110 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023111 */
23112 if (ht == &vimvarht)
23113 {
23114 if (v->di_tv.v_type == VAR_STRING)
23115 {
23116 vim_free(v->di_tv.vval.v_string);
23117 if (copy || tv->v_type != VAR_STRING)
23118 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23119 else
23120 {
23121 /* Take over the string to avoid an extra alloc/free. */
23122 v->di_tv.vval.v_string = tv->vval.v_string;
23123 tv->vval.v_string = NULL;
23124 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023125 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023126 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023127 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023128 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023129 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023130 if (STRCMP(varname, "searchforward") == 0)
23131 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023132#ifdef FEAT_SEARCH_EXTRA
23133 else if (STRCMP(varname, "hlsearch") == 0)
23134 {
23135 no_hlsearch = !v->di_tv.vval.v_number;
23136 redraw_all_later(SOME_VALID);
23137 }
23138#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023139 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023140 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023141 else if (v->di_tv.v_type != tv->v_type)
23142 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023143 }
23144
23145 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 }
23147 else /* add a new variable */
23148 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023149 /* Can't add "v:" variable. */
23150 if (ht == &vimvarht)
23151 {
23152 EMSG2(_(e_illvar), name);
23153 return;
23154 }
23155
Bram Moolenaar92124a32005-06-17 22:03:40 +000023156 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023157 if (!valid_varname(varname))
23158 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023159
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023160 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23161 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023162 if (v == NULL)
23163 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023164 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023165 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023167 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 return;
23169 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023170 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023172
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023173 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023174 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023175 else
23176 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023177 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023178 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023179 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181}
23182
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023183/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023184 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023185 * Also give an error message.
23186 */
23187 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023188var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023189{
23190 if (flags & DI_FLAGS_RO)
23191 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023192 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023193 return TRUE;
23194 }
23195 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23196 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023197 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023198 return TRUE;
23199 }
23200 return FALSE;
23201}
23202
23203/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023204 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23205 * Also give an error message.
23206 */
23207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023208var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023209{
23210 if (flags & DI_FLAGS_FIX)
23211 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023212 EMSG2(_("E795: Cannot delete variable %s"),
23213 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023214 return TRUE;
23215 }
23216 return FALSE;
23217}
23218
23219/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023220 * Check if a funcref is assigned to a valid variable name.
23221 * Return TRUE and give an error if not.
23222 */
23223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023224var_check_func_name(
23225 char_u *name, /* points to start of variable name */
23226 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023227{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023228 /* Allow for w: b: s: and t:. */
23229 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023230 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23231 ? name[2] : name[0]))
23232 {
23233 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23234 name);
23235 return TRUE;
23236 }
23237 /* Don't allow hiding a function. When "v" is not NULL we might be
23238 * assigning another function to the same var, the type is checked
23239 * below. */
23240 if (new_var && function_exists(name))
23241 {
23242 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23243 name);
23244 return TRUE;
23245 }
23246 return FALSE;
23247}
23248
23249/*
23250 * Check if a variable name is valid.
23251 * Return FALSE and give an error if not.
23252 */
23253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023254valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023255{
23256 char_u *p;
23257
23258 for (p = varname; *p != NUL; ++p)
23259 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23260 && *p != AUTOLOAD_CHAR)
23261 {
23262 EMSG2(_(e_illvar), varname);
23263 return FALSE;
23264 }
23265 return TRUE;
23266}
23267
23268/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023269 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023270 * Also give an error message, using "name" or _("name") when use_gettext is
23271 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023272 */
23273 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023274tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023275{
23276 if (lock & VAR_LOCKED)
23277 {
23278 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023279 name == NULL ? (char_u *)_("Unknown")
23280 : use_gettext ? (char_u *)_(name)
23281 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023282 return TRUE;
23283 }
23284 if (lock & VAR_FIXED)
23285 {
23286 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023287 name == NULL ? (char_u *)_("Unknown")
23288 : use_gettext ? (char_u *)_(name)
23289 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023290 return TRUE;
23291 }
23292 return FALSE;
23293}
23294
23295/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023296 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023297 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023298 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023299 * It is OK for "from" and "to" to point to the same item. This is used to
23300 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023301 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023303copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023305 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023306 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023307 switch (from->v_type)
23308 {
23309 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023310 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023311 to->vval.v_number = from->vval.v_number;
23312 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023313 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023314#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023315 to->vval.v_float = from->vval.v_float;
23316 break;
23317#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023318 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023319#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023320 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023321 if (to->vval.v_job != NULL)
23322 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023323 break;
23324#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023325 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023327 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023328 if (to->vval.v_channel != NULL)
23329 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023330 break;
23331#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023332 case VAR_STRING:
23333 case VAR_FUNC:
23334 if (from->vval.v_string == NULL)
23335 to->vval.v_string = NULL;
23336 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023337 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023338 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023339 if (from->v_type == VAR_FUNC)
23340 func_ref(to->vval.v_string);
23341 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023342 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023343 case VAR_PARTIAL:
23344 if (from->vval.v_partial == NULL)
23345 to->vval.v_partial = NULL;
23346 else
23347 {
23348 to->vval.v_partial = from->vval.v_partial;
23349 ++to->vval.v_partial->pt_refcount;
23350 }
23351 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023352 case VAR_LIST:
23353 if (from->vval.v_list == NULL)
23354 to->vval.v_list = NULL;
23355 else
23356 {
23357 to->vval.v_list = from->vval.v_list;
23358 ++to->vval.v_list->lv_refcount;
23359 }
23360 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023361 case VAR_DICT:
23362 if (from->vval.v_dict == NULL)
23363 to->vval.v_dict = NULL;
23364 else
23365 {
23366 to->vval.v_dict = from->vval.v_dict;
23367 ++to->vval.v_dict->dv_refcount;
23368 }
23369 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023370 case VAR_UNKNOWN:
23371 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023372 break;
23373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374}
23375
23376/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023377 * Make a copy of an item.
23378 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023379 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23380 * reference to an already copied list/dict can be used.
23381 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023382 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023384item_copy(
23385 typval_T *from,
23386 typval_T *to,
23387 int deep,
23388 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023389{
23390 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023391 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023392
Bram Moolenaar33570922005-01-25 22:26:29 +000023393 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023394 {
23395 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023396 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023397 }
23398 ++recurse;
23399
23400 switch (from->v_type)
23401 {
23402 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023403 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023404 case VAR_STRING:
23405 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023406 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023407 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023408 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023409 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023410 copy_tv(from, to);
23411 break;
23412 case VAR_LIST:
23413 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023414 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023415 if (from->vval.v_list == NULL)
23416 to->vval.v_list = NULL;
23417 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23418 {
23419 /* use the copy made earlier */
23420 to->vval.v_list = from->vval.v_list->lv_copylist;
23421 ++to->vval.v_list->lv_refcount;
23422 }
23423 else
23424 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23425 if (to->vval.v_list == NULL)
23426 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023427 break;
23428 case VAR_DICT:
23429 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023430 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023431 if (from->vval.v_dict == NULL)
23432 to->vval.v_dict = NULL;
23433 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23434 {
23435 /* use the copy made earlier */
23436 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23437 ++to->vval.v_dict->dv_refcount;
23438 }
23439 else
23440 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23441 if (to->vval.v_dict == NULL)
23442 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023443 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023444 case VAR_UNKNOWN:
23445 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023446 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023447 }
23448 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023449 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023450}
23451
23452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 * ":echo expr1 ..." print each argument separated with a space, add a
23454 * newline at the end.
23455 * ":echon expr1 ..." print each argument plain.
23456 */
23457 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023458ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459{
23460 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023461 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023462 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023463 char_u *p;
23464 int needclr = TRUE;
23465 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023466 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467
23468 if (eap->skip)
23469 ++emsg_skip;
23470 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23471 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023472 /* If eval1() causes an error message the text from the command may
23473 * still need to be cleared. E.g., "echo 22,44". */
23474 need_clr_eos = needclr;
23475
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023477 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 {
23479 /*
23480 * Report the invalid expression unless the expression evaluation
23481 * has been cancelled due to an aborting error, an interrupt, or an
23482 * exception.
23483 */
23484 if (!aborting())
23485 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023486 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 break;
23488 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023489 need_clr_eos = FALSE;
23490
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 if (!eap->skip)
23492 {
23493 if (atstart)
23494 {
23495 atstart = FALSE;
23496 /* Call msg_start() after eval1(), evaluating the expression
23497 * may cause a message to appear. */
23498 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023499 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023500 /* Mark the saved text as finishing the line, so that what
23501 * follows is displayed on a new line when scrolling back
23502 * at the more prompt. */
23503 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023504 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 }
23507 else if (eap->cmdidx == CMD_echo)
23508 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023509 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023510 if (p != NULL)
23511 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023513 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023515 if (*p != TAB && needclr)
23516 {
23517 /* remove any text still there from the command */
23518 msg_clr_eos();
23519 needclr = FALSE;
23520 }
23521 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 }
23523 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023524 {
23525#ifdef FEAT_MBYTE
23526 if (has_mbyte)
23527 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023528 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023529
23530 (void)msg_outtrans_len_attr(p, i, echo_attr);
23531 p += i - 1;
23532 }
23533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023535 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023538 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023540 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 arg = skipwhite(arg);
23542 }
23543 eap->nextcmd = check_nextcmd(arg);
23544
23545 if (eap->skip)
23546 --emsg_skip;
23547 else
23548 {
23549 /* remove text that may still be there from the command */
23550 if (needclr)
23551 msg_clr_eos();
23552 if (eap->cmdidx == CMD_echo)
23553 msg_end();
23554 }
23555}
23556
23557/*
23558 * ":echohl {name}".
23559 */
23560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023561ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562{
23563 int id;
23564
23565 id = syn_name2id(eap->arg);
23566 if (id == 0)
23567 echo_attr = 0;
23568 else
23569 echo_attr = syn_id2attr(id);
23570}
23571
23572/*
23573 * ":execute expr1 ..." execute the result of an expression.
23574 * ":echomsg expr1 ..." Print a message
23575 * ":echoerr expr1 ..." Print an error
23576 * Each gets spaces around each argument and a newline at the end for
23577 * echo commands
23578 */
23579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023580ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581{
23582 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023583 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584 int ret = OK;
23585 char_u *p;
23586 garray_T ga;
23587 int len;
23588 int save_did_emsg;
23589
23590 ga_init2(&ga, 1, 80);
23591
23592 if (eap->skip)
23593 ++emsg_skip;
23594 while (*arg != NUL && *arg != '|' && *arg != '\n')
23595 {
23596 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023597 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598 {
23599 /*
23600 * Report the invalid expression unless the expression evaluation
23601 * has been cancelled due to an aborting error, an interrupt, or an
23602 * exception.
23603 */
23604 if (!aborting())
23605 EMSG2(_(e_invexpr2), p);
23606 ret = FAIL;
23607 break;
23608 }
23609
23610 if (!eap->skip)
23611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023612 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613 len = (int)STRLEN(p);
23614 if (ga_grow(&ga, len + 2) == FAIL)
23615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023616 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 ret = FAIL;
23618 break;
23619 }
23620 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 ga.ga_len += len;
23624 }
23625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023626 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 arg = skipwhite(arg);
23628 }
23629
23630 if (ret != FAIL && ga.ga_data != NULL)
23631 {
23632 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023635 out_flush();
23636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637 else if (eap->cmdidx == CMD_echoerr)
23638 {
23639 /* We don't want to abort following commands, restore did_emsg. */
23640 save_did_emsg = did_emsg;
23641 EMSG((char_u *)ga.ga_data);
23642 if (!force_abort)
23643 did_emsg = save_did_emsg;
23644 }
23645 else if (eap->cmdidx == CMD_execute)
23646 do_cmdline((char_u *)ga.ga_data,
23647 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23648 }
23649
23650 ga_clear(&ga);
23651
23652 if (eap->skip)
23653 --emsg_skip;
23654
23655 eap->nextcmd = check_nextcmd(arg);
23656}
23657
23658/*
23659 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23660 * "arg" points to the "&" or '+' when called, to "option" when returning.
23661 * Returns NULL when no option name found. Otherwise pointer to the char
23662 * after the option name.
23663 */
23664 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023665find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023666{
23667 char_u *p = *arg;
23668
23669 ++p;
23670 if (*p == 'g' && p[1] == ':')
23671 {
23672 *opt_flags = OPT_GLOBAL;
23673 p += 2;
23674 }
23675 else if (*p == 'l' && p[1] == ':')
23676 {
23677 *opt_flags = OPT_LOCAL;
23678 p += 2;
23679 }
23680 else
23681 *opt_flags = 0;
23682
23683 if (!ASCII_ISALPHA(*p))
23684 return NULL;
23685 *arg = p;
23686
23687 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23688 p += 4; /* termcap option */
23689 else
23690 while (ASCII_ISALPHA(*p))
23691 ++p;
23692 return p;
23693}
23694
23695/*
23696 * ":function"
23697 */
23698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023699ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700{
23701 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023702 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 int j;
23704 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023706 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 char_u *name = NULL;
23708 char_u *p;
23709 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023710 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023711 garray_T newargs;
23712 garray_T newlines;
23713 int varargs = FALSE;
23714 int mustend = FALSE;
23715 int flags = 0;
23716 ufunc_T *fp;
23717 int indent;
23718 int nesting;
23719 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023720 dictitem_T *v;
23721 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023722 static int func_nr = 0; /* number for nameless function */
23723 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023724 hashtab_T *ht;
23725 int todo;
23726 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023727 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728
23729 /*
23730 * ":function" without argument: list functions.
23731 */
23732 if (ends_excmd(*eap->arg))
23733 {
23734 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023735 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023736 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023737 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023738 {
23739 if (!HASHITEM_EMPTY(hi))
23740 {
23741 --todo;
23742 fp = HI2UF(hi);
23743 if (!isdigit(*fp->uf_name))
23744 list_func_head(fp, FALSE);
23745 }
23746 }
23747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 eap->nextcmd = check_nextcmd(eap->arg);
23749 return;
23750 }
23751
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023752 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023753 * ":function /pat": list functions matching pattern.
23754 */
23755 if (*eap->arg == '/')
23756 {
23757 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23758 if (!eap->skip)
23759 {
23760 regmatch_T regmatch;
23761
23762 c = *p;
23763 *p = NUL;
23764 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23765 *p = c;
23766 if (regmatch.regprog != NULL)
23767 {
23768 regmatch.rm_ic = p_ic;
23769
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023770 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023771 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23772 {
23773 if (!HASHITEM_EMPTY(hi))
23774 {
23775 --todo;
23776 fp = HI2UF(hi);
23777 if (!isdigit(*fp->uf_name)
23778 && vim_regexec(&regmatch, fp->uf_name, 0))
23779 list_func_head(fp, FALSE);
23780 }
23781 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023782 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023783 }
23784 }
23785 if (*p == '/')
23786 ++p;
23787 eap->nextcmd = check_nextcmd(p);
23788 return;
23789 }
23790
23791 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023792 * Get the function name. There are these situations:
23793 * func normal function name
23794 * "name" == func, "fudi.fd_dict" == NULL
23795 * dict.func new dictionary entry
23796 * "name" == NULL, "fudi.fd_dict" set,
23797 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23798 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023799 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023800 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23801 * dict.func existing dict entry that's not a Funcref
23802 * "name" == NULL, "fudi.fd_dict" set,
23803 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023804 * s:func script-local function name
23805 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023808 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023809 paren = (vim_strchr(p, '(') != NULL);
23810 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 {
23812 /*
23813 * Return on an invalid expression in braces, unless the expression
23814 * evaluation has been cancelled due to an aborting error, an
23815 * interrupt, or an exception.
23816 */
23817 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023818 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023819 if (!eap->skip && fudi.fd_newkey != NULL)
23820 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023821 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 else
23825 eap->skip = TRUE;
23826 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023827
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828 /* An error in a function call during evaluation of an expression in magic
23829 * braces should not cause the function not to be defined. */
23830 saved_did_emsg = did_emsg;
23831 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832
23833 /*
23834 * ":function func" with only function name: list function.
23835 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 {
23838 if (!ends_excmd(*skipwhite(p)))
23839 {
23840 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023841 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 }
23843 eap->nextcmd = check_nextcmd(p);
23844 if (eap->nextcmd != NULL)
23845 *p = NUL;
23846 if (!eap->skip && !got_int)
23847 {
23848 fp = find_func(name);
23849 if (fp != NULL)
23850 {
23851 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023852 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023854 if (FUNCLINE(fp, j) == NULL)
23855 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 msg_putchar('\n');
23857 msg_outnum((long)(j + 1));
23858 if (j < 9)
23859 msg_putchar(' ');
23860 if (j < 99)
23861 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023862 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 out_flush(); /* show a line at a time */
23864 ui_breakcheck();
23865 }
23866 if (!got_int)
23867 {
23868 msg_putchar('\n');
23869 msg_puts((char_u *)" endfunction");
23870 }
23871 }
23872 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023873 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023875 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 }
23877
23878 /*
23879 * ":function name(arg1, arg2)" Define function.
23880 */
23881 p = skipwhite(p);
23882 if (*p != '(')
23883 {
23884 if (!eap->skip)
23885 {
23886 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023887 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 }
23889 /* attempt to continue by skipping some text */
23890 if (vim_strchr(p, '(') != NULL)
23891 p = vim_strchr(p, '(');
23892 }
23893 p = skipwhite(p + 1);
23894
23895 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23896 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23897
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023898 if (!eap->skip)
23899 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023900 /* Check the name of the function. Unless it's a dictionary function
23901 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023902 if (name != NULL)
23903 arg = name;
23904 else
23905 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023906 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023907 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23908 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023909 {
23910 if (*arg == K_SPECIAL)
23911 j = 3;
23912 else
23913 j = 0;
23914 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23915 : eval_isnamec(arg[j])))
23916 ++j;
23917 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023918 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023919 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023920 /* Disallow using the g: dict. */
23921 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23922 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023923 }
23924
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 /*
23926 * Isolate the arguments: "arg1, arg2, ...)"
23927 */
23928 while (*p != ')')
23929 {
23930 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23931 {
23932 varargs = TRUE;
23933 p += 3;
23934 mustend = TRUE;
23935 }
23936 else
23937 {
23938 arg = p;
23939 while (ASCII_ISALNUM(*p) || *p == '_')
23940 ++p;
23941 if (arg == p || isdigit(*arg)
23942 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23943 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23944 {
23945 if (!eap->skip)
23946 EMSG2(_("E125: Illegal argument: %s"), arg);
23947 break;
23948 }
23949 if (ga_grow(&newargs, 1) == FAIL)
23950 goto erret;
23951 c = *p;
23952 *p = NUL;
23953 arg = vim_strsave(arg);
23954 if (arg == NULL)
23955 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023956
23957 /* Check for duplicate argument name. */
23958 for (i = 0; i < newargs.ga_len; ++i)
23959 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23960 {
23961 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023962 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023963 goto erret;
23964 }
23965
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23967 *p = c;
23968 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 if (*p == ',')
23970 ++p;
23971 else
23972 mustend = TRUE;
23973 }
23974 p = skipwhite(p);
23975 if (mustend && *p != ')')
23976 {
23977 if (!eap->skip)
23978 EMSG2(_(e_invarg2), eap->arg);
23979 break;
23980 }
23981 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023982 if (*p != ')')
23983 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 ++p; /* skip the ')' */
23985
Bram Moolenaare9a41262005-01-15 22:18:47 +000023986 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 for (;;)
23988 {
23989 p = skipwhite(p);
23990 if (STRNCMP(p, "range", 5) == 0)
23991 {
23992 flags |= FC_RANGE;
23993 p += 5;
23994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023995 else if (STRNCMP(p, "dict", 4) == 0)
23996 {
23997 flags |= FC_DICT;
23998 p += 4;
23999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 else if (STRNCMP(p, "abort", 5) == 0)
24001 {
24002 flags |= FC_ABORT;
24003 p += 5;
24004 }
24005 else
24006 break;
24007 }
24008
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024009 /* When there is a line break use what follows for the function body.
24010 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24011 if (*p == '\n')
24012 line_arg = p + 1;
24013 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 EMSG(_(e_trailing));
24015
24016 /*
24017 * Read the body of the function, until ":endfunction" is found.
24018 */
24019 if (KeyTyped)
24020 {
24021 /* Check if the function already exists, don't let the user type the
24022 * whole function before telling him it doesn't work! For a script we
24023 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024024 if (!eap->skip && !eap->forceit)
24025 {
24026 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24027 EMSG(_(e_funcdict));
24028 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024029 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024032 if (!eap->skip && did_emsg)
24033 goto erret;
24034
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 msg_putchar('\n'); /* don't overwrite the function name */
24036 cmdline_row = msg_row;
24037 }
24038
24039 indent = 2;
24040 nesting = 0;
24041 for (;;)
24042 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024043 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024044 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024045 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024046 saved_wait_return = FALSE;
24047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024049 sourcing_lnum_off = sourcing_lnum;
24050
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024051 if (line_arg != NULL)
24052 {
24053 /* Use eap->arg, split up in parts by line breaks. */
24054 theline = line_arg;
24055 p = vim_strchr(theline, '\n');
24056 if (p == NULL)
24057 line_arg += STRLEN(line_arg);
24058 else
24059 {
24060 *p = NUL;
24061 line_arg = p + 1;
24062 }
24063 }
24064 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024065 theline = getcmdline(':', 0L, indent);
24066 else
24067 theline = eap->getline(':', eap->cookie, indent);
24068 if (KeyTyped)
24069 lines_left = Rows - 1;
24070 if (theline == NULL)
24071 {
24072 EMSG(_("E126: Missing :endfunction"));
24073 goto erret;
24074 }
24075
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024076 /* Detect line continuation: sourcing_lnum increased more than one. */
24077 if (sourcing_lnum > sourcing_lnum_off + 1)
24078 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24079 else
24080 sourcing_lnum_off = 0;
24081
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 if (skip_until != NULL)
24083 {
24084 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24085 * don't check for ":endfunc". */
24086 if (STRCMP(theline, skip_until) == 0)
24087 {
24088 vim_free(skip_until);
24089 skip_until = NULL;
24090 }
24091 }
24092 else
24093 {
24094 /* skip ':' and blanks*/
24095 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24096 ;
24097
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024098 /* Check for "endfunction". */
24099 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024101 if (line_arg == NULL)
24102 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 break;
24104 }
24105
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024106 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 * at "end". */
24108 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24109 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024110 else if (STRNCMP(p, "if", 2) == 0
24111 || STRNCMP(p, "wh", 2) == 0
24112 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 || STRNCMP(p, "try", 3) == 0)
24114 indent += 2;
24115
24116 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024117 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024119 if (*p == '!')
24120 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024122 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024123 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024125 ++nesting;
24126 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024127 }
24128 }
24129
24130 /* Check for ":append" or ":insert". */
24131 p = skip_range(p, NULL);
24132 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24133 || (p[0] == 'i'
24134 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24135 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24136 skip_until = vim_strsave((char_u *)".");
24137
24138 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24139 arg = skipwhite(skiptowhite(p));
24140 if (arg[0] == '<' && arg[1] =='<'
24141 && ((p[0] == 'p' && p[1] == 'y'
24142 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24143 || (p[0] == 'p' && p[1] == 'e'
24144 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24145 || (p[0] == 't' && p[1] == 'c'
24146 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024147 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24148 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24150 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024151 || (p[0] == 'm' && p[1] == 'z'
24152 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 ))
24154 {
24155 /* ":python <<" continues until a dot, like ":append" */
24156 p = skipwhite(arg + 2);
24157 if (*p == NUL)
24158 skip_until = vim_strsave((char_u *)".");
24159 else
24160 skip_until = vim_strsave(p);
24161 }
24162 }
24163
24164 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024165 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024166 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024167 if (line_arg == NULL)
24168 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024170 }
24171
24172 /* Copy the line to newly allocated memory. get_one_sourceline()
24173 * allocates 250 bytes per line, this saves 80% on average. The cost
24174 * is an extra alloc/free. */
24175 p = vim_strsave(theline);
24176 if (p != NULL)
24177 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024178 if (line_arg == NULL)
24179 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024180 theline = p;
24181 }
24182
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024183 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24184
24185 /* Add NULL lines for continuation lines, so that the line count is
24186 * equal to the index in the growarray. */
24187 while (sourcing_lnum_off-- > 0)
24188 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024189
24190 /* Check for end of eap->arg. */
24191 if (line_arg != NULL && *line_arg == NUL)
24192 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193 }
24194
24195 /* Don't define the function when skipping commands or when an error was
24196 * detected. */
24197 if (eap->skip || did_emsg)
24198 goto erret;
24199
24200 /*
24201 * If there are no errors, add the function
24202 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024203 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024204 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024205 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024206 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024207 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024208 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024209 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024210 goto erret;
24211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024212
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024213 fp = find_func(name);
24214 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024216 if (!eap->forceit)
24217 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024218 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024219 goto erret;
24220 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024221 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024222 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024223 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024224 name);
24225 goto erret;
24226 }
24227 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024228 ga_clear_strings(&(fp->uf_args));
24229 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024230 vim_free(name);
24231 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233 }
24234 else
24235 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024236 char numbuf[20];
24237
24238 fp = NULL;
24239 if (fudi.fd_newkey == NULL && !eap->forceit)
24240 {
24241 EMSG(_(e_funcdict));
24242 goto erret;
24243 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024244 if (fudi.fd_di == NULL)
24245 {
24246 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024247 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024248 goto erret;
24249 }
24250 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024251 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024252 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024253
24254 /* Give the function a sequential number. Can only be used with a
24255 * Funcref! */
24256 vim_free(name);
24257 sprintf(numbuf, "%d", ++func_nr);
24258 name = vim_strsave((char_u *)numbuf);
24259 if (name == NULL)
24260 goto erret;
24261 }
24262
24263 if (fp == NULL)
24264 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024265 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024266 {
24267 int slen, plen;
24268 char_u *scriptname;
24269
24270 /* Check that the autoload name matches the script name. */
24271 j = FAIL;
24272 if (sourcing_name != NULL)
24273 {
24274 scriptname = autoload_name(name);
24275 if (scriptname != NULL)
24276 {
24277 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024278 plen = (int)STRLEN(p);
24279 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024280 if (slen > plen && fnamecmp(p,
24281 sourcing_name + slen - plen) == 0)
24282 j = OK;
24283 vim_free(scriptname);
24284 }
24285 }
24286 if (j == FAIL)
24287 {
24288 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24289 goto erret;
24290 }
24291 }
24292
24293 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294 if (fp == NULL)
24295 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024296
24297 if (fudi.fd_dict != NULL)
24298 {
24299 if (fudi.fd_di == NULL)
24300 {
24301 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024302 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024303 if (fudi.fd_di == NULL)
24304 {
24305 vim_free(fp);
24306 goto erret;
24307 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024308 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24309 {
24310 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024311 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024312 goto erret;
24313 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024314 }
24315 else
24316 /* overwrite existing dict entry */
24317 clear_tv(&fudi.fd_di->di_tv);
24318 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024319 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024320 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024321 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024322
24323 /* behave like "dict" was used */
24324 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024325 }
24326
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024328 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024329 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24330 {
24331 vim_free(fp);
24332 goto erret;
24333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024335 fp->uf_args = newargs;
24336 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024337#ifdef FEAT_PROFILE
24338 fp->uf_tml_count = NULL;
24339 fp->uf_tml_total = NULL;
24340 fp->uf_tml_self = NULL;
24341 fp->uf_profiling = FALSE;
24342 if (prof_def_func())
24343 func_do_profile(fp);
24344#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024345 fp->uf_varargs = varargs;
24346 fp->uf_flags = flags;
24347 fp->uf_calls = 0;
24348 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024349 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350
24351erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 ga_clear_strings(&newargs);
24353 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024354ret_free:
24355 vim_free(skip_until);
24356 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024359 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024360}
24361
24362/*
24363 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024364 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024366 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024367 * TFN_INT: internal function name OK
24368 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024369 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 * Advances "pp" to just after the function name (if no error).
24371 */
24372 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024373trans_function_name(
24374 char_u **pp,
24375 int skip, /* only find the end, don't evaluate */
24376 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024377 funcdict_T *fdp, /* return: info about dictionary used */
24378 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024380 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381 char_u *start;
24382 char_u *end;
24383 int lead;
24384 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024386 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024387
24388 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024389 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024390 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024391
24392 /* Check for hard coded <SNR>: already translated function ID (from a user
24393 * command). */
24394 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24395 && (*pp)[2] == (int)KE_SNR)
24396 {
24397 *pp += 3;
24398 len = get_id_len(pp) + 3;
24399 return vim_strnsave(start, len);
24400 }
24401
24402 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24403 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024405 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024407
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024408 /* Note that TFN_ flags use the same values as GLV_ flags. */
24409 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024410 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024411 if (end == start)
24412 {
24413 if (!skip)
24414 EMSG(_("E129: Function name required"));
24415 goto theend;
24416 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024417 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024418 {
24419 /*
24420 * Report an invalid expression in braces, unless the expression
24421 * evaluation has been cancelled due to an aborting error, an
24422 * interrupt, or an exception.
24423 */
24424 if (!aborting())
24425 {
24426 if (end != NULL)
24427 EMSG2(_(e_invarg2), start);
24428 }
24429 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024430 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024431 goto theend;
24432 }
24433
24434 if (lv.ll_tv != NULL)
24435 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024436 if (fdp != NULL)
24437 {
24438 fdp->fd_dict = lv.ll_dict;
24439 fdp->fd_newkey = lv.ll_newkey;
24440 lv.ll_newkey = NULL;
24441 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024442 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024443 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24444 {
24445 name = vim_strsave(lv.ll_tv->vval.v_string);
24446 *pp = end;
24447 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024448 else if (lv.ll_tv->v_type == VAR_PARTIAL
24449 && lv.ll_tv->vval.v_partial != NULL)
24450 {
24451 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24452 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024453 if (partial != NULL)
24454 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024455 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024456 else
24457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024458 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24459 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024460 EMSG(_(e_funcref));
24461 else
24462 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024463 name = NULL;
24464 }
24465 goto theend;
24466 }
24467
24468 if (lv.ll_name == NULL)
24469 {
24470 /* Error found, but continue after the function name. */
24471 *pp = end;
24472 goto theend;
24473 }
24474
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024475 /* Check if the name is a Funcref. If so, use the value. */
24476 if (lv.ll_exp_name != NULL)
24477 {
24478 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024479 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024480 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024481 if (name == lv.ll_exp_name)
24482 name = NULL;
24483 }
24484 else
24485 {
24486 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024487 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024488 if (name == *pp)
24489 name = NULL;
24490 }
24491 if (name != NULL)
24492 {
24493 name = vim_strsave(name);
24494 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024495 if (STRNCMP(name, "<SNR>", 5) == 0)
24496 {
24497 /* Change "<SNR>" to the byte sequence. */
24498 name[0] = K_SPECIAL;
24499 name[1] = KS_EXTRA;
24500 name[2] = (int)KE_SNR;
24501 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24502 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024503 goto theend;
24504 }
24505
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024506 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024507 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024508 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024509 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24510 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24511 {
24512 /* When there was "s:" already or the name expanded to get a
24513 * leading "s:" then remove it. */
24514 lv.ll_name += 2;
24515 len -= 2;
24516 lead = 2;
24517 }
24518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024519 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024520 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024521 /* skip over "s:" and "g:" */
24522 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024523 lv.ll_name += 2;
24524 len = (int)(end - lv.ll_name);
24525 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024526
24527 /*
24528 * Copy the function name to allocated memory.
24529 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24530 * Accept <SNR>123_name() outside a script.
24531 */
24532 if (skip)
24533 lead = 0; /* do nothing */
24534 else if (lead > 0)
24535 {
24536 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024537 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24538 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024539 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024540 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024541 if (current_SID <= 0)
24542 {
24543 EMSG(_(e_usingsid));
24544 goto theend;
24545 }
24546 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24547 lead += (int)STRLEN(sid_buf);
24548 }
24549 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024550 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024551 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024552 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024553 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024554 goto theend;
24555 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024556 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024557 {
24558 char_u *cp = vim_strchr(lv.ll_name, ':');
24559
24560 if (cp != NULL && cp < end)
24561 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024562 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024563 goto theend;
24564 }
24565 }
24566
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024567 name = alloc((unsigned)(len + lead + 1));
24568 if (name != NULL)
24569 {
24570 if (lead > 0)
24571 {
24572 name[0] = K_SPECIAL;
24573 name[1] = KS_EXTRA;
24574 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024575 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024576 STRCPY(name + 3, sid_buf);
24577 }
24578 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024579 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024580 }
24581 *pp = end;
24582
24583theend:
24584 clear_lval(&lv);
24585 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586}
24587
24588/*
24589 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24590 * Return 2 if "p" starts with "s:".
24591 * Return 0 otherwise.
24592 */
24593 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024594eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024596 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24597 * the standard library function. */
24598 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24599 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600 return 5;
24601 if (p[0] == 's' && p[1] == ':')
24602 return 2;
24603 return 0;
24604}
24605
24606/*
24607 * Return TRUE if "p" starts with "<SID>" or "s:".
24608 * Only works if eval_fname_script() returned non-zero for "p"!
24609 */
24610 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024611eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612{
24613 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24614}
24615
24616/*
24617 * List the head of the function: "name(arg1, arg2)".
24618 */
24619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024620list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621{
24622 int j;
24623
24624 msg_start();
24625 if (indent)
24626 MSG_PUTS(" ");
24627 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024628 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 {
24630 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024631 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 }
24633 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024634 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024636 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 {
24638 if (j)
24639 MSG_PUTS(", ");
24640 msg_puts(FUNCARG(fp, j));
24641 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024642 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 {
24644 if (j)
24645 MSG_PUTS(", ");
24646 MSG_PUTS("...");
24647 }
24648 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024649 if (fp->uf_flags & FC_ABORT)
24650 MSG_PUTS(" abort");
24651 if (fp->uf_flags & FC_RANGE)
24652 MSG_PUTS(" range");
24653 if (fp->uf_flags & FC_DICT)
24654 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024655 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024656 if (p_verbose > 0)
24657 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658}
24659
24660/*
24661 * Find a function by name, return pointer to it in ufuncs.
24662 * Return NULL for unknown function.
24663 */
24664 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024665find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024666{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024667 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024669 hi = hash_find(&func_hashtab, name);
24670 if (!HASHITEM_EMPTY(hi))
24671 return HI2UF(hi);
24672 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673}
24674
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024675#if defined(EXITFREE) || defined(PROTO)
24676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024677free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024678{
24679 hashitem_T *hi;
24680
24681 /* Need to start all over every time, because func_free() may change the
24682 * hash table. */
24683 while (func_hashtab.ht_used > 0)
24684 for (hi = func_hashtab.ht_array; ; ++hi)
24685 if (!HASHITEM_EMPTY(hi))
24686 {
24687 func_free(HI2UF(hi));
24688 break;
24689 }
24690}
24691#endif
24692
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024694translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024695{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024696 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024697 return find_internal_func(name) >= 0;
24698 return find_func(name) != NULL;
24699}
24700
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024701/*
24702 * Return TRUE if a function "name" exists.
24703 */
24704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024705function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024706{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024707 char_u *nm = name;
24708 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024709 int n = FALSE;
24710
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024711 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024712 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024713 nm = skipwhite(nm);
24714
24715 /* Only accept "funcname", "funcname ", "funcname (..." and
24716 * "funcname(...", not "funcname!...". */
24717 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024718 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024719 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024720 return n;
24721}
24722
Bram Moolenaara1544c02013-05-30 12:35:52 +020024723 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024724get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024725{
24726 char_u *nm = name;
24727 char_u *p;
24728
Bram Moolenaar65639032016-03-16 21:40:30 +010024729 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024730
24731 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024732 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024733 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024734
Bram Moolenaara1544c02013-05-30 12:35:52 +020024735 vim_free(p);
24736 return NULL;
24737}
24738
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024739/*
24740 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024741 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24742 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024743 */
24744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024745builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024746{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024747 char_u *p;
24748
24749 if (!ASCII_ISLOWER(name[0]))
24750 return FALSE;
24751 p = vim_strchr(name, AUTOLOAD_CHAR);
24752 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024753}
24754
Bram Moolenaar05159a02005-02-26 23:04:13 +000024755#if defined(FEAT_PROFILE) || defined(PROTO)
24756/*
24757 * Start profiling function "fp".
24758 */
24759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024760func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024761{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024762 int len = fp->uf_lines.ga_len;
24763
24764 if (len == 0)
24765 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024766 fp->uf_tm_count = 0;
24767 profile_zero(&fp->uf_tm_self);
24768 profile_zero(&fp->uf_tm_total);
24769 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024770 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024771 if (fp->uf_tml_total == NULL)
24772 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024773 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024774 if (fp->uf_tml_self == NULL)
24775 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024776 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024777 fp->uf_tml_idx = -1;
24778 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24779 || fp->uf_tml_self == NULL)
24780 return; /* out of memory */
24781
24782 fp->uf_profiling = TRUE;
24783}
24784
24785/*
24786 * Dump the profiling results for all functions in file "fd".
24787 */
24788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024789func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024790{
24791 hashitem_T *hi;
24792 int todo;
24793 ufunc_T *fp;
24794 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024795 ufunc_T **sorttab;
24796 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024797
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024798 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024799 if (todo == 0)
24800 return; /* nothing to dump */
24801
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024802 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024803
Bram Moolenaar05159a02005-02-26 23:04:13 +000024804 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24805 {
24806 if (!HASHITEM_EMPTY(hi))
24807 {
24808 --todo;
24809 fp = HI2UF(hi);
24810 if (fp->uf_profiling)
24811 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024812 if (sorttab != NULL)
24813 sorttab[st_len++] = fp;
24814
Bram Moolenaar05159a02005-02-26 23:04:13 +000024815 if (fp->uf_name[0] == K_SPECIAL)
24816 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24817 else
24818 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24819 if (fp->uf_tm_count == 1)
24820 fprintf(fd, "Called 1 time\n");
24821 else
24822 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24823 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24824 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24825 fprintf(fd, "\n");
24826 fprintf(fd, "count total (s) self (s)\n");
24827
24828 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24829 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024830 if (FUNCLINE(fp, i) == NULL)
24831 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024832 prof_func_line(fd, fp->uf_tml_count[i],
24833 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024834 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24835 }
24836 fprintf(fd, "\n");
24837 }
24838 }
24839 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024840
24841 if (sorttab != NULL && st_len > 0)
24842 {
24843 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24844 prof_total_cmp);
24845 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24846 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24847 prof_self_cmp);
24848 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24849 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024850
24851 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024852}
Bram Moolenaar73830342005-02-28 22:48:19 +000024853
24854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024855prof_sort_list(
24856 FILE *fd,
24857 ufunc_T **sorttab,
24858 int st_len,
24859 char *title,
24860 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024861{
24862 int i;
24863 ufunc_T *fp;
24864
24865 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24866 fprintf(fd, "count total (s) self (s) function\n");
24867 for (i = 0; i < 20 && i < st_len; ++i)
24868 {
24869 fp = sorttab[i];
24870 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24871 prefer_self);
24872 if (fp->uf_name[0] == K_SPECIAL)
24873 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24874 else
24875 fprintf(fd, " %s()\n", fp->uf_name);
24876 }
24877 fprintf(fd, "\n");
24878}
24879
24880/*
24881 * Print the count and times for one function or function line.
24882 */
24883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024884prof_func_line(
24885 FILE *fd,
24886 int count,
24887 proftime_T *total,
24888 proftime_T *self,
24889 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024890{
24891 if (count > 0)
24892 {
24893 fprintf(fd, "%5d ", count);
24894 if (prefer_self && profile_equal(total, self))
24895 fprintf(fd, " ");
24896 else
24897 fprintf(fd, "%s ", profile_msg(total));
24898 if (!prefer_self && profile_equal(total, self))
24899 fprintf(fd, " ");
24900 else
24901 fprintf(fd, "%s ", profile_msg(self));
24902 }
24903 else
24904 fprintf(fd, " ");
24905}
24906
24907/*
24908 * Compare function for total time sorting.
24909 */
24910 static int
24911#ifdef __BORLANDC__
24912_RTLENTRYF
24913#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024914prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024915{
24916 ufunc_T *p1, *p2;
24917
24918 p1 = *(ufunc_T **)s1;
24919 p2 = *(ufunc_T **)s2;
24920 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24921}
24922
24923/*
24924 * Compare function for self time sorting.
24925 */
24926 static int
24927#ifdef __BORLANDC__
24928_RTLENTRYF
24929#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024930prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024931{
24932 ufunc_T *p1, *p2;
24933
24934 p1 = *(ufunc_T **)s1;
24935 p2 = *(ufunc_T **)s2;
24936 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24937}
24938
Bram Moolenaar05159a02005-02-26 23:04:13 +000024939#endif
24940
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024941/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024942 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024943 * Return TRUE if a package was loaded.
24944 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024946script_autoload(
24947 char_u *name,
24948 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024949{
24950 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024951 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024952 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024953 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024954
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024955 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024956 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024957 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024958 return FALSE;
24959
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024960 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024961
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024962 /* Find the name in the list of previously loaded package names. Skip
24963 * "autoload/", it's always the same. */
24964 for (i = 0; i < ga_loaded.ga_len; ++i)
24965 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24966 break;
24967 if (!reload && i < ga_loaded.ga_len)
24968 ret = FALSE; /* was loaded already */
24969 else
24970 {
24971 /* Remember the name if it wasn't loaded already. */
24972 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24973 {
24974 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24975 tofree = NULL;
24976 }
24977
24978 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024979 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024980 ret = TRUE;
24981 }
24982
24983 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024984 return ret;
24985}
24986
24987/*
24988 * Return the autoload script name for a function or variable name.
24989 * Returns NULL when out of memory.
24990 */
24991 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024992autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024993{
24994 char_u *p;
24995 char_u *scriptname;
24996
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024997 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024998 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24999 if (scriptname == NULL)
25000 return FALSE;
25001 STRCPY(scriptname, "autoload/");
25002 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025003 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025004 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025005 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025006 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025007 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025008}
25009
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25011
25012/*
25013 * Function given to ExpandGeneric() to obtain the list of user defined
25014 * function names.
25015 */
25016 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025017get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025019 static long_u done;
25020 static hashitem_T *hi;
25021 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025022
25023 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025025 done = 0;
25026 hi = func_hashtab.ht_array;
25027 }
25028 if (done < func_hashtab.ht_used)
25029 {
25030 if (done++ > 0)
25031 ++hi;
25032 while (HASHITEM_EMPTY(hi))
25033 ++hi;
25034 fp = HI2UF(hi);
25035
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025036 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025037 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025038
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025039 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25040 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041
25042 cat_func_name(IObuff, fp);
25043 if (xp->xp_context != EXPAND_USER_FUNC)
25044 {
25045 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025046 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047 STRCAT(IObuff, ")");
25048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 return IObuff;
25050 }
25051 return NULL;
25052}
25053
25054#endif /* FEAT_CMDL_COMPL */
25055
25056/*
25057 * Copy the function name of "fp" to buffer "buf".
25058 * "buf" must be able to hold the function name plus three bytes.
25059 * Takes care of script-local function names.
25060 */
25061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025062cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025063{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025064 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065 {
25066 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025067 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 }
25069 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025070 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071}
25072
25073/*
25074 * ":delfunction {name}"
25075 */
25076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025077ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025079 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080 char_u *p;
25081 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025082 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083
25084 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025085 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025086 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025088 {
25089 if (fudi.fd_dict != NULL && !eap->skip)
25090 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 if (!ends_excmd(*skipwhite(p)))
25094 {
25095 vim_free(name);
25096 EMSG(_(e_trailing));
25097 return;
25098 }
25099 eap->nextcmd = check_nextcmd(p);
25100 if (eap->nextcmd != NULL)
25101 *p = NUL;
25102
25103 if (!eap->skip)
25104 fp = find_func(name);
25105 vim_free(name);
25106
25107 if (!eap->skip)
25108 {
25109 if (fp == NULL)
25110 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025111 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 return;
25113 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025114 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 {
25116 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25117 return;
25118 }
25119
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025120 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025121 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025122 /* Delete the dict item that refers to the function, it will
25123 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025124 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025126 else
25127 func_free(fp);
25128 }
25129}
25130
25131/*
25132 * Free a function and remove it from the list of functions.
25133 */
25134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025135func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025136{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025137 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025138
25139 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025140 ga_clear_strings(&(fp->uf_args));
25141 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025142#ifdef FEAT_PROFILE
25143 vim_free(fp->uf_tml_count);
25144 vim_free(fp->uf_tml_total);
25145 vim_free(fp->uf_tml_self);
25146#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025147
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025148 /* remove the function from the function hashtable */
25149 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25150 if (HASHITEM_EMPTY(hi))
25151 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025152 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025153 hash_remove(&func_hashtab, hi);
25154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025155 vim_free(fp);
25156}
25157
25158/*
25159 * Unreference a Function: decrement the reference count and free it when it
25160 * becomes zero. Only for numbered functions.
25161 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025163func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025164{
25165 ufunc_T *fp;
25166
25167 if (name != NULL && isdigit(*name))
25168 {
25169 fp = find_func(name);
25170 if (fp == NULL)
25171 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025172 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025173 {
25174 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025175 * when "uf_calls" becomes zero. */
25176 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025177 func_free(fp);
25178 }
25179 }
25180}
25181
25182/*
25183 * Count a reference to a Function.
25184 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025186func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025187{
25188 ufunc_T *fp;
25189
25190 if (name != NULL && isdigit(*name))
25191 {
25192 fp = find_func(name);
25193 if (fp == NULL)
25194 EMSG2(_(e_intern2), "func_ref()");
25195 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025196 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 }
25198}
25199
25200/*
25201 * Call a user function.
25202 */
25203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025204call_user_func(
25205 ufunc_T *fp, /* pointer to function */
25206 int argcount, /* nr of args */
25207 typval_T *argvars, /* arguments */
25208 typval_T *rettv, /* return value */
25209 linenr_T firstline, /* first line of range */
25210 linenr_T lastline, /* last line of range */
25211 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212{
Bram Moolenaar33570922005-01-25 22:26:29 +000025213 char_u *save_sourcing_name;
25214 linenr_T save_sourcing_lnum;
25215 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025216 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025217 int save_did_emsg;
25218 static int depth = 0;
25219 dictitem_T *v;
25220 int fixvar_idx = 0; /* index in fixvar[] */
25221 int i;
25222 int ai;
25223 char_u numbuf[NUMBUFLEN];
25224 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025225 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025226#ifdef FEAT_PROFILE
25227 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025228 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230
25231 /* If depth of calling is getting too high, don't execute the function */
25232 if (depth >= p_mfd)
25233 {
25234 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025235 rettv->v_type = VAR_NUMBER;
25236 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237 return;
25238 }
25239 ++depth;
25240
25241 line_breakcheck(); /* check for CTRL-C hit */
25242
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025243 fc = (funccall_T *)alloc(sizeof(funccall_T));
25244 fc->caller = current_funccal;
25245 current_funccal = fc;
25246 fc->func = fp;
25247 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025248 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025249 fc->linenr = 0;
25250 fc->returned = FALSE;
25251 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025253 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25254 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255
Bram Moolenaar33570922005-01-25 22:26:29 +000025256 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025257 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025258 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25259 * each argument variable and saves a lot of time.
25260 */
25261 /*
25262 * Init l: variables.
25263 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025264 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025265 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025266 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025267 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25268 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025269 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025270 name = v->di_key;
25271 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025272 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025273 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025274 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025275 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025276 v->di_tv.vval.v_dict = selfdict;
25277 ++selfdict->dv_refcount;
25278 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025279
Bram Moolenaar33570922005-01-25 22:26:29 +000025280 /*
25281 * Init a: variables.
25282 * Set a:0 to "argcount".
25283 * Set a:000 to a list with room for the "..." arguments.
25284 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025285 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025286 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025287 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025288 /* Use "name" to avoid a warning from some compiler that checks the
25289 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025290 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025291 name = v->di_key;
25292 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025293 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025294 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025295 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025296 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025297 v->di_tv.vval.v_list = &fc->l_varlist;
25298 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25299 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25300 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025301
25302 /*
25303 * Set a:firstline to "firstline" and a:lastline to "lastline".
25304 * Set a:name to named arguments.
25305 * Set a:N to the "..." arguments.
25306 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025307 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025308 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025309 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025310 (varnumber_T)lastline);
25311 for (i = 0; i < argcount; ++i)
25312 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025313 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025314 if (ai < 0)
25315 /* named argument a:name */
25316 name = FUNCARG(fp, i);
25317 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025318 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025319 /* "..." argument a:1, a:2, etc. */
25320 sprintf((char *)numbuf, "%d", ai + 1);
25321 name = numbuf;
25322 }
25323 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25324 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025325 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025326 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25327 }
25328 else
25329 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025330 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25331 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025332 if (v == NULL)
25333 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025334 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025335 }
25336 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025337 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025338
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025339 /* Note: the values are copied directly to avoid alloc/free.
25340 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025341 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025342 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025343
25344 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25345 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025346 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25347 fc->l_listitems[ai].li_tv = argvars[i];
25348 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025349 }
25350 }
25351
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352 /* Don't redraw while executing the function. */
25353 ++RedrawingDisabled;
25354 save_sourcing_name = sourcing_name;
25355 save_sourcing_lnum = sourcing_lnum;
25356 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025357 /* need space for function name + ("function " + 3) or "[number]" */
25358 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25359 + STRLEN(fp->uf_name) + 20;
25360 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 if (sourcing_name != NULL)
25362 {
25363 if (save_sourcing_name != NULL
25364 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025365 sprintf((char *)sourcing_name, "%s[%d]..",
25366 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367 else
25368 STRCPY(sourcing_name, "function ");
25369 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25370
25371 if (p_verbose >= 12)
25372 {
25373 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025374 verbose_enter_scroll();
25375
Bram Moolenaar555b2802005-05-19 21:08:39 +000025376 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 if (p_verbose >= 14)
25378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025380 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025381 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025382 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383
25384 msg_puts((char_u *)"(");
25385 for (i = 0; i < argcount; ++i)
25386 {
25387 if (i > 0)
25388 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025389 if (argvars[i].v_type == VAR_NUMBER)
25390 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 else
25392 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025393 /* Do not want errors such as E724 here. */
25394 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025395 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025396 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025397 if (s != NULL)
25398 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025399 if (vim_strsize(s) > MSG_BUF_CLEN)
25400 {
25401 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25402 s = buf;
25403 }
25404 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025405 vim_free(tofree);
25406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407 }
25408 }
25409 msg_puts((char_u *)")");
25410 }
25411 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025412
25413 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414 --no_wait_return;
25415 }
25416 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025417#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025418 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025419 {
25420 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25421 func_do_profile(fp);
25422 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025423 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025424 {
25425 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025426 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025427 profile_zero(&fp->uf_tm_children);
25428 }
25429 script_prof_save(&wait_start);
25430 }
25431#endif
25432
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025434 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435 save_did_emsg = did_emsg;
25436 did_emsg = FALSE;
25437
25438 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025439 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025440 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25441
25442 --RedrawingDisabled;
25443
25444 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025445 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025447 clear_tv(rettv);
25448 rettv->v_type = VAR_NUMBER;
25449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450 }
25451
Bram Moolenaar05159a02005-02-26 23:04:13 +000025452#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025453 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025454 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025455 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025456 profile_end(&call_start);
25457 profile_sub_wait(&wait_start, &call_start);
25458 profile_add(&fp->uf_tm_total, &call_start);
25459 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025460 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025461 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025462 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25463 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025464 }
25465 }
25466#endif
25467
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468 /* when being verbose, mention the return value */
25469 if (p_verbose >= 12)
25470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025472 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025473
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025475 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025476 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025477 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025478 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025479 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025481 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025482 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025483 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025484 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025485
Bram Moolenaar555b2802005-05-19 21:08:39 +000025486 /* The value may be very long. Skip the middle part, so that we
25487 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025488 * truncate it at the end. Don't want errors such as E724 here. */
25489 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025490 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025491 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025492 if (s != NULL)
25493 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025494 if (vim_strsize(s) > MSG_BUF_CLEN)
25495 {
25496 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25497 s = buf;
25498 }
25499 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025500 vim_free(tofree);
25501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025502 }
25503 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025504
25505 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506 --no_wait_return;
25507 }
25508
25509 vim_free(sourcing_name);
25510 sourcing_name = save_sourcing_name;
25511 sourcing_lnum = save_sourcing_lnum;
25512 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025513#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025514 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025515 script_prof_restore(&wait_start);
25516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517
25518 if (p_verbose >= 12 && sourcing_name != NULL)
25519 {
25520 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025521 verbose_enter_scroll();
25522
Bram Moolenaar555b2802005-05-19 21:08:39 +000025523 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025525
25526 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527 --no_wait_return;
25528 }
25529
25530 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025531 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025533
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025534 /* If the a:000 list and the l: and a: dicts are not referenced we can
25535 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025536 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25537 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25538 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25539 {
25540 free_funccal(fc, FALSE);
25541 }
25542 else
25543 {
25544 hashitem_T *hi;
25545 listitem_T *li;
25546 int todo;
25547
25548 /* "fc" is still in use. This can happen when returning "a:000" or
25549 * assigning "l:" to a global variable.
25550 * Link "fc" in the list for garbage collection later. */
25551 fc->caller = previous_funccal;
25552 previous_funccal = fc;
25553
25554 /* Make a copy of the a: variables, since we didn't do that above. */
25555 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25556 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25557 {
25558 if (!HASHITEM_EMPTY(hi))
25559 {
25560 --todo;
25561 v = HI2DI(hi);
25562 copy_tv(&v->di_tv, &v->di_tv);
25563 }
25564 }
25565
25566 /* Make a copy of the a:000 items, since we didn't do that above. */
25567 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25568 copy_tv(&li->li_tv, &li->li_tv);
25569 }
25570}
25571
25572/*
25573 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025574 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025575 */
25576 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025577can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025578{
25579 return (fc->l_varlist.lv_copyID != copyID
25580 && fc->l_vars.dv_copyID != copyID
25581 && fc->l_avars.dv_copyID != copyID);
25582}
25583
25584/*
25585 * Free "fc" and what it contains.
25586 */
25587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025588free_funccal(
25589 funccall_T *fc,
25590 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025591{
25592 listitem_T *li;
25593
25594 /* The a: variables typevals may not have been allocated, only free the
25595 * allocated variables. */
25596 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25597
25598 /* free all l: variables */
25599 vars_clear(&fc->l_vars.dv_hashtab);
25600
25601 /* Free the a:000 variables if they were allocated. */
25602 if (free_val)
25603 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25604 clear_tv(&li->li_tv);
25605
25606 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607}
25608
25609/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025610 * Add a number variable "name" to dict "dp" with value "nr".
25611 */
25612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025613add_nr_var(
25614 dict_T *dp,
25615 dictitem_T *v,
25616 char *name,
25617 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025618{
25619 STRCPY(v->di_key, name);
25620 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25621 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25622 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025623 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025624 v->di_tv.vval.v_number = nr;
25625}
25626
25627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628 * ":return [expr]"
25629 */
25630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025631ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632{
25633 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025634 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635 int returning = FALSE;
25636
25637 if (current_funccal == NULL)
25638 {
25639 EMSG(_("E133: :return not inside a function"));
25640 return;
25641 }
25642
25643 if (eap->skip)
25644 ++emsg_skip;
25645
25646 eap->nextcmd = NULL;
25647 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025648 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025649 {
25650 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025651 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 }
25655 /* It's safer to return also on error. */
25656 else if (!eap->skip)
25657 {
25658 /*
25659 * Return unless the expression evaluation has been cancelled due to an
25660 * aborting error, an interrupt, or an exception.
25661 */
25662 if (!aborting())
25663 returning = do_return(eap, FALSE, TRUE, NULL);
25664 }
25665
25666 /* When skipping or the return gets pending, advance to the next command
25667 * in this line (!returning). Otherwise, ignore the rest of the line.
25668 * Following lines will be ignored by get_func_line(). */
25669 if (returning)
25670 eap->nextcmd = NULL;
25671 else if (eap->nextcmd == NULL) /* no argument */
25672 eap->nextcmd = check_nextcmd(arg);
25673
25674 if (eap->skip)
25675 --emsg_skip;
25676}
25677
25678/*
25679 * Return from a function. Possibly makes the return pending. Also called
25680 * for a pending return at the ":endtry" or after returning from an extra
25681 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025682 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025683 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684 * FALSE when the return gets pending.
25685 */
25686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025687do_return(
25688 exarg_T *eap,
25689 int reanimate,
25690 int is_cmd,
25691 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692{
25693 int idx;
25694 struct condstack *cstack = eap->cstack;
25695
25696 if (reanimate)
25697 /* Undo the return. */
25698 current_funccal->returned = FALSE;
25699
25700 /*
25701 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25702 * not in its finally clause (which then is to be executed next) is found.
25703 * In this case, make the ":return" pending for execution at the ":endtry".
25704 * Otherwise, return normally.
25705 */
25706 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25707 if (idx >= 0)
25708 {
25709 cstack->cs_pending[idx] = CSTP_RETURN;
25710
25711 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025712 /* A pending return again gets pending. "rettv" points to an
25713 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025714 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025715 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716 else
25717 {
25718 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025719 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025721 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025723 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025724 {
25725 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025726 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025727 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 else
25729 EMSG(_(e_outofmem));
25730 }
25731 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025732 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025733
25734 if (reanimate)
25735 {
25736 /* The pending return value could be overwritten by a ":return"
25737 * without argument in a finally clause; reset the default
25738 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025739 current_funccal->rettv->v_type = VAR_NUMBER;
25740 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741 }
25742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025743 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744 }
25745 else
25746 {
25747 current_funccal->returned = TRUE;
25748
25749 /* If the return is carried out now, store the return value. For
25750 * a return immediately after reanimation, the value is already
25751 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025752 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025754 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025755 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025757 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 }
25759 }
25760
25761 return idx < 0;
25762}
25763
25764/*
25765 * Free the variable with a pending return value.
25766 */
25767 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025768discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769{
Bram Moolenaar33570922005-01-25 22:26:29 +000025770 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771}
25772
25773/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025774 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025775 * is an allocated string. Used by report_pending() for verbose messages.
25776 */
25777 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025778get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025780 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025781 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025782 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025784 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025785 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025786 if (s == NULL)
25787 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025788
25789 STRCPY(IObuff, ":return ");
25790 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25791 if (STRLEN(s) + 8 >= IOSIZE)
25792 STRCPY(IObuff + IOSIZE - 4, "...");
25793 vim_free(tofree);
25794 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795}
25796
25797/*
25798 * Get next function line.
25799 * Called by do_cmdline() to get the next line.
25800 * Returns allocated string, or NULL for end of function.
25801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025803get_func_line(
25804 int c UNUSED,
25805 void *cookie,
25806 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025807{
Bram Moolenaar33570922005-01-25 22:26:29 +000025808 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025809 ufunc_T *fp = fcp->func;
25810 char_u *retval;
25811 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812
25813 /* If breakpoints have been added/deleted need to check for it. */
25814 if (fcp->dbg_tick != debug_tick)
25815 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025816 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025817 sourcing_lnum);
25818 fcp->dbg_tick = debug_tick;
25819 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025820#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025821 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025822 func_line_end(cookie);
25823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025824
Bram Moolenaar05159a02005-02-26 23:04:13 +000025825 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025826 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25827 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025828 retval = NULL;
25829 else
25830 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025831 /* Skip NULL lines (continuation lines). */
25832 while (fcp->linenr < gap->ga_len
25833 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25834 ++fcp->linenr;
25835 if (fcp->linenr >= gap->ga_len)
25836 retval = NULL;
25837 else
25838 {
25839 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25840 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025841#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025842 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025843 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025844#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025846 }
25847
25848 /* Did we encounter a breakpoint? */
25849 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25850 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025851 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025853 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854 sourcing_lnum);
25855 fcp->dbg_tick = debug_tick;
25856 }
25857
25858 return retval;
25859}
25860
Bram Moolenaar05159a02005-02-26 23:04:13 +000025861#if defined(FEAT_PROFILE) || defined(PROTO)
25862/*
25863 * Called when starting to read a function line.
25864 * "sourcing_lnum" must be correct!
25865 * When skipping lines it may not actually be executed, but we won't find out
25866 * until later and we need to store the time now.
25867 */
25868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025869func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025870{
25871 funccall_T *fcp = (funccall_T *)cookie;
25872 ufunc_T *fp = fcp->func;
25873
25874 if (fp->uf_profiling && sourcing_lnum >= 1
25875 && sourcing_lnum <= fp->uf_lines.ga_len)
25876 {
25877 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025878 /* Skip continuation lines. */
25879 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25880 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025881 fp->uf_tml_execed = FALSE;
25882 profile_start(&fp->uf_tml_start);
25883 profile_zero(&fp->uf_tml_children);
25884 profile_get_wait(&fp->uf_tml_wait);
25885 }
25886}
25887
25888/*
25889 * Called when actually executing a function line.
25890 */
25891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025892func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025893{
25894 funccall_T *fcp = (funccall_T *)cookie;
25895 ufunc_T *fp = fcp->func;
25896
25897 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25898 fp->uf_tml_execed = TRUE;
25899}
25900
25901/*
25902 * Called when done with a function line.
25903 */
25904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025905func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025906{
25907 funccall_T *fcp = (funccall_T *)cookie;
25908 ufunc_T *fp = fcp->func;
25909
25910 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25911 {
25912 if (fp->uf_tml_execed)
25913 {
25914 ++fp->uf_tml_count[fp->uf_tml_idx];
25915 profile_end(&fp->uf_tml_start);
25916 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025917 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025918 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25919 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025920 }
25921 fp->uf_tml_idx = -1;
25922 }
25923}
25924#endif
25925
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926/*
25927 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025928 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929 */
25930 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025931func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025932{
Bram Moolenaar33570922005-01-25 22:26:29 +000025933 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934
25935 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25936 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025937 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938 || fcp->returned);
25939}
25940
25941/*
25942 * return TRUE if cookie indicates a function which "abort"s on errors.
25943 */
25944 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025945func_has_abort(
25946 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025948 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025949}
25950
25951#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25952typedef enum
25953{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025954 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25955 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25956 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025957} var_flavour_T;
25958
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025959static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960
25961 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025962var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025963{
25964 char_u *p = varname;
25965
25966 if (ASCII_ISUPPER(*p))
25967 {
25968 while (*(++p))
25969 if (ASCII_ISLOWER(*p))
25970 return VAR_FLAVOUR_SESSION;
25971 return VAR_FLAVOUR_VIMINFO;
25972 }
25973 else
25974 return VAR_FLAVOUR_DEFAULT;
25975}
25976#endif
25977
25978#if defined(FEAT_VIMINFO) || defined(PROTO)
25979/*
25980 * Restore global vars that start with a capital from the viminfo file
25981 */
25982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025983read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984{
25985 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025986 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025987 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025988 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989
25990 if (!writing && (find_viminfo_parameter('!') != NULL))
25991 {
25992 tab = vim_strchr(virp->vir_line + 1, '\t');
25993 if (tab != NULL)
25994 {
25995 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025996 switch (*tab)
25997 {
25998 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025999#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026000 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026001#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026002 case 'D': type = VAR_DICT; break;
26003 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026004 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006
26007 tab = vim_strchr(tab, '\t');
26008 if (tab != NULL)
26009 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026010 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026011 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026012 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026014#ifdef FEAT_FLOAT
26015 else if (type == VAR_FLOAT)
26016 (void)string2float(tab + 1, &tv.vval.v_float);
26017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026019 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026020 if (type == VAR_DICT || type == VAR_LIST)
26021 {
26022 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26023
26024 if (etv == NULL)
26025 /* Failed to parse back the dict or list, use it as a
26026 * string. */
26027 tv.v_type = VAR_STRING;
26028 else
26029 {
26030 vim_free(tv.vval.v_string);
26031 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026032 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026033 }
26034 }
26035
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026036 /* when in a function use global variables */
26037 save_funccal = current_funccal;
26038 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026039 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026040 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026041
26042 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026043 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026044 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26045 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026046 }
26047 }
26048 }
26049
26050 return viminfo_readline(virp);
26051}
26052
26053/*
26054 * Write global vars that start with a capital to the viminfo file
26055 */
26056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026057write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058{
Bram Moolenaar33570922005-01-25 22:26:29 +000026059 hashitem_T *hi;
26060 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026061 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026062 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026063 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026064 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026065 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026066
26067 if (find_viminfo_parameter('!') == NULL)
26068 return;
26069
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026070 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026071
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026072 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026073 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026074 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026075 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026076 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026077 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026078 this_var = HI2DI(hi);
26079 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026080 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026081 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026082 {
26083 case VAR_STRING: s = "STR"; break;
26084 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026085 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026086 case VAR_DICT: s = "DIC"; break;
26087 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026088 case VAR_SPECIAL: s = "XPL"; break;
26089
26090 case VAR_UNKNOWN:
26091 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026092 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026093 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026094 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026095 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026096 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026097 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026098 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026099 if (p != NULL)
26100 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026101 vim_free(tofree);
26102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 }
26104 }
26105}
26106#endif
26107
26108#if defined(FEAT_SESSION) || defined(PROTO)
26109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026110store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111{
Bram Moolenaar33570922005-01-25 22:26:29 +000026112 hashitem_T *hi;
26113 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026114 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026115 char_u *p, *t;
26116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026117 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026118 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026119 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026120 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026122 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026123 this_var = HI2DI(hi);
26124 if ((this_var->di_tv.v_type == VAR_NUMBER
26125 || this_var->di_tv.v_type == VAR_STRING)
26126 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026127 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026128 /* Escape special characters with a backslash. Turn a LF and
26129 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026130 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026131 (char_u *)"\\\"\n\r");
26132 if (p == NULL) /* out of memory */
26133 break;
26134 for (t = p; *t != NUL; ++t)
26135 if (*t == '\n')
26136 *t = 'n';
26137 else if (*t == '\r')
26138 *t = 'r';
26139 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026140 this_var->di_key,
26141 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26142 : ' ',
26143 p,
26144 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26145 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026146 || put_eol(fd) == FAIL)
26147 {
26148 vim_free(p);
26149 return FAIL;
26150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026151 vim_free(p);
26152 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026153#ifdef FEAT_FLOAT
26154 else if (this_var->di_tv.v_type == VAR_FLOAT
26155 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26156 {
26157 float_T f = this_var->di_tv.vval.v_float;
26158 int sign = ' ';
26159
26160 if (f < 0)
26161 {
26162 f = -f;
26163 sign = '-';
26164 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026165 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026166 this_var->di_key, sign, f) < 0)
26167 || put_eol(fd) == FAIL)
26168 return FAIL;
26169 }
26170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171 }
26172 }
26173 return OK;
26174}
26175#endif
26176
Bram Moolenaar661b1822005-07-28 22:36:45 +000026177/*
26178 * Display script name where an item was last set.
26179 * Should only be invoked when 'verbose' is non-zero.
26180 */
26181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026182last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026183{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026184 char_u *p;
26185
Bram Moolenaar661b1822005-07-28 22:36:45 +000026186 if (scriptID != 0)
26187 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026188 p = home_replace_save(NULL, get_scriptname(scriptID));
26189 if (p != NULL)
26190 {
26191 verbose_enter();
26192 MSG_PUTS(_("\n\tLast set from "));
26193 MSG_PUTS(p);
26194 vim_free(p);
26195 verbose_leave();
26196 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026197 }
26198}
26199
Bram Moolenaard812df62008-11-09 12:46:09 +000026200/*
26201 * List v:oldfiles in a nice way.
26202 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026204ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026205{
26206 list_T *l = vimvars[VV_OLDFILES].vv_list;
26207 listitem_T *li;
26208 int nr = 0;
26209
26210 if (l == NULL)
26211 msg((char_u *)_("No old files"));
26212 else
26213 {
26214 msg_start();
26215 msg_scroll = TRUE;
26216 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26217 {
26218 msg_outnum((long)++nr);
26219 MSG_PUTS(": ");
26220 msg_outtrans(get_tv_string(&li->li_tv));
26221 msg_putchar('\n');
26222 out_flush(); /* output one line at a time */
26223 ui_breakcheck();
26224 }
26225 /* Assume "got_int" was set to truncate the listing. */
26226 got_int = FALSE;
26227
26228#ifdef FEAT_BROWSE_CMD
26229 if (cmdmod.browse)
26230 {
26231 quit_more = FALSE;
26232 nr = prompt_for_number(FALSE);
26233 msg_starthere();
26234 if (nr > 0)
26235 {
26236 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26237 (long)nr);
26238
26239 if (p != NULL)
26240 {
26241 p = expand_env_save(p);
26242 eap->arg = p;
26243 eap->cmdidx = CMD_edit;
26244 cmdmod.browse = FALSE;
26245 do_exedit(eap, NULL);
26246 vim_free(p);
26247 }
26248 }
26249 }
26250#endif
26251 }
26252}
26253
Bram Moolenaar53744302015-07-17 17:38:22 +020026254/* reset v:option_new, v:option_old and v:option_type */
26255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026256reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026257{
26258 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26259 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26260 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26261}
26262
26263
Bram Moolenaar071d4272004-06-13 20:20:40 +000026264#endif /* FEAT_EVAL */
26265
Bram Moolenaar071d4272004-06-13 20:20:40 +000026266
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026267#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026268
26269#ifdef WIN3264
26270/*
26271 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26272 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026273static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26274static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26275static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026276
26277/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026278 * Get the short path (8.3) for the filename in "fnamep".
26279 * Only works for a valid file name.
26280 * When the path gets longer "fnamep" is changed and the allocated buffer
26281 * is put in "bufp".
26282 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26283 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026284 */
26285 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026286get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026287{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026288 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026289 char_u *newbuf;
26290
26291 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026292 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293 if (l > len - 1)
26294 {
26295 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026296 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297 newbuf = vim_strnsave(*fnamep, l);
26298 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026299 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026300
26301 vim_free(*bufp);
26302 *fnamep = *bufp = newbuf;
26303
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026304 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026305 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026306 }
26307
26308 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026309 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310}
26311
26312/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026313 * Get the short path (8.3) for the filename in "fname". The converted
26314 * path is returned in "bufp".
26315 *
26316 * Some of the directories specified in "fname" may not exist. This function
26317 * will shorten the existing directories at the beginning of the path and then
26318 * append the remaining non-existing path.
26319 *
26320 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026321 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026322 * bufp - Pointer to an allocated buffer for the filename.
26323 * fnamelen - Length of the filename pointed to by fname
26324 *
26325 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026326 */
26327 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026328shortpath_for_invalid_fname(
26329 char_u **fname,
26330 char_u **bufp,
26331 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026332{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026333 char_u *short_fname, *save_fname, *pbuf_unused;
26334 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026335 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026336 int old_len, len;
26337 int new_len, sfx_len;
26338 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026339
26340 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026341 old_len = *fnamelen;
26342 save_fname = vim_strnsave(*fname, old_len);
26343 pbuf_unused = NULL;
26344 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026345
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026346 endp = save_fname + old_len - 1; /* Find the end of the copy */
26347 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026348
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026349 /*
26350 * Try shortening the supplied path till it succeeds by removing one
26351 * directory at a time from the tail of the path.
26352 */
26353 len = 0;
26354 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026355 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026356 /* go back one path-separator */
26357 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26358 --endp;
26359 if (endp <= save_fname)
26360 break; /* processed the complete path */
26361
26362 /*
26363 * Replace the path separator with a NUL and try to shorten the
26364 * resulting path.
26365 */
26366 ch = *endp;
26367 *endp = 0;
26368 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026369 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026370 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26371 {
26372 retval = FAIL;
26373 goto theend;
26374 }
26375 *endp = ch; /* preserve the string */
26376
26377 if (len > 0)
26378 break; /* successfully shortened the path */
26379
26380 /* failed to shorten the path. Skip the path separator */
26381 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382 }
26383
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026384 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026385 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026386 /*
26387 * Succeeded in shortening the path. Now concatenate the shortened
26388 * path with the remaining path at the tail.
26389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026390
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026391 /* Compute the length of the new path. */
26392 sfx_len = (int)(save_endp - endp) + 1;
26393 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026394
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026395 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026396 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026397 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026398 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026399 /* There is not enough space in the currently allocated string,
26400 * copy it to a buffer big enough. */
26401 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026402 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026403 {
26404 retval = FAIL;
26405 goto theend;
26406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026407 }
26408 else
26409 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026410 /* Transfer short_fname to the main buffer (it's big enough),
26411 * unless get_short_pathname() did its work in-place. */
26412 *fname = *bufp = save_fname;
26413 if (short_fname != save_fname)
26414 vim_strncpy(save_fname, short_fname, len);
26415 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026416 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026417
26418 /* concat the not-shortened part of the path */
26419 vim_strncpy(*fname + len, endp, sfx_len);
26420 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026422
26423theend:
26424 vim_free(pbuf_unused);
26425 vim_free(save_fname);
26426
26427 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026428}
26429
26430/*
26431 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026432 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026433 */
26434 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026435shortpath_for_partial(
26436 char_u **fnamep,
26437 char_u **bufp,
26438 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026439{
26440 int sepcount, len, tflen;
26441 char_u *p;
26442 char_u *pbuf, *tfname;
26443 int hasTilde;
26444
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026445 /* Count up the path separators from the RHS.. so we know which part
26446 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026447 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026448 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026449 if (vim_ispathsep(*p))
26450 ++sepcount;
26451
26452 /* Need full path first (use expand_env() to remove a "~/") */
26453 hasTilde = (**fnamep == '~');
26454 if (hasTilde)
26455 pbuf = tfname = expand_env_save(*fnamep);
26456 else
26457 pbuf = tfname = FullName_save(*fnamep, FALSE);
26458
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026459 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026461 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026463
26464 if (len == 0)
26465 {
26466 /* Don't have a valid filename, so shorten the rest of the
26467 * path if we can. This CAN give us invalid 8.3 filenames, but
26468 * there's not a lot of point in guessing what it might be.
26469 */
26470 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026471 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26472 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026473 }
26474
26475 /* Count the paths backward to find the beginning of the desired string. */
26476 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026477 {
26478#ifdef FEAT_MBYTE
26479 if (has_mbyte)
26480 p -= mb_head_off(tfname, p);
26481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026482 if (vim_ispathsep(*p))
26483 {
26484 if (sepcount == 0 || (hasTilde && sepcount == 1))
26485 break;
26486 else
26487 sepcount --;
26488 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026490 if (hasTilde)
26491 {
26492 --p;
26493 if (p >= tfname)
26494 *p = '~';
26495 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026496 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026497 }
26498 else
26499 ++p;
26500
26501 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26502 vim_free(*bufp);
26503 *fnamelen = (int)STRLEN(p);
26504 *bufp = pbuf;
26505 *fnamep = p;
26506
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026507 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026508}
26509#endif /* WIN3264 */
26510
26511/*
26512 * Adjust a filename, according to a string of modifiers.
26513 * *fnamep must be NUL terminated when called. When returning, the length is
26514 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026515 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026516 * When there is an error, *fnamep is set to NULL.
26517 */
26518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026519modify_fname(
26520 char_u *src, /* string with modifiers */
26521 int *usedlen, /* characters after src that are used */
26522 char_u **fnamep, /* file name so far */
26523 char_u **bufp, /* buffer for allocated file name or NULL */
26524 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026525{
26526 int valid = 0;
26527 char_u *tail;
26528 char_u *s, *p, *pbuf;
26529 char_u dirname[MAXPATHL];
26530 int c;
26531 int has_fullname = 0;
26532#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026533 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026534 int has_shortname = 0;
26535#endif
26536
26537repeat:
26538 /* ":p" - full path/file_name */
26539 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26540 {
26541 has_fullname = 1;
26542
26543 valid |= VALID_PATH;
26544 *usedlen += 2;
26545
26546 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26547 if ((*fnamep)[0] == '~'
26548#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26549 && ((*fnamep)[1] == '/'
26550# ifdef BACKSLASH_IN_FILENAME
26551 || (*fnamep)[1] == '\\'
26552# endif
26553 || (*fnamep)[1] == NUL)
26554
26555#endif
26556 )
26557 {
26558 *fnamep = expand_env_save(*fnamep);
26559 vim_free(*bufp); /* free any allocated file name */
26560 *bufp = *fnamep;
26561 if (*fnamep == NULL)
26562 return -1;
26563 }
26564
26565 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026566 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026567 {
26568 if (vim_ispathsep(*p)
26569 && p[1] == '.'
26570 && (p[2] == NUL
26571 || vim_ispathsep(p[2])
26572 || (p[2] == '.'
26573 && (p[3] == NUL || vim_ispathsep(p[3])))))
26574 break;
26575 }
26576
26577 /* FullName_save() is slow, don't use it when not needed. */
26578 if (*p != NUL || !vim_isAbsName(*fnamep))
26579 {
26580 *fnamep = FullName_save(*fnamep, *p != NUL);
26581 vim_free(*bufp); /* free any allocated file name */
26582 *bufp = *fnamep;
26583 if (*fnamep == NULL)
26584 return -1;
26585 }
26586
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026587#ifdef WIN3264
26588# if _WIN32_WINNT >= 0x0500
26589 if (vim_strchr(*fnamep, '~') != NULL)
26590 {
26591 /* Expand 8.3 filename to full path. Needed to make sure the same
26592 * file does not have two different names.
26593 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26594 p = alloc(_MAX_PATH + 1);
26595 if (p != NULL)
26596 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026597 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026598 {
26599 vim_free(*bufp);
26600 *bufp = *fnamep = p;
26601 }
26602 else
26603 vim_free(p);
26604 }
26605 }
26606# endif
26607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026608 /* Append a path separator to a directory. */
26609 if (mch_isdir(*fnamep))
26610 {
26611 /* Make room for one or two extra characters. */
26612 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26613 vim_free(*bufp); /* free any allocated file name */
26614 *bufp = *fnamep;
26615 if (*fnamep == NULL)
26616 return -1;
26617 add_pathsep(*fnamep);
26618 }
26619 }
26620
26621 /* ":." - path relative to the current directory */
26622 /* ":~" - path relative to the home directory */
26623 /* ":8" - shortname path - postponed till after */
26624 while (src[*usedlen] == ':'
26625 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26626 {
26627 *usedlen += 2;
26628 if (c == '8')
26629 {
26630#ifdef WIN3264
26631 has_shortname = 1; /* Postpone this. */
26632#endif
26633 continue;
26634 }
26635 pbuf = NULL;
26636 /* Need full path first (use expand_env() to remove a "~/") */
26637 if (!has_fullname)
26638 {
26639 if (c == '.' && **fnamep == '~')
26640 p = pbuf = expand_env_save(*fnamep);
26641 else
26642 p = pbuf = FullName_save(*fnamep, FALSE);
26643 }
26644 else
26645 p = *fnamep;
26646
26647 has_fullname = 0;
26648
26649 if (p != NULL)
26650 {
26651 if (c == '.')
26652 {
26653 mch_dirname(dirname, MAXPATHL);
26654 s = shorten_fname(p, dirname);
26655 if (s != NULL)
26656 {
26657 *fnamep = s;
26658 if (pbuf != NULL)
26659 {
26660 vim_free(*bufp); /* free any allocated file name */
26661 *bufp = pbuf;
26662 pbuf = NULL;
26663 }
26664 }
26665 }
26666 else
26667 {
26668 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26669 /* Only replace it when it starts with '~' */
26670 if (*dirname == '~')
26671 {
26672 s = vim_strsave(dirname);
26673 if (s != NULL)
26674 {
26675 *fnamep = s;
26676 vim_free(*bufp);
26677 *bufp = s;
26678 }
26679 }
26680 }
26681 vim_free(pbuf);
26682 }
26683 }
26684
26685 tail = gettail(*fnamep);
26686 *fnamelen = (int)STRLEN(*fnamep);
26687
26688 /* ":h" - head, remove "/file_name", can be repeated */
26689 /* Don't remove the first "/" or "c:\" */
26690 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26691 {
26692 valid |= VALID_HEAD;
26693 *usedlen += 2;
26694 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026695 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026696 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026697 *fnamelen = (int)(tail - *fnamep);
26698#ifdef VMS
26699 if (*fnamelen > 0)
26700 *fnamelen += 1; /* the path separator is part of the path */
26701#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026702 if (*fnamelen == 0)
26703 {
26704 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26705 p = vim_strsave((char_u *)".");
26706 if (p == NULL)
26707 return -1;
26708 vim_free(*bufp);
26709 *bufp = *fnamep = tail = p;
26710 *fnamelen = 1;
26711 }
26712 else
26713 {
26714 while (tail > s && !after_pathsep(s, tail))
26715 mb_ptr_back(*fnamep, tail);
26716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026717 }
26718
26719 /* ":8" - shortname */
26720 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26721 {
26722 *usedlen += 2;
26723#ifdef WIN3264
26724 has_shortname = 1;
26725#endif
26726 }
26727
26728#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026729 /*
26730 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026731 */
26732 if (has_shortname)
26733 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026734 /* Copy the string if it is shortened by :h and when it wasn't copied
26735 * yet, because we are going to change it in place. Avoids changing
26736 * the buffer name for "%:8". */
26737 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026738 {
26739 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026740 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026741 return -1;
26742 vim_free(*bufp);
26743 *bufp = *fnamep = p;
26744 }
26745
26746 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026747 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026748 if (!has_fullname && !vim_isAbsName(*fnamep))
26749 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026750 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026751 return -1;
26752 }
26753 else
26754 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026755 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026756
Bram Moolenaardc935552011-08-17 15:23:23 +020026757 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026758 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026759 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026760 return -1;
26761
26762 if (l == 0)
26763 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026764 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026765 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026766 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026767 return -1;
26768 }
26769 *fnamelen = l;
26770 }
26771 }
26772#endif /* WIN3264 */
26773
26774 /* ":t" - tail, just the basename */
26775 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26776 {
26777 *usedlen += 2;
26778 *fnamelen -= (int)(tail - *fnamep);
26779 *fnamep = tail;
26780 }
26781
26782 /* ":e" - extension, can be repeated */
26783 /* ":r" - root, without extension, can be repeated */
26784 while (src[*usedlen] == ':'
26785 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26786 {
26787 /* find a '.' in the tail:
26788 * - for second :e: before the current fname
26789 * - otherwise: The last '.'
26790 */
26791 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26792 s = *fnamep - 2;
26793 else
26794 s = *fnamep + *fnamelen - 1;
26795 for ( ; s > tail; --s)
26796 if (s[0] == '.')
26797 break;
26798 if (src[*usedlen + 1] == 'e') /* :e */
26799 {
26800 if (s > tail)
26801 {
26802 *fnamelen += (int)(*fnamep - (s + 1));
26803 *fnamep = s + 1;
26804#ifdef VMS
26805 /* cut version from the extension */
26806 s = *fnamep + *fnamelen - 1;
26807 for ( ; s > *fnamep; --s)
26808 if (s[0] == ';')
26809 break;
26810 if (s > *fnamep)
26811 *fnamelen = s - *fnamep;
26812#endif
26813 }
26814 else if (*fnamep <= tail)
26815 *fnamelen = 0;
26816 }
26817 else /* :r */
26818 {
26819 if (s > tail) /* remove one extension */
26820 *fnamelen = (int)(s - *fnamep);
26821 }
26822 *usedlen += 2;
26823 }
26824
26825 /* ":s?pat?foo?" - substitute */
26826 /* ":gs?pat?foo?" - global substitute */
26827 if (src[*usedlen] == ':'
26828 && (src[*usedlen + 1] == 's'
26829 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26830 {
26831 char_u *str;
26832 char_u *pat;
26833 char_u *sub;
26834 int sep;
26835 char_u *flags;
26836 int didit = FALSE;
26837
26838 flags = (char_u *)"";
26839 s = src + *usedlen + 2;
26840 if (src[*usedlen + 1] == 'g')
26841 {
26842 flags = (char_u *)"g";
26843 ++s;
26844 }
26845
26846 sep = *s++;
26847 if (sep)
26848 {
26849 /* find end of pattern */
26850 p = vim_strchr(s, sep);
26851 if (p != NULL)
26852 {
26853 pat = vim_strnsave(s, (int)(p - s));
26854 if (pat != NULL)
26855 {
26856 s = p + 1;
26857 /* find end of substitution */
26858 p = vim_strchr(s, sep);
26859 if (p != NULL)
26860 {
26861 sub = vim_strnsave(s, (int)(p - s));
26862 str = vim_strnsave(*fnamep, *fnamelen);
26863 if (sub != NULL && str != NULL)
26864 {
26865 *usedlen = (int)(p + 1 - src);
26866 s = do_string_sub(str, pat, sub, flags);
26867 if (s != NULL)
26868 {
26869 *fnamep = s;
26870 *fnamelen = (int)STRLEN(s);
26871 vim_free(*bufp);
26872 *bufp = s;
26873 didit = TRUE;
26874 }
26875 }
26876 vim_free(sub);
26877 vim_free(str);
26878 }
26879 vim_free(pat);
26880 }
26881 }
26882 /* after using ":s", repeat all the modifiers */
26883 if (didit)
26884 goto repeat;
26885 }
26886 }
26887
Bram Moolenaar26df0922014-02-23 23:39:13 +010026888 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26889 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026890 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026891 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026892 if (c != NUL)
26893 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026894 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026895 if (c != NUL)
26896 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026897 if (p == NULL)
26898 return -1;
26899 vim_free(*bufp);
26900 *bufp = *fnamep = p;
26901 *fnamelen = (int)STRLEN(p);
26902 *usedlen += 2;
26903 }
26904
Bram Moolenaar071d4272004-06-13 20:20:40 +000026905 return valid;
26906}
26907
26908/*
26909 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26910 * "flags" can be "g" to do a global substitute.
26911 * Returns an allocated string, NULL for error.
26912 */
26913 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026914do_string_sub(
26915 char_u *str,
26916 char_u *pat,
26917 char_u *sub,
26918 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026919{
26920 int sublen;
26921 regmatch_T regmatch;
26922 int i;
26923 int do_all;
26924 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026925 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026926 garray_T ga;
26927 char_u *ret;
26928 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026929 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026930
26931 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26932 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026933 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026934
26935 ga_init2(&ga, 1, 200);
26936
26937 do_all = (flags[0] == 'g');
26938
26939 regmatch.rm_ic = p_ic;
26940 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26941 if (regmatch.regprog != NULL)
26942 {
26943 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026944 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026945 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26946 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026947 /* Skip empty match except for first match. */
26948 if (regmatch.startp[0] == regmatch.endp[0])
26949 {
26950 if (zero_width == regmatch.startp[0])
26951 {
26952 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026953 i = MB_PTR2LEN(tail);
26954 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26955 (size_t)i);
26956 ga.ga_len += i;
26957 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026958 continue;
26959 }
26960 zero_width = regmatch.startp[0];
26961 }
26962
Bram Moolenaar071d4272004-06-13 20:20:40 +000026963 /*
26964 * Get some space for a temporary buffer to do the substitution
26965 * into. It will contain:
26966 * - The text up to where the match is.
26967 * - The substituted text.
26968 * - The text after the match.
26969 */
26970 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026971 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026972 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26973 {
26974 ga_clear(&ga);
26975 break;
26976 }
26977
26978 /* copy the text up to where the match is */
26979 i = (int)(regmatch.startp[0] - tail);
26980 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26981 /* add the substituted text */
26982 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26983 + ga.ga_len + i, TRUE, TRUE, FALSE);
26984 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026985 tail = regmatch.endp[0];
26986 if (*tail == NUL)
26987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026988 if (!do_all)
26989 break;
26990 }
26991
26992 if (ga.ga_data != NULL)
26993 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26994
Bram Moolenaar473de612013-06-08 18:19:48 +020026995 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026996 }
26997
26998 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26999 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027000 if (p_cpo == empty_option)
27001 p_cpo = save_cpo;
27002 else
27003 /* Darn, evaluating {sub} expression changed the value. */
27004 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027005
27006 return ret;
27007}
27008
27009#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */