blob: 53a651a4d9dcff93142e49fa8290b981ebc7217b [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 Moolenaar517ffbe2016-04-20 14:59:29 +02006054 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006055 * 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 Moolenaar245c4102016-04-20 17:37:41 +020011550 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011551 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011552
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011553 /* This is not allowed in the sandbox. If the commands would still be
11554 * executed in the sandbox it would be OK, but it probably happens later,
11555 * when "sandbox" is no longer set. */
11556 if (check_secure())
11557 return;
11558
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011559 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011560
11561 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011562 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011563 flags = get_tv_string_buf(&argvars[1], nbuf);
11564 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011565 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011566 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011567 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011568 case 'n': remap = FALSE; break;
11569 case 'm': remap = TRUE; break;
11570 case 't': typed = TRUE; break;
11571 case 'i': insert = TRUE; break;
11572 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011573 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011574 }
11575 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011576 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011577
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011578 if (*keys != NUL || execute)
11579 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011580 /* Need to escape K_SPECIAL and CSI before putting the string in the
11581 * typeahead buffer. */
11582 keys_esc = vim_strsave_escape_csi(keys);
11583 if (keys_esc != NULL)
11584 {
11585 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011586 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011587 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011588 if (vgetc_busy)
11589 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011590 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011591 {
11592 int save_msg_scroll = msg_scroll;
11593
11594 /* Avoid a 1 second delay when the keys start Insert mode. */
11595 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011596
Bram Moolenaar245c4102016-04-20 17:37:41 +020011597 if (!dangerous)
11598 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011599 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011600 if (!dangerous)
11601 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011602 msg_scroll |= save_msg_scroll;
11603 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011604 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011605 }
11606}
11607
11608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 * "filereadable()" function
11610 */
11611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011612f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011614 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 char_u *p;
11616 int n;
11617
Bram Moolenaarc236c162008-07-13 17:41:49 +000011618#ifndef O_NONBLOCK
11619# define O_NONBLOCK 0
11620#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011622 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11623 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 {
11625 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011626 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 }
11628 else
11629 n = FALSE;
11630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632}
11633
11634/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011635 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 * rights to write into.
11637 */
11638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011639f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011641 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642}
11643
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011644 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011645findfilendir(
11646 typval_T *argvars UNUSED,
11647 typval_T *rettv,
11648 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011649{
11650#ifdef FEAT_SEARCHPATH
11651 char_u *fname;
11652 char_u *fresult = NULL;
11653 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11654 char_u *p;
11655 char_u pathbuf[NUMBUFLEN];
11656 int count = 1;
11657 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011658 int error = FALSE;
11659#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011660
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011661 rettv->vval.v_string = NULL;
11662 rettv->v_type = VAR_STRING;
11663
11664#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011666
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011667 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011669 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11670 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011671 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 else
11673 {
11674 if (*p != NUL)
11675 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011678 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011680 }
11681
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011682 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11683 error = TRUE;
11684
11685 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 do
11688 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011689 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011690 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 fresult = find_file_in_path_option(first ? fname : NULL,
11692 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011693 0, first, path,
11694 find_what,
11695 curbuf->b_ffname,
11696 find_what == FINDFILE_DIR
11697 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011698 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011699
11700 if (fresult != NULL && rettv->v_type == VAR_LIST)
11701 list_append_string(rettv->vval.v_list, fresult, -1);
11702
11703 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011704 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011705
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011706 if (rettv->v_type == VAR_STRING)
11707 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011708#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011709}
11710
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011711static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11712static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011713
11714/*
11715 * Implementation of map() and filter().
11716 */
11717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011718filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011719{
11720 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011721 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011722 listitem_T *li, *nli;
11723 list_T *l = NULL;
11724 dictitem_T *di;
11725 hashtab_T *ht;
11726 hashitem_T *hi;
11727 dict_T *d = NULL;
11728 typval_T save_val;
11729 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011730 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011731 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011732 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011733 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011734 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011735 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011736 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011737
Bram Moolenaare9a41262005-01-15 22:18:47 +000011738 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011739 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011740 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011741 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011742 return;
11743 }
11744 else if (argvars[0].v_type == VAR_DICT)
11745 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011746 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011747 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 return;
11749 }
11750 else
11751 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011752 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011753 return;
11754 }
11755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011756 expr = get_tv_string_buf_chk(&argvars[1], buf);
11757 /* On type errors, the preceding call has already displayed an error
11758 * message. Avoid a misleading error message for an empty string that
11759 * was not passed as argument. */
11760 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011762 prepare_vimvar(VV_VAL, &save_val);
11763 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011764
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011765 /* We reset "did_emsg" to be able to detect whether an error
11766 * occurred during evaluation of the expression. */
11767 save_did_emsg = did_emsg;
11768 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011769
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011770 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 vimvars[VV_KEY].vv_type = VAR_STRING;
11774
11775 ht = &d->dv_hashtab;
11776 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011777 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011778 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011780 if (!HASHITEM_EMPTY(hi))
11781 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011782 int r;
11783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011784 --todo;
11785 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011786 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011787 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11788 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011789 break;
11790 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011791 r = filter_map_one(&di->di_tv, expr, map, &rem);
11792 clear_tv(&vimvars[VV_KEY].vv_tv);
11793 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011794 break;
11795 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011796 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011797 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11798 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011799 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011800 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011801 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 }
11803 }
11804 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011805 }
11806 else
11807 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011808 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 for (li = l->lv_first; li != NULL; li = nli)
11811 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011812 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011813 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011814 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011815 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011816 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011817 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011818 break;
11819 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011821 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011822 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011823 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011824
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011825 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011827
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011828 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011829 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011830
11831 copy_tv(&argvars[0], rettv);
11832}
11833
11834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011835filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011836{
Bram Moolenaar33570922005-01-25 22:26:29 +000011837 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011838 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011839 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011840
Bram Moolenaar33570922005-01-25 22:26:29 +000011841 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011842 s = expr;
11843 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011844 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011845 if (*s != NUL) /* check for trailing chars after expr */
11846 {
11847 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011848 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011849 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011850 }
11851 if (map)
11852 {
11853 /* map(): replace the list item value */
11854 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011855 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011856 *tv = rettv;
11857 }
11858 else
11859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011860 int error = FALSE;
11861
Bram Moolenaare9a41262005-01-15 22:18:47 +000011862 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011863 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011864 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 /* On type error, nothing has been removed; return FAIL to stop the
11866 * loop. The error message was given by get_tv_number_chk(). */
11867 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011868 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011869 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011870 retval = OK;
11871theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011872 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011873 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011874}
11875
11876/*
11877 * "filter()" function
11878 */
11879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011880f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011881{
11882 filter_map(argvars, rettv, FALSE);
11883}
11884
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011885/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011886 * "finddir({fname}[, {path}[, {count}]])" function
11887 */
11888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011889f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011890{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011891 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892}
11893
11894/*
11895 * "findfile({fname}[, {path}[, {count}]])" function
11896 */
11897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011898f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011899{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011900 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011901}
11902
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011903#ifdef FEAT_FLOAT
11904/*
11905 * "float2nr({float})" function
11906 */
11907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011908f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011909{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011910 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011911
11912 if (get_float_arg(argvars, &f) == OK)
11913 {
11914 if (f < -0x7fffffff)
11915 rettv->vval.v_number = -0x7fffffff;
11916 else if (f > 0x7fffffff)
11917 rettv->vval.v_number = 0x7fffffff;
11918 else
11919 rettv->vval.v_number = (varnumber_T)f;
11920 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011921}
11922
11923/*
11924 * "floor({float})" function
11925 */
11926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011927f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011928{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011929 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011930
11931 rettv->v_type = VAR_FLOAT;
11932 if (get_float_arg(argvars, &f) == OK)
11933 rettv->vval.v_float = floor(f);
11934 else
11935 rettv->vval.v_float = 0.0;
11936}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011937
11938/*
11939 * "fmod()" function
11940 */
11941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011942f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011943{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011944 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011945
11946 rettv->v_type = VAR_FLOAT;
11947 if (get_float_arg(argvars, &fx) == OK
11948 && get_float_arg(&argvars[1], &fy) == OK)
11949 rettv->vval.v_float = fmod(fx, fy);
11950 else
11951 rettv->vval.v_float = 0.0;
11952}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011953#endif
11954
Bram Moolenaar0d660222005-01-07 21:51:51 +000011955/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011956 * "fnameescape({string})" function
11957 */
11958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011959f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011960{
11961 rettv->vval.v_string = vim_strsave_fnameescape(
11962 get_tv_string(&argvars[0]), FALSE);
11963 rettv->v_type = VAR_STRING;
11964}
11965
11966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 * "fnamemodify({fname}, {mods})" function
11968 */
11969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011970f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
11972 char_u *fname;
11973 char_u *mods;
11974 int usedlen = 0;
11975 int len;
11976 char_u *fbuf = NULL;
11977 char_u buf[NUMBUFLEN];
11978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011979 fname = get_tv_string_chk(&argvars[0]);
11980 mods = get_tv_string_buf_chk(&argvars[1], buf);
11981 if (fname == NULL || mods == NULL)
11982 fname = NULL;
11983 else
11984 {
11985 len = (int)STRLEN(fname);
11986 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 vim_free(fbuf);
11995}
11996
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011997static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998
11999/*
12000 * "foldclosed()" function
12001 */
12002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012003foldclosed_both(
12004 typval_T *argvars UNUSED,
12005 typval_T *rettv,
12006 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007{
12008#ifdef FEAT_FOLDING
12009 linenr_T lnum;
12010 linenr_T first, last;
12011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12014 {
12015 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12016 {
12017 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012020 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 return;
12022 }
12023 }
12024#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026}
12027
12028/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012029 * "foldclosed()" function
12030 */
12031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012032f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012033{
12034 foldclosed_both(argvars, rettv, FALSE);
12035}
12036
12037/*
12038 * "foldclosedend()" function
12039 */
12040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012041f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012042{
12043 foldclosed_both(argvars, rettv, TRUE);
12044}
12045
12046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 * "foldlevel()" function
12048 */
12049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012050f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
12052#ifdef FEAT_FOLDING
12053 linenr_T lnum;
12054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059}
12060
12061/*
12062 * "foldtext()" function
12063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012065f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066{
12067#ifdef FEAT_FOLDING
12068 linenr_T lnum;
12069 char_u *s;
12070 char_u *r;
12071 int len;
12072 char *txt;
12073#endif
12074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012075 rettv->v_type = VAR_STRING;
12076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012078 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12079 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12080 <= curbuf->b_ml.ml_line_count
12081 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 {
12083 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012084 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12085 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 {
12087 if (!linewhite(lnum))
12088 break;
12089 ++lnum;
12090 }
12091
12092 /* Find interesting text in this line. */
12093 s = skipwhite(ml_get(lnum));
12094 /* skip C comment-start */
12095 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012096 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012098 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012099 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012100 {
12101 s = skipwhite(ml_get(lnum + 1));
12102 if (*s == '*')
12103 s = skipwhite(s + 1);
12104 }
12105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 txt = _("+-%s%3ld lines: ");
12107 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012108 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 + 20 /* for %3ld */
12110 + STRLEN(s))); /* concatenated */
12111 if (r != NULL)
12112 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012113 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12114 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12115 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 len = (int)STRLEN(r);
12117 STRCAT(r, s);
12118 /* remove 'foldmarker' and 'commentstring' */
12119 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 }
12122 }
12123#endif
12124}
12125
12126/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012127 * "foldtextresult(lnum)" function
12128 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012130f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012131{
12132#ifdef FEAT_FOLDING
12133 linenr_T lnum;
12134 char_u *text;
12135 char_u buf[51];
12136 foldinfo_T foldinfo;
12137 int fold_count;
12138#endif
12139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 rettv->v_type = VAR_STRING;
12141 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012142#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012144 /* treat illegal types and illegal string values for {lnum} the same */
12145 if (lnum < 0)
12146 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012147 fold_count = foldedCount(curwin, lnum, &foldinfo);
12148 if (fold_count > 0)
12149 {
12150 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12151 &foldinfo, buf);
12152 if (text == buf)
12153 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012154 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012155 }
12156#endif
12157}
12158
12159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 * "foreground()" function
12161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012163f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165#ifdef FEAT_GUI
12166 if (gui.in_use)
12167 gui_mch_set_foreground();
12168#else
12169# ifdef WIN32
12170 win32_set_foreground();
12171# endif
12172#endif
12173}
12174
12175/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012176 * "function()" function
12177 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012179f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012180{
12181 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012182 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012183 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012184 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012185
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012186 if (argvars[0].v_type == VAR_FUNC)
12187 {
12188 /* function(MyFunc, [arg], dict) */
12189 s = argvars[0].vval.v_string;
12190 }
12191 else if (argvars[0].v_type == VAR_PARTIAL
12192 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012193 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012194 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012195 arg_pt = argvars[0].vval.v_partial;
12196 s = arg_pt->pt_name;
12197 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012198 else
12199 {
12200 /* function('MyFunc', [arg], dict) */
12201 s = get_tv_string(&argvars[0]);
12202 use_string = TRUE;
12203 }
12204
12205 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012206 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012207 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012208 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12209 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012210 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012211 else
12212 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012213 int dict_idx = 0;
12214 int arg_idx = 0;
12215 list_T *list = NULL;
12216
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012217 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012218 {
12219 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012220 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012221
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012222 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12223 * also be called from another script. Using trans_function_name()
12224 * would also work, but some plugins depend on the name being
12225 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012226 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012227 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12228 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012229 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012230 STRCPY(name, sid_buf);
12231 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012232 }
12233 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012234 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012235 name = vim_strsave(s);
12236
12237 if (argvars[1].v_type != VAR_UNKNOWN)
12238 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012239 if (argvars[2].v_type != VAR_UNKNOWN)
12240 {
12241 /* function(name, [args], dict) */
12242 arg_idx = 1;
12243 dict_idx = 2;
12244 }
12245 else if (argvars[1].v_type == VAR_DICT)
12246 /* function(name, dict) */
12247 dict_idx = 1;
12248 else
12249 /* function(name, [args]) */
12250 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012251 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012252 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012253 if (argvars[dict_idx].v_type != VAR_DICT)
12254 {
12255 EMSG(_("E922: expected a dict"));
12256 vim_free(name);
12257 return;
12258 }
12259 if (argvars[dict_idx].vval.v_dict == NULL)
12260 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012261 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012262 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012263 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012264 if (argvars[arg_idx].v_type != VAR_LIST)
12265 {
12266 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12267 vim_free(name);
12268 return;
12269 }
12270 list = argvars[arg_idx].vval.v_list;
12271 if (list == NULL || list->lv_len == 0)
12272 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012273 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012274 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012275 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012276 {
12277 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012278
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012279 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012280 if (pt == NULL)
12281 vim_free(name);
12282 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012283 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012284 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012285 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012286 listitem_T *li;
12287 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012288 int arg_len = 0;
12289 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012290
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012291 if (arg_pt != NULL)
12292 arg_len = arg_pt->pt_argc;
12293 if (list != NULL)
12294 lv_len = list->lv_len;
12295 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012296 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012297 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012298 if (pt->pt_argv == NULL)
12299 {
12300 vim_free(pt);
12301 vim_free(name);
12302 return;
12303 }
12304 else
12305 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012306 for (i = 0; i < arg_len; i++)
12307 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12308 if (lv_len > 0)
12309 for (li = list->lv_first; li != NULL;
12310 li = li->li_next)
12311 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012312 }
12313 }
12314
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012315 /* For "function(dict.func, [], dict)" and "func" is a partial
12316 * use "dict". That is backwards compatible. */
12317 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012318 {
12319 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12320 ++pt->pt_dict->dv_refcount;
12321 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012322 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012323 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012324 pt->pt_dict = arg_pt->pt_dict;
12325 if (pt->pt_dict != NULL)
12326 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012327 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012328
12329 pt->pt_refcount = 1;
12330 pt->pt_name = name;
12331 func_ref(pt->pt_name);
12332 }
12333 rettv->v_type = VAR_PARTIAL;
12334 rettv->vval.v_partial = pt;
12335 }
12336 else
12337 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012338 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012339 rettv->v_type = VAR_FUNC;
12340 rettv->vval.v_string = name;
12341 func_ref(name);
12342 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012343 }
12344}
12345
12346/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012347 * "garbagecollect()" function
12348 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012350f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012351{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012352 /* This is postponed until we are back at the toplevel, because we may be
12353 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12354 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012355
12356 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12357 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012358}
12359
12360/*
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020012361 * "garbagecollect_for_testing()" function
12362 */
12363 static void
12364f_garbagecollect_for_testing(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12365{
12366 /* This is dangerous, any Lists and Dicts used internally may be freed
12367 * while still in use. */
12368 garbage_collect(TRUE);
12369}
12370
12371/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012372 * "get()" function
12373 */
12374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012375f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012376{
Bram Moolenaar33570922005-01-25 22:26:29 +000012377 listitem_T *li;
12378 list_T *l;
12379 dictitem_T *di;
12380 dict_T *d;
12381 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012382
Bram Moolenaare9a41262005-01-15 22:18:47 +000012383 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012384 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012385 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012387 int error = FALSE;
12388
12389 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12390 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012391 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012392 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012394 else if (argvars[0].v_type == VAR_DICT)
12395 {
12396 if ((d = argvars[0].vval.v_dict) != NULL)
12397 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012398 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012399 if (di != NULL)
12400 tv = &di->di_tv;
12401 }
12402 }
12403 else
12404 EMSG2(_(e_listdictarg), "get()");
12405
12406 if (tv == NULL)
12407 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012408 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012409 copy_tv(&argvars[2], rettv);
12410 }
12411 else
12412 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012413}
12414
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012415static 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 +000012416
12417/*
12418 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012419 * Return a range (from start to end) of lines in rettv from the specified
12420 * buffer.
12421 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012422 */
12423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012424get_buffer_lines(
12425 buf_T *buf,
12426 linenr_T start,
12427 linenr_T end,
12428 int retlist,
12429 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012430{
12431 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012432
Bram Moolenaar959a1432013-12-14 12:17:38 +010012433 rettv->v_type = VAR_STRING;
12434 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012435 if (retlist && rettv_list_alloc(rettv) == FAIL)
12436 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012437
12438 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12439 return;
12440
12441 if (!retlist)
12442 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012443 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12444 p = ml_get_buf(buf, start, FALSE);
12445 else
12446 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012447 rettv->vval.v_string = vim_strsave(p);
12448 }
12449 else
12450 {
12451 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012452 return;
12453
12454 if (start < 1)
12455 start = 1;
12456 if (end > buf->b_ml.ml_line_count)
12457 end = buf->b_ml.ml_line_count;
12458 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012459 if (list_append_string(rettv->vval.v_list,
12460 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012461 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012462 }
12463}
12464
12465/*
12466 * "getbufline()" function
12467 */
12468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012469f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012470{
12471 linenr_T lnum;
12472 linenr_T end;
12473 buf_T *buf;
12474
12475 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12476 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012477 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012478 --emsg_off;
12479
Bram Moolenaar661b1822005-07-28 22:36:45 +000012480 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012481 if (argvars[2].v_type == VAR_UNKNOWN)
12482 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012483 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012484 end = get_tv_lnum_buf(&argvars[2], buf);
12485
Bram Moolenaar342337a2005-07-21 21:11:17 +000012486 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012487}
12488
Bram Moolenaar0d660222005-01-07 21:51:51 +000012489/*
12490 * "getbufvar()" function
12491 */
12492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012493f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012494{
12495 buf_T *buf;
12496 buf_T *save_curbuf;
12497 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012498 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012499 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012501 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12502 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012503 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012504 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012505
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012506 rettv->v_type = VAR_STRING;
12507 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508
12509 if (buf != NULL && varname != NULL)
12510 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012511 /* set curbuf to be our buf, temporarily */
12512 save_curbuf = curbuf;
12513 curbuf = buf;
12514
Bram Moolenaar0d660222005-01-07 21:51:51 +000012515 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012516 {
12517 if (get_option_tv(&varname, rettv, TRUE) == OK)
12518 done = TRUE;
12519 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012520 else if (STRCMP(varname, "changedtick") == 0)
12521 {
12522 rettv->v_type = VAR_NUMBER;
12523 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012524 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012525 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012526 else
12527 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012528 /* Look up the variable. */
12529 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12530 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12531 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012533 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012535 done = TRUE;
12536 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012538
12539 /* restore previous notion of curbuf */
12540 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012541 }
12542
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012543 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12544 /* use the default value */
12545 copy_tv(&argvars[2], rettv);
12546
Bram Moolenaar0d660222005-01-07 21:51:51 +000012547 --emsg_off;
12548}
12549
12550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 * "getchar()" function
12552 */
12553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012554f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555{
12556 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012557 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012559 /* Position the cursor. Needed after a message that ends in a space. */
12560 windgoto(msg_row, msg_col);
12561
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 ++no_mapping;
12563 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012564 for (;;)
12565 {
12566 if (argvars[0].v_type == VAR_UNKNOWN)
12567 /* getchar(): blocking wait. */
12568 n = safe_vgetc();
12569 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12570 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012571 n = vpeekc_any();
12572 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012573 /* illegal argument or getchar(0) and no char avail: return zero */
12574 n = 0;
12575 else
12576 /* getchar(0) and char avail: return char */
12577 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012578
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012579 if (n == K_IGNORE)
12580 continue;
12581 break;
12582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 --no_mapping;
12584 --allow_keys;
12585
Bram Moolenaar219b8702006-11-01 14:32:36 +000012586 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12587 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12588 vimvars[VV_MOUSE_COL].vv_nr = 0;
12589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012590 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 if (IS_SPECIAL(n) || mod_mask != 0)
12592 {
12593 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12594 int i = 0;
12595
12596 /* Turn a special key into three bytes, plus modifier. */
12597 if (mod_mask != 0)
12598 {
12599 temp[i++] = K_SPECIAL;
12600 temp[i++] = KS_MODIFIER;
12601 temp[i++] = mod_mask;
12602 }
12603 if (IS_SPECIAL(n))
12604 {
12605 temp[i++] = K_SPECIAL;
12606 temp[i++] = K_SECOND(n);
12607 temp[i++] = K_THIRD(n);
12608 }
12609#ifdef FEAT_MBYTE
12610 else if (has_mbyte)
12611 i += (*mb_char2bytes)(n, temp + i);
12612#endif
12613 else
12614 temp[i++] = n;
12615 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 rettv->v_type = VAR_STRING;
12617 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012618
12619#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012620 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012621 {
12622 int row = mouse_row;
12623 int col = mouse_col;
12624 win_T *win;
12625 linenr_T lnum;
12626# ifdef FEAT_WINDOWS
12627 win_T *wp;
12628# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012629 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012630
12631 if (row >= 0 && col >= 0)
12632 {
12633 /* Find the window at the mouse coordinates and compute the
12634 * text position. */
12635 win = mouse_find_win(&row, &col);
12636 (void)mouse_comp_pos(win, &row, &col, &lnum);
12637# ifdef FEAT_WINDOWS
12638 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012639 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012640# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012641 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012642 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12643 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12644 }
12645 }
12646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647 }
12648}
12649
12650/*
12651 * "getcharmod()" function
12652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012654f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657}
12658
12659/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012660 * "getcharsearch()" function
12661 */
12662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012663f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012664{
12665 if (rettv_dict_alloc(rettv) != FAIL)
12666 {
12667 dict_T *dict = rettv->vval.v_dict;
12668
12669 dict_add_nr_str(dict, "char", 0L, last_csearch());
12670 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12671 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12672 }
12673}
12674
12675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 * "getcmdline()" function
12677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012679f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012681 rettv->v_type = VAR_STRING;
12682 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683}
12684
12685/*
12686 * "getcmdpos()" function
12687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012689f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692}
12693
12694/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012695 * "getcmdtype()" function
12696 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012698f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012699{
12700 rettv->v_type = VAR_STRING;
12701 rettv->vval.v_string = alloc(2);
12702 if (rettv->vval.v_string != NULL)
12703 {
12704 rettv->vval.v_string[0] = get_cmdline_type();
12705 rettv->vval.v_string[1] = NUL;
12706 }
12707}
12708
12709/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012710 * "getcmdwintype()" function
12711 */
12712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012713f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012714{
12715 rettv->v_type = VAR_STRING;
12716 rettv->vval.v_string = NULL;
12717#ifdef FEAT_CMDWIN
12718 rettv->vval.v_string = alloc(2);
12719 if (rettv->vval.v_string != NULL)
12720 {
12721 rettv->vval.v_string[0] = cmdwin_type;
12722 rettv->vval.v_string[1] = NUL;
12723 }
12724#endif
12725}
12726
12727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 * "getcwd()" function
12729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012731f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012733 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012734 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012737 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012738
12739 wp = find_tabwin(&argvars[0], &argvars[1]);
12740 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012742 if (wp->w_localdir != NULL)
12743 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12744 else if(globaldir != NULL)
12745 rettv->vval.v_string = vim_strsave(globaldir);
12746 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012747 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012748 cwd = alloc(MAXPATHL);
12749 if (cwd != NULL)
12750 {
12751 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12752 rettv->vval.v_string = vim_strsave(cwd);
12753 vim_free(cwd);
12754 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012755 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012756#ifdef BACKSLASH_IN_FILENAME
12757 if (rettv->vval.v_string != NULL)
12758 slash_adjust(rettv->vval.v_string);
12759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 }
12761}
12762
12763/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012764 * "getfontname()" function
12765 */
12766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012767f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012768{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 rettv->v_type = VAR_STRING;
12770 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012771#ifdef FEAT_GUI
12772 if (gui.in_use)
12773 {
12774 GuiFont font;
12775 char_u *name = NULL;
12776
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012777 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012778 {
12779 /* Get the "Normal" font. Either the name saved by
12780 * hl_set_font_name() or from the font ID. */
12781 font = gui.norm_font;
12782 name = hl_get_font_name();
12783 }
12784 else
12785 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012787 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12788 return;
12789 font = gui_mch_get_font(name, FALSE);
12790 if (font == NOFONT)
12791 return; /* Invalid font name, return empty string. */
12792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012794 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012795 gui_mch_free_font(font);
12796 }
12797#endif
12798}
12799
12800/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012801 * "getfperm({fname})" function
12802 */
12803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012804f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012805{
12806 char_u *fname;
12807 struct stat st;
12808 char_u *perm = NULL;
12809 char_u flags[] = "rwx";
12810 int i;
12811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012815 if (mch_stat((char *)fname, &st) >= 0)
12816 {
12817 perm = vim_strsave((char_u *)"---------");
12818 if (perm != NULL)
12819 {
12820 for (i = 0; i < 9; i++)
12821 {
12822 if (st.st_mode & (1 << (8 - i)))
12823 perm[i] = flags[i % 3];
12824 }
12825 }
12826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012828}
12829
12830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 * "getfsize({fname})" function
12832 */
12833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012834f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835{
12836 char_u *fname;
12837 struct stat st;
12838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842
12843 if (mch_stat((char *)fname, &st) >= 0)
12844 {
12845 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012850
12851 /* non-perfect check for overflow */
12852 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12853 rettv->vval.v_number = -2;
12854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 }
12856 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858}
12859
12860/*
12861 * "getftime({fname})" function
12862 */
12863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012864f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865{
12866 char_u *fname;
12867 struct stat st;
12868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870
12871 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875}
12876
12877/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012878 * "getftype({fname})" function
12879 */
12880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012881f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012882{
12883 char_u *fname;
12884 struct stat st;
12885 char_u *type = NULL;
12886 char *t;
12887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012891 if (mch_lstat((char *)fname, &st) >= 0)
12892 {
12893#ifdef S_ISREG
12894 if (S_ISREG(st.st_mode))
12895 t = "file";
12896 else if (S_ISDIR(st.st_mode))
12897 t = "dir";
12898# ifdef S_ISLNK
12899 else if (S_ISLNK(st.st_mode))
12900 t = "link";
12901# endif
12902# ifdef S_ISBLK
12903 else if (S_ISBLK(st.st_mode))
12904 t = "bdev";
12905# endif
12906# ifdef S_ISCHR
12907 else if (S_ISCHR(st.st_mode))
12908 t = "cdev";
12909# endif
12910# ifdef S_ISFIFO
12911 else if (S_ISFIFO(st.st_mode))
12912 t = "fifo";
12913# endif
12914# ifdef S_ISSOCK
12915 else if (S_ISSOCK(st.st_mode))
12916 t = "fifo";
12917# endif
12918 else
12919 t = "other";
12920#else
12921# ifdef S_IFMT
12922 switch (st.st_mode & S_IFMT)
12923 {
12924 case S_IFREG: t = "file"; break;
12925 case S_IFDIR: t = "dir"; break;
12926# ifdef S_IFLNK
12927 case S_IFLNK: t = "link"; break;
12928# endif
12929# ifdef S_IFBLK
12930 case S_IFBLK: t = "bdev"; break;
12931# endif
12932# ifdef S_IFCHR
12933 case S_IFCHR: t = "cdev"; break;
12934# endif
12935# ifdef S_IFIFO
12936 case S_IFIFO: t = "fifo"; break;
12937# endif
12938# ifdef S_IFSOCK
12939 case S_IFSOCK: t = "socket"; break;
12940# endif
12941 default: t = "other";
12942 }
12943# else
12944 if (mch_isdir(fname))
12945 t = "dir";
12946 else
12947 t = "file";
12948# endif
12949#endif
12950 type = vim_strsave((char_u *)t);
12951 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012953}
12954
12955/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012956 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012957 */
12958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012959f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012960{
12961 linenr_T lnum;
12962 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012963 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012964
12965 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012966 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012967 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012968 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012969 retlist = FALSE;
12970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012971 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012972 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012973 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012974 retlist = TRUE;
12975 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012976
Bram Moolenaar342337a2005-07-21 21:11:17 +000012977 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012978}
12979
12980/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012981 * "getmatches()" function
12982 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012984f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012985{
12986#ifdef FEAT_SEARCH_EXTRA
12987 dict_T *dict;
12988 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012989 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012990
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012991 if (rettv_list_alloc(rettv) == OK)
12992 {
12993 while (cur != NULL)
12994 {
12995 dict = dict_alloc();
12996 if (dict == NULL)
12997 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012998 if (cur->match.regprog == NULL)
12999 {
13000 /* match added with matchaddpos() */
13001 for (i = 0; i < MAXPOSMATCH; ++i)
13002 {
13003 llpos_T *llpos;
13004 char buf[6];
13005 list_T *l;
13006
13007 llpos = &cur->pos.pos[i];
13008 if (llpos->lnum == 0)
13009 break;
13010 l = list_alloc();
13011 if (l == NULL)
13012 break;
13013 list_append_number(l, (varnumber_T)llpos->lnum);
13014 if (llpos->col > 0)
13015 {
13016 list_append_number(l, (varnumber_T)llpos->col);
13017 list_append_number(l, (varnumber_T)llpos->len);
13018 }
13019 sprintf(buf, "pos%d", i + 1);
13020 dict_add_list(dict, buf, l);
13021 }
13022 }
13023 else
13024 {
13025 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13026 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013027 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013028 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13029 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013030# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013031 if (cur->conceal_char)
13032 {
13033 char_u buf[MB_MAXBYTES + 1];
13034
13035 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13036 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13037 }
13038# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013039 list_append_dict(rettv->vval.v_list, dict);
13040 cur = cur->next;
13041 }
13042 }
13043#endif
13044}
13045
13046/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013047 * "getpid()" function
13048 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013050f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013051{
13052 rettv->vval.v_number = mch_get_pid();
13053}
13054
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013056getpos_both(
13057 typval_T *argvars,
13058 typval_T *rettv,
13059 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013060{
Bram Moolenaara5525202006-03-02 22:52:09 +000013061 pos_T *fp;
13062 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013063 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013064
13065 if (rettv_list_alloc(rettv) == OK)
13066 {
13067 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013068 if (getcurpos)
13069 fp = &curwin->w_cursor;
13070 else
13071 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013072 if (fnum != -1)
13073 list_append_number(l, (varnumber_T)fnum);
13074 else
13075 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013076 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13077 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013078 list_append_number(l, (fp != NULL)
13079 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013080 : (varnumber_T)0);
13081 list_append_number(l,
13082#ifdef FEAT_VIRTUALEDIT
13083 (fp != NULL) ? (varnumber_T)fp->coladd :
13084#endif
13085 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013086 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013087 {
13088 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013089 list_append_number(l, curwin->w_curswant == MAXCOL ?
13090 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013091 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013092 }
13093 else
13094 rettv->vval.v_number = FALSE;
13095}
13096
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013097
13098/*
13099 * "getcurpos()" function
13100 */
13101 static void
13102f_getcurpos(typval_T *argvars, typval_T *rettv)
13103{
13104 getpos_both(argvars, rettv, TRUE);
13105}
13106
13107/*
13108 * "getpos(string)" function
13109 */
13110 static void
13111f_getpos(typval_T *argvars, typval_T *rettv)
13112{
13113 getpos_both(argvars, rettv, FALSE);
13114}
13115
Bram Moolenaara5525202006-03-02 22:52:09 +000013116/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013117 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013118 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013120f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013121{
13122#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013123 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013124#endif
13125
Bram Moolenaar2641f772005-03-25 21:58:17 +000013126#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013127 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013128 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013129 wp = NULL;
13130 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13131 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013132 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013133 if (wp == NULL)
13134 return;
13135 }
13136
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013137 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013138 }
13139#endif
13140}
13141
13142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 * "getreg()" function
13144 */
13145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013146f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147{
13148 char_u *strregname;
13149 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013150 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013151 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013152 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013154 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013156 strregname = get_tv_string_chk(&argvars[0]);
13157 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013158 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013160 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013161 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13162 return_list = get_tv_number_chk(&argvars[2], &error);
13163 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013166 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013167
13168 if (error)
13169 return;
13170
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 regname = (strregname == NULL ? '"' : *strregname);
13172 if (regname == 0)
13173 regname = '"';
13174
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013175 if (return_list)
13176 {
13177 rettv->v_type = VAR_LIST;
13178 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13179 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013180 if (rettv->vval.v_list == NULL)
13181 rettv_list_alloc(rettv);
13182 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013183 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013184 }
13185 else
13186 {
13187 rettv->v_type = VAR_STRING;
13188 rettv->vval.v_string = get_reg_contents(regname,
13189 arg2 ? GREG_EXPR_SRC : 0);
13190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191}
13192
13193/*
13194 * "getregtype()" function
13195 */
13196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013197f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198{
13199 char_u *strregname;
13200 int regname;
13201 char_u buf[NUMBUFLEN + 2];
13202 long reglen = 0;
13203
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013204 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013205 {
13206 strregname = get_tv_string_chk(&argvars[0]);
13207 if (strregname == NULL) /* type error; errmsg already given */
13208 {
13209 rettv->v_type = VAR_STRING;
13210 rettv->vval.v_string = NULL;
13211 return;
13212 }
13213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 else
13215 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013216 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217
13218 regname = (strregname == NULL ? '"' : *strregname);
13219 if (regname == 0)
13220 regname = '"';
13221
13222 buf[0] = NUL;
13223 buf[1] = NUL;
13224 switch (get_reg_type(regname, &reglen))
13225 {
13226 case MLINE: buf[0] = 'V'; break;
13227 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 case MBLOCK:
13229 buf[0] = Ctrl_V;
13230 sprintf((char *)buf + 1, "%ld", reglen + 1);
13231 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 rettv->v_type = VAR_STRING;
13234 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235}
13236
13237/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013238 * "gettabvar()" function
13239 */
13240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013241f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013242{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013243 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013244 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013245 dictitem_T *v;
13246 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013247 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013248
13249 rettv->v_type = VAR_STRING;
13250 rettv->vval.v_string = NULL;
13251
13252 varname = get_tv_string_chk(&argvars[1]);
13253 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13254 if (tp != NULL && varname != NULL)
13255 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013256 /* Set tp to be our tabpage, temporarily. Also set the window to the
13257 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013258 if (switch_win(&oldcurwin, &oldtabpage,
13259 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013260 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013261 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013262 /* look up the variable */
13263 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13264 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13265 if (v != NULL)
13266 {
13267 copy_tv(&v->di_tv, rettv);
13268 done = TRUE;
13269 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013270 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013271
13272 /* restore previous notion of curwin */
13273 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013274 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013275
13276 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013277 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013278}
13279
13280/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013281 * "gettabwinvar()" function
13282 */
13283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013284f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013285{
13286 getwinvar(argvars, rettv, 1);
13287}
13288
13289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 * "getwinposx()" function
13291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013293f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296#ifdef FEAT_GUI
13297 if (gui.in_use)
13298 {
13299 int x, y;
13300
13301 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013302 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 }
13304#endif
13305}
13306
13307/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013308 * "win_findbuf()" function
13309 */
13310 static void
13311f_win_findbuf(typval_T *argvars, typval_T *rettv)
13312{
13313 if (rettv_list_alloc(rettv) != FAIL)
13314 win_findbuf(argvars, rettv->vval.v_list);
13315}
13316
13317/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013318 * "win_getid()" function
13319 */
13320 static void
13321f_win_getid(typval_T *argvars, typval_T *rettv)
13322{
13323 rettv->vval.v_number = win_getid(argvars);
13324}
13325
13326/*
13327 * "win_gotoid()" function
13328 */
13329 static void
13330f_win_gotoid(typval_T *argvars, typval_T *rettv)
13331{
13332 rettv->vval.v_number = win_gotoid(argvars);
13333}
13334
13335/*
13336 * "win_id2tabwin()" function
13337 */
13338 static void
13339f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13340{
13341 if (rettv_list_alloc(rettv) != FAIL)
13342 win_id2tabwin(argvars, rettv->vval.v_list);
13343}
13344
13345/*
13346 * "win_id2win()" function
13347 */
13348 static void
13349f_win_id2win(typval_T *argvars, typval_T *rettv)
13350{
13351 rettv->vval.v_number = win_id2win(argvars);
13352}
13353
13354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355 * "getwinposy()" function
13356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013358f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013360 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361#ifdef FEAT_GUI
13362 if (gui.in_use)
13363 {
13364 int x, y;
13365
13366 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013367 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 }
13369#endif
13370}
13371
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013372/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013373 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013374 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013375 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013376find_win_by_nr(
13377 typval_T *vp,
13378 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013379{
13380#ifdef FEAT_WINDOWS
13381 win_T *wp;
13382#endif
13383 int nr;
13384
13385 nr = get_tv_number_chk(vp, NULL);
13386
13387#ifdef FEAT_WINDOWS
13388 if (nr < 0)
13389 return NULL;
13390 if (nr == 0)
13391 return curwin;
13392
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013393 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13394 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013395 if (--nr <= 0)
13396 break;
13397 return wp;
13398#else
13399 if (nr == 0 || nr == 1)
13400 return curwin;
13401 return NULL;
13402#endif
13403}
13404
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013406 * Find window specified by "wvp" in tabpage "tvp".
13407 */
13408 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013409find_tabwin(
13410 typval_T *wvp, /* VAR_UNKNOWN for current window */
13411 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013412{
13413 win_T *wp = NULL;
13414 tabpage_T *tp = NULL;
13415 long n;
13416
13417 if (wvp->v_type != VAR_UNKNOWN)
13418 {
13419 if (tvp->v_type != VAR_UNKNOWN)
13420 {
13421 n = get_tv_number(tvp);
13422 if (n >= 0)
13423 tp = find_tabpage(n);
13424 }
13425 else
13426 tp = curtab;
13427
13428 if (tp != NULL)
13429 wp = find_win_by_nr(wvp, tp);
13430 }
13431 else
13432 wp = curwin;
13433
13434 return wp;
13435}
13436
13437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 * "getwinvar()" function
13439 */
13440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013441f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013443 getwinvar(argvars, rettv, 0);
13444}
13445
13446/*
13447 * getwinvar() and gettabwinvar()
13448 */
13449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013450getwinvar(
13451 typval_T *argvars,
13452 typval_T *rettv,
13453 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013454{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013455 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013457 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013458 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013459 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013460#ifdef FEAT_WINDOWS
13461 win_T *oldcurwin;
13462 tabpage_T *oldtabpage;
13463 int need_switch_win;
13464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013466#ifdef FEAT_WINDOWS
13467 if (off == 1)
13468 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13469 else
13470 tp = curtab;
13471#endif
13472 win = find_win_by_nr(&argvars[off], tp);
13473 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013474 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013476 rettv->v_type = VAR_STRING;
13477 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478
13479 if (win != NULL && varname != NULL)
13480 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013481#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013482 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013483 * otherwise the window is not valid. Only do this when needed,
13484 * autocommands get blocked. */
13485 need_switch_win = !(tp == curtab && win == curwin);
13486 if (!need_switch_win
13487 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13488#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013489 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013490 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013491 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013492 if (get_option_tv(&varname, rettv, 1) == OK)
13493 done = TRUE;
13494 }
13495 else
13496 {
13497 /* Look up the variable. */
13498 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13499 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13500 varname, FALSE);
13501 if (v != NULL)
13502 {
13503 copy_tv(&v->di_tv, rettv);
13504 done = TRUE;
13505 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013508
Bram Moolenaarba117c22015-09-29 16:53:22 +020013509#ifdef FEAT_WINDOWS
13510 if (need_switch_win)
13511 /* restore previous notion of curwin */
13512 restore_win(oldcurwin, oldtabpage, TRUE);
13513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514 }
13515
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013516 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13517 /* use the default return value */
13518 copy_tv(&argvars[off + 2], rettv);
13519
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 --emsg_off;
13521}
13522
13523/*
13524 * "glob()" function
13525 */
13526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013527f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013529 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013531 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013533 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013534 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013536 if (argvars[1].v_type != VAR_UNKNOWN)
13537 {
13538 if (get_tv_number_chk(&argvars[1], &error))
13539 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013540 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013541 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013542 if (get_tv_number_chk(&argvars[2], &error))
13543 {
13544 rettv->v_type = VAR_LIST;
13545 rettv->vval.v_list = NULL;
13546 }
13547 if (argvars[3].v_type != VAR_UNKNOWN
13548 && get_tv_number_chk(&argvars[3], &error))
13549 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013550 }
13551 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013552 if (!error)
13553 {
13554 ExpandInit(&xpc);
13555 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013556 if (p_wic)
13557 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013558 if (rettv->v_type == VAR_STRING)
13559 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013560 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013561 else if (rettv_list_alloc(rettv) != FAIL)
13562 {
13563 int i;
13564
13565 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13566 NULL, options, WILD_ALL_KEEP);
13567 for (i = 0; i < xpc.xp_numfiles; i++)
13568 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13569
13570 ExpandCleanup(&xpc);
13571 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013572 }
13573 else
13574 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575}
13576
13577/*
13578 * "globpath()" function
13579 */
13580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013581f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013583 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013585 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013586 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013587 garray_T ga;
13588 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013590 /* When the optional second argument is non-zero, don't remove matches
13591 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013593 if (argvars[2].v_type != VAR_UNKNOWN)
13594 {
13595 if (get_tv_number_chk(&argvars[2], &error))
13596 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013597 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013598 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013599 if (get_tv_number_chk(&argvars[3], &error))
13600 {
13601 rettv->v_type = VAR_LIST;
13602 rettv->vval.v_list = NULL;
13603 }
13604 if (argvars[4].v_type != VAR_UNKNOWN
13605 && get_tv_number_chk(&argvars[4], &error))
13606 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013607 }
13608 }
13609 if (file != NULL && !error)
13610 {
13611 ga_init2(&ga, (int)sizeof(char_u *), 10);
13612 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13613 if (rettv->v_type == VAR_STRING)
13614 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13615 else if (rettv_list_alloc(rettv) != FAIL)
13616 for (i = 0; i < ga.ga_len; ++i)
13617 list_append_string(rettv->vval.v_list,
13618 ((char_u **)(ga.ga_data))[i], -1);
13619 ga_clear_strings(&ga);
13620 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013622 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623}
13624
13625/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013626 * "glob2regpat()" function
13627 */
13628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013629f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013630{
13631 char_u *pat = get_tv_string_chk(&argvars[0]);
13632
13633 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013634 rettv->vval.v_string = (pat == NULL)
13635 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013636}
13637
13638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 * "has()" function
13640 */
13641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013642f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643{
13644 int i;
13645 char_u *name;
13646 int n = FALSE;
13647 static char *(has_list[]) =
13648 {
13649#ifdef AMIGA
13650 "amiga",
13651# ifdef FEAT_ARP
13652 "arp",
13653# endif
13654#endif
13655#ifdef __BEOS__
13656 "beos",
13657#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013658#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 "mac",
13660#endif
13661#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013662 "macunix", /* built with 'darwin' enabled */
13663#endif
13664#if defined(__APPLE__) && __APPLE__ == 1
13665 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667#ifdef __QNX__
13668 "qnx",
13669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670#ifdef UNIX
13671 "unix",
13672#endif
13673#ifdef VMS
13674 "vms",
13675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676#ifdef WIN32
13677 "win32",
13678#endif
13679#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13680 "win32unix",
13681#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013682#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 "win64",
13684#endif
13685#ifdef EBCDIC
13686 "ebcdic",
13687#endif
13688#ifndef CASE_INSENSITIVE_FILENAME
13689 "fname_case",
13690#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013691#ifdef HAVE_ACL
13692 "acl",
13693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694#ifdef FEAT_ARABIC
13695 "arabic",
13696#endif
13697#ifdef FEAT_AUTOCMD
13698 "autocmd",
13699#endif
13700#ifdef FEAT_BEVAL
13701 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013702# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13703 "balloon_multiline",
13704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705#endif
13706#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13707 "builtin_terms",
13708# ifdef ALL_BUILTIN_TCAPS
13709 "all_builtin_terms",
13710# endif
13711#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013712#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13713 || defined(FEAT_GUI_W32) \
13714 || defined(FEAT_GUI_MOTIF))
13715 "browsefilter",
13716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717#ifdef FEAT_BYTEOFF
13718 "byte_offset",
13719#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013720#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013721 "channel",
13722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723#ifdef FEAT_CINDENT
13724 "cindent",
13725#endif
13726#ifdef FEAT_CLIENTSERVER
13727 "clientserver",
13728#endif
13729#ifdef FEAT_CLIPBOARD
13730 "clipboard",
13731#endif
13732#ifdef FEAT_CMDL_COMPL
13733 "cmdline_compl",
13734#endif
13735#ifdef FEAT_CMDHIST
13736 "cmdline_hist",
13737#endif
13738#ifdef FEAT_COMMENTS
13739 "comments",
13740#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013741#ifdef FEAT_CONCEAL
13742 "conceal",
13743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744#ifdef FEAT_CRYPT
13745 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013746 "crypt-blowfish",
13747 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#endif
13749#ifdef FEAT_CSCOPE
13750 "cscope",
13751#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013752#ifdef FEAT_CURSORBIND
13753 "cursorbind",
13754#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013755#ifdef CURSOR_SHAPE
13756 "cursorshape",
13757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758#ifdef DEBUG
13759 "debug",
13760#endif
13761#ifdef FEAT_CON_DIALOG
13762 "dialog_con",
13763#endif
13764#ifdef FEAT_GUI_DIALOG
13765 "dialog_gui",
13766#endif
13767#ifdef FEAT_DIFF
13768 "diff",
13769#endif
13770#ifdef FEAT_DIGRAPHS
13771 "digraphs",
13772#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013773#ifdef FEAT_DIRECTX
13774 "directx",
13775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776#ifdef FEAT_DND
13777 "dnd",
13778#endif
13779#ifdef FEAT_EMACS_TAGS
13780 "emacs_tags",
13781#endif
13782 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013783 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784#ifdef FEAT_SEARCH_EXTRA
13785 "extra_search",
13786#endif
13787#ifdef FEAT_FKMAP
13788 "farsi",
13789#endif
13790#ifdef FEAT_SEARCHPATH
13791 "file_in_path",
13792#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013793#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013794 "filterpipe",
13795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796#ifdef FEAT_FIND_ID
13797 "find_in_path",
13798#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013799#ifdef FEAT_FLOAT
13800 "float",
13801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802#ifdef FEAT_FOLDING
13803 "folding",
13804#endif
13805#ifdef FEAT_FOOTER
13806 "footer",
13807#endif
13808#if !defined(USE_SYSTEM) && defined(UNIX)
13809 "fork",
13810#endif
13811#ifdef FEAT_GETTEXT
13812 "gettext",
13813#endif
13814#ifdef FEAT_GUI
13815 "gui",
13816#endif
13817#ifdef FEAT_GUI_ATHENA
13818# ifdef FEAT_GUI_NEXTAW
13819 "gui_neXtaw",
13820# else
13821 "gui_athena",
13822# endif
13823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824#ifdef FEAT_GUI_GTK
13825 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013826# ifdef USE_GTK3
13827 "gui_gtk3",
13828# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013832#ifdef FEAT_GUI_GNOME
13833 "gui_gnome",
13834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835#ifdef FEAT_GUI_MAC
13836 "gui_mac",
13837#endif
13838#ifdef FEAT_GUI_MOTIF
13839 "gui_motif",
13840#endif
13841#ifdef FEAT_GUI_PHOTON
13842 "gui_photon",
13843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844#ifdef FEAT_GUI_W32
13845 "gui_win32",
13846#endif
13847#ifdef FEAT_HANGULIN
13848 "hangul_input",
13849#endif
13850#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13851 "iconv",
13852#endif
13853#ifdef FEAT_INS_EXPAND
13854 "insert_expand",
13855#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013856#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013857 "job",
13858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859#ifdef FEAT_JUMPLIST
13860 "jumplist",
13861#endif
13862#ifdef FEAT_KEYMAP
13863 "keymap",
13864#endif
13865#ifdef FEAT_LANGMAP
13866 "langmap",
13867#endif
13868#ifdef FEAT_LIBCALL
13869 "libcall",
13870#endif
13871#ifdef FEAT_LINEBREAK
13872 "linebreak",
13873#endif
13874#ifdef FEAT_LISP
13875 "lispindent",
13876#endif
13877#ifdef FEAT_LISTCMDS
13878 "listcmds",
13879#endif
13880#ifdef FEAT_LOCALMAP
13881 "localmap",
13882#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013883#ifdef FEAT_LUA
13884# ifndef DYNAMIC_LUA
13885 "lua",
13886# endif
13887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888#ifdef FEAT_MENU
13889 "menu",
13890#endif
13891#ifdef FEAT_SESSION
13892 "mksession",
13893#endif
13894#ifdef FEAT_MODIFY_FNAME
13895 "modify_fname",
13896#endif
13897#ifdef FEAT_MOUSE
13898 "mouse",
13899#endif
13900#ifdef FEAT_MOUSESHAPE
13901 "mouseshape",
13902#endif
13903#if defined(UNIX) || defined(VMS)
13904# ifdef FEAT_MOUSE_DEC
13905 "mouse_dec",
13906# endif
13907# ifdef FEAT_MOUSE_GPM
13908 "mouse_gpm",
13909# endif
13910# ifdef FEAT_MOUSE_JSB
13911 "mouse_jsbterm",
13912# endif
13913# ifdef FEAT_MOUSE_NET
13914 "mouse_netterm",
13915# endif
13916# ifdef FEAT_MOUSE_PTERM
13917 "mouse_pterm",
13918# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013919# ifdef FEAT_MOUSE_SGR
13920 "mouse_sgr",
13921# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013922# ifdef FEAT_SYSMOUSE
13923 "mouse_sysmouse",
13924# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013925# ifdef FEAT_MOUSE_URXVT
13926 "mouse_urxvt",
13927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928# ifdef FEAT_MOUSE_XTERM
13929 "mouse_xterm",
13930# endif
13931#endif
13932#ifdef FEAT_MBYTE
13933 "multi_byte",
13934#endif
13935#ifdef FEAT_MBYTE_IME
13936 "multi_byte_ime",
13937#endif
13938#ifdef FEAT_MULTI_LANG
13939 "multi_lang",
13940#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013941#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013942#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013943 "mzscheme",
13944#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946#ifdef FEAT_OLE
13947 "ole",
13948#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013949 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950#ifdef FEAT_PATH_EXTRA
13951 "path_extra",
13952#endif
13953#ifdef FEAT_PERL
13954#ifndef DYNAMIC_PERL
13955 "perl",
13956#endif
13957#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013958#ifdef FEAT_PERSISTENT_UNDO
13959 "persistent_undo",
13960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961#ifdef FEAT_PYTHON
13962#ifndef DYNAMIC_PYTHON
13963 "python",
13964#endif
13965#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013966#ifdef FEAT_PYTHON3
13967#ifndef DYNAMIC_PYTHON3
13968 "python3",
13969#endif
13970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971#ifdef FEAT_POSTSCRIPT
13972 "postscript",
13973#endif
13974#ifdef FEAT_PRINTER
13975 "printer",
13976#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013977#ifdef FEAT_PROFILE
13978 "profile",
13979#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013980#ifdef FEAT_RELTIME
13981 "reltime",
13982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983#ifdef FEAT_QUICKFIX
13984 "quickfix",
13985#endif
13986#ifdef FEAT_RIGHTLEFT
13987 "rightleft",
13988#endif
13989#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13990 "ruby",
13991#endif
13992#ifdef FEAT_SCROLLBIND
13993 "scrollbind",
13994#endif
13995#ifdef FEAT_CMDL_INFO
13996 "showcmd",
13997 "cmdline_info",
13998#endif
13999#ifdef FEAT_SIGNS
14000 "signs",
14001#endif
14002#ifdef FEAT_SMARTINDENT
14003 "smartindent",
14004#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014005#ifdef STARTUPTIME
14006 "startuptime",
14007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008#ifdef FEAT_STL_OPT
14009 "statusline",
14010#endif
14011#ifdef FEAT_SUN_WORKSHOP
14012 "sun_workshop",
14013#endif
14014#ifdef FEAT_NETBEANS_INTG
14015 "netbeans_intg",
14016#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014017#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014018 "spell",
14019#endif
14020#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 "syntax",
14022#endif
14023#if defined(USE_SYSTEM) || !defined(UNIX)
14024 "system",
14025#endif
14026#ifdef FEAT_TAG_BINS
14027 "tag_binary",
14028#endif
14029#ifdef FEAT_TAG_OLDSTATIC
14030 "tag_old_static",
14031#endif
14032#ifdef FEAT_TAG_ANYWHITE
14033 "tag_any_white",
14034#endif
14035#ifdef FEAT_TCL
14036# ifndef DYNAMIC_TCL
14037 "tcl",
14038# endif
14039#endif
14040#ifdef TERMINFO
14041 "terminfo",
14042#endif
14043#ifdef FEAT_TERMRESPONSE
14044 "termresponse",
14045#endif
14046#ifdef FEAT_TEXTOBJ
14047 "textobjects",
14048#endif
14049#ifdef HAVE_TGETENT
14050 "tgetent",
14051#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014052#ifdef FEAT_TIMERS
14053 "timers",
14054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055#ifdef FEAT_TITLE
14056 "title",
14057#endif
14058#ifdef FEAT_TOOLBAR
14059 "toolbar",
14060#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014061#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14062 "unnamedplus",
14063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064#ifdef FEAT_USR_CMDS
14065 "user-commands", /* was accidentally included in 5.4 */
14066 "user_commands",
14067#endif
14068#ifdef FEAT_VIMINFO
14069 "viminfo",
14070#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014071#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 "vertsplit",
14073#endif
14074#ifdef FEAT_VIRTUALEDIT
14075 "virtualedit",
14076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078#ifdef FEAT_VISUALEXTRA
14079 "visualextra",
14080#endif
14081#ifdef FEAT_VREPLACE
14082 "vreplace",
14083#endif
14084#ifdef FEAT_WILDIGN
14085 "wildignore",
14086#endif
14087#ifdef FEAT_WILDMENU
14088 "wildmenu",
14089#endif
14090#ifdef FEAT_WINDOWS
14091 "windows",
14092#endif
14093#ifdef FEAT_WAK
14094 "winaltkeys",
14095#endif
14096#ifdef FEAT_WRITEBACKUP
14097 "writebackup",
14098#endif
14099#ifdef FEAT_XIM
14100 "xim",
14101#endif
14102#ifdef FEAT_XFONTSET
14103 "xfontset",
14104#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014105#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014106 "xpm",
14107 "xpm_w32", /* for backward compatibility */
14108#else
14109# if defined(HAVE_XPM)
14110 "xpm",
14111# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113#ifdef USE_XSMP
14114 "xsmp",
14115#endif
14116#ifdef USE_XSMP_INTERACT
14117 "xsmp_interact",
14118#endif
14119#ifdef FEAT_XCLIPBOARD
14120 "xterm_clipboard",
14121#endif
14122#ifdef FEAT_XTERM_SAVE
14123 "xterm_save",
14124#endif
14125#if defined(UNIX) && defined(FEAT_X11)
14126 "X11",
14127#endif
14128 NULL
14129 };
14130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014131 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 for (i = 0; has_list[i] != NULL; ++i)
14133 if (STRICMP(name, has_list[i]) == 0)
14134 {
14135 n = TRUE;
14136 break;
14137 }
14138
14139 if (n == FALSE)
14140 {
14141 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014142 {
14143 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014144 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014145 && vim_isdigit(name[6])
14146 && vim_isdigit(name[8])
14147 && vim_isdigit(name[10]))
14148 {
14149 int major = atoi((char *)name + 6);
14150 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014151
14152 /* Expect "patch-9.9.01234". */
14153 n = (major < VIM_VERSION_MAJOR
14154 || (major == VIM_VERSION_MAJOR
14155 && (minor < VIM_VERSION_MINOR
14156 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014157 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014158 }
14159 else
14160 n = has_patch(atoi((char *)name + 5));
14161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 else if (STRICMP(name, "vim_starting") == 0)
14163 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014164#ifdef FEAT_MBYTE
14165 else if (STRICMP(name, "multi_byte_encoding") == 0)
14166 n = has_mbyte;
14167#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014168#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14169 else if (STRICMP(name, "balloon_multiline") == 0)
14170 n = multiline_balloon_available();
14171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172#ifdef DYNAMIC_TCL
14173 else if (STRICMP(name, "tcl") == 0)
14174 n = tcl_enabled(FALSE);
14175#endif
14176#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14177 else if (STRICMP(name, "iconv") == 0)
14178 n = iconv_enabled(FALSE);
14179#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014180#ifdef DYNAMIC_LUA
14181 else if (STRICMP(name, "lua") == 0)
14182 n = lua_enabled(FALSE);
14183#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014184#ifdef DYNAMIC_MZSCHEME
14185 else if (STRICMP(name, "mzscheme") == 0)
14186 n = mzscheme_enabled(FALSE);
14187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188#ifdef DYNAMIC_RUBY
14189 else if (STRICMP(name, "ruby") == 0)
14190 n = ruby_enabled(FALSE);
14191#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014192#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193#ifdef DYNAMIC_PYTHON
14194 else if (STRICMP(name, "python") == 0)
14195 n = python_enabled(FALSE);
14196#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014197#endif
14198#ifdef FEAT_PYTHON3
14199#ifdef DYNAMIC_PYTHON3
14200 else if (STRICMP(name, "python3") == 0)
14201 n = python3_enabled(FALSE);
14202#endif
14203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204#ifdef DYNAMIC_PERL
14205 else if (STRICMP(name, "perl") == 0)
14206 n = perl_enabled(FALSE);
14207#endif
14208#ifdef FEAT_GUI
14209 else if (STRICMP(name, "gui_running") == 0)
14210 n = (gui.in_use || gui.starting);
14211# ifdef FEAT_GUI_W32
14212 else if (STRICMP(name, "gui_win32s") == 0)
14213 n = gui_is_win32s();
14214# endif
14215# ifdef FEAT_BROWSE
14216 else if (STRICMP(name, "browse") == 0)
14217 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14218# endif
14219#endif
14220#ifdef FEAT_SYN_HL
14221 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014222 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223#endif
14224#if defined(WIN3264)
14225 else if (STRICMP(name, "win95") == 0)
14226 n = mch_windows95();
14227#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014228#ifdef FEAT_NETBEANS_INTG
14229 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014230 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 }
14233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014234 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235}
14236
14237/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014238 * "has_key()" function
14239 */
14240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014241f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014242{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014243 if (argvars[0].v_type != VAR_DICT)
14244 {
14245 EMSG(_(e_dictreq));
14246 return;
14247 }
14248 if (argvars[0].vval.v_dict == NULL)
14249 return;
14250
14251 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014252 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014253}
14254
14255/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014256 * "haslocaldir()" function
14257 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014259f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014260{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014261 win_T *wp = NULL;
14262
14263 wp = find_tabwin(&argvars[0], &argvars[1]);
14264 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014265}
14266
14267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 * "hasmapto()" function
14269 */
14270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014271f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272{
14273 char_u *name;
14274 char_u *mode;
14275 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014276 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014278 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014279 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 mode = (char_u *)"nvo";
14281 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014284 if (argvars[2].v_type != VAR_UNKNOWN)
14285 abbr = get_tv_number(&argvars[2]);
14286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287
Bram Moolenaar2c932302006-03-18 21:42:09 +000014288 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014289 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292}
14293
14294/*
14295 * "histadd()" function
14296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014298f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299{
14300#ifdef FEAT_CMDHIST
14301 int histype;
14302 char_u *str;
14303 char_u buf[NUMBUFLEN];
14304#endif
14305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014306 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 if (check_restricted() || check_secure())
14308 return;
14309#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014310 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14311 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 if (histype >= 0)
14313 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 if (*str != NUL)
14316 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014317 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014319 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 return;
14321 }
14322 }
14323#endif
14324}
14325
14326/*
14327 * "histdel()" function
14328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014330f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331{
14332#ifdef FEAT_CMDHIST
14333 int n;
14334 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014337 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14338 if (str == NULL)
14339 n = 0;
14340 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014343 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014345 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014346 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347 else
14348 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350 get_tv_string_buf(&argvars[1], buf));
14351 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352#endif
14353}
14354
14355/*
14356 * "histget()" function
14357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014359f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360{
14361#ifdef FEAT_CMDHIST
14362 int type;
14363 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14367 if (str == NULL)
14368 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 {
14371 type = get_histtype(str);
14372 if (argvars[1].v_type == VAR_UNKNOWN)
14373 idx = get_history_idx(type);
14374 else
14375 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14376 /* -1 on type error */
14377 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383}
14384
14385/*
14386 * "histnr()" function
14387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014389f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390{
14391 int i;
14392
14393#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014394 char_u *history = get_tv_string_chk(&argvars[0]);
14395
14396 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 if (i >= HIST_CMD && i < HIST_COUNT)
14398 i = get_history_idx(i);
14399 else
14400#endif
14401 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014402 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403}
14404
14405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 * "highlightID(name)" function
14407 */
14408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014409f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412}
14413
14414/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014415 * "highlight_exists()" function
14416 */
14417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014418f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014419{
14420 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14421}
14422
14423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 * "hostname()" function
14425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014427f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428{
14429 char_u hostname[256];
14430
14431 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 rettv->v_type = VAR_STRING;
14433 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434}
14435
14436/*
14437 * iconv() function
14438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014440f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441{
14442#ifdef FEAT_MBYTE
14443 char_u buf1[NUMBUFLEN];
14444 char_u buf2[NUMBUFLEN];
14445 char_u *from, *to, *str;
14446 vimconv_T vimconv;
14447#endif
14448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014449 rettv->v_type = VAR_STRING;
14450 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451
14452#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 str = get_tv_string(&argvars[0]);
14454 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14455 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 vimconv.vc_type = CONV_NONE;
14457 convert_setup(&vimconv, from, to);
14458
14459 /* If the encodings are equal, no conversion needed. */
14460 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014463 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464
14465 convert_setup(&vimconv, NULL, NULL);
14466 vim_free(from);
14467 vim_free(to);
14468#endif
14469}
14470
14471/*
14472 * "indent()" function
14473 */
14474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014475f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476{
14477 linenr_T lnum;
14478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484}
14485
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014486/*
14487 * "index()" function
14488 */
14489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014490f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014491{
Bram Moolenaar33570922005-01-25 22:26:29 +000014492 list_T *l;
14493 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014494 long idx = 0;
14495 int ic = FALSE;
14496
14497 rettv->vval.v_number = -1;
14498 if (argvars[0].v_type != VAR_LIST)
14499 {
14500 EMSG(_(e_listreq));
14501 return;
14502 }
14503 l = argvars[0].vval.v_list;
14504 if (l != NULL)
14505 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014506 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014507 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014509 int error = FALSE;
14510
Bram Moolenaar758711c2005-02-02 23:11:38 +000014511 /* Start at specified item. Use the cached index that list_find()
14512 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014513 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014514 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014515 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014516 ic = get_tv_number_chk(&argvars[3], &error);
14517 if (error)
14518 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014519 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014520
Bram Moolenaar758711c2005-02-02 23:11:38 +000014521 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014522 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014523 {
14524 rettv->vval.v_number = idx;
14525 break;
14526 }
14527 }
14528}
14529
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530static int inputsecret_flag = 0;
14531
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014532static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014533
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014535 * This function is used by f_input() and f_inputdialog() functions. The third
14536 * argument to f_input() specifies the type of completion to use at the
14537 * prompt. The third argument to f_inputdialog() specifies the value to return
14538 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 */
14540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014541get_user_input(
14542 typval_T *argvars,
14543 typval_T *rettv,
14544 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014546 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 char_u *p = NULL;
14548 int c;
14549 char_u buf[NUMBUFLEN];
14550 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014551 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014552 int xp_type = EXPAND_NOTHING;
14553 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014555 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014556 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557
14558#ifdef NO_CONSOLE_INPUT
14559 /* While starting up, there is no place to enter text. */
14560 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562#endif
14563
14564 cmd_silent = FALSE; /* Want to see the prompt. */
14565 if (prompt != NULL)
14566 {
14567 /* Only the part of the message after the last NL is considered as
14568 * prompt for the command line */
14569 p = vim_strrchr(prompt, '\n');
14570 if (p == NULL)
14571 p = prompt;
14572 else
14573 {
14574 ++p;
14575 c = *p;
14576 *p = NUL;
14577 msg_start();
14578 msg_clr_eos();
14579 msg_puts_attr(prompt, echo_attr);
14580 msg_didout = FALSE;
14581 msg_starthere();
14582 *p = c;
14583 }
14584 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014586 if (argvars[1].v_type != VAR_UNKNOWN)
14587 {
14588 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14589 if (defstr != NULL)
14590 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014592 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014593 {
14594 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014595 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014596 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014597
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014598 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014599 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014600
Bram Moolenaar4463f292005-09-25 22:20:24 +000014601 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14602 if (xp_name == NULL)
14603 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014604
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014605 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014606
Bram Moolenaar4463f292005-09-25 22:20:24 +000014607 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14608 &xp_arg) == FAIL)
14609 return;
14610 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014611 }
14612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014614 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014615 int save_ex_normal_busy = ex_normal_busy;
14616 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014617 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014618 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14619 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014620 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014621 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014622 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014623 && argvars[1].v_type != VAR_UNKNOWN
14624 && argvars[2].v_type != VAR_UNKNOWN)
14625 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14626 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014627
14628 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 /* since the user typed this, no need to wait for return */
14631 need_wait_return = FALSE;
14632 msg_didout = FALSE;
14633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 cmd_silent = cmd_silent_save;
14635}
14636
14637/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014638 * "input()" function
14639 * Also handles inputsecret() when inputsecret is set.
14640 */
14641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014642f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014643{
14644 get_user_input(argvars, rettv, FALSE);
14645}
14646
14647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 * "inputdialog()" function
14649 */
14650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014651f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652{
14653#if defined(FEAT_GUI_TEXTDIALOG)
14654 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14655 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14656 {
14657 char_u *message;
14658 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014659 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 message = get_tv_string_chk(&argvars[0]);
14662 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014663 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014664 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665 else
14666 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 if (message != NULL && defstr != NULL
14668 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014669 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014670 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671 else
14672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014673 if (message != NULL && defstr != NULL
14674 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014675 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 rettv->vval.v_string = vim_strsave(
14677 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682 }
14683 else
14684#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014685 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686}
14687
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014688/*
14689 * "inputlist()" function
14690 */
14691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014692f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014693{
14694 listitem_T *li;
14695 int selected;
14696 int mouse_used;
14697
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014698#ifdef NO_CONSOLE_INPUT
14699 /* While starting up, there is no place to enter text. */
14700 if (no_console_input())
14701 return;
14702#endif
14703 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14704 {
14705 EMSG2(_(e_listarg), "inputlist()");
14706 return;
14707 }
14708
14709 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014710 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014711 lines_left = Rows; /* avoid more prompt */
14712 msg_scroll = TRUE;
14713 msg_clr_eos();
14714
14715 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14716 {
14717 msg_puts(get_tv_string(&li->li_tv));
14718 msg_putchar('\n');
14719 }
14720
14721 /* Ask for choice. */
14722 selected = prompt_for_number(&mouse_used);
14723 if (mouse_used)
14724 selected -= lines_left;
14725
14726 rettv->vval.v_number = selected;
14727}
14728
14729
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14731
14732/*
14733 * "inputrestore()" function
14734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014736f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737{
14738 if (ga_userinput.ga_len > 0)
14739 {
14740 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14742 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014743 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 }
14745 else if (p_verbose > 1)
14746 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014747 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 }
14750}
14751
14752/*
14753 * "inputsave()" function
14754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014756f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014758 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 if (ga_grow(&ga_userinput, 1) == OK)
14760 {
14761 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14762 + ga_userinput.ga_len);
14763 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014764 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 }
14766 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014767 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768}
14769
14770/*
14771 * "inputsecret()" function
14772 */
14773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014774f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775{
14776 ++cmdline_star;
14777 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779 --cmdline_star;
14780 --inputsecret_flag;
14781}
14782
14783/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014784 * "insert()" function
14785 */
14786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014787f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014788{
14789 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 listitem_T *item;
14791 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014792 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014793
14794 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014795 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014796 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014797 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014798 {
14799 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014800 before = get_tv_number_chk(&argvars[2], &error);
14801 if (error)
14802 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014803
Bram Moolenaar758711c2005-02-02 23:11:38 +000014804 if (before == l->lv_len)
14805 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014806 else
14807 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014808 item = list_find(l, before);
14809 if (item == NULL)
14810 {
14811 EMSGN(_(e_listidx), before);
14812 l = NULL;
14813 }
14814 }
14815 if (l != NULL)
14816 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014817 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014818 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014819 }
14820 }
14821}
14822
14823/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014824 * "invert(expr)" function
14825 */
14826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014827f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014828{
14829 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14830}
14831
14832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 * "isdirectory()" function
14834 */
14835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014836f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014838 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839}
14840
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014841/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014842 * "islocked()" function
14843 */
14844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014845f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014846{
14847 lval_T lv;
14848 char_u *end;
14849 dictitem_T *di;
14850
14851 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014852 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14853 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014854 if (end != NULL && lv.ll_name != NULL)
14855 {
14856 if (*end != NUL)
14857 EMSG(_(e_trailing));
14858 else
14859 {
14860 if (lv.ll_tv == NULL)
14861 {
14862 if (check_changedtick(lv.ll_name))
14863 rettv->vval.v_number = 1; /* always locked */
14864 else
14865 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014866 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014867 if (di != NULL)
14868 {
14869 /* Consider a variable locked when:
14870 * 1. the variable itself is locked
14871 * 2. the value of the variable is locked.
14872 * 3. the List or Dict value is locked.
14873 */
14874 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14875 || tv_islocked(&di->di_tv));
14876 }
14877 }
14878 }
14879 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014880 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014881 else if (lv.ll_newkey != NULL)
14882 EMSG2(_(e_dictkey), lv.ll_newkey);
14883 else if (lv.ll_list != NULL)
14884 /* List item. */
14885 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14886 else
14887 /* Dictionary item. */
14888 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14889 }
14890 }
14891
14892 clear_lval(&lv);
14893}
14894
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014895#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14896/*
14897 * "isnan()" function
14898 */
14899 static void
14900f_isnan(typval_T *argvars, typval_T *rettv)
14901{
14902 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14903 && isnan(argvars[0].vval.v_float);
14904}
14905#endif
14906
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014907static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014908
14909/*
14910 * Turn a dict into a list:
14911 * "what" == 0: list of keys
14912 * "what" == 1: list of values
14913 * "what" == 2: list of items
14914 */
14915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014916dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014917{
Bram Moolenaar33570922005-01-25 22:26:29 +000014918 list_T *l2;
14919 dictitem_T *di;
14920 hashitem_T *hi;
14921 listitem_T *li;
14922 listitem_T *li2;
14923 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014924 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014925
Bram Moolenaar8c711452005-01-14 21:53:12 +000014926 if (argvars[0].v_type != VAR_DICT)
14927 {
14928 EMSG(_(e_dictreq));
14929 return;
14930 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014931 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014932 return;
14933
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014934 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014935 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014936
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014937 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014938 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014939 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014940 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014941 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014942 --todo;
14943 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014944
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014945 li = listitem_alloc();
14946 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014947 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014948 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014949
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014950 if (what == 0)
14951 {
14952 /* keys() */
14953 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014954 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014955 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14956 }
14957 else if (what == 1)
14958 {
14959 /* values() */
14960 copy_tv(&di->di_tv, &li->li_tv);
14961 }
14962 else
14963 {
14964 /* items() */
14965 l2 = list_alloc();
14966 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014967 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014968 li->li_tv.vval.v_list = l2;
14969 if (l2 == NULL)
14970 break;
14971 ++l2->lv_refcount;
14972
14973 li2 = listitem_alloc();
14974 if (li2 == NULL)
14975 break;
14976 list_append(l2, li2);
14977 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014978 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014979 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14980
14981 li2 = listitem_alloc();
14982 if (li2 == NULL)
14983 break;
14984 list_append(l2, li2);
14985 copy_tv(&di->di_tv, &li2->li_tv);
14986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014987 }
14988 }
14989}
14990
14991/*
14992 * "items(dict)" function
14993 */
14994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014995f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014996{
14997 dict_list(argvars, rettv, 2);
14998}
14999
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015000#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015001/*
15002 * Get the job from the argument.
15003 * Returns NULL if the job is invalid.
15004 */
15005 static job_T *
15006get_job_arg(typval_T *tv)
15007{
15008 job_T *job;
15009
15010 if (tv->v_type != VAR_JOB)
15011 {
15012 EMSG2(_(e_invarg2), get_tv_string(tv));
15013 return NULL;
15014 }
15015 job = tv->vval.v_job;
15016
15017 if (job == NULL)
15018 EMSG(_("E916: not a valid job"));
15019 return job;
15020}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015021
Bram Moolenaar835dc632016-02-07 14:27:38 +010015022/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015023 * "job_getchannel()" function
15024 */
15025 static void
15026f_job_getchannel(typval_T *argvars, typval_T *rettv)
15027{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015028 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015029
Bram Moolenaar65edff82016-02-21 16:40:11 +010015030 if (job != NULL)
15031 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015032 rettv->v_type = VAR_CHANNEL;
15033 rettv->vval.v_channel = job->jv_channel;
15034 if (job->jv_channel != NULL)
15035 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015036 }
15037}
15038
15039/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015040 * "job_info()" function
15041 */
15042 static void
15043f_job_info(typval_T *argvars, typval_T *rettv)
15044{
15045 job_T *job = get_job_arg(&argvars[0]);
15046
15047 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15048 job_info(job, rettv->vval.v_dict);
15049}
15050
15051/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015052 * "job_setoptions()" function
15053 */
15054 static void
15055f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15056{
15057 job_T *job = get_job_arg(&argvars[0]);
15058 jobopt_T opt;
15059
15060 if (job == NULL)
15061 return;
15062 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015063 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15064 job_set_options(job, &opt);
15065 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015066}
15067
15068/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015069 * "job_start()" function
15070 */
15071 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015072f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015073{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015074 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015075 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015076}
15077
15078/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015079 * "job_status()" function
15080 */
15081 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015082f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015083{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015084 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015085
Bram Moolenaar65edff82016-02-21 16:40:11 +010015086 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015087 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015088 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015089 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015090 }
15091}
15092
15093/*
15094 * "job_stop()" function
15095 */
15096 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015097f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015099 job_T *job = get_job_arg(&argvars[0]);
15100
15101 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015102 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015103}
15104#endif
15105
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015107 * "join()" function
15108 */
15109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015110f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015111{
15112 garray_T ga;
15113 char_u *sep;
15114
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015115 if (argvars[0].v_type != VAR_LIST)
15116 {
15117 EMSG(_(e_listreq));
15118 return;
15119 }
15120 if (argvars[0].vval.v_list == NULL)
15121 return;
15122 if (argvars[1].v_type == VAR_UNKNOWN)
15123 sep = (char_u *)" ";
15124 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015125 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015126
15127 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015128
15129 if (sep != NULL)
15130 {
15131 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015132 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015133 ga_append(&ga, NUL);
15134 rettv->vval.v_string = (char_u *)ga.ga_data;
15135 }
15136 else
15137 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015138}
15139
15140/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015141 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015142 */
15143 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015144f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015145{
15146 js_read_T reader;
15147
15148 reader.js_buf = get_tv_string(&argvars[0]);
15149 reader.js_fill = NULL;
15150 reader.js_used = 0;
15151 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15152 EMSG(_(e_invarg));
15153}
15154
15155/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015156 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015157 */
15158 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015159f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015160{
15161 rettv->v_type = VAR_STRING;
15162 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15163}
15164
15165/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015166 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015167 */
15168 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015169f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015170{
15171 js_read_T reader;
15172
15173 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015174 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015175 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015176 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015177 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015178}
15179
15180/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015181 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015182 */
15183 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015184f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015185{
15186 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015187 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015188}
15189
15190/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015191 * "keys()" function
15192 */
15193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015194f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015195{
15196 dict_list(argvars, rettv, 0);
15197}
15198
15199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200 * "last_buffer_nr()" function.
15201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015203f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204{
15205 int n = 0;
15206 buf_T *buf;
15207
15208 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15209 if (n < buf->b_fnum)
15210 n = buf->b_fnum;
15211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015213}
15214
15215/*
15216 * "len()" function
15217 */
15218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015219f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015220{
15221 switch (argvars[0].v_type)
15222 {
15223 case VAR_STRING:
15224 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015225 rettv->vval.v_number = (varnumber_T)STRLEN(
15226 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015227 break;
15228 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015229 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015230 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015231 case VAR_DICT:
15232 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15233 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015234 case VAR_UNKNOWN:
15235 case VAR_SPECIAL:
15236 case VAR_FLOAT:
15237 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015238 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015239 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015240 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015241 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015242 break;
15243 }
15244}
15245
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015246static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015247
15248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015249libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015250{
15251#ifdef FEAT_LIBCALL
15252 char_u *string_in;
15253 char_u **string_result;
15254 int nr_result;
15255#endif
15256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015258 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015259 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015260
15261 if (check_restricted() || check_secure())
15262 return;
15263
15264#ifdef FEAT_LIBCALL
15265 /* The first two args must be strings, otherwise its meaningless */
15266 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15267 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015268 string_in = NULL;
15269 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270 string_in = argvars[2].vval.v_string;
15271 if (type == VAR_NUMBER)
15272 string_result = NULL;
15273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015274 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015275 if (mch_libcall(argvars[0].vval.v_string,
15276 argvars[1].vval.v_string,
15277 string_in,
15278 argvars[2].vval.v_number,
15279 string_result,
15280 &nr_result) == OK
15281 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015283 }
15284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285}
15286
15287/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015288 * "libcall()" function
15289 */
15290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015291f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015292{
15293 libcall_common(argvars, rettv, VAR_STRING);
15294}
15295
15296/*
15297 * "libcallnr()" function
15298 */
15299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015300f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015301{
15302 libcall_common(argvars, rettv, VAR_NUMBER);
15303}
15304
15305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 * "line(string)" function
15307 */
15308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015309f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310{
15311 linenr_T lnum = 0;
15312 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015313 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015315 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 if (fp != NULL)
15317 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015318 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319}
15320
15321/*
15322 * "line2byte(lnum)" function
15323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015325f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326{
15327#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015328 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329#else
15330 linenr_T lnum;
15331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015332 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015334 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015336 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15337 if (rettv->vval.v_number >= 0)
15338 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339#endif
15340}
15341
15342/*
15343 * "lispindent(lnum)" function
15344 */
15345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015346f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347{
15348#ifdef FEAT_LISP
15349 pos_T pos;
15350 linenr_T lnum;
15351
15352 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015353 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15355 {
15356 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 curwin->w_cursor = pos;
15359 }
15360 else
15361#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363}
15364
15365/*
15366 * "localtime()" function
15367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015369f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015371 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372}
15373
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015374static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375
15376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015377get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378{
15379 char_u *keys;
15380 char_u *which;
15381 char_u buf[NUMBUFLEN];
15382 char_u *keys_buf = NULL;
15383 char_u *rhs;
15384 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015385 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015386 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015387 mapblock_T *mp;
15388 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389
15390 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391 rettv->v_type = VAR_STRING;
15392 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 if (*keys == NUL)
15396 return;
15397
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015398 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015400 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015401 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015402 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015403 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015404 if (argvars[3].v_type != VAR_UNKNOWN)
15405 get_dict = get_tv_number(&argvars[3]);
15406 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 else
15409 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015410 if (which == NULL)
15411 return;
15412
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 mode = get_map_mode(&which, 0);
15414
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015415 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015416 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015418
15419 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015421 /* Return a string. */
15422 if (rhs != NULL)
15423 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424
Bram Moolenaarbd743252010-10-20 21:23:33 +020015425 }
15426 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15427 {
15428 /* Return a dictionary. */
15429 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15430 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15431 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432
Bram Moolenaarbd743252010-10-20 21:23:33 +020015433 dict_add_nr_str(dict, "lhs", 0L, lhs);
15434 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15435 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15436 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15437 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15438 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15439 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015440 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015441 dict_add_nr_str(dict, "mode", 0L, mapmode);
15442
15443 vim_free(lhs);
15444 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 }
15446}
15447
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015448#ifdef FEAT_FLOAT
15449/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015450 * "log()" function
15451 */
15452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015453f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015454{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015455 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015456
15457 rettv->v_type = VAR_FLOAT;
15458 if (get_float_arg(argvars, &f) == OK)
15459 rettv->vval.v_float = log(f);
15460 else
15461 rettv->vval.v_float = 0.0;
15462}
15463
15464/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015465 * "log10()" function
15466 */
15467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015468f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015469{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015470 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015471
15472 rettv->v_type = VAR_FLOAT;
15473 if (get_float_arg(argvars, &f) == OK)
15474 rettv->vval.v_float = log10(f);
15475 else
15476 rettv->vval.v_float = 0.0;
15477}
15478#endif
15479
Bram Moolenaar1dced572012-04-05 16:54:08 +020015480#ifdef FEAT_LUA
15481/*
15482 * "luaeval()" function
15483 */
15484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015485f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015486{
15487 char_u *str;
15488 char_u buf[NUMBUFLEN];
15489
15490 str = get_tv_string_buf(&argvars[0], buf);
15491 do_luaeval(str, argvars + 1, rettv);
15492}
15493#endif
15494
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015496 * "map()" function
15497 */
15498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015499f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015500{
15501 filter_map(argvars, rettv, TRUE);
15502}
15503
15504/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015505 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 */
15507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015508f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015510 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511}
15512
15513/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015514 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 */
15516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015517f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015519 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520}
15521
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015522static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523
15524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015525find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015527 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015528 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015529 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 char_u *pat;
15531 regmatch_T regmatch;
15532 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015533 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 char_u *save_cpo;
15535 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015536 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015537 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015538 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015539 list_T *l = NULL;
15540 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015541 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015542 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543
15544 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15545 save_cpo = p_cpo;
15546 p_cpo = (char_u *)"";
15547
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015548 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015549 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015550 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015551 /* type 3: return empty list when there are no matches.
15552 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015553 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015554 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015555 if (type == 4
15556 && (list_append_string(rettv->vval.v_list,
15557 (char_u *)"", 0) == FAIL
15558 || list_append_number(rettv->vval.v_list,
15559 (varnumber_T)-1) == FAIL
15560 || list_append_number(rettv->vval.v_list,
15561 (varnumber_T)-1) == FAIL
15562 || list_append_number(rettv->vval.v_list,
15563 (varnumber_T)-1) == FAIL))
15564 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015565 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015566 rettv->vval.v_list = NULL;
15567 goto theend;
15568 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015569 }
15570 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015572 rettv->v_type = VAR_STRING;
15573 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015576 if (argvars[0].v_type == VAR_LIST)
15577 {
15578 if ((l = argvars[0].vval.v_list) == NULL)
15579 goto theend;
15580 li = l->lv_first;
15581 }
15582 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015583 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015584 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015585 len = (long)STRLEN(str);
15586 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15589 if (pat == NULL)
15590 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015591
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015592 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015594 int error = FALSE;
15595
15596 start = get_tv_number_chk(&argvars[2], &error);
15597 if (error)
15598 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015599 if (l != NULL)
15600 {
15601 li = list_find(l, start);
15602 if (li == NULL)
15603 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015604 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015605 }
15606 else
15607 {
15608 if (start < 0)
15609 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015610 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015611 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015612 /* When "count" argument is there ignore matches before "start",
15613 * otherwise skip part of the string. Differs when pattern is "^"
15614 * or "\<". */
15615 if (argvars[3].v_type != VAR_UNKNOWN)
15616 startcol = start;
15617 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015618 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015619 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015620 len -= start;
15621 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015622 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015623
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015624 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015625 nth = get_tv_number_chk(&argvars[3], &error);
15626 if (error)
15627 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 }
15629
15630 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15631 if (regmatch.regprog != NULL)
15632 {
15633 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015634
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015635 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015636 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015637 if (l != NULL)
15638 {
15639 if (li == NULL)
15640 {
15641 match = FALSE;
15642 break;
15643 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015644 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015645 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015646 if (str == NULL)
15647 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015648 }
15649
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015650 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015651
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015652 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015653 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015654 if (l == NULL && !match)
15655 break;
15656
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015657 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015658 if (l != NULL)
15659 {
15660 li = li->li_next;
15661 ++idx;
15662 }
15663 else
15664 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015665#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015666 startcol = (colnr_T)(regmatch.startp[0]
15667 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015668#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015669 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015670#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015671 if (startcol > (colnr_T)len
15672 || str + startcol <= regmatch.startp[0])
15673 {
15674 match = FALSE;
15675 break;
15676 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015677 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015678 }
15679
15680 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015682 if (type == 4)
15683 {
15684 listitem_T *li1 = rettv->vval.v_list->lv_first;
15685 listitem_T *li2 = li1->li_next;
15686 listitem_T *li3 = li2->li_next;
15687 listitem_T *li4 = li3->li_next;
15688
15689 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15690 (int)(regmatch.endp[0] - regmatch.startp[0]));
15691 li3->li_tv.vval.v_number =
15692 (varnumber_T)(regmatch.startp[0] - expr);
15693 li4->li_tv.vval.v_number =
15694 (varnumber_T)(regmatch.endp[0] - expr);
15695 if (l != NULL)
15696 li2->li_tv.vval.v_number = (varnumber_T)idx;
15697 }
15698 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015699 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015700 int i;
15701
15702 /* return list with matched string and submatches */
15703 for (i = 0; i < NSUBEXP; ++i)
15704 {
15705 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015706 {
15707 if (list_append_string(rettv->vval.v_list,
15708 (char_u *)"", 0) == FAIL)
15709 break;
15710 }
15711 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015712 regmatch.startp[i],
15713 (int)(regmatch.endp[i] - regmatch.startp[i]))
15714 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015715 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015716 }
15717 }
15718 else if (type == 2)
15719 {
15720 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015721 if (l != NULL)
15722 copy_tv(&li->li_tv, rettv);
15723 else
15724 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015726 }
15727 else if (l != NULL)
15728 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 else
15730 {
15731 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015732 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 (varnumber_T)(regmatch.startp[0] - str);
15734 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015735 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015737 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 }
15739 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015740 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 }
15742
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015743 if (type == 4 && l == NULL)
15744 /* matchstrpos() without a list: drop the second item. */
15745 listitem_remove(rettv->vval.v_list,
15746 rettv->vval.v_list->lv_first->li_next);
15747
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015749 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 p_cpo = save_cpo;
15751}
15752
15753/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015754 * "match()" function
15755 */
15756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015757f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758{
15759 find_some_match(argvars, rettv, 1);
15760}
15761
15762/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015763 * "matchadd()" function
15764 */
15765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015766f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015767{
15768#ifdef FEAT_SEARCH_EXTRA
15769 char_u buf[NUMBUFLEN];
15770 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15771 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15772 int prio = 10; /* default priority */
15773 int id = -1;
15774 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015775 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015776
15777 rettv->vval.v_number = -1;
15778
15779 if (grp == NULL || pat == NULL)
15780 return;
15781 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015782 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015783 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015784 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015785 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015786 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015787 if (argvars[4].v_type != VAR_UNKNOWN)
15788 {
15789 if (argvars[4].v_type != VAR_DICT)
15790 {
15791 EMSG(_(e_dictreq));
15792 return;
15793 }
15794 if (dict_find(argvars[4].vval.v_dict,
15795 (char_u *)"conceal", -1) != NULL)
15796 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15797 (char_u *)"conceal", FALSE);
15798 }
15799 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015800 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015801 if (error == TRUE)
15802 return;
15803 if (id >= 1 && id <= 3)
15804 {
15805 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15806 return;
15807 }
15808
Bram Moolenaar6561d522015-07-21 15:48:27 +020015809 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15810 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015811#endif
15812}
15813
15814/*
15815 * "matchaddpos()" function
15816 */
15817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015818f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015819{
15820#ifdef FEAT_SEARCH_EXTRA
15821 char_u buf[NUMBUFLEN];
15822 char_u *group;
15823 int prio = 10;
15824 int id = -1;
15825 int error = FALSE;
15826 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015827 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015828
15829 rettv->vval.v_number = -1;
15830
15831 group = get_tv_string_buf_chk(&argvars[0], buf);
15832 if (group == NULL)
15833 return;
15834
15835 if (argvars[1].v_type != VAR_LIST)
15836 {
15837 EMSG2(_(e_listarg), "matchaddpos()");
15838 return;
15839 }
15840 l = argvars[1].vval.v_list;
15841 if (l == NULL)
15842 return;
15843
15844 if (argvars[2].v_type != VAR_UNKNOWN)
15845 {
15846 prio = get_tv_number_chk(&argvars[2], &error);
15847 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015848 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015849 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015850 if (argvars[4].v_type != VAR_UNKNOWN)
15851 {
15852 if (argvars[4].v_type != VAR_DICT)
15853 {
15854 EMSG(_(e_dictreq));
15855 return;
15856 }
15857 if (dict_find(argvars[4].vval.v_dict,
15858 (char_u *)"conceal", -1) != NULL)
15859 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15860 (char_u *)"conceal", FALSE);
15861 }
15862 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015863 }
15864 if (error == TRUE)
15865 return;
15866
15867 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15868 if (id == 1 || id == 2)
15869 {
15870 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15871 return;
15872 }
15873
Bram Moolenaar6561d522015-07-21 15:48:27 +020015874 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15875 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015876#endif
15877}
15878
15879/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015880 * "matcharg()" function
15881 */
15882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015883f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015884{
15885 if (rettv_list_alloc(rettv) == OK)
15886 {
15887#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015888 int id = get_tv_number(&argvars[0]);
15889 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015890
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015891 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015892 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015893 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15894 {
15895 list_append_string(rettv->vval.v_list,
15896 syn_id2name(m->hlg_id), -1);
15897 list_append_string(rettv->vval.v_list, m->pattern, -1);
15898 }
15899 else
15900 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015901 list_append_string(rettv->vval.v_list, NULL, -1);
15902 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015903 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015904 }
15905#endif
15906 }
15907}
15908
15909/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015910 * "matchdelete()" function
15911 */
15912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015913f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015914{
15915#ifdef FEAT_SEARCH_EXTRA
15916 rettv->vval.v_number = match_delete(curwin,
15917 (int)get_tv_number(&argvars[0]), TRUE);
15918#endif
15919}
15920
15921/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015922 * "matchend()" function
15923 */
15924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015925f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015926{
15927 find_some_match(argvars, rettv, 0);
15928}
15929
15930/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015931 * "matchlist()" function
15932 */
15933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015934f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015935{
15936 find_some_match(argvars, rettv, 3);
15937}
15938
15939/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015940 * "matchstr()" function
15941 */
15942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015943f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015944{
15945 find_some_match(argvars, rettv, 2);
15946}
15947
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015948/*
15949 * "matchstrpos()" function
15950 */
15951 static void
15952f_matchstrpos(typval_T *argvars, typval_T *rettv)
15953{
15954 find_some_match(argvars, rettv, 4);
15955}
15956
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015957static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015958
15959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015960max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015961{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015962 long n = 0;
15963 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015964 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015965
15966 if (argvars[0].v_type == VAR_LIST)
15967 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015968 list_T *l;
15969 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015970
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015971 l = argvars[0].vval.v_list;
15972 if (l != NULL)
15973 {
15974 li = l->lv_first;
15975 if (li != NULL)
15976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015977 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015978 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015979 {
15980 li = li->li_next;
15981 if (li == NULL)
15982 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015984 if (domax ? i > n : i < n)
15985 n = i;
15986 }
15987 }
15988 }
15989 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015990 else if (argvars[0].v_type == VAR_DICT)
15991 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015992 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015993 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015994 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015995 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015996
15997 d = argvars[0].vval.v_dict;
15998 if (d != NULL)
15999 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016000 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016001 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016002 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016003 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016004 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016005 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016006 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016007 if (first)
16008 {
16009 n = i;
16010 first = FALSE;
16011 }
16012 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016013 n = i;
16014 }
16015 }
16016 }
16017 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016018 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016019 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016021}
16022
16023/*
16024 * "max()" function
16025 */
16026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016027f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016028{
16029 max_min(argvars, rettv, TRUE);
16030}
16031
16032/*
16033 * "min()" function
16034 */
16035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016036f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016037{
16038 max_min(argvars, rettv, FALSE);
16039}
16040
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016041static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016042
16043/*
16044 * Create the directory in which "dir" is located, and higher levels when
16045 * needed.
16046 */
16047 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016048mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016049{
16050 char_u *p;
16051 char_u *updir;
16052 int r = FAIL;
16053
16054 /* Get end of directory name in "dir".
16055 * We're done when it's "/" or "c:/". */
16056 p = gettail_sep(dir);
16057 if (p <= get_past_head(dir))
16058 return OK;
16059
16060 /* If the directory exists we're done. Otherwise: create it.*/
16061 updir = vim_strnsave(dir, (int)(p - dir));
16062 if (updir == NULL)
16063 return FAIL;
16064 if (mch_isdir(updir))
16065 r = OK;
16066 else if (mkdir_recurse(updir, prot) == OK)
16067 r = vim_mkdir_emsg(updir, prot);
16068 vim_free(updir);
16069 return r;
16070}
16071
16072#ifdef vim_mkdir
16073/*
16074 * "mkdir()" function
16075 */
16076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016077f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016078{
16079 char_u *dir;
16080 char_u buf[NUMBUFLEN];
16081 int prot = 0755;
16082
16083 rettv->vval.v_number = FAIL;
16084 if (check_restricted() || check_secure())
16085 return;
16086
16087 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016088 if (*dir == NUL)
16089 rettv->vval.v_number = FAIL;
16090 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016091 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016092 if (*gettail(dir) == NUL)
16093 /* remove trailing slashes */
16094 *gettail_sep(dir) = NUL;
16095
16096 if (argvars[1].v_type != VAR_UNKNOWN)
16097 {
16098 if (argvars[2].v_type != VAR_UNKNOWN)
16099 prot = get_tv_number_chk(&argvars[2], NULL);
16100 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16101 mkdir_recurse(dir, prot);
16102 }
16103 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016104 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016105}
16106#endif
16107
Bram Moolenaar0d660222005-01-07 21:51:51 +000016108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 * "mode()" function
16110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016112f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016114 char_u buf[3];
16115
16116 buf[1] = NUL;
16117 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 if (VIsual_active)
16120 {
16121 if (VIsual_select)
16122 buf[0] = VIsual_mode + 's' - 'v';
16123 else
16124 buf[0] = VIsual_mode;
16125 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016126 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016127 || State == CONFIRM)
16128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016130 if (State == ASKMORE)
16131 buf[1] = 'm';
16132 else if (State == CONFIRM)
16133 buf[1] = '?';
16134 }
16135 else if (State == EXTERNCMD)
16136 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 else if (State & INSERT)
16138 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016139#ifdef FEAT_VREPLACE
16140 if (State & VREPLACE_FLAG)
16141 {
16142 buf[0] = 'R';
16143 buf[1] = 'v';
16144 }
16145 else
16146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 if (State & REPLACE_FLAG)
16148 buf[0] = 'R';
16149 else
16150 buf[0] = 'i';
16151 }
16152 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016155 if (exmode_active)
16156 buf[1] = 'v';
16157 }
16158 else if (exmode_active)
16159 {
16160 buf[0] = 'c';
16161 buf[1] = 'e';
16162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016166 if (finish_op)
16167 buf[1] = 'o';
16168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016170 /* Clear out the minor mode when the argument is not a non-zero number or
16171 * non-empty string. */
16172 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016173 buf[1] = NUL;
16174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175 rettv->vval.v_string = vim_strsave(buf);
16176 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177}
16178
Bram Moolenaar429fa852013-04-15 12:27:36 +020016179#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016180/*
16181 * "mzeval()" function
16182 */
16183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016184f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016185{
16186 char_u *str;
16187 char_u buf[NUMBUFLEN];
16188
16189 str = get_tv_string_buf(&argvars[0], buf);
16190 do_mzeval(str, rettv);
16191}
Bram Moolenaar75676462013-01-30 14:55:42 +010016192
16193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016194mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016195{
16196 typval_T argvars[3];
16197
16198 argvars[0].v_type = VAR_STRING;
16199 argvars[0].vval.v_string = name;
16200 copy_tv(args, &argvars[1]);
16201 argvars[2].v_type = VAR_UNKNOWN;
16202 f_call(argvars, rettv);
16203 clear_tv(&argvars[1]);
16204}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016205#endif
16206
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016208 * "nextnonblank()" function
16209 */
16210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016211f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212{
16213 linenr_T lnum;
16214
16215 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016217 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218 {
16219 lnum = 0;
16220 break;
16221 }
16222 if (*skipwhite(ml_get(lnum)) != NUL)
16223 break;
16224 }
16225 rettv->vval.v_number = lnum;
16226}
16227
16228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229 * "nr2char()" function
16230 */
16231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016232f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233{
16234 char_u buf[NUMBUFLEN];
16235
16236#ifdef FEAT_MBYTE
16237 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016238 {
16239 int utf8 = 0;
16240
16241 if (argvars[1].v_type != VAR_UNKNOWN)
16242 utf8 = get_tv_number_chk(&argvars[1], NULL);
16243 if (utf8)
16244 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16245 else
16246 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 else
16249#endif
16250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016251 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 buf[1] = NUL;
16253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016254 rettv->v_type = VAR_STRING;
16255 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016256}
16257
16258/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016259 * "or(expr, expr)" function
16260 */
16261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016262f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016263{
16264 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16265 | get_tv_number_chk(&argvars[1], NULL);
16266}
16267
16268/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016269 * "pathshorten()" function
16270 */
16271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016272f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016273{
16274 char_u *p;
16275
16276 rettv->v_type = VAR_STRING;
16277 p = get_tv_string_chk(&argvars[0]);
16278 if (p == NULL)
16279 rettv->vval.v_string = NULL;
16280 else
16281 {
16282 p = vim_strsave(p);
16283 rettv->vval.v_string = p;
16284 if (p != NULL)
16285 shorten_dir(p);
16286 }
16287}
16288
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016289#ifdef FEAT_PERL
16290/*
16291 * "perleval()" function
16292 */
16293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016294f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016295{
16296 char_u *str;
16297 char_u buf[NUMBUFLEN];
16298
16299 str = get_tv_string_buf(&argvars[0], buf);
16300 do_perleval(str, rettv);
16301}
16302#endif
16303
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016304#ifdef FEAT_FLOAT
16305/*
16306 * "pow()" function
16307 */
16308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016309f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016310{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016311 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016312
16313 rettv->v_type = VAR_FLOAT;
16314 if (get_float_arg(argvars, &fx) == OK
16315 && get_float_arg(&argvars[1], &fy) == OK)
16316 rettv->vval.v_float = pow(fx, fy);
16317 else
16318 rettv->vval.v_float = 0.0;
16319}
16320#endif
16321
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016322/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016323 * "prevnonblank()" function
16324 */
16325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016326f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016327{
16328 linenr_T lnum;
16329
16330 lnum = get_tv_lnum(argvars);
16331 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16332 lnum = 0;
16333 else
16334 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16335 --lnum;
16336 rettv->vval.v_number = lnum;
16337}
16338
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016339/* This dummy va_list is here because:
16340 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16341 * - locally in the function results in a "used before set" warning
16342 * - using va_start() to initialize it gives "function with fixed args" error */
16343static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016344
Bram Moolenaar8c711452005-01-14 21:53:12 +000016345/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016346 * "printf()" function
16347 */
16348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016349f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016350{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016351 char_u buf[NUMBUFLEN];
16352 int len;
16353 char_u *s;
16354 int saved_did_emsg = did_emsg;
16355 char *fmt;
16356
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016357 rettv->v_type = VAR_STRING;
16358 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016359
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016360 /* Get the required length, allocate the buffer and do it for real. */
16361 did_emsg = FALSE;
16362 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16363 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16364 if (!did_emsg)
16365 {
16366 s = alloc(len + 1);
16367 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016368 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016369 rettv->vval.v_string = s;
16370 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016371 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016372 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016373 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016374}
16375
16376/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016377 * "pumvisible()" function
16378 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016380f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016381{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016382#ifdef FEAT_INS_EXPAND
16383 if (pum_visible())
16384 rettv->vval.v_number = 1;
16385#endif
16386}
16387
Bram Moolenaardb913952012-06-29 12:54:53 +020016388#ifdef FEAT_PYTHON3
16389/*
16390 * "py3eval()" function
16391 */
16392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016393f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016394{
16395 char_u *str;
16396 char_u buf[NUMBUFLEN];
16397
16398 str = get_tv_string_buf(&argvars[0], buf);
16399 do_py3eval(str, rettv);
16400}
16401#endif
16402
16403#ifdef FEAT_PYTHON
16404/*
16405 * "pyeval()" function
16406 */
16407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016408f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016409{
16410 char_u *str;
16411 char_u buf[NUMBUFLEN];
16412
16413 str = get_tv_string_buf(&argvars[0], buf);
16414 do_pyeval(str, rettv);
16415}
16416#endif
16417
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016418/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016419 * "range()" function
16420 */
16421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016422f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016423{
16424 long start;
16425 long end;
16426 long stride = 1;
16427 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016428 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016430 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016431 if (argvars[1].v_type == VAR_UNKNOWN)
16432 {
16433 end = start - 1;
16434 start = 0;
16435 }
16436 else
16437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016438 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016439 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016440 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016441 }
16442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 if (error)
16444 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016445 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016446 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016447 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016448 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016449 else
16450 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016451 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016452 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016453 if (list_append_number(rettv->vval.v_list,
16454 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016455 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016456 }
16457}
16458
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016459/*
16460 * "readfile()" function
16461 */
16462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016463f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016464{
16465 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016466 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016467 char_u *fname;
16468 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016469 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16470 int io_size = sizeof(buf);
16471 int readlen; /* size of last fread() */
16472 char_u *prev = NULL; /* previously read bytes, if any */
16473 long prevlen = 0; /* length of data in prev */
16474 long prevsize = 0; /* size of prev buffer */
16475 long maxline = MAXLNUM;
16476 long cnt = 0;
16477 char_u *p; /* position in buf */
16478 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016479
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016480 if (argvars[1].v_type != VAR_UNKNOWN)
16481 {
16482 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16483 binary = TRUE;
16484 if (argvars[2].v_type != VAR_UNKNOWN)
16485 maxline = get_tv_number(&argvars[2]);
16486 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016487
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016488 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016489 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016490
16491 /* Always open the file in binary mode, library functions have a mind of
16492 * their own about CR-LF conversion. */
16493 fname = get_tv_string(&argvars[0]);
16494 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16495 {
16496 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16497 return;
16498 }
16499
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016500 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016501 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016502 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016503
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016504 /* This for loop processes what was read, but is also entered at end
16505 * of file so that either:
16506 * - an incomplete line gets written
16507 * - a "binary" file gets an empty line at the end if it ends in a
16508 * newline. */
16509 for (p = buf, start = buf;
16510 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16511 ++p)
16512 {
16513 if (*p == '\n' || readlen <= 0)
16514 {
16515 listitem_T *li;
16516 char_u *s = NULL;
16517 long_u len = p - start;
16518
16519 /* Finished a line. Remove CRs before NL. */
16520 if (readlen > 0 && !binary)
16521 {
16522 while (len > 0 && start[len - 1] == '\r')
16523 --len;
16524 /* removal may cross back to the "prev" string */
16525 if (len == 0)
16526 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16527 --prevlen;
16528 }
16529 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016530 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016531 else
16532 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016533 /* Change "prev" buffer to be the right size. This way
16534 * the bytes are only copied once, and very long lines are
16535 * allocated only once. */
16536 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016537 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016538 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016539 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016540 prev = NULL; /* the list will own the string */
16541 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016542 }
16543 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016544 if (s == NULL)
16545 {
16546 do_outofmem_msg((long_u) prevlen + len + 1);
16547 failed = TRUE;
16548 break;
16549 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016550
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016551 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016552 {
16553 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016554 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016555 break;
16556 }
16557 li->li_tv.v_type = VAR_STRING;
16558 li->li_tv.v_lock = 0;
16559 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016560 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016561
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016562 start = p + 1; /* step over newline */
16563 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016564 break;
16565 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016566 else if (*p == NUL)
16567 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016568#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016569 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16570 * when finding the BF and check the previous two bytes. */
16571 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016572 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016573 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16574 * + 1, these may be in the "prev" string. */
16575 char_u back1 = p >= buf + 1 ? p[-1]
16576 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16577 char_u back2 = p >= buf + 2 ? p[-2]
16578 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16579 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016580
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016581 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016582 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016583 char_u *dest = p - 2;
16584
16585 /* Usually a BOM is at the beginning of a file, and so at
16586 * the beginning of a line; then we can just step over it.
16587 */
16588 if (start == dest)
16589 start = p + 1;
16590 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016591 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016592 /* have to shuffle buf to close gap */
16593 int adjust_prevlen = 0;
16594
16595 if (dest < buf)
16596 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016597 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016598 dest = buf;
16599 }
16600 if (readlen > p - buf + 1)
16601 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16602 readlen -= 3 - adjust_prevlen;
16603 prevlen -= adjust_prevlen;
16604 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016605 }
16606 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016607 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016608#endif
16609 } /* for */
16610
16611 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16612 break;
16613 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016614 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016615 /* There's part of a line in buf, store it in "prev". */
16616 if (p - start + prevlen >= prevsize)
16617 {
16618 /* need bigger "prev" buffer */
16619 char_u *newprev;
16620
16621 /* A common use case is ordinary text files and "prev" gets a
16622 * fragment of a line, so the first allocation is made
16623 * small, to avoid repeatedly 'allocing' large and
16624 * 'reallocing' small. */
16625 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016626 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016627 else
16628 {
16629 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016630 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016631 prevsize = grow50pc > growmin ? grow50pc : growmin;
16632 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016633 newprev = prev == NULL ? alloc(prevsize)
16634 : vim_realloc(prev, prevsize);
16635 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016636 {
16637 do_outofmem_msg((long_u)prevsize);
16638 failed = TRUE;
16639 break;
16640 }
16641 prev = newprev;
16642 }
16643 /* Add the line part to end of "prev". */
16644 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016645 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016646 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016647 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016648
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016649 /*
16650 * For a negative line count use only the lines at the end of the file,
16651 * free the rest.
16652 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016653 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016654 while (cnt > -maxline)
16655 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016656 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016657 --cnt;
16658 }
16659
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016660 if (failed)
16661 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016662 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016663 /* readfile doc says an empty list is returned on error */
16664 rettv->vval.v_list = list_alloc();
16665 }
16666
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016667 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016668 fclose(fd);
16669}
16670
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016671#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016672static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016673
16674/*
16675 * Convert a List to proftime_T.
16676 * Return FAIL when there is something wrong.
16677 */
16678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016679list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016680{
16681 long n1, n2;
16682 int error = FALSE;
16683
16684 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16685 || arg->vval.v_list->lv_len != 2)
16686 return FAIL;
16687 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16688 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16689# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016690 tm->HighPart = n1;
16691 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016692# else
16693 tm->tv_sec = n1;
16694 tm->tv_usec = n2;
16695# endif
16696 return error ? FAIL : OK;
16697}
16698#endif /* FEAT_RELTIME */
16699
16700/*
16701 * "reltime()" function
16702 */
16703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016704f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016705{
16706#ifdef FEAT_RELTIME
16707 proftime_T res;
16708 proftime_T start;
16709
16710 if (argvars[0].v_type == VAR_UNKNOWN)
16711 {
16712 /* No arguments: get current time. */
16713 profile_start(&res);
16714 }
16715 else if (argvars[1].v_type == VAR_UNKNOWN)
16716 {
16717 if (list2proftime(&argvars[0], &res) == FAIL)
16718 return;
16719 profile_end(&res);
16720 }
16721 else
16722 {
16723 /* Two arguments: compute the difference. */
16724 if (list2proftime(&argvars[0], &start) == FAIL
16725 || list2proftime(&argvars[1], &res) == FAIL)
16726 return;
16727 profile_sub(&res, &start);
16728 }
16729
16730 if (rettv_list_alloc(rettv) == OK)
16731 {
16732 long n1, n2;
16733
16734# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016735 n1 = res.HighPart;
16736 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016737# else
16738 n1 = res.tv_sec;
16739 n2 = res.tv_usec;
16740# endif
16741 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16742 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16743 }
16744#endif
16745}
16746
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016747#ifdef FEAT_FLOAT
16748/*
16749 * "reltimefloat()" function
16750 */
16751 static void
16752f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16753{
16754# ifdef FEAT_RELTIME
16755 proftime_T tm;
16756# endif
16757
16758 rettv->v_type = VAR_FLOAT;
16759 rettv->vval.v_float = 0;
16760# ifdef FEAT_RELTIME
16761 if (list2proftime(&argvars[0], &tm) == OK)
16762 rettv->vval.v_float = profile_float(&tm);
16763# endif
16764}
16765#endif
16766
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016767/*
16768 * "reltimestr()" function
16769 */
16770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016771f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016772{
16773#ifdef FEAT_RELTIME
16774 proftime_T tm;
16775#endif
16776
16777 rettv->v_type = VAR_STRING;
16778 rettv->vval.v_string = NULL;
16779#ifdef FEAT_RELTIME
16780 if (list2proftime(&argvars[0], &tm) == OK)
16781 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16782#endif
16783}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016784
Bram Moolenaar0d660222005-01-07 21:51:51 +000016785#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016786static void make_connection(void);
16787static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016788
16789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016790make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016791{
16792 if (X_DISPLAY == NULL
16793# ifdef FEAT_GUI
16794 && !gui.in_use
16795# endif
16796 )
16797 {
16798 x_force_connect = TRUE;
16799 setup_term_clip();
16800 x_force_connect = FALSE;
16801 }
16802}
16803
16804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016805check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016806{
16807 make_connection();
16808 if (X_DISPLAY == NULL)
16809 {
16810 EMSG(_("E240: No connection to Vim server"));
16811 return FAIL;
16812 }
16813 return OK;
16814}
16815#endif
16816
16817#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016818static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016819
16820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016821remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016822{
16823 char_u *server_name;
16824 char_u *keys;
16825 char_u *r = NULL;
16826 char_u buf[NUMBUFLEN];
16827# ifdef WIN32
16828 HWND w;
16829# else
16830 Window w;
16831# endif
16832
16833 if (check_restricted() || check_secure())
16834 return;
16835
16836# ifdef FEAT_X11
16837 if (check_connection() == FAIL)
16838 return;
16839# endif
16840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016841 server_name = get_tv_string_chk(&argvars[0]);
16842 if (server_name == NULL)
16843 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016844 keys = get_tv_string_buf(&argvars[1], buf);
16845# ifdef WIN32
16846 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16847# else
16848 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16849 < 0)
16850# endif
16851 {
16852 if (r != NULL)
16853 EMSG(r); /* sending worked but evaluation failed */
16854 else
16855 EMSG2(_("E241: Unable to send to %s"), server_name);
16856 return;
16857 }
16858
16859 rettv->vval.v_string = r;
16860
16861 if (argvars[2].v_type != VAR_UNKNOWN)
16862 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016864 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016865 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016866
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016867 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016868 v.di_tv.v_type = VAR_STRING;
16869 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016870 idvar = get_tv_string_chk(&argvars[2]);
16871 if (idvar != NULL)
16872 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016873 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874 }
16875}
16876#endif
16877
16878/*
16879 * "remote_expr()" function
16880 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016882f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883{
16884 rettv->v_type = VAR_STRING;
16885 rettv->vval.v_string = NULL;
16886#ifdef FEAT_CLIENTSERVER
16887 remote_common(argvars, rettv, TRUE);
16888#endif
16889}
16890
16891/*
16892 * "remote_foreground()" function
16893 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016895f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016896{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897#ifdef FEAT_CLIENTSERVER
16898# ifdef WIN32
16899 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016900 {
16901 char_u *server_name = get_tv_string_chk(&argvars[0]);
16902
16903 if (server_name != NULL)
16904 serverForeground(server_name);
16905 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906# else
16907 /* Send a foreground() expression to the server. */
16908 argvars[1].v_type = VAR_STRING;
16909 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16910 argvars[2].v_type = VAR_UNKNOWN;
16911 remote_common(argvars, rettv, TRUE);
16912 vim_free(argvars[1].vval.v_string);
16913# endif
16914#endif
16915}
16916
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016918f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016919{
16920#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016921 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922 char_u *s = NULL;
16923# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016924 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016926 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927
16928 if (check_restricted() || check_secure())
16929 {
16930 rettv->vval.v_number = -1;
16931 return;
16932 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016933 serverid = get_tv_string_chk(&argvars[0]);
16934 if (serverid == NULL)
16935 {
16936 rettv->vval.v_number = -1;
16937 return; /* type error; errmsg already given */
16938 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016940 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941 if (n == 0)
16942 rettv->vval.v_number = -1;
16943 else
16944 {
16945 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16946 rettv->vval.v_number = (s != NULL);
16947 }
16948# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 if (check_connection() == FAIL)
16950 return;
16951
16952 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016953 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954# endif
16955
16956 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016958 char_u *retvar;
16959
Bram Moolenaar33570922005-01-25 22:26:29 +000016960 v.di_tv.v_type = VAR_STRING;
16961 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016962 retvar = get_tv_string_chk(&argvars[1]);
16963 if (retvar != NULL)
16964 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016965 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 }
16967#else
16968 rettv->vval.v_number = -1;
16969#endif
16970}
16971
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016973f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974{
16975 char_u *r = NULL;
16976
16977#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016978 char_u *serverid = get_tv_string_chk(&argvars[0]);
16979
16980 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981 {
16982# ifdef WIN32
16983 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016984 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016986 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987 if (n != 0)
16988 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16989 if (r == NULL)
16990# else
16991 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016992 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993# endif
16994 EMSG(_("E277: Unable to read a server reply"));
16995 }
16996#endif
16997 rettv->v_type = VAR_STRING;
16998 rettv->vval.v_string = r;
16999}
17000
17001/*
17002 * "remote_send()" function
17003 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017005f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006{
17007 rettv->v_type = VAR_STRING;
17008 rettv->vval.v_string = NULL;
17009#ifdef FEAT_CLIENTSERVER
17010 remote_common(argvars, rettv, FALSE);
17011#endif
17012}
17013
17014/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017015 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017016 */
17017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017018f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017019{
Bram Moolenaar33570922005-01-25 22:26:29 +000017020 list_T *l;
17021 listitem_T *item, *item2;
17022 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017023 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017024 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017025 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017026 dict_T *d;
17027 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017028 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017029
Bram Moolenaar8c711452005-01-14 21:53:12 +000017030 if (argvars[0].v_type == VAR_DICT)
17031 {
17032 if (argvars[2].v_type != VAR_UNKNOWN)
17033 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017034 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017035 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017036 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017037 key = get_tv_string_chk(&argvars[1]);
17038 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017040 di = dict_find(d, key, -1);
17041 if (di == NULL)
17042 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017043 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17044 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045 {
17046 *rettv = di->di_tv;
17047 init_tv(&di->di_tv);
17048 dictitem_remove(d, di);
17049 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017050 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017051 }
17052 }
17053 else if (argvars[0].v_type != VAR_LIST)
17054 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017055 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017056 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017057 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017058 int error = FALSE;
17059
17060 idx = get_tv_number_chk(&argvars[1], &error);
17061 if (error)
17062 ; /* type error: do nothing, errmsg already given */
17063 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017064 EMSGN(_(e_listidx), idx);
17065 else
17066 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017067 if (argvars[2].v_type == VAR_UNKNOWN)
17068 {
17069 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017070 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017071 *rettv = item->li_tv;
17072 vim_free(item);
17073 }
17074 else
17075 {
17076 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017077 end = get_tv_number_chk(&argvars[2], &error);
17078 if (error)
17079 ; /* type error: do nothing */
17080 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017081 EMSGN(_(e_listidx), end);
17082 else
17083 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017084 int cnt = 0;
17085
17086 for (li = item; li != NULL; li = li->li_next)
17087 {
17088 ++cnt;
17089 if (li == item2)
17090 break;
17091 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017092 if (li == NULL) /* didn't find "item2" after "item" */
17093 EMSG(_(e_invrange));
17094 else
17095 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017096 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017097 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017098 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017099 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017100 l->lv_first = item;
17101 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017102 item->li_prev = NULL;
17103 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017104 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017105 }
17106 }
17107 }
17108 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017109 }
17110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111}
17112
17113/*
17114 * "rename({from}, {to})" function
17115 */
17116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017117f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118{
17119 char_u buf[NUMBUFLEN];
17120
17121 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017122 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017124 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17125 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126}
17127
17128/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017129 * "repeat()" function
17130 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017132f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017133{
17134 char_u *p;
17135 int n;
17136 int slen;
17137 int len;
17138 char_u *r;
17139 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017140
17141 n = get_tv_number(&argvars[1]);
17142 if (argvars[0].v_type == VAR_LIST)
17143 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017144 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017145 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017146 if (list_extend(rettv->vval.v_list,
17147 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017148 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017149 }
17150 else
17151 {
17152 p = get_tv_string(&argvars[0]);
17153 rettv->v_type = VAR_STRING;
17154 rettv->vval.v_string = NULL;
17155
17156 slen = (int)STRLEN(p);
17157 len = slen * n;
17158 if (len <= 0)
17159 return;
17160
17161 r = alloc(len + 1);
17162 if (r != NULL)
17163 {
17164 for (i = 0; i < n; i++)
17165 mch_memmove(r + i * slen, p, (size_t)slen);
17166 r[len] = NUL;
17167 }
17168
17169 rettv->vval.v_string = r;
17170 }
17171}
17172
17173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 * "resolve()" function
17175 */
17176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017177f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178{
17179 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017180#ifdef HAVE_READLINK
17181 char_u *buf = NULL;
17182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017184 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185#ifdef FEAT_SHORTCUT
17186 {
17187 char_u *v = NULL;
17188
17189 v = mch_resolve_shortcut(p);
17190 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017191 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017193 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 }
17195#else
17196# ifdef HAVE_READLINK
17197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 char_u *cpy;
17199 int len;
17200 char_u *remain = NULL;
17201 char_u *q;
17202 int is_relative_to_current = FALSE;
17203 int has_trailing_pathsep = FALSE;
17204 int limit = 100;
17205
17206 p = vim_strsave(p);
17207
17208 if (p[0] == '.' && (vim_ispathsep(p[1])
17209 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17210 is_relative_to_current = TRUE;
17211
17212 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017213 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017216 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218
17219 q = getnextcomp(p);
17220 if (*q != NUL)
17221 {
17222 /* Separate the first path component in "p", and keep the
17223 * remainder (beginning with the path separator). */
17224 remain = vim_strsave(q - 1);
17225 q[-1] = NUL;
17226 }
17227
Bram Moolenaard9462e32011-04-11 21:35:11 +020017228 buf = alloc(MAXPATHL + 1);
17229 if (buf == NULL)
17230 goto fail;
17231
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 for (;;)
17233 {
17234 for (;;)
17235 {
17236 len = readlink((char *)p, (char *)buf, MAXPATHL);
17237 if (len <= 0)
17238 break;
17239 buf[len] = NUL;
17240
17241 if (limit-- == 0)
17242 {
17243 vim_free(p);
17244 vim_free(remain);
17245 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017246 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247 goto fail;
17248 }
17249
17250 /* Ensure that the result will have a trailing path separator
17251 * if the argument has one. */
17252 if (remain == NULL && has_trailing_pathsep)
17253 add_pathsep(buf);
17254
17255 /* Separate the first path component in the link value and
17256 * concatenate the remainders. */
17257 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17258 if (*q != NUL)
17259 {
17260 if (remain == NULL)
17261 remain = vim_strsave(q - 1);
17262 else
17263 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017264 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265 if (cpy != NULL)
17266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 vim_free(remain);
17268 remain = cpy;
17269 }
17270 }
17271 q[-1] = NUL;
17272 }
17273
17274 q = gettail(p);
17275 if (q > p && *q == NUL)
17276 {
17277 /* Ignore trailing path separator. */
17278 q[-1] = NUL;
17279 q = gettail(p);
17280 }
17281 if (q > p && !mch_isFullName(buf))
17282 {
17283 /* symlink is relative to directory of argument */
17284 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17285 if (cpy != NULL)
17286 {
17287 STRCPY(cpy, p);
17288 STRCPY(gettail(cpy), buf);
17289 vim_free(p);
17290 p = cpy;
17291 }
17292 }
17293 else
17294 {
17295 vim_free(p);
17296 p = vim_strsave(buf);
17297 }
17298 }
17299
17300 if (remain == NULL)
17301 break;
17302
17303 /* Append the first path component of "remain" to "p". */
17304 q = getnextcomp(remain + 1);
17305 len = q - remain - (*q != NUL);
17306 cpy = vim_strnsave(p, STRLEN(p) + len);
17307 if (cpy != NULL)
17308 {
17309 STRNCAT(cpy, remain, len);
17310 vim_free(p);
17311 p = cpy;
17312 }
17313 /* Shorten "remain". */
17314 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017315 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 else
17317 {
17318 vim_free(remain);
17319 remain = NULL;
17320 }
17321 }
17322
17323 /* If the result is a relative path name, make it explicitly relative to
17324 * the current directory if and only if the argument had this form. */
17325 if (!vim_ispathsep(*p))
17326 {
17327 if (is_relative_to_current
17328 && *p != NUL
17329 && !(p[0] == '.'
17330 && (p[1] == NUL
17331 || vim_ispathsep(p[1])
17332 || (p[1] == '.'
17333 && (p[2] == NUL
17334 || vim_ispathsep(p[2]))))))
17335 {
17336 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017337 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 if (cpy != NULL)
17339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340 vim_free(p);
17341 p = cpy;
17342 }
17343 }
17344 else if (!is_relative_to_current)
17345 {
17346 /* Strip leading "./". */
17347 q = p;
17348 while (q[0] == '.' && vim_ispathsep(q[1]))
17349 q += 2;
17350 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017351 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 }
17353 }
17354
17355 /* Ensure that the result will have no trailing path separator
17356 * if the argument had none. But keep "/" or "//". */
17357 if (!has_trailing_pathsep)
17358 {
17359 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017360 if (after_pathsep(p, q))
17361 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 }
17363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017364 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 }
17366# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017367 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368# endif
17369#endif
17370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017371 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372
17373#ifdef HAVE_READLINK
17374fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017375 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017377 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378}
17379
17380/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017381 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 */
17383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017384f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385{
Bram Moolenaar33570922005-01-25 22:26:29 +000017386 list_T *l;
17387 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388
Bram Moolenaar0d660222005-01-07 21:51:51 +000017389 if (argvars[0].v_type != VAR_LIST)
17390 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017391 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017392 && !tv_check_lock(l->lv_lock,
17393 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017394 {
17395 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017396 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017397 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017398 while (li != NULL)
17399 {
17400 ni = li->li_prev;
17401 list_append(l, li);
17402 li = ni;
17403 }
17404 rettv->vval.v_list = l;
17405 rettv->v_type = VAR_LIST;
17406 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017407 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409}
17410
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017411#define SP_NOMOVE 0x01 /* don't move cursor */
17412#define SP_REPEAT 0x02 /* repeat to find outer pair */
17413#define SP_RETCOUNT 0x04 /* return matchcount */
17414#define SP_SETPCMARK 0x08 /* set previous context mark */
17415#define SP_START 0x10 /* accept match at start position */
17416#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17417#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017418#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017419
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017420static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017421
17422/*
17423 * Get flags for a search function.
17424 * Possibly sets "p_ws".
17425 * Returns BACKWARD, FORWARD or zero (for an error).
17426 */
17427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017428get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017429{
17430 int dir = FORWARD;
17431 char_u *flags;
17432 char_u nbuf[NUMBUFLEN];
17433 int mask;
17434
17435 if (varp->v_type != VAR_UNKNOWN)
17436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017437 flags = get_tv_string_buf_chk(varp, nbuf);
17438 if (flags == NULL)
17439 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440 while (*flags != NUL)
17441 {
17442 switch (*flags)
17443 {
17444 case 'b': dir = BACKWARD; break;
17445 case 'w': p_ws = TRUE; break;
17446 case 'W': p_ws = FALSE; break;
17447 default: mask = 0;
17448 if (flagsp != NULL)
17449 switch (*flags)
17450 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017451 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017452 case 'e': mask = SP_END; break;
17453 case 'm': mask = SP_RETCOUNT; break;
17454 case 'n': mask = SP_NOMOVE; break;
17455 case 'p': mask = SP_SUBPAT; break;
17456 case 'r': mask = SP_REPEAT; break;
17457 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017458 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017459 }
17460 if (mask == 0)
17461 {
17462 EMSG2(_(e_invarg2), flags);
17463 dir = 0;
17464 }
17465 else
17466 *flagsp |= mask;
17467 }
17468 if (dir == 0)
17469 break;
17470 ++flags;
17471 }
17472 }
17473 return dir;
17474}
17475
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017477 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017479 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017480search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017482 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 char_u *pat;
17484 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017485 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 int save_p_ws = p_ws;
17487 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017488 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017489 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017490 proftime_T tm;
17491#ifdef FEAT_RELTIME
17492 long time_limit = 0;
17493#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017494 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017495 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017497 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017498 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017499 if (dir == 0)
17500 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017501 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017502 if (flags & SP_START)
17503 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017504 if (flags & SP_END)
17505 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017506 if (flags & SP_COLUMN)
17507 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017508
Bram Moolenaar76929292008-01-06 19:07:36 +000017509 /* Optional arguments: line number to stop searching and timeout. */
17510 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017511 {
17512 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17513 if (lnum_stop < 0)
17514 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017515#ifdef FEAT_RELTIME
17516 if (argvars[3].v_type != VAR_UNKNOWN)
17517 {
17518 time_limit = get_tv_number_chk(&argvars[3], NULL);
17519 if (time_limit < 0)
17520 goto theend;
17521 }
17522#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017523 }
17524
Bram Moolenaar76929292008-01-06 19:07:36 +000017525#ifdef FEAT_RELTIME
17526 /* Set the time limit, if there is one. */
17527 profile_setlimit(time_limit, &tm);
17528#endif
17529
Bram Moolenaar231334e2005-07-25 20:46:57 +000017530 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017531 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017532 * Check to make sure only those flags are set.
17533 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17534 * flags cannot be set. Check for that condition also.
17535 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017536 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017537 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017539 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017540 goto theend;
17541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017543 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017544 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017545 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017546 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017548 if (flags & SP_SUBPAT)
17549 retval = subpatnum;
17550 else
17551 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017552 if (flags & SP_SETPCMARK)
17553 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017555 if (match_pos != NULL)
17556 {
17557 /* Store the match cursor position */
17558 match_pos->lnum = pos.lnum;
17559 match_pos->col = pos.col + 1;
17560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 /* "/$" will put the cursor after the end of the line, may need to
17562 * correct that here */
17563 check_cursor();
17564 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017565
17566 /* If 'n' flag is used: restore cursor position. */
17567 if (flags & SP_NOMOVE)
17568 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017569 else
17570 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017571theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017573
17574 return retval;
17575}
17576
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017577#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017578
17579/*
17580 * round() is not in C90, use ceil() or floor() instead.
17581 */
17582 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017583vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017584{
17585 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17586}
17587
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017588/*
17589 * "round({float})" function
17590 */
17591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017592f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017593{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017594 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017595
17596 rettv->v_type = VAR_FLOAT;
17597 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017598 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017599 else
17600 rettv->vval.v_float = 0.0;
17601}
17602#endif
17603
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017604/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017605 * "screenattr()" function
17606 */
17607 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017608f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017609{
17610 int row;
17611 int col;
17612 int c;
17613
17614 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17615 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17616 if (row < 0 || row >= screen_Rows
17617 || col < 0 || col >= screen_Columns)
17618 c = -1;
17619 else
17620 c = ScreenAttrs[LineOffset[row] + col];
17621 rettv->vval.v_number = c;
17622}
17623
17624/*
17625 * "screenchar()" function
17626 */
17627 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017628f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017629{
17630 int row;
17631 int col;
17632 int off;
17633 int c;
17634
17635 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17636 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17637 if (row < 0 || row >= screen_Rows
17638 || col < 0 || col >= screen_Columns)
17639 c = -1;
17640 else
17641 {
17642 off = LineOffset[row] + col;
17643#ifdef FEAT_MBYTE
17644 if (enc_utf8 && ScreenLinesUC[off] != 0)
17645 c = ScreenLinesUC[off];
17646 else
17647#endif
17648 c = ScreenLines[off];
17649 }
17650 rettv->vval.v_number = c;
17651}
17652
17653/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017654 * "screencol()" function
17655 *
17656 * First column is 1 to be consistent with virtcol().
17657 */
17658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017659f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017660{
17661 rettv->vval.v_number = screen_screencol() + 1;
17662}
17663
17664/*
17665 * "screenrow()" function
17666 */
17667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017668f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017669{
17670 rettv->vval.v_number = screen_screenrow() + 1;
17671}
17672
17673/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017674 * "search()" function
17675 */
17676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017677f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017678{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017679 int flags = 0;
17680
17681 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682}
17683
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017685 * "searchdecl()" function
17686 */
17687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017688f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017689{
17690 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017691 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017692 int error = FALSE;
17693 char_u *name;
17694
17695 rettv->vval.v_number = 1; /* default: FAIL */
17696
17697 name = get_tv_string_chk(&argvars[0]);
17698 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017699 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017700 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017701 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17702 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17703 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017704 if (!error && name != NULL)
17705 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017706 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017707}
17708
17709/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017710 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017713searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714{
17715 char_u *spat, *mpat, *epat;
17716 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 int dir;
17719 int flags = 0;
17720 char_u nbuf1[NUMBUFLEN];
17721 char_u nbuf2[NUMBUFLEN];
17722 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017723 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017724 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017725 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017728 spat = get_tv_string_chk(&argvars[0]);
17729 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17730 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17731 if (spat == NULL || mpat == NULL || epat == NULL)
17732 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 /* Handle the optional fourth argument: flags */
17735 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017736 if (dir == 0)
17737 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017738
17739 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017740 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17741 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017742 if ((flags & (SP_END | SP_SUBPAT)) != 0
17743 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017744 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017745 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017746 goto theend;
17747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017749 /* Using 'r' implies 'W', otherwise it doesn't work. */
17750 if (flags & SP_REPEAT)
17751 p_ws = FALSE;
17752
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017753 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017754 if (argvars[3].v_type == VAR_UNKNOWN
17755 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 skip = (char_u *)"";
17757 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017759 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017760 if (argvars[5].v_type != VAR_UNKNOWN)
17761 {
17762 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17763 if (lnum_stop < 0)
17764 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017765#ifdef FEAT_RELTIME
17766 if (argvars[6].v_type != VAR_UNKNOWN)
17767 {
17768 time_limit = get_tv_number_chk(&argvars[6], NULL);
17769 if (time_limit < 0)
17770 goto theend;
17771 }
17772#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017773 }
17774 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017775 if (skip == NULL)
17776 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017778 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017779 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017780
17781theend:
17782 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017783
17784 return retval;
17785}
17786
17787/*
17788 * "searchpair()" function
17789 */
17790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017791f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017792{
17793 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17794}
17795
17796/*
17797 * "searchpairpos()" function
17798 */
17799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017800f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017801{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017802 pos_T match_pos;
17803 int lnum = 0;
17804 int col = 0;
17805
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017806 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017807 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017808
17809 if (searchpair_cmn(argvars, &match_pos) > 0)
17810 {
17811 lnum = match_pos.lnum;
17812 col = match_pos.col;
17813 }
17814
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017815 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17816 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017817}
17818
17819/*
17820 * Search for a start/middle/end thing.
17821 * Used by searchpair(), see its documentation for the details.
17822 * Returns 0 or -1 for no match,
17823 */
17824 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017825do_searchpair(
17826 char_u *spat, /* start pattern */
17827 char_u *mpat, /* middle pattern */
17828 char_u *epat, /* end pattern */
17829 int dir, /* BACKWARD or FORWARD */
17830 char_u *skip, /* skip expression */
17831 int flags, /* SP_SETPCMARK and other SP_ values */
17832 pos_T *match_pos,
17833 linenr_T lnum_stop, /* stop at this line if not zero */
17834 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017835{
17836 char_u *save_cpo;
17837 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17838 long retval = 0;
17839 pos_T pos;
17840 pos_T firstpos;
17841 pos_T foundpos;
17842 pos_T save_cursor;
17843 pos_T save_pos;
17844 int n;
17845 int r;
17846 int nest = 1;
17847 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017848 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017849 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017850
17851 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17852 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017853 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017854
Bram Moolenaar76929292008-01-06 19:07:36 +000017855#ifdef FEAT_RELTIME
17856 /* Set the time limit, if there is one. */
17857 profile_setlimit(time_limit, &tm);
17858#endif
17859
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017860 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17861 * start/middle/end (pat3, for the top pair). */
17862 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17863 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17864 if (pat2 == NULL || pat3 == NULL)
17865 goto theend;
17866 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17867 if (*mpat == NUL)
17868 STRCPY(pat3, pat2);
17869 else
17870 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17871 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017872 if (flags & SP_START)
17873 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017874
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 save_cursor = curwin->w_cursor;
17876 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017877 clearpos(&firstpos);
17878 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 pat = pat3;
17880 for (;;)
17881 {
17882 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017883 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17885 /* didn't find it or found the first match again: FAIL */
17886 break;
17887
17888 if (firstpos.lnum == 0)
17889 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017890 if (equalpos(pos, foundpos))
17891 {
17892 /* Found the same position again. Can happen with a pattern that
17893 * has "\zs" at the end and searching backwards. Advance one
17894 * character and try again. */
17895 if (dir == BACKWARD)
17896 decl(&pos);
17897 else
17898 incl(&pos);
17899 }
17900 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017902 /* clear the start flag to avoid getting stuck here */
17903 options &= ~SEARCH_START;
17904
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 /* If the skip pattern matches, ignore this match. */
17906 if (*skip != NUL)
17907 {
17908 save_pos = curwin->w_cursor;
17909 curwin->w_cursor = pos;
17910 r = eval_to_bool(skip, &err, NULL, FALSE);
17911 curwin->w_cursor = save_pos;
17912 if (err)
17913 {
17914 /* Evaluating {skip} caused an error, break here. */
17915 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017916 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 break;
17918 }
17919 if (r)
17920 continue;
17921 }
17922
17923 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17924 {
17925 /* Found end when searching backwards or start when searching
17926 * forward: nested pair. */
17927 ++nest;
17928 pat = pat2; /* nested, don't search for middle */
17929 }
17930 else
17931 {
17932 /* Found end when searching forward or start when searching
17933 * backward: end of (nested) pair; or found middle in outer pair. */
17934 if (--nest == 1)
17935 pat = pat3; /* outer level, search for middle */
17936 }
17937
17938 if (nest == 0)
17939 {
17940 /* Found the match: return matchcount or line number. */
17941 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017942 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017944 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017945 if (flags & SP_SETPCMARK)
17946 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 curwin->w_cursor = pos;
17948 if (!(flags & SP_REPEAT))
17949 break;
17950 nest = 1; /* search for next unmatched */
17951 }
17952 }
17953
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017954 if (match_pos != NULL)
17955 {
17956 /* Store the match cursor position */
17957 match_pos->lnum = curwin->w_cursor.lnum;
17958 match_pos->col = curwin->w_cursor.col + 1;
17959 }
17960
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017962 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963 curwin->w_cursor = save_cursor;
17964
17965theend:
17966 vim_free(pat2);
17967 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017968 if (p_cpo == empty_option)
17969 p_cpo = save_cpo;
17970 else
17971 /* Darn, evaluating the {skip} expression changed the value. */
17972 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017973
17974 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975}
17976
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017977/*
17978 * "searchpos()" function
17979 */
17980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017981f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017982{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983 pos_T match_pos;
17984 int lnum = 0;
17985 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017986 int n;
17987 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017988
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017989 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017990 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017991
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017992 n = search_cmn(argvars, &match_pos, &flags);
17993 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017994 {
17995 lnum = match_pos.lnum;
17996 col = match_pos.col;
17997 }
17998
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017999 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18000 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018001 if (flags & SP_SUBPAT)
18002 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018003}
18004
Bram Moolenaar0d660222005-01-07 21:51:51 +000018005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018006f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018008#ifdef FEAT_CLIENTSERVER
18009 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018010 char_u *server = get_tv_string_chk(&argvars[0]);
18011 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012
Bram Moolenaar0d660222005-01-07 21:51:51 +000018013 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018014 if (server == NULL || reply == NULL)
18015 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018016 if (check_restricted() || check_secure())
18017 return;
18018# ifdef FEAT_X11
18019 if (check_connection() == FAIL)
18020 return;
18021# endif
18022
18023 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018025 EMSG(_("E258: Unable to send to client"));
18026 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018028 rettv->vval.v_number = 0;
18029#else
18030 rettv->vval.v_number = -1;
18031#endif
18032}
18033
Bram Moolenaar0d660222005-01-07 21:51:51 +000018034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018035f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018036{
18037 char_u *r = NULL;
18038
18039#ifdef FEAT_CLIENTSERVER
18040# ifdef WIN32
18041 r = serverGetVimNames();
18042# else
18043 make_connection();
18044 if (X_DISPLAY != NULL)
18045 r = serverGetVimNames(X_DISPLAY);
18046# endif
18047#endif
18048 rettv->v_type = VAR_STRING;
18049 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050}
18051
18052/*
18053 * "setbufvar()" function
18054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018056f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057{
18058 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018061 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 char_u nbuf[NUMBUFLEN];
18063
18064 if (check_restricted() || check_secure())
18065 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018066 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18067 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018068 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 varp = &argvars[2];
18070
18071 if (buf != NULL && varname != NULL && varp != NULL)
18072 {
18073 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075
18076 if (*varname == '&')
18077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018078 long numval;
18079 char_u *strval;
18080 int error = FALSE;
18081
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 numval = get_tv_number_chk(varp, &error);
18084 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018085 if (!error && strval != NULL)
18086 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 }
18088 else
18089 {
18090 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18091 if (bufvarname != NULL)
18092 {
18093 STRCPY(bufvarname, "b:");
18094 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018095 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 vim_free(bufvarname);
18097 }
18098 }
18099
18100 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103}
18104
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018106f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018107{
18108 dict_T *d;
18109 dictitem_T *di;
18110 char_u *csearch;
18111
18112 if (argvars[0].v_type != VAR_DICT)
18113 {
18114 EMSG(_(e_dictreq));
18115 return;
18116 }
18117
18118 if ((d = argvars[0].vval.v_dict) != NULL)
18119 {
18120 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18121 if (csearch != NULL)
18122 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018123#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018124 if (enc_utf8)
18125 {
18126 int pcc[MAX_MCO];
18127 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018128
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018129 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18130 }
18131 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018132#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018133 set_last_csearch(PTR2CHAR(csearch),
18134 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018135 }
18136
18137 di = dict_find(d, (char_u *)"forward", -1);
18138 if (di != NULL)
18139 set_csearch_direction(get_tv_number(&di->di_tv)
18140 ? FORWARD : BACKWARD);
18141
18142 di = dict_find(d, (char_u *)"until", -1);
18143 if (di != NULL)
18144 set_csearch_until(!!get_tv_number(&di->di_tv));
18145 }
18146}
18147
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148/*
18149 * "setcmdpos()" function
18150 */
18151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018152f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018154 int pos = (int)get_tv_number(&argvars[0]) - 1;
18155
18156 if (pos >= 0)
18157 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158}
18159
18160/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018161 * "setfperm({fname}, {mode})" function
18162 */
18163 static void
18164f_setfperm(typval_T *argvars, typval_T *rettv)
18165{
18166 char_u *fname;
18167 char_u modebuf[NUMBUFLEN];
18168 char_u *mode_str;
18169 int i;
18170 int mask;
18171 int mode = 0;
18172
18173 rettv->vval.v_number = 0;
18174 fname = get_tv_string_chk(&argvars[0]);
18175 if (fname == NULL)
18176 return;
18177 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18178 if (mode_str == NULL)
18179 return;
18180 if (STRLEN(mode_str) != 9)
18181 {
18182 EMSG2(_(e_invarg2), mode_str);
18183 return;
18184 }
18185
18186 mask = 1;
18187 for (i = 8; i >= 0; --i)
18188 {
18189 if (mode_str[i] != '-')
18190 mode |= mask;
18191 mask = mask << 1;
18192 }
18193 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18194}
18195
18196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 * "setline()" function
18198 */
18199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018200f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201{
18202 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018203 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018204 list_T *l = NULL;
18205 listitem_T *li = NULL;
18206 long added = 0;
18207 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018209 lnum = get_tv_lnum(&argvars[0]);
18210 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018212 l = argvars[1].vval.v_list;
18213 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018215 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018216 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018217
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018218 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018219 for (;;)
18220 {
18221 if (l != NULL)
18222 {
18223 /* list argument, get next string */
18224 if (li == NULL)
18225 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018226 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018227 li = li->li_next;
18228 }
18229
18230 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018231 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018232 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018233
18234 /* When coming here from Insert mode, sync undo, so that this can be
18235 * undone separately from what was previously inserted. */
18236 if (u_sync_once == 2)
18237 {
18238 u_sync_once = 1; /* notify that u_sync() was called */
18239 u_sync(TRUE);
18240 }
18241
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018242 if (lnum <= curbuf->b_ml.ml_line_count)
18243 {
18244 /* existing line, replace it */
18245 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18246 {
18247 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018248 if (lnum == curwin->w_cursor.lnum)
18249 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018250 rettv->vval.v_number = 0; /* OK */
18251 }
18252 }
18253 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18254 {
18255 /* lnum is one past the last line, append the line */
18256 ++added;
18257 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18258 rettv->vval.v_number = 0; /* OK */
18259 }
18260
18261 if (l == NULL) /* only one string argument */
18262 break;
18263 ++lnum;
18264 }
18265
18266 if (added > 0)
18267 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268}
18269
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018270static 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 +000018271
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018273 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018274 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018276set_qf_ll_list(
18277 win_T *wp UNUSED,
18278 typval_T *list_arg UNUSED,
18279 typval_T *action_arg UNUSED,
18280 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018281{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018282#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018283 char_u *act;
18284 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018285#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018286
Bram Moolenaar2641f772005-03-25 21:58:17 +000018287 rettv->vval.v_number = -1;
18288
18289#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018290 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018291 EMSG(_(e_listreq));
18292 else
18293 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018294 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018295
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018296 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018297 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018298 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018299 if (act == NULL)
18300 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018301 if (*act == 'a' || *act == 'r')
18302 action = *act;
18303 }
18304
Bram Moolenaar81484f42012-12-05 15:16:47 +010018305 if (l != NULL && set_errorlist(wp, l, action,
18306 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018307 rettv->vval.v_number = 0;
18308 }
18309#endif
18310}
18311
18312/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018313 * "setloclist()" function
18314 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018316f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018317{
18318 win_T *win;
18319
18320 rettv->vval.v_number = -1;
18321
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018322 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018323 if (win != NULL)
18324 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18325}
18326
18327/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018328 * "setmatches()" function
18329 */
18330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018331f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018332{
18333#ifdef FEAT_SEARCH_EXTRA
18334 list_T *l;
18335 listitem_T *li;
18336 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018337 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018338
18339 rettv->vval.v_number = -1;
18340 if (argvars[0].v_type != VAR_LIST)
18341 {
18342 EMSG(_(e_listreq));
18343 return;
18344 }
18345 if ((l = argvars[0].vval.v_list) != NULL)
18346 {
18347
18348 /* To some extent make sure that we are dealing with a list from
18349 * "getmatches()". */
18350 li = l->lv_first;
18351 while (li != NULL)
18352 {
18353 if (li->li_tv.v_type != VAR_DICT
18354 || (d = li->li_tv.vval.v_dict) == NULL)
18355 {
18356 EMSG(_(e_invarg));
18357 return;
18358 }
18359 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018360 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18361 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018362 && dict_find(d, (char_u *)"priority", -1) != NULL
18363 && dict_find(d, (char_u *)"id", -1) != NULL))
18364 {
18365 EMSG(_(e_invarg));
18366 return;
18367 }
18368 li = li->li_next;
18369 }
18370
18371 clear_matches(curwin);
18372 li = l->lv_first;
18373 while (li != NULL)
18374 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018375 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018376 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018377 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018378 char_u *group;
18379 int priority;
18380 int id;
18381 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018382
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018383 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018384 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18385 {
18386 if (s == NULL)
18387 {
18388 s = list_alloc();
18389 if (s == NULL)
18390 return;
18391 }
18392
18393 /* match from matchaddpos() */
18394 for (i = 1; i < 9; i++)
18395 {
18396 sprintf((char *)buf, (char *)"pos%d", i);
18397 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18398 {
18399 if (di->di_tv.v_type != VAR_LIST)
18400 return;
18401
18402 list_append_tv(s, &di->di_tv);
18403 s->lv_refcount++;
18404 }
18405 else
18406 break;
18407 }
18408 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018409
18410 group = get_dict_string(d, (char_u *)"group", FALSE);
18411 priority = (int)get_dict_number(d, (char_u *)"priority");
18412 id = (int)get_dict_number(d, (char_u *)"id");
18413 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18414 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18415 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018416 if (i == 0)
18417 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018418 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018419 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018420 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018421 }
18422 else
18423 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018424 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018425 list_unref(s);
18426 s = NULL;
18427 }
18428
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018429 li = li->li_next;
18430 }
18431 rettv->vval.v_number = 0;
18432 }
18433#endif
18434}
18435
18436/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018437 * "setpos()" function
18438 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018440f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018441{
18442 pos_T pos;
18443 int fnum;
18444 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018445 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018446
Bram Moolenaar08250432008-02-13 11:42:46 +000018447 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018448 name = get_tv_string_chk(argvars);
18449 if (name != NULL)
18450 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018451 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018452 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018453 if (--pos.col < 0)
18454 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018455 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018456 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018457 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018458 if (fnum == curbuf->b_fnum)
18459 {
18460 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018461 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018462 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018463 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018464 curwin->w_set_curswant = FALSE;
18465 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018466 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018467 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018468 }
18469 else
18470 EMSG(_(e_invarg));
18471 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018472 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18473 {
18474 /* set mark */
18475 if (setmark_pos(name[1], &pos, fnum) == OK)
18476 rettv->vval.v_number = 0;
18477 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018478 else
18479 EMSG(_(e_invarg));
18480 }
18481 }
18482}
18483
18484/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018485 * "setqflist()" function
18486 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018488f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018489{
18490 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18491}
18492
18493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 * "setreg()" function
18495 */
18496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018497f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498{
18499 int regname;
18500 char_u *strregname;
18501 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018502 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 int append;
18504 char_u yank_type;
18505 long block_len;
18506
18507 block_len = -1;
18508 yank_type = MAUTO;
18509 append = FALSE;
18510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018511 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018512 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018514 if (strregname == NULL)
18515 return; /* type error; errmsg already given */
18516 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 if (regname == 0 || regname == '@')
18518 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018520 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018522 stropt = get_tv_string_chk(&argvars[2]);
18523 if (stropt == NULL)
18524 return; /* type error */
18525 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 switch (*stropt)
18527 {
18528 case 'a': case 'A': /* append */
18529 append = TRUE;
18530 break;
18531 case 'v': case 'c': /* character-wise selection */
18532 yank_type = MCHAR;
18533 break;
18534 case 'V': case 'l': /* line-wise selection */
18535 yank_type = MLINE;
18536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 case 'b': case Ctrl_V: /* block-wise selection */
18538 yank_type = MBLOCK;
18539 if (VIM_ISDIGIT(stropt[1]))
18540 {
18541 ++stropt;
18542 block_len = getdigits(&stropt) - 1;
18543 --stropt;
18544 }
18545 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 }
18547 }
18548
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018549 if (argvars[1].v_type == VAR_LIST)
18550 {
18551 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018552 char_u **allocval;
18553 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018554 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018555 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018556 int len = argvars[1].vval.v_list->lv_len;
18557 listitem_T *li;
18558
Bram Moolenaar7d647822014-04-05 21:28:56 +020018559 /* First half: use for pointers to result lines; second half: use for
18560 * pointers to allocated copies. */
18561 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018562 if (lstval == NULL)
18563 return;
18564 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018565 allocval = lstval + len + 2;
18566 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018567
18568 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18569 li = li->li_next)
18570 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018571 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018572 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018573 goto free_lstval;
18574 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018575 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018576 /* Need to make a copy, next get_tv_string_buf_chk() will
18577 * overwrite the string. */
18578 strval = vim_strsave(buf);
18579 if (strval == NULL)
18580 goto free_lstval;
18581 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018582 }
18583 *curval++ = strval;
18584 }
18585 *curval++ = NULL;
18586
18587 write_reg_contents_lst(regname, lstval, -1,
18588 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018589free_lstval:
18590 while (curallocval > allocval)
18591 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018592 vim_free(lstval);
18593 }
18594 else
18595 {
18596 strval = get_tv_string_chk(&argvars[1]);
18597 if (strval == NULL)
18598 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018599 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018602 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603}
18604
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018605/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018606 * "settabvar()" function
18607 */
18608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018609f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018610{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018611#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018612 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018613 tabpage_T *tp;
18614#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018615 char_u *varname, *tabvarname;
18616 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018617
18618 rettv->vval.v_number = 0;
18619
18620 if (check_restricted() || check_secure())
18621 return;
18622
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018623#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018624 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018625#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018626 varname = get_tv_string_chk(&argvars[1]);
18627 varp = &argvars[2];
18628
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018629 if (varname != NULL && varp != NULL
18630#ifdef FEAT_WINDOWS
18631 && tp != NULL
18632#endif
18633 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018634 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018635#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018636 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018637 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018638#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018639
18640 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18641 if (tabvarname != NULL)
18642 {
18643 STRCPY(tabvarname, "t:");
18644 STRCPY(tabvarname + 2, varname);
18645 set_var(tabvarname, varp, TRUE);
18646 vim_free(tabvarname);
18647 }
18648
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018649#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018650 /* Restore current tabpage */
18651 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018652 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018653#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018654 }
18655}
18656
18657/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018658 * "settabwinvar()" function
18659 */
18660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018661f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018662{
18663 setwinvar(argvars, rettv, 1);
18664}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665
18666/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018667 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018670f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018672 setwinvar(argvars, rettv, 0);
18673}
18674
18675/*
18676 * "setwinvar()" and "settabwinvar()" functions
18677 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018678
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018680setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018681{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 win_T *win;
18683#ifdef FEAT_WINDOWS
18684 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018685 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018686 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687#endif
18688 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018689 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018691 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692
18693 if (check_restricted() || check_secure())
18694 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018695
18696#ifdef FEAT_WINDOWS
18697 if (off == 1)
18698 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18699 else
18700 tp = curtab;
18701#endif
18702 win = find_win_by_nr(&argvars[off], tp);
18703 varname = get_tv_string_chk(&argvars[off + 1]);
18704 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705
18706 if (win != NULL && varname != NULL && varp != NULL)
18707 {
18708#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018709 need_switch_win = !(tp == curtab && win == curwin);
18710 if (!need_switch_win
18711 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018714 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018716 long numval;
18717 char_u *strval;
18718 int error = FALSE;
18719
18720 ++varname;
18721 numval = get_tv_number_chk(varp, &error);
18722 strval = get_tv_string_buf_chk(varp, nbuf);
18723 if (!error && strval != NULL)
18724 set_option_value(varname, numval, strval, OPT_LOCAL);
18725 }
18726 else
18727 {
18728 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18729 if (winvarname != NULL)
18730 {
18731 STRCPY(winvarname, "w:");
18732 STRCPY(winvarname + 2, varname);
18733 set_var(winvarname, varp, TRUE);
18734 vim_free(winvarname);
18735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 }
18737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018739 if (need_switch_win)
18740 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741#endif
18742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743}
18744
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018745#ifdef FEAT_CRYPT
18746/*
18747 * "sha256({string})" function
18748 */
18749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018750f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018751{
18752 char_u *p;
18753
18754 p = get_tv_string(&argvars[0]);
18755 rettv->vval.v_string = vim_strsave(
18756 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18757 rettv->v_type = VAR_STRING;
18758}
18759#endif /* FEAT_CRYPT */
18760
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018762 * "shellescape({string})" function
18763 */
18764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018765f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018766{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018767 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018768 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018769 rettv->v_type = VAR_STRING;
18770}
18771
18772/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018773 * shiftwidth() function
18774 */
18775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018776f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018777{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018778 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018779}
18780
18781/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018782 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 */
18784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018785f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018787 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788
Bram Moolenaar0d660222005-01-07 21:51:51 +000018789 p = get_tv_string(&argvars[0]);
18790 rettv->vval.v_string = vim_strsave(p);
18791 simplify_filename(rettv->vval.v_string); /* simplify in place */
18792 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793}
18794
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018795#ifdef FEAT_FLOAT
18796/*
18797 * "sin()" function
18798 */
18799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018800f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018801{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018802 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018803
18804 rettv->v_type = VAR_FLOAT;
18805 if (get_float_arg(argvars, &f) == OK)
18806 rettv->vval.v_float = sin(f);
18807 else
18808 rettv->vval.v_float = 0.0;
18809}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018810
18811/*
18812 * "sinh()" function
18813 */
18814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018815f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018816{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018817 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018818
18819 rettv->v_type = VAR_FLOAT;
18820 if (get_float_arg(argvars, &f) == OK)
18821 rettv->vval.v_float = sinh(f);
18822 else
18823 rettv->vval.v_float = 0.0;
18824}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018825#endif
18826
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_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018832static int
18833#ifdef __BORLANDC__
18834 _RTLENTRYF
18835#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018836 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018837
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018838/* struct used in the array that's given to qsort() */
18839typedef struct
18840{
18841 listitem_T *item;
18842 int idx;
18843} sortItem_T;
18844
Bram Moolenaar0b962472016-02-22 22:51:33 +010018845/* struct storing information about current sort */
18846typedef struct
18847{
18848 int item_compare_ic;
18849 int item_compare_numeric;
18850 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018851#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018852 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018853#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018854 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018855 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018856 dict_T *item_compare_selfdict;
18857 int item_compare_func_err;
18858 int item_compare_keep_zero;
18859} sortinfo_T;
18860static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018861static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018862#define ITEM_COMPARE_FAIL 999
18863
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018865 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018867 static int
18868#ifdef __BORLANDC__
18869_RTLENTRYF
18870#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018871item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018873 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018874 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018875 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018876 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018877 int res;
18878 char_u numbuf1[NUMBUFLEN];
18879 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018881 si1 = (sortItem_T *)s1;
18882 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018883 tv1 = &si1->item->li_tv;
18884 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018885
Bram Moolenaar0b962472016-02-22 22:51:33 +010018886 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018887 {
18888 long v1 = get_tv_number(tv1);
18889 long v2 = get_tv_number(tv2);
18890
18891 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18892 }
18893
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018894#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018895 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018896 {
18897 float_T v1 = get_tv_float(tv1);
18898 float_T v2 = get_tv_float(tv2);
18899
18900 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18901 }
18902#endif
18903
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018904 /* tv2string() puts quotes around a string and allocates memory. Don't do
18905 * that for string variables. Use a single quote when comparing with a
18906 * non-string to do what the docs promise. */
18907 if (tv1->v_type == VAR_STRING)
18908 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018909 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018910 p1 = (char_u *)"'";
18911 else
18912 p1 = tv1->vval.v_string;
18913 }
18914 else
18915 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18916 if (tv2->v_type == VAR_STRING)
18917 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018918 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018919 p2 = (char_u *)"'";
18920 else
18921 p2 = tv2->vval.v_string;
18922 }
18923 else
18924 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018925 if (p1 == NULL)
18926 p1 = (char_u *)"";
18927 if (p2 == NULL)
18928 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018929 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018930 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018931 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018932 res = STRICMP(p1, p2);
18933 else
18934 res = STRCMP(p1, p2);
18935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018937 {
18938 double n1, n2;
18939 n1 = strtod((char *)p1, (char **)&p1);
18940 n2 = strtod((char *)p2, (char **)&p2);
18941 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18942 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018943
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018944 /* When the result would be zero, compare the item indexes. Makes the
18945 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018946 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018947 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018948
Bram Moolenaar0d660222005-01-07 21:51:51 +000018949 vim_free(tofree1);
18950 vim_free(tofree2);
18951 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952}
18953
18954 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018955#ifdef __BORLANDC__
18956_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018958item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018959{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018960 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018961 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018962 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018963 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018964 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018965 char_u *func_name;
18966 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018968 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018969 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018970 return 0;
18971
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018972 si1 = (sortItem_T *)s1;
18973 si2 = (sortItem_T *)s2;
18974
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018975 if (partial == NULL)
18976 func_name = sortinfo->item_compare_func;
18977 else
18978 func_name = partial->pt_name;
18979
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018980 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018981 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018982 copy_tv(&si1->item->li_tv, &argv[0]);
18983 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018984
18985 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018986 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018987 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018988 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018989 clear_tv(&argv[0]);
18990 clear_tv(&argv[1]);
18991
18992 if (res == FAIL)
18993 res = ITEM_COMPARE_FAIL;
18994 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018995 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18996 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018997 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018998 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018999
19000 /* When the result would be zero, compare the pointers themselves. Makes
19001 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019002 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019003 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019004
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005 return res;
19006}
19007
19008/*
19009 * "sort({list})" function
19010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019012do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013{
Bram Moolenaar33570922005-01-25 22:26:29 +000019014 list_T *l;
19015 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019016 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019017 sortinfo_T *old_sortinfo;
19018 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019019 long len;
19020 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021
Bram Moolenaar0b962472016-02-22 22:51:33 +010019022 /* Pointer to current info struct used in compare function. Save and
19023 * restore the current one for nested calls. */
19024 old_sortinfo = sortinfo;
19025 sortinfo = &info;
19026
Bram Moolenaar0d660222005-01-07 21:51:51 +000019027 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019028 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 else
19030 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019031 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019032 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019033 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19034 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019035 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019036 rettv->vval.v_list = l;
19037 rettv->v_type = VAR_LIST;
19038 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039
Bram Moolenaar0d660222005-01-07 21:51:51 +000019040 len = list_len(l);
19041 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019042 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043
Bram Moolenaar0b962472016-02-22 22:51:33 +010019044 info.item_compare_ic = FALSE;
19045 info.item_compare_numeric = FALSE;
19046 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019047#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019048 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019049#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019050 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019051 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019052 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053 if (argvars[1].v_type != VAR_UNKNOWN)
19054 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019055 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019056 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019057 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019058 else if (argvars[1].v_type == VAR_PARTIAL)
19059 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019060 else
19061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019062 int error = FALSE;
19063
19064 i = get_tv_number_chk(&argvars[1], &error);
19065 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019066 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019067 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019068 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019069 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019070 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019071 else if (i != 0)
19072 {
19073 EMSG(_(e_invarg));
19074 goto theend;
19075 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019076 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019077 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019078 if (*info.item_compare_func == NUL)
19079 {
19080 /* empty string means default sort */
19081 info.item_compare_func = NULL;
19082 }
19083 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019084 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019085 info.item_compare_func = NULL;
19086 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019087 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019088 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019089 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019090 info.item_compare_func = NULL;
19091 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019092 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019093#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019095 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019096 info.item_compare_func = NULL;
19097 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019098 }
19099#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019100 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019101 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019102 info.item_compare_func = NULL;
19103 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019104 }
19105 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019106 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019107
19108 if (argvars[2].v_type != VAR_UNKNOWN)
19109 {
19110 /* optional third argument: {dict} */
19111 if (argvars[2].v_type != VAR_DICT)
19112 {
19113 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019115 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019117 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119
Bram Moolenaar0d660222005-01-07 21:51:51 +000019120 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019121 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019122 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019123 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124
Bram Moolenaar327aa022014-03-25 18:24:23 +010019125 i = 0;
19126 if (sort)
19127 {
19128 /* sort(): ptrs will be the list to sort */
19129 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019130 {
19131 ptrs[i].item = li;
19132 ptrs[i].idx = i;
19133 ++i;
19134 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019135
Bram Moolenaar0b962472016-02-22 22:51:33 +010019136 info.item_compare_func_err = FALSE;
19137 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019138 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019139 if ((info.item_compare_func != NULL
19140 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019141 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019142 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019143 EMSG(_("E702: Sort compare function failed"));
19144 else
19145 {
19146 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019147 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019148 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019149 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019150 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019151
Bram Moolenaar0b962472016-02-22 22:51:33 +010019152 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019153 {
19154 /* Clear the List and append the items in sorted order. */
19155 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19156 l->lv_len = 0;
19157 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019158 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019159 }
19160 }
19161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019163 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019164 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019165
19166 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019167 info.item_compare_func_err = FALSE;
19168 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019169 item_compare_func_ptr = info.item_compare_func != NULL
19170 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019171 ? item_compare2 : item_compare;
19172
19173 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19174 li = li->li_next)
19175 {
19176 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19177 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019178 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019179 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019180 {
19181 EMSG(_("E882: Uniq compare function failed"));
19182 break;
19183 }
19184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019185
Bram Moolenaar0b962472016-02-22 22:51:33 +010019186 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019187 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019188 while (--i >= 0)
19189 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019190 li = ptrs[i].item->li_next;
19191 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019192 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019193 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019194 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019195 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019196 list_fix_watch(l, li);
19197 listitem_free(li);
19198 l->lv_len--;
19199 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019200 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019201 }
19202
19203 vim_free(ptrs);
19204 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019205theend:
19206 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019207}
19208
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019209/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019210 * "sort({list})" function
19211 */
19212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019213f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019214{
19215 do_sort_uniq(argvars, rettv, TRUE);
19216}
19217
19218/*
19219 * "uniq({list})" function
19220 */
19221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019222f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019223{
19224 do_sort_uniq(argvars, rettv, FALSE);
19225}
19226
19227/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019228 * "soundfold({word})" function
19229 */
19230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019231f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019232{
19233 char_u *s;
19234
19235 rettv->v_type = VAR_STRING;
19236 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019237#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019238 rettv->vval.v_string = eval_soundfold(s);
19239#else
19240 rettv->vval.v_string = vim_strsave(s);
19241#endif
19242}
19243
19244/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019245 * "spellbadword()" function
19246 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019248f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019249{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019250 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019251 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019252 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019253
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019254 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019255 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019256
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019257#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019258 if (argvars[0].v_type == VAR_UNKNOWN)
19259 {
19260 /* Find the start and length of the badly spelled word. */
19261 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19262 if (len != 0)
19263 word = ml_get_cursor();
19264 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019265 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019266 {
19267 char_u *str = get_tv_string_chk(&argvars[0]);
19268 int capcol = -1;
19269
19270 if (str != NULL)
19271 {
19272 /* Check the argument for spelling. */
19273 while (*str != NUL)
19274 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019275 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019276 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019277 {
19278 word = str;
19279 break;
19280 }
19281 str += len;
19282 }
19283 }
19284 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019285#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019286
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019287 list_append_string(rettv->vval.v_list, word, len);
19288 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019289 attr == HLF_SPB ? "bad" :
19290 attr == HLF_SPR ? "rare" :
19291 attr == HLF_SPL ? "local" :
19292 attr == HLF_SPC ? "caps" :
19293 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019294}
19295
19296/*
19297 * "spellsuggest()" function
19298 */
19299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019300f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019301{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019302#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019303 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019304 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019305 int maxcount;
19306 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019307 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019308 listitem_T *li;
19309 int need_capital = FALSE;
19310#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019311
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019312 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019313 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019314
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019315#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019316 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019317 {
19318 str = get_tv_string(&argvars[0]);
19319 if (argvars[1].v_type != VAR_UNKNOWN)
19320 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019321 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019322 if (maxcount <= 0)
19323 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019324 if (argvars[2].v_type != VAR_UNKNOWN)
19325 {
19326 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19327 if (typeerr)
19328 return;
19329 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019330 }
19331 else
19332 maxcount = 25;
19333
Bram Moolenaar4770d092006-01-12 23:22:24 +000019334 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019335
19336 for (i = 0; i < ga.ga_len; ++i)
19337 {
19338 str = ((char_u **)ga.ga_data)[i];
19339
19340 li = listitem_alloc();
19341 if (li == NULL)
19342 vim_free(str);
19343 else
19344 {
19345 li->li_tv.v_type = VAR_STRING;
19346 li->li_tv.v_lock = 0;
19347 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019348 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019349 }
19350 }
19351 ga_clear(&ga);
19352 }
19353#endif
19354}
19355
Bram Moolenaar0d660222005-01-07 21:51:51 +000019356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019357f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019358{
19359 char_u *str;
19360 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019361 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019362 regmatch_T regmatch;
19363 char_u patbuf[NUMBUFLEN];
19364 char_u *save_cpo;
19365 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019366 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019367 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019368 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019369
19370 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19371 save_cpo = p_cpo;
19372 p_cpo = (char_u *)"";
19373
19374 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019375 if (argvars[1].v_type != VAR_UNKNOWN)
19376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019377 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19378 if (pat == NULL)
19379 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019380 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019381 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019382 }
19383 if (pat == NULL || *pat == NUL)
19384 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019385
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019386 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019388 if (typeerr)
19389 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390
Bram Moolenaar0d660222005-01-07 21:51:51 +000019391 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19392 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019394 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019395 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019396 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019397 if (*str == NUL)
19398 match = FALSE; /* empty item at the end */
19399 else
19400 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019401 if (match)
19402 end = regmatch.startp[0];
19403 else
19404 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019405 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19406 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019407 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019408 if (list_append_string(rettv->vval.v_list, str,
19409 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019410 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019411 }
19412 if (!match)
19413 break;
19414 /* Advance to just after the match. */
19415 if (regmatch.endp[0] > str)
19416 col = 0;
19417 else
19418 {
19419 /* Don't get stuck at the same match. */
19420#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019421 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019422#else
19423 col = 1;
19424#endif
19425 }
19426 str = regmatch.endp[0];
19427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428
Bram Moolenaar473de612013-06-08 18:19:48 +020019429 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431
Bram Moolenaar0d660222005-01-07 21:51:51 +000019432 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433}
19434
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019435#ifdef FEAT_FLOAT
19436/*
19437 * "sqrt()" function
19438 */
19439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019440f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019441{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019442 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019443
19444 rettv->v_type = VAR_FLOAT;
19445 if (get_float_arg(argvars, &f) == OK)
19446 rettv->vval.v_float = sqrt(f);
19447 else
19448 rettv->vval.v_float = 0.0;
19449}
19450
19451/*
19452 * "str2float()" function
19453 */
19454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019455f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019456{
19457 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19458
19459 if (*p == '+')
19460 p = skipwhite(p + 1);
19461 (void)string2float(p, &rettv->vval.v_float);
19462 rettv->v_type = VAR_FLOAT;
19463}
19464#endif
19465
Bram Moolenaar2c932302006-03-18 21:42:09 +000019466/*
19467 * "str2nr()" function
19468 */
19469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019470f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019471{
19472 int base = 10;
19473 char_u *p;
19474 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019475 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019476
19477 if (argvars[1].v_type != VAR_UNKNOWN)
19478 {
19479 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019480 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019481 {
19482 EMSG(_(e_invarg));
19483 return;
19484 }
19485 }
19486
19487 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019488 if (*p == '+')
19489 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019490 switch (base)
19491 {
19492 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19493 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19494 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19495 default: what = 0;
19496 }
19497 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019498 rettv->vval.v_number = n;
19499}
19500
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501#ifdef HAVE_STRFTIME
19502/*
19503 * "strftime({format}[, {time}])" function
19504 */
19505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019506f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507{
19508 char_u result_buf[256];
19509 struct tm *curtime;
19510 time_t seconds;
19511 char_u *p;
19512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019513 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019515 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019516 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 seconds = time(NULL);
19518 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019519 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 curtime = localtime(&seconds);
19521 /* MSVC returns NULL for an invalid value of seconds. */
19522 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 else
19525 {
19526# ifdef FEAT_MBYTE
19527 vimconv_T conv;
19528 char_u *enc;
19529
19530 conv.vc_type = CONV_NONE;
19531 enc = enc_locale();
19532 convert_setup(&conv, p_enc, enc);
19533 if (conv.vc_type != CONV_NONE)
19534 p = string_convert(&conv, p, NULL);
19535# endif
19536 if (p != NULL)
19537 (void)strftime((char *)result_buf, sizeof(result_buf),
19538 (char *)p, curtime);
19539 else
19540 result_buf[0] = NUL;
19541
19542# ifdef FEAT_MBYTE
19543 if (conv.vc_type != CONV_NONE)
19544 vim_free(p);
19545 convert_setup(&conv, enc, p_enc);
19546 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019547 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 else
19549# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019550 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551
19552# ifdef FEAT_MBYTE
19553 /* Release conversion descriptors */
19554 convert_setup(&conv, NULL, NULL);
19555 vim_free(enc);
19556# endif
19557 }
19558}
19559#endif
19560
19561/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019562 * "strgetchar()" function
19563 */
19564 static void
19565f_strgetchar(typval_T *argvars, typval_T *rettv)
19566{
19567 char_u *str;
19568 int len;
19569 int error = FALSE;
19570 int charidx;
19571
19572 rettv->vval.v_number = -1;
19573 str = get_tv_string_chk(&argvars[0]);
19574 if (str == NULL)
19575 return;
19576 len = (int)STRLEN(str);
19577 charidx = get_tv_number_chk(&argvars[1], &error);
19578 if (error)
19579 return;
19580#ifdef FEAT_MBYTE
19581 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019582 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019583
19584 while (charidx >= 0 && byteidx < len)
19585 {
19586 if (charidx == 0)
19587 {
19588 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19589 break;
19590 }
19591 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019592 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019593 }
19594 }
19595#else
19596 if (charidx < len)
19597 rettv->vval.v_number = str[charidx];
19598#endif
19599}
19600
19601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 * "stridx()" function
19603 */
19604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019605f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606{
19607 char_u buf[NUMBUFLEN];
19608 char_u *needle;
19609 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019610 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019612 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019614 needle = get_tv_string_chk(&argvars[1]);
19615 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019617 if (needle == NULL || haystack == NULL)
19618 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619
Bram Moolenaar33570922005-01-25 22:26:29 +000019620 if (argvars[2].v_type != VAR_UNKNOWN)
19621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019622 int error = FALSE;
19623
19624 start_idx = get_tv_number_chk(&argvars[2], &error);
19625 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019627 if (start_idx >= 0)
19628 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019629 }
19630
19631 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19632 if (pos != NULL)
19633 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634}
19635
19636/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019637 * "string()" function
19638 */
19639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019640f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019641{
19642 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019643 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019645 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019646 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19647 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019648 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019649 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019650 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651}
19652
19653/*
19654 * "strlen()" function
19655 */
19656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019657f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019659 rettv->vval.v_number = (varnumber_T)(STRLEN(
19660 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661}
19662
19663/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019664 * "strchars()" function
19665 */
19666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019667f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019668{
19669 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019670 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019671#ifdef FEAT_MBYTE
19672 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019673 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019674#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019675
19676 if (argvars[1].v_type != VAR_UNKNOWN)
19677 skipcc = get_tv_number_chk(&argvars[1], NULL);
19678 if (skipcc < 0 || skipcc > 1)
19679 EMSG(_(e_invarg));
19680 else
19681 {
19682#ifdef FEAT_MBYTE
19683 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19684 while (*s != NUL)
19685 {
19686 func_mb_ptr2char_adv(&s);
19687 ++len;
19688 }
19689 rettv->vval.v_number = len;
19690#else
19691 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19692#endif
19693 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019694}
19695
19696/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019697 * "strdisplaywidth()" function
19698 */
19699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019700f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019701{
19702 char_u *s = get_tv_string(&argvars[0]);
19703 int col = 0;
19704
19705 if (argvars[1].v_type != VAR_UNKNOWN)
19706 col = get_tv_number(&argvars[1]);
19707
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019708 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019709}
19710
19711/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019712 * "strwidth()" function
19713 */
19714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019715f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019716{
19717 char_u *s = get_tv_string(&argvars[0]);
19718
19719 rettv->vval.v_number = (varnumber_T)(
19720#ifdef FEAT_MBYTE
19721 mb_string2cells(s, -1)
19722#else
19723 STRLEN(s)
19724#endif
19725 );
19726}
19727
19728/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019729 * "strcharpart()" function
19730 */
19731 static void
19732f_strcharpart(typval_T *argvars, typval_T *rettv)
19733{
19734#ifdef FEAT_MBYTE
19735 char_u *p;
19736 int nchar;
19737 int nbyte = 0;
19738 int charlen;
19739 int len = 0;
19740 int slen;
19741 int error = FALSE;
19742
19743 p = get_tv_string(&argvars[0]);
19744 slen = (int)STRLEN(p);
19745
19746 nchar = get_tv_number_chk(&argvars[1], &error);
19747 if (!error)
19748 {
19749 if (nchar > 0)
19750 while (nchar > 0 && nbyte < slen)
19751 {
19752 nbyte += mb_char2len(p[nbyte]);
19753 --nchar;
19754 }
19755 else
19756 nbyte = nchar;
19757 if (argvars[2].v_type != VAR_UNKNOWN)
19758 {
19759 charlen = get_tv_number(&argvars[2]);
19760 while (charlen > 0 && nbyte + len < slen)
19761 {
19762 len += mb_char2len(p[nbyte + len]);
19763 --charlen;
19764 }
19765 }
19766 else
19767 len = slen - nbyte; /* default: all bytes that are available. */
19768 }
19769
19770 /*
19771 * Only return the overlap between the specified part and the actual
19772 * string.
19773 */
19774 if (nbyte < 0)
19775 {
19776 len += nbyte;
19777 nbyte = 0;
19778 }
19779 else if (nbyte > slen)
19780 nbyte = slen;
19781 if (len < 0)
19782 len = 0;
19783 else if (nbyte + len > slen)
19784 len = slen - nbyte;
19785
19786 rettv->v_type = VAR_STRING;
19787 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19788#else
19789 f_strpart(argvars, rettv);
19790#endif
19791}
19792
19793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 * "strpart()" function
19795 */
19796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019797f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798{
19799 char_u *p;
19800 int n;
19801 int len;
19802 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019803 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 slen = (int)STRLEN(p);
19807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019808 n = get_tv_number_chk(&argvars[1], &error);
19809 if (error)
19810 len = 0;
19811 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 else
19814 len = slen - n; /* default len: all bytes that are available. */
19815
19816 /*
19817 * Only return the overlap between the specified part and the actual
19818 * string.
19819 */
19820 if (n < 0)
19821 {
19822 len += n;
19823 n = 0;
19824 }
19825 else if (n > slen)
19826 n = slen;
19827 if (len < 0)
19828 len = 0;
19829 else if (n + len > slen)
19830 len = slen - n;
19831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019832 rettv->v_type = VAR_STRING;
19833 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834}
19835
19836/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019837 * "strridx()" function
19838 */
19839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019840f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019841{
19842 char_u buf[NUMBUFLEN];
19843 char_u *needle;
19844 char_u *haystack;
19845 char_u *rest;
19846 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019847 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019849 needle = get_tv_string_chk(&argvars[1]);
19850 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019851
19852 rettv->vval.v_number = -1;
19853 if (needle == NULL || haystack == NULL)
19854 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019855
19856 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019857 if (argvars[2].v_type != VAR_UNKNOWN)
19858 {
19859 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019860 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019861 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019862 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019863 }
19864 else
19865 end_idx = haystack_len;
19866
Bram Moolenaar0d660222005-01-07 21:51:51 +000019867 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019868 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019869 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019870 lastmatch = haystack + end_idx;
19871 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019872 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019873 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019874 for (rest = haystack; *rest != '\0'; ++rest)
19875 {
19876 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019877 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019878 break;
19879 lastmatch = rest;
19880 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019881 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019882
19883 if (lastmatch == NULL)
19884 rettv->vval.v_number = -1;
19885 else
19886 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19887}
19888
19889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 * "strtrans()" function
19891 */
19892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019893f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019895 rettv->v_type = VAR_STRING;
19896 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897}
19898
19899/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019900 * "submatch()" function
19901 */
19902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019903f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019904{
Bram Moolenaar41571762014-04-02 19:00:58 +020019905 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019906 int no;
19907 int retList = 0;
19908
19909 no = (int)get_tv_number_chk(&argvars[0], &error);
19910 if (error)
19911 return;
19912 error = FALSE;
19913 if (argvars[1].v_type != VAR_UNKNOWN)
19914 retList = get_tv_number_chk(&argvars[1], &error);
19915 if (error)
19916 return;
19917
19918 if (retList == 0)
19919 {
19920 rettv->v_type = VAR_STRING;
19921 rettv->vval.v_string = reg_submatch(no);
19922 }
19923 else
19924 {
19925 rettv->v_type = VAR_LIST;
19926 rettv->vval.v_list = reg_submatch_list(no);
19927 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019928}
19929
19930/*
19931 * "substitute()" function
19932 */
19933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019934f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019935{
19936 char_u patbuf[NUMBUFLEN];
19937 char_u subbuf[NUMBUFLEN];
19938 char_u flagsbuf[NUMBUFLEN];
19939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019940 char_u *str = get_tv_string_chk(&argvars[0]);
19941 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19942 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19943 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19944
Bram Moolenaar0d660222005-01-07 21:51:51 +000019945 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019946 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19947 rettv->vval.v_string = NULL;
19948 else
19949 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019950}
19951
19952/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019953 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019956f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957{
19958 int id = 0;
19959#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019960 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961 long col;
19962 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019963 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019965 lnum = get_tv_lnum(argvars); /* -1 on type error */
19966 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19967 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019969 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019970 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019971 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972#endif
19973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975}
19976
19977/*
19978 * "synIDattr(id, what [, mode])" function
19979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019981f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982{
19983 char_u *p = NULL;
19984#ifdef FEAT_SYN_HL
19985 int id;
19986 char_u *what;
19987 char_u *mode;
19988 char_u modebuf[NUMBUFLEN];
19989 int modec;
19990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019991 id = get_tv_number(&argvars[0]);
19992 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019993 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019995 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019997 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 modec = 0; /* replace invalid with current */
19999 }
20000 else
20001 {
20002#ifdef FEAT_GUI
20003 if (gui.in_use)
20004 modec = 'g';
20005 else
20006#endif
20007 if (t_colors > 1)
20008 modec = 'c';
20009 else
20010 modec = 't';
20011 }
20012
20013
20014 switch (TOLOWER_ASC(what[0]))
20015 {
20016 case 'b':
20017 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20018 p = highlight_color(id, what, modec);
20019 else /* bold */
20020 p = highlight_has_attr(id, HL_BOLD, modec);
20021 break;
20022
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020023 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 p = highlight_color(id, what, modec);
20025 break;
20026
20027 case 'i':
20028 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20029 p = highlight_has_attr(id, HL_INVERSE, modec);
20030 else /* italic */
20031 p = highlight_has_attr(id, HL_ITALIC, modec);
20032 break;
20033
20034 case 'n': /* name */
20035 p = get_highlight_name(NULL, id - 1);
20036 break;
20037
20038 case 'r': /* reverse */
20039 p = highlight_has_attr(id, HL_INVERSE, modec);
20040 break;
20041
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020042 case 's':
20043 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20044 p = highlight_color(id, what, modec);
20045 else /* standout */
20046 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 break;
20048
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020049 case 'u':
20050 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20051 /* underline */
20052 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20053 else
20054 /* undercurl */
20055 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 break;
20057 }
20058
20059 if (p != NULL)
20060 p = vim_strsave(p);
20061#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020062 rettv->v_type = VAR_STRING;
20063 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064}
20065
20066/*
20067 * "synIDtrans(id)" function
20068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020070f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071{
20072 int id;
20073
20074#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076
20077 if (id > 0)
20078 id = syn_get_final_id(id);
20079 else
20080#endif
20081 id = 0;
20082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020083 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084}
20085
20086/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020087 * "synconcealed(lnum, col)" function
20088 */
20089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020090f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020091{
20092#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20093 long lnum;
20094 long col;
20095 int syntax_flags = 0;
20096 int cchar;
20097 int matchid = 0;
20098 char_u str[NUMBUFLEN];
20099#endif
20100
20101 rettv->v_type = VAR_LIST;
20102 rettv->vval.v_list = NULL;
20103
20104#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20105 lnum = get_tv_lnum(argvars); /* -1 on type error */
20106 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20107
20108 vim_memset(str, NUL, sizeof(str));
20109
20110 if (rettv_list_alloc(rettv) != FAIL)
20111 {
20112 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20113 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20114 && curwin->w_p_cole > 0)
20115 {
20116 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20117 syntax_flags = get_syntax_info(&matchid);
20118
20119 /* get the conceal character */
20120 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20121 {
20122 cchar = syn_get_sub_char();
20123 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20124 cchar = lcs_conceal;
20125 if (cchar != NUL)
20126 {
20127# ifdef FEAT_MBYTE
20128 if (has_mbyte)
20129 (*mb_char2bytes)(cchar, str);
20130 else
20131# endif
20132 str[0] = cchar;
20133 }
20134 }
20135 }
20136
20137 list_append_number(rettv->vval.v_list,
20138 (syntax_flags & HL_CONCEAL) != 0);
20139 /* -1 to auto-determine strlen */
20140 list_append_string(rettv->vval.v_list, str, -1);
20141 list_append_number(rettv->vval.v_list, matchid);
20142 }
20143#endif
20144}
20145
20146/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020147 * "synstack(lnum, col)" function
20148 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020150f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020151{
20152#ifdef FEAT_SYN_HL
20153 long lnum;
20154 long col;
20155 int i;
20156 int id;
20157#endif
20158
20159 rettv->v_type = VAR_LIST;
20160 rettv->vval.v_list = NULL;
20161
20162#ifdef FEAT_SYN_HL
20163 lnum = get_tv_lnum(argvars); /* -1 on type error */
20164 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20165
20166 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020167 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020168 && rettv_list_alloc(rettv) != FAIL)
20169 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020170 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020171 for (i = 0; ; ++i)
20172 {
20173 id = syn_get_stack_item(i);
20174 if (id < 0)
20175 break;
20176 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20177 break;
20178 }
20179 }
20180#endif
20181}
20182
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020184get_cmd_output_as_rettv(
20185 typval_T *argvars,
20186 typval_T *rettv,
20187 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020189 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020191 char_u *infile = NULL;
20192 char_u buf[NUMBUFLEN];
20193 int err = FALSE;
20194 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020195 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020196 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020198 rettv->v_type = VAR_STRING;
20199 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020200 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020201 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020202
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020203 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020204 {
20205 /*
20206 * Write the string to a temp file, to be used for input of the shell
20207 * command.
20208 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020209 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020210 {
20211 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020212 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020213 }
20214
20215 fd = mch_fopen((char *)infile, WRITEBIN);
20216 if (fd == NULL)
20217 {
20218 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020219 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020220 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020221 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020222 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020223 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20224 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020225 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020226 else
20227 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020228 size_t len;
20229
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020230 p = get_tv_string_buf_chk(&argvars[1], buf);
20231 if (p == NULL)
20232 {
20233 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020234 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020235 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020236 len = STRLEN(p);
20237 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020238 err = TRUE;
20239 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020240 if (fclose(fd) != 0)
20241 err = TRUE;
20242 if (err)
20243 {
20244 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020245 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020246 }
20247 }
20248
Bram Moolenaar52a72462014-08-29 15:53:52 +020020249 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20250 * echoes typeahead, that messes up the display. */
20251 if (!msg_silent)
20252 flags += SHELL_COOKED;
20253
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020254 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020256 int len;
20257 listitem_T *li;
20258 char_u *s = NULL;
20259 char_u *start;
20260 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020261 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262
Bram Moolenaar52a72462014-08-29 15:53:52 +020020263 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020264 if (res == NULL)
20265 goto errret;
20266
20267 list = list_alloc();
20268 if (list == NULL)
20269 goto errret;
20270
20271 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020273 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020274 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020275 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020276 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020277
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020278 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020279 if (s == NULL)
20280 goto errret;
20281
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020282 for (p = s; start < end; ++p, ++start)
20283 *p = *start == NUL ? NL : *start;
20284 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020285
20286 li = listitem_alloc();
20287 if (li == NULL)
20288 {
20289 vim_free(s);
20290 goto errret;
20291 }
20292 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020293 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020294 li->li_tv.vval.v_string = s;
20295 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020297
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020298 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020299 rettv->v_type = VAR_LIST;
20300 rettv->vval.v_list = list;
20301 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020303 else
20304 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020305 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020306#ifdef USE_CR
20307 /* translate <CR> into <NL> */
20308 if (res != NULL)
20309 {
20310 char_u *s;
20311
20312 for (s = res; *s; ++s)
20313 {
20314 if (*s == CAR)
20315 *s = NL;
20316 }
20317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318#else
20319# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020320 /* translate <CR><NL> into <NL> */
20321 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020323 char_u *s, *d;
20324
20325 d = res;
20326 for (s = res; *s; ++s)
20327 {
20328 if (s[0] == CAR && s[1] == NL)
20329 ++s;
20330 *d++ = *s;
20331 }
20332 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334# endif
20335#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020336 rettv->vval.v_string = res;
20337 res = NULL;
20338 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020339
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020340errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020341 if (infile != NULL)
20342 {
20343 mch_remove(infile);
20344 vim_free(infile);
20345 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020346 if (res != NULL)
20347 vim_free(res);
20348 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020349 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020350}
20351
20352/*
20353 * "system()" function
20354 */
20355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020356f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020357{
20358 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20359}
20360
20361/*
20362 * "systemlist()" function
20363 */
20364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020365f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020366{
20367 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368}
20369
20370/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020371 * "tabpagebuflist()" function
20372 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020374f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020375{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020376#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020377 tabpage_T *tp;
20378 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020379
20380 if (argvars[0].v_type == VAR_UNKNOWN)
20381 wp = firstwin;
20382 else
20383 {
20384 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20385 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020386 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020387 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020388 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020389 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020390 for (; wp != NULL; wp = wp->w_next)
20391 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020392 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020393 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020394 }
20395#endif
20396}
20397
20398
20399/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020400 * "tabpagenr()" function
20401 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020403f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020404{
20405 int nr = 1;
20406#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020407 char_u *arg;
20408
20409 if (argvars[0].v_type != VAR_UNKNOWN)
20410 {
20411 arg = get_tv_string_chk(&argvars[0]);
20412 nr = 0;
20413 if (arg != NULL)
20414 {
20415 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020416 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020417 else
20418 EMSG2(_(e_invexpr2), arg);
20419 }
20420 }
20421 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020422 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020423#endif
20424 rettv->vval.v_number = nr;
20425}
20426
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020427
20428#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020429static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020430
20431/*
20432 * Common code for tabpagewinnr() and winnr().
20433 */
20434 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020435get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020436{
20437 win_T *twin;
20438 int nr = 1;
20439 win_T *wp;
20440 char_u *arg;
20441
20442 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20443 if (argvar->v_type != VAR_UNKNOWN)
20444 {
20445 arg = get_tv_string_chk(argvar);
20446 if (arg == NULL)
20447 nr = 0; /* type error; errmsg already given */
20448 else if (STRCMP(arg, "$") == 0)
20449 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20450 else if (STRCMP(arg, "#") == 0)
20451 {
20452 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20453 if (twin == NULL)
20454 nr = 0;
20455 }
20456 else
20457 {
20458 EMSG2(_(e_invexpr2), arg);
20459 nr = 0;
20460 }
20461 }
20462
20463 if (nr > 0)
20464 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20465 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020466 {
20467 if (wp == NULL)
20468 {
20469 /* didn't find it in this tabpage */
20470 nr = 0;
20471 break;
20472 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020473 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020474 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020475 return nr;
20476}
20477#endif
20478
20479/*
20480 * "tabpagewinnr()" function
20481 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020483f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020484{
20485 int nr = 1;
20486#ifdef FEAT_WINDOWS
20487 tabpage_T *tp;
20488
20489 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20490 if (tp == NULL)
20491 nr = 0;
20492 else
20493 nr = get_winnr(tp, &argvars[1]);
20494#endif
20495 rettv->vval.v_number = nr;
20496}
20497
20498
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020499/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020500 * "tagfiles()" function
20501 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020503f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020504{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020505 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020506 tagname_T tn;
20507 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020508
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020509 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020510 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020511 fname = alloc(MAXPATHL);
20512 if (fname == NULL)
20513 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020514
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020515 for (first = TRUE; ; first = FALSE)
20516 if (get_tagfname(&tn, first, fname) == FAIL
20517 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020518 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020519 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020520 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020521}
20522
20523/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020524 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020525 */
20526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020527f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020528{
20529 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020530
20531 tag_pattern = get_tv_string(&argvars[0]);
20532
20533 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020534 if (*tag_pattern == NUL)
20535 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020536
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020537 if (rettv_list_alloc(rettv) == OK)
20538 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020539}
20540
20541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542 * "tempname()" function
20543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020545f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546{
20547 static int x = 'A';
20548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020549 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020550 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551
20552 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20553 * names. Skip 'I' and 'O', they are used for shell redirection. */
20554 do
20555 {
20556 if (x == 'Z')
20557 x = '0';
20558 else if (x == '9')
20559 x = 'A';
20560 else
20561 {
20562#ifdef EBCDIC
20563 if (x == 'I')
20564 x = 'J';
20565 else if (x == 'R')
20566 x = 'S';
20567 else
20568#endif
20569 ++x;
20570 }
20571 } while (x == 'I' || x == 'O');
20572}
20573
20574/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020575 * "test(list)" function: Just checking the walls...
20576 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020578f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020579{
20580 /* Used for unit testing. Change the code below to your liking. */
20581#if 0
20582 listitem_T *li;
20583 list_T *l;
20584 char_u *bad, *good;
20585
20586 if (argvars[0].v_type != VAR_LIST)
20587 return;
20588 l = argvars[0].vval.v_list;
20589 if (l == NULL)
20590 return;
20591 li = l->lv_first;
20592 if (li == NULL)
20593 return;
20594 bad = get_tv_string(&li->li_tv);
20595 li = li->li_next;
20596 if (li == NULL)
20597 return;
20598 good = get_tv_string(&li->li_tv);
20599 rettv->vval.v_number = test_edit_score(bad, good);
20600#endif
20601}
20602
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020603#ifdef FEAT_FLOAT
20604/*
20605 * "tan()" function
20606 */
20607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020608f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020609{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020610 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020611
20612 rettv->v_type = VAR_FLOAT;
20613 if (get_float_arg(argvars, &f) == OK)
20614 rettv->vval.v_float = tan(f);
20615 else
20616 rettv->vval.v_float = 0.0;
20617}
20618
20619/*
20620 * "tanh()" function
20621 */
20622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020623f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020624{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020625 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020626
20627 rettv->v_type = VAR_FLOAT;
20628 if (get_float_arg(argvars, &f) == OK)
20629 rettv->vval.v_float = tanh(f);
20630 else
20631 rettv->vval.v_float = 0.0;
20632}
20633#endif
20634
Bram Moolenaar975b5272016-03-15 23:10:59 +010020635#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20636/*
20637 * Get a callback from "arg". It can be a Funcref or a function name.
20638 * When "arg" is zero return an empty string.
20639 * Return NULL for an invalid argument.
20640 */
20641 char_u *
20642get_callback(typval_T *arg, partial_T **pp)
20643{
20644 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20645 {
20646 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020647 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020648 return (*pp)->pt_name;
20649 }
20650 *pp = NULL;
20651 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20652 return arg->vval.v_string;
20653 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20654 return (char_u *)"";
20655 EMSG(_("E921: Invalid callback argument"));
20656 return NULL;
20657}
20658#endif
20659
20660#ifdef FEAT_TIMERS
20661/*
20662 * "timer_start(time, callback [, options])" function
20663 */
20664 static void
20665f_timer_start(typval_T *argvars, typval_T *rettv)
20666{
20667 long msec = get_tv_number(&argvars[0]);
20668 timer_T *timer;
20669 int repeat = 0;
20670 char_u *callback;
20671 dict_T *dict;
20672
20673 if (argvars[2].v_type != VAR_UNKNOWN)
20674 {
20675 if (argvars[2].v_type != VAR_DICT
20676 || (dict = argvars[2].vval.v_dict) == NULL)
20677 {
20678 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20679 return;
20680 }
20681 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20682 repeat = get_dict_number(dict, (char_u *)"repeat");
20683 }
20684
20685 timer = create_timer(msec, repeat);
20686 callback = get_callback(&argvars[1], &timer->tr_partial);
20687 if (callback == NULL)
20688 {
20689 stop_timer(timer);
20690 rettv->vval.v_number = -1;
20691 }
20692 else
20693 {
20694 timer->tr_callback = vim_strsave(callback);
20695 rettv->vval.v_number = timer->tr_id;
20696 }
20697}
20698
20699/*
20700 * "timer_stop(timer)" function
20701 */
20702 static void
20703f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20704{
20705 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20706
20707 if (timer != NULL)
20708 stop_timer(timer);
20709}
20710#endif
20711
Bram Moolenaard52d9742005-08-21 22:20:28 +000020712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 * "tolower(string)" function
20714 */
20715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020716f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717{
20718 char_u *p;
20719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020720 p = vim_strsave(get_tv_string(&argvars[0]));
20721 rettv->v_type = VAR_STRING;
20722 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723
20724 if (p != NULL)
20725 while (*p != NUL)
20726 {
20727#ifdef FEAT_MBYTE
20728 int l;
20729
20730 if (enc_utf8)
20731 {
20732 int c, lc;
20733
20734 c = utf_ptr2char(p);
20735 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020736 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 /* TODO: reallocate string when byte count changes. */
20738 if (utf_char2len(lc) == l)
20739 utf_char2bytes(lc, p);
20740 p += l;
20741 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020742 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 p += l; /* skip multi-byte character */
20744 else
20745#endif
20746 {
20747 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20748 ++p;
20749 }
20750 }
20751}
20752
20753/*
20754 * "toupper(string)" function
20755 */
20756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020757f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020759 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020760 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761}
20762
20763/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020764 * "tr(string, fromstr, tostr)" function
20765 */
20766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020767f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020768{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020769 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020770 char_u *fromstr;
20771 char_u *tostr;
20772 char_u *p;
20773#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020774 int inlen;
20775 int fromlen;
20776 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020777 int idx;
20778 char_u *cpstr;
20779 int cplen;
20780 int first = TRUE;
20781#endif
20782 char_u buf[NUMBUFLEN];
20783 char_u buf2[NUMBUFLEN];
20784 garray_T ga;
20785
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020786 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020787 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20788 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020789
20790 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020791 rettv->v_type = VAR_STRING;
20792 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020793 if (fromstr == NULL || tostr == NULL)
20794 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020795 ga_init2(&ga, (int)sizeof(char), 80);
20796
20797#ifdef FEAT_MBYTE
20798 if (!has_mbyte)
20799#endif
20800 /* not multi-byte: fromstr and tostr must be the same length */
20801 if (STRLEN(fromstr) != STRLEN(tostr))
20802 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020803#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020804error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020805#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020806 EMSG2(_(e_invarg2), fromstr);
20807 ga_clear(&ga);
20808 return;
20809 }
20810
20811 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020812 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020813 {
20814#ifdef FEAT_MBYTE
20815 if (has_mbyte)
20816 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020817 inlen = (*mb_ptr2len)(in_str);
20818 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020819 cplen = inlen;
20820 idx = 0;
20821 for (p = fromstr; *p != NUL; p += fromlen)
20822 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020823 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020824 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020825 {
20826 for (p = tostr; *p != NUL; p += tolen)
20827 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020828 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020829 if (idx-- == 0)
20830 {
20831 cplen = tolen;
20832 cpstr = p;
20833 break;
20834 }
20835 }
20836 if (*p == NUL) /* tostr is shorter than fromstr */
20837 goto error;
20838 break;
20839 }
20840 ++idx;
20841 }
20842
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020843 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020844 {
20845 /* Check that fromstr and tostr have the same number of
20846 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020847 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020848 first = FALSE;
20849 for (p = tostr; *p != NUL; p += tolen)
20850 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020851 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020852 --idx;
20853 }
20854 if (idx != 0)
20855 goto error;
20856 }
20857
Bram Moolenaarcde88542015-08-11 19:14:00 +020020858 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020859 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020860 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020861
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020862 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020863 }
20864 else
20865#endif
20866 {
20867 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020868 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020869 if (p != NULL)
20870 ga_append(&ga, tostr[p - fromstr]);
20871 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020872 ga_append(&ga, *in_str);
20873 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020874 }
20875 }
20876
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020877 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020878 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020879 ga_append(&ga, NUL);
20880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020881 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020882}
20883
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020884#ifdef FEAT_FLOAT
20885/*
20886 * "trunc({float})" function
20887 */
20888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020889f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020890{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020891 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020892
20893 rettv->v_type = VAR_FLOAT;
20894 if (get_float_arg(argvars, &f) == OK)
20895 /* trunc() is not in C90, use floor() or ceil() instead. */
20896 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20897 else
20898 rettv->vval.v_float = 0.0;
20899}
20900#endif
20901
Bram Moolenaar8299df92004-07-10 09:47:34 +000020902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 * "type(expr)" function
20904 */
20905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020906f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020908 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020909
20910 switch (argvars[0].v_type)
20911 {
20912 case VAR_NUMBER: n = 0; break;
20913 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020914 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020915 case VAR_FUNC: n = 2; break;
20916 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020917 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020918 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020919 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020920 if (argvars[0].vval.v_number == VVAL_FALSE
20921 || argvars[0].vval.v_number == VVAL_TRUE)
20922 n = 6;
20923 else
20924 n = 7;
20925 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020926 case VAR_JOB: n = 8; break;
20927 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020928 case VAR_UNKNOWN:
20929 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20930 n = -1;
20931 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020932 }
20933 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934}
20935
20936/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020937 * "undofile(name)" function
20938 */
20939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020940f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020941{
20942 rettv->v_type = VAR_STRING;
20943#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020944 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020945 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020946
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020947 if (*fname == NUL)
20948 {
20949 /* If there is no file name there will be no undo file. */
20950 rettv->vval.v_string = NULL;
20951 }
20952 else
20953 {
20954 char_u *ffname = FullName_save(fname, FALSE);
20955
20956 if (ffname != NULL)
20957 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20958 vim_free(ffname);
20959 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020960 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020961#else
20962 rettv->vval.v_string = NULL;
20963#endif
20964}
20965
20966/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020967 * "undotree()" function
20968 */
20969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020970f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020971{
20972 if (rettv_dict_alloc(rettv) == OK)
20973 {
20974 dict_T *dict = rettv->vval.v_dict;
20975 list_T *list;
20976
Bram Moolenaar730cde92010-06-27 05:18:54 +020020977 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020978 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020979 dict_add_nr_str(dict, "save_last",
20980 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020981 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20982 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020983 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020984
20985 list = list_alloc();
20986 if (list != NULL)
20987 {
20988 u_eval_tree(curbuf->b_u_oldhead, list);
20989 dict_add_list(dict, "entries", list);
20990 }
20991 }
20992}
20993
20994/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020995 * "values(dict)" function
20996 */
20997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020998f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020999{
21000 dict_list(argvars, rettv, 1);
21001}
21002
21003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 * "virtcol(string)" function
21005 */
21006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021007f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008{
21009 colnr_T vcol = 0;
21010 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021011 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021013 fp = var2fpos(&argvars[0], FALSE, &fnum);
21014 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21015 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 {
21017 getvvcol(curwin, fp, NULL, NULL, &vcol);
21018 ++vcol;
21019 }
21020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021021 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022}
21023
21024/*
21025 * "visualmode()" function
21026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021028f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 char_u str[2];
21031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 str[0] = curbuf->b_visual_mode_eval;
21034 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021035 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036
21037 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021038 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040}
21041
21042/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021043 * "wildmenumode()" function
21044 */
21045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021046f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021047{
21048#ifdef FEAT_WILDMENU
21049 if (wild_menu_showing)
21050 rettv->vval.v_number = 1;
21051#endif
21052}
21053
21054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 * "winbufnr(nr)" function
21056 */
21057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021058f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059{
21060 win_T *wp;
21061
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021062 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021064 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021066 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067}
21068
21069/*
21070 * "wincol()" function
21071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021073f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074{
21075 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021076 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077}
21078
21079/*
21080 * "winheight(nr)" function
21081 */
21082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021083f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084{
21085 win_T *wp;
21086
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021087 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021089 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021091 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092}
21093
21094/*
21095 * "winline()" function
21096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021098f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099{
21100 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021101 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102}
21103
21104/*
21105 * "winnr()" function
21106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021108f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109{
21110 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021111
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021113 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021115 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116}
21117
21118/*
21119 * "winrestcmd()" function
21120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021122f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123{
21124#ifdef FEAT_WINDOWS
21125 win_T *wp;
21126 int winnr = 1;
21127 garray_T ga;
21128 char_u buf[50];
21129
21130 ga_init2(&ga, (int)sizeof(char), 70);
21131 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21132 {
21133 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21134 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21136 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 ++winnr;
21138 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021139 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021141 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021143 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021145 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146}
21147
21148/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021149 * "winrestview()" function
21150 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021152f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021153{
21154 dict_T *dict;
21155
21156 if (argvars[0].v_type != VAR_DICT
21157 || (dict = argvars[0].vval.v_dict) == NULL)
21158 EMSG(_(e_invarg));
21159 else
21160 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021161 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21162 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21163 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21164 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021165#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021166 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21167 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021168#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021169 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21170 {
21171 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21172 curwin->w_set_curswant = FALSE;
21173 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021174
Bram Moolenaar82c25852014-05-28 16:47:16 +020021175 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21176 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021177#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021178 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21179 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021180#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021181 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21182 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21183 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21184 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021185
21186 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021187 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021188# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021189 win_new_width(curwin, W_WIDTH(curwin));
21190# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021191 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021192
Bram Moolenaarb851a962014-10-31 15:45:52 +010021193 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021194 curwin->w_topline = 1;
21195 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21196 curwin->w_topline = curbuf->b_ml.ml_line_count;
21197#ifdef FEAT_DIFF
21198 check_topfill(curwin, TRUE);
21199#endif
21200 }
21201}
21202
21203/*
21204 * "winsaveview()" function
21205 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021207f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021208{
21209 dict_T *dict;
21210
Bram Moolenaara800b422010-06-27 01:15:55 +020021211 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021212 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021213 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021214
21215 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21216 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21217#ifdef FEAT_VIRTUALEDIT
21218 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21219#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021220 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021221 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21222
21223 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21224#ifdef FEAT_DIFF
21225 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21226#endif
21227 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21228 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21229}
21230
21231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 * "winwidth(nr)" function
21233 */
21234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021235f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236{
21237 win_T *wp;
21238
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021239 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021243#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021244 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021246 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247#endif
21248}
21249
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021251 * "wordcount()" function
21252 */
21253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021254f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021255{
21256 if (rettv_dict_alloc(rettv) == FAIL)
21257 return;
21258 cursor_pos_info(rettv->vval.v_dict);
21259}
21260
21261/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021262 * Write list of strings to file
21263 */
21264 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021265write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021266{
21267 listitem_T *li;
21268 int c;
21269 int ret = OK;
21270 char_u *s;
21271
21272 for (li = list->lv_first; li != NULL; li = li->li_next)
21273 {
21274 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21275 {
21276 if (*s == '\n')
21277 c = putc(NUL, fd);
21278 else
21279 c = putc(*s, fd);
21280 if (c == EOF)
21281 {
21282 ret = FAIL;
21283 break;
21284 }
21285 }
21286 if (!binary || li->li_next != NULL)
21287 if (putc('\n', fd) == EOF)
21288 {
21289 ret = FAIL;
21290 break;
21291 }
21292 if (ret == FAIL)
21293 {
21294 EMSG(_(e_write));
21295 break;
21296 }
21297 }
21298 return ret;
21299}
21300
21301/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021302 * "writefile()" function
21303 */
21304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021305f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021306{
21307 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021308 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021309 char_u *fname;
21310 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021311 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021312
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021313 if (check_restricted() || check_secure())
21314 return;
21315
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021316 if (argvars[0].v_type != VAR_LIST)
21317 {
21318 EMSG2(_(e_listarg), "writefile()");
21319 return;
21320 }
21321 if (argvars[0].vval.v_list == NULL)
21322 return;
21323
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021324 if (argvars[2].v_type != VAR_UNKNOWN)
21325 {
21326 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21327 binary = TRUE;
21328 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21329 append = TRUE;
21330 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021331
21332 /* Always open the file in binary mode, library functions have a mind of
21333 * their own about CR-LF conversion. */
21334 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021335 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21336 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021337 {
21338 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21339 ret = -1;
21340 }
21341 else
21342 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021343 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21344 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021345 fclose(fd);
21346 }
21347
21348 rettv->vval.v_number = ret;
21349}
21350
21351/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021352 * "xor(expr, expr)" function
21353 */
21354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021355f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021356{
21357 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21358 ^ get_tv_number_chk(&argvars[1], NULL);
21359}
21360
21361
21362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021364 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 */
21366 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021367var2fpos(
21368 typval_T *varp,
21369 int dollar_lnum, /* TRUE when $ is last line */
21370 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021372 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021374 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375
Bram Moolenaara5525202006-03-02 22:52:09 +000021376 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021377 if (varp->v_type == VAR_LIST)
21378 {
21379 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021380 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021381 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021382 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021383
21384 l = varp->vval.v_list;
21385 if (l == NULL)
21386 return NULL;
21387
21388 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021389 pos.lnum = list_find_nr(l, 0L, &error);
21390 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021391 return NULL; /* invalid line number */
21392
21393 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021394 pos.col = list_find_nr(l, 1L, &error);
21395 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021396 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021397 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021398
21399 /* We accept "$" for the column number: last column. */
21400 li = list_find(l, 1L);
21401 if (li != NULL && li->li_tv.v_type == VAR_STRING
21402 && li->li_tv.vval.v_string != NULL
21403 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21404 pos.col = len + 1;
21405
Bram Moolenaara5525202006-03-02 22:52:09 +000021406 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021407 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021408 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021409 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021410
Bram Moolenaara5525202006-03-02 22:52:09 +000021411#ifdef FEAT_VIRTUALEDIT
21412 /* Get the virtual offset. Defaults to zero. */
21413 pos.coladd = list_find_nr(l, 2L, &error);
21414 if (error)
21415 pos.coladd = 0;
21416#endif
21417
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021418 return &pos;
21419 }
21420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021421 name = get_tv_string_chk(varp);
21422 if (name == NULL)
21423 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021424 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021426 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21427 {
21428 if (VIsual_active)
21429 return &VIsual;
21430 return &curwin->w_cursor;
21431 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021432 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021434 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21436 return NULL;
21437 return pp;
21438 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021439
21440#ifdef FEAT_VIRTUALEDIT
21441 pos.coladd = 0;
21442#endif
21443
Bram Moolenaar477933c2007-07-17 14:32:23 +000021444 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021445 {
21446 pos.col = 0;
21447 if (name[1] == '0') /* "w0": first visible line */
21448 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021449 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021450 pos.lnum = curwin->w_topline;
21451 return &pos;
21452 }
21453 else if (name[1] == '$') /* "w$": last visible line */
21454 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021455 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021456 pos.lnum = curwin->w_botline - 1;
21457 return &pos;
21458 }
21459 }
21460 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021462 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 {
21464 pos.lnum = curbuf->b_ml.ml_line_count;
21465 pos.col = 0;
21466 }
21467 else
21468 {
21469 pos.lnum = curwin->w_cursor.lnum;
21470 pos.col = (colnr_T)STRLEN(ml_get_curline());
21471 }
21472 return &pos;
21473 }
21474 return NULL;
21475}
21476
21477/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021478 * Convert list in "arg" into a position and optional file number.
21479 * When "fnump" is NULL there is no file number, only 3 items.
21480 * Note that the column is passed on as-is, the caller may want to decrement
21481 * it to use 1 for the first column.
21482 * Return FAIL when conversion is not possible, doesn't check the position for
21483 * validity.
21484 */
21485 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021486list2fpos(
21487 typval_T *arg,
21488 pos_T *posp,
21489 int *fnump,
21490 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021491{
21492 list_T *l = arg->vval.v_list;
21493 long i = 0;
21494 long n;
21495
Bram Moolenaar493c1782014-05-28 14:34:46 +020021496 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21497 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021498 if (arg->v_type != VAR_LIST
21499 || l == NULL
21500 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021501 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021502 return FAIL;
21503
21504 if (fnump != NULL)
21505 {
21506 n = list_find_nr(l, i++, NULL); /* fnum */
21507 if (n < 0)
21508 return FAIL;
21509 if (n == 0)
21510 n = curbuf->b_fnum; /* current buffer */
21511 *fnump = n;
21512 }
21513
21514 n = list_find_nr(l, i++, NULL); /* lnum */
21515 if (n < 0)
21516 return FAIL;
21517 posp->lnum = n;
21518
21519 n = list_find_nr(l, i++, NULL); /* col */
21520 if (n < 0)
21521 return FAIL;
21522 posp->col = n;
21523
21524#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021525 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021526 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021527 posp->coladd = 0;
21528 else
21529 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021530#endif
21531
Bram Moolenaar493c1782014-05-28 14:34:46 +020021532 if (curswantp != NULL)
21533 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21534
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021535 return OK;
21536}
21537
21538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 * Get the length of an environment variable name.
21540 * Advance "arg" to the first character after the name.
21541 * Return 0 for error.
21542 */
21543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021544get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545{
21546 char_u *p;
21547 int len;
21548
21549 for (p = *arg; vim_isIDc(*p); ++p)
21550 ;
21551 if (p == *arg) /* no name found */
21552 return 0;
21553
21554 len = (int)(p - *arg);
21555 *arg = p;
21556 return len;
21557}
21558
21559/*
21560 * Get the length of the name of a function or internal variable.
21561 * "arg" is advanced to the first non-white character after the name.
21562 * Return 0 if something is wrong.
21563 */
21564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021565get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566{
21567 char_u *p;
21568 int len;
21569
21570 /* Find the end of the name. */
21571 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021572 {
21573 if (*p == ':')
21574 {
21575 /* "s:" is start of "s:var", but "n:" is not and can be used in
21576 * slice "[n:]". Also "xx:" is not a namespace. */
21577 len = (int)(p - *arg);
21578 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21579 || len > 1)
21580 break;
21581 }
21582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 if (p == *arg) /* no name found */
21584 return 0;
21585
21586 len = (int)(p - *arg);
21587 *arg = skipwhite(p);
21588
21589 return len;
21590}
21591
21592/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021593 * Get the length of the name of a variable or function.
21594 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021596 * Return -1 if curly braces expansion failed.
21597 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 * If the name contains 'magic' {}'s, expand them and return the
21599 * expanded name in an allocated string via 'alias' - caller must free.
21600 */
21601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021602get_name_len(
21603 char_u **arg,
21604 char_u **alias,
21605 int evaluate,
21606 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607{
21608 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 char_u *p;
21610 char_u *expr_start;
21611 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612
21613 *alias = NULL; /* default to no alias */
21614
21615 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21616 && (*arg)[2] == (int)KE_SNR)
21617 {
21618 /* hard coded <SNR>, already translated */
21619 *arg += 3;
21620 return get_id_len(arg) + 3;
21621 }
21622 len = eval_fname_script(*arg);
21623 if (len > 0)
21624 {
21625 /* literal "<SID>", "s:" or "<SNR>" */
21626 *arg += len;
21627 }
21628
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021630 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021632 p = find_name_end(*arg, &expr_start, &expr_end,
21633 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 if (expr_start != NULL)
21635 {
21636 char_u *temp_string;
21637
21638 if (!evaluate)
21639 {
21640 len += (int)(p - *arg);
21641 *arg = skipwhite(p);
21642 return len;
21643 }
21644
21645 /*
21646 * Include any <SID> etc in the expanded string:
21647 * Thus the -len here.
21648 */
21649 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21650 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021651 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 *alias = temp_string;
21653 *arg = skipwhite(p);
21654 return (int)STRLEN(temp_string);
21655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656
21657 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021658 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 EMSG2(_(e_invexpr2), *arg);
21660
21661 return len;
21662}
21663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021664/*
21665 * Find the end of a variable or function name, taking care of magic braces.
21666 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21667 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021668 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021669 * Return a pointer to just after the name. Equal to "arg" if there is no
21670 * valid name.
21671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021673find_name_end(
21674 char_u *arg,
21675 char_u **expr_start,
21676 char_u **expr_end,
21677 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021679 int mb_nest = 0;
21680 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021682 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021684 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021686 *expr_start = NULL;
21687 *expr_end = NULL;
21688 }
21689
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021690 /* Quick check for valid starting character. */
21691 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21692 return arg;
21693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021694 for (p = arg; *p != NUL
21695 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021696 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021697 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021698 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021699 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021700 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021701 if (*p == '\'')
21702 {
21703 /* skip over 'string' to avoid counting [ and ] inside it. */
21704 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21705 ;
21706 if (*p == NUL)
21707 break;
21708 }
21709 else if (*p == '"')
21710 {
21711 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21712 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21713 if (*p == '\\' && p[1] != NUL)
21714 ++p;
21715 if (*p == NUL)
21716 break;
21717 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021718 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21719 {
21720 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021721 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021722 len = (int)(p - arg);
21723 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021724 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021725 break;
21726 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021728 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021730 if (*p == '[')
21731 ++br_nest;
21732 else if (*p == ']')
21733 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021736 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021738 if (*p == '{')
21739 {
21740 mb_nest++;
21741 if (expr_start != NULL && *expr_start == NULL)
21742 *expr_start = p;
21743 }
21744 else if (*p == '}')
21745 {
21746 mb_nest--;
21747 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21748 *expr_end = p;
21749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751 }
21752
21753 return p;
21754}
21755
21756/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021757 * Expands out the 'magic' {}'s in a variable/function name.
21758 * Note that this can call itself recursively, to deal with
21759 * constructs like foo{bar}{baz}{bam}
21760 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21761 * "in_start" ^
21762 * "expr_start" ^
21763 * "expr_end" ^
21764 * "in_end" ^
21765 *
21766 * Returns a new allocated string, which the caller must free.
21767 * Returns NULL for failure.
21768 */
21769 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021770make_expanded_name(
21771 char_u *in_start,
21772 char_u *expr_start,
21773 char_u *expr_end,
21774 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021775{
21776 char_u c1;
21777 char_u *retval = NULL;
21778 char_u *temp_result;
21779 char_u *nextcmd = NULL;
21780
21781 if (expr_end == NULL || in_end == NULL)
21782 return NULL;
21783 *expr_start = NUL;
21784 *expr_end = NUL;
21785 c1 = *in_end;
21786 *in_end = NUL;
21787
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021788 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021789 if (temp_result != NULL && nextcmd == NULL)
21790 {
21791 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21792 + (in_end - expr_end) + 1));
21793 if (retval != NULL)
21794 {
21795 STRCPY(retval, in_start);
21796 STRCAT(retval, temp_result);
21797 STRCAT(retval, expr_end + 1);
21798 }
21799 }
21800 vim_free(temp_result);
21801
21802 *in_end = c1; /* put char back for error messages */
21803 *expr_start = '{';
21804 *expr_end = '}';
21805
21806 if (retval != NULL)
21807 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021808 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021809 if (expr_start != NULL)
21810 {
21811 /* Further expansion! */
21812 temp_result = make_expanded_name(retval, expr_start,
21813 expr_end, temp_result);
21814 vim_free(retval);
21815 retval = temp_result;
21816 }
21817 }
21818
21819 return retval;
21820}
21821
21822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021824 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825 */
21826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021827eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021829 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21830}
21831
21832/*
21833 * Return TRUE if character "c" can be used as the first character in a
21834 * variable or function name (excluding '{' and '}').
21835 */
21836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021837eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021838{
21839 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840}
21841
21842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 * Set number v: variable to "val".
21844 */
21845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021846set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021848 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849}
21850
21851/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021852 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 */
21854 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021855get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021857 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858}
21859
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021860/*
21861 * Get string v: variable value. Uses a static buffer, can only be used once.
21862 */
21863 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021864get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021865{
21866 return get_tv_string(&vimvars[idx].vv_tv);
21867}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021868
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021870 * Get List v: variable value. Caller must take care of reference count when
21871 * needed.
21872 */
21873 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021874get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021875{
21876 return vimvars[idx].vv_list;
21877}
21878
21879/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021880 * Set v:char to character "c".
21881 */
21882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021883set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021884{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021885 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021886
21887#ifdef FEAT_MBYTE
21888 if (has_mbyte)
21889 buf[(*mb_char2bytes)(c, buf)] = NUL;
21890 else
21891#endif
21892 {
21893 buf[0] = c;
21894 buf[1] = NUL;
21895 }
21896 set_vim_var_string(VV_CHAR, buf, -1);
21897}
21898
21899/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021900 * Set v:count to "count" and v:count1 to "count1".
21901 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 */
21903 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021904set_vcount(
21905 long count,
21906 long count1,
21907 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021909 if (set_prevcount)
21910 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021911 vimvars[VV_COUNT].vv_nr = count;
21912 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913}
21914
21915/*
21916 * Set string v: variable to a copy of "val".
21917 */
21918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021919set_vim_var_string(
21920 int idx,
21921 char_u *val,
21922 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923{
Bram Moolenaara542c682016-01-31 16:28:04 +010021924 clear_tv(&vimvars[idx].vv_di.di_tv);
21925 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021927 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021929 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021931 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932}
21933
21934/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021935 * Set List v: variable to "val".
21936 */
21937 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021938set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021939{
Bram Moolenaara542c682016-01-31 16:28:04 +010021940 clear_tv(&vimvars[idx].vv_di.di_tv);
21941 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021942 vimvars[idx].vv_list = val;
21943 if (val != NULL)
21944 ++val->lv_refcount;
21945}
21946
21947/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021948 * Set Dictionary v: variable to "val".
21949 */
21950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021951set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021952{
21953 int todo;
21954 hashitem_T *hi;
21955
Bram Moolenaara542c682016-01-31 16:28:04 +010021956 clear_tv(&vimvars[idx].vv_di.di_tv);
21957 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021958 vimvars[idx].vv_dict = val;
21959 if (val != NULL)
21960 {
21961 ++val->dv_refcount;
21962
21963 /* Set readonly */
21964 todo = (int)val->dv_hashtab.ht_used;
21965 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21966 {
21967 if (HASHITEM_EMPTY(hi))
21968 continue;
21969 --todo;
21970 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21971 }
21972 }
21973}
21974
21975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 * Set v:register if needed.
21977 */
21978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021979set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980{
21981 char_u regname;
21982
21983 if (c == 0 || c == ' ')
21984 regname = '"';
21985 else
21986 regname = c;
21987 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021988 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 set_vim_var_string(VV_REG, &regname, 1);
21990}
21991
21992/*
21993 * Get or set v:exception. If "oldval" == NULL, return the current value.
21994 * Otherwise, restore the value to "oldval" and return NULL.
21995 * Must always be called in pairs to save and restore v:exception! Does not
21996 * take care of memory allocations.
21997 */
21998 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021999v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000{
22001 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022002 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003
Bram Moolenaare9a41262005-01-15 22:18:47 +000022004 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 return NULL;
22006}
22007
22008/*
22009 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22010 * Otherwise, restore the value to "oldval" and return NULL.
22011 * Must always be called in pairs to save and restore v:throwpoint! Does not
22012 * take care of memory allocations.
22013 */
22014 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022015v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016{
22017 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022018 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019
Bram Moolenaare9a41262005-01-15 22:18:47 +000022020 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 return NULL;
22022}
22023
22024#if defined(FEAT_AUTOCMD) || defined(PROTO)
22025/*
22026 * Set v:cmdarg.
22027 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22028 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22029 * Must always be called in pairs!
22030 */
22031 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022032set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033{
22034 char_u *oldval;
22035 char_u *newval;
22036 unsigned len;
22037
Bram Moolenaare9a41262005-01-15 22:18:47 +000022038 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022039 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022041 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022042 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022043 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 }
22045
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022046 if (eap->force_bin == FORCE_BIN)
22047 len = 6;
22048 else if (eap->force_bin == FORCE_NOBIN)
22049 len = 8;
22050 else
22051 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022052
22053 if (eap->read_edit)
22054 len += 7;
22055
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022056 if (eap->force_ff != 0)
22057 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22058# ifdef FEAT_MBYTE
22059 if (eap->force_enc != 0)
22060 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022061 if (eap->bad_char != 0)
22062 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022063# endif
22064
22065 newval = alloc(len + 1);
22066 if (newval == NULL)
22067 return NULL;
22068
22069 if (eap->force_bin == FORCE_BIN)
22070 sprintf((char *)newval, " ++bin");
22071 else if (eap->force_bin == FORCE_NOBIN)
22072 sprintf((char *)newval, " ++nobin");
22073 else
22074 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022075
22076 if (eap->read_edit)
22077 STRCAT(newval, " ++edit");
22078
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022079 if (eap->force_ff != 0)
22080 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22081 eap->cmd + eap->force_ff);
22082# ifdef FEAT_MBYTE
22083 if (eap->force_enc != 0)
22084 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22085 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022086 if (eap->bad_char == BAD_KEEP)
22087 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22088 else if (eap->bad_char == BAD_DROP)
22089 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22090 else if (eap->bad_char != 0)
22091 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022092# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022093 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022094 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095}
22096#endif
22097
22098/*
22099 * Get the value of internal variable "name".
22100 * Return OK or FAIL.
22101 */
22102 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022103get_var_tv(
22104 char_u *name,
22105 int len, /* length of "name" */
22106 typval_T *rettv, /* NULL when only checking existence */
22107 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22108 int verbose, /* may give error message */
22109 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110{
22111 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022112 typval_T *tv = NULL;
22113 typval_T atv;
22114 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116
22117 /* truncate the name, so that we can use strcmp() */
22118 cc = name[len];
22119 name[len] = NUL;
22120
22121 /*
22122 * Check for "b:changedtick".
22123 */
22124 if (STRCMP(name, "b:changedtick") == 0)
22125 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022126 atv.v_type = VAR_NUMBER;
22127 atv.vval.v_number = curbuf->b_changedtick;
22128 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 }
22130
22131 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 * Check for user-defined variables.
22133 */
22134 else
22135 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022136 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022139 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022140 if (dip != NULL)
22141 *dip = v;
22142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 }
22144
Bram Moolenaare9a41262005-01-15 22:18:47 +000022145 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022147 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022148 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 ret = FAIL;
22150 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022151 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022152 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153
22154 name[len] = cc;
22155
22156 return ret;
22157}
22158
22159/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022160 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22161 * Also handle function call with Funcref variable: func(expr)
22162 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22163 */
22164 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022165handle_subscript(
22166 char_u **arg,
22167 typval_T *rettv,
22168 int evaluate, /* do more than finding the end */
22169 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022170{
22171 int ret = OK;
22172 dict_T *selfdict = NULL;
22173 char_u *s;
22174 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022175 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022176
22177 while (ret == OK
22178 && (**arg == '['
22179 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022180 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22181 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022182 && !vim_iswhite(*(*arg - 1)))
22183 {
22184 if (**arg == '(')
22185 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022186 partial_T *pt = NULL;
22187
Bram Moolenaard9fba312005-06-26 22:34:35 +000022188 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022189 if (evaluate)
22190 {
22191 functv = *rettv;
22192 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022193
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022194 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022195 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022196 {
22197 pt = functv.vval.v_partial;
22198 s = pt->pt_name;
22199 }
22200 else
22201 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022202 }
22203 else
22204 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022205 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022206 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022207 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022208
22209 /* Clear the funcref afterwards, so that deleting it while
22210 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022211 if (evaluate)
22212 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022213
22214 /* Stop the expression evaluation when immediately aborting on
22215 * error, or when an interrupt occurred or an exception was thrown
22216 * but not caught. */
22217 if (aborting())
22218 {
22219 if (ret == OK)
22220 clear_tv(rettv);
22221 ret = FAIL;
22222 }
22223 dict_unref(selfdict);
22224 selfdict = NULL;
22225 }
22226 else /* **arg == '[' || **arg == '.' */
22227 {
22228 dict_unref(selfdict);
22229 if (rettv->v_type == VAR_DICT)
22230 {
22231 selfdict = rettv->vval.v_dict;
22232 if (selfdict != NULL)
22233 ++selfdict->dv_refcount;
22234 }
22235 else
22236 selfdict = NULL;
22237 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22238 {
22239 clear_tv(rettv);
22240 ret = FAIL;
22241 }
22242 }
22243 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022244
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022245 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
22246 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022247 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022248 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22249 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022250 char_u *tofree = NULL;
22251 ufunc_T *fp;
22252 char_u fname_buf[FLEN_FIXED + 1];
22253 int error;
22254
22255 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022256 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022257 fp = find_func(fname);
22258 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022259
22260 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010022261 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022262 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022263 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22264
22265 if (pt != NULL)
22266 {
22267 pt->pt_refcount = 1;
22268 pt->pt_dict = selfdict;
22269 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022270 if (rettv->v_type == VAR_FUNC)
22271 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022272 /* Just a function: Take over the function name and use
22273 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022274 pt->pt_name = rettv->vval.v_string;
22275 }
22276 else
22277 {
22278 partial_T *ret_pt = rettv->vval.v_partial;
22279 int i;
22280
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022281 /* Partial: copy the function name, use selfdict and copy
22282 * args. Can't take over name or args, the partial might
22283 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022284 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022285 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022286 if (ret_pt->pt_argc > 0)
22287 {
22288 pt->pt_argv = (typval_T *)alloc(
22289 sizeof(typval_T) * ret_pt->pt_argc);
22290 if (pt->pt_argv == NULL)
22291 /* out of memory: drop the arguments */
22292 pt->pt_argc = 0;
22293 else
22294 {
22295 pt->pt_argc = ret_pt->pt_argc;
22296 for (i = 0; i < pt->pt_argc; i++)
22297 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22298 }
22299 }
22300 partial_unref(ret_pt);
22301 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022302 rettv->v_type = VAR_PARTIAL;
22303 rettv->vval.v_partial = pt;
22304 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022305 }
22306 }
22307
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022308 dict_unref(selfdict);
22309 return ret;
22310}
22311
22312/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022313 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022314 * value).
22315 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022316 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022317alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022318{
Bram Moolenaar33570922005-01-25 22:26:29 +000022319 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022320}
22321
22322/*
22323 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 * The string "s" must have been allocated, it is consumed.
22325 * Return NULL for out of memory, the variable otherwise.
22326 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022328alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329{
Bram Moolenaar33570922005-01-25 22:26:29 +000022330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022332 rettv = alloc_tv();
22333 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022335 rettv->v_type = VAR_STRING;
22336 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 }
22338 else
22339 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022340 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341}
22342
22343/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022344 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022347free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348{
22349 if (varp != NULL)
22350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022351 switch (varp->v_type)
22352 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022353 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022354 func_unref(varp->vval.v_string);
22355 /*FALLTHROUGH*/
22356 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022357 vim_free(varp->vval.v_string);
22358 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022359 case VAR_PARTIAL:
22360 partial_unref(varp->vval.v_partial);
22361 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022362 case VAR_LIST:
22363 list_unref(varp->vval.v_list);
22364 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022365 case VAR_DICT:
22366 dict_unref(varp->vval.v_dict);
22367 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022368 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022369#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022370 job_unref(varp->vval.v_job);
22371 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022372#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022373 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022374#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022375 channel_unref(varp->vval.v_channel);
22376 break;
22377#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022378 case VAR_NUMBER:
22379 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022380 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022381 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022382 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 vim_free(varp);
22385 }
22386}
22387
22388/*
22389 * Free the memory for a variable value and set the value to NULL or 0.
22390 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022392clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393{
22394 if (varp != NULL)
22395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022396 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022398 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022399 func_unref(varp->vval.v_string);
22400 /*FALLTHROUGH*/
22401 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022402 vim_free(varp->vval.v_string);
22403 varp->vval.v_string = NULL;
22404 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022405 case VAR_PARTIAL:
22406 partial_unref(varp->vval.v_partial);
22407 varp->vval.v_partial = NULL;
22408 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022409 case VAR_LIST:
22410 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022411 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022412 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022413 case VAR_DICT:
22414 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022415 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022416 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022417 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022418 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022419 varp->vval.v_number = 0;
22420 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022421 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022422#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022423 varp->vval.v_float = 0.0;
22424 break;
22425#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022426 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022427#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022428 job_unref(varp->vval.v_job);
22429 varp->vval.v_job = NULL;
22430#endif
22431 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022432 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022433#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022434 channel_unref(varp->vval.v_channel);
22435 varp->vval.v_channel = NULL;
22436#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022437 case VAR_UNKNOWN:
22438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022440 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441 }
22442}
22443
22444/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022445 * Set the value of a variable to NULL without freeing items.
22446 */
22447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022448init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022449{
22450 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022451 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022452}
22453
22454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 * Get the number value of a variable.
22456 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022457 * For incompatible types, return 0.
22458 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22459 * caller of incompatible types: it sets *denote to TRUE if "denote"
22460 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022462 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022463get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022465 int error = FALSE;
22466
22467 return get_tv_number_chk(varp, &error); /* return 0L on error */
22468}
22469
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022470 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022471get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022472{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022473 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022475 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022477 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022478 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022479 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022480#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022481 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022482 break;
22483#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022484 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022485 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022486 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022487 break;
22488 case VAR_STRING:
22489 if (varp->vval.v_string != NULL)
22490 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022491 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022492 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022493 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022494 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022495 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022496 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022497 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022498 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022499 case VAR_SPECIAL:
22500 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22501 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022502 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022504 EMSG(_("E910: Using a Job as a Number"));
22505 break;
22506#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022507 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022508#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022509 EMSG(_("E913: Using a Channel as a Number"));
22510 break;
22511#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022512 case VAR_UNKNOWN:
22513 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022514 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022516 if (denote == NULL) /* useful for values that must be unsigned */
22517 n = -1;
22518 else
22519 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022520 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521}
22522
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022523#ifdef FEAT_FLOAT
22524 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022525get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022526{
22527 switch (varp->v_type)
22528 {
22529 case VAR_NUMBER:
22530 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022531 case VAR_FLOAT:
22532 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022533 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022534 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022535 EMSG(_("E891: Using a Funcref as a Float"));
22536 break;
22537 case VAR_STRING:
22538 EMSG(_("E892: Using a String as a Float"));
22539 break;
22540 case VAR_LIST:
22541 EMSG(_("E893: Using a List as a Float"));
22542 break;
22543 case VAR_DICT:
22544 EMSG(_("E894: Using a Dictionary as a Float"));
22545 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022546 case VAR_SPECIAL:
22547 EMSG(_("E907: Using a special value as a Float"));
22548 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022549 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022550# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022551 EMSG(_("E911: Using a Job as a Float"));
22552 break;
22553# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022554 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022555# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022556 EMSG(_("E914: Using a Channel as a Float"));
22557 break;
22558# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022559 case VAR_UNKNOWN:
22560 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022561 break;
22562 }
22563 return 0;
22564}
22565#endif
22566
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022568 * Get the lnum from the first argument.
22569 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022570 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 */
22572 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022573get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574{
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 linenr_T lnum;
22577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022578 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 if (lnum == 0) /* no valid number, try using line() */
22580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022581 rettv.v_type = VAR_NUMBER;
22582 f_line(argvars, &rettv);
22583 lnum = rettv.vval.v_number;
22584 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 }
22586 return lnum;
22587}
22588
22589/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022590 * Get the lnum from the first argument.
22591 * Also accepts "$", then "buf" is used.
22592 * Returns 0 on error.
22593 */
22594 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022595get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022596{
22597 if (argvars[0].v_type == VAR_STRING
22598 && argvars[0].vval.v_string != NULL
22599 && argvars[0].vval.v_string[0] == '$'
22600 && buf != NULL)
22601 return buf->b_ml.ml_line_count;
22602 return get_tv_number_chk(&argvars[0], NULL);
22603}
22604
22605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606 * Get the string value of a variable.
22607 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022608 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22609 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 * If the String variable has never been set, return an empty string.
22611 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022612 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22613 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022615 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022616get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617{
22618 static char_u mybuf[NUMBUFLEN];
22619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022620 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621}
22622
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022623 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022624get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022626 char_u *res = get_tv_string_buf_chk(varp, buf);
22627
22628 return res != NULL ? res : (char_u *)"";
22629}
22630
Bram Moolenaar7d647822014-04-05 21:28:56 +020022631/*
22632 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22633 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022634 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022635get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022636{
22637 static char_u mybuf[NUMBUFLEN];
22638
22639 return get_tv_string_buf_chk(varp, mybuf);
22640}
22641
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022642 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022643get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022644{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022645 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022647 case VAR_NUMBER:
22648 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22649 return buf;
22650 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022651 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022652 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022653 break;
22654 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022655 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022656 break;
22657 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022658 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022659 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022660 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022661#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022662 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022663 break;
22664#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022665 case VAR_STRING:
22666 if (varp->vval.v_string != NULL)
22667 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022668 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022669 case VAR_SPECIAL:
22670 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22671 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022672 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022673#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022674 {
22675 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022676 char *status;
22677
22678 if (job == NULL)
22679 return (char_u *)"no process";
22680 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022681 : job->jv_status == JOB_ENDED ? "dead"
22682 : "run";
22683# ifdef UNIX
22684 vim_snprintf((char *)buf, NUMBUFLEN,
22685 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022686# elif defined(WIN32)
22687 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022688 "process %ld %s",
22689 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022690 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022691# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022692 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022693 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22694# endif
22695 return buf;
22696 }
22697#endif
22698 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022699 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022700#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022701 {
22702 channel_T *channel = varp->vval.v_channel;
22703 char *status = channel_status(channel);
22704
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022705 if (channel == NULL)
22706 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22707 else
22708 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022709 "channel %d %s", channel->ch_id, status);
22710 return buf;
22711 }
22712#endif
22713 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022714 case VAR_UNKNOWN:
22715 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022716 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022718 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719}
22720
22721/*
22722 * Find variable "name" in the list of variables.
22723 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022724 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022725 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022726 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022728 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022729find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022732 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733
Bram Moolenaara7043832005-01-21 11:56:39 +000022734 ht = find_var_ht(name, &varname);
22735 if (htp != NULL)
22736 *htp = ht;
22737 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022739 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740}
22741
22742/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022743 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022744 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022746 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022747find_var_in_ht(
22748 hashtab_T *ht,
22749 int htname,
22750 char_u *varname,
22751 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022752{
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 hashitem_T *hi;
22754
22755 if (*varname == NUL)
22756 {
22757 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022758 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022760 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022761 case 'g': return &globvars_var;
22762 case 'v': return &vimvars_var;
22763 case 'b': return &curbuf->b_bufvar;
22764 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022765#ifdef FEAT_WINDOWS
22766 case 't': return &curtab->tp_winvar;
22767#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022768 case 'l': return current_funccal == NULL
22769 ? NULL : &current_funccal->l_vars_var;
22770 case 'a': return current_funccal == NULL
22771 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022772 }
22773 return NULL;
22774 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022775
22776 hi = hash_find(ht, varname);
22777 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022778 {
22779 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022780 * worked find the variable again. Don't auto-load a script if it was
22781 * loaded already, otherwise it would be loaded every time when
22782 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022783 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022784 {
22785 /* Note: script_autoload() may make "hi" invalid. It must either
22786 * be obtained again or not used. */
22787 if (!script_autoload(varname, FALSE) || aborting())
22788 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022789 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022790 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022791 if (HASHITEM_EMPTY(hi))
22792 return NULL;
22793 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022795}
22796
22797/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022798 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022799 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022800 * Set "varname" to the start of name without ':'.
22801 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022802 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022803find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022805 hashitem_T *hi;
22806
Bram Moolenaar73627d02015-08-11 15:46:09 +020022807 if (name[0] == NUL)
22808 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 if (name[1] != ':')
22810 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022811 /* The name must not start with a colon or #. */
22812 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813 return NULL;
22814 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022815
22816 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022817 hi = hash_find(&compat_hashtab, name);
22818 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022819 return &compat_hashtab;
22820
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022822 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022823 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 }
22825 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022826 if (*name == 'g') /* global variable */
22827 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022828 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22829 */
22830 if (vim_strchr(name + 2, ':') != NULL
22831 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022832 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022834 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022836 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022837#ifdef FEAT_WINDOWS
22838 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022839 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022840#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022841 if (*name == 'v') /* v: variable */
22842 return &vimvarht;
22843 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022844 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022845 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022846 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 if (*name == 's' /* script variable */
22848 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22849 return &SCRIPT_VARS(current_SID);
22850 return NULL;
22851}
22852
22853/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022854 * Get function call environment based on bactrace debug level
22855 */
22856 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022857get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022858{
22859 int i;
22860 funccall_T *funccal;
22861 funccall_T *temp_funccal;
22862
22863 funccal = current_funccal;
22864 if (debug_backtrace_level > 0)
22865 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022866 for (i = 0; i < debug_backtrace_level; i++)
22867 {
22868 temp_funccal = funccal->caller;
22869 if (temp_funccal)
22870 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022871 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022872 /* backtrace level overflow. reset to max */
22873 debug_backtrace_level = i;
22874 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022875 }
22876 return funccal;
22877}
22878
22879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022881 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 * Returns NULL when it doesn't exist.
22883 */
22884 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022885get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886{
Bram Moolenaar33570922005-01-25 22:26:29 +000022887 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022889 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 if (v == NULL)
22891 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022892 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893}
22894
22895/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022896 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 * sourcing this script and when executing functions defined in the script.
22898 */
22899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022900new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901{
Bram Moolenaara7043832005-01-21 11:56:39 +000022902 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022903 hashtab_T *ht;
22904 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022905
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22907 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022908 /* Re-allocating ga_data means that an ht_array pointing to
22909 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022910 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022911 for (i = 1; i <= ga_scripts.ga_len; ++i)
22912 {
22913 ht = &SCRIPT_VARS(i);
22914 if (ht->ht_mask == HT_INIT_SIZE - 1)
22915 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022916 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022917 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022918 }
22919
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 while (ga_scripts.ga_len < id)
22921 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022922 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022923 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022924 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 }
22927 }
22928}
22929
22930/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022931 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22932 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 */
22934 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022935init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936{
Bram Moolenaar33570922005-01-25 22:26:29 +000022937 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022938 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022939 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022940 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022941 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022942 dict_var->di_tv.vval.v_dict = dict;
22943 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022944 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022945 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22946 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947}
22948
22949/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022950 * Unreference a dictionary initialized by init_var_dict().
22951 */
22952 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022953unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022954{
22955 /* Now the dict needs to be freed if no one else is using it, go back to
22956 * normal reference counting. */
22957 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22958 dict_unref(dict);
22959}
22960
22961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022963 * Frees all allocated variables and the value they contain.
22964 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 */
22966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022967vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022968{
22969 vars_clear_ext(ht, TRUE);
22970}
22971
22972/*
22973 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22974 */
22975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022976vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977{
Bram Moolenaara7043832005-01-21 11:56:39 +000022978 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022979 hashitem_T *hi;
22980 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022983 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022984 for (hi = ht->ht_array; todo > 0; ++hi)
22985 {
22986 if (!HASHITEM_EMPTY(hi))
22987 {
22988 --todo;
22989
Bram Moolenaar33570922005-01-25 22:26:29 +000022990 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022991 * ht_array might change then. hash_clear() takes care of it
22992 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022993 v = HI2DI(hi);
22994 if (free_val)
22995 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022996 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022997 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022998 }
22999 }
23000 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023001 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002}
23003
Bram Moolenaara7043832005-01-21 11:56:39 +000023004/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023005 * Delete a variable from hashtab "ht" at item "hi".
23006 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023009delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010{
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023012
23013 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 clear_tv(&di->di_tv);
23015 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016}
23017
23018/*
23019 * List the value of one internal variable.
23020 */
23021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023022list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023024 char_u *tofree;
23025 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023026 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023027
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023028 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023029 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023030 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023031 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032}
23033
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023035list_one_var_a(
23036 char_u *prefix,
23037 char_u *name,
23038 int type,
23039 char_u *string,
23040 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041{
Bram Moolenaar31859182007-08-14 20:41:13 +000023042 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23043 msg_start();
23044 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 if (name != NULL) /* "a:" vars don't have a name stored */
23046 msg_puts(name);
23047 msg_putchar(' ');
23048 msg_advance(22);
23049 if (type == VAR_NUMBER)
23050 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023051 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023052 msg_putchar('*');
23053 else if (type == VAR_LIST)
23054 {
23055 msg_putchar('[');
23056 if (*string == '[')
23057 ++string;
23058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023059 else if (type == VAR_DICT)
23060 {
23061 msg_putchar('{');
23062 if (*string == '{')
23063 ++string;
23064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 else
23066 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023067
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023069
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023070 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023071 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023072 if (*first)
23073 {
23074 msg_clr_eos();
23075 *first = FALSE;
23076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077}
23078
23079/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023080 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 * If the variable already exists, the value is updated.
23082 * Otherwise the variable is created.
23083 */
23084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023085set_var(
23086 char_u *name,
23087 typval_T *tv,
23088 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089{
Bram Moolenaar33570922005-01-25 22:26:29 +000023090 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023092 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023094 ht = find_var_ht(name, &varname);
23095 if (ht == NULL || *varname == NUL)
23096 {
23097 EMSG2(_(e_illvar), name);
23098 return;
23099 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023100 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023101
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023102 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23103 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023104 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023105
Bram Moolenaar33570922005-01-25 22:26:29 +000023106 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023107 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023108 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023109 if (var_check_ro(v->di_flags, name, FALSE)
23110 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023111 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023112
23113 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023114 * Handle setting internal v: variables separately where needed to
23115 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023116 */
23117 if (ht == &vimvarht)
23118 {
23119 if (v->di_tv.v_type == VAR_STRING)
23120 {
23121 vim_free(v->di_tv.vval.v_string);
23122 if (copy || tv->v_type != VAR_STRING)
23123 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23124 else
23125 {
23126 /* Take over the string to avoid an extra alloc/free. */
23127 v->di_tv.vval.v_string = tv->vval.v_string;
23128 tv->vval.v_string = NULL;
23129 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023130 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023131 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023132 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023133 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023134 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023135 if (STRCMP(varname, "searchforward") == 0)
23136 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023137#ifdef FEAT_SEARCH_EXTRA
23138 else if (STRCMP(varname, "hlsearch") == 0)
23139 {
23140 no_hlsearch = !v->di_tv.vval.v_number;
23141 redraw_all_later(SOME_VALID);
23142 }
23143#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023144 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023145 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023146 else if (v->di_tv.v_type != tv->v_type)
23147 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023148 }
23149
23150 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 }
23152 else /* add a new variable */
23153 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023154 /* Can't add "v:" variable. */
23155 if (ht == &vimvarht)
23156 {
23157 EMSG2(_(e_illvar), name);
23158 return;
23159 }
23160
Bram Moolenaar92124a32005-06-17 22:03:40 +000023161 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023162 if (!valid_varname(varname))
23163 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023164
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023165 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23166 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023167 if (v == NULL)
23168 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023169 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023172 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 return;
23174 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023175 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023177
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023178 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023179 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023180 else
23181 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023182 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023183 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023184 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186}
23187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023188/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023189 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023190 * Also give an error message.
23191 */
23192 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023193var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023194{
23195 if (flags & DI_FLAGS_RO)
23196 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023197 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023198 return TRUE;
23199 }
23200 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23201 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023202 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023203 return TRUE;
23204 }
23205 return FALSE;
23206}
23207
23208/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023209 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23210 * Also give an error message.
23211 */
23212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023213var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023214{
23215 if (flags & DI_FLAGS_FIX)
23216 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023217 EMSG2(_("E795: Cannot delete variable %s"),
23218 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023219 return TRUE;
23220 }
23221 return FALSE;
23222}
23223
23224/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023225 * Check if a funcref is assigned to a valid variable name.
23226 * Return TRUE and give an error if not.
23227 */
23228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023229var_check_func_name(
23230 char_u *name, /* points to start of variable name */
23231 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023232{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023233 /* Allow for w: b: s: and t:. */
23234 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023235 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23236 ? name[2] : name[0]))
23237 {
23238 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23239 name);
23240 return TRUE;
23241 }
23242 /* Don't allow hiding a function. When "v" is not NULL we might be
23243 * assigning another function to the same var, the type is checked
23244 * below. */
23245 if (new_var && function_exists(name))
23246 {
23247 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23248 name);
23249 return TRUE;
23250 }
23251 return FALSE;
23252}
23253
23254/*
23255 * Check if a variable name is valid.
23256 * Return FALSE and give an error if not.
23257 */
23258 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023259valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023260{
23261 char_u *p;
23262
23263 for (p = varname; *p != NUL; ++p)
23264 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23265 && *p != AUTOLOAD_CHAR)
23266 {
23267 EMSG2(_(e_illvar), varname);
23268 return FALSE;
23269 }
23270 return TRUE;
23271}
23272
23273/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023274 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023275 * Also give an error message, using "name" or _("name") when use_gettext is
23276 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023277 */
23278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023279tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023280{
23281 if (lock & VAR_LOCKED)
23282 {
23283 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023284 name == NULL ? (char_u *)_("Unknown")
23285 : use_gettext ? (char_u *)_(name)
23286 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023287 return TRUE;
23288 }
23289 if (lock & VAR_FIXED)
23290 {
23291 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023292 name == NULL ? (char_u *)_("Unknown")
23293 : use_gettext ? (char_u *)_(name)
23294 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023295 return TRUE;
23296 }
23297 return FALSE;
23298}
23299
23300/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023301 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023302 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023303 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023304 * It is OK for "from" and "to" to point to the same item. This is used to
23305 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023306 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023308copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023310 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023311 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023312 switch (from->v_type)
23313 {
23314 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023315 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023316 to->vval.v_number = from->vval.v_number;
23317 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023318 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023319#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023320 to->vval.v_float = from->vval.v_float;
23321 break;
23322#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023323 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023324#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023325 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023326 if (to->vval.v_job != NULL)
23327 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023328 break;
23329#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023330 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023331#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023332 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023333 if (to->vval.v_channel != NULL)
23334 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023335 break;
23336#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023337 case VAR_STRING:
23338 case VAR_FUNC:
23339 if (from->vval.v_string == NULL)
23340 to->vval.v_string = NULL;
23341 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023342 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023343 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023344 if (from->v_type == VAR_FUNC)
23345 func_ref(to->vval.v_string);
23346 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023347 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023348 case VAR_PARTIAL:
23349 if (from->vval.v_partial == NULL)
23350 to->vval.v_partial = NULL;
23351 else
23352 {
23353 to->vval.v_partial = from->vval.v_partial;
23354 ++to->vval.v_partial->pt_refcount;
23355 }
23356 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023357 case VAR_LIST:
23358 if (from->vval.v_list == NULL)
23359 to->vval.v_list = NULL;
23360 else
23361 {
23362 to->vval.v_list = from->vval.v_list;
23363 ++to->vval.v_list->lv_refcount;
23364 }
23365 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023366 case VAR_DICT:
23367 if (from->vval.v_dict == NULL)
23368 to->vval.v_dict = NULL;
23369 else
23370 {
23371 to->vval.v_dict = from->vval.v_dict;
23372 ++to->vval.v_dict->dv_refcount;
23373 }
23374 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023375 case VAR_UNKNOWN:
23376 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023377 break;
23378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379}
23380
23381/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023382 * Make a copy of an item.
23383 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023384 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23385 * reference to an already copied list/dict can be used.
23386 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023387 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023389item_copy(
23390 typval_T *from,
23391 typval_T *to,
23392 int deep,
23393 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023394{
23395 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023396 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023397
Bram Moolenaar33570922005-01-25 22:26:29 +000023398 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023399 {
23400 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023401 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023402 }
23403 ++recurse;
23404
23405 switch (from->v_type)
23406 {
23407 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023408 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023409 case VAR_STRING:
23410 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023411 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023412 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023413 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023414 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023415 copy_tv(from, to);
23416 break;
23417 case VAR_LIST:
23418 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023419 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023420 if (from->vval.v_list == NULL)
23421 to->vval.v_list = NULL;
23422 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23423 {
23424 /* use the copy made earlier */
23425 to->vval.v_list = from->vval.v_list->lv_copylist;
23426 ++to->vval.v_list->lv_refcount;
23427 }
23428 else
23429 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23430 if (to->vval.v_list == NULL)
23431 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023432 break;
23433 case VAR_DICT:
23434 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023435 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023436 if (from->vval.v_dict == NULL)
23437 to->vval.v_dict = NULL;
23438 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23439 {
23440 /* use the copy made earlier */
23441 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23442 ++to->vval.v_dict->dv_refcount;
23443 }
23444 else
23445 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23446 if (to->vval.v_dict == NULL)
23447 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023448 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023449 case VAR_UNKNOWN:
23450 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023451 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023452 }
23453 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023454 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023455}
23456
23457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 * ":echo expr1 ..." print each argument separated with a space, add a
23459 * newline at the end.
23460 * ":echon expr1 ..." print each argument plain.
23461 */
23462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023463ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464{
23465 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023466 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023467 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468 char_u *p;
23469 int needclr = TRUE;
23470 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023471 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472
23473 if (eap->skip)
23474 ++emsg_skip;
23475 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23476 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023477 /* If eval1() causes an error message the text from the command may
23478 * still need to be cleared. E.g., "echo 22,44". */
23479 need_clr_eos = needclr;
23480
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023482 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 {
23484 /*
23485 * Report the invalid expression unless the expression evaluation
23486 * has been cancelled due to an aborting error, an interrupt, or an
23487 * exception.
23488 */
23489 if (!aborting())
23490 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023491 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 break;
23493 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023494 need_clr_eos = FALSE;
23495
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 if (!eap->skip)
23497 {
23498 if (atstart)
23499 {
23500 atstart = FALSE;
23501 /* Call msg_start() after eval1(), evaluating the expression
23502 * may cause a message to appear. */
23503 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023504 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023505 /* Mark the saved text as finishing the line, so that what
23506 * follows is displayed on a new line when scrolling back
23507 * at the more prompt. */
23508 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 }
23512 else if (eap->cmdidx == CMD_echo)
23513 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023514 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023515 if (p != NULL)
23516 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023518 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023520 if (*p != TAB && needclr)
23521 {
23522 /* remove any text still there from the command */
23523 msg_clr_eos();
23524 needclr = FALSE;
23525 }
23526 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 }
23528 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023529 {
23530#ifdef FEAT_MBYTE
23531 if (has_mbyte)
23532 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023533 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023534
23535 (void)msg_outtrans_len_attr(p, i, echo_attr);
23536 p += i - 1;
23537 }
23538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023540 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023543 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023545 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 arg = skipwhite(arg);
23547 }
23548 eap->nextcmd = check_nextcmd(arg);
23549
23550 if (eap->skip)
23551 --emsg_skip;
23552 else
23553 {
23554 /* remove text that may still be there from the command */
23555 if (needclr)
23556 msg_clr_eos();
23557 if (eap->cmdidx == CMD_echo)
23558 msg_end();
23559 }
23560}
23561
23562/*
23563 * ":echohl {name}".
23564 */
23565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023566ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567{
23568 int id;
23569
23570 id = syn_name2id(eap->arg);
23571 if (id == 0)
23572 echo_attr = 0;
23573 else
23574 echo_attr = syn_id2attr(id);
23575}
23576
23577/*
23578 * ":execute expr1 ..." execute the result of an expression.
23579 * ":echomsg expr1 ..." Print a message
23580 * ":echoerr expr1 ..." Print an error
23581 * Each gets spaces around each argument and a newline at the end for
23582 * echo commands
23583 */
23584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023585ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586{
23587 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023588 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 int ret = OK;
23590 char_u *p;
23591 garray_T ga;
23592 int len;
23593 int save_did_emsg;
23594
23595 ga_init2(&ga, 1, 80);
23596
23597 if (eap->skip)
23598 ++emsg_skip;
23599 while (*arg != NUL && *arg != '|' && *arg != '\n')
23600 {
23601 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023602 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 {
23604 /*
23605 * Report the invalid expression unless the expression evaluation
23606 * has been cancelled due to an aborting error, an interrupt, or an
23607 * exception.
23608 */
23609 if (!aborting())
23610 EMSG2(_(e_invexpr2), p);
23611 ret = FAIL;
23612 break;
23613 }
23614
23615 if (!eap->skip)
23616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023617 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 len = (int)STRLEN(p);
23619 if (ga_grow(&ga, len + 2) == FAIL)
23620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023621 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 ret = FAIL;
23623 break;
23624 }
23625 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 ga.ga_len += len;
23629 }
23630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023631 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 arg = skipwhite(arg);
23633 }
23634
23635 if (ret != FAIL && ga.ga_data != NULL)
23636 {
23637 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023640 out_flush();
23641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 else if (eap->cmdidx == CMD_echoerr)
23643 {
23644 /* We don't want to abort following commands, restore did_emsg. */
23645 save_did_emsg = did_emsg;
23646 EMSG((char_u *)ga.ga_data);
23647 if (!force_abort)
23648 did_emsg = save_did_emsg;
23649 }
23650 else if (eap->cmdidx == CMD_execute)
23651 do_cmdline((char_u *)ga.ga_data,
23652 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23653 }
23654
23655 ga_clear(&ga);
23656
23657 if (eap->skip)
23658 --emsg_skip;
23659
23660 eap->nextcmd = check_nextcmd(arg);
23661}
23662
23663/*
23664 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23665 * "arg" points to the "&" or '+' when called, to "option" when returning.
23666 * Returns NULL when no option name found. Otherwise pointer to the char
23667 * after the option name.
23668 */
23669 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023670find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671{
23672 char_u *p = *arg;
23673
23674 ++p;
23675 if (*p == 'g' && p[1] == ':')
23676 {
23677 *opt_flags = OPT_GLOBAL;
23678 p += 2;
23679 }
23680 else if (*p == 'l' && p[1] == ':')
23681 {
23682 *opt_flags = OPT_LOCAL;
23683 p += 2;
23684 }
23685 else
23686 *opt_flags = 0;
23687
23688 if (!ASCII_ISALPHA(*p))
23689 return NULL;
23690 *arg = p;
23691
23692 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23693 p += 4; /* termcap option */
23694 else
23695 while (ASCII_ISALPHA(*p))
23696 ++p;
23697 return p;
23698}
23699
23700/*
23701 * ":function"
23702 */
23703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023704ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705{
23706 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023707 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708 int j;
23709 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023711 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712 char_u *name = NULL;
23713 char_u *p;
23714 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023715 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 garray_T newargs;
23717 garray_T newlines;
23718 int varargs = FALSE;
23719 int mustend = FALSE;
23720 int flags = 0;
23721 ufunc_T *fp;
23722 int indent;
23723 int nesting;
23724 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023725 dictitem_T *v;
23726 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023727 static int func_nr = 0; /* number for nameless function */
23728 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023729 hashtab_T *ht;
23730 int todo;
23731 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023732 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733
23734 /*
23735 * ":function" without argument: list functions.
23736 */
23737 if (ends_excmd(*eap->arg))
23738 {
23739 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023740 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023741 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023742 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023743 {
23744 if (!HASHITEM_EMPTY(hi))
23745 {
23746 --todo;
23747 fp = HI2UF(hi);
23748 if (!isdigit(*fp->uf_name))
23749 list_func_head(fp, FALSE);
23750 }
23751 }
23752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 eap->nextcmd = check_nextcmd(eap->arg);
23754 return;
23755 }
23756
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023757 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023758 * ":function /pat": list functions matching pattern.
23759 */
23760 if (*eap->arg == '/')
23761 {
23762 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23763 if (!eap->skip)
23764 {
23765 regmatch_T regmatch;
23766
23767 c = *p;
23768 *p = NUL;
23769 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23770 *p = c;
23771 if (regmatch.regprog != NULL)
23772 {
23773 regmatch.rm_ic = p_ic;
23774
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023775 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023776 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23777 {
23778 if (!HASHITEM_EMPTY(hi))
23779 {
23780 --todo;
23781 fp = HI2UF(hi);
23782 if (!isdigit(*fp->uf_name)
23783 && vim_regexec(&regmatch, fp->uf_name, 0))
23784 list_func_head(fp, FALSE);
23785 }
23786 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023787 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023788 }
23789 }
23790 if (*p == '/')
23791 ++p;
23792 eap->nextcmd = check_nextcmd(p);
23793 return;
23794 }
23795
23796 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023797 * Get the function name. There are these situations:
23798 * func normal function name
23799 * "name" == func, "fudi.fd_dict" == NULL
23800 * dict.func new dictionary entry
23801 * "name" == NULL, "fudi.fd_dict" set,
23802 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23803 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023804 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023805 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23806 * dict.func existing dict entry that's not a Funcref
23807 * "name" == NULL, "fudi.fd_dict" set,
23808 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023809 * s:func script-local function name
23810 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023813 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023814 paren = (vim_strchr(p, '(') != NULL);
23815 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 {
23817 /*
23818 * Return on an invalid expression in braces, unless the expression
23819 * evaluation has been cancelled due to an aborting error, an
23820 * interrupt, or an exception.
23821 */
23822 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023823 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023824 if (!eap->skip && fudi.fd_newkey != NULL)
23825 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023826 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 else
23830 eap->skip = TRUE;
23831 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023832
Bram Moolenaar071d4272004-06-13 20:20:40 +000023833 /* An error in a function call during evaluation of an expression in magic
23834 * braces should not cause the function not to be defined. */
23835 saved_did_emsg = did_emsg;
23836 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837
23838 /*
23839 * ":function func" with only function name: list function.
23840 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023841 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 {
23843 if (!ends_excmd(*skipwhite(p)))
23844 {
23845 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023846 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847 }
23848 eap->nextcmd = check_nextcmd(p);
23849 if (eap->nextcmd != NULL)
23850 *p = NUL;
23851 if (!eap->skip && !got_int)
23852 {
23853 fp = find_func(name);
23854 if (fp != NULL)
23855 {
23856 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023857 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023859 if (FUNCLINE(fp, j) == NULL)
23860 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861 msg_putchar('\n');
23862 msg_outnum((long)(j + 1));
23863 if (j < 9)
23864 msg_putchar(' ');
23865 if (j < 99)
23866 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023867 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 out_flush(); /* show a line at a time */
23869 ui_breakcheck();
23870 }
23871 if (!got_int)
23872 {
23873 msg_putchar('\n');
23874 msg_puts((char_u *)" endfunction");
23875 }
23876 }
23877 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023878 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023880 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881 }
23882
23883 /*
23884 * ":function name(arg1, arg2)" Define function.
23885 */
23886 p = skipwhite(p);
23887 if (*p != '(')
23888 {
23889 if (!eap->skip)
23890 {
23891 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023892 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893 }
23894 /* attempt to continue by skipping some text */
23895 if (vim_strchr(p, '(') != NULL)
23896 p = vim_strchr(p, '(');
23897 }
23898 p = skipwhite(p + 1);
23899
23900 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23901 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23902
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023903 if (!eap->skip)
23904 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023905 /* Check the name of the function. Unless it's a dictionary function
23906 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023907 if (name != NULL)
23908 arg = name;
23909 else
23910 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023911 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023912 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23913 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023914 {
23915 if (*arg == K_SPECIAL)
23916 j = 3;
23917 else
23918 j = 0;
23919 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23920 : eval_isnamec(arg[j])))
23921 ++j;
23922 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023923 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023924 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023925 /* Disallow using the g: dict. */
23926 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23927 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023928 }
23929
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930 /*
23931 * Isolate the arguments: "arg1, arg2, ...)"
23932 */
23933 while (*p != ')')
23934 {
23935 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23936 {
23937 varargs = TRUE;
23938 p += 3;
23939 mustend = TRUE;
23940 }
23941 else
23942 {
23943 arg = p;
23944 while (ASCII_ISALNUM(*p) || *p == '_')
23945 ++p;
23946 if (arg == p || isdigit(*arg)
23947 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23948 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23949 {
23950 if (!eap->skip)
23951 EMSG2(_("E125: Illegal argument: %s"), arg);
23952 break;
23953 }
23954 if (ga_grow(&newargs, 1) == FAIL)
23955 goto erret;
23956 c = *p;
23957 *p = NUL;
23958 arg = vim_strsave(arg);
23959 if (arg == NULL)
23960 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023961
23962 /* Check for duplicate argument name. */
23963 for (i = 0; i < newargs.ga_len; ++i)
23964 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23965 {
23966 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023967 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023968 goto erret;
23969 }
23970
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23972 *p = c;
23973 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 if (*p == ',')
23975 ++p;
23976 else
23977 mustend = TRUE;
23978 }
23979 p = skipwhite(p);
23980 if (mustend && *p != ')')
23981 {
23982 if (!eap->skip)
23983 EMSG2(_(e_invarg2), eap->arg);
23984 break;
23985 }
23986 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023987 if (*p != ')')
23988 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023989 ++p; /* skip the ')' */
23990
Bram Moolenaare9a41262005-01-15 22:18:47 +000023991 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 for (;;)
23993 {
23994 p = skipwhite(p);
23995 if (STRNCMP(p, "range", 5) == 0)
23996 {
23997 flags |= FC_RANGE;
23998 p += 5;
23999 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024000 else if (STRNCMP(p, "dict", 4) == 0)
24001 {
24002 flags |= FC_DICT;
24003 p += 4;
24004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 else if (STRNCMP(p, "abort", 5) == 0)
24006 {
24007 flags |= FC_ABORT;
24008 p += 5;
24009 }
24010 else
24011 break;
24012 }
24013
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024014 /* When there is a line break use what follows for the function body.
24015 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24016 if (*p == '\n')
24017 line_arg = p + 1;
24018 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 EMSG(_(e_trailing));
24020
24021 /*
24022 * Read the body of the function, until ":endfunction" is found.
24023 */
24024 if (KeyTyped)
24025 {
24026 /* Check if the function already exists, don't let the user type the
24027 * whole function before telling him it doesn't work! For a script we
24028 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024029 if (!eap->skip && !eap->forceit)
24030 {
24031 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24032 EMSG(_(e_funcdict));
24033 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024034 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024037 if (!eap->skip && did_emsg)
24038 goto erret;
24039
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 msg_putchar('\n'); /* don't overwrite the function name */
24041 cmdline_row = msg_row;
24042 }
24043
24044 indent = 2;
24045 nesting = 0;
24046 for (;;)
24047 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024048 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024049 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024050 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024051 saved_wait_return = FALSE;
24052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024054 sourcing_lnum_off = sourcing_lnum;
24055
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024056 if (line_arg != NULL)
24057 {
24058 /* Use eap->arg, split up in parts by line breaks. */
24059 theline = line_arg;
24060 p = vim_strchr(theline, '\n');
24061 if (p == NULL)
24062 line_arg += STRLEN(line_arg);
24063 else
24064 {
24065 *p = NUL;
24066 line_arg = p + 1;
24067 }
24068 }
24069 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 theline = getcmdline(':', 0L, indent);
24071 else
24072 theline = eap->getline(':', eap->cookie, indent);
24073 if (KeyTyped)
24074 lines_left = Rows - 1;
24075 if (theline == NULL)
24076 {
24077 EMSG(_("E126: Missing :endfunction"));
24078 goto erret;
24079 }
24080
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024081 /* Detect line continuation: sourcing_lnum increased more than one. */
24082 if (sourcing_lnum > sourcing_lnum_off + 1)
24083 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24084 else
24085 sourcing_lnum_off = 0;
24086
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 if (skip_until != NULL)
24088 {
24089 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24090 * don't check for ":endfunc". */
24091 if (STRCMP(theline, skip_until) == 0)
24092 {
24093 vim_free(skip_until);
24094 skip_until = NULL;
24095 }
24096 }
24097 else
24098 {
24099 /* skip ':' and blanks*/
24100 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24101 ;
24102
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024103 /* Check for "endfunction". */
24104 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024106 if (line_arg == NULL)
24107 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108 break;
24109 }
24110
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024111 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 * at "end". */
24113 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24114 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024115 else if (STRNCMP(p, "if", 2) == 0
24116 || STRNCMP(p, "wh", 2) == 0
24117 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 || STRNCMP(p, "try", 3) == 0)
24119 indent += 2;
24120
24121 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024122 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024124 if (*p == '!')
24125 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024127 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024128 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024130 ++nesting;
24131 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024132 }
24133 }
24134
24135 /* Check for ":append" or ":insert". */
24136 p = skip_range(p, NULL);
24137 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24138 || (p[0] == 'i'
24139 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24140 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24141 skip_until = vim_strsave((char_u *)".");
24142
24143 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24144 arg = skipwhite(skiptowhite(p));
24145 if (arg[0] == '<' && arg[1] =='<'
24146 && ((p[0] == 'p' && p[1] == 'y'
24147 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24148 || (p[0] == 'p' && p[1] == 'e'
24149 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24150 || (p[0] == 't' && p[1] == 'c'
24151 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024152 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24153 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24155 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024156 || (p[0] == 'm' && p[1] == 'z'
24157 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 ))
24159 {
24160 /* ":python <<" continues until a dot, like ":append" */
24161 p = skipwhite(arg + 2);
24162 if (*p == NUL)
24163 skip_until = vim_strsave((char_u *)".");
24164 else
24165 skip_until = vim_strsave(p);
24166 }
24167 }
24168
24169 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024170 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024171 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024172 if (line_arg == NULL)
24173 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024175 }
24176
24177 /* Copy the line to newly allocated memory. get_one_sourceline()
24178 * allocates 250 bytes per line, this saves 80% on average. The cost
24179 * is an extra alloc/free. */
24180 p = vim_strsave(theline);
24181 if (p != NULL)
24182 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024183 if (line_arg == NULL)
24184 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024185 theline = p;
24186 }
24187
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024188 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24189
24190 /* Add NULL lines for continuation lines, so that the line count is
24191 * equal to the index in the growarray. */
24192 while (sourcing_lnum_off-- > 0)
24193 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024194
24195 /* Check for end of eap->arg. */
24196 if (line_arg != NULL && *line_arg == NUL)
24197 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 }
24199
24200 /* Don't define the function when skipping commands or when an error was
24201 * detected. */
24202 if (eap->skip || did_emsg)
24203 goto erret;
24204
24205 /*
24206 * If there are no errors, add the function
24207 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024208 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024209 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024210 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024211 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024212 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024213 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024214 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024215 goto erret;
24216 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024217
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024218 fp = find_func(name);
24219 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024221 if (!eap->forceit)
24222 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024223 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024224 goto erret;
24225 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024226 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024227 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024228 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024229 name);
24230 goto erret;
24231 }
24232 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024233 ga_clear_strings(&(fp->uf_args));
24234 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024235 vim_free(name);
24236 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 }
24239 else
24240 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024241 char numbuf[20];
24242
24243 fp = NULL;
24244 if (fudi.fd_newkey == NULL && !eap->forceit)
24245 {
24246 EMSG(_(e_funcdict));
24247 goto erret;
24248 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024249 if (fudi.fd_di == NULL)
24250 {
24251 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024252 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024253 goto erret;
24254 }
24255 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024256 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024257 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024258
24259 /* Give the function a sequential number. Can only be used with a
24260 * Funcref! */
24261 vim_free(name);
24262 sprintf(numbuf, "%d", ++func_nr);
24263 name = vim_strsave((char_u *)numbuf);
24264 if (name == NULL)
24265 goto erret;
24266 }
24267
24268 if (fp == NULL)
24269 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024270 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024271 {
24272 int slen, plen;
24273 char_u *scriptname;
24274
24275 /* Check that the autoload name matches the script name. */
24276 j = FAIL;
24277 if (sourcing_name != NULL)
24278 {
24279 scriptname = autoload_name(name);
24280 if (scriptname != NULL)
24281 {
24282 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024283 plen = (int)STRLEN(p);
24284 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024285 if (slen > plen && fnamecmp(p,
24286 sourcing_name + slen - plen) == 0)
24287 j = OK;
24288 vim_free(scriptname);
24289 }
24290 }
24291 if (j == FAIL)
24292 {
24293 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24294 goto erret;
24295 }
24296 }
24297
24298 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 if (fp == NULL)
24300 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024301
24302 if (fudi.fd_dict != NULL)
24303 {
24304 if (fudi.fd_di == NULL)
24305 {
24306 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024307 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024308 if (fudi.fd_di == NULL)
24309 {
24310 vim_free(fp);
24311 goto erret;
24312 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024313 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24314 {
24315 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024316 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024317 goto erret;
24318 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024319 }
24320 else
24321 /* overwrite existing dict entry */
24322 clear_tv(&fudi.fd_di->di_tv);
24323 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024324 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024325 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024326 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024327
24328 /* behave like "dict" was used */
24329 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024330 }
24331
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024333 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024334 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24335 {
24336 vim_free(fp);
24337 goto erret;
24338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024340 fp->uf_args = newargs;
24341 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024342#ifdef FEAT_PROFILE
24343 fp->uf_tml_count = NULL;
24344 fp->uf_tml_total = NULL;
24345 fp->uf_tml_self = NULL;
24346 fp->uf_profiling = FALSE;
24347 if (prof_def_func())
24348 func_do_profile(fp);
24349#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024350 fp->uf_varargs = varargs;
24351 fp->uf_flags = flags;
24352 fp->uf_calls = 0;
24353 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024354 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355
24356erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 ga_clear_strings(&newargs);
24358 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024359ret_free:
24360 vim_free(skip_until);
24361 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024364 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365}
24366
24367/*
24368 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024369 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024371 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024372 * TFN_INT: internal function name OK
24373 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024374 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 * Advances "pp" to just after the function name (if no error).
24376 */
24377 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024378trans_function_name(
24379 char_u **pp,
24380 int skip, /* only find the end, don't evaluate */
24381 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024382 funcdict_T *fdp, /* return: info about dictionary used */
24383 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024385 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 char_u *start;
24387 char_u *end;
24388 int lead;
24389 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024390 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024391 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024392
24393 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024394 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024396
24397 /* Check for hard coded <SNR>: already translated function ID (from a user
24398 * command). */
24399 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24400 && (*pp)[2] == (int)KE_SNR)
24401 {
24402 *pp += 3;
24403 len = get_id_len(pp) + 3;
24404 return vim_strnsave(start, len);
24405 }
24406
24407 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24408 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024410 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024411 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024412
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024413 /* Note that TFN_ flags use the same values as GLV_ flags. */
24414 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024415 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024416 if (end == start)
24417 {
24418 if (!skip)
24419 EMSG(_("E129: Function name required"));
24420 goto theend;
24421 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024422 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024423 {
24424 /*
24425 * Report an invalid expression in braces, unless the expression
24426 * evaluation has been cancelled due to an aborting error, an
24427 * interrupt, or an exception.
24428 */
24429 if (!aborting())
24430 {
24431 if (end != NULL)
24432 EMSG2(_(e_invarg2), start);
24433 }
24434 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024435 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024436 goto theend;
24437 }
24438
24439 if (lv.ll_tv != NULL)
24440 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024441 if (fdp != NULL)
24442 {
24443 fdp->fd_dict = lv.ll_dict;
24444 fdp->fd_newkey = lv.ll_newkey;
24445 lv.ll_newkey = NULL;
24446 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024447 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024448 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24449 {
24450 name = vim_strsave(lv.ll_tv->vval.v_string);
24451 *pp = end;
24452 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024453 else if (lv.ll_tv->v_type == VAR_PARTIAL
24454 && lv.ll_tv->vval.v_partial != NULL)
24455 {
24456 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24457 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024458 if (partial != NULL)
24459 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024460 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024461 else
24462 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024463 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24464 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024465 EMSG(_(e_funcref));
24466 else
24467 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024468 name = NULL;
24469 }
24470 goto theend;
24471 }
24472
24473 if (lv.ll_name == NULL)
24474 {
24475 /* Error found, but continue after the function name. */
24476 *pp = end;
24477 goto theend;
24478 }
24479
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024480 /* Check if the name is a Funcref. If so, use the value. */
24481 if (lv.ll_exp_name != NULL)
24482 {
24483 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024484 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024485 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024486 if (name == lv.ll_exp_name)
24487 name = NULL;
24488 }
24489 else
24490 {
24491 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024492 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024493 if (name == *pp)
24494 name = NULL;
24495 }
24496 if (name != NULL)
24497 {
24498 name = vim_strsave(name);
24499 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024500 if (STRNCMP(name, "<SNR>", 5) == 0)
24501 {
24502 /* Change "<SNR>" to the byte sequence. */
24503 name[0] = K_SPECIAL;
24504 name[1] = KS_EXTRA;
24505 name[2] = (int)KE_SNR;
24506 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24507 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024508 goto theend;
24509 }
24510
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024511 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024512 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024513 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024514 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24515 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24516 {
24517 /* When there was "s:" already or the name expanded to get a
24518 * leading "s:" then remove it. */
24519 lv.ll_name += 2;
24520 len -= 2;
24521 lead = 2;
24522 }
24523 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024524 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024525 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024526 /* skip over "s:" and "g:" */
24527 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024528 lv.ll_name += 2;
24529 len = (int)(end - lv.ll_name);
24530 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024531
24532 /*
24533 * Copy the function name to allocated memory.
24534 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24535 * Accept <SNR>123_name() outside a script.
24536 */
24537 if (skip)
24538 lead = 0; /* do nothing */
24539 else if (lead > 0)
24540 {
24541 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024542 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24543 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024544 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024545 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024546 if (current_SID <= 0)
24547 {
24548 EMSG(_(e_usingsid));
24549 goto theend;
24550 }
24551 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24552 lead += (int)STRLEN(sid_buf);
24553 }
24554 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024555 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024556 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024557 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024558 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024559 goto theend;
24560 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024561 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024562 {
24563 char_u *cp = vim_strchr(lv.ll_name, ':');
24564
24565 if (cp != NULL && cp < end)
24566 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024567 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024568 goto theend;
24569 }
24570 }
24571
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024572 name = alloc((unsigned)(len + lead + 1));
24573 if (name != NULL)
24574 {
24575 if (lead > 0)
24576 {
24577 name[0] = K_SPECIAL;
24578 name[1] = KS_EXTRA;
24579 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024580 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024581 STRCPY(name + 3, sid_buf);
24582 }
24583 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024584 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024585 }
24586 *pp = end;
24587
24588theend:
24589 clear_lval(&lv);
24590 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591}
24592
24593/*
24594 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24595 * Return 2 if "p" starts with "s:".
24596 * Return 0 otherwise.
24597 */
24598 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024599eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024601 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24602 * the standard library function. */
24603 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24604 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 return 5;
24606 if (p[0] == 's' && p[1] == ':')
24607 return 2;
24608 return 0;
24609}
24610
24611/*
24612 * Return TRUE if "p" starts with "<SID>" or "s:".
24613 * Only works if eval_fname_script() returned non-zero for "p"!
24614 */
24615 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024616eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617{
24618 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24619}
24620
24621/*
24622 * List the head of the function: "name(arg1, arg2)".
24623 */
24624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024625list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626{
24627 int j;
24628
24629 msg_start();
24630 if (indent)
24631 MSG_PUTS(" ");
24632 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024633 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 {
24635 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024636 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 }
24638 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024639 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024640 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024641 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 {
24643 if (j)
24644 MSG_PUTS(", ");
24645 msg_puts(FUNCARG(fp, j));
24646 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024647 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648 {
24649 if (j)
24650 MSG_PUTS(", ");
24651 MSG_PUTS("...");
24652 }
24653 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024654 if (fp->uf_flags & FC_ABORT)
24655 MSG_PUTS(" abort");
24656 if (fp->uf_flags & FC_RANGE)
24657 MSG_PUTS(" range");
24658 if (fp->uf_flags & FC_DICT)
24659 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024660 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024661 if (p_verbose > 0)
24662 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663}
24664
24665/*
24666 * Find a function by name, return pointer to it in ufuncs.
24667 * Return NULL for unknown function.
24668 */
24669 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024670find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024672 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024674 hi = hash_find(&func_hashtab, name);
24675 if (!HASHITEM_EMPTY(hi))
24676 return HI2UF(hi);
24677 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678}
24679
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024680#if defined(EXITFREE) || defined(PROTO)
24681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024682free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024683{
24684 hashitem_T *hi;
24685
24686 /* Need to start all over every time, because func_free() may change the
24687 * hash table. */
24688 while (func_hashtab.ht_used > 0)
24689 for (hi = func_hashtab.ht_array; ; ++hi)
24690 if (!HASHITEM_EMPTY(hi))
24691 {
24692 func_free(HI2UF(hi));
24693 break;
24694 }
24695}
24696#endif
24697
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024698 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024699translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024700{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024701 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024702 return find_internal_func(name) >= 0;
24703 return find_func(name) != NULL;
24704}
24705
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024706/*
24707 * Return TRUE if a function "name" exists.
24708 */
24709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024710function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024711{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024712 char_u *nm = name;
24713 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024714 int n = FALSE;
24715
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024716 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024717 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024718 nm = skipwhite(nm);
24719
24720 /* Only accept "funcname", "funcname ", "funcname (..." and
24721 * "funcname(...", not "funcname!...". */
24722 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024723 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024724 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024725 return n;
24726}
24727
Bram Moolenaara1544c02013-05-30 12:35:52 +020024728 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024729get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024730{
24731 char_u *nm = name;
24732 char_u *p;
24733
Bram Moolenaar65639032016-03-16 21:40:30 +010024734 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024735
24736 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024737 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024738 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024739
Bram Moolenaara1544c02013-05-30 12:35:52 +020024740 vim_free(p);
24741 return NULL;
24742}
24743
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024744/*
24745 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024746 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24747 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024748 */
24749 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024750builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024751{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024752 char_u *p;
24753
24754 if (!ASCII_ISLOWER(name[0]))
24755 return FALSE;
24756 p = vim_strchr(name, AUTOLOAD_CHAR);
24757 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024758}
24759
Bram Moolenaar05159a02005-02-26 23:04:13 +000024760#if defined(FEAT_PROFILE) || defined(PROTO)
24761/*
24762 * Start profiling function "fp".
24763 */
24764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024765func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024766{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024767 int len = fp->uf_lines.ga_len;
24768
24769 if (len == 0)
24770 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024771 fp->uf_tm_count = 0;
24772 profile_zero(&fp->uf_tm_self);
24773 profile_zero(&fp->uf_tm_total);
24774 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024775 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024776 if (fp->uf_tml_total == NULL)
24777 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024778 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024779 if (fp->uf_tml_self == NULL)
24780 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024781 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024782 fp->uf_tml_idx = -1;
24783 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24784 || fp->uf_tml_self == NULL)
24785 return; /* out of memory */
24786
24787 fp->uf_profiling = TRUE;
24788}
24789
24790/*
24791 * Dump the profiling results for all functions in file "fd".
24792 */
24793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024794func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024795{
24796 hashitem_T *hi;
24797 int todo;
24798 ufunc_T *fp;
24799 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024800 ufunc_T **sorttab;
24801 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024802
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024803 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024804 if (todo == 0)
24805 return; /* nothing to dump */
24806
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024807 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024808
Bram Moolenaar05159a02005-02-26 23:04:13 +000024809 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24810 {
24811 if (!HASHITEM_EMPTY(hi))
24812 {
24813 --todo;
24814 fp = HI2UF(hi);
24815 if (fp->uf_profiling)
24816 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024817 if (sorttab != NULL)
24818 sorttab[st_len++] = fp;
24819
Bram Moolenaar05159a02005-02-26 23:04:13 +000024820 if (fp->uf_name[0] == K_SPECIAL)
24821 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24822 else
24823 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24824 if (fp->uf_tm_count == 1)
24825 fprintf(fd, "Called 1 time\n");
24826 else
24827 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24828 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24829 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24830 fprintf(fd, "\n");
24831 fprintf(fd, "count total (s) self (s)\n");
24832
24833 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24834 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024835 if (FUNCLINE(fp, i) == NULL)
24836 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024837 prof_func_line(fd, fp->uf_tml_count[i],
24838 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024839 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24840 }
24841 fprintf(fd, "\n");
24842 }
24843 }
24844 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024845
24846 if (sorttab != NULL && st_len > 0)
24847 {
24848 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24849 prof_total_cmp);
24850 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24851 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24852 prof_self_cmp);
24853 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24854 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024855
24856 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024857}
Bram Moolenaar73830342005-02-28 22:48:19 +000024858
24859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024860prof_sort_list(
24861 FILE *fd,
24862 ufunc_T **sorttab,
24863 int st_len,
24864 char *title,
24865 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024866{
24867 int i;
24868 ufunc_T *fp;
24869
24870 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24871 fprintf(fd, "count total (s) self (s) function\n");
24872 for (i = 0; i < 20 && i < st_len; ++i)
24873 {
24874 fp = sorttab[i];
24875 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24876 prefer_self);
24877 if (fp->uf_name[0] == K_SPECIAL)
24878 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24879 else
24880 fprintf(fd, " %s()\n", fp->uf_name);
24881 }
24882 fprintf(fd, "\n");
24883}
24884
24885/*
24886 * Print the count and times for one function or function line.
24887 */
24888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024889prof_func_line(
24890 FILE *fd,
24891 int count,
24892 proftime_T *total,
24893 proftime_T *self,
24894 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024895{
24896 if (count > 0)
24897 {
24898 fprintf(fd, "%5d ", count);
24899 if (prefer_self && profile_equal(total, self))
24900 fprintf(fd, " ");
24901 else
24902 fprintf(fd, "%s ", profile_msg(total));
24903 if (!prefer_self && profile_equal(total, self))
24904 fprintf(fd, " ");
24905 else
24906 fprintf(fd, "%s ", profile_msg(self));
24907 }
24908 else
24909 fprintf(fd, " ");
24910}
24911
24912/*
24913 * Compare function for total time sorting.
24914 */
24915 static int
24916#ifdef __BORLANDC__
24917_RTLENTRYF
24918#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024919prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024920{
24921 ufunc_T *p1, *p2;
24922
24923 p1 = *(ufunc_T **)s1;
24924 p2 = *(ufunc_T **)s2;
24925 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24926}
24927
24928/*
24929 * Compare function for self time sorting.
24930 */
24931 static int
24932#ifdef __BORLANDC__
24933_RTLENTRYF
24934#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024935prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024936{
24937 ufunc_T *p1, *p2;
24938
24939 p1 = *(ufunc_T **)s1;
24940 p2 = *(ufunc_T **)s2;
24941 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24942}
24943
Bram Moolenaar05159a02005-02-26 23:04:13 +000024944#endif
24945
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024946/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024947 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024948 * Return TRUE if a package was loaded.
24949 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024951script_autoload(
24952 char_u *name,
24953 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024954{
24955 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024956 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024957 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024958 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024959
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024960 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024961 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024962 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024963 return FALSE;
24964
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024965 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024966
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024967 /* Find the name in the list of previously loaded package names. Skip
24968 * "autoload/", it's always the same. */
24969 for (i = 0; i < ga_loaded.ga_len; ++i)
24970 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24971 break;
24972 if (!reload && i < ga_loaded.ga_len)
24973 ret = FALSE; /* was loaded already */
24974 else
24975 {
24976 /* Remember the name if it wasn't loaded already. */
24977 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24978 {
24979 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24980 tofree = NULL;
24981 }
24982
24983 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024984 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024985 ret = TRUE;
24986 }
24987
24988 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024989 return ret;
24990}
24991
24992/*
24993 * Return the autoload script name for a function or variable name.
24994 * Returns NULL when out of memory.
24995 */
24996 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024997autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024998{
24999 char_u *p;
25000 char_u *scriptname;
25001
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025002 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025003 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25004 if (scriptname == NULL)
25005 return FALSE;
25006 STRCPY(scriptname, "autoload/");
25007 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025008 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025009 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025010 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025011 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025012 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025013}
25014
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25016
25017/*
25018 * Function given to ExpandGeneric() to obtain the list of user defined
25019 * function names.
25020 */
25021 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025022get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025024 static long_u done;
25025 static hashitem_T *hi;
25026 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027
25028 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025029 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025030 done = 0;
25031 hi = func_hashtab.ht_array;
25032 }
25033 if (done < func_hashtab.ht_used)
25034 {
25035 if (done++ > 0)
25036 ++hi;
25037 while (HASHITEM_EMPTY(hi))
25038 ++hi;
25039 fp = HI2UF(hi);
25040
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025041 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025042 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025043
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025044 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25045 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046
25047 cat_func_name(IObuff, fp);
25048 if (xp->xp_context != EXPAND_USER_FUNC)
25049 {
25050 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025051 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052 STRCAT(IObuff, ")");
25053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 return IObuff;
25055 }
25056 return NULL;
25057}
25058
25059#endif /* FEAT_CMDL_COMPL */
25060
25061/*
25062 * Copy the function name of "fp" to buffer "buf".
25063 * "buf" must be able to hold the function name plus three bytes.
25064 * Takes care of script-local function names.
25065 */
25066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025067cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025069 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070 {
25071 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025072 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025073 }
25074 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025075 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076}
25077
25078/*
25079 * ":delfunction {name}"
25080 */
25081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025082ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025084 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 char_u *p;
25086 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025087 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088
25089 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025090 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025091 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025093 {
25094 if (fudi.fd_dict != NULL && !eap->skip)
25095 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098 if (!ends_excmd(*skipwhite(p)))
25099 {
25100 vim_free(name);
25101 EMSG(_(e_trailing));
25102 return;
25103 }
25104 eap->nextcmd = check_nextcmd(p);
25105 if (eap->nextcmd != NULL)
25106 *p = NUL;
25107
25108 if (!eap->skip)
25109 fp = find_func(name);
25110 vim_free(name);
25111
25112 if (!eap->skip)
25113 {
25114 if (fp == NULL)
25115 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025116 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117 return;
25118 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025119 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 {
25121 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25122 return;
25123 }
25124
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025125 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025127 /* Delete the dict item that refers to the function, it will
25128 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025129 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025131 else
25132 func_free(fp);
25133 }
25134}
25135
25136/*
25137 * Free a function and remove it from the list of functions.
25138 */
25139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025140func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025141{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025142 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025143
25144 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025145 ga_clear_strings(&(fp->uf_args));
25146 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025147#ifdef FEAT_PROFILE
25148 vim_free(fp->uf_tml_count);
25149 vim_free(fp->uf_tml_total);
25150 vim_free(fp->uf_tml_self);
25151#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025152
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025153 /* remove the function from the function hashtable */
25154 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25155 if (HASHITEM_EMPTY(hi))
25156 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025157 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025158 hash_remove(&func_hashtab, hi);
25159
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025160 vim_free(fp);
25161}
25162
25163/*
25164 * Unreference a Function: decrement the reference count and free it when it
25165 * becomes zero. Only for numbered functions.
25166 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025168func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025169{
25170 ufunc_T *fp;
25171
25172 if (name != NULL && isdigit(*name))
25173 {
25174 fp = find_func(name);
25175 if (fp == NULL)
25176 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025177 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025178 {
25179 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025180 * when "uf_calls" becomes zero. */
25181 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025182 func_free(fp);
25183 }
25184 }
25185}
25186
25187/*
25188 * Count a reference to a Function.
25189 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025191func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025192{
25193 ufunc_T *fp;
25194
25195 if (name != NULL && isdigit(*name))
25196 {
25197 fp = find_func(name);
25198 if (fp == NULL)
25199 EMSG2(_(e_intern2), "func_ref()");
25200 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025201 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202 }
25203}
25204
25205/*
25206 * Call a user function.
25207 */
25208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025209call_user_func(
25210 ufunc_T *fp, /* pointer to function */
25211 int argcount, /* nr of args */
25212 typval_T *argvars, /* arguments */
25213 typval_T *rettv, /* return value */
25214 linenr_T firstline, /* first line of range */
25215 linenr_T lastline, /* last line of range */
25216 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217{
Bram Moolenaar33570922005-01-25 22:26:29 +000025218 char_u *save_sourcing_name;
25219 linenr_T save_sourcing_lnum;
25220 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025221 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025222 int save_did_emsg;
25223 static int depth = 0;
25224 dictitem_T *v;
25225 int fixvar_idx = 0; /* index in fixvar[] */
25226 int i;
25227 int ai;
25228 char_u numbuf[NUMBUFLEN];
25229 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025230 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025231#ifdef FEAT_PROFILE
25232 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025233 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235
25236 /* If depth of calling is getting too high, don't execute the function */
25237 if (depth >= p_mfd)
25238 {
25239 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025240 rettv->v_type = VAR_NUMBER;
25241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242 return;
25243 }
25244 ++depth;
25245
25246 line_breakcheck(); /* check for CTRL-C hit */
25247
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025248 fc = (funccall_T *)alloc(sizeof(funccall_T));
25249 fc->caller = current_funccal;
25250 current_funccal = fc;
25251 fc->func = fp;
25252 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025253 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025254 fc->linenr = 0;
25255 fc->returned = FALSE;
25256 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025258 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25259 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260
Bram Moolenaar33570922005-01-25 22:26:29 +000025261 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025262 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025263 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25264 * each argument variable and saves a lot of time.
25265 */
25266 /*
25267 * Init l: variables.
25268 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025269 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025270 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025271 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025272 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25273 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025274 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025275 name = v->di_key;
25276 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025277 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025278 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025279 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025280 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025281 v->di_tv.vval.v_dict = selfdict;
25282 ++selfdict->dv_refcount;
25283 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025284
Bram Moolenaar33570922005-01-25 22:26:29 +000025285 /*
25286 * Init a: variables.
25287 * Set a:0 to "argcount".
25288 * Set a:000 to a list with room for the "..." arguments.
25289 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025290 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025291 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025292 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025293 /* Use "name" to avoid a warning from some compiler that checks the
25294 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025295 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025296 name = v->di_key;
25297 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025298 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025299 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025300 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025301 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025302 v->di_tv.vval.v_list = &fc->l_varlist;
25303 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25304 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25305 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025306
25307 /*
25308 * Set a:firstline to "firstline" and a:lastline to "lastline".
25309 * Set a:name to named arguments.
25310 * Set a:N to the "..." arguments.
25311 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025312 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025313 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025314 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025315 (varnumber_T)lastline);
25316 for (i = 0; i < argcount; ++i)
25317 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025318 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025319 if (ai < 0)
25320 /* named argument a:name */
25321 name = FUNCARG(fp, i);
25322 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025323 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025324 /* "..." argument a:1, a:2, etc. */
25325 sprintf((char *)numbuf, "%d", ai + 1);
25326 name = numbuf;
25327 }
25328 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25329 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025330 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025331 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25332 }
25333 else
25334 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025335 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25336 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025337 if (v == NULL)
25338 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025339 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025340 }
25341 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025342 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025343
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025344 /* Note: the values are copied directly to avoid alloc/free.
25345 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025346 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025347 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025348
25349 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25350 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025351 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25352 fc->l_listitems[ai].li_tv = argvars[i];
25353 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025354 }
25355 }
25356
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 /* Don't redraw while executing the function. */
25358 ++RedrawingDisabled;
25359 save_sourcing_name = sourcing_name;
25360 save_sourcing_lnum = sourcing_lnum;
25361 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025362 /* need space for function name + ("function " + 3) or "[number]" */
25363 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25364 + STRLEN(fp->uf_name) + 20;
25365 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366 if (sourcing_name != NULL)
25367 {
25368 if (save_sourcing_name != NULL
25369 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025370 sprintf((char *)sourcing_name, "%s[%d]..",
25371 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 else
25373 STRCPY(sourcing_name, "function ");
25374 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25375
25376 if (p_verbose >= 12)
25377 {
25378 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025379 verbose_enter_scroll();
25380
Bram Moolenaar555b2802005-05-19 21:08:39 +000025381 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382 if (p_verbose >= 14)
25383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025384 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025385 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025386 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025387 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388
25389 msg_puts((char_u *)"(");
25390 for (i = 0; i < argcount; ++i)
25391 {
25392 if (i > 0)
25393 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025394 if (argvars[i].v_type == VAR_NUMBER)
25395 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025396 else
25397 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025398 /* Do not want errors such as E724 here. */
25399 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025400 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025401 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025402 if (s != NULL)
25403 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025404 if (vim_strsize(s) > MSG_BUF_CLEN)
25405 {
25406 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25407 s = buf;
25408 }
25409 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025410 vim_free(tofree);
25411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412 }
25413 }
25414 msg_puts((char_u *)")");
25415 }
25416 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025417
25418 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 --no_wait_return;
25420 }
25421 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025422#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025423 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025424 {
25425 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25426 func_do_profile(fp);
25427 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025428 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025429 {
25430 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025431 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025432 profile_zero(&fp->uf_tm_children);
25433 }
25434 script_prof_save(&wait_start);
25435 }
25436#endif
25437
Bram Moolenaar071d4272004-06-13 20:20:40 +000025438 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025439 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025440 save_did_emsg = did_emsg;
25441 did_emsg = FALSE;
25442
25443 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025444 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025445 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25446
25447 --RedrawingDisabled;
25448
25449 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025450 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025452 clear_tv(rettv);
25453 rettv->v_type = VAR_NUMBER;
25454 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455 }
25456
Bram Moolenaar05159a02005-02-26 23:04:13 +000025457#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025458 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025459 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025460 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025461 profile_end(&call_start);
25462 profile_sub_wait(&wait_start, &call_start);
25463 profile_add(&fp->uf_tm_total, &call_start);
25464 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025465 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025466 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025467 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25468 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025469 }
25470 }
25471#endif
25472
Bram Moolenaar071d4272004-06-13 20:20:40 +000025473 /* when being verbose, mention the return value */
25474 if (p_verbose >= 12)
25475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025477 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478
Bram Moolenaar071d4272004-06-13 20:20:40 +000025479 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025480 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025481 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025482 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025483 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025486 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025487 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025488 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025489 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025490
Bram Moolenaar555b2802005-05-19 21:08:39 +000025491 /* The value may be very long. Skip the middle part, so that we
25492 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025493 * truncate it at the end. Don't want errors such as E724 here. */
25494 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025495 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025496 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025497 if (s != NULL)
25498 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025499 if (vim_strsize(s) > MSG_BUF_CLEN)
25500 {
25501 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25502 s = buf;
25503 }
25504 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025505 vim_free(tofree);
25506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025507 }
25508 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025509
25510 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 --no_wait_return;
25512 }
25513
25514 vim_free(sourcing_name);
25515 sourcing_name = save_sourcing_name;
25516 sourcing_lnum = save_sourcing_lnum;
25517 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025518#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025519 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025520 script_prof_restore(&wait_start);
25521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522
25523 if (p_verbose >= 12 && sourcing_name != NULL)
25524 {
25525 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025526 verbose_enter_scroll();
25527
Bram Moolenaar555b2802005-05-19 21:08:39 +000025528 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025530
25531 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532 --no_wait_return;
25533 }
25534
25535 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025536 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025537 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025538
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025539 /* If the a:000 list and the l: and a: dicts are not referenced we can
25540 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025541 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25542 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25543 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25544 {
25545 free_funccal(fc, FALSE);
25546 }
25547 else
25548 {
25549 hashitem_T *hi;
25550 listitem_T *li;
25551 int todo;
25552
25553 /* "fc" is still in use. This can happen when returning "a:000" or
25554 * assigning "l:" to a global variable.
25555 * Link "fc" in the list for garbage collection later. */
25556 fc->caller = previous_funccal;
25557 previous_funccal = fc;
25558
25559 /* Make a copy of the a: variables, since we didn't do that above. */
25560 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25561 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25562 {
25563 if (!HASHITEM_EMPTY(hi))
25564 {
25565 --todo;
25566 v = HI2DI(hi);
25567 copy_tv(&v->di_tv, &v->di_tv);
25568 }
25569 }
25570
25571 /* Make a copy of the a:000 items, since we didn't do that above. */
25572 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25573 copy_tv(&li->li_tv, &li->li_tv);
25574 }
25575}
25576
25577/*
25578 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025579 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025580 */
25581 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025582can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025583{
25584 return (fc->l_varlist.lv_copyID != copyID
25585 && fc->l_vars.dv_copyID != copyID
25586 && fc->l_avars.dv_copyID != copyID);
25587}
25588
25589/*
25590 * Free "fc" and what it contains.
25591 */
25592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025593free_funccal(
25594 funccall_T *fc,
25595 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025596{
25597 listitem_T *li;
25598
25599 /* The a: variables typevals may not have been allocated, only free the
25600 * allocated variables. */
25601 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25602
25603 /* free all l: variables */
25604 vars_clear(&fc->l_vars.dv_hashtab);
25605
25606 /* Free the a:000 variables if they were allocated. */
25607 if (free_val)
25608 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25609 clear_tv(&li->li_tv);
25610
25611 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612}
25613
25614/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025615 * Add a number variable "name" to dict "dp" with value "nr".
25616 */
25617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025618add_nr_var(
25619 dict_T *dp,
25620 dictitem_T *v,
25621 char *name,
25622 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025623{
25624 STRCPY(v->di_key, name);
25625 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25626 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25627 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025628 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025629 v->di_tv.vval.v_number = nr;
25630}
25631
25632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025633 * ":return [expr]"
25634 */
25635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025636ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025637{
25638 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025639 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 int returning = FALSE;
25641
25642 if (current_funccal == NULL)
25643 {
25644 EMSG(_("E133: :return not inside a function"));
25645 return;
25646 }
25647
25648 if (eap->skip)
25649 ++emsg_skip;
25650
25651 eap->nextcmd = NULL;
25652 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025653 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 {
25655 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025656 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025659 }
25660 /* It's safer to return also on error. */
25661 else if (!eap->skip)
25662 {
25663 /*
25664 * Return unless the expression evaluation has been cancelled due to an
25665 * aborting error, an interrupt, or an exception.
25666 */
25667 if (!aborting())
25668 returning = do_return(eap, FALSE, TRUE, NULL);
25669 }
25670
25671 /* When skipping or the return gets pending, advance to the next command
25672 * in this line (!returning). Otherwise, ignore the rest of the line.
25673 * Following lines will be ignored by get_func_line(). */
25674 if (returning)
25675 eap->nextcmd = NULL;
25676 else if (eap->nextcmd == NULL) /* no argument */
25677 eap->nextcmd = check_nextcmd(arg);
25678
25679 if (eap->skip)
25680 --emsg_skip;
25681}
25682
25683/*
25684 * Return from a function. Possibly makes the return pending. Also called
25685 * for a pending return at the ":endtry" or after returning from an extra
25686 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025687 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025688 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689 * FALSE when the return gets pending.
25690 */
25691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025692do_return(
25693 exarg_T *eap,
25694 int reanimate,
25695 int is_cmd,
25696 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025697{
25698 int idx;
25699 struct condstack *cstack = eap->cstack;
25700
25701 if (reanimate)
25702 /* Undo the return. */
25703 current_funccal->returned = FALSE;
25704
25705 /*
25706 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25707 * not in its finally clause (which then is to be executed next) is found.
25708 * In this case, make the ":return" pending for execution at the ":endtry".
25709 * Otherwise, return normally.
25710 */
25711 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25712 if (idx >= 0)
25713 {
25714 cstack->cs_pending[idx] = CSTP_RETURN;
25715
25716 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025717 /* A pending return again gets pending. "rettv" points to an
25718 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025719 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025720 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025721 else
25722 {
25723 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025724 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025726 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025728 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025729 {
25730 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025731 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025732 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025733 else
25734 EMSG(_(e_outofmem));
25735 }
25736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025737 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738
25739 if (reanimate)
25740 {
25741 /* The pending return value could be overwritten by a ":return"
25742 * without argument in a finally clause; reset the default
25743 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025744 current_funccal->rettv->v_type = VAR_NUMBER;
25745 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746 }
25747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025748 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749 }
25750 else
25751 {
25752 current_funccal->returned = TRUE;
25753
25754 /* If the return is carried out now, store the return value. For
25755 * a return immediately after reanimation, the value is already
25756 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025757 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025759 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025760 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025761 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025762 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763 }
25764 }
25765
25766 return idx < 0;
25767}
25768
25769/*
25770 * Free the variable with a pending return value.
25771 */
25772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025773discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774{
Bram Moolenaar33570922005-01-25 22:26:29 +000025775 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776}
25777
25778/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025779 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 * is an allocated string. Used by report_pending() for verbose messages.
25781 */
25782 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025783get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025785 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025786 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025787 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025789 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025790 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025791 if (s == NULL)
25792 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025793
25794 STRCPY(IObuff, ":return ");
25795 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25796 if (STRLEN(s) + 8 >= IOSIZE)
25797 STRCPY(IObuff + IOSIZE - 4, "...");
25798 vim_free(tofree);
25799 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800}
25801
25802/*
25803 * Get next function line.
25804 * Called by do_cmdline() to get the next line.
25805 * Returns allocated string, or NULL for end of function.
25806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025807 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025808get_func_line(
25809 int c UNUSED,
25810 void *cookie,
25811 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812{
Bram Moolenaar33570922005-01-25 22:26:29 +000025813 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025814 ufunc_T *fp = fcp->func;
25815 char_u *retval;
25816 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025817
25818 /* If breakpoints have been added/deleted need to check for it. */
25819 if (fcp->dbg_tick != debug_tick)
25820 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025821 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822 sourcing_lnum);
25823 fcp->dbg_tick = debug_tick;
25824 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025825#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025826 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025827 func_line_end(cookie);
25828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829
Bram Moolenaar05159a02005-02-26 23:04:13 +000025830 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025831 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25832 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025833 retval = NULL;
25834 else
25835 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025836 /* Skip NULL lines (continuation lines). */
25837 while (fcp->linenr < gap->ga_len
25838 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25839 ++fcp->linenr;
25840 if (fcp->linenr >= gap->ga_len)
25841 retval = NULL;
25842 else
25843 {
25844 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25845 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025846#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025847 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025848 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025849#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025851 }
25852
25853 /* Did we encounter a breakpoint? */
25854 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25855 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025856 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025857 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025858 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025859 sourcing_lnum);
25860 fcp->dbg_tick = debug_tick;
25861 }
25862
25863 return retval;
25864}
25865
Bram Moolenaar05159a02005-02-26 23:04:13 +000025866#if defined(FEAT_PROFILE) || defined(PROTO)
25867/*
25868 * Called when starting to read a function line.
25869 * "sourcing_lnum" must be correct!
25870 * When skipping lines it may not actually be executed, but we won't find out
25871 * until later and we need to store the time now.
25872 */
25873 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025874func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025875{
25876 funccall_T *fcp = (funccall_T *)cookie;
25877 ufunc_T *fp = fcp->func;
25878
25879 if (fp->uf_profiling && sourcing_lnum >= 1
25880 && sourcing_lnum <= fp->uf_lines.ga_len)
25881 {
25882 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025883 /* Skip continuation lines. */
25884 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25885 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025886 fp->uf_tml_execed = FALSE;
25887 profile_start(&fp->uf_tml_start);
25888 profile_zero(&fp->uf_tml_children);
25889 profile_get_wait(&fp->uf_tml_wait);
25890 }
25891}
25892
25893/*
25894 * Called when actually executing a function line.
25895 */
25896 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025897func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025898{
25899 funccall_T *fcp = (funccall_T *)cookie;
25900 ufunc_T *fp = fcp->func;
25901
25902 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25903 fp->uf_tml_execed = TRUE;
25904}
25905
25906/*
25907 * Called when done with a function line.
25908 */
25909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025910func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025911{
25912 funccall_T *fcp = (funccall_T *)cookie;
25913 ufunc_T *fp = fcp->func;
25914
25915 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25916 {
25917 if (fp->uf_tml_execed)
25918 {
25919 ++fp->uf_tml_count[fp->uf_tml_idx];
25920 profile_end(&fp->uf_tml_start);
25921 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025922 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025923 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25924 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025925 }
25926 fp->uf_tml_idx = -1;
25927 }
25928}
25929#endif
25930
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931/*
25932 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025933 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934 */
25935 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025936func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937{
Bram Moolenaar33570922005-01-25 22:26:29 +000025938 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939
25940 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25941 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025942 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025943 || fcp->returned);
25944}
25945
25946/*
25947 * return TRUE if cookie indicates a function which "abort"s on errors.
25948 */
25949 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025950func_has_abort(
25951 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025953 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954}
25955
25956#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25957typedef enum
25958{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025959 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25960 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25961 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025962} var_flavour_T;
25963
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025964static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965
25966 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025967var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968{
25969 char_u *p = varname;
25970
25971 if (ASCII_ISUPPER(*p))
25972 {
25973 while (*(++p))
25974 if (ASCII_ISLOWER(*p))
25975 return VAR_FLAVOUR_SESSION;
25976 return VAR_FLAVOUR_VIMINFO;
25977 }
25978 else
25979 return VAR_FLAVOUR_DEFAULT;
25980}
25981#endif
25982
25983#if defined(FEAT_VIMINFO) || defined(PROTO)
25984/*
25985 * Restore global vars that start with a capital from the viminfo file
25986 */
25987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025988read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989{
25990 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025991 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025992 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025993 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025994
25995 if (!writing && (find_viminfo_parameter('!') != NULL))
25996 {
25997 tab = vim_strchr(virp->vir_line + 1, '\t');
25998 if (tab != NULL)
25999 {
26000 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026001 switch (*tab)
26002 {
26003 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026004#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026005 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026006#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026007 case 'D': type = VAR_DICT; break;
26008 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026009 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026011
26012 tab = vim_strchr(tab, '\t');
26013 if (tab != NULL)
26014 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026015 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026016 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026017 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026019#ifdef FEAT_FLOAT
26020 else if (type == VAR_FLOAT)
26021 (void)string2float(tab + 1, &tv.vval.v_float);
26022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026024 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026025 if (type == VAR_DICT || type == VAR_LIST)
26026 {
26027 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26028
26029 if (etv == NULL)
26030 /* Failed to parse back the dict or list, use it as a
26031 * string. */
26032 tv.v_type = VAR_STRING;
26033 else
26034 {
26035 vim_free(tv.vval.v_string);
26036 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026037 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026038 }
26039 }
26040
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026041 /* when in a function use global variables */
26042 save_funccal = current_funccal;
26043 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026044 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026045 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026046
26047 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026048 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026049 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26050 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051 }
26052 }
26053 }
26054
26055 return viminfo_readline(virp);
26056}
26057
26058/*
26059 * Write global vars that start with a capital to the viminfo file
26060 */
26061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026062write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063{
Bram Moolenaar33570922005-01-25 22:26:29 +000026064 hashitem_T *hi;
26065 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026066 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026067 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026068 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026069 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026070 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071
26072 if (find_viminfo_parameter('!') == NULL)
26073 return;
26074
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026075 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026077 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026078 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026079 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026080 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026082 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026083 this_var = HI2DI(hi);
26084 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026085 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026086 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026087 {
26088 case VAR_STRING: s = "STR"; break;
26089 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026090 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026091 case VAR_DICT: s = "DIC"; break;
26092 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026093 case VAR_SPECIAL: s = "XPL"; break;
26094
26095 case VAR_UNKNOWN:
26096 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026097 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026098 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026099 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026100 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026101 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026102 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026103 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026104 if (p != NULL)
26105 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026106 vim_free(tofree);
26107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108 }
26109 }
26110}
26111#endif
26112
26113#if defined(FEAT_SESSION) || defined(PROTO)
26114 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026115store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026116{
Bram Moolenaar33570922005-01-25 22:26:29 +000026117 hashitem_T *hi;
26118 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026119 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026120 char_u *p, *t;
26121
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026122 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026123 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026124 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026125 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026126 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026127 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026128 this_var = HI2DI(hi);
26129 if ((this_var->di_tv.v_type == VAR_NUMBER
26130 || this_var->di_tv.v_type == VAR_STRING)
26131 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026132 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026133 /* Escape special characters with a backslash. Turn a LF and
26134 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026135 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026136 (char_u *)"\\\"\n\r");
26137 if (p == NULL) /* out of memory */
26138 break;
26139 for (t = p; *t != NUL; ++t)
26140 if (*t == '\n')
26141 *t = 'n';
26142 else if (*t == '\r')
26143 *t = 'r';
26144 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026145 this_var->di_key,
26146 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26147 : ' ',
26148 p,
26149 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26150 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026151 || put_eol(fd) == FAIL)
26152 {
26153 vim_free(p);
26154 return FAIL;
26155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026156 vim_free(p);
26157 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026158#ifdef FEAT_FLOAT
26159 else if (this_var->di_tv.v_type == VAR_FLOAT
26160 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26161 {
26162 float_T f = this_var->di_tv.vval.v_float;
26163 int sign = ' ';
26164
26165 if (f < 0)
26166 {
26167 f = -f;
26168 sign = '-';
26169 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026170 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026171 this_var->di_key, sign, f) < 0)
26172 || put_eol(fd) == FAIL)
26173 return FAIL;
26174 }
26175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026176 }
26177 }
26178 return OK;
26179}
26180#endif
26181
Bram Moolenaar661b1822005-07-28 22:36:45 +000026182/*
26183 * Display script name where an item was last set.
26184 * Should only be invoked when 'verbose' is non-zero.
26185 */
26186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026187last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026188{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026189 char_u *p;
26190
Bram Moolenaar661b1822005-07-28 22:36:45 +000026191 if (scriptID != 0)
26192 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026193 p = home_replace_save(NULL, get_scriptname(scriptID));
26194 if (p != NULL)
26195 {
26196 verbose_enter();
26197 MSG_PUTS(_("\n\tLast set from "));
26198 MSG_PUTS(p);
26199 vim_free(p);
26200 verbose_leave();
26201 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026202 }
26203}
26204
Bram Moolenaard812df62008-11-09 12:46:09 +000026205/*
26206 * List v:oldfiles in a nice way.
26207 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026209ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026210{
26211 list_T *l = vimvars[VV_OLDFILES].vv_list;
26212 listitem_T *li;
26213 int nr = 0;
26214
26215 if (l == NULL)
26216 msg((char_u *)_("No old files"));
26217 else
26218 {
26219 msg_start();
26220 msg_scroll = TRUE;
26221 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26222 {
26223 msg_outnum((long)++nr);
26224 MSG_PUTS(": ");
26225 msg_outtrans(get_tv_string(&li->li_tv));
26226 msg_putchar('\n');
26227 out_flush(); /* output one line at a time */
26228 ui_breakcheck();
26229 }
26230 /* Assume "got_int" was set to truncate the listing. */
26231 got_int = FALSE;
26232
26233#ifdef FEAT_BROWSE_CMD
26234 if (cmdmod.browse)
26235 {
26236 quit_more = FALSE;
26237 nr = prompt_for_number(FALSE);
26238 msg_starthere();
26239 if (nr > 0)
26240 {
26241 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26242 (long)nr);
26243
26244 if (p != NULL)
26245 {
26246 p = expand_env_save(p);
26247 eap->arg = p;
26248 eap->cmdidx = CMD_edit;
26249 cmdmod.browse = FALSE;
26250 do_exedit(eap, NULL);
26251 vim_free(p);
26252 }
26253 }
26254 }
26255#endif
26256 }
26257}
26258
Bram Moolenaar53744302015-07-17 17:38:22 +020026259/* reset v:option_new, v:option_old and v:option_type */
26260 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026261reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026262{
26263 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26264 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26265 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26266}
26267
26268
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269#endif /* FEAT_EVAL */
26270
Bram Moolenaar071d4272004-06-13 20:20:40 +000026271
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026272#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026273
26274#ifdef WIN3264
26275/*
26276 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26277 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026278static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26279static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26280static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026281
26282/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026283 * Get the short path (8.3) for the filename in "fnamep".
26284 * Only works for a valid file name.
26285 * When the path gets longer "fnamep" is changed and the allocated buffer
26286 * is put in "bufp".
26287 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26288 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026289 */
26290 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026291get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026292{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026293 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026294 char_u *newbuf;
26295
26296 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026297 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026298 if (l > len - 1)
26299 {
26300 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026301 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026302 newbuf = vim_strnsave(*fnamep, l);
26303 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305
26306 vim_free(*bufp);
26307 *fnamep = *bufp = newbuf;
26308
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026309 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026310 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026311 }
26312
26313 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026314 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026315}
26316
26317/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026318 * Get the short path (8.3) for the filename in "fname". The converted
26319 * path is returned in "bufp".
26320 *
26321 * Some of the directories specified in "fname" may not exist. This function
26322 * will shorten the existing directories at the beginning of the path and then
26323 * append the remaining non-existing path.
26324 *
26325 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026326 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026327 * bufp - Pointer to an allocated buffer for the filename.
26328 * fnamelen - Length of the filename pointed to by fname
26329 *
26330 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026331 */
26332 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026333shortpath_for_invalid_fname(
26334 char_u **fname,
26335 char_u **bufp,
26336 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026337{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026338 char_u *short_fname, *save_fname, *pbuf_unused;
26339 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026340 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026341 int old_len, len;
26342 int new_len, sfx_len;
26343 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026344
26345 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026346 old_len = *fnamelen;
26347 save_fname = vim_strnsave(*fname, old_len);
26348 pbuf_unused = NULL;
26349 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026350
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026351 endp = save_fname + old_len - 1; /* Find the end of the copy */
26352 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026354 /*
26355 * Try shortening the supplied path till it succeeds by removing one
26356 * directory at a time from the tail of the path.
26357 */
26358 len = 0;
26359 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026361 /* go back one path-separator */
26362 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26363 --endp;
26364 if (endp <= save_fname)
26365 break; /* processed the complete path */
26366
26367 /*
26368 * Replace the path separator with a NUL and try to shorten the
26369 * resulting path.
26370 */
26371 ch = *endp;
26372 *endp = 0;
26373 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026374 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026375 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26376 {
26377 retval = FAIL;
26378 goto theend;
26379 }
26380 *endp = ch; /* preserve the string */
26381
26382 if (len > 0)
26383 break; /* successfully shortened the path */
26384
26385 /* failed to shorten the path. Skip the path separator */
26386 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026387 }
26388
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026389 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026390 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026391 /*
26392 * Succeeded in shortening the path. Now concatenate the shortened
26393 * path with the remaining path at the tail.
26394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026395
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026396 /* Compute the length of the new path. */
26397 sfx_len = (int)(save_endp - endp) + 1;
26398 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026399
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026400 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026401 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026402 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026403 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026404 /* There is not enough space in the currently allocated string,
26405 * copy it to a buffer big enough. */
26406 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026407 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026408 {
26409 retval = FAIL;
26410 goto theend;
26411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026412 }
26413 else
26414 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026415 /* Transfer short_fname to the main buffer (it's big enough),
26416 * unless get_short_pathname() did its work in-place. */
26417 *fname = *bufp = save_fname;
26418 if (short_fname != save_fname)
26419 vim_strncpy(save_fname, short_fname, len);
26420 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026422
26423 /* concat the not-shortened part of the path */
26424 vim_strncpy(*fname + len, endp, sfx_len);
26425 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026427
26428theend:
26429 vim_free(pbuf_unused);
26430 vim_free(save_fname);
26431
26432 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026433}
26434
26435/*
26436 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026437 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026438 */
26439 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026440shortpath_for_partial(
26441 char_u **fnamep,
26442 char_u **bufp,
26443 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444{
26445 int sepcount, len, tflen;
26446 char_u *p;
26447 char_u *pbuf, *tfname;
26448 int hasTilde;
26449
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026450 /* Count up the path separators from the RHS.. so we know which part
26451 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026453 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026454 if (vim_ispathsep(*p))
26455 ++sepcount;
26456
26457 /* Need full path first (use expand_env() to remove a "~/") */
26458 hasTilde = (**fnamep == '~');
26459 if (hasTilde)
26460 pbuf = tfname = expand_env_save(*fnamep);
26461 else
26462 pbuf = tfname = FullName_save(*fnamep, FALSE);
26463
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026464 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026465
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026466 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26467 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026468
26469 if (len == 0)
26470 {
26471 /* Don't have a valid filename, so shorten the rest of the
26472 * path if we can. This CAN give us invalid 8.3 filenames, but
26473 * there's not a lot of point in guessing what it might be.
26474 */
26475 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026476 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26477 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026478 }
26479
26480 /* Count the paths backward to find the beginning of the desired string. */
26481 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026482 {
26483#ifdef FEAT_MBYTE
26484 if (has_mbyte)
26485 p -= mb_head_off(tfname, p);
26486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026487 if (vim_ispathsep(*p))
26488 {
26489 if (sepcount == 0 || (hasTilde && sepcount == 1))
26490 break;
26491 else
26492 sepcount --;
26493 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026495 if (hasTilde)
26496 {
26497 --p;
26498 if (p >= tfname)
26499 *p = '~';
26500 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026501 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026502 }
26503 else
26504 ++p;
26505
26506 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26507 vim_free(*bufp);
26508 *fnamelen = (int)STRLEN(p);
26509 *bufp = pbuf;
26510 *fnamep = p;
26511
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026512 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026513}
26514#endif /* WIN3264 */
26515
26516/*
26517 * Adjust a filename, according to a string of modifiers.
26518 * *fnamep must be NUL terminated when called. When returning, the length is
26519 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026520 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026521 * When there is an error, *fnamep is set to NULL.
26522 */
26523 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026524modify_fname(
26525 char_u *src, /* string with modifiers */
26526 int *usedlen, /* characters after src that are used */
26527 char_u **fnamep, /* file name so far */
26528 char_u **bufp, /* buffer for allocated file name or NULL */
26529 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026530{
26531 int valid = 0;
26532 char_u *tail;
26533 char_u *s, *p, *pbuf;
26534 char_u dirname[MAXPATHL];
26535 int c;
26536 int has_fullname = 0;
26537#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026538 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026539 int has_shortname = 0;
26540#endif
26541
26542repeat:
26543 /* ":p" - full path/file_name */
26544 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26545 {
26546 has_fullname = 1;
26547
26548 valid |= VALID_PATH;
26549 *usedlen += 2;
26550
26551 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26552 if ((*fnamep)[0] == '~'
26553#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26554 && ((*fnamep)[1] == '/'
26555# ifdef BACKSLASH_IN_FILENAME
26556 || (*fnamep)[1] == '\\'
26557# endif
26558 || (*fnamep)[1] == NUL)
26559
26560#endif
26561 )
26562 {
26563 *fnamep = expand_env_save(*fnamep);
26564 vim_free(*bufp); /* free any allocated file name */
26565 *bufp = *fnamep;
26566 if (*fnamep == NULL)
26567 return -1;
26568 }
26569
26570 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026571 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026572 {
26573 if (vim_ispathsep(*p)
26574 && p[1] == '.'
26575 && (p[2] == NUL
26576 || vim_ispathsep(p[2])
26577 || (p[2] == '.'
26578 && (p[3] == NUL || vim_ispathsep(p[3])))))
26579 break;
26580 }
26581
26582 /* FullName_save() is slow, don't use it when not needed. */
26583 if (*p != NUL || !vim_isAbsName(*fnamep))
26584 {
26585 *fnamep = FullName_save(*fnamep, *p != NUL);
26586 vim_free(*bufp); /* free any allocated file name */
26587 *bufp = *fnamep;
26588 if (*fnamep == NULL)
26589 return -1;
26590 }
26591
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026592#ifdef WIN3264
26593# if _WIN32_WINNT >= 0x0500
26594 if (vim_strchr(*fnamep, '~') != NULL)
26595 {
26596 /* Expand 8.3 filename to full path. Needed to make sure the same
26597 * file does not have two different names.
26598 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26599 p = alloc(_MAX_PATH + 1);
26600 if (p != NULL)
26601 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026602 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026603 {
26604 vim_free(*bufp);
26605 *bufp = *fnamep = p;
26606 }
26607 else
26608 vim_free(p);
26609 }
26610 }
26611# endif
26612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026613 /* Append a path separator to a directory. */
26614 if (mch_isdir(*fnamep))
26615 {
26616 /* Make room for one or two extra characters. */
26617 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26618 vim_free(*bufp); /* free any allocated file name */
26619 *bufp = *fnamep;
26620 if (*fnamep == NULL)
26621 return -1;
26622 add_pathsep(*fnamep);
26623 }
26624 }
26625
26626 /* ":." - path relative to the current directory */
26627 /* ":~" - path relative to the home directory */
26628 /* ":8" - shortname path - postponed till after */
26629 while (src[*usedlen] == ':'
26630 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26631 {
26632 *usedlen += 2;
26633 if (c == '8')
26634 {
26635#ifdef WIN3264
26636 has_shortname = 1; /* Postpone this. */
26637#endif
26638 continue;
26639 }
26640 pbuf = NULL;
26641 /* Need full path first (use expand_env() to remove a "~/") */
26642 if (!has_fullname)
26643 {
26644 if (c == '.' && **fnamep == '~')
26645 p = pbuf = expand_env_save(*fnamep);
26646 else
26647 p = pbuf = FullName_save(*fnamep, FALSE);
26648 }
26649 else
26650 p = *fnamep;
26651
26652 has_fullname = 0;
26653
26654 if (p != NULL)
26655 {
26656 if (c == '.')
26657 {
26658 mch_dirname(dirname, MAXPATHL);
26659 s = shorten_fname(p, dirname);
26660 if (s != NULL)
26661 {
26662 *fnamep = s;
26663 if (pbuf != NULL)
26664 {
26665 vim_free(*bufp); /* free any allocated file name */
26666 *bufp = pbuf;
26667 pbuf = NULL;
26668 }
26669 }
26670 }
26671 else
26672 {
26673 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26674 /* Only replace it when it starts with '~' */
26675 if (*dirname == '~')
26676 {
26677 s = vim_strsave(dirname);
26678 if (s != NULL)
26679 {
26680 *fnamep = s;
26681 vim_free(*bufp);
26682 *bufp = s;
26683 }
26684 }
26685 }
26686 vim_free(pbuf);
26687 }
26688 }
26689
26690 tail = gettail(*fnamep);
26691 *fnamelen = (int)STRLEN(*fnamep);
26692
26693 /* ":h" - head, remove "/file_name", can be repeated */
26694 /* Don't remove the first "/" or "c:\" */
26695 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26696 {
26697 valid |= VALID_HEAD;
26698 *usedlen += 2;
26699 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026700 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026701 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026702 *fnamelen = (int)(tail - *fnamep);
26703#ifdef VMS
26704 if (*fnamelen > 0)
26705 *fnamelen += 1; /* the path separator is part of the path */
26706#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026707 if (*fnamelen == 0)
26708 {
26709 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26710 p = vim_strsave((char_u *)".");
26711 if (p == NULL)
26712 return -1;
26713 vim_free(*bufp);
26714 *bufp = *fnamep = tail = p;
26715 *fnamelen = 1;
26716 }
26717 else
26718 {
26719 while (tail > s && !after_pathsep(s, tail))
26720 mb_ptr_back(*fnamep, tail);
26721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026722 }
26723
26724 /* ":8" - shortname */
26725 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26726 {
26727 *usedlen += 2;
26728#ifdef WIN3264
26729 has_shortname = 1;
26730#endif
26731 }
26732
26733#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026734 /*
26735 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026736 */
26737 if (has_shortname)
26738 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026739 /* Copy the string if it is shortened by :h and when it wasn't copied
26740 * yet, because we are going to change it in place. Avoids changing
26741 * the buffer name for "%:8". */
26742 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026743 {
26744 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026745 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026746 return -1;
26747 vim_free(*bufp);
26748 *bufp = *fnamep = p;
26749 }
26750
26751 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026752 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026753 if (!has_fullname && !vim_isAbsName(*fnamep))
26754 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026755 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026756 return -1;
26757 }
26758 else
26759 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026760 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026761
Bram Moolenaardc935552011-08-17 15:23:23 +020026762 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026763 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026764 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026765 return -1;
26766
26767 if (l == 0)
26768 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026769 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026770 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026771 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026772 return -1;
26773 }
26774 *fnamelen = l;
26775 }
26776 }
26777#endif /* WIN3264 */
26778
26779 /* ":t" - tail, just the basename */
26780 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26781 {
26782 *usedlen += 2;
26783 *fnamelen -= (int)(tail - *fnamep);
26784 *fnamep = tail;
26785 }
26786
26787 /* ":e" - extension, can be repeated */
26788 /* ":r" - root, without extension, can be repeated */
26789 while (src[*usedlen] == ':'
26790 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26791 {
26792 /* find a '.' in the tail:
26793 * - for second :e: before the current fname
26794 * - otherwise: The last '.'
26795 */
26796 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26797 s = *fnamep - 2;
26798 else
26799 s = *fnamep + *fnamelen - 1;
26800 for ( ; s > tail; --s)
26801 if (s[0] == '.')
26802 break;
26803 if (src[*usedlen + 1] == 'e') /* :e */
26804 {
26805 if (s > tail)
26806 {
26807 *fnamelen += (int)(*fnamep - (s + 1));
26808 *fnamep = s + 1;
26809#ifdef VMS
26810 /* cut version from the extension */
26811 s = *fnamep + *fnamelen - 1;
26812 for ( ; s > *fnamep; --s)
26813 if (s[0] == ';')
26814 break;
26815 if (s > *fnamep)
26816 *fnamelen = s - *fnamep;
26817#endif
26818 }
26819 else if (*fnamep <= tail)
26820 *fnamelen = 0;
26821 }
26822 else /* :r */
26823 {
26824 if (s > tail) /* remove one extension */
26825 *fnamelen = (int)(s - *fnamep);
26826 }
26827 *usedlen += 2;
26828 }
26829
26830 /* ":s?pat?foo?" - substitute */
26831 /* ":gs?pat?foo?" - global substitute */
26832 if (src[*usedlen] == ':'
26833 && (src[*usedlen + 1] == 's'
26834 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26835 {
26836 char_u *str;
26837 char_u *pat;
26838 char_u *sub;
26839 int sep;
26840 char_u *flags;
26841 int didit = FALSE;
26842
26843 flags = (char_u *)"";
26844 s = src + *usedlen + 2;
26845 if (src[*usedlen + 1] == 'g')
26846 {
26847 flags = (char_u *)"g";
26848 ++s;
26849 }
26850
26851 sep = *s++;
26852 if (sep)
26853 {
26854 /* find end of pattern */
26855 p = vim_strchr(s, sep);
26856 if (p != NULL)
26857 {
26858 pat = vim_strnsave(s, (int)(p - s));
26859 if (pat != NULL)
26860 {
26861 s = p + 1;
26862 /* find end of substitution */
26863 p = vim_strchr(s, sep);
26864 if (p != NULL)
26865 {
26866 sub = vim_strnsave(s, (int)(p - s));
26867 str = vim_strnsave(*fnamep, *fnamelen);
26868 if (sub != NULL && str != NULL)
26869 {
26870 *usedlen = (int)(p + 1 - src);
26871 s = do_string_sub(str, pat, sub, flags);
26872 if (s != NULL)
26873 {
26874 *fnamep = s;
26875 *fnamelen = (int)STRLEN(s);
26876 vim_free(*bufp);
26877 *bufp = s;
26878 didit = TRUE;
26879 }
26880 }
26881 vim_free(sub);
26882 vim_free(str);
26883 }
26884 vim_free(pat);
26885 }
26886 }
26887 /* after using ":s", repeat all the modifiers */
26888 if (didit)
26889 goto repeat;
26890 }
26891 }
26892
Bram Moolenaar26df0922014-02-23 23:39:13 +010026893 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26894 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026895 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026896 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026897 if (c != NUL)
26898 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026899 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026900 if (c != NUL)
26901 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026902 if (p == NULL)
26903 return -1;
26904 vim_free(*bufp);
26905 *bufp = *fnamep = p;
26906 *fnamelen = (int)STRLEN(p);
26907 *usedlen += 2;
26908 }
26909
Bram Moolenaar071d4272004-06-13 20:20:40 +000026910 return valid;
26911}
26912
26913/*
26914 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26915 * "flags" can be "g" to do a global substitute.
26916 * Returns an allocated string, NULL for error.
26917 */
26918 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026919do_string_sub(
26920 char_u *str,
26921 char_u *pat,
26922 char_u *sub,
26923 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026924{
26925 int sublen;
26926 regmatch_T regmatch;
26927 int i;
26928 int do_all;
26929 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026930 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026931 garray_T ga;
26932 char_u *ret;
26933 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026934 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026935
26936 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26937 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026938 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026939
26940 ga_init2(&ga, 1, 200);
26941
26942 do_all = (flags[0] == 'g');
26943
26944 regmatch.rm_ic = p_ic;
26945 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26946 if (regmatch.regprog != NULL)
26947 {
26948 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026949 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026950 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26951 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026952 /* Skip empty match except for first match. */
26953 if (regmatch.startp[0] == regmatch.endp[0])
26954 {
26955 if (zero_width == regmatch.startp[0])
26956 {
26957 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026958 i = MB_PTR2LEN(tail);
26959 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26960 (size_t)i);
26961 ga.ga_len += i;
26962 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026963 continue;
26964 }
26965 zero_width = regmatch.startp[0];
26966 }
26967
Bram Moolenaar071d4272004-06-13 20:20:40 +000026968 /*
26969 * Get some space for a temporary buffer to do the substitution
26970 * into. It will contain:
26971 * - The text up to where the match is.
26972 * - The substituted text.
26973 * - The text after the match.
26974 */
26975 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026976 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026977 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26978 {
26979 ga_clear(&ga);
26980 break;
26981 }
26982
26983 /* copy the text up to where the match is */
26984 i = (int)(regmatch.startp[0] - tail);
26985 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26986 /* add the substituted text */
26987 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26988 + ga.ga_len + i, TRUE, TRUE, FALSE);
26989 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026990 tail = regmatch.endp[0];
26991 if (*tail == NUL)
26992 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026993 if (!do_all)
26994 break;
26995 }
26996
26997 if (ga.ga_data != NULL)
26998 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26999
Bram Moolenaar473de612013-06-08 18:19:48 +020027000 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027001 }
27002
27003 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27004 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027005 if (p_cpo == empty_option)
27006 p_cpo = save_cpo;
27007 else
27008 /* Darn, evaluating {sub} expression changed the value. */
27009 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027010
27011 return ret;
27012}
27013
27014#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */