blob: 225658921c09c1235b614826d5dd88aa24f898af [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 Moolenaar33570922005-01-25 22:26:29 +0000375};
376
377/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000378#define vv_type vv_di.di_tv.v_type
379#define vv_nr vv_di.di_tv.vval.v_number
380#define vv_float vv_di.di_tv.vval.v_float
381#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000382#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200383#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000384#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000385
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200386static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000387#define vimvarht vimvardict.dv_hashtab
388
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100389static void prepare_vimvar(int idx, typval_T *save_tv);
390static void restore_vimvar(int idx, typval_T *save_tv);
391static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
392static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
393static char_u *skip_var_one(char_u *arg);
394static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
395static void list_glob_vars(int *first);
396static void list_buf_vars(int *first);
397static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000398#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100399static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_vim_vars(int *first);
402static void list_script_vars(int *first);
403static void list_func_vars(int *first);
404static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
405static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
406static int check_changedtick(char_u *arg);
407static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
408static void clear_lval(lval_T *lp);
409static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
410static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
411static void list_fix_watch(list_T *l, listitem_T *item);
412static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
413static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
414static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
415static void item_lock(typval_T *tv, int deep, int lock);
416static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000417
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100418static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
419static int eval1(char_u **arg, typval_T *rettv, int evaluate);
420static int eval2(char_u **arg, typval_T *rettv, int evaluate);
421static int eval3(char_u **arg, typval_T *rettv, int evaluate);
422static int eval4(char_u **arg, typval_T *rettv, int evaluate);
423static int eval5(char_u **arg, typval_T *rettv, int evaluate);
424static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
425static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000426
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100427static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
428static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
429static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
432static long list_len(list_T *l);
433static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
434static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
435static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
436static long list_find_nr(list_T *l, long idx, int *errorp);
437static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100438static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
439static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
440static list_T *list_copy(list_T *orig, int deep, int copyID);
441static char_u *list2string(typval_T *tv, int copyID);
442static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
443static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
444static int free_unref_items(int copyID);
445static dictitem_T *dictitem_copy(dictitem_T *org);
446static void dictitem_remove(dict_T *dict, dictitem_T *item);
447static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
448static long dict_len(dict_T *d);
449static char_u *dict2string(typval_T *tv, int copyID);
450static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
451static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *string_quote(char_u *str, int function);
454static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
455static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100456static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
457static 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 +0100458static void emsg_funcname(char *ermsg, char_u *name);
459static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000460
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static void f_abs(typval_T *argvars, typval_T *rettv);
463static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_add(typval_T *argvars, typval_T *rettv);
466static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
467static void f_and(typval_T *argvars, typval_T *rettv);
468static void f_append(typval_T *argvars, typval_T *rettv);
469static void f_argc(typval_T *argvars, typval_T *rettv);
470static void f_argidx(typval_T *argvars, typval_T *rettv);
471static void f_arglistid(typval_T *argvars, typval_T *rettv);
472static void f_argv(typval_T *argvars, typval_T *rettv);
473static void f_assert_equal(typval_T *argvars, typval_T *rettv);
474static void f_assert_exception(typval_T *argvars, typval_T *rettv);
475static void f_assert_fails(typval_T *argvars, typval_T *rettv);
476static void f_assert_false(typval_T *argvars, typval_T *rettv);
477static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100479static void f_asin(typval_T *argvars, typval_T *rettv);
480static void f_atan(typval_T *argvars, typval_T *rettv);
481static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_browse(typval_T *argvars, typval_T *rettv);
484static void f_browsedir(typval_T *argvars, typval_T *rettv);
485static void f_bufexists(typval_T *argvars, typval_T *rettv);
486static void f_buflisted(typval_T *argvars, typval_T *rettv);
487static void f_bufloaded(typval_T *argvars, typval_T *rettv);
488static void f_bufname(typval_T *argvars, typval_T *rettv);
489static void f_bufnr(typval_T *argvars, typval_T *rettv);
490static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
491static void f_byte2line(typval_T *argvars, typval_T *rettv);
492static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
493static void f_byteidx(typval_T *argvars, typval_T *rettv);
494static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
495static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100500static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100501static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
502static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100503static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100505static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100506static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100507static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
508static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100509static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100510static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100511static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
512static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100513static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100514static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100516static void f_changenr(typval_T *argvars, typval_T *rettv);
517static void f_char2nr(typval_T *argvars, typval_T *rettv);
518static void f_cindent(typval_T *argvars, typval_T *rettv);
519static void f_clearmatches(typval_T *argvars, typval_T *rettv);
520static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000521#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100522static void f_complete(typval_T *argvars, typval_T *rettv);
523static void f_complete_add(typval_T *argvars, typval_T *rettv);
524static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000525#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_confirm(typval_T *argvars, typval_T *rettv);
527static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100529static void f_cos(typval_T *argvars, typval_T *rettv);
530static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000531#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_count(typval_T *argvars, typval_T *rettv);
533static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
534static void f_cursor(typval_T *argsvars, typval_T *rettv);
535static void f_deepcopy(typval_T *argvars, typval_T *rettv);
536static void f_delete(typval_T *argvars, typval_T *rettv);
537static void f_did_filetype(typval_T *argvars, typval_T *rettv);
538static void f_diff_filler(typval_T *argvars, typval_T *rettv);
539static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100540static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_empty(typval_T *argvars, typval_T *rettv);
542static void f_escape(typval_T *argvars, typval_T *rettv);
543static void f_eval(typval_T *argvars, typval_T *rettv);
544static void f_eventhandler(typval_T *argvars, typval_T *rettv);
545static void f_executable(typval_T *argvars, typval_T *rettv);
546static void f_exepath(typval_T *argvars, typval_T *rettv);
547static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_expand(typval_T *argvars, typval_T *rettv);
552static void f_extend(typval_T *argvars, typval_T *rettv);
553static void f_feedkeys(typval_T *argvars, typval_T *rettv);
554static void f_filereadable(typval_T *argvars, typval_T *rettv);
555static void f_filewritable(typval_T *argvars, typval_T *rettv);
556static void f_filter(typval_T *argvars, typval_T *rettv);
557static void f_finddir(typval_T *argvars, typval_T *rettv);
558static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000559#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_float2nr(typval_T *argvars, typval_T *rettv);
561static void f_floor(typval_T *argvars, typval_T *rettv);
562static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000563#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_fnameescape(typval_T *argvars, typval_T *rettv);
565static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
566static void f_foldclosed(typval_T *argvars, typval_T *rettv);
567static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
568static void f_foldlevel(typval_T *argvars, typval_T *rettv);
569static void f_foldtext(typval_T *argvars, typval_T *rettv);
570static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
571static void f_foreground(typval_T *argvars, typval_T *rettv);
572static void f_function(typval_T *argvars, typval_T *rettv);
573static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
574static void f_get(typval_T *argvars, typval_T *rettv);
575static void f_getbufline(typval_T *argvars, typval_T *rettv);
576static void f_getbufvar(typval_T *argvars, typval_T *rettv);
577static void f_getchar(typval_T *argvars, typval_T *rettv);
578static void f_getcharmod(typval_T *argvars, typval_T *rettv);
579static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
580static void f_getcmdline(typval_T *argvars, typval_T *rettv);
581static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
582static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
583static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
584static void f_getcwd(typval_T *argvars, typval_T *rettv);
585static void f_getfontname(typval_T *argvars, typval_T *rettv);
586static void f_getfperm(typval_T *argvars, typval_T *rettv);
587static void f_getfsize(typval_T *argvars, typval_T *rettv);
588static void f_getftime(typval_T *argvars, typval_T *rettv);
589static void f_getftype(typval_T *argvars, typval_T *rettv);
590static void f_getline(typval_T *argvars, typval_T *rettv);
591static void f_getmatches(typval_T *argvars, typval_T *rettv);
592static void f_getpid(typval_T *argvars, typval_T *rettv);
593static void f_getcurpos(typval_T *argvars, typval_T *rettv);
594static void f_getpos(typval_T *argvars, typval_T *rettv);
595static void f_getqflist(typval_T *argvars, typval_T *rettv);
596static void f_getreg(typval_T *argvars, typval_T *rettv);
597static void f_getregtype(typval_T *argvars, typval_T *rettv);
598static void f_gettabvar(typval_T *argvars, typval_T *rettv);
599static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
600static void f_getwinposx(typval_T *argvars, typval_T *rettv);
601static void f_getwinposy(typval_T *argvars, typval_T *rettv);
602static void f_getwinvar(typval_T *argvars, typval_T *rettv);
603static void f_glob(typval_T *argvars, typval_T *rettv);
604static void f_globpath(typval_T *argvars, typval_T *rettv);
605static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
606static void f_has(typval_T *argvars, typval_T *rettv);
607static void f_has_key(typval_T *argvars, typval_T *rettv);
608static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
609static void f_hasmapto(typval_T *argvars, typval_T *rettv);
610static void f_histadd(typval_T *argvars, typval_T *rettv);
611static void f_histdel(typval_T *argvars, typval_T *rettv);
612static void f_histget(typval_T *argvars, typval_T *rettv);
613static void f_histnr(typval_T *argvars, typval_T *rettv);
614static void f_hlID(typval_T *argvars, typval_T *rettv);
615static void f_hlexists(typval_T *argvars, typval_T *rettv);
616static void f_hostname(typval_T *argvars, typval_T *rettv);
617static void f_iconv(typval_T *argvars, typval_T *rettv);
618static void f_indent(typval_T *argvars, typval_T *rettv);
619static void f_index(typval_T *argvars, typval_T *rettv);
620static void f_input(typval_T *argvars, typval_T *rettv);
621static void f_inputdialog(typval_T *argvars, typval_T *rettv);
622static void f_inputlist(typval_T *argvars, typval_T *rettv);
623static void f_inputrestore(typval_T *argvars, typval_T *rettv);
624static void f_inputsave(typval_T *argvars, typval_T *rettv);
625static void f_inputsecret(typval_T *argvars, typval_T *rettv);
626static void f_insert(typval_T *argvars, typval_T *rettv);
627static void f_invert(typval_T *argvars, typval_T *rettv);
628static void f_isdirectory(typval_T *argvars, typval_T *rettv);
629static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100630#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
631static void f_isnan(typval_T *argvars, typval_T *rettv);
632#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100633static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100634#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100635static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100636static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100637static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100638static void f_job_start(typval_T *argvars, typval_T *rettv);
639static void f_job_stop(typval_T *argvars, typval_T *rettv);
640static void f_job_status(typval_T *argvars, typval_T *rettv);
641#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100642static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100643static void f_js_decode(typval_T *argvars, typval_T *rettv);
644static void f_js_encode(typval_T *argvars, typval_T *rettv);
645static void f_json_decode(typval_T *argvars, typval_T *rettv);
646static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100647static void f_keys(typval_T *argvars, typval_T *rettv);
648static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
649static void f_len(typval_T *argvars, typval_T *rettv);
650static void f_libcall(typval_T *argvars, typval_T *rettv);
651static void f_libcallnr(typval_T *argvars, typval_T *rettv);
652static void f_line(typval_T *argvars, typval_T *rettv);
653static void f_line2byte(typval_T *argvars, typval_T *rettv);
654static void f_lispindent(typval_T *argvars, typval_T *rettv);
655static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000656#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_log(typval_T *argvars, typval_T *rettv);
658static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000659#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200660#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200662#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100663static void f_map(typval_T *argvars, typval_T *rettv);
664static void f_maparg(typval_T *argvars, typval_T *rettv);
665static void f_mapcheck(typval_T *argvars, typval_T *rettv);
666static void f_match(typval_T *argvars, typval_T *rettv);
667static void f_matchadd(typval_T *argvars, typval_T *rettv);
668static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
669static void f_matcharg(typval_T *argvars, typval_T *rettv);
670static void f_matchdelete(typval_T *argvars, typval_T *rettv);
671static void f_matchend(typval_T *argvars, typval_T *rettv);
672static void f_matchlist(typval_T *argvars, typval_T *rettv);
673static void f_matchstr(typval_T *argvars, typval_T *rettv);
674static void f_max(typval_T *argvars, typval_T *rettv);
675static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000676#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100680#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100682#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
684static void f_nr2char(typval_T *argvars, typval_T *rettv);
685static void f_or(typval_T *argvars, typval_T *rettv);
686static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100687#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100689#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
694static void f_printf(typval_T *argvars, typval_T *rettv);
695static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#endif
699#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200701#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_range(typval_T *argvars, typval_T *rettv);
703static void f_readfile(typval_T *argvars, typval_T *rettv);
704static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100705#ifdef FEAT_FLOAT
706static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
707#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_reltimestr(typval_T *argvars, typval_T *rettv);
709static void f_remote_expr(typval_T *argvars, typval_T *rettv);
710static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
711static void f_remote_peek(typval_T *argvars, typval_T *rettv);
712static void f_remote_read(typval_T *argvars, typval_T *rettv);
713static void f_remote_send(typval_T *argvars, typval_T *rettv);
714static void f_remove(typval_T *argvars, typval_T *rettv);
715static void f_rename(typval_T *argvars, typval_T *rettv);
716static void f_repeat(typval_T *argvars, typval_T *rettv);
717static void f_resolve(typval_T *argvars, typval_T *rettv);
718static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_screenattr(typval_T *argvars, typval_T *rettv);
723static void f_screenchar(typval_T *argvars, typval_T *rettv);
724static void f_screencol(typval_T *argvars, typval_T *rettv);
725static void f_screenrow(typval_T *argvars, typval_T *rettv);
726static void f_search(typval_T *argvars, typval_T *rettv);
727static void f_searchdecl(typval_T *argvars, typval_T *rettv);
728static void f_searchpair(typval_T *argvars, typval_T *rettv);
729static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
730static void f_searchpos(typval_T *argvars, typval_T *rettv);
731static void f_server2client(typval_T *argvars, typval_T *rettv);
732static void f_serverlist(typval_T *argvars, typval_T *rettv);
733static void f_setbufvar(typval_T *argvars, typval_T *rettv);
734static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
735static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100736static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100737static void f_setline(typval_T *argvars, typval_T *rettv);
738static void f_setloclist(typval_T *argvars, typval_T *rettv);
739static void f_setmatches(typval_T *argvars, typval_T *rettv);
740static void f_setpos(typval_T *argvars, typval_T *rettv);
741static void f_setqflist(typval_T *argvars, typval_T *rettv);
742static void f_setreg(typval_T *argvars, typval_T *rettv);
743static void f_settabvar(typval_T *argvars, typval_T *rettv);
744static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
745static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100746#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100748#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_shellescape(typval_T *argvars, typval_T *rettv);
750static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
751static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000752#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100753static void f_sin(typval_T *argvars, typval_T *rettv);
754static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000755#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_sort(typval_T *argvars, typval_T *rettv);
757static void f_soundfold(typval_T *argvars, typval_T *rettv);
758static void f_spellbadword(typval_T *argvars, typval_T *rettv);
759static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
760static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000761#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_sqrt(typval_T *argvars, typval_T *rettv);
763static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_str2nr(typval_T *argvars, typval_T *rettv);
766static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000767#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000769#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_stridx(typval_T *argvars, typval_T *rettv);
771static void f_string(typval_T *argvars, typval_T *rettv);
772static void f_strlen(typval_T *argvars, typval_T *rettv);
773static void f_strpart(typval_T *argvars, typval_T *rettv);
774static void f_strridx(typval_T *argvars, typval_T *rettv);
775static void f_strtrans(typval_T *argvars, typval_T *rettv);
776static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
777static void f_strwidth(typval_T *argvars, typval_T *rettv);
778static void f_submatch(typval_T *argvars, typval_T *rettv);
779static void f_substitute(typval_T *argvars, typval_T *rettv);
780static void f_synID(typval_T *argvars, typval_T *rettv);
781static void f_synIDattr(typval_T *argvars, typval_T *rettv);
782static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
783static void f_synstack(typval_T *argvars, typval_T *rettv);
784static void f_synconcealed(typval_T *argvars, typval_T *rettv);
785static void f_system(typval_T *argvars, typval_T *rettv);
786static void f_systemlist(typval_T *argvars, typval_T *rettv);
787static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
788static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
789static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
790static void f_taglist(typval_T *argvars, typval_T *rettv);
791static void f_tagfiles(typval_T *argvars, typval_T *rettv);
792static void f_tempname(typval_T *argvars, typval_T *rettv);
793static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200794#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100795static void f_tan(typval_T *argvars, typval_T *rettv);
796static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200797#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100798#ifdef FEAT_TIMERS
799static void f_timer_start(typval_T *argvars, typval_T *rettv);
800static void f_timer_stop(typval_T *argvars, typval_T *rettv);
801#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100802static void f_tolower(typval_T *argvars, typval_T *rettv);
803static void f_toupper(typval_T *argvars, typval_T *rettv);
804static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000805#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100806static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000807#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100808static void f_type(typval_T *argvars, typval_T *rettv);
809static void f_undofile(typval_T *argvars, typval_T *rettv);
810static void f_undotree(typval_T *argvars, typval_T *rettv);
811static void f_uniq(typval_T *argvars, typval_T *rettv);
812static void f_values(typval_T *argvars, typval_T *rettv);
813static void f_virtcol(typval_T *argvars, typval_T *rettv);
814static void f_visualmode(typval_T *argvars, typval_T *rettv);
815static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100816static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100817static void f_win_getid(typval_T *argvars, typval_T *rettv);
818static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
819static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
820static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static void f_winbufnr(typval_T *argvars, typval_T *rettv);
822static void f_wincol(typval_T *argvars, typval_T *rettv);
823static void f_winheight(typval_T *argvars, typval_T *rettv);
824static void f_winline(typval_T *argvars, typval_T *rettv);
825static void f_winnr(typval_T *argvars, typval_T *rettv);
826static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
827static void f_winrestview(typval_T *argvars, typval_T *rettv);
828static void f_winsaveview(typval_T *argvars, typval_T *rettv);
829static void f_winwidth(typval_T *argvars, typval_T *rettv);
830static void f_writefile(typval_T *argvars, typval_T *rettv);
831static void f_wordcount(typval_T *argvars, typval_T *rettv);
832static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000833
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100834static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
835static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
836static int get_env_len(char_u **arg);
837static int get_id_len(char_u **arg);
838static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
839static 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 +0000840#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
841#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
842 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
844static int eval_isnamec(int c);
845static int eval_isnamec1(int c);
846static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
847static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100848static typval_T *alloc_string_tv(char_u *string);
849static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100850#ifdef FEAT_FLOAT
851static float_T get_tv_float(typval_T *varp);
852#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100853static linenr_T get_tv_lnum(typval_T *argvars);
854static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100855static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
856static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
857static hashtab_T *find_var_ht(char_u *name, char_u **varname);
858static funccall_T *get_funccal(void);
859static void vars_clear_ext(hashtab_T *ht, int free_val);
860static void delete_var(hashtab_T *ht, hashitem_T *hi);
861static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
862static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
863static void set_var(char_u *name, typval_T *varp, int copy);
864static int var_check_ro(int flags, char_u *name, int use_gettext);
865static int var_check_fixed(int flags, char_u *name, int use_gettext);
866static int var_check_func_name(char_u *name, int new_var);
867static int valid_varname(char_u *varname);
868static int tv_check_lock(int lock, char_u *name, int use_gettext);
869static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
870static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100871static 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 +0100872static int eval_fname_script(char_u *p);
873static int eval_fname_sid(char_u *p);
874static void list_func_head(ufunc_T *fp, int indent);
875static ufunc_T *find_func(char_u *name);
876static int function_exists(char_u *name);
877static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000878#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879static void func_do_profile(ufunc_T *fp);
880static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
881static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000882static int
883# ifdef __BORLANDC__
884 _RTLENTRYF
885# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000887static int
888# ifdef __BORLANDC__
889 _RTLENTRYF
890# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100891 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100893static int script_autoload(char_u *name, int reload);
894static char_u *autoload_name(char_u *name);
895static void cat_func_name(char_u *buf, ufunc_T *fp);
896static void func_free(ufunc_T *fp);
897static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
898static int can_free_funccal(funccall_T *fc, int copyID) ;
899static void free_funccal(funccall_T *fc, int free_val);
900static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
901static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
902static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
903static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
904static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
905static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
906static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
907static int write_list(FILE *fd, list_T *list, int binary);
908static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000909
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200910
911#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100912static int compare_func_name(const void *s1, const void *s2);
913static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200914#endif
915
Bram Moolenaar33570922005-01-25 22:26:29 +0000916/*
917 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000918 */
919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100920eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000921{
Bram Moolenaar33570922005-01-25 22:26:29 +0000922 int i;
923 struct vimvar *p;
924
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200925 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
926 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200927 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000928 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000929 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000930
931 for (i = 0; i < VV_LEN; ++i)
932 {
933 p = &vimvars[i];
934 STRCPY(p->vv_di.di_key, p->vv_name);
935 if (p->vv_flags & VV_RO)
936 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
937 else if (p->vv_flags & VV_RO_SBX)
938 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
939 else
940 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000941
942 /* add to v: scope dict, unless the value is not always available */
943 if (p->vv_type != VAR_UNKNOWN)
944 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000945 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000946 /* add to compat scope dict */
947 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000948 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100949 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
950
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000951 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100952 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200953 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100954 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100955
956 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
957 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
958 set_vim_var_nr(VV_NONE, VVAL_NONE);
959 set_vim_var_nr(VV_NULL, VVAL_NULL);
960
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200961 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200962
963#ifdef EBCDIC
964 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100965 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200966 */
967 sortFunctions();
968#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000969}
970
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000971#if defined(EXITFREE) || defined(PROTO)
972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100973eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000974{
975 int i;
976 struct vimvar *p;
977
978 for (i = 0; i < VV_LEN; ++i)
979 {
980 p = &vimvars[i];
981 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000982 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000983 vim_free(p->vv_str);
984 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000985 }
986 else if (p->vv_di.di_tv.v_type == VAR_LIST)
987 {
988 list_unref(p->vv_list);
989 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000990 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000991 }
992 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000993 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000994 hash_clear(&compat_hashtab);
995
Bram Moolenaard9fba312005-06-26 22:34:35 +0000996 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100997# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200998 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100999# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001000
1001 /* global variables */
1002 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001003
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001004 /* autoloaded script names */
1005 ga_clear_strings(&ga_loaded);
1006
Bram Moolenaarcca74132013-09-25 21:00:28 +02001007 /* Script-local variables. First clear all the variables and in a second
1008 * loop free the scriptvar_T, because a variable in one script might hold
1009 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001010 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001011 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001012 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001013 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001014 ga_clear(&ga_scripts);
1015
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001016 /* unreferenced lists and dicts */
1017 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001018
1019 /* functions */
1020 free_all_functions();
1021 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001022}
1023#endif
1024
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 * Return the name of the executed function.
1027 */
1028 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001029func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032}
1033
1034/*
1035 * Return the address holding the next breakpoint line for a funccall cookie.
1036 */
1037 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001038func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
Bram Moolenaar33570922005-01-25 22:26:29 +00001040 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041}
1042
1043/*
1044 * Return the address holding the debug tick for a funccall cookie.
1045 */
1046 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001047func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
Bram Moolenaar33570922005-01-25 22:26:29 +00001049 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050}
1051
1052/*
1053 * Return the nesting level for a funccall cookie.
1054 */
1055 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001056func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
Bram Moolenaar33570922005-01-25 22:26:29 +00001058 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059}
1060
1061/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001062funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001064/* pointer to list of previously used funccal, still around because some
1065 * item in it is still being used. */
1066funccall_T *previous_funccal = NULL;
1067
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068/*
1069 * Return TRUE when a function was ended by a ":return" command.
1070 */
1071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001072current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073{
1074 return current_funccal->returned;
1075}
1076
1077
1078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 * Set an internal variable to a string value. Creates the variable if it does
1080 * not already exist.
1081 */
1082 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001083set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001085 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001086 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088 val = vim_strsave(value);
1089 if (val != NULL)
1090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001092 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001094 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001095 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 }
1098}
1099
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001101static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102static char_u *redir_endp = NULL;
1103static char_u *redir_varname = NULL;
1104
1105/*
1106 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001107 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 * Returns OK if successfully completed the setup. FAIL otherwise.
1109 */
1110 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001111var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112{
1113 int save_emsg;
1114 int err;
1115 typval_T tv;
1116
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001117 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001118 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 {
1120 EMSG(_(e_invarg));
1121 return FAIL;
1122 }
1123
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001124 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 redir_varname = vim_strsave(name);
1126 if (redir_varname == NULL)
1127 return FAIL;
1128
1129 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1130 if (redir_lval == NULL)
1131 {
1132 var_redir_stop();
1133 return FAIL;
1134 }
1135
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 /* The output is stored in growarray "redir_ga" until redirection ends. */
1137 ga_init2(&redir_ga, (int)sizeof(char), 500);
1138
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001140 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001141 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1143 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001144 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 if (redir_endp != NULL && *redir_endp != NUL)
1146 /* Trailing characters are present after the variable name */
1147 EMSG(_(e_trailing));
1148 else
1149 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 var_redir_stop();
1152 return FAIL;
1153 }
1154
1155 /* check if we can write to the variable: set it to or append an empty
1156 * string */
1157 save_emsg = did_emsg;
1158 did_emsg = FALSE;
1159 tv.v_type = VAR_STRING;
1160 tv.vval.v_string = (char_u *)"";
1161 if (append)
1162 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1163 else
1164 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001167 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 if (err)
1169 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001170 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 var_redir_stop();
1172 return FAIL;
1173 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174
1175 return OK;
1176}
1177
1178/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001179 * Append "value[value_len]" to the variable set by var_redir_start().
1180 * The actual appending is postponed until redirection ends, because the value
1181 * appended may in fact be the string we write to, changing it may cause freed
1182 * memory to be used:
1183 * :redir => foo
1184 * :let foo
1185 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 */
1187 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001188var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001190 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191
1192 if (redir_lval == NULL)
1193 return;
1194
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001195 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001196 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001200 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001201 {
1202 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001203 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001204 }
1205 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207}
1208
1209/*
1210 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001211 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 */
1213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001214var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001215{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216 typval_T tv;
1217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001218 if (redir_lval != NULL)
1219 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001220 /* If there was no error: assign the text to the variable. */
1221 if (redir_endp != NULL)
1222 {
1223 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1224 tv.v_type = VAR_STRING;
1225 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001226 /* Call get_lval() again, if it's inside a Dict or List it may
1227 * have changed. */
1228 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001229 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001230 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1231 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1232 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001233 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001235 /* free the collected output */
1236 vim_free(redir_ga.ga_data);
1237 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001238
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001239 vim_free(redir_lval);
1240 redir_lval = NULL;
1241 }
1242 vim_free(redir_varname);
1243 redir_varname = NULL;
1244}
1245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246# if defined(FEAT_MBYTE) || defined(PROTO)
1247 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001248eval_charconvert(
1249 char_u *enc_from,
1250 char_u *enc_to,
1251 char_u *fname_from,
1252 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253{
1254 int err = FALSE;
1255
1256 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1257 set_vim_var_string(VV_CC_TO, enc_to, -1);
1258 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1259 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1260 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1261 err = TRUE;
1262 set_vim_var_string(VV_CC_FROM, NULL, -1);
1263 set_vim_var_string(VV_CC_TO, NULL, -1);
1264 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1265 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1266
1267 if (err)
1268 return FAIL;
1269 return OK;
1270}
1271# endif
1272
1273# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001275eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276{
1277 int err = FALSE;
1278
1279 set_vim_var_string(VV_FNAME_IN, fname, -1);
1280 set_vim_var_string(VV_CMDARG, args, -1);
1281 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1282 err = TRUE;
1283 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1284 set_vim_var_string(VV_CMDARG, NULL, -1);
1285
1286 if (err)
1287 {
1288 mch_remove(fname);
1289 return FAIL;
1290 }
1291 return OK;
1292}
1293# endif
1294
1295# if defined(FEAT_DIFF) || defined(PROTO)
1296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001297eval_diff(
1298 char_u *origfile,
1299 char_u *newfile,
1300 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
1302 int err = FALSE;
1303
1304 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1305 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1306 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1307 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1308 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1309 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1310 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1311}
1312
1313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001314eval_patch(
1315 char_u *origfile,
1316 char_u *difffile,
1317 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318{
1319 int err;
1320
1321 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1322 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1323 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1324 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1325 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1326 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1327 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1328}
1329# endif
1330
1331/*
1332 * Top level evaluation function, returning a boolean.
1333 * Sets "error" to TRUE if there was an error.
1334 * Return TRUE or FALSE.
1335 */
1336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001337eval_to_bool(
1338 char_u *arg,
1339 int *error,
1340 char_u **nextcmd,
1341 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 int retval = FALSE;
1345
1346 if (skip)
1347 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 else
1351 {
1352 *error = FALSE;
1353 if (!skip)
1354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001355 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358 }
1359 if (skip)
1360 --emsg_skip;
1361
1362 return retval;
1363}
1364
1365/*
1366 * Top level evaluation function, returning a string. If "skip" is TRUE,
1367 * only parsing to "nextcmd" is done, without reporting errors. Return
1368 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1369 */
1370 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001371eval_to_string_skip(
1372 char_u *arg,
1373 char_u **nextcmd,
1374 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
Bram Moolenaar33570922005-01-25 22:26:29 +00001376 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 char_u *retval;
1378
1379 if (skip)
1380 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 retval = NULL;
1383 else
1384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001385 retval = vim_strsave(get_tv_string(&tv));
1386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 if (skip)
1389 --emsg_skip;
1390
1391 return retval;
1392}
1393
1394/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395 * Skip over an expression at "*pp".
1396 * Return FAIL for an error, OK otherwise.
1397 */
1398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001399skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001400{
Bram Moolenaar33570922005-01-25 22:26:29 +00001401 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001402
1403 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001404 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001405}
1406
1407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001409 * When "convert" is TRUE convert a List into a sequence of lines and convert
1410 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 * Return pointer to allocated memory, or NULL for failure.
1412 */
1413 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001414eval_to_string(
1415 char_u *arg,
1416 char_u **nextcmd,
1417 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
Bram Moolenaar33570922005-01-25 22:26:29 +00001419 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001421 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001422#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001423 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 retval = NULL;
1428 else
1429 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001430 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001431 {
1432 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001433 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001434 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001435 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001436 if (tv.vval.v_list->lv_len > 0)
1437 ga_append(&ga, NL);
1438 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001439 ga_append(&ga, NUL);
1440 retval = (char_u *)ga.ga_data;
1441 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001442#ifdef FEAT_FLOAT
1443 else if (convert && tv.v_type == VAR_FLOAT)
1444 {
1445 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1446 retval = vim_strsave(numbuf);
1447 }
1448#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001449 else
1450 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453
1454 return retval;
1455}
1456
1457/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001458 * Call eval_to_string() without using current local variables and using
1459 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 */
1461 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001462eval_to_string_safe(
1463 char_u *arg,
1464 char_u **nextcmd,
1465 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 char_u *retval;
1468 void *save_funccalp;
1469
1470 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001471 if (use_sandbox)
1472 ++sandbox;
1473 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001474 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001475 if (use_sandbox)
1476 --sandbox;
1477 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 restore_funccal(save_funccalp);
1479 return retval;
1480}
1481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482/*
1483 * Top level evaluation function, returning a number.
1484 * Evaluates "expr" silently.
1485 * Returns -1 for an error.
1486 */
1487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001488eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
Bram Moolenaar33570922005-01-25 22:26:29 +00001490 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001492 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494 ++emsg_off;
1495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 retval = -1;
1498 else
1499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001500 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
1503 --emsg_off;
1504
1505 return retval;
1506}
1507
Bram Moolenaara40058a2005-07-11 22:42:07 +00001508/*
1509 * Prepare v: variable "idx" to be used.
1510 * Save the current typeval in "save_tv".
1511 * When not used yet add the variable to the v: hashtable.
1512 */
1513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001514prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001515{
1516 *save_tv = vimvars[idx].vv_tv;
1517 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1518 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1519}
1520
1521/*
1522 * Restore v: variable "idx" to typeval "save_tv".
1523 * When no longer defined, remove the variable from the v: hashtable.
1524 */
1525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001526restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001527{
1528 hashitem_T *hi;
1529
Bram Moolenaara40058a2005-07-11 22:42:07 +00001530 vimvars[idx].vv_tv = *save_tv;
1531 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1532 {
1533 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1534 if (HASHITEM_EMPTY(hi))
1535 EMSG2(_(e_intern2), "restore_vimvar()");
1536 else
1537 hash_remove(&vimvarht, hi);
1538 }
1539}
1540
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001541#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001542/*
1543 * Evaluate an expression to a list with suggestions.
1544 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001545 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001546 */
1547 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001548eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001549{
1550 typval_T save_val;
1551 typval_T rettv;
1552 list_T *list = NULL;
1553 char_u *p = skipwhite(expr);
1554
1555 /* Set "v:val" to the bad word. */
1556 prepare_vimvar(VV_VAL, &save_val);
1557 vimvars[VV_VAL].vv_type = VAR_STRING;
1558 vimvars[VV_VAL].vv_str = badword;
1559 if (p_verbose == 0)
1560 ++emsg_off;
1561
1562 if (eval1(&p, &rettv, TRUE) == OK)
1563 {
1564 if (rettv.v_type != VAR_LIST)
1565 clear_tv(&rettv);
1566 else
1567 list = rettv.vval.v_list;
1568 }
1569
1570 if (p_verbose == 0)
1571 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001572 restore_vimvar(VV_VAL, &save_val);
1573
1574 return list;
1575}
1576
1577/*
1578 * "list" is supposed to contain two items: a word and a number. Return the
1579 * word in "pp" and the number as the return value.
1580 * Return -1 if anything isn't right.
1581 * Used to get the good word and score from the eval_spell_expr() result.
1582 */
1583 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001584get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001585{
1586 listitem_T *li;
1587
1588 li = list->lv_first;
1589 if (li == NULL)
1590 return -1;
1591 *pp = get_tv_string(&li->li_tv);
1592
1593 li = li->li_next;
1594 if (li == NULL)
1595 return -1;
1596 return get_tv_number(&li->li_tv);
1597}
1598#endif
1599
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001601 * Top level evaluation function.
1602 * Returns an allocated typval_T with the result.
1603 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001604 */
1605 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001606eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607{
1608 typval_T *tv;
1609
1610 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001611 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001612 {
1613 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001614 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001615 }
1616
1617 return tv;
1618}
1619
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001622 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 * Uses argv[argc] for the function arguments. Only Number and String
1624 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001628call_vim_function(
1629 char_u *func,
1630 int argc,
1631 char_u **argv,
1632 int safe, /* use the sandbox */
1633 int str_arg_only, /* all arguments are strings */
1634 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635{
Bram Moolenaar33570922005-01-25 22:26:29 +00001636 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 long n;
1638 int len;
1639 int i;
1640 int doesrange;
1641 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001644 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648 for (i = 0; i < argc; i++)
1649 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001650 /* Pass a NULL or empty argument as an empty string */
1651 if (argv[i] == NULL || *argv[i] == NUL)
1652 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001653 argvars[i].v_type = VAR_STRING;
1654 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001655 continue;
1656 }
1657
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001658 if (str_arg_only)
1659 len = 0;
1660 else
1661 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001662 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (len != 0 && len == (int)STRLEN(argv[i]))
1664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001665 argvars[i].v_type = VAR_NUMBER;
1666 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 else
1669 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001670 argvars[i].v_type = VAR_STRING;
1671 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 }
1673 }
1674
1675 if (safe)
1676 {
1677 save_funccalp = save_funccal();
1678 ++sandbox;
1679 }
1680
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1682 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001684 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 if (safe)
1686 {
1687 --sandbox;
1688 restore_funccal(save_funccalp);
1689 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690 vim_free(argvars);
1691
1692 if (ret == FAIL)
1693 clear_tv(rettv);
1694
1695 return ret;
1696}
1697
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001698/*
1699 * Call vimL function "func" and return the result as a number.
1700 * Returns -1 when calling the function fails.
1701 * Uses argv[argc] for the function arguments.
1702 */
1703 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001704call_func_retnr(
1705 char_u *func,
1706 int argc,
1707 char_u **argv,
1708 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001709{
1710 typval_T rettv;
1711 long retval;
1712
1713 /* All arguments are passed as strings, no conversion to number. */
1714 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1715 return -1;
1716
1717 retval = get_tv_number_chk(&rettv, NULL);
1718 clear_tv(&rettv);
1719 return retval;
1720}
1721
1722#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1723 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1724
Bram Moolenaar4f688582007-07-24 12:34:30 +00001725# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001727 * Call vimL function "func" and return the result as a string.
1728 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729 * Uses argv[argc] for the function arguments.
1730 */
1731 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001732call_func_retstr(
1733 char_u *func,
1734 int argc,
1735 char_u **argv,
1736 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737{
1738 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001739 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001741 /* All arguments are passed as strings, no conversion to number. */
1742 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743 return NULL;
1744
1745 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001746 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 return retval;
1748}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001749# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001751/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001752 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001754 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755 */
1756 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001757call_func_retlist(
1758 char_u *func,
1759 int argc,
1760 char_u **argv,
1761 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001762{
1763 typval_T rettv;
1764
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001765 /* All arguments are passed as strings, no conversion to number. */
1766 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767 return NULL;
1768
1769 if (rettv.v_type != VAR_LIST)
1770 {
1771 clear_tv(&rettv);
1772 return NULL;
1773 }
1774
1775 return rettv.vval.v_list;
1776}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#endif
1778
1779/*
1780 * Save the current function call pointer, and set it to NULL.
1781 * Used when executing autocommands and for ":source".
1782 */
1783 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001784save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001786 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 current_funccal = NULL;
1789 return (void *)fc;
1790}
1791
1792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001793restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001795 funccall_T *fc = (funccall_T *)vfc;
1796
1797 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798}
1799
Bram Moolenaar05159a02005-02-26 23:04:13 +00001800#if defined(FEAT_PROFILE) || defined(PROTO)
1801/*
1802 * Prepare profiling for entering a child or something else that is not
1803 * counted for the script/function itself.
1804 * Should always be called in pair with prof_child_exit().
1805 */
1806 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001807prof_child_enter(
1808 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001809{
1810 funccall_T *fc = current_funccal;
1811
1812 if (fc != NULL && fc->func->uf_profiling)
1813 profile_start(&fc->prof_child);
1814 script_prof_save(tm);
1815}
1816
1817/*
1818 * Take care of time spent in a child.
1819 * Should always be called after prof_child_enter().
1820 */
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822prof_child_exit(
1823 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001824{
1825 funccall_T *fc = current_funccal;
1826
1827 if (fc != NULL && fc->func->uf_profiling)
1828 {
1829 profile_end(&fc->prof_child);
1830 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1831 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1832 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1833 }
1834 script_prof_restore(tm);
1835}
1836#endif
1837
1838
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839#ifdef FEAT_FOLDING
1840/*
1841 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1842 * it in "*cp". Doesn't give error messages.
1843 */
1844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001845eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
Bram Moolenaar33570922005-01-25 22:26:29 +00001847 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 int retval;
1849 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001850 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1851 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001854 if (use_sandbox)
1855 ++sandbox;
1856 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 retval = 0;
1860 else
1861 {
1862 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 if (tv.v_type == VAR_NUMBER)
1864 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001865 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 retval = 0;
1867 else
1868 {
1869 /* If the result is a string, check if there is a non-digit before
1870 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (!VIM_ISDIGIT(*s) && *s != '-')
1873 *cp = *s++;
1874 retval = atol((char *)s);
1875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 }
1878 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001879 if (use_sandbox)
1880 --sandbox;
1881 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883 return retval;
1884}
1885#endif
1886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 * ":let" list all variable values
1889 * ":let var1 var2" list variable values
1890 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 * ":let var += expr" assignment command.
1892 * ":let var -= expr" assignment command.
1893 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 */
1896 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001897ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001901 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 int var_count = 0;
1904 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001905 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001906 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001907 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
Bram Moolenaardb552d602006-03-23 22:59:57 +00001909 argend = skip_var_list(arg, &var_count, &semicolon);
1910 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001912 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1913 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001914 expr = skipwhite(argend);
1915 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1916 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001918 /*
1919 * ":let" without "=": list variables
1920 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 if (*arg == '[')
1922 EMSG(_(e_invarg));
1923 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001929 list_glob_vars(&first);
1930 list_buf_vars(&first);
1931 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001932#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001933 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001934#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001935 list_script_vars(&first);
1936 list_func_vars(&first);
1937 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 eap->nextcmd = check_nextcmd(arg);
1940 }
1941 else
1942 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 op[0] = '=';
1944 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001945 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001947 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1948 op[0] = *expr; /* +=, -= or .= */
1949 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001951 else
1952 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (eap->skip)
1955 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if (eap->skip)
1958 {
1959 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 --emsg_skip;
1962 }
1963 else if (i != FAIL)
1964 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001966 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
1969 }
1970}
1971
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972/*
1973 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1974 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001975 * When "nextchars" is not NULL it points to a string with characters that
1976 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1977 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 * Returns OK or FAIL;
1979 */
1980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001981ex_let_vars(
1982 char_u *arg_start,
1983 typval_T *tv,
1984 int copy, /* copy values from "tv", don't move */
1985 int semicolon, /* from skip_var_list() */
1986 int var_count, /* from skip_var_list() */
1987 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988{
1989 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001990 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001992 listitem_T *item;
1993 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994
1995 if (*arg != '[')
1996 {
1997 /*
1998 * ":let var = expr" or ":for var in list"
1999 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 return FAIL;
2002 return OK;
2003 }
2004
2005 /*
2006 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2007 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002008 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 {
2010 EMSG(_(e_listreq));
2011 return FAIL;
2012 }
2013
2014 i = list_len(l);
2015 if (semicolon == 0 && var_count < i)
2016 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002017 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 return FAIL;
2019 }
2020 if (var_count - semicolon > i)
2021 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002022 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 return FAIL;
2024 }
2025
2026 item = l->lv_first;
2027 while (*arg != ']')
2028 {
2029 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 item = item->li_next;
2032 if (arg == NULL)
2033 return FAIL;
2034
2035 arg = skipwhite(arg);
2036 if (*arg == ';')
2037 {
2038 /* Put the rest of the list (may be empty) in the var after ';'.
2039 * Create a new list for this. */
2040 l = list_alloc();
2041 if (l == NULL)
2042 return FAIL;
2043 while (item != NULL)
2044 {
2045 list_append_tv(l, &item->li_tv);
2046 item = item->li_next;
2047 }
2048
2049 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002050 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 ltv.vval.v_list = l;
2052 l->lv_refcount = 1;
2053
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002054 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2055 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 clear_tv(&ltv);
2057 if (arg == NULL)
2058 return FAIL;
2059 break;
2060 }
2061 else if (*arg != ',' && *arg != ']')
2062 {
2063 EMSG2(_(e_intern2), "ex_let_vars()");
2064 return FAIL;
2065 }
2066 }
2067
2068 return OK;
2069}
2070
2071/*
2072 * Skip over assignable variable "var" or list of variables "[var, var]".
2073 * Used for ":let varvar = expr" and ":for varvar in expr".
2074 * For "[var, var]" increment "*var_count" for each variable.
2075 * for "[var, var; var]" set "semicolon".
2076 * Return NULL for an error.
2077 */
2078 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002079skip_var_list(
2080 char_u *arg,
2081 int *var_count,
2082 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002083{
2084 char_u *p, *s;
2085
2086 if (*arg == '[')
2087 {
2088 /* "[var, var]": find the matching ']'. */
2089 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002090 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002091 {
2092 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2093 s = skip_var_one(p);
2094 if (s == p)
2095 {
2096 EMSG2(_(e_invarg2), p);
2097 return NULL;
2098 }
2099 ++*var_count;
2100
2101 p = skipwhite(s);
2102 if (*p == ']')
2103 break;
2104 else if (*p == ';')
2105 {
2106 if (*semicolon == 1)
2107 {
2108 EMSG(_("Double ; in list of variables"));
2109 return NULL;
2110 }
2111 *semicolon = 1;
2112 }
2113 else if (*p != ',')
2114 {
2115 EMSG2(_(e_invarg2), p);
2116 return NULL;
2117 }
2118 }
2119 return p + 1;
2120 }
2121 else
2122 return skip_var_one(arg);
2123}
2124
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002125/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002126 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002127 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002128 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002130skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002132 if (*arg == '@' && arg[1] != NUL)
2133 return arg + 2;
2134 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2135 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002136}
2137
Bram Moolenaara7043832005-01-21 11:56:39 +00002138/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002139 * List variables for hashtab "ht" with prefix "prefix".
2140 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002141 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002143list_hashtable_vars(
2144 hashtab_T *ht,
2145 char_u *prefix,
2146 int empty,
2147 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 hashitem_T *hi;
2150 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002151 int todo;
2152
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002153 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002154 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2155 {
2156 if (!HASHITEM_EMPTY(hi))
2157 {
2158 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002159 di = HI2DI(hi);
2160 if (empty || di->di_tv.v_type != VAR_STRING
2161 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002163 }
2164 }
2165}
2166
2167/*
2168 * List global variables.
2169 */
2170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002172{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002174}
2175
2176/*
2177 * List buffer variables.
2178 */
2179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002180list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002181{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182 char_u numbuf[NUMBUFLEN];
2183
Bram Moolenaar429fa852013-04-15 12:27:36 +02002184 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186
2187 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2189 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002190}
2191
2192/*
2193 * List window variables.
2194 */
2195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002196list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002197{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002198 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002200}
2201
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002202#ifdef FEAT_WINDOWS
2203/*
2204 * List tab page variables.
2205 */
2206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002207list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002208{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002209 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002211}
2212#endif
2213
Bram Moolenaara7043832005-01-21 11:56:39 +00002214/*
2215 * List Vim variables.
2216 */
2217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221}
2222
2223/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224 * List script-local variables, if there is a script.
2225 */
2226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002228{
2229 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2231 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002232}
2233
2234/*
2235 * List function variables, if there is a function.
2236 */
2237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002239{
2240 if (current_funccal != NULL)
2241 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002243}
2244
2245/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 * List variables in "arg".
2247 */
2248 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002249list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250{
2251 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 char_u *name_start;
2255 char_u *arg_subsc;
2256 char_u *tofree;
2257 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258
2259 while (!ends_excmd(*arg) && !got_int)
2260 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002263 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2265 {
2266 emsg_severe = TRUE;
2267 EMSG(_(e_trailing));
2268 break;
2269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 /* get_name_len() takes care of expanding curly braces */
2274 name_start = name = arg;
2275 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2276 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 /* This is mainly to keep test 49 working: when expanding
2279 * curly braces fails overrule the exception error message. */
2280 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 emsg_severe = TRUE;
2283 EMSG2(_(e_invarg2), arg);
2284 break;
2285 }
2286 error = TRUE;
2287 }
2288 else
2289 {
2290 if (tofree != NULL)
2291 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002292 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 else
2295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 /* handle d.key, l[idx], f(expr) */
2297 arg_subsc = arg;
2298 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002306 case 'g': list_glob_vars(first); break;
2307 case 'b': list_buf_vars(first); break;
2308 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002309#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002310 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002311#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002312 case 'v': list_vim_vars(first); break;
2313 case 's': list_script_vars(first); break;
2314 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 default:
2316 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002317 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 }
2319 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320 {
2321 char_u numbuf[NUMBUFLEN];
2322 char_u *tf;
2323 int c;
2324 char_u *s;
2325
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002326 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002327 c = *arg;
2328 *arg = NUL;
2329 list_one_var_a((char_u *)"",
2330 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002331 tv.v_type,
2332 s == NULL ? (char_u *)"" : s,
2333 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334 *arg = c;
2335 vim_free(tf);
2336 }
2337 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 }
2340 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002341
2342 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002344
2345 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 }
2347
2348 return arg;
2349}
2350
2351/*
2352 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2353 * Returns a pointer to the char just after the var name.
2354 * Returns NULL if there is an error.
2355 */
2356 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002357ex_let_one(
2358 char_u *arg, /* points to variable name */
2359 typval_T *tv, /* value to assign to variable */
2360 int copy, /* copy value from "tv" */
2361 char_u *endchars, /* valid chars after variable name or NULL */
2362 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363{
2364 int c1;
2365 char_u *name;
2366 char_u *p;
2367 char_u *arg_end = NULL;
2368 int len;
2369 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371
2372 /*
2373 * ":let $VAR = expr": Set environment variable.
2374 */
2375 if (*arg == '$')
2376 {
2377 /* Find the end of the name. */
2378 ++arg;
2379 name = arg;
2380 len = get_env_len(&arg);
2381 if (len == 0)
2382 EMSG2(_(e_invarg2), name - 1);
2383 else
2384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 if (op != NULL && (*op == '+' || *op == '-'))
2386 EMSG2(_(e_letwrong), op);
2387 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002388 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002390 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 {
2392 c1 = name[len];
2393 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 p = get_tv_string_chk(tv);
2395 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 {
2397 int mustfree = FALSE;
2398 char_u *s = vim_getenv(name, &mustfree);
2399
2400 if (s != NULL)
2401 {
2402 p = tofree = concat_str(s, p);
2403 if (mustfree)
2404 vim_free(s);
2405 }
2406 }
2407 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 if (STRICMP(name, "HOME") == 0)
2411 init_homedir();
2412 else if (didset_vim && STRICMP(name, "VIM") == 0)
2413 didset_vim = FALSE;
2414 else if (didset_vimruntime
2415 && STRICMP(name, "VIMRUNTIME") == 0)
2416 didset_vimruntime = FALSE;
2417 arg_end = arg;
2418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 }
2422 }
2423 }
2424
2425 /*
2426 * ":let &option = expr": Set option value.
2427 * ":let &l:option = expr": Set local option value.
2428 * ":let &g:option = expr": Set global option value.
2429 */
2430 else if (*arg == '&')
2431 {
2432 /* Find the end of the name. */
2433 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002434 if (p == NULL || (endchars != NULL
2435 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 EMSG(_(e_letunexp));
2437 else
2438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 long n;
2440 int opt_type;
2441 long numval;
2442 char_u *stringval = NULL;
2443 char_u *s;
2444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445 c1 = *p;
2446 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447
2448 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 s = get_tv_string_chk(tv); /* != NULL if number or string */
2450 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 {
2452 opt_type = get_option_value(arg, &numval,
2453 &stringval, opt_flags);
2454 if ((opt_type == 1 && *op == '.')
2455 || (opt_type == 0 && *op != '.'))
2456 EMSG2(_(e_letwrong), op);
2457 else
2458 {
2459 if (opt_type == 1) /* number */
2460 {
2461 if (*op == '+')
2462 n = numval + n;
2463 else
2464 n = numval - n;
2465 }
2466 else if (opt_type == 0 && stringval != NULL) /* string */
2467 {
2468 s = concat_str(stringval, s);
2469 vim_free(stringval);
2470 stringval = s;
2471 }
2472 }
2473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 if (s != NULL)
2475 {
2476 set_option_value(arg, n, s, opt_flags);
2477 arg_end = p;
2478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 }
2482 }
2483
2484 /*
2485 * ":let @r = expr": Set register contents.
2486 */
2487 else if (*arg == '@')
2488 {
2489 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 if (op != NULL && (*op == '+' || *op == '-'))
2491 EMSG2(_(e_letwrong), op);
2492 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002493 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 EMSG(_(e_letunexp));
2495 else
2496 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002497 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 char_u *s;
2499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 p = get_tv_string_chk(tv);
2501 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002503 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002504 if (s != NULL)
2505 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002506 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 vim_free(s);
2508 }
2509 }
2510 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002513 arg_end = arg + 1;
2514 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002515 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 }
2517 }
2518
2519 /*
2520 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002527 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2531 EMSG(_(e_letunexp));
2532 else
2533 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 arg_end = p;
2536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 }
2540
2541 else
2542 EMSG2(_(e_invarg2), arg);
2543
2544 return arg_end;
2545}
2546
2547/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2549 */
2550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002551check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552{
2553 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2554 {
2555 EMSG2(_(e_readonlyvar), arg);
2556 return TRUE;
2557 }
2558 return FALSE;
2559}
2560
2561/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 * Get an lval: variable, Dict item or List item that can be assigned a value
2563 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2564 * "name.key", "name.key[expr]" etc.
2565 * Indexing only works if "name" is an existing List or Dictionary.
2566 * "name" points to the start of the name.
2567 * If "rettv" is not NULL it points to the value to be assigned.
2568 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2569 * wrong; must end in space or cmd separator.
2570 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002571 * flags:
2572 * GLV_QUIET: do not give error messages
2573 * GLV_NO_AUTOLOAD: do not use script autoloading
2574 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002576 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578 */
2579 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002580get_lval(
2581 char_u *name,
2582 typval_T *rettv,
2583 lval_T *lp,
2584 int unlet,
2585 int skip,
2586 int flags, /* GLV_ values */
2587 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 char_u *expr_start, *expr_end;
2591 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 dictitem_T *v;
2593 typval_T var1;
2594 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002596 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002600 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002603 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604
2605 if (skip)
2606 {
2607 /* When skipping just find the end of the name. */
2608 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002609 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 }
2611
2612 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002613 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (expr_start != NULL)
2615 {
2616 /* Don't expand the name when we already know there is an error. */
2617 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2618 && *p != '[' && *p != '.')
2619 {
2620 EMSG(_(e_trailing));
2621 return NULL;
2622 }
2623
2624 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2625 if (lp->ll_exp_name == NULL)
2626 {
2627 /* Report an invalid expression in braces, unless the
2628 * expression evaluation has been cancelled due to an
2629 * aborting error, an interrupt, or an exception. */
2630 if (!aborting() && !quiet)
2631 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002632 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 EMSG2(_(e_invarg2), name);
2634 return NULL;
2635 }
2636 }
2637 lp->ll_name = lp->ll_exp_name;
2638 }
2639 else
2640 lp->ll_name = name;
2641
2642 /* Without [idx] or .key we are done. */
2643 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2644 return p;
2645
2646 cc = *p;
2647 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002648 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (v == NULL && !quiet)
2650 EMSG2(_(e_undefvar), lp->ll_name);
2651 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 if (v == NULL)
2653 return NULL;
2654
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 /*
2656 * Loop until no more [idx] or .key is following.
2657 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002658 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2662 && !(lp->ll_tv->v_type == VAR_DICT
2663 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_("E689: Can only index a List or Dictionary"));
2667 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 if (!quiet)
2672 EMSG(_("E708: [:] must come last"));
2673 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002674 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 len = -1;
2677 if (*p == '.')
2678 {
2679 key = p + 1;
2680 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2681 ;
2682 if (len == 0)
2683 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (!quiet)
2685 EMSG(_(e_emptykey));
2686 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
2688 p = key + len;
2689 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 else
2691 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (*p == ':')
2695 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 else
2697 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 empty1 = FALSE;
2699 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002701 if (get_tv_string_chk(&var1) == NULL)
2702 {
2703 /* not a number or string */
2704 clear_tv(&var1);
2705 return NULL;
2706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 }
2708
2709 /* Optionally get the second index [ :expr]. */
2710 if (*p == ':')
2711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716 if (!empty1)
2717 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (rettv != NULL && (rettv->v_type != VAR_LIST
2721 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (!quiet)
2724 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (!empty1)
2726 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
2729 p = skipwhite(p + 1);
2730 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 else
2733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2736 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (!empty1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002741 if (get_tv_string_chk(&var2) == NULL)
2742 {
2743 /* not a number or string */
2744 if (!empty1)
2745 clear_tv(&var1);
2746 clear_tv(&var2);
2747 return NULL;
2748 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002756 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (!quiet)
2758 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 if (!empty1)
2760 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 }
2765
2766 /* Skip to past ']'. */
2767 ++p;
2768 }
2769
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 {
2772 if (len == -1)
2773 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002775 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (*key == NUL)
2777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!quiet)
2779 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 }
2783 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002784 lp->ll_list = NULL;
2785 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002786 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 /* When assigning to a scope dictionary check that a function and
2789 * variable name is valid (only variable name unless it is l: or
2790 * g: dictionary). Disallow overwriting a builtin function. */
2791 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002792 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002793 int prevval;
2794 int wrong;
2795
2796 if (len != -1)
2797 {
2798 prevval = key[len];
2799 key[len] = NUL;
2800 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002801 else
2802 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002803 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2804 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002806 || !valid_varname(key);
2807 if (len != -1)
2808 key[len] = prevval;
2809 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 return NULL;
2811 }
2812
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002815 /* Can't add "v:" variable. */
2816 if (lp->ll_dict == &vimvardict)
2817 {
2818 EMSG2(_(e_illvar), name);
2819 return NULL;
2820 }
2821
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002822 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002826 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 if (len == -1)
2828 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 }
2831 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (len == -1)
2836 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 p = NULL;
2839 break;
2840 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002841 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002842 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 return NULL;
2844
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 if (len == -1)
2846 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 }
2849 else
2850 {
2851 /*
2852 * Get the number and item for the only or first index of the List.
2853 */
2854 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 else
2857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var1);
2860 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002861 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_list = lp->ll_tv->vval.v_list;
2863 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2864 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002866 if (lp->ll_n1 < 0)
2867 {
2868 lp->ll_n1 = 0;
2869 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2870 }
2871 }
2872 if (lp->ll_li == NULL)
2873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 if (!quiet)
2877 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 }
2880
2881 /*
2882 * May need to find the item or absolute index for the second
2883 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 * When no index given: "lp->ll_empty2" is TRUE.
2885 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002889 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 {
2896 if (!quiet)
2897 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002899 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 }
2902
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2904 if (lp->ll_n1 < 0)
2905 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2906 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002907 {
2908 if (!quiet)
2909 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002911 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002912 }
2913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002916 }
2917
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 return p;
2919}
2920
2921/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002922 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 */
2924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926{
2927 vim_free(lp->ll_exp_name);
2928 vim_free(lp->ll_newkey);
2929}
2930
2931/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002932 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002934 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 */
2936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002937set_var_lval(
2938 lval_T *lp,
2939 char_u *endp,
2940 typval_T *rettv,
2941 int copy,
2942 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943{
2944 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002945 listitem_T *ri;
2946 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947
2948 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002949 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 cc = *endp;
2953 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 if (op != NULL && *op != '=')
2955 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002956 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002959 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002960 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002961 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002963 if ((di == NULL
2964 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2965 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2966 FALSE)))
2967 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968 set_var(lp->ll_name, &tv, FALSE);
2969 clear_tv(&tv);
2970 }
2971 }
2972 else
2973 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002977 else if (tv_check_lock(lp->ll_newkey == NULL
2978 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002979 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002980 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 else if (lp->ll_range)
2982 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002983 listitem_T *ll_li = lp->ll_li;
2984 int ll_n1 = lp->ll_n1;
2985
2986 /*
2987 * Check whether any of the list items is locked
2988 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002989 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002990 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002991 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002992 return;
2993 ri = ri->li_next;
2994 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2995 break;
2996 ll_li = ll_li->li_next;
2997 ++ll_n1;
2998 }
2999
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 /*
3001 * Assign the List values to the list items.
3002 */
3003 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 if (op != NULL && *op != '=')
3006 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3007 else
3008 {
3009 clear_tv(&lp->ll_li->li_tv);
3010 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3011 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 ri = ri->li_next;
3013 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3014 break;
3015 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003016 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003018 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 ri = NULL;
3021 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003022 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003023 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 lp->ll_li = lp->ll_li->li_next;
3025 ++lp->ll_n1;
3026 }
3027 if (ri != NULL)
3028 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 else if (lp->ll_empty2
3030 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 : lp->ll_n1 != lp->ll_n2)
3032 EMSG(_("E711: List value has not enough items"));
3033 }
3034 else
3035 {
3036 /*
3037 * Assign to a List or Dictionary item.
3038 */
3039 if (lp->ll_newkey != NULL)
3040 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003041 if (op != NULL && *op != '=')
3042 {
3043 EMSG2(_(e_letwrong), op);
3044 return;
3045 }
3046
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003048 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 if (di == NULL)
3050 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003051 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3052 {
3053 vim_free(di);
3054 return;
3055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003057 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 else if (op != NULL && *op != '=')
3059 {
3060 tv_op(lp->ll_tv, rettv, op);
3061 return;
3062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003063 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003065
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 /*
3067 * Assign the value to the variable or list item.
3068 */
3069 if (copy)
3070 copy_tv(rettv, lp->ll_tv);
3071 else
3072 {
3073 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003074 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076 }
3077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078}
3079
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003080/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3082 * Returns OK or FAIL.
3083 */
3084 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003085tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086{
3087 long n;
3088 char_u numbuf[NUMBUFLEN];
3089 char_u *s;
3090
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003091 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3092 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3093 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094 {
3095 switch (tv1->v_type)
3096 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003097 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 case VAR_DICT:
3099 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003100 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003101 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003102 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003103 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003104 break;
3105
3106 case VAR_LIST:
3107 if (*op != '+' || tv2->v_type != VAR_LIST)
3108 break;
3109 /* List += List */
3110 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3111 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3112 return OK;
3113
3114 case VAR_NUMBER:
3115 case VAR_STRING:
3116 if (tv2->v_type == VAR_LIST)
3117 break;
3118 if (*op == '+' || *op == '-')
3119 {
3120 /* nr += nr or nr -= nr*/
3121 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122#ifdef FEAT_FLOAT
3123 if (tv2->v_type == VAR_FLOAT)
3124 {
3125 float_T f = n;
3126
3127 if (*op == '+')
3128 f += tv2->vval.v_float;
3129 else
3130 f -= tv2->vval.v_float;
3131 clear_tv(tv1);
3132 tv1->v_type = VAR_FLOAT;
3133 tv1->vval.v_float = f;
3134 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136#endif
3137 {
3138 if (*op == '+')
3139 n += get_tv_number(tv2);
3140 else
3141 n -= get_tv_number(tv2);
3142 clear_tv(tv1);
3143 tv1->v_type = VAR_NUMBER;
3144 tv1->vval.v_number = n;
3145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 }
3147 else
3148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149 if (tv2->v_type == VAR_FLOAT)
3150 break;
3151
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003152 /* str .= str */
3153 s = get_tv_string(tv1);
3154 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3155 clear_tv(tv1);
3156 tv1->v_type = VAR_STRING;
3157 tv1->vval.v_string = s;
3158 }
3159 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003162#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003163 {
3164 float_T f;
3165
3166 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3167 && tv2->v_type != VAR_NUMBER
3168 && tv2->v_type != VAR_STRING))
3169 break;
3170 if (tv2->v_type == VAR_FLOAT)
3171 f = tv2->vval.v_float;
3172 else
3173 f = get_tv_number(tv2);
3174 if (*op == '+')
3175 tv1->vval.v_float += f;
3176 else
3177 tv1->vval.v_float -= f;
3178 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003180 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 }
3182 }
3183
3184 EMSG2(_(e_letwrong), op);
3185 return FAIL;
3186}
3187
3188/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 * Add a watcher to a list.
3190 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003192list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
3194 lw->lw_next = l->lv_watch;
3195 l->lv_watch = lw;
3196}
3197
3198/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003199 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 * No warning when it isn't found...
3201 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003203list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204{
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206
3207 lwp = &l->lv_watch;
3208 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3209 {
3210 if (lw == lwrem)
3211 {
3212 *lwp = lw->lw_next;
3213 break;
3214 }
3215 lwp = &lw->lw_next;
3216 }
3217}
3218
3219/*
3220 * Just before removing an item from a list: advance watchers to the next
3221 * item.
3222 */
3223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225{
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227
3228 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3229 if (lw->lw_item == item)
3230 lw->lw_item = item->li_next;
3231}
3232
3233/*
3234 * Evaluate the expression used in a ":for var in expr" command.
3235 * "arg" points to "var".
3236 * Set "*errp" to TRUE for an error, FALSE otherwise;
3237 * Return a pointer that holds the info. Null when there is an error.
3238 */
3239 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240eval_for_line(
3241 char_u *arg,
3242 int *errp,
3243 char_u **nextcmdp,
3244 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245{
Bram Moolenaar33570922005-01-25 22:26:29 +00003246 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 typval_T tv;
3249 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250
3251 *errp = TRUE; /* default: there is an error */
3252
Bram Moolenaar33570922005-01-25 22:26:29 +00003253 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 if (fi == NULL)
3255 return NULL;
3256
3257 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3258 if (expr == NULL)
3259 return fi;
3260
3261 expr = skipwhite(expr);
3262 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3263 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003264 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 return fi;
3266 }
3267
3268 if (skip)
3269 ++emsg_skip;
3270 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3271 {
3272 *errp = FALSE;
3273 if (!skip)
3274 {
3275 l = tv.vval.v_list;
3276 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003279 clear_tv(&tv);
3280 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 else
3282 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003283 /* No need to increment the refcount, it's already set for the
3284 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 fi->fi_list = l;
3286 list_add_watch(l, &fi->fi_lw);
3287 fi->fi_lw.lw_item = l->lv_first;
3288 }
3289 }
3290 }
3291 if (skip)
3292 --emsg_skip;
3293
3294 return fi;
3295}
3296
3297/*
3298 * Use the first item in a ":for" list. Advance to the next.
3299 * Assign the values to the variable (list). "arg" points to the first one.
3300 * Return TRUE when a valid item was found, FALSE when at end of list or
3301 * something wrong.
3302 */
3303 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003304next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003306 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003308 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309
3310 item = fi->fi_lw.lw_item;
3311 if (item == NULL)
3312 result = FALSE;
3313 else
3314 {
3315 fi->fi_lw.lw_item = item->li_next;
3316 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3317 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3318 }
3319 return result;
3320}
3321
3322/*
3323 * Free the structure used to store info used by ":for".
3324 */
3325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003326free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327{
Bram Moolenaar33570922005-01-25 22:26:29 +00003328 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003330 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003331 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003333 list_unref(fi->fi_list);
3334 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335 vim_free(fi);
3336}
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3339
3340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003341set_context_for_expression(
3342 expand_T *xp,
3343 char_u *arg,
3344 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 int got_eq = FALSE;
3347 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 if (cmdidx == CMD_let)
3351 {
3352 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003353 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 {
3355 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003356 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003357 {
3358 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003359 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360 if (vim_iswhite(*p))
3361 break;
3362 }
3363 return;
3364 }
3365 }
3366 else
3367 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3368 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 while ((xp->xp_pattern = vim_strpbrk(arg,
3370 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3371 {
3372 c = *xp->xp_pattern;
3373 if (c == '&')
3374 {
3375 c = xp->xp_pattern[1];
3376 if (c == '&')
3377 {
3378 ++xp->xp_pattern;
3379 xp->xp_context = cmdidx != CMD_let || got_eq
3380 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3381 }
3382 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003385 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3386 xp->xp_pattern += 2;
3387
3388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390 else if (c == '$')
3391 {
3392 /* environment variable */
3393 xp->xp_context = EXPAND_ENV_VARS;
3394 }
3395 else if (c == '=')
3396 {
3397 got_eq = TRUE;
3398 xp->xp_context = EXPAND_EXPRESSION;
3399 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003400 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 && xp->xp_context == EXPAND_FUNCTIONS
3402 && vim_strchr(xp->xp_pattern, '(') == NULL)
3403 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003404 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 break;
3406 }
3407 else if (cmdidx != CMD_let || got_eq)
3408 {
3409 if (c == '"') /* string */
3410 {
3411 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3412 if (c == '\\' && xp->xp_pattern[1] != NUL)
3413 ++xp->xp_pattern;
3414 xp->xp_context = EXPAND_NOTHING;
3415 }
3416 else if (c == '\'') /* literal string */
3417 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003418 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3420 /* skip */ ;
3421 xp->xp_context = EXPAND_NOTHING;
3422 }
3423 else if (c == '|')
3424 {
3425 if (xp->xp_pattern[1] == '|')
3426 {
3427 ++xp->xp_pattern;
3428 xp->xp_context = EXPAND_EXPRESSION;
3429 }
3430 else
3431 xp->xp_context = EXPAND_COMMANDS;
3432 }
3433 else
3434 xp->xp_context = EXPAND_EXPRESSION;
3435 }
3436 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003437 /* Doesn't look like something valid, expand as an expression
3438 * anyway. */
3439 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 arg = xp->xp_pattern;
3441 if (*arg != NUL)
3442 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3443 /* skip */ ;
3444 }
3445 xp->xp_pattern = arg;
3446}
3447
3448#endif /* FEAT_CMDL_COMPL */
3449
3450/*
3451 * ":1,25call func(arg1, arg2)" function call.
3452 */
3453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003454ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 char_u *arg = eap->arg;
3457 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003459 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 linenr_T lnum;
3463 int doesrange;
3464 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003465 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003466 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003468 if (eap->skip)
3469 {
3470 /* trans_function_name() doesn't work well when skipping, use eval0()
3471 * instead to skip to any following command, e.g. for:
3472 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003473 ++emsg_skip;
3474 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3475 clear_tv(&rettv);
3476 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003477 return;
3478 }
3479
Bram Moolenaar65639032016-03-16 21:40:30 +01003480 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003481 if (fudi.fd_newkey != NULL)
3482 {
3483 /* Still need to give an error message for missing key. */
3484 EMSG2(_(e_dictkey), fudi.fd_newkey);
3485 vim_free(fudi.fd_newkey);
3486 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003487 if (tofree == NULL)
3488 return;
3489
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003490 /* Increase refcount on dictionary, it could get deleted when evaluating
3491 * the arguments. */
3492 if (fudi.fd_dict != NULL)
3493 ++fudi.fd_dict->dv_refcount;
3494
Bram Moolenaar65639032016-03-16 21:40:30 +01003495 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3496 * contents. For VAR_PARTIAL get its partial, unless we already have one
3497 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003498 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003499 name = deref_func_name(tofree, &len,
3500 partial != NULL ? NULL : &partial, FALSE);
3501
Bram Moolenaar532c7802005-01-27 14:44:31 +00003502 /* Skip white space to allow ":call func ()". Not good, but required for
3503 * backward compatibility. */
3504 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003505 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 if (*startarg != '(')
3508 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003509 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 goto end;
3511 }
3512
3513 /*
3514 * When skipping, evaluate the function once, to find the end of the
3515 * arguments.
3516 * When the function takes a range, this is discovered after the first
3517 * call, and the loop is broken.
3518 */
3519 if (eap->skip)
3520 {
3521 ++emsg_skip;
3522 lnum = eap->line2; /* do it once, also with an invalid range */
3523 }
3524 else
3525 lnum = eap->line1;
3526 for ( ; lnum <= eap->line2; ++lnum)
3527 {
3528 if (!eap->skip && eap->addr_count > 0)
3529 {
3530 curwin->w_cursor.lnum = lnum;
3531 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003532#ifdef FEAT_VIRTUALEDIT
3533 curwin->w_cursor.coladd = 0;
3534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003537 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003538 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003539 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 failed = TRUE;
3542 break;
3543 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003544
3545 /* Handle a function returning a Funcref, Dictionary or List. */
3546 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3547 {
3548 failed = TRUE;
3549 break;
3550 }
3551
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003552 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (doesrange || eap->skip)
3554 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003558 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003559 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (aborting())
3561 break;
3562 }
3563 if (eap->skip)
3564 --emsg_skip;
3565
3566 if (!failed)
3567 {
3568 /* Check for trailing illegal characters and a following command. */
3569 if (!ends_excmd(*arg))
3570 {
3571 emsg_severe = TRUE;
3572 EMSG(_(e_trailing));
3573 }
3574 else
3575 eap->nextcmd = check_nextcmd(arg);
3576 }
3577
3578end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003579 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003580 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
3582
3583/*
3584 * ":unlet[!] var1 ... " command.
3585 */
3586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 ex_unletlock(eap, eap->arg, 0);
3590}
3591
3592/*
3593 * ":lockvar" and ":unlockvar" commands
3594 */
3595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003596ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 int deep = 2;
3600
3601 if (eap->forceit)
3602 deep = -1;
3603 else if (vim_isdigit(*arg))
3604 {
3605 deep = getdigits(&arg);
3606 arg = skipwhite(arg);
3607 }
3608
3609 ex_unletlock(eap, arg, deep);
3610}
3611
3612/*
3613 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3614 */
3615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616ex_unletlock(
3617 exarg_T *eap,
3618 char_u *argstart,
3619 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003620{
3621 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
3626 do
3627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003629 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003630 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 if (lv.ll_name == NULL)
3632 error = TRUE; /* error but continue parsing */
3633 if (name_end == NULL || (!vim_iswhite(*name_end)
3634 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 if (name_end != NULL)
3637 {
3638 emsg_severe = TRUE;
3639 EMSG(_(e_trailing));
3640 }
3641 if (!(eap->skip || error))
3642 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 break;
3644 }
3645
3646 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 {
3648 if (eap->cmdidx == CMD_unlet)
3649 {
3650 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3651 error = TRUE;
3652 }
3653 else
3654 {
3655 if (do_lock_var(&lv, name_end, deep,
3656 eap->cmdidx == CMD_lockvar) == FAIL)
3657 error = TRUE;
3658 }
3659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 if (!eap->skip)
3662 clear_lval(&lv);
3663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 arg = skipwhite(name_end);
3665 } while (!ends_excmd(*arg));
3666
3667 eap->nextcmd = check_nextcmd(arg);
3668}
3669
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003671do_unlet_var(
3672 lval_T *lp,
3673 char_u *name_end,
3674 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003675{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 int ret = OK;
3677 int cc;
3678
3679 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003681 cc = *name_end;
3682 *name_end = NUL;
3683
3684 /* Normal name or expanded name. */
3685 if (check_changedtick(lp->ll_name))
3686 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003687 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003690 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003691 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003692 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003693 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003694 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003695 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003696 else if (lp->ll_range)
3697 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003698 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003699 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003700 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003701
3702 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3703 {
3704 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003705 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003706 return FAIL;
3707 ll_li = li;
3708 ++ll_n1;
3709 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710
3711 /* Delete a range of List items. */
3712 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3713 {
3714 li = lp->ll_li->li_next;
3715 listitem_remove(lp->ll_list, lp->ll_li);
3716 lp->ll_li = li;
3717 ++lp->ll_n1;
3718 }
3719 }
3720 else
3721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003722 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003723 /* unlet a List item. */
3724 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003725 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003727 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 }
3729
3730 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003731}
3732
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733/*
3734 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003735 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
3737 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003738do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 hashtab_T *ht;
3741 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003742 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003743 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003744 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
Bram Moolenaar33570922005-01-25 22:26:29 +00003746 ht = find_var_ht(name, &varname);
3747 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003749 if (ht == &globvarht)
3750 d = &globvardict;
3751 else if (current_funccal != NULL
3752 && ht == &current_funccal->l_vars.dv_hashtab)
3753 d = &current_funccal->l_vars;
3754 else if (ht == &compat_hashtab)
3755 d = &vimvardict;
3756 else
3757 {
3758 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3759 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3760 }
3761 if (d == NULL)
3762 {
3763 EMSG2(_(e_intern2), "do_unlet()");
3764 return FAIL;
3765 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003766 hi = hash_find(ht, varname);
3767 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003768 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003769 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003770 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003771 || var_check_ro(di->di_flags, name, FALSE)
3772 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003773 return FAIL;
3774
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 delete_var(ht, hi);
3776 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779 if (forceit)
3780 return OK;
3781 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 return FAIL;
3783}
3784
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003785/*
3786 * Lock or unlock variable indicated by "lp".
3787 * "deep" is the levels to go (-1 for unlimited);
3788 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3789 */
3790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003791do_lock_var(
3792 lval_T *lp,
3793 char_u *name_end,
3794 int deep,
3795 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003796{
3797 int ret = OK;
3798 int cc;
3799 dictitem_T *di;
3800
3801 if (deep == 0) /* nothing to do */
3802 return OK;
3803
3804 if (lp->ll_tv == NULL)
3805 {
3806 cc = *name_end;
3807 *name_end = NUL;
3808
3809 /* Normal name or expanded name. */
3810 if (check_changedtick(lp->ll_name))
3811 ret = FAIL;
3812 else
3813 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003814 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815 if (di == NULL)
3816 ret = FAIL;
3817 else
3818 {
3819 if (lock)
3820 di->di_flags |= DI_FLAGS_LOCK;
3821 else
3822 di->di_flags &= ~DI_FLAGS_LOCK;
3823 item_lock(&di->di_tv, deep, lock);
3824 }
3825 }
3826 *name_end = cc;
3827 }
3828 else if (lp->ll_range)
3829 {
3830 listitem_T *li = lp->ll_li;
3831
3832 /* (un)lock a range of List items. */
3833 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3834 {
3835 item_lock(&li->li_tv, deep, lock);
3836 li = li->li_next;
3837 ++lp->ll_n1;
3838 }
3839 }
3840 else if (lp->ll_list != NULL)
3841 /* (un)lock a List item. */
3842 item_lock(&lp->ll_li->li_tv, deep, lock);
3843 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003844 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003845 item_lock(&lp->ll_di->di_tv, deep, lock);
3846
3847 return ret;
3848}
3849
3850/*
3851 * Lock or unlock an item. "deep" is nr of levels to go.
3852 */
3853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003854item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003855{
3856 static int recurse = 0;
3857 list_T *l;
3858 listitem_T *li;
3859 dict_T *d;
3860 hashitem_T *hi;
3861 int todo;
3862
3863 if (recurse >= DICT_MAXNEST)
3864 {
3865 EMSG(_("E743: variable nested too deep for (un)lock"));
3866 return;
3867 }
3868 if (deep == 0)
3869 return;
3870 ++recurse;
3871
3872 /* lock/unlock the item itself */
3873 if (lock)
3874 tv->v_lock |= VAR_LOCKED;
3875 else
3876 tv->v_lock &= ~VAR_LOCKED;
3877
3878 switch (tv->v_type)
3879 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003880 case VAR_UNKNOWN:
3881 case VAR_NUMBER:
3882 case VAR_STRING:
3883 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003884 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003885 case VAR_FLOAT:
3886 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003887 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003888 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003889 break;
3890
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003891 case VAR_LIST:
3892 if ((l = tv->vval.v_list) != NULL)
3893 {
3894 if (lock)
3895 l->lv_lock |= VAR_LOCKED;
3896 else
3897 l->lv_lock &= ~VAR_LOCKED;
3898 if (deep < 0 || deep > 1)
3899 /* recursive: lock/unlock the items the List contains */
3900 for (li = l->lv_first; li != NULL; li = li->li_next)
3901 item_lock(&li->li_tv, deep - 1, lock);
3902 }
3903 break;
3904 case VAR_DICT:
3905 if ((d = tv->vval.v_dict) != NULL)
3906 {
3907 if (lock)
3908 d->dv_lock |= VAR_LOCKED;
3909 else
3910 d->dv_lock &= ~VAR_LOCKED;
3911 if (deep < 0 || deep > 1)
3912 {
3913 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003914 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003915 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3916 {
3917 if (!HASHITEM_EMPTY(hi))
3918 {
3919 --todo;
3920 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3921 }
3922 }
3923 }
3924 }
3925 }
3926 --recurse;
3927}
3928
Bram Moolenaara40058a2005-07-11 22:42:07 +00003929/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003930 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3931 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003932 */
3933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003934tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003935{
3936 return (tv->v_lock & VAR_LOCKED)
3937 || (tv->v_type == VAR_LIST
3938 && tv->vval.v_list != NULL
3939 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3940 || (tv->v_type == VAR_DICT
3941 && tv->vval.v_dict != NULL
3942 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3943}
3944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3946/*
3947 * Delete all "menutrans_" variables.
3948 */
3949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003950del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951{
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003956 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 {
3959 if (!HASHITEM_EMPTY(hi))
3960 {
3961 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3963 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 }
3965 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967}
3968#endif
3969
3970#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3971
3972/*
3973 * Local string buffer for the next two functions to store a variable name
3974 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3975 * get_user_var_name().
3976 */
3977
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003978static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980static char_u *varnamebuf = NULL;
3981static int varnamebuflen = 0;
3982
3983/*
3984 * Function to concatenate a prefix and a variable name.
3985 */
3986 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003987cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988{
3989 int len;
3990
3991 len = (int)STRLEN(name) + 3;
3992 if (len > varnamebuflen)
3993 {
3994 vim_free(varnamebuf);
3995 len += 10; /* some additional space */
3996 varnamebuf = alloc(len);
3997 if (varnamebuf == NULL)
3998 {
3999 varnamebuflen = 0;
4000 return NULL;
4001 }
4002 varnamebuflen = len;
4003 }
4004 *varnamebuf = prefix;
4005 varnamebuf[1] = ':';
4006 STRCPY(varnamebuf + 2, name);
4007 return varnamebuf;
4008}
4009
4010/*
4011 * Function given to ExpandGeneric() to obtain the list of user defined
4012 * (global/buffer/window/built-in) variable names.
4013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004015get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004017 static long_u gdone;
4018 static long_u bdone;
4019 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 static long_u tdone;
4022#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004023 static int vidx;
4024 static hashitem_T *hi;
4025 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004030#ifdef FEAT_WINDOWS
4031 tdone = 0;
4032#endif
4033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034
4035 /* Global variables */
4036 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004038 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004040 else
4041 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 while (HASHITEM_EMPTY(hi))
4043 ++hi;
4044 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4045 return cat_prefix_varname('g', hi->hi_key);
4046 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
4049 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004050 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004054 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004055 else
4056 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004057 while (HASHITEM_EMPTY(hi))
4058 ++hi;
4059 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004063 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return (char_u *)"b:changedtick";
4065 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004066
4067 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004068 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004071 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004073 else
4074 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004075 while (HASHITEM_EMPTY(hi))
4076 ++hi;
4077 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004079
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004080#ifdef FEAT_WINDOWS
4081 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004082 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004083 if (tdone < ht->ht_used)
4084 {
4085 if (tdone++ == 0)
4086 hi = ht->ht_array;
4087 else
4088 ++hi;
4089 while (HASHITEM_EMPTY(hi))
4090 ++hi;
4091 return cat_prefix_varname('t', hi->hi_key);
4092 }
4093#endif
4094
Bram Moolenaar33570922005-01-25 22:26:29 +00004095 /* v: variables */
4096 if (vidx < VV_LEN)
4097 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099 vim_free(varnamebuf);
4100 varnamebuf = NULL;
4101 varnamebuflen = 0;
4102 return NULL;
4103}
4104
4105#endif /* FEAT_CMDL_COMPL */
4106
4107/*
4108 * types for expressions.
4109 */
4110typedef enum
4111{
4112 TYPE_UNKNOWN = 0
4113 , TYPE_EQUAL /* == */
4114 , TYPE_NEQUAL /* != */
4115 , TYPE_GREATER /* > */
4116 , TYPE_GEQUAL /* >= */
4117 , TYPE_SMALLER /* < */
4118 , TYPE_SEQUAL /* <= */
4119 , TYPE_MATCH /* =~ */
4120 , TYPE_NOMATCH /* !~ */
4121} exptype_T;
4122
4123/*
4124 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4127 */
4128
4129/*
4130 * Handle zero level expression.
4131 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004133 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 * Return OK or FAIL.
4135 */
4136 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004137eval0(
4138 char_u *arg,
4139 typval_T *rettv,
4140 char_u **nextcmd,
4141 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 int ret;
4144 char_u *p;
4145
4146 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (ret == FAIL || !ends_excmd(*p))
4149 {
4150 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 /*
4153 * Report the invalid expression unless the expression evaluation has
4154 * been cancelled due to an aborting error, an interrupt, or an
4155 * exception.
4156 */
4157 if (!aborting())
4158 EMSG2(_(e_invexpr2), arg);
4159 ret = FAIL;
4160 }
4161 if (nextcmd != NULL)
4162 *nextcmd = check_nextcmd(p);
4163
4164 return ret;
4165}
4166
4167/*
4168 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004169 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 *
4171 * "arg" must point to the first non-white of the expression.
4172 * "arg" is advanced to the next non-white after the recognized expression.
4173 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004174 * Note: "rettv.v_lock" is not set.
4175 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 * Return OK or FAIL.
4177 */
4178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004179eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180{
4181 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004182 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 /*
4185 * Get the first variable.
4186 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return FAIL;
4189
4190 if ((*arg)[0] == '?')
4191 {
4192 result = FALSE;
4193 if (evaluate)
4194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 int error = FALSE;
4196
4197 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004200 if (error)
4201 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203
4204 /*
4205 * Get the second variable.
4206 */
4207 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210
4211 /*
4212 * Check for the ":".
4213 */
4214 if ((*arg)[0] != ':')
4215 {
4216 EMSG(_("E109: Missing ':' after '?'"));
4217 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return FAIL;
4220 }
4221
4222 /*
4223 * Get the third variable.
4224 */
4225 *arg = skipwhite(*arg + 1);
4226 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4227 {
4228 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return FAIL;
4231 }
4232 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235
4236 return OK;
4237}
4238
4239/*
4240 * Handle first level expression:
4241 * expr2 || expr2 || expr2 logical OR
4242 *
4243 * "arg" must point to the first non-white of the expression.
4244 * "arg" is advanced to the next non-white after the recognized expression.
4245 *
4246 * Return OK or FAIL.
4247 */
4248 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004249eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
Bram Moolenaar33570922005-01-25 22:26:29 +00004251 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 long result;
4253 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
4256 /*
4257 * Get the first variable.
4258 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 return FAIL;
4261
4262 /*
4263 * Repeat until there is no following "||".
4264 */
4265 first = TRUE;
4266 result = FALSE;
4267 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4268 {
4269 if (evaluate && first)
4270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 if (error)
4275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 first = FALSE;
4277 }
4278
4279 /*
4280 * Get the second variable.
4281 */
4282 *arg = skipwhite(*arg + 2);
4283 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4284 return FAIL;
4285
4286 /*
4287 * Compute the result.
4288 */
4289 if (evaluate && !result)
4290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004291 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 if (error)
4295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 if (evaluate)
4298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 rettv->v_type = VAR_NUMBER;
4300 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
4303
4304 return OK;
4305}
4306
4307/*
4308 * Handle second level expression:
4309 * expr3 && expr3 && expr3 logical AND
4310 *
4311 * "arg" must point to the first non-white of the expression.
4312 * "arg" is advanced to the next non-white after the recognized expression.
4313 *
4314 * Return OK or FAIL.
4315 */
4316 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004317eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
Bram Moolenaar33570922005-01-25 22:26:29 +00004319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 long result;
4321 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 /*
4325 * Get the first variable.
4326 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 return FAIL;
4329
4330 /*
4331 * Repeat until there is no following "&&".
4332 */
4333 first = TRUE;
4334 result = TRUE;
4335 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4336 {
4337 if (evaluate && first)
4338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (error)
4343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 first = FALSE;
4345 }
4346
4347 /*
4348 * Get the second variable.
4349 */
4350 *arg = skipwhite(*arg + 2);
4351 if (eval4(arg, &var2, evaluate && result) == FAIL)
4352 return FAIL;
4353
4354 /*
4355 * Compute the result.
4356 */
4357 if (evaluate && result)
4358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004359 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004362 if (error)
4363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 if (evaluate)
4366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004367 rettv->v_type = VAR_NUMBER;
4368 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 }
4371
4372 return OK;
4373}
4374
4375/*
4376 * Handle third level expression:
4377 * var1 == var2
4378 * var1 =~ var2
4379 * var1 != var2
4380 * var1 !~ var2
4381 * var1 > var2
4382 * var1 >= var2
4383 * var1 < var2
4384 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004385 * var1 is var2
4386 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 *
4388 * "arg" must point to the first non-white of the expression.
4389 * "arg" is advanced to the next non-white after the recognized expression.
4390 *
4391 * Return OK or FAIL.
4392 */
4393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004394eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395{
Bram Moolenaar33570922005-01-25 22:26:29 +00004396 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 char_u *p;
4398 int i;
4399 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004400 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 int len = 2;
4402 long n1, n2;
4403 char_u *s1, *s2;
4404 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4405 regmatch_T regmatch;
4406 int ic;
4407 char_u *save_cpo;
4408
4409 /*
4410 * Get the first variable.
4411 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 return FAIL;
4414
4415 p = *arg;
4416 switch (p[0])
4417 {
4418 case '=': if (p[1] == '=')
4419 type = TYPE_EQUAL;
4420 else if (p[1] == '~')
4421 type = TYPE_MATCH;
4422 break;
4423 case '!': if (p[1] == '=')
4424 type = TYPE_NEQUAL;
4425 else if (p[1] == '~')
4426 type = TYPE_NOMATCH;
4427 break;
4428 case '>': if (p[1] != '=')
4429 {
4430 type = TYPE_GREATER;
4431 len = 1;
4432 }
4433 else
4434 type = TYPE_GEQUAL;
4435 break;
4436 case '<': if (p[1] != '=')
4437 {
4438 type = TYPE_SMALLER;
4439 len = 1;
4440 }
4441 else
4442 type = TYPE_SEQUAL;
4443 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 case 'i': if (p[1] == 's')
4445 {
4446 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4447 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004448 i = p[len];
4449 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004450 {
4451 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4452 type_is = TRUE;
4453 }
4454 }
4455 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457
4458 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004459 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 */
4461 if (type != TYPE_UNKNOWN)
4462 {
4463 /* extra question mark appended: ignore case */
4464 if (p[len] == '?')
4465 {
4466 ic = TRUE;
4467 ++len;
4468 }
4469 /* extra '#' appended: match case */
4470 else if (p[len] == '#')
4471 {
4472 ic = FALSE;
4473 ++len;
4474 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004475 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 else
4477 ic = p_ic;
4478
4479 /*
4480 * Get the second variable.
4481 */
4482 *arg = skipwhite(p + len);
4483 if (eval5(arg, &var2, evaluate) == FAIL)
4484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004485 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 return FAIL;
4487 }
4488
4489 if (evaluate)
4490 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004491 if (type_is && rettv->v_type != var2.v_type)
4492 {
4493 /* For "is" a different type always means FALSE, for "notis"
4494 * it means TRUE. */
4495 n1 = (type == TYPE_NEQUAL);
4496 }
4497 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4498 {
4499 if (type_is)
4500 {
4501 n1 = (rettv->v_type == var2.v_type
4502 && rettv->vval.v_list == var2.vval.v_list);
4503 if (type == TYPE_NEQUAL)
4504 n1 = !n1;
4505 }
4506 else if (rettv->v_type != var2.v_type
4507 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4508 {
4509 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004510 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004512 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 clear_tv(rettv);
4514 clear_tv(&var2);
4515 return FAIL;
4516 }
4517 else
4518 {
4519 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004520 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4521 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004522 if (type == TYPE_NEQUAL)
4523 n1 = !n1;
4524 }
4525 }
4526
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004527 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4528 {
4529 if (type_is)
4530 {
4531 n1 = (rettv->v_type == var2.v_type
4532 && rettv->vval.v_dict == var2.vval.v_dict);
4533 if (type == TYPE_NEQUAL)
4534 n1 = !n1;
4535 }
4536 else if (rettv->v_type != var2.v_type
4537 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4538 {
4539 if (rettv->v_type != var2.v_type)
4540 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4541 else
4542 EMSG(_("E736: Invalid operation for Dictionary"));
4543 clear_tv(rettv);
4544 clear_tv(&var2);
4545 return FAIL;
4546 }
4547 else
4548 {
4549 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004550 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4551 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004552 if (type == TYPE_NEQUAL)
4553 n1 = !n1;
4554 }
4555 }
4556
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004557 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4558 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004560 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004562 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004563 clear_tv(rettv);
4564 clear_tv(&var2);
4565 return FAIL;
4566 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004567 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004568 if (type == TYPE_NEQUAL)
4569 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004570 }
4571
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004572#ifdef FEAT_FLOAT
4573 /*
4574 * If one of the two variables is a float, compare as a float.
4575 * When using "=~" or "!~", always compare as string.
4576 */
4577 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4578 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4579 {
4580 float_T f1, f2;
4581
4582 if (rettv->v_type == VAR_FLOAT)
4583 f1 = rettv->vval.v_float;
4584 else
4585 f1 = get_tv_number(rettv);
4586 if (var2.v_type == VAR_FLOAT)
4587 f2 = var2.vval.v_float;
4588 else
4589 f2 = get_tv_number(&var2);
4590 n1 = FALSE;
4591 switch (type)
4592 {
4593 case TYPE_EQUAL: n1 = (f1 == f2); break;
4594 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4595 case TYPE_GREATER: n1 = (f1 > f2); break;
4596 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4597 case TYPE_SMALLER: n1 = (f1 < f2); break;
4598 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4599 case TYPE_UNKNOWN:
4600 case TYPE_MATCH:
4601 case TYPE_NOMATCH: break; /* avoid gcc warning */
4602 }
4603 }
4604#endif
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 /*
4607 * If one of the two variables is a number, compare as a number.
4608 * When using "=~" or "!~", always compare as string.
4609 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004610 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613 n1 = get_tv_number(rettv);
4614 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 switch (type)
4616 {
4617 case TYPE_EQUAL: n1 = (n1 == n2); break;
4618 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4619 case TYPE_GREATER: n1 = (n1 > n2); break;
4620 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4621 case TYPE_SMALLER: n1 = (n1 < n2); break;
4622 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4623 case TYPE_UNKNOWN:
4624 case TYPE_MATCH:
4625 case TYPE_NOMATCH: break; /* avoid gcc warning */
4626 }
4627 }
4628 else
4629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630 s1 = get_tv_string_buf(rettv, buf1);
4631 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4633 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4634 else
4635 i = 0;
4636 n1 = FALSE;
4637 switch (type)
4638 {
4639 case TYPE_EQUAL: n1 = (i == 0); break;
4640 case TYPE_NEQUAL: n1 = (i != 0); break;
4641 case TYPE_GREATER: n1 = (i > 0); break;
4642 case TYPE_GEQUAL: n1 = (i >= 0); break;
4643 case TYPE_SMALLER: n1 = (i < 0); break;
4644 case TYPE_SEQUAL: n1 = (i <= 0); break;
4645
4646 case TYPE_MATCH:
4647 case TYPE_NOMATCH:
4648 /* avoid 'l' flag in 'cpoptions' */
4649 save_cpo = p_cpo;
4650 p_cpo = (char_u *)"";
4651 regmatch.regprog = vim_regcomp(s2,
4652 RE_MAGIC + RE_STRING);
4653 regmatch.rm_ic = ic;
4654 if (regmatch.regprog != NULL)
4655 {
4656 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004657 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (type == TYPE_NOMATCH)
4659 n1 = !n1;
4660 }
4661 p_cpo = save_cpo;
4662 break;
4663
4664 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4665 }
4666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 clear_tv(rettv);
4668 clear_tv(&var2);
4669 rettv->v_type = VAR_NUMBER;
4670 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672 }
4673
4674 return OK;
4675}
4676
4677/*
4678 * Handle fourth level expression:
4679 * + number addition
4680 * - number subtraction
4681 * . string concatenation
4682 *
4683 * "arg" must point to the first non-white of the expression.
4684 * "arg" is advanced to the next non-white after the recognized expression.
4685 *
4686 * Return OK or FAIL.
4687 */
4688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004689eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
Bram Moolenaar33570922005-01-25 22:26:29 +00004691 typval_T var2;
4692 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 int op;
4694 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004695#ifdef FEAT_FLOAT
4696 float_T f1 = 0, f2 = 0;
4697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 char_u *s1, *s2;
4699 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4700 char_u *p;
4701
4702 /*
4703 * Get the first variable.
4704 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004705 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 return FAIL;
4707
4708 /*
4709 * Repeat computing, until no '+', '-' or '.' is following.
4710 */
4711 for (;;)
4712 {
4713 op = **arg;
4714 if (op != '+' && op != '-' && op != '.')
4715 break;
4716
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717 if ((op != '+' || rettv->v_type != VAR_LIST)
4718#ifdef FEAT_FLOAT
4719 && (op == '.' || rettv->v_type != VAR_FLOAT)
4720#endif
4721 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 {
4723 /* For "list + ...", an illegal use of the first operand as
4724 * a number cannot be determined before evaluating the 2nd
4725 * operand: if this is also a list, all is ok.
4726 * For "something . ...", "something - ..." or "non-list + ...",
4727 * we know that the first operand needs to be a string or number
4728 * without evaluating the 2nd operand. So check before to avoid
4729 * side effects after an error. */
4730 if (evaluate && get_tv_string_chk(rettv) == NULL)
4731 {
4732 clear_tv(rettv);
4733 return FAIL;
4734 }
4735 }
4736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /*
4738 * Get the second variable.
4739 */
4740 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004741 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 return FAIL;
4745 }
4746
4747 if (evaluate)
4748 {
4749 /*
4750 * Compute the result.
4751 */
4752 if (op == '.')
4753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4755 s2 = get_tv_string_buf_chk(&var2, buf2);
4756 if (s2 == NULL) /* type error ? */
4757 {
4758 clear_tv(rettv);
4759 clear_tv(&var2);
4760 return FAIL;
4761 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004762 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
4764 rettv->v_type = VAR_STRING;
4765 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004767 else if (op == '+' && rettv->v_type == VAR_LIST
4768 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004769 {
4770 /* concatenate Lists */
4771 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4772 &var3) == FAIL)
4773 {
4774 clear_tv(rettv);
4775 clear_tv(&var2);
4776 return FAIL;
4777 }
4778 clear_tv(rettv);
4779 *rettv = var3;
4780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 else
4782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004783 int error = FALSE;
4784
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785#ifdef FEAT_FLOAT
4786 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 f1 = rettv->vval.v_float;
4789 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004790 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791 else
4792#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794 n1 = get_tv_number_chk(rettv, &error);
4795 if (error)
4796 {
4797 /* This can only happen for "list + non-list". For
4798 * "non-list + ..." or "something - ...", we returned
4799 * before evaluating the 2nd operand. */
4800 clear_tv(rettv);
4801 return FAIL;
4802 }
4803#ifdef FEAT_FLOAT
4804 if (var2.v_type == VAR_FLOAT)
4805 f1 = n1;
4806#endif
4807 }
4808#ifdef FEAT_FLOAT
4809 if (var2.v_type == VAR_FLOAT)
4810 {
4811 f2 = var2.vval.v_float;
4812 n2 = 0;
4813 }
4814 else
4815#endif
4816 {
4817 n2 = get_tv_number_chk(&var2, &error);
4818 if (error)
4819 {
4820 clear_tv(rettv);
4821 clear_tv(&var2);
4822 return FAIL;
4823 }
4824#ifdef FEAT_FLOAT
4825 if (rettv->v_type == VAR_FLOAT)
4826 f2 = n2;
4827#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830
4831#ifdef FEAT_FLOAT
4832 /* If there is a float on either side the result is a float. */
4833 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4834 {
4835 if (op == '+')
4836 f1 = f1 + f2;
4837 else
4838 f1 = f1 - f2;
4839 rettv->v_type = VAR_FLOAT;
4840 rettv->vval.v_float = f1;
4841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004843#endif
4844 {
4845 if (op == '+')
4846 n1 = n1 + n2;
4847 else
4848 n1 = n1 - n2;
4849 rettv->v_type = VAR_NUMBER;
4850 rettv->vval.v_number = n1;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 }
4856 return OK;
4857}
4858
4859/*
4860 * Handle fifth level expression:
4861 * * number multiplication
4862 * / number division
4863 * % number modulo
4864 *
4865 * "arg" must point to the first non-white of the expression.
4866 * "arg" is advanced to the next non-white after the recognized expression.
4867 *
4868 * Return OK or FAIL.
4869 */
4870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004871eval6(
4872 char_u **arg,
4873 typval_T *rettv,
4874 int evaluate,
4875 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876{
Bram Moolenaar33570922005-01-25 22:26:29 +00004877 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 int op;
4879 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880#ifdef FEAT_FLOAT
4881 int use_float = FALSE;
4882 float_T f1 = 0, f2;
4883#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004884 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 /*
4887 * Get the first variable.
4888 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004889 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 return FAIL;
4891
4892 /*
4893 * Repeat computing, until no '*', '/' or '%' is following.
4894 */
4895 for (;;)
4896 {
4897 op = **arg;
4898 if (op != '*' && op != '/' && op != '%')
4899 break;
4900
4901 if (evaluate)
4902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903#ifdef FEAT_FLOAT
4904 if (rettv->v_type == VAR_FLOAT)
4905 {
4906 f1 = rettv->vval.v_float;
4907 use_float = TRUE;
4908 n1 = 0;
4909 }
4910 else
4911#endif
4912 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004914 if (error)
4915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else
4918 n1 = 0;
4919
4920 /*
4921 * Get the second variable.
4922 */
4923 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004924 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return FAIL;
4926
4927 if (evaluate)
4928 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#ifdef FEAT_FLOAT
4930 if (var2.v_type == VAR_FLOAT)
4931 {
4932 if (!use_float)
4933 {
4934 f1 = n1;
4935 use_float = TRUE;
4936 }
4937 f2 = var2.vval.v_float;
4938 n2 = 0;
4939 }
4940 else
4941#endif
4942 {
4943 n2 = get_tv_number_chk(&var2, &error);
4944 clear_tv(&var2);
4945 if (error)
4946 return FAIL;
4947#ifdef FEAT_FLOAT
4948 if (use_float)
4949 f2 = n2;
4950#endif
4951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
4953 /*
4954 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957#ifdef FEAT_FLOAT
4958 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004960 if (op == '*')
4961 f1 = f1 * f2;
4962 else if (op == '/')
4963 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964# ifdef VMS
4965 /* VMS crashes on divide by zero, work around it */
4966 if (f2 == 0.0)
4967 {
4968 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004973 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974 }
4975 else
4976 f1 = f1 / f2;
4977# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 /* We rely on the floating point library to handle divide
4979 * by zero to result in "inf" and not a crash. */
4980 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004985 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 return FAIL;
4987 }
4988 rettv->v_type = VAR_FLOAT;
4989 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 if (op == '*')
4995 n1 = n1 * n2;
4996 else if (op == '/')
4997 {
4998 if (n2 == 0) /* give an error message? */
4999 {
5000 if (n1 == 0)
5001 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5002 else if (n1 < 0)
5003 n1 = -0x7fffffffL;
5004 else
5005 n1 = 0x7fffffffL;
5006 }
5007 else
5008 n1 = n1 / n2;
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
5012 if (n2 == 0) /* give an error message? */
5013 n1 = 0;
5014 else
5015 n1 = n1 % n2;
5016 }
5017 rettv->v_type = VAR_NUMBER;
5018 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 }
5022
5023 return OK;
5024}
5025
5026/*
5027 * Handle sixth level expression:
5028 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005029 * "string" string constant
5030 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 * &option-name option value
5032 * @r register contents
5033 * identifier variable value
5034 * function() function call
5035 * $VAR environment variable
5036 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005037 * [expr, expr] List
5038 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 *
5040 * Also handle:
5041 * ! in front logical NOT
5042 * - in front unary minus
5043 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 * trailing [] subscript in String or List
5045 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *
5047 * "arg" must point to the first non-white of the expression.
5048 * "arg" is advanced to the next non-white after the recognized expression.
5049 *
5050 * Return OK or FAIL.
5051 */
5052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005053eval7(
5054 char_u **arg,
5055 typval_T *rettv,
5056 int evaluate,
5057 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 long n;
5060 int len;
5061 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 char_u *start_leader, *end_leader;
5063 int ret = OK;
5064 char_u *alias;
5065
5066 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005068 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072 /*
5073 * Skip '!' and '-' characters. They are handled later.
5074 */
5075 start_leader = *arg;
5076 while (**arg == '!' || **arg == '-' || **arg == '+')
5077 *arg = skipwhite(*arg + 1);
5078 end_leader = *arg;
5079
5080 switch (**arg)
5081 {
5082 /*
5083 * Number constant.
5084 */
5085 case '0':
5086 case '1':
5087 case '2':
5088 case '3':
5089 case '4':
5090 case '5':
5091 case '6':
5092 case '7':
5093 case '8':
5094 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095 {
5096#ifdef FEAT_FLOAT
5097 char_u *p = skipdigits(*arg + 1);
5098 int get_float = FALSE;
5099
5100 /* We accept a float when the format matches
5101 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005102 * strict to avoid backwards compatibility problems.
5103 * Don't look for a float after the "." operator, so that
5104 * ":let vers = 1.2.3" doesn't fail. */
5105 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005107 get_float = TRUE;
5108 p = skipdigits(p + 2);
5109 if (*p == 'e' || *p == 'E')
5110 {
5111 ++p;
5112 if (*p == '-' || *p == '+')
5113 ++p;
5114 if (!vim_isdigit(*p))
5115 get_float = FALSE;
5116 else
5117 p = skipdigits(p + 1);
5118 }
5119 if (ASCII_ISALPHA(*p) || *p == '.')
5120 get_float = FALSE;
5121 }
5122 if (get_float)
5123 {
5124 float_T f;
5125
5126 *arg += string2float(*arg, &f);
5127 if (evaluate)
5128 {
5129 rettv->v_type = VAR_FLOAT;
5130 rettv->vval.v_float = f;
5131 }
5132 }
5133 else
5134#endif
5135 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005136 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137 *arg += len;
5138 if (evaluate)
5139 {
5140 rettv->v_type = VAR_NUMBER;
5141 rettv->vval.v_number = n;
5142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
5147 /*
5148 * String constant: "string".
5149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
5152
5153 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 break;
5158
5159 /*
5160 * List: [expr, expr]
5161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164
5165 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 * Dictionary: {key: val, key: val}
5167 */
5168 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5169 break;
5170
5171 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 /*
5178 * Environment variable: $VAR.
5179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /*
5184 * Register contents: @r.
5185 */
5186 case '@': ++*arg;
5187 if (evaluate)
5188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005190 rettv->vval.v_string = get_reg_contents(**arg,
5191 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 if (**arg != NUL)
5194 ++*arg;
5195 break;
5196
5197 /*
5198 * nested expression: (expression).
5199 */
5200 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (**arg == ')')
5203 ++*arg;
5204 else if (ret == OK)
5205 {
5206 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ret = FAIL;
5209 }
5210 break;
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215
5216 if (ret == NOTDONE)
5217 {
5218 /*
5219 * Must be a variable or function name.
5220 * Can also be a curly-braces kind of name: {expr}.
5221 */
5222 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005223 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 if (alias != NULL)
5225 s = alias;
5226
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005227 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 ret = FAIL;
5229 else
5230 {
5231 if (**arg == '(') /* recursive! */
5232 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005233 partial_T *partial;
5234
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 /* If "s" is the name of a variable of type VAR_FUNC
5236 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005237 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238
5239 /* Invoke the function. */
5240 ret = get_func_tv(s, len, rettv, arg,
5241 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005242 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005243
5244 /* If evaluate is FALSE rettv->v_type was not set in
5245 * get_func_tv, but it's needed in handle_subscript() to parse
5246 * what follows. So set it here. */
5247 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5248 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005249 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005250 rettv->v_type = VAR_FUNC;
5251 }
5252
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 /* Stop the expression evaluation when immediately
5254 * aborting on error, or when an interrupt occurred or
5255 * an exception was thrown but not caught. */
5256 if (aborting())
5257 {
5258 if (ret == OK)
5259 clear_tv(rettv);
5260 ret = FAIL;
5261 }
5262 }
5263 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005264 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005265 else
5266 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005268 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 }
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 *arg = skipwhite(*arg);
5272
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005273 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5274 * expr(expr). */
5275 if (ret == OK)
5276 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /*
5279 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5280 */
5281 if (ret == OK && evaluate && end_leader > start_leader)
5282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 int val = 0;
5285#ifdef FEAT_FLOAT
5286 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005288 if (rettv->v_type == VAR_FLOAT)
5289 f = rettv->vval.v_float;
5290 else
5291#endif
5292 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 clear_tv(rettv);
5296 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 else
5299 {
5300 while (end_leader > start_leader)
5301 {
5302 --end_leader;
5303 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005304 {
5305#ifdef FEAT_FLOAT
5306 if (rettv->v_type == VAR_FLOAT)
5307 f = !f;
5308 else
5309#endif
5310 val = !val;
5311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 {
5314#ifdef FEAT_FLOAT
5315 if (rettv->v_type == VAR_FLOAT)
5316 f = -f;
5317 else
5318#endif
5319 val = -val;
5320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322#ifdef FEAT_FLOAT
5323 if (rettv->v_type == VAR_FLOAT)
5324 {
5325 clear_tv(rettv);
5326 rettv->vval.v_float = f;
5327 }
5328 else
5329#endif
5330 {
5331 clear_tv(rettv);
5332 rettv->v_type = VAR_NUMBER;
5333 rettv->vval.v_number = val;
5334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337
5338 return ret;
5339}
5340
5341/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005342 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5343 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5345 */
5346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005347eval_index(
5348 char_u **arg,
5349 typval_T *rettv,
5350 int evaluate,
5351 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352{
5353 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 long len = -1;
5357 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005364 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005365 if (verbose)
5366 EMSG(_("E695: Cannot index a Funcref"));
5367 return FAIL;
5368 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005369#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005370 if (verbose)
5371 EMSG(_(e_float_as_string));
5372 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005373#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005374 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005375 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005376 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005377 if (verbose)
5378 EMSG(_("E909: Cannot index a special variable"));
5379 return FAIL;
5380 case VAR_UNKNOWN:
5381 if (evaluate)
5382 return FAIL;
5383 /* FALLTHROUGH */
5384
5385 case VAR_STRING:
5386 case VAR_NUMBER:
5387 case VAR_LIST:
5388 case VAR_DICT:
5389 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005390 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005392 init_tv(&var1);
5393 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 /*
5397 * dict.name
5398 */
5399 key = *arg + 1;
5400 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5401 ;
5402 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 }
5406 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005408 /*
5409 * something[idx]
5410 *
5411 * Get the (first) variable from inside the [].
5412 */
5413 *arg = skipwhite(*arg + 1);
5414 if (**arg == ':')
5415 empty1 = TRUE;
5416 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5417 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005418 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5419 {
5420 /* not a number or string */
5421 clear_tv(&var1);
5422 return FAIL;
5423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424
5425 /*
5426 * Get the second variable from inside the [:].
5427 */
5428 if (**arg == ':')
5429 {
5430 range = TRUE;
5431 *arg = skipwhite(*arg + 1);
5432 if (**arg == ']')
5433 empty2 = TRUE;
5434 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005436 if (!empty1)
5437 clear_tv(&var1);
5438 return FAIL;
5439 }
5440 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5441 {
5442 /* not a number or string */
5443 if (!empty1)
5444 clear_tv(&var1);
5445 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 return FAIL;
5447 }
5448 }
5449
5450 /* Check for the ']'. */
5451 if (**arg != ']')
5452 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005453 if (verbose)
5454 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 clear_tv(&var1);
5456 if (range)
5457 clear_tv(&var2);
5458 return FAIL;
5459 }
5460 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462
5463 if (evaluate)
5464 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465 n1 = 0;
5466 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 n1 = get_tv_number(&var1);
5469 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 }
5471 if (range)
5472 {
5473 if (empty2)
5474 n2 = -1;
5475 else
5476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 n2 = get_tv_number(&var2);
5478 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 }
5481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005482 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005484 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005485 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005486 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005487 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488 case VAR_SPECIAL:
5489 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005490 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005491 break; /* not evaluating, skipping over subscript */
5492
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 case VAR_NUMBER:
5494 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 len = (long)STRLEN(s);
5497 if (range)
5498 {
5499 /* The resulting variable is a substring. If the indexes
5500 * are out of range the result is empty. */
5501 if (n1 < 0)
5502 {
5503 n1 = len + n1;
5504 if (n1 < 0)
5505 n1 = 0;
5506 }
5507 if (n2 < 0)
5508 n2 = len + n2;
5509 else if (n2 >= len)
5510 n2 = len;
5511 if (n1 >= len || n2 < 0 || n1 > n2)
5512 s = NULL;
5513 else
5514 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5515 }
5516 else
5517 {
5518 /* The resulting variable is a string of a single
5519 * character. If the index is too big or negative the
5520 * result is empty. */
5521 if (n1 >= len || n1 < 0)
5522 s = NULL;
5523 else
5524 s = vim_strnsave(s + n1, 1);
5525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 clear_tv(rettv);
5527 rettv->v_type = VAR_STRING;
5528 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 break;
5530
5531 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 if (n1 < 0)
5534 n1 = len + n1;
5535 if (!empty1 && (n1 < 0 || n1 >= len))
5536 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005537 /* For a range we allow invalid values and return an empty
5538 * list. A list index out of range is an error. */
5539 if (!range)
5540 {
5541 if (verbose)
5542 EMSGN(_(e_listidx), n1);
5543 return FAIL;
5544 }
5545 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 }
5547 if (range)
5548 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005549 list_T *l;
5550 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551
5552 if (n2 < 0)
5553 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005554 else if (n2 >= len)
5555 n2 = len - 1;
5556 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005557 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 l = list_alloc();
5559 if (l == NULL)
5560 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 n1 <= n2; ++n1)
5563 {
5564 if (list_append_tv(l, &item->li_tv) == FAIL)
5565 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005566 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 return FAIL;
5568 }
5569 item = item->li_next;
5570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 clear_tv(rettv);
5572 rettv->v_type = VAR_LIST;
5573 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005574 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 }
5576 else
5577 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005578 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 clear_tv(rettv);
5580 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 }
5582 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583
5584 case VAR_DICT:
5585 if (range)
5586 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005587 if (verbose)
5588 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589 if (len == -1)
5590 clear_tv(&var1);
5591 return FAIL;
5592 }
5593 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005594 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595
5596 if (len == -1)
5597 {
5598 key = get_tv_string(&var1);
5599 if (*key == NUL)
5600 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005601 if (verbose)
5602 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 clear_tv(&var1);
5604 return FAIL;
5605 }
5606 }
5607
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005608 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005610 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005611 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612 if (len == -1)
5613 clear_tv(&var1);
5614 if (item == NULL)
5615 return FAIL;
5616
5617 copy_tv(&item->di_tv, &var1);
5618 clear_tv(rettv);
5619 *rettv = var1;
5620 }
5621 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 }
5623 }
5624
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625 return OK;
5626}
5627
5628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 * Get an option value.
5630 * "arg" points to the '&' or '+' before the option name.
5631 * "arg" is advanced to character after the option name.
5632 * Return OK or FAIL.
5633 */
5634 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005635get_option_tv(
5636 char_u **arg,
5637 typval_T *rettv, /* when NULL, only check if option exists */
5638 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 char_u *option_end;
5641 long numval;
5642 char_u *stringval;
5643 int opt_type;
5644 int c;
5645 int working = (**arg == '+'); /* has("+option") */
5646 int ret = OK;
5647 int opt_flags;
5648
5649 /*
5650 * Isolate the option name and find its value.
5651 */
5652 option_end = find_option_end(arg, &opt_flags);
5653 if (option_end == NULL)
5654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 EMSG2(_("E112: Option name missing: %s"), *arg);
5657 return FAIL;
5658 }
5659
5660 if (!evaluate)
5661 {
5662 *arg = option_end;
5663 return OK;
5664 }
5665
5666 c = *option_end;
5667 *option_end = NUL;
5668 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671 if (opt_type == -3) /* invalid name */
5672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 EMSG2(_("E113: Unknown option: %s"), *arg);
5675 ret = FAIL;
5676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {
5679 if (opt_type == -2) /* hidden string option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_STRING;
5682 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else if (opt_type == -1) /* hidden number option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_NUMBER;
5687 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 else if (opt_type == 1) /* number option */
5690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005691 rettv->v_type = VAR_NUMBER;
5692 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694 else /* string option */
5695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696 rettv->v_type = VAR_STRING;
5697 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699 }
5700 else if (working && (opt_type == -2 || opt_type == -1))
5701 ret = FAIL;
5702
5703 *option_end = c; /* put back for error messages */
5704 *arg = option_end;
5705
5706 return ret;
5707}
5708
5709/*
5710 * Allocate a variable for a string constant.
5711 * Return OK or FAIL.
5712 */
5713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005714get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715{
5716 char_u *p;
5717 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 int extra = 0;
5719
5720 /*
5721 * Find the end of the string, skipping backslashed characters.
5722 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005723 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 {
5725 if (*p == '\\' && p[1] != NUL)
5726 {
5727 ++p;
5728 /* A "\<x>" form occupies at least 4 characters, and produces up
5729 * to 6 characters: reserve space for 2 extra */
5730 if (*p == '<')
5731 extra += 2;
5732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734
5735 if (*p != '"')
5736 {
5737 EMSG2(_("E114: Missing quote: %s"), *arg);
5738 return FAIL;
5739 }
5740
5741 /* If only parsing, set *arg and return here */
5742 if (!evaluate)
5743 {
5744 *arg = p + 1;
5745 return OK;
5746 }
5747
5748 /*
5749 * Copy the string into allocated memory, handling backslashed
5750 * characters.
5751 */
5752 name = alloc((unsigned)(p - *arg + extra));
5753 if (name == NULL)
5754 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 rettv->v_type = VAR_STRING;
5756 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {
5760 if (*p == '\\')
5761 {
5762 switch (*++p)
5763 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 case 'b': *name++ = BS; ++p; break;
5765 case 'e': *name++ = ESC; ++p; break;
5766 case 'f': *name++ = FF; ++p; break;
5767 case 'n': *name++ = NL; ++p; break;
5768 case 'r': *name++ = CAR; ++p; break;
5769 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770
5771 case 'X': /* hex: "\x1", "\x12" */
5772 case 'x':
5773 case 'u': /* Unicode: "\u0023" */
5774 case 'U':
5775 if (vim_isxdigit(p[1]))
5776 {
5777 int n, nr;
5778 int c = toupper(*p);
5779
5780 if (c == 'X')
5781 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005782 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005784 else
5785 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 nr = 0;
5787 while (--n >= 0 && vim_isxdigit(p[1]))
5788 {
5789 ++p;
5790 nr = (nr << 4) + hex2nr(*p);
5791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793#ifdef FEAT_MBYTE
5794 /* For "\u" store the number according to
5795 * 'encoding'. */
5796 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 else
5799#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 break;
5803
5804 /* octal: "\1", "\12", "\123" */
5805 case '0':
5806 case '1':
5807 case '2':
5808 case '3':
5809 case '4':
5810 case '5':
5811 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 case '7': *name = *p++ - '0';
5813 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 *name = (*name << 3) + *p++ - '0';
5816 if (*p >= '0' && *p <= '7')
5817 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 break;
5821
5822 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 if (extra != 0)
5825 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 break;
5828 }
5829 /* FALLTHROUGH */
5830
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 break;
5833 }
5834 }
5835 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 *arg = p + 1;
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 return OK;
5843}
5844
5845/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 * Return OK or FAIL.
5848 */
5849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005850get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 int reduce = 0;
5855
5856 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 */
5859 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 break;
5865 ++reduce;
5866 ++p;
5867 }
5868 }
5869
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 return FAIL;
5874 }
5875
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 if (!evaluate)
5878 {
5879 *arg = p + 1;
5880 return OK;
5881 }
5882
5883 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 */
5886 str = alloc((unsigned)((p - *arg) - reduce));
5887 if (str == NULL)
5888 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 rettv->v_type = VAR_STRING;
5890 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 break;
5898 ++p;
5899 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005902 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 *arg = p + 1;
5904
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 return OK;
5906}
5907
5908/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 * Allocate a variable for a List and fill it from "*arg".
5910 * Return OK or FAIL.
5911 */
5912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005913get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914{
Bram Moolenaar33570922005-01-25 22:26:29 +00005915 list_T *l = NULL;
5916 typval_T tv;
5917 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918
5919 if (evaluate)
5920 {
5921 l = list_alloc();
5922 if (l == NULL)
5923 return FAIL;
5924 }
5925
5926 *arg = skipwhite(*arg + 1);
5927 while (**arg != ']' && **arg != NUL)
5928 {
5929 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5930 goto failret;
5931 if (evaluate)
5932 {
5933 item = listitem_alloc();
5934 if (item != NULL)
5935 {
5936 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005937 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 list_append(l, item);
5939 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005940 else
5941 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 }
5943
5944 if (**arg == ']')
5945 break;
5946 if (**arg != ',')
5947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 goto failret;
5950 }
5951 *arg = skipwhite(*arg + 1);
5952 }
5953
5954 if (**arg != ']')
5955 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005956 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957failret:
5958 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005959 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 return FAIL;
5961 }
5962
5963 *arg = skipwhite(*arg + 1);
5964 if (evaluate)
5965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005966 rettv->v_type = VAR_LIST;
5967 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 ++l->lv_refcount;
5969 }
5970
5971 return OK;
5972}
5973
5974/*
5975 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005976 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005978 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005979list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005981 list_T *l;
5982
5983 l = (list_T *)alloc_clear(sizeof(list_T));
5984 if (l != NULL)
5985 {
5986 /* Prepend the list to the list of lists for garbage collection. */
5987 if (first_list != NULL)
5988 first_list->lv_used_prev = l;
5989 l->lv_used_prev = NULL;
5990 l->lv_used_next = first_list;
5991 first_list = l;
5992 }
5993 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994}
5995
5996/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005997 * Allocate an empty list for a return value.
5998 * Returns OK or FAIL.
5999 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006001rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006002{
6003 list_T *l = list_alloc();
6004
6005 if (l == NULL)
6006 return FAIL;
6007
6008 rettv->vval.v_list = l;
6009 rettv->v_type = VAR_LIST;
6010 ++l->lv_refcount;
6011 return OK;
6012}
6013
6014/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 * Unreference a list: decrement the reference count and free it when it
6016 * becomes zero.
6017 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006019list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006021 if (l != NULL && --l->lv_refcount <= 0)
6022 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023}
6024
6025/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006026 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027 * Ignores the reference count.
6028 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006029 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006030list_free(
6031 list_T *l,
6032 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033{
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006036 /* Remove the list from the list of lists for garbage collection. */
6037 if (l->lv_used_prev == NULL)
6038 first_list = l->lv_used_next;
6039 else
6040 l->lv_used_prev->lv_used_next = l->lv_used_next;
6041 if (l->lv_used_next != NULL)
6042 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6043
Bram Moolenaard9fba312005-06-26 22:34:35 +00006044 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006046 /* Remove the item before deleting it. */
6047 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006048 if (recurse || (item->li_tv.v_type != VAR_LIST
6049 && item->li_tv.v_type != VAR_DICT))
6050 clear_tv(&item->li_tv);
6051 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 }
6053 vim_free(l);
6054}
6055
6056/*
6057 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006058 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006060 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006061listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062{
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064}
6065
6066/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006067 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006069 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006070listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006072 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 vim_free(item);
6074}
6075
6076/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006077 * Remove a list item from a List and free it. Also clears the value.
6078 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006081{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006082 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006083 listitem_free(item);
6084}
6085
6086/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 * Get the number of items in a list.
6088 */
6089 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006090list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092 if (l == NULL)
6093 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006094 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095}
6096
6097/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098 * Return TRUE when two lists have exactly the same values.
6099 */
6100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006101list_equal(
6102 list_T *l1,
6103 list_T *l2,
6104 int ic, /* ignore case for strings */
6105 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106{
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006109 if (l1 == NULL || l2 == NULL)
6110 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006111 if (l1 == l2)
6112 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006113 if (list_len(l1) != list_len(l2))
6114 return FALSE;
6115
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006116 for (item1 = l1->lv_first, item2 = l2->lv_first;
6117 item1 != NULL && item2 != NULL;
6118 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006119 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006120 return FALSE;
6121 return item1 == NULL && item2 == NULL;
6122}
6123
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006124/*
6125 * Return the dictitem that an entry in a hashtable points to.
6126 */
6127 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006128dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006129{
6130 return HI2DI(hi);
6131}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006132
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134 * Return TRUE when two dictionaries have exactly the same key/values.
6135 */
6136 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006137dict_equal(
6138 dict_T *d1,
6139 dict_T *d2,
6140 int ic, /* ignore case for strings */
6141 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006142{
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 hashitem_T *hi;
6144 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006145 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006147 if (d1 == NULL || d2 == NULL)
6148 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006149 if (d1 == d2)
6150 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151 if (dict_len(d1) != dict_len(d2))
6152 return FALSE;
6153
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006154 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157 if (!HASHITEM_EMPTY(hi))
6158 {
6159 item2 = dict_find(d2, hi->hi_key, -1);
6160 if (item2 == NULL)
6161 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006162 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006163 return FALSE;
6164 --todo;
6165 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166 }
6167 return TRUE;
6168}
6169
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006170static int tv_equal_recurse_limit;
6171
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006172/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006174 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006175 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176 */
6177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178tv_equal(
6179 typval_T *tv1,
6180 typval_T *tv2,
6181 int ic, /* ignore case */
6182 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183{
6184 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006185 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006186 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006187 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006188
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006189 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6190 if ((tv1->v_type == VAR_FUNC
6191 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6192 && (tv2->v_type == VAR_FUNC
6193 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6194 {
6195 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6196 : tv1->vval.v_partial->pt_name;
6197 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6198 : tv2->vval.v_partial->pt_name;
6199 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6200 }
6201
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006202 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006204
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006205 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 * recursiveness to a limit. We guess they are equal then.
6207 * A fixed limit has the problem of still taking an awful long time.
6208 * Reduce the limit every time running into it. That should work fine for
6209 * deeply linked structures that are not recursively linked and catch
6210 * recursiveness quickly. */
6211 if (!recursive)
6212 tv_equal_recurse_limit = 1000;
6213 if (recursive_cnt >= tv_equal_recurse_limit)
6214 {
6215 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006216 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006217 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006218
6219 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006220 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006221 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006222 ++recursive_cnt;
6223 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6224 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006225 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006226
6227 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006228 ++recursive_cnt;
6229 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6230 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006231 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006232
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006233 case VAR_NUMBER:
6234 return tv1->vval.v_number == tv2->vval.v_number;
6235
6236 case VAR_STRING:
6237 s1 = get_tv_string_buf(tv1, buf1);
6238 s2 = get_tv_string_buf(tv2, buf2);
6239 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006240
6241 case VAR_SPECIAL:
6242 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006243
6244 case VAR_FLOAT:
6245#ifdef FEAT_FLOAT
6246 return tv1->vval.v_float == tv2->vval.v_float;
6247#endif
6248 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006249#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006250 return tv1->vval.v_job == tv2->vval.v_job;
6251#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006252 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006253#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006254 return tv1->vval.v_channel == tv2->vval.v_channel;
6255#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006256 case VAR_FUNC:
6257 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006258 case VAR_UNKNOWN:
6259 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006260 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006261
Bram Moolenaara03f2332016-02-06 18:09:59 +01006262 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6263 * does not equal anything, not even itself. */
6264 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265}
6266
6267/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268 * Locate item with index "n" in list "l" and return it.
6269 * A negative index is counted from the end; -1 is the last item.
6270 * Returns NULL when "n" is out of range.
6271 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006272 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006273list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274{
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 long idx;
6277
6278 if (l == NULL)
6279 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006280
6281 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006282 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006283 n = l->lv_len + n;
6284
6285 /* Check for index out of range. */
6286 if (n < 0 || n >= l->lv_len)
6287 return NULL;
6288
6289 /* When there is a cached index may start search from there. */
6290 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006292 if (n < l->lv_idx / 2)
6293 {
6294 /* closest to the start of the list */
6295 item = l->lv_first;
6296 idx = 0;
6297 }
6298 else if (n > (l->lv_idx + l->lv_len) / 2)
6299 {
6300 /* closest to the end of the list */
6301 item = l->lv_last;
6302 idx = l->lv_len - 1;
6303 }
6304 else
6305 {
6306 /* closest to the cached index */
6307 item = l->lv_idx_item;
6308 idx = l->lv_idx;
6309 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 }
6311 else
6312 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313 if (n < l->lv_len / 2)
6314 {
6315 /* closest to the start of the list */
6316 item = l->lv_first;
6317 idx = 0;
6318 }
6319 else
6320 {
6321 /* closest to the end of the list */
6322 item = l->lv_last;
6323 idx = l->lv_len - 1;
6324 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006326
6327 while (n > idx)
6328 {
6329 /* search forward */
6330 item = item->li_next;
6331 ++idx;
6332 }
6333 while (n < idx)
6334 {
6335 /* search backward */
6336 item = item->li_prev;
6337 --idx;
6338 }
6339
6340 /* cache the used index */
6341 l->lv_idx = idx;
6342 l->lv_idx_item = item;
6343
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344 return item;
6345}
6346
6347/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006348 * Get list item "l[idx]" as a number.
6349 */
6350 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006351list_find_nr(
6352 list_T *l,
6353 long idx,
6354 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006355{
6356 listitem_T *li;
6357
6358 li = list_find(l, idx);
6359 if (li == NULL)
6360 {
6361 if (errorp != NULL)
6362 *errorp = TRUE;
6363 return -1L;
6364 }
6365 return get_tv_number_chk(&li->li_tv, errorp);
6366}
6367
6368/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006369 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6370 */
6371 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006372list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006373{
6374 listitem_T *li;
6375
6376 li = list_find(l, idx - 1);
6377 if (li == NULL)
6378 {
6379 EMSGN(_(e_listidx), idx);
6380 return NULL;
6381 }
6382 return get_tv_string(&li->li_tv);
6383}
6384
6385/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006386 * Locate "item" list "l" and return its index.
6387 * Returns -1 when "item" is not in the list.
6388 */
6389 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006390list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006391{
6392 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006394
6395 if (l == NULL)
6396 return -1;
6397 idx = 0;
6398 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6399 ++idx;
6400 if (li == NULL)
6401 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006402 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006403}
6404
6405/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406 * Append item "item" to the end of list "l".
6407 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410{
6411 if (l->lv_last == NULL)
6412 {
6413 /* empty list */
6414 l->lv_first = item;
6415 l->lv_last = item;
6416 item->li_prev = NULL;
6417 }
6418 else
6419 {
6420 l->lv_last->li_next = item;
6421 item->li_prev = l->lv_last;
6422 l->lv_last = item;
6423 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006424 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 item->li_next = NULL;
6426}
6427
6428/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006432 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006433list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006434{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006435 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006439 copy_tv(tv, &li->li_tv);
6440 list_append(l, li);
6441 return OK;
6442}
6443
6444/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006445 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006446 * Return FAIL when out of memory.
6447 */
6448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006450{
6451 listitem_T *li = listitem_alloc();
6452
6453 if (li == NULL)
6454 return FAIL;
6455 li->li_tv.v_type = VAR_DICT;
6456 li->li_tv.v_lock = 0;
6457 li->li_tv.vval.v_dict = dict;
6458 list_append(list, li);
6459 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460 return OK;
6461}
6462
6463/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006464 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006465 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006466 * Returns FAIL when out of memory.
6467 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006469list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470{
6471 listitem_T *li = listitem_alloc();
6472
6473 if (li == NULL)
6474 return FAIL;
6475 list_append(l, li);
6476 li->li_tv.v_type = VAR_STRING;
6477 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006478 if (str == NULL)
6479 li->li_tv.vval.v_string = NULL;
6480 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006481 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006482 return FAIL;
6483 return OK;
6484}
6485
6486/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006487 * Append "n" to list "l".
6488 * Returns FAIL when out of memory.
6489 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006491list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006492{
6493 listitem_T *li;
6494
6495 li = listitem_alloc();
6496 if (li == NULL)
6497 return FAIL;
6498 li->li_tv.v_type = VAR_NUMBER;
6499 li->li_tv.v_lock = 0;
6500 li->li_tv.vval.v_number = n;
6501 list_append(l, li);
6502 return OK;
6503}
6504
6505/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 * If "item" is NULL append at the end.
6508 * Return FAIL when out of memory.
6509 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512{
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514
6515 if (ni == NULL)
6516 return FAIL;
6517 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006518 list_insert(l, ni, item);
6519 return OK;
6520}
6521
6522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006523list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006524{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 if (item == NULL)
6526 /* Append new item at end of list. */
6527 list_append(l, ni);
6528 else
6529 {
6530 /* Insert new item before existing item. */
6531 ni->li_prev = item->li_prev;
6532 ni->li_next = item;
6533 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006534 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006536 ++l->lv_idx;
6537 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006539 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006541 l->lv_idx_item = NULL;
6542 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006544 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546}
6547
6548/*
6549 * Extend "l1" with "l2".
6550 * If "bef" is NULL append at the end, otherwise insert before this item.
6551 * Returns FAIL when out of memory.
6552 */
6553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006554list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006557 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006559 /* We also quit the loop when we have inserted the original item count of
6560 * the list, avoid a hang when we extend a list with itself. */
6561 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6563 return FAIL;
6564 return OK;
6565}
6566
6567/*
6568 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6569 * Return FAIL when out of memory.
6570 */
6571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006572list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573{
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006575
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006576 if (l1 == NULL || l2 == NULL)
6577 return FAIL;
6578
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581 if (l == NULL)
6582 return FAIL;
6583 tv->v_type = VAR_LIST;
6584 tv->vval.v_list = l;
6585
6586 /* append all items from the second list */
6587 return list_extend(l, l2, NULL);
6588}
6589
6590/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006591 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006593 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 * Returns NULL when out of memory.
6595 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006596 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598{
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 list_T *copy;
6600 listitem_T *item;
6601 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602
6603 if (orig == NULL)
6604 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605
6606 copy = list_alloc();
6607 if (copy != NULL)
6608 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006609 if (copyID != 0)
6610 {
6611 /* Do this before adding the items, because one of the items may
6612 * refer back to this list. */
6613 orig->lv_copyID = copyID;
6614 orig->lv_copylist = copy;
6615 }
6616 for (item = orig->lv_first; item != NULL && !got_int;
6617 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 {
6619 ni = listitem_alloc();
6620 if (ni == NULL)
6621 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006622 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006623 {
6624 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6625 {
6626 vim_free(ni);
6627 break;
6628 }
6629 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006631 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 list_append(copy, ni);
6633 }
6634 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006635 if (item != NULL)
6636 {
6637 list_unref(copy);
6638 copy = NULL;
6639 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 }
6641
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642 return copy;
6643}
6644
6645/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006647 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006648 * This used to be called list_remove, but that conflicts with a Sun header
6649 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006651 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006652vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006653{
Bram Moolenaar33570922005-01-25 22:26:29 +00006654 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006655
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006656 /* notify watchers */
6657 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006659 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006660 list_fix_watch(l, ip);
6661 if (ip == item2)
6662 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664
6665 if (item2->li_next == NULL)
6666 l->lv_last = item->li_prev;
6667 else
6668 item2->li_next->li_prev = item->li_prev;
6669 if (item->li_prev == NULL)
6670 l->lv_first = item2->li_next;
6671 else
6672 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006673 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674}
6675
6676/*
6677 * Return an allocated string with the string representation of a list.
6678 * May return NULL.
6679 */
6680 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006681list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682{
6683 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684
6685 if (tv->vval.v_list == NULL)
6686 return NULL;
6687 ga_init2(&ga, (int)sizeof(char), 80);
6688 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006689 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006690 {
6691 vim_free(ga.ga_data);
6692 return NULL;
6693 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694 ga_append(&ga, ']');
6695 ga_append(&ga, NUL);
6696 return (char_u *)ga.ga_data;
6697}
6698
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699typedef struct join_S {
6700 char_u *s;
6701 char_u *tofree;
6702} join_T;
6703
6704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006705list_join_inner(
6706 garray_T *gap, /* to store the result in */
6707 list_T *l,
6708 char_u *sep,
6709 int echo_style,
6710 int copyID,
6711 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006712{
6713 int i;
6714 join_T *p;
6715 int len;
6716 int sumlen = 0;
6717 int first = TRUE;
6718 char_u *tofree;
6719 char_u numbuf[NUMBUFLEN];
6720 listitem_T *item;
6721 char_u *s;
6722
6723 /* Stringify each item in the list. */
6724 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6725 {
6726 if (echo_style)
6727 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6728 else
6729 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6730 if (s == NULL)
6731 return FAIL;
6732
6733 len = (int)STRLEN(s);
6734 sumlen += len;
6735
Bram Moolenaarcde88542015-08-11 19:14:00 +02006736 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006737 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6738 if (tofree != NULL || s != numbuf)
6739 {
6740 p->s = s;
6741 p->tofree = tofree;
6742 }
6743 else
6744 {
6745 p->s = vim_strnsave(s, len);
6746 p->tofree = p->s;
6747 }
6748
6749 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006750 if (did_echo_string_emsg) /* recursion error, bail out */
6751 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006752 }
6753
6754 /* Allocate result buffer with its total size, avoid re-allocation and
6755 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6756 if (join_gap->ga_len >= 2)
6757 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6758 if (ga_grow(gap, sumlen + 2) == FAIL)
6759 return FAIL;
6760
6761 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6762 {
6763 if (first)
6764 first = FALSE;
6765 else
6766 ga_concat(gap, sep);
6767 p = ((join_T *)join_gap->ga_data) + i;
6768
6769 if (p->s != NULL)
6770 ga_concat(gap, p->s);
6771 line_breakcheck();
6772 }
6773
6774 return OK;
6775}
6776
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006779 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006780 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006783list_join(
6784 garray_T *gap,
6785 list_T *l,
6786 char_u *sep,
6787 int echo_style,
6788 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006790 garray_T join_ga;
6791 int retval;
6792 join_T *p;
6793 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006794
Bram Moolenaard39a7512015-04-16 22:51:22 +02006795 if (l->lv_len < 1)
6796 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006797 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6798 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6799
6800 /* Dispose each item in join_ga. */
6801 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006802 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006803 p = (join_T *)join_ga.ga_data;
6804 for (i = 0; i < join_ga.ga_len; ++i)
6805 {
6806 vim_free(p->tofree);
6807 ++p;
6808 }
6809 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006810 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006811
6812 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006813}
6814
6815/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006816 * Return the next (unique) copy ID.
6817 * Used for serializing nested structures.
6818 */
6819 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006820get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006821{
6822 current_copyID += COPYID_INC;
6823 return current_copyID;
6824}
6825
6826/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 * Garbage collection for lists and dictionaries.
6828 *
6829 * We use reference counts to be able to free most items right away when they
6830 * are no longer used. But for composite items it's possible that it becomes
6831 * unused while the reference count is > 0: When there is a recursive
6832 * reference. Example:
6833 * :let l = [1, 2, 3]
6834 * :let d = {9: l}
6835 * :let l[1] = d
6836 *
6837 * Since this is quite unusual we handle this with garbage collection: every
6838 * once in a while find out which lists and dicts are not referenced from any
6839 * variable.
6840 *
6841 * Here is a good reference text about garbage collection (refers to Python
6842 * but it applies to all reference-counting mechanisms):
6843 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845
6846/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006847 * Do garbage collection for lists and dicts.
6848 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006851garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006852{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006854 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 buf_T *buf;
6856 win_T *wp;
6857 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006858 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006859 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006861#ifdef FEAT_WINDOWS
6862 tabpage_T *tp;
6863#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006865 /* Only do this once. */
6866 want_garbage_collect = FALSE;
6867 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006868 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006869
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 /* We advance by two because we add one for items referenced through
6871 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006872 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 /*
6875 * 1. Go through all accessible variables and mark all lists and dicts
6876 * with copyID.
6877 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006878
6879 /* Don't free variables in the previous_funccal list unless they are only
6880 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006881 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006882 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6883 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6885 NULL);
6886 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6887 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 }
6889
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 /* script-local variables */
6891 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893
6894 /* buffer-local variables */
6895 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6897 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898
6899 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006900 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6902 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006903#ifdef FEAT_AUTOCMD
6904 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6906 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006907#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006909#ifdef FEAT_WINDOWS
6910 /* tabpage-local variables */
6911 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6913 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006914#endif
6915
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918
6919 /* function-local variables */
6920 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6921 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6923 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 }
6925
Bram Moolenaard812df62008-11-09 12:46:09 +00006926 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006928
Bram Moolenaar1dced572012-04-05 16:54:08 +02006929#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006931#endif
6932
Bram Moolenaardb913952012-06-29 12:54:53 +02006933#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006935#endif
6936
6937#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006939#endif
6940
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006941#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006942 abort = abort || set_ref_in_channel(copyID);
6943#endif
6944
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006946 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 /*
6948 * 2. Free lists and dictionaries that are not referenced.
6949 */
6950 did_free = free_unref_items(copyID);
6951
6952 /*
6953 * 3. Check if any funccal can be freed now.
6954 */
6955 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006956 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 if (can_free_funccal(*pfc, copyID))
6958 {
6959 fc = *pfc;
6960 *pfc = fc->caller;
6961 free_funccal(fc, TRUE);
6962 did_free = TRUE;
6963 did_free_funccal = TRUE;
6964 }
6965 else
6966 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 if (did_free_funccal)
6969 /* When a funccal was freed some more items might be garbage
6970 * collected, so run again. */
6971 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006973 else if (p_verbose > 0)
6974 {
6975 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6976 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977
6978 return did_free;
6979}
6980
6981/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006982 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006983 */
6984 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006985free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006986{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006987 dict_T *dd, *dd_next;
6988 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006989 int did_free = FALSE;
6990
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006992 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 */
6994 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006995 {
6996 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006999 /* Free the Dictionary and ordinary items it contains, but don't
7000 * recurse into Lists and Dictionaries, they will be in the list
7001 * of dicts or list of lists. */
7002 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007005 dd = dd_next;
7006 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007
7008 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009 * Go through the list of lists and free items without the copyID.
7010 * But don't free a list that has a watcher (used in a for loop), these
7011 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 */
7013 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007014 {
7015 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007016 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7017 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007019 /* Free the List and ordinary items it contains, but don't recurse
7020 * into Lists and Dictionaries, they will be in the list of dicts
7021 * or list of lists. */
7022 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007025 ll = ll_next;
7026 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007027
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 return did_free;
7029}
7030
7031/*
7032 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 * "list_stack" is used to add lists to be marked. Can be NULL.
7034 *
7035 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007038set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039{
7040 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007042 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007043 hashtab_T *cur_ht;
7044 ht_stack_T *ht_stack = NULL;
7045 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 cur_ht = ht;
7048 for (;;)
7049 {
7050 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 /* Mark each item in the hashtab. If the item contains a hashtab
7053 * it is added to ht_stack, if it contains a list it is added to
7054 * list_stack. */
7055 todo = (int)cur_ht->ht_used;
7056 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7057 if (!HASHITEM_EMPTY(hi))
7058 {
7059 --todo;
7060 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7061 &ht_stack, list_stack);
7062 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064
7065 if (ht_stack == NULL)
7066 break;
7067
7068 /* take an item from the stack */
7069 cur_ht = ht_stack->ht;
7070 tempitem = ht_stack;
7071 ht_stack = ht_stack->prev;
7072 free(tempitem);
7073 }
7074
7075 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076}
7077
7078/*
7079 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7081 *
7082 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007085set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007087 listitem_T *li;
7088 int abort = FALSE;
7089 list_T *cur_l;
7090 list_stack_T *list_stack = NULL;
7091 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007093 cur_l = l;
7094 for (;;)
7095 {
7096 if (!abort)
7097 /* Mark each item in the list. If the item contains a hashtab
7098 * it is added to ht_stack, if it contains a list it is added to
7099 * list_stack. */
7100 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7101 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7102 ht_stack, &list_stack);
7103 if (list_stack == NULL)
7104 break;
7105
7106 /* take an item from the stack */
7107 cur_l = list_stack->list;
7108 tempitem = list_stack;
7109 list_stack = list_stack->prev;
7110 free(tempitem);
7111 }
7112
7113 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114}
7115
7116/*
7117 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 * "list_stack" is used to add lists to be marked. Can be NULL.
7119 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7120 *
7121 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007123 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007124set_ref_in_item(
7125 typval_T *tv,
7126 int copyID,
7127 ht_stack_T **ht_stack,
7128 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007129{
7130 dict_T *dd;
7131 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007133
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007134 if (tv->v_type == VAR_DICT || tv->v_type == VAR_PARTIAL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007135 {
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007136 if (tv->v_type == VAR_DICT)
7137 dd = tv->vval.v_dict;
7138 else if (tv->vval.v_partial != NULL)
7139 dd = tv->vval.v_partial->pt_dict;
7140 else
7141 dd = NULL;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007142 if (dd != NULL && dd->dv_copyID != copyID)
7143 {
7144 /* Didn't see this dict yet. */
7145 dd->dv_copyID = copyID;
7146 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007148 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7149 }
7150 else
7151 {
7152 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7153 if (newitem == NULL)
7154 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007155 else
7156 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007157 newitem->ht = &dd->dv_hashtab;
7158 newitem->prev = *ht_stack;
7159 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007160 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007161 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007162 }
7163 }
7164 else if (tv->v_type == VAR_LIST)
7165 {
7166 ll = tv->vval.v_list;
7167 if (ll != NULL && ll->lv_copyID != copyID)
7168 {
7169 /* Didn't see this list yet. */
7170 ll->lv_copyID = copyID;
7171 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007173 abort = set_ref_in_list(ll, copyID, ht_stack);
7174 }
7175 else
7176 {
7177 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007179 if (newitem == NULL)
7180 abort = TRUE;
7181 else
7182 {
7183 newitem->list = ll;
7184 newitem->prev = *list_stack;
7185 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007186 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007187 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007188 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007189 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007190 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007191}
7192
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007193 static void
7194partial_free(partial_T *pt, int free_dict)
7195{
7196 int i;
7197
7198 for (i = 0; i < pt->pt_argc; ++i)
7199 clear_tv(&pt->pt_argv[i]);
7200 vim_free(pt->pt_argv);
7201 if (free_dict)
7202 dict_unref(pt->pt_dict);
7203 func_unref(pt->pt_name);
7204 vim_free(pt->pt_name);
7205 vim_free(pt);
7206}
7207
7208/*
7209 * Unreference a closure: decrement the reference count and free it when it
7210 * becomes zero.
7211 */
7212 void
7213partial_unref(partial_T *pt)
7214{
7215 if (pt != NULL && --pt->pt_refcount <= 0)
7216 partial_free(pt, TRUE);
7217}
7218
Bram Moolenaard9fba312005-06-26 22:34:35 +00007219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 * Allocate an empty header for a dictionary.
7221 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007222 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007223dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224{
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007226
Bram Moolenaar33570922005-01-25 22:26:29 +00007227 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007228 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007229 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007230 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007231 if (first_dict != NULL)
7232 first_dict->dv_used_prev = d;
7233 d->dv_used_next = first_dict;
7234 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007235 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007236
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007238 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007239 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007240 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007241 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007242 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007243 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244}
7245
7246/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007247 * Allocate an empty dict for a return value.
7248 * Returns OK or FAIL.
7249 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007251rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007252{
7253 dict_T *d = dict_alloc();
7254
7255 if (d == NULL)
7256 return FAIL;
7257
7258 rettv->vval.v_dict = d;
7259 rettv->v_type = VAR_DICT;
7260 ++d->dv_refcount;
7261 return OK;
7262}
7263
7264
7265/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 * Unreference a Dictionary: decrement the reference count and free it when it
7267 * becomes zero.
7268 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007272 if (d != NULL && --d->dv_refcount <= 0)
7273 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274}
7275
7276/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007277 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 * Ignores the reference count.
7279 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007281dict_free(
7282 dict_T *d,
7283 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007286 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007287 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007289 /* Remove the dict from the list of dicts for garbage collection. */
7290 if (d->dv_used_prev == NULL)
7291 first_dict = d->dv_used_next;
7292 else
7293 d->dv_used_prev->dv_used_next = d->dv_used_next;
7294 if (d->dv_used_next != NULL)
7295 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7296
7297 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007298 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007299 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 if (!HASHITEM_EMPTY(hi))
7303 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007304 /* Remove the item before deleting it, just in case there is
7305 * something recursive causing trouble. */
7306 di = HI2DI(hi);
7307 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007308 if (recurse || (di->di_tv.v_type != VAR_LIST
7309 && di->di_tv.v_type != VAR_DICT))
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007310 {
7311 if (!recurse && di->di_tv.v_type == VAR_PARTIAL)
7312 {
7313 partial_T *pt = di->di_tv.vval.v_partial;
7314
7315 /* We unref the partial but not the dict it refers to. */
7316 if (pt != NULL && --pt->pt_refcount == 0)
7317 partial_free(pt, FALSE);
7318 }
7319 else
7320 clear_tv(&di->di_tv);
7321 }
Bram Moolenaar685295c2006-10-15 20:37:38 +00007322 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 --todo;
7324 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007325 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327 vim_free(d);
7328}
7329
7330/*
7331 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 * The "key" is copied to the new item.
7333 * Note that the value of the item "di_tv" still needs to be initialized!
7334 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007336 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007337dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338{
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007341 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007343 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007345 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007346 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348}
7349
7350/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351 * Make a copy of a Dictionary item.
7352 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007354dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355{
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007357
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007358 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7359 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 if (di != NULL)
7361 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007363 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007364 copy_tv(&org->di_tv, &di->di_tv);
7365 }
7366 return di;
7367}
7368
7369/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 * Remove item "item" from Dictionary "dict" and free it.
7371 */
7372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007373dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007374{
Bram Moolenaar33570922005-01-25 22:26:29 +00007375 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007378 if (HASHITEM_EMPTY(hi))
7379 EMSG2(_(e_intern2), "dictitem_remove()");
7380 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 dictitem_free(item);
7383}
7384
7385/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 * Free a dict item. Also clears the value.
7387 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007392 if (item->di_flags & DI_FLAGS_ALLOC)
7393 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394}
7395
7396/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007397 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7398 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007399 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 * Returns NULL when out of memory.
7401 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007403dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404{
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 dict_T *copy;
7406 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007407 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409
7410 if (orig == NULL)
7411 return NULL;
7412
7413 copy = dict_alloc();
7414 if (copy != NULL)
7415 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007416 if (copyID != 0)
7417 {
7418 orig->dv_copyID = copyID;
7419 orig->dv_copydict = copy;
7420 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007421 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007422 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007424 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007426 --todo;
7427
7428 di = dictitem_alloc(hi->hi_key);
7429 if (di == NULL)
7430 break;
7431 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007432 {
7433 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7434 copyID) == FAIL)
7435 {
7436 vim_free(di);
7437 break;
7438 }
7439 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007440 else
7441 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7442 if (dict_add(copy, di) == FAIL)
7443 {
7444 dictitem_free(di);
7445 break;
7446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007449
Bram Moolenaare9a41262005-01-15 22:18:47 +00007450 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007451 if (todo > 0)
7452 {
7453 dict_unref(copy);
7454 copy = NULL;
7455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007456 }
7457
7458 return copy;
7459}
7460
7461/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007463 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007466dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467{
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007469}
7470
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007472 * Add a number or string entry to dictionary "d".
7473 * When "str" is NULL use number "nr", otherwise use "str".
7474 * Returns FAIL when out of memory and when key already exists.
7475 */
7476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007477dict_add_nr_str(
7478 dict_T *d,
7479 char *key,
7480 long nr,
7481 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007482{
7483 dictitem_T *item;
7484
7485 item = dictitem_alloc((char_u *)key);
7486 if (item == NULL)
7487 return FAIL;
7488 item->di_tv.v_lock = 0;
7489 if (str == NULL)
7490 {
7491 item->di_tv.v_type = VAR_NUMBER;
7492 item->di_tv.vval.v_number = nr;
7493 }
7494 else
7495 {
7496 item->di_tv.v_type = VAR_STRING;
7497 item->di_tv.vval.v_string = vim_strsave(str);
7498 }
7499 if (dict_add(d, item) == FAIL)
7500 {
7501 dictitem_free(item);
7502 return FAIL;
7503 }
7504 return OK;
7505}
7506
7507/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007508 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007509 * Returns FAIL when out of memory and when key already exists.
7510 */
7511 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007512dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007513{
7514 dictitem_T *item;
7515
7516 item = dictitem_alloc((char_u *)key);
7517 if (item == NULL)
7518 return FAIL;
7519 item->di_tv.v_lock = 0;
7520 item->di_tv.v_type = VAR_LIST;
7521 item->di_tv.vval.v_list = list;
7522 if (dict_add(d, item) == FAIL)
7523 {
7524 dictitem_free(item);
7525 return FAIL;
7526 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007527 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007528 return OK;
7529}
7530
7531/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007532 * Get the number of items in a Dictionary.
7533 */
7534 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007536{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007537 if (d == NULL)
7538 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007539 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007540}
7541
7542/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543 * Find item "key[len]" in Dictionary "d".
7544 * If "len" is negative use strlen(key).
7545 * Returns NULL when not found.
7546 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007547 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550#define AKEYLEN 200
7551 char_u buf[AKEYLEN];
7552 char_u *akey;
7553 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 if (len < 0)
7557 akey = key;
7558 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007559 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 tofree = akey = vim_strnsave(key, len);
7561 if (akey == NULL)
7562 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007563 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007564 else
7565 {
7566 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007567 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007568 akey = buf;
7569 }
7570
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 vim_free(tofree);
7573 if (HASHITEM_EMPTY(hi))
7574 return NULL;
7575 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576}
7577
7578/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007579 * Get a string item from a dictionary.
7580 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007581 * Returns NULL if the entry doesn't exist or out of memory.
7582 */
7583 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007585{
7586 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007587 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007588
7589 di = dict_find(d, key, -1);
7590 if (di == NULL)
7591 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007592 s = get_tv_string(&di->di_tv);
7593 if (save && s != NULL)
7594 s = vim_strsave(s);
7595 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007596}
7597
7598/*
7599 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007600 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007601 */
7602 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007603get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007604{
7605 dictitem_T *di;
7606
7607 di = dict_find(d, key, -1);
7608 if (di == NULL)
7609 return 0;
7610 return get_tv_number(&di->di_tv);
7611}
7612
7613/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 * Return an allocated string with the string representation of a Dictionary.
7615 * May return NULL.
7616 */
7617 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007618dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619{
7620 garray_T ga;
7621 int first = TRUE;
7622 char_u *tofree;
7623 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 return NULL;
7631 ga_init2(&ga, (int)sizeof(char), 80);
7632 ga_append(&ga, '{');
7633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007634 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007635 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 --todo;
7640
7641 if (first)
7642 first = FALSE;
7643 else
7644 ga_concat(&ga, (char_u *)", ");
7645
7646 tofree = string_quote(hi->hi_key, FALSE);
7647 if (tofree != NULL)
7648 {
7649 ga_concat(&ga, tofree);
7650 vim_free(tofree);
7651 }
7652 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007653 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007654 if (s != NULL)
7655 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007657 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007658 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007659 line_breakcheck();
7660
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007663 if (todo > 0)
7664 {
7665 vim_free(ga.ga_data);
7666 return NULL;
7667 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668
7669 ga_append(&ga, '}');
7670 ga_append(&ga, NUL);
7671 return (char_u *)ga.ga_data;
7672}
7673
7674/*
7675 * Allocate a variable for a Dictionary and fill it from "*arg".
7676 * Return OK or FAIL. Returns NOTDONE for {expr}.
7677 */
7678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007679get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680{
Bram Moolenaar33570922005-01-25 22:26:29 +00007681 dict_T *d = NULL;
7682 typval_T tvkey;
7683 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007684 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688
7689 /*
7690 * First check if it's not a curly-braces thing: {expr}.
7691 * Must do this without evaluating, otherwise a function may be called
7692 * twice. Unfortunately this means we need to call eval1() twice for the
7693 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007694 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007696 if (*start != '}')
7697 {
7698 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7699 return FAIL;
7700 if (*start == '}')
7701 return NOTDONE;
7702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703
7704 if (evaluate)
7705 {
7706 d = dict_alloc();
7707 if (d == NULL)
7708 return FAIL;
7709 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007710 tvkey.v_type = VAR_UNKNOWN;
7711 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712
7713 *arg = skipwhite(*arg + 1);
7714 while (**arg != '}' && **arg != NUL)
7715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007716 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 goto failret;
7718 if (**arg != ':')
7719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007720 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007721 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 goto failret;
7723 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007724 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007726 key = get_tv_string_buf_chk(&tvkey, buf);
7727 if (key == NULL || *key == NUL)
7728 {
7729 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7730 if (key != NULL)
7731 EMSG(_(e_emptykey));
7732 clear_tv(&tvkey);
7733 goto failret;
7734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736
7737 *arg = skipwhite(*arg + 1);
7738 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7739 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007740 if (evaluate)
7741 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742 goto failret;
7743 }
7744 if (evaluate)
7745 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007746 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747 if (item != NULL)
7748 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007749 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007750 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 clear_tv(&tv);
7752 goto failret;
7753 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007754 item = dictitem_alloc(key);
7755 clear_tv(&tvkey);
7756 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007759 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007760 if (dict_add(d, item) == FAIL)
7761 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 }
7763 }
7764
7765 if (**arg == '}')
7766 break;
7767 if (**arg != ',')
7768 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007769 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 goto failret;
7771 }
7772 *arg = skipwhite(*arg + 1);
7773 }
7774
7775 if (**arg != '}')
7776 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007777 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007778failret:
7779 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007780 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 return FAIL;
7782 }
7783
7784 *arg = skipwhite(*arg + 1);
7785 if (evaluate)
7786 {
7787 rettv->v_type = VAR_DICT;
7788 rettv->vval.v_dict = d;
7789 ++d->dv_refcount;
7790 }
7791
7792 return OK;
7793}
7794
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007795#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007796#endif
7797
Bram Moolenaar17a13432016-01-24 14:22:10 +01007798 static char *
7799get_var_special_name(int nr)
7800{
7801 switch (nr)
7802 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007803 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007804 case VVAL_TRUE: return "v:true";
7805 case VVAL_NONE: return "v:none";
7806 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007807 }
7808 EMSG2(_(e_intern2), "get_var_special_name()");
7809 return "42";
7810}
7811
Bram Moolenaar8c711452005-01-14 21:53:12 +00007812/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 * Return a string with the string representation of a variable.
7814 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007815 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007816 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007817 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007818 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007819 */
7820 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007821echo_string(
7822 typval_T *tv,
7823 char_u **tofree,
7824 char_u *numbuf,
7825 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007826{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827 static int recurse = 0;
7828 char_u *r = NULL;
7829
Bram Moolenaar33570922005-01-25 22:26:29 +00007830 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007832 if (!did_echo_string_emsg)
7833 {
7834 /* Only give this message once for a recursive call to avoid
7835 * flooding the user with errors. And stop iterating over lists
7836 * and dicts. */
7837 did_echo_string_emsg = TRUE;
7838 EMSG(_("E724: variable nested too deep for displaying"));
7839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007840 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007841 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 }
7843 ++recurse;
7844
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007845 switch (tv->v_type)
7846 {
7847 case VAR_FUNC:
7848 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 r = tv->vval.v_string;
7850 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007851
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007852 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007853 {
7854 partial_T *pt = tv->vval.v_partial;
7855 char_u *fname = string_quote(pt == NULL ? NULL
7856 : pt->pt_name, FALSE);
7857 garray_T ga;
7858 int i;
7859 char_u *tf;
7860
7861 ga_init2(&ga, 1, 100);
7862 ga_concat(&ga, (char_u *)"function(");
7863 if (fname != NULL)
7864 {
7865 ga_concat(&ga, fname);
7866 vim_free(fname);
7867 }
7868 if (pt != NULL && pt->pt_argc > 0)
7869 {
7870 ga_concat(&ga, (char_u *)", [");
7871 for (i = 0; i < pt->pt_argc; ++i)
7872 {
7873 if (i > 0)
7874 ga_concat(&ga, (char_u *)", ");
7875 ga_concat(&ga,
7876 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7877 vim_free(tf);
7878 }
7879 ga_concat(&ga, (char_u *)"]");
7880 }
7881 if (pt != NULL && pt->pt_dict != NULL)
7882 {
7883 typval_T dtv;
7884
7885 ga_concat(&ga, (char_u *)", ");
7886 dtv.v_type = VAR_DICT;
7887 dtv.vval.v_dict = pt->pt_dict;
7888 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7889 vim_free(tf);
7890 }
7891 ga_concat(&ga, (char_u *)")");
7892
7893 *tofree = ga.ga_data;
7894 r = *tofree;
7895 break;
7896 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007897
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007898 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007899 if (tv->vval.v_list == NULL)
7900 {
7901 *tofree = NULL;
7902 r = NULL;
7903 }
7904 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7905 {
7906 *tofree = NULL;
7907 r = (char_u *)"[...]";
7908 }
7909 else
7910 {
7911 tv->vval.v_list->lv_copyID = copyID;
7912 *tofree = list2string(tv, copyID);
7913 r = *tofree;
7914 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007915 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007916
Bram Moolenaar8c711452005-01-14 21:53:12 +00007917 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007918 if (tv->vval.v_dict == NULL)
7919 {
7920 *tofree = NULL;
7921 r = NULL;
7922 }
7923 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7924 {
7925 *tofree = NULL;
7926 r = (char_u *)"{...}";
7927 }
7928 else
7929 {
7930 tv->vval.v_dict->dv_copyID = copyID;
7931 *tofree = dict2string(tv, copyID);
7932 r = *tofree;
7933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007934 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007935
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007936 case VAR_STRING:
7937 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007938 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007939 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007940 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 *tofree = NULL;
7942 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007943 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007944
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007945 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007946#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007947 *tofree = NULL;
7948 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7949 r = numbuf;
7950 break;
7951#endif
7952
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007953 case VAR_SPECIAL:
7954 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007955 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007956 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958
Bram Moolenaar8502c702014-06-17 12:51:16 +02007959 if (--recurse == 0)
7960 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007961 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962}
7963
7964/*
7965 * Return a string with the string representation of a variable.
7966 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7967 * "numbuf" is used for a number.
7968 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007969 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007970 */
7971 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007972tv2string(
7973 typval_T *tv,
7974 char_u **tofree,
7975 char_u *numbuf,
7976 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007977{
7978 switch (tv->v_type)
7979 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007980 case VAR_FUNC:
7981 *tofree = string_quote(tv->vval.v_string, TRUE);
7982 return *tofree;
7983 case VAR_STRING:
7984 *tofree = string_quote(tv->vval.v_string, FALSE);
7985 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007986 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007987#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007988 *tofree = NULL;
7989 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7990 return numbuf;
7991#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007992 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007993 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007994 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007995 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007996 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007997 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007998 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007999 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008000 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008001 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008002 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008003}
8004
8005/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008006 * Return string "str" in ' quotes, doubling ' characters.
8007 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009 */
8010 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008011string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008012{
Bram Moolenaar33570922005-01-25 22:26:29 +00008013 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008014 char_u *p, *r, *s;
8015
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 len = (function ? 13 : 3);
8017 if (str != NULL)
8018 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008019 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 for (p = str; *p != NUL; mb_ptr_adv(p))
8021 if (*p == '\'')
8022 ++len;
8023 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008024 s = r = alloc(len);
8025 if (r != NULL)
8026 {
8027 if (function)
8028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008029 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008030 r += 10;
8031 }
8032 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 if (str != NULL)
8035 for (p = str; *p != NUL; )
8036 {
8037 if (*p == '\'')
8038 *r++ = '\'';
8039 MB_COPY_CHAR(p, r);
8040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008041 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008042 if (function)
8043 *r++ = ')';
8044 *r++ = NUL;
8045 }
8046 return s;
8047}
8048
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008049#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008050/*
8051 * Convert the string "text" to a floating point number.
8052 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8053 * this always uses a decimal point.
8054 * Returns the length of the text that was consumed.
8055 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008056 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008057string2float(
8058 char_u *text,
8059 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060{
8061 char *s = (char *)text;
8062 float_T f;
8063
8064 f = strtod(s, &s);
8065 *value = f;
8066 return (int)((char_u *)s - text);
8067}
8068#endif
8069
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 * Get the value of an environment variable.
8072 * "arg" is pointing to the '$'. It is advanced to after the name.
8073 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008074 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 */
8076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008077get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078{
8079 char_u *string = NULL;
8080 int len;
8081 int cc;
8082 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008083 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
8085 ++*arg;
8086 name = *arg;
8087 len = get_env_len(arg);
8088 if (evaluate)
8089 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008090 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008091 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008092
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008093 cc = name[len];
8094 name[len] = NUL;
8095 /* first try vim_getenv(), fast for normal environment vars */
8096 string = vim_getenv(name, &mustfree);
8097 if (string != NULL && *string != NUL)
8098 {
8099 if (!mustfree)
8100 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008102 else
8103 {
8104 if (mustfree)
8105 vim_free(string);
8106
8107 /* next try expanding things like $VIM and ${HOME} */
8108 string = expand_env_save(name - 1);
8109 if (string != NULL && *string == '$')
8110 {
8111 vim_free(string);
8112 string = NULL;
8113 }
8114 }
8115 name[len] = cc;
8116
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008117 rettv->v_type = VAR_STRING;
8118 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 }
8120
8121 return OK;
8122}
8123
8124/*
8125 * Array with names and number of arguments of all internal functions
8126 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8127 */
8128static struct fst
8129{
8130 char *f_name; /* function name */
8131 char f_min_argc; /* minimal number of arguments */
8132 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008133 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008134 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135} functions[] =
8136{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008137#ifdef FEAT_FLOAT
8138 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008139 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008140#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008141 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008142 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008143 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"append", 2, 2, f_append},
8145 {"argc", 0, 0, f_argc},
8146 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008147 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008148 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008149#ifdef FEAT_FLOAT
8150 {"asin", 1, 1, f_asin}, /* WJMc */
8151#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008152 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008153 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008154 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008155 {"assert_false", 1, 2, f_assert_false},
8156 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008157#ifdef FEAT_FLOAT
8158 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008159 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008162 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"bufexists", 1, 1, f_bufexists},
8164 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8165 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8166 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8167 {"buflisted", 1, 1, f_buflisted},
8168 {"bufloaded", 1, 1, f_bufloaded},
8169 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008170 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"bufwinnr", 1, 1, f_bufwinnr},
8172 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008173 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008174 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008175 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008176#ifdef FEAT_FLOAT
8177 {"ceil", 1, 1, f_ceil},
8178#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008179#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008180 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008181 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8182 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008183 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008184 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008185 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008186 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008187 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008188 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008189 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008190 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008191 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8192 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008193 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008194 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008195#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008196 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008197 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008199 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008201#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008202 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008203 {"complete_add", 1, 1, f_complete_add},
8204 {"complete_check", 0, 0, f_complete_check},
8205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008207 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008208#ifdef FEAT_FLOAT
8209 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008210 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008211#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008212 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008214 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008215 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008216 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008218 {"diff_filler", 1, 1, f_diff_filler},
8219 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008220 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008221 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008223 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"eventhandler", 0, 0, f_eventhandler},
8225 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008226 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008228#ifdef FEAT_FLOAT
8229 {"exp", 1, 1, f_exp},
8230#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008231 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008232 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008233 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8235 {"filereadable", 1, 1, f_filereadable},
8236 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008237 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008238 {"finddir", 1, 3, f_finddir},
8239 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240#ifdef FEAT_FLOAT
8241 {"float2nr", 1, 1, f_float2nr},
8242 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008243 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008244#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008245 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"fnamemodify", 2, 2, f_fnamemodify},
8247 {"foldclosed", 1, 1, f_foldclosed},
8248 {"foldclosedend", 1, 1, f_foldclosedend},
8249 {"foldlevel", 1, 1, f_foldlevel},
8250 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008251 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008253 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008254 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008255 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008256 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008257 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"getchar", 0, 1, f_getchar},
8259 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008260 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"getcmdline", 0, 0, f_getcmdline},
8262 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008263 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008264 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008265 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008266 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008267 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008268 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"getfsize", 1, 1, f_getfsize},
8270 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008271 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008273 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008274 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008275 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008276 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008277 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008278 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008280 {"gettabvar", 2, 3, f_gettabvar},
8281 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"getwinposx", 0, 0, f_getwinposx},
8283 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008284 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008285 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008286 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008287 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008289 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008290 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008291 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8293 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8294 {"histadd", 2, 2, f_histadd},
8295 {"histdel", 1, 2, f_histdel},
8296 {"histget", 1, 2, f_histget},
8297 {"histnr", 1, 1, f_histnr},
8298 {"hlID", 1, 1, f_hlID},
8299 {"hlexists", 1, 1, f_hlexists},
8300 {"hostname", 0, 0, f_hostname},
8301 {"iconv", 3, 3, f_iconv},
8302 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008303 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008304 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008306 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"inputrestore", 0, 0, f_inputrestore},
8308 {"inputsave", 0, 0, f_inputsave},
8309 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008310 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008311 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008313 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008314#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8315 {"isnan", 1, 1, f_isnan},
8316#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008317 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008318#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008319 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008320 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008321 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008322 {"job_start", 1, 2, f_job_start},
8323 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008324 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008325#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008326 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008327 {"js_decode", 1, 1, f_js_decode},
8328 {"js_encode", 1, 1, f_js_encode},
8329 {"json_decode", 1, 1, f_json_decode},
8330 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008331 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008333 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"libcall", 3, 3, f_libcall},
8335 {"libcallnr", 3, 3, f_libcallnr},
8336 {"line", 1, 1, f_line},
8337 {"line2byte", 1, 1, f_line2byte},
8338 {"lispindent", 1, 1, f_lispindent},
8339 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008341 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008342 {"log10", 1, 1, f_log10},
8343#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008344#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008345 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008346#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008347 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008348 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008349 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008350 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008351 {"matchadd", 2, 5, f_matchadd},
8352 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008353 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008354 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008355 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008356 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008357 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008358 {"max", 1, 1, f_max},
8359 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008360#ifdef vim_mkdir
8361 {"mkdir", 1, 3, f_mkdir},
8362#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008363 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008364#ifdef FEAT_MZSCHEME
8365 {"mzeval", 1, 1, f_mzeval},
8366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008368 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008369 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008370 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008371#ifdef FEAT_PERL
8372 {"perleval", 1, 1, f_perleval},
8373#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008374#ifdef FEAT_FLOAT
8375 {"pow", 2, 2, f_pow},
8376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008378 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008379 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008380#ifdef FEAT_PYTHON3
8381 {"py3eval", 1, 1, f_py3eval},
8382#endif
8383#ifdef FEAT_PYTHON
8384 {"pyeval", 1, 1, f_pyeval},
8385#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008386 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008387 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008388 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008389#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008390 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008391#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008392 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"remote_expr", 2, 3, f_remote_expr},
8394 {"remote_foreground", 1, 1, f_remote_foreground},
8395 {"remote_peek", 1, 2, f_remote_peek},
8396 {"remote_read", 1, 1, f_remote_read},
8397 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008398 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008400 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008402 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008403#ifdef FEAT_FLOAT
8404 {"round", 1, 1, f_round},
8405#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008406 {"screenattr", 2, 2, f_screenattr},
8407 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008408 {"screencol", 0, 0, f_screencol},
8409 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008410 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008411 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008412 {"searchpair", 3, 7, f_searchpair},
8413 {"searchpairpos", 3, 7, f_searchpairpos},
8414 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {"server2client", 2, 2, f_server2client},
8416 {"serverlist", 0, 0, f_serverlist},
8417 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008418 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008420 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008422 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008423 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008424 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008425 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008427 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008428 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008430#ifdef FEAT_CRYPT
8431 {"sha256", 1, 1, f_sha256},
8432#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008433 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008434 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008436#ifdef FEAT_FLOAT
8437 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008438 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008439#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008440 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008441 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008442 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008443 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008444 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008445#ifdef FEAT_FLOAT
8446 {"sqrt", 1, 1, f_sqrt},
8447 {"str2float", 1, 1, f_str2float},
8448#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008449 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008450 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008451 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452#ifdef HAVE_STRFTIME
8453 {"strftime", 1, 2, f_strftime},
8454#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008455 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008456 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {"strlen", 1, 1, f_strlen},
8458 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008459 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008461 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008462 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {"substitute", 4, 4, f_substitute},
8464 {"synID", 3, 3, f_synID},
8465 {"synIDattr", 2, 3, f_synIDattr},
8466 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008467 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008468 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008469 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008470 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008471 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008472 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008473 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008474 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008475 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008476#ifdef FEAT_FLOAT
8477 {"tan", 1, 1, f_tan},
8478 {"tanh", 1, 1, f_tanh},
8479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008481 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008482#ifdef FEAT_TIMERS
8483 {"timer_start", 2, 3, f_timer_start},
8484 {"timer_stop", 1, 1, f_timer_stop},
8485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {"tolower", 1, 1, f_tolower},
8487 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008488 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008489#ifdef FEAT_FLOAT
8490 {"trunc", 1, 1, f_trunc},
8491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008493 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008494 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008495 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008496 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 {"virtcol", 1, 1, f_virtcol},
8498 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008499 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008500 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008501 {"win_getid", 0, 2, f_win_getid},
8502 {"win_gotoid", 1, 1, f_win_gotoid},
8503 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8504 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {"winbufnr", 1, 1, f_winbufnr},
8506 {"wincol", 0, 0, f_wincol},
8507 {"winheight", 1, 1, f_winheight},
8508 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008509 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008511 {"winrestview", 1, 1, f_winrestview},
8512 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008514 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008515 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008516 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517};
8518
8519#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8520
8521/*
8522 * Function given to ExpandGeneric() to obtain the list of internal
8523 * or user defined function names.
8524 */
8525 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008526get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527{
8528 static int intidx = -1;
8529 char_u *name;
8530
8531 if (idx == 0)
8532 intidx = -1;
8533 if (intidx < 0)
8534 {
8535 name = get_user_func_name(xp, idx);
8536 if (name != NULL)
8537 return name;
8538 }
8539 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8540 {
8541 STRCPY(IObuff, functions[intidx].f_name);
8542 STRCAT(IObuff, "(");
8543 if (functions[intidx].f_max_argc == 0)
8544 STRCAT(IObuff, ")");
8545 return IObuff;
8546 }
8547
8548 return NULL;
8549}
8550
8551/*
8552 * Function given to ExpandGeneric() to obtain the list of internal or
8553 * user defined variable or function names.
8554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008556get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558 static int intidx = -1;
8559 char_u *name;
8560
8561 if (idx == 0)
8562 intidx = -1;
8563 if (intidx < 0)
8564 {
8565 name = get_function_name(xp, idx);
8566 if (name != NULL)
8567 return name;
8568 }
8569 return get_user_var_name(xp, ++intidx);
8570}
8571
8572#endif /* FEAT_CMDL_COMPL */
8573
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008574#if defined(EBCDIC) || defined(PROTO)
8575/*
8576 * Compare struct fst by function name.
8577 */
8578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008579compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008580{
8581 struct fst *p1 = (struct fst *)s1;
8582 struct fst *p2 = (struct fst *)s2;
8583
8584 return STRCMP(p1->f_name, p2->f_name);
8585}
8586
8587/*
8588 * Sort the function table by function name.
8589 * The sorting of the table above is ASCII dependant.
8590 * On machines using EBCDIC we have to sort it.
8591 */
8592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008593sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008594{
8595 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8596
8597 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8598}
8599#endif
8600
8601
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602/*
8603 * Find internal function in table above.
8604 * Return index, or -1 if not found
8605 */
8606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008607find_internal_func(
8608 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
8610 int first = 0;
8611 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8612 int cmp;
8613 int x;
8614
8615 /*
8616 * Find the function name in the table. Binary search.
8617 */
8618 while (first <= last)
8619 {
8620 x = first + ((unsigned)(last - first) >> 1);
8621 cmp = STRCMP(name, functions[x].f_name);
8622 if (cmp < 0)
8623 last = x - 1;
8624 else if (cmp > 0)
8625 first = x + 1;
8626 else
8627 return x;
8628 }
8629 return -1;
8630}
8631
8632/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008633 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8634 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008635 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8636 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008637 */
8638 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008639deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008640{
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008642 int cc;
8643
Bram Moolenaar65639032016-03-16 21:40:30 +01008644 if (partialp != NULL)
8645 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008646
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008647 cc = name[*lenp];
8648 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008649 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008651 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008652 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 {
8655 *lenp = 0;
8656 return (char_u *)""; /* just in case */
8657 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008658 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008660 }
8661
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008662 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8663 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008664 partial_T *pt = v->di_tv.vval.v_partial;
8665
8666 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008667 {
8668 *lenp = 0;
8669 return (char_u *)""; /* just in case */
8670 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008671 if (partialp != NULL)
8672 *partialp = pt;
8673 *lenp = (int)STRLEN(pt->pt_name);
8674 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008675 }
8676
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008677 return name;
8678}
8679
8680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 * Allocate a variable for the result of a function.
8682 * Return OK or FAIL.
8683 */
8684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008685get_func_tv(
8686 char_u *name, /* name of the function */
8687 int len, /* length of "name" */
8688 typval_T *rettv,
8689 char_u **arg, /* argument, pointing to the '(' */
8690 linenr_T firstline, /* first line of range */
8691 linenr_T lastline, /* last line of range */
8692 int *doesrange, /* return: function handled range */
8693 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008694 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008695 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 char_u *argp;
8698 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008699 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 int argcount = 0; /* number of arguments found */
8701
8702 /*
8703 * Get the arguments.
8704 */
8705 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008706 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {
8708 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8709 if (*argp == ')' || *argp == ',' || *argp == NUL)
8710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8712 {
8713 ret = FAIL;
8714 break;
8715 }
8716 ++argcount;
8717 if (*argp != ',')
8718 break;
8719 }
8720 if (*argp == ')')
8721 ++argp;
8722 else
8723 ret = FAIL;
8724
8725 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008726 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008727 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 {
8730 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008731 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008733 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
8739 *arg = skipwhite(argp);
8740 return ret;
8741}
8742
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008743#define ERROR_UNKNOWN 0
8744#define ERROR_TOOMANY 1
8745#define ERROR_TOOFEW 2
8746#define ERROR_SCRIPT 3
8747#define ERROR_DICT 4
8748#define ERROR_NONE 5
8749#define ERROR_OTHER 6
8750#define FLEN_FIXED 40
8751
8752/*
8753 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8754 * Change <SNR>123_name() to K_SNR 123_name().
8755 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8756 * (slow).
8757 */
8758 static char_u *
8759fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8760{
8761 int llen;
8762 char_u *fname;
8763 int i;
8764
8765 llen = eval_fname_script(name);
8766 if (llen > 0)
8767 {
8768 fname_buf[0] = K_SPECIAL;
8769 fname_buf[1] = KS_EXTRA;
8770 fname_buf[2] = (int)KE_SNR;
8771 i = 3;
8772 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8773 {
8774 if (current_SID <= 0)
8775 *error = ERROR_SCRIPT;
8776 else
8777 {
8778 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8779 i = (int)STRLEN(fname_buf);
8780 }
8781 }
8782 if (i + STRLEN(name + llen) < FLEN_FIXED)
8783 {
8784 STRCPY(fname_buf + i, name + llen);
8785 fname = fname_buf;
8786 }
8787 else
8788 {
8789 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8790 if (fname == NULL)
8791 *error = ERROR_OTHER;
8792 else
8793 {
8794 *tofree = fname;
8795 mch_memmove(fname, fname_buf, (size_t)i);
8796 STRCPY(fname + i, name + llen);
8797 }
8798 }
8799 }
8800 else
8801 fname = name;
8802 return fname;
8803}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804
8805/*
8806 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008807 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008808 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008811call_func(
8812 char_u *funcname, /* name of the function */
8813 int len, /* length of "name" */
8814 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008815 int argcount_in, /* number of "argvars" */
8816 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008817 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008818 linenr_T firstline, /* first line of range */
8819 linenr_T lastline, /* last line of range */
8820 int *doesrange, /* return: function handled range */
8821 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008822 partial_T *partial, /* optional, can be NULL */
8823 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 int error = ERROR_NONE;
8827 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008830 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008832 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008833 int argcount = argcount_in;
8834 typval_T *argvars = argvars_in;
8835 dict_T *selfdict = selfdict_in;
8836 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8837 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008838
8839 /* Make a copy of the name, if it comes from a funcref variable it could
8840 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008841 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008842 if (name == NULL)
8843 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008845 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846
8847 *doesrange = FALSE;
8848
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008849 if (partial != NULL)
8850 {
8851 if (partial->pt_dict != NULL)
8852 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008853 /* When the function has a partial with a dict and there is a dict
8854 * argument, use the dict argument. That is backwards compatible.
8855 */
8856 if (selfdict_in == NULL)
8857 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008858 }
8859 if (error == ERROR_NONE && partial->pt_argc > 0)
8860 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008861 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8862 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8863 for (i = 0; i < argcount_in; ++i)
8864 argv[i + argv_clear] = argvars_in[i];
8865 argvars = argv;
8866 argcount = partial->pt_argc + argcount_in;
8867 }
8868 }
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870
8871 /* execute the function if no errors detected and executing */
8872 if (evaluate && error == ERROR_NONE)
8873 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008874 char_u *rfname = fname;
8875
8876 /* Ignore "g:" before a function name. */
8877 if (fname[0] == 'g' && fname[1] == ':')
8878 rfname = fname + 2;
8879
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008880 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8881 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 error = ERROR_UNKNOWN;
8883
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008884 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
8886 /*
8887 * User defined function.
8888 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008889 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008890
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008892 /* Trigger FuncUndefined event, may load the function. */
8893 if (fp == NULL
8894 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008895 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008896 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008898 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008899 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 }
8901#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008902 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008903 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008904 {
8905 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008906 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008907 }
8908
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 if (fp != NULL)
8910 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008911 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008913 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008915 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008917 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008918 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 else
8920 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008921 int did_save_redo = FALSE;
8922
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 /*
8924 * Call the user function.
8925 * Save and restore search patterns, script variables and
8926 * redo buffer.
8927 */
8928 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008929#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008930 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008931#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008932 {
8933 saveRedobuff();
8934 did_save_redo = TRUE;
8935 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008936 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008937 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008938 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008939 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8940 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8941 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008942 /* Function was unreferenced while being used, free it
8943 * now. */
8944 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008945 if (did_save_redo)
8946 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 restore_search_patterns();
8948 error = ERROR_NONE;
8949 }
8950 }
8951 }
8952 else
8953 {
8954 /*
8955 * Find the function name in the table, call its implementation.
8956 */
8957 i = find_internal_func(fname);
8958 if (i >= 0)
8959 {
8960 if (argcount < functions[i].f_min_argc)
8961 error = ERROR_TOOFEW;
8962 else if (argcount > functions[i].f_max_argc)
8963 error = ERROR_TOOMANY;
8964 else
8965 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008966 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 error = ERROR_NONE;
8969 }
8970 }
8971 }
8972 /*
8973 * The function call (or "FuncUndefined" autocommand sequence) might
8974 * have been aborted by an error, an interrupt, or an explicitly thrown
8975 * exception that has not been caught so far. This situation can be
8976 * tested for by calling aborting(). For an error in an internal
8977 * function or for the "E132" error in call_user_func(), however, the
8978 * throw point at which the "force_abort" flag (temporarily reset by
8979 * emsg()) is normally updated has not been reached yet. We need to
8980 * update that flag first to make aborting() reliable.
8981 */
8982 update_force_abort();
8983 }
8984 if (error == ERROR_NONE)
8985 ret = OK;
8986
8987 /*
8988 * Report an error unless the argument evaluation or function call has been
8989 * cancelled due to an aborting error, an interrupt, or an exception.
8990 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008991 if (!aborting())
8992 {
8993 switch (error)
8994 {
8995 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008996 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008997 break;
8998 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008999 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009000 break;
9001 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009002 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009003 name);
9004 break;
9005 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009006 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009007 name);
9008 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009009 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009010 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009011 name);
9012 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009013 }
9014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009016 while (argv_clear > 0)
9017 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009018 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009019 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020
9021 return ret;
9022}
9023
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009024/*
9025 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009026 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009027 */
9028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009029emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009030{
9031 char_u *p;
9032
9033 if (*name == K_SPECIAL)
9034 p = concat_str((char_u *)"<SNR>", name + 3);
9035 else
9036 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009037 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009038 if (p != name)
9039 vim_free(p);
9040}
9041
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009042/*
9043 * Return TRUE for a non-zero Number and a non-empty String.
9044 */
9045 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009046non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009047{
9048 return ((argvars[0].v_type == VAR_NUMBER
9049 && argvars[0].vval.v_number != 0)
9050 || (argvars[0].v_type == VAR_STRING
9051 && argvars[0].vval.v_string != NULL
9052 && *argvars[0].vval.v_string != NUL));
9053}
9054
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055/*********************************************
9056 * Implementation of the built-in functions
9057 */
9058
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009059#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009060static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009061
9062/*
9063 * Get the float value of "argvars[0]" into "f".
9064 * Returns FAIL when the argument is not a Number or Float.
9065 */
9066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009067get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009068{
9069 if (argvars[0].v_type == VAR_FLOAT)
9070 {
9071 *f = argvars[0].vval.v_float;
9072 return OK;
9073 }
9074 if (argvars[0].v_type == VAR_NUMBER)
9075 {
9076 *f = (float_T)argvars[0].vval.v_number;
9077 return OK;
9078 }
9079 EMSG(_("E808: Number or Float required"));
9080 return FAIL;
9081}
9082
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009083/*
9084 * "abs(expr)" function
9085 */
9086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009087f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009088{
9089 if (argvars[0].v_type == VAR_FLOAT)
9090 {
9091 rettv->v_type = VAR_FLOAT;
9092 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9093 }
9094 else
9095 {
9096 varnumber_T n;
9097 int error = FALSE;
9098
9099 n = get_tv_number_chk(&argvars[0], &error);
9100 if (error)
9101 rettv->vval.v_number = -1;
9102 else if (n > 0)
9103 rettv->vval.v_number = n;
9104 else
9105 rettv->vval.v_number = -n;
9106 }
9107}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009108
9109/*
9110 * "acos()" function
9111 */
9112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009113f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009114{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009115 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009116
9117 rettv->v_type = VAR_FLOAT;
9118 if (get_float_arg(argvars, &f) == OK)
9119 rettv->vval.v_float = acos(f);
9120 else
9121 rettv->vval.v_float = 0.0;
9122}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009123#endif
9124
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009126 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 */
9128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009129f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130{
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009134 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009136 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009137 && !tv_check_lock(l->lv_lock,
9138 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009139 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009141 }
9142 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009143 EMSG(_(e_listreq));
9144}
9145
9146/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009147 * "alloc_fail(id, countdown, repeat)" function
9148 */
9149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009150f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009151{
9152 if (argvars[0].v_type != VAR_NUMBER
9153 || argvars[0].vval.v_number <= 0
9154 || argvars[1].v_type != VAR_NUMBER
9155 || argvars[1].vval.v_number < 0
9156 || argvars[2].v_type != VAR_NUMBER)
9157 EMSG(_(e_invarg));
9158 else
9159 {
9160 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009161 if (alloc_fail_id >= aid_last)
9162 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009163 alloc_fail_countdown = argvars[1].vval.v_number;
9164 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009165 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009166 }
9167}
9168
9169/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009170 * "and(expr, expr)" function
9171 */
9172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009173f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009174{
9175 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9176 & get_tv_number_chk(&argvars[1], NULL);
9177}
9178
9179/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009180 * "append(lnum, string/list)" function
9181 */
9182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009183f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009184{
9185 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009187 list_T *l = NULL;
9188 listitem_T *li = NULL;
9189 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009190 long added = 0;
9191
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009192 /* When coming here from Insert mode, sync undo, so that this can be
9193 * undone separately from what was previously inserted. */
9194 if (u_sync_once == 2)
9195 {
9196 u_sync_once = 1; /* notify that u_sync() was called */
9197 u_sync(TRUE);
9198 }
9199
Bram Moolenaar0d660222005-01-07 21:51:51 +00009200 lnum = get_tv_lnum(argvars);
9201 if (lnum >= 0
9202 && lnum <= curbuf->b_ml.ml_line_count
9203 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009204 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009205 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009206 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009207 l = argvars[1].vval.v_list;
9208 if (l == NULL)
9209 return;
9210 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009211 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009212 for (;;)
9213 {
9214 if (l == NULL)
9215 tv = &argvars[1]; /* append a string */
9216 else if (li == NULL)
9217 break; /* end of list */
9218 else
9219 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009220 line = get_tv_string_chk(tv);
9221 if (line == NULL) /* type error */
9222 {
9223 rettv->vval.v_number = 1; /* Failed */
9224 break;
9225 }
9226 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009227 ++added;
9228 if (l == NULL)
9229 break;
9230 li = li->li_next;
9231 }
9232
9233 appended_lines_mark(lnum, added);
9234 if (curwin->w_cursor.lnum > lnum)
9235 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 else
9238 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239}
9240
9241/*
9242 * "argc()" function
9243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009245f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009247 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248}
9249
9250/*
9251 * "argidx()" function
9252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009254f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257}
9258
9259/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009260 * "arglistid()" function
9261 */
9262 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009263f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009264{
9265 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009266
9267 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009268 wp = find_tabwin(&argvars[0], &argvars[1]);
9269 if (wp != NULL)
9270 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009271}
9272
9273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 * "argv(nr)" function
9275 */
9276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009277f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279 int idx;
9280
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009281 if (argvars[0].v_type != VAR_UNKNOWN)
9282 {
9283 idx = get_tv_number_chk(&argvars[0], NULL);
9284 if (idx >= 0 && idx < ARGCOUNT)
9285 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9286 else
9287 rettv->vval.v_string = NULL;
9288 rettv->v_type = VAR_STRING;
9289 }
9290 else if (rettv_list_alloc(rettv) == OK)
9291 for (idx = 0; idx < ARGCOUNT; ++idx)
9292 list_append_string(rettv->vval.v_list,
9293 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294}
9295
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009296static void prepare_assert_error(garray_T*gap);
9297static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9298static void assert_error(garray_T *gap);
9299static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009300
9301/*
9302 * Prepare "gap" for an assert error and add the sourcing position.
9303 */
9304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009305prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009306{
9307 char buf[NUMBUFLEN];
9308
9309 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009310 if (sourcing_name != NULL)
9311 {
9312 ga_concat(gap, sourcing_name);
9313 if (sourcing_lnum > 0)
9314 ga_concat(gap, (char_u *)" ");
9315 }
9316 if (sourcing_lnum > 0)
9317 {
9318 sprintf(buf, "line %ld", (long)sourcing_lnum);
9319 ga_concat(gap, (char_u *)buf);
9320 }
9321 if (sourcing_name != NULL || sourcing_lnum > 0)
9322 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009323}
9324
9325/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009326 * Append "str" to "gap", escaping unprintable characters.
9327 * Changes NL to \n, CR to \r, etc.
9328 */
9329 static void
9330ga_concat_esc(garray_T *gap, char_u *str)
9331{
9332 char_u *p;
9333 char_u buf[NUMBUFLEN];
9334
Bram Moolenaarf1551962016-03-15 12:55:58 +01009335 if (str == NULL)
9336 {
9337 ga_concat(gap, (char_u *)"NULL");
9338 return;
9339 }
9340
Bram Moolenaar23689172016-02-15 22:37:37 +01009341 for (p = str; *p != NUL; ++p)
9342 switch (*p)
9343 {
9344 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9345 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9346 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9347 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9348 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9349 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9350 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9351 default:
9352 if (*p < ' ')
9353 {
9354 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9355 ga_concat(gap, buf);
9356 }
9357 else
9358 ga_append(gap, *p);
9359 break;
9360 }
9361}
9362
9363/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009364 * Fill "gap" with information about an assert error.
9365 */
9366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009367fill_assert_error(
9368 garray_T *gap,
9369 typval_T *opt_msg_tv,
9370 char_u *exp_str,
9371 typval_T *exp_tv,
9372 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009373{
9374 char_u numbuf[NUMBUFLEN];
9375 char_u *tofree;
9376
9377 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9378 {
9379 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9380 vim_free(tofree);
9381 }
9382 else
9383 {
9384 ga_concat(gap, (char_u *)"Expected ");
9385 if (exp_str == NULL)
9386 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009387 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009388 vim_free(tofree);
9389 }
9390 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009391 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009392 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009393 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009394 vim_free(tofree);
9395 }
9396}
Bram Moolenaar43345542015-11-29 17:35:35 +01009397
9398/*
9399 * Add an assert error to v:errors.
9400 */
9401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009402assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009403{
9404 struct vimvar *vp = &vimvars[VV_ERRORS];
9405
9406 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9407 /* Make sure v:errors is a list. */
9408 set_vim_var_list(VV_ERRORS, list_alloc());
9409 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9410}
9411
Bram Moolenaar43345542015-11-29 17:35:35 +01009412/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009413 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009414 */
9415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009416f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009417{
9418 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009419
9420 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9421 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009422 prepare_assert_error(&ga);
9423 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9424 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009425 ga_clear(&ga);
9426 }
9427}
9428
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009429/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009430 * "assert_exception(string[, msg])" function
9431 */
9432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009433f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009434{
9435 garray_T ga;
9436 char *error;
9437
9438 error = (char *)get_tv_string_chk(&argvars[0]);
9439 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9440 {
9441 prepare_assert_error(&ga);
9442 ga_concat(&ga, (char_u *)"v:exception is not set");
9443 assert_error(&ga);
9444 ga_clear(&ga);
9445 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009446 else if (error != NULL
9447 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009448 {
9449 prepare_assert_error(&ga);
9450 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9451 &vimvars[VV_EXCEPTION].vv_tv);
9452 assert_error(&ga);
9453 ga_clear(&ga);
9454 }
9455}
9456
9457/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009458 * "assert_fails(cmd [, error])" function
9459 */
9460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009461f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009462{
9463 char_u *cmd = get_tv_string_chk(&argvars[0]);
9464 garray_T ga;
9465
9466 called_emsg = FALSE;
9467 suppress_errthrow = TRUE;
9468 emsg_silent = TRUE;
9469 do_cmdline_cmd(cmd);
9470 if (!called_emsg)
9471 {
9472 prepare_assert_error(&ga);
9473 ga_concat(&ga, (char_u *)"command did not fail: ");
9474 ga_concat(&ga, cmd);
9475 assert_error(&ga);
9476 ga_clear(&ga);
9477 }
9478 else if (argvars[1].v_type != VAR_UNKNOWN)
9479 {
9480 char_u buf[NUMBUFLEN];
9481 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9482
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009483 if (error == NULL
9484 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009485 {
9486 prepare_assert_error(&ga);
9487 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9488 &vimvars[VV_ERRMSG].vv_tv);
9489 assert_error(&ga);
9490 ga_clear(&ga);
9491 }
9492 }
9493
9494 called_emsg = FALSE;
9495 suppress_errthrow = FALSE;
9496 emsg_silent = FALSE;
9497 emsg_on_display = FALSE;
9498 set_vim_var_string(VV_ERRMSG, NULL, 0);
9499}
9500
9501/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009502 * Common for assert_true() and assert_false().
9503 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009505assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009506{
9507 int error = FALSE;
9508 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009509
Bram Moolenaar37127922016-02-06 20:29:28 +01009510 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009511 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009512 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009513 if (argvars[0].v_type != VAR_NUMBER
9514 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9515 || error)
9516 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009517 prepare_assert_error(&ga);
9518 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009519 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009520 NULL, &argvars[0]);
9521 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009522 ga_clear(&ga);
9523 }
9524}
9525
9526/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009527 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009528 */
9529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009530f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009531{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009532 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009533}
9534
9535/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009536 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009537 */
9538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009539f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009540{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009541 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009542}
9543
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009544#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009545/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009546 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009547 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009549f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009550{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009551 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009552
9553 rettv->v_type = VAR_FLOAT;
9554 if (get_float_arg(argvars, &f) == OK)
9555 rettv->vval.v_float = asin(f);
9556 else
9557 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009558}
9559
9560/*
9561 * "atan()" function
9562 */
9563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009564f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009565{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009566 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009567
9568 rettv->v_type = VAR_FLOAT;
9569 if (get_float_arg(argvars, &f) == OK)
9570 rettv->vval.v_float = atan(f);
9571 else
9572 rettv->vval.v_float = 0.0;
9573}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009574
9575/*
9576 * "atan2()" function
9577 */
9578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009579f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009580{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009581 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009582
9583 rettv->v_type = VAR_FLOAT;
9584 if (get_float_arg(argvars, &fx) == OK
9585 && get_float_arg(&argvars[1], &fy) == OK)
9586 rettv->vval.v_float = atan2(fx, fy);
9587 else
9588 rettv->vval.v_float = 0.0;
9589}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009590#endif
9591
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592/*
9593 * "browse(save, title, initdir, default)" function
9594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009596f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598#ifdef FEAT_BROWSE
9599 int save;
9600 char_u *title;
9601 char_u *initdir;
9602 char_u *defname;
9603 char_u buf[NUMBUFLEN];
9604 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009605 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009607 save = get_tv_number_chk(&argvars[0], &error);
9608 title = get_tv_string_chk(&argvars[1]);
9609 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9610 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009612 if (error || title == NULL || initdir == NULL || defname == NULL)
9613 rettv->vval.v_string = NULL;
9614 else
9615 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009616 do_browse(save ? BROWSE_SAVE : 0,
9617 title, defname, NULL, initdir, NULL, curbuf);
9618#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009620#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009622}
9623
9624/*
9625 * "browsedir(title, initdir)" function
9626 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009628f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009629{
9630#ifdef FEAT_BROWSE
9631 char_u *title;
9632 char_u *initdir;
9633 char_u buf[NUMBUFLEN];
9634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635 title = get_tv_string_chk(&argvars[0]);
9636 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 if (title == NULL || initdir == NULL)
9639 rettv->vval.v_string = NULL;
9640 else
9641 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009642 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647}
9648
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009649static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009650
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651/*
9652 * Find a buffer by number or exact name.
9653 */
9654 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009655find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009659 if (avar->v_type == VAR_NUMBER)
9660 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009661 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009663 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009664 if (buf == NULL)
9665 {
9666 /* No full path name match, try a match with a URL or a "nofile"
9667 * buffer, these don't use the full path. */
9668 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9669 if (buf->b_fname != NULL
9670 && (path_with_url(buf->b_fname)
9671#ifdef FEAT_QUICKFIX
9672 || bt_nofile(buf)
9673#endif
9674 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009675 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009676 break;
9677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 }
9679 return buf;
9680}
9681
9682/*
9683 * "bufexists(expr)" function
9684 */
9685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009686f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689}
9690
9691/*
9692 * "buflisted(expr)" function
9693 */
9694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009695f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696{
9697 buf_T *buf;
9698
9699 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
9702
9703/*
9704 * "bufloaded(expr)" function
9705 */
9706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009707f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
9709 buf_T *buf;
9710
9711 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713}
9714
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009715 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009716buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 int save_magic;
9719 char_u *save_cpo;
9720 buf_T *buf;
9721
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9723 save_magic = p_magic;
9724 p_magic = TRUE;
9725 save_cpo = p_cpo;
9726 p_cpo = (char_u *)"";
9727
9728 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009729 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730
9731 p_magic = save_magic;
9732 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009733 return buf;
9734}
9735
9736/*
9737 * Get buffer by number or pattern.
9738 */
9739 static buf_T *
9740get_buf_tv(typval_T *tv, int curtab_only)
9741{
9742 char_u *name = tv->vval.v_string;
9743 buf_T *buf;
9744
9745 if (tv->v_type == VAR_NUMBER)
9746 return buflist_findnr((int)tv->vval.v_number);
9747 if (tv->v_type != VAR_STRING)
9748 return NULL;
9749 if (name == NULL || *name == NUL)
9750 return curbuf;
9751 if (name[0] == '$' && name[1] == NUL)
9752 return lastbuf;
9753
9754 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755
9756 /* If not found, try expanding the name, like done for bufexists(). */
9757 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759
9760 return buf;
9761}
9762
9763/*
9764 * "bufname(expr)" function
9765 */
9766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009767f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768{
9769 buf_T *buf;
9770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009773 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 --emsg_off;
9780}
9781
9782/*
9783 * "bufnr(expr)" function
9784 */
9785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009786f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
9788 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009789 int error = FALSE;
9790 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009792 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009794 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009795 --emsg_off;
9796
9797 /* If the buffer isn't found and the second argument is not zero create a
9798 * new buffer. */
9799 if (buf == NULL
9800 && argvars[1].v_type != VAR_UNKNOWN
9801 && get_tv_number_chk(&argvars[1], &error) != 0
9802 && !error
9803 && (name = get_tv_string_chk(&argvars[0])) != NULL
9804 && !error)
9805 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9806
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811}
9812
9813/*
9814 * "bufwinnr(nr)" function
9815 */
9816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009817f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819#ifdef FEAT_WINDOWS
9820 win_T *wp;
9821 int winnr = 0;
9822#endif
9823 buf_T *buf;
9824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009825 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009827 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828#ifdef FEAT_WINDOWS
9829 for (wp = firstwin; wp; wp = wp->w_next)
9830 {
9831 ++winnr;
9832 if (wp->w_buffer == buf)
9833 break;
9834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009837 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838#endif
9839 --emsg_off;
9840}
9841
9842/*
9843 * "byte2line(byte)" function
9844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009846f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847{
9848#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850#else
9851 long boff = 0;
9852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009855 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 (linenr_T)0, &boff);
9859#endif
9860}
9861
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009863byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009864{
9865#ifdef FEAT_MBYTE
9866 char_u *t;
9867#endif
9868 char_u *str;
9869 long idx;
9870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009871 str = get_tv_string_chk(&argvars[0]);
9872 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009875 return;
9876
9877#ifdef FEAT_MBYTE
9878 t = str;
9879 for ( ; idx > 0; idx--)
9880 {
9881 if (*t == NUL) /* EOL reached */
9882 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009883 if (enc_utf8 && comp)
9884 t += utf_ptr2len(t);
9885 else
9886 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009887 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009888 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009889#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009890 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009892#endif
9893}
9894
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009895/*
9896 * "byteidx()" function
9897 */
9898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009899f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009900{
9901 byteidx(argvars, rettv, FALSE);
9902}
9903
9904/*
9905 * "byteidxcomp()" function
9906 */
9907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009908f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009909{
9910 byteidx(argvars, rettv, TRUE);
9911}
9912
Bram Moolenaardb913952012-06-29 12:54:53 +02009913 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009914func_call(
9915 char_u *name,
9916 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009917 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009918 dict_T *selfdict,
9919 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009920{
9921 listitem_T *item;
9922 typval_T argv[MAX_FUNC_ARGS + 1];
9923 int argc = 0;
9924 int dummy;
9925 int r = 0;
9926
9927 for (item = args->vval.v_list->lv_first; item != NULL;
9928 item = item->li_next)
9929 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009930 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009931 {
9932 EMSG(_("E699: Too many arguments"));
9933 break;
9934 }
9935 /* Make a copy of each argument. This is needed to be able to set
9936 * v_lock to VAR_FIXED in the copy without changing the original list.
9937 */
9938 copy_tv(&item->li_tv, &argv[argc++]);
9939 }
9940
9941 if (item == NULL)
9942 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9943 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009944 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009945
9946 /* Free the arguments. */
9947 while (argc > 0)
9948 clear_tv(&argv[--argc]);
9949
9950 return r;
9951}
9952
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009953/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009954 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009955 */
9956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009957f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009958{
9959 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009960 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009961 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009962
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009963 if (argvars[1].v_type != VAR_LIST)
9964 {
9965 EMSG(_(e_listreq));
9966 return;
9967 }
9968 if (argvars[1].vval.v_list == NULL)
9969 return;
9970
9971 if (argvars[0].v_type == VAR_FUNC)
9972 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009973 else if (argvars[0].v_type == VAR_PARTIAL)
9974 {
9975 partial = argvars[0].vval.v_partial;
9976 func = partial->pt_name;
9977 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009978 else
9979 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009980 if (*func == NUL)
9981 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009982
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 if (argvars[2].v_type != VAR_UNKNOWN)
9984 {
9985 if (argvars[2].v_type != VAR_DICT)
9986 {
9987 EMSG(_(e_dictreq));
9988 return;
9989 }
9990 selfdict = argvars[2].vval.v_dict;
9991 }
9992
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009993 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009994}
9995
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009996#ifdef FEAT_FLOAT
9997/*
9998 * "ceil({float})" function
9999 */
10000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010001f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010002{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010003 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010004
10005 rettv->v_type = VAR_FLOAT;
10006 if (get_float_arg(argvars, &f) == OK)
10007 rettv->vval.v_float = ceil(f);
10008 else
10009 rettv->vval.v_float = 0.0;
10010}
10011#endif
10012
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010013#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010014/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010015 * "ch_close()" function
10016 */
10017 static void
10018f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10019{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010020 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010021
10022 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010023 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010024 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010025 channel_clear(channel);
10026 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010027}
10028
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010029/*
10030 * "ch_getbufnr()" function
10031 */
10032 static void
10033f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10034{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010035 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010036
10037 rettv->vval.v_number = -1;
10038 if (channel != NULL)
10039 {
10040 char_u *what = get_tv_string(&argvars[1]);
10041 int part;
10042
10043 if (STRCMP(what, "err") == 0)
10044 part = PART_ERR;
10045 else if (STRCMP(what, "out") == 0)
10046 part = PART_OUT;
10047 else if (STRCMP(what, "in") == 0)
10048 part = PART_IN;
10049 else
10050 part = PART_SOCK;
10051 if (channel->ch_part[part].ch_buffer != NULL)
10052 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10053 }
10054}
10055
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010056/*
10057 * "ch_getjob()" function
10058 */
10059 static void
10060f_ch_getjob(typval_T *argvars, typval_T *rettv)
10061{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010062 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010063
10064 if (channel != NULL)
10065 {
10066 rettv->v_type = VAR_JOB;
10067 rettv->vval.v_job = channel->ch_job;
10068 if (channel->ch_job != NULL)
10069 ++channel->ch_job->jv_refcount;
10070 }
10071}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010072
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010073/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010074 * "ch_info()" function
10075 */
10076 static void
10077f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10078{
10079 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10080
10081 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10082 channel_info(channel, rettv->vval.v_dict);
10083}
10084
10085/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010086 * "ch_log()" function
10087 */
10088 static void
10089f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10090{
10091 char_u *msg = get_tv_string(&argvars[0]);
10092 channel_T *channel = NULL;
10093
10094 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010095 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010096
10097 ch_log(channel, (char *)msg);
10098}
10099
10100/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010101 * "ch_logfile()" function
10102 */
10103 static void
10104f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10105{
10106 char_u *fname;
10107 char_u *opt = (char_u *)"";
10108 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010109
10110 fname = get_tv_string(&argvars[0]);
10111 if (argvars[1].v_type == VAR_STRING)
10112 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010113 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010114}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010115
10116/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010117 * "ch_open()" function
10118 */
10119 static void
10120f_ch_open(typval_T *argvars, typval_T *rettv)
10121{
Bram Moolenaar77073442016-02-13 23:23:53 +010010122 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010123 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010124}
10125
10126/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010127 * "ch_read()" function
10128 */
10129 static void
10130f_ch_read(typval_T *argvars, typval_T *rettv)
10131{
10132 common_channel_read(argvars, rettv, FALSE);
10133}
10134
10135/*
10136 * "ch_readraw()" function
10137 */
10138 static void
10139f_ch_readraw(typval_T *argvars, typval_T *rettv)
10140{
10141 common_channel_read(argvars, rettv, TRUE);
10142}
10143
10144/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010145 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010146 */
10147 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010148f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10149{
10150 ch_expr_common(argvars, rettv, TRUE);
10151}
10152
10153/*
10154 * "ch_sendexpr()" function
10155 */
10156 static void
10157f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10158{
10159 ch_expr_common(argvars, rettv, FALSE);
10160}
10161
10162/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010163 * "ch_evalraw()" function
10164 */
10165 static void
10166f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10167{
10168 ch_raw_common(argvars, rettv, TRUE);
10169}
10170
10171/*
10172 * "ch_sendraw()" function
10173 */
10174 static void
10175f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10176{
10177 ch_raw_common(argvars, rettv, FALSE);
10178}
10179
10180/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010181 * "ch_setoptions()" function
10182 */
10183 static void
10184f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10185{
10186 channel_T *channel;
10187 jobopt_T opt;
10188
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010189 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010190 if (channel == NULL)
10191 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010192 clear_job_options(&opt);
10193 if (get_job_options(&argvars[1], &opt,
10194 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010195 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010196 channel_set_options(channel, &opt);
10197}
10198
10199/*
10200 * "ch_status()" function
10201 */
10202 static void
10203f_ch_status(typval_T *argvars, typval_T *rettv)
10204{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010205 channel_T *channel;
10206
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010207 /* return an empty string by default */
10208 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010209 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010210
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010211 channel = get_channel_arg(&argvars[0], FALSE);
10212 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010213}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010214#endif
10215
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010216/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010217 * "changenr()" function
10218 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010220f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010221{
10222 rettv->vval.v_number = curbuf->b_u_seq_cur;
10223}
10224
10225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 * "char2nr(string)" function
10227 */
10228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010229f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230{
10231#ifdef FEAT_MBYTE
10232 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010233 {
10234 int utf8 = 0;
10235
10236 if (argvars[1].v_type != VAR_UNKNOWN)
10237 utf8 = get_tv_number_chk(&argvars[1], NULL);
10238
10239 if (utf8)
10240 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10241 else
10242 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 else
10245#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010246 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247}
10248
10249/*
10250 * "cindent(lnum)" function
10251 */
10252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010253f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254{
10255#ifdef FEAT_CINDENT
10256 pos_T pos;
10257 linenr_T lnum;
10258
10259 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10262 {
10263 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 curwin->w_cursor = pos;
10266 }
10267 else
10268#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010269 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270}
10271
10272/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010273 * "clearmatches()" function
10274 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010276f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010277{
10278#ifdef FEAT_SEARCH_EXTRA
10279 clear_matches(curwin);
10280#endif
10281}
10282
10283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 * "col(string)" function
10285 */
10286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010287f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288{
10289 colnr_T col = 0;
10290 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010291 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010293 fp = var2fpos(&argvars[0], FALSE, &fnum);
10294 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 {
10296 if (fp->col == MAXCOL)
10297 {
10298 /* '> can be MAXCOL, get the length of the line then */
10299 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010300 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 else
10302 col = MAXCOL;
10303 }
10304 else
10305 {
10306 col = fp->col + 1;
10307#ifdef FEAT_VIRTUALEDIT
10308 /* col(".") when the cursor is on the NUL at the end of the line
10309 * because of "coladd" can be seen as an extra column. */
10310 if (virtual_active() && fp == &curwin->w_cursor)
10311 {
10312 char_u *p = ml_get_cursor();
10313
10314 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10315 curwin->w_virtcol - curwin->w_cursor.coladd))
10316 {
10317# ifdef FEAT_MBYTE
10318 int l;
10319
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010320 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 col += l;
10322# else
10323 if (*p != NUL && p[1] == NUL)
10324 ++col;
10325# endif
10326 }
10327 }
10328#endif
10329 }
10330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332}
10333
Bram Moolenaar572cb562005-08-05 21:35:02 +000010334#if defined(FEAT_INS_EXPAND)
10335/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010336 * "complete()" function
10337 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010339f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010340{
10341 int startcol;
10342
10343 if ((State & INSERT) == 0)
10344 {
10345 EMSG(_("E785: complete() can only be used in Insert mode"));
10346 return;
10347 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010348
10349 /* Check for undo allowed here, because if something was already inserted
10350 * the line was already saved for undo and this check isn't done. */
10351 if (!undo_allowed())
10352 return;
10353
Bram Moolenaarade00832006-03-10 21:46:58 +000010354 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10355 {
10356 EMSG(_(e_invarg));
10357 return;
10358 }
10359
10360 startcol = get_tv_number_chk(&argvars[0], NULL);
10361 if (startcol <= 0)
10362 return;
10363
10364 set_completion(startcol - 1, argvars[1].vval.v_list);
10365}
10366
10367/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010368 * "complete_add()" function
10369 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010371f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010372{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010373 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010374}
10375
10376/*
10377 * "complete_check()" function
10378 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010380f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010381{
10382 int saved = RedrawingDisabled;
10383
10384 RedrawingDisabled = 0;
10385 ins_compl_check_keys(0);
10386 rettv->vval.v_number = compl_interrupted;
10387 RedrawingDisabled = saved;
10388}
10389#endif
10390
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391/*
10392 * "confirm(message, buttons[, default [, type]])" function
10393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010395f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396{
10397#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10398 char_u *message;
10399 char_u *buttons = NULL;
10400 char_u buf[NUMBUFLEN];
10401 char_u buf2[NUMBUFLEN];
10402 int def = 1;
10403 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010404 char_u *typestr;
10405 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 message = get_tv_string_chk(&argvars[0]);
10408 if (message == NULL)
10409 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010410 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010412 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10413 if (buttons == NULL)
10414 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010415 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010418 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10421 if (typestr == NULL)
10422 error = TRUE;
10423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010425 switch (TOUPPER_ASC(*typestr))
10426 {
10427 case 'E': type = VIM_ERROR; break;
10428 case 'Q': type = VIM_QUESTION; break;
10429 case 'I': type = VIM_INFO; break;
10430 case 'W': type = VIM_WARNING; break;
10431 case 'G': type = VIM_GENERIC; break;
10432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 }
10434 }
10435 }
10436 }
10437
10438 if (buttons == NULL || *buttons == NUL)
10439 buttons = (char_u *)_("&Ok");
10440
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010441 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010443 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444#endif
10445}
10446
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010447/*
10448 * "copy()" function
10449 */
10450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010451f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010452{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010453 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010454}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010456#ifdef FEAT_FLOAT
10457/*
10458 * "cos()" function
10459 */
10460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010461f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010462{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010463 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010464
10465 rettv->v_type = VAR_FLOAT;
10466 if (get_float_arg(argvars, &f) == OK)
10467 rettv->vval.v_float = cos(f);
10468 else
10469 rettv->vval.v_float = 0.0;
10470}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010471
10472/*
10473 * "cosh()" function
10474 */
10475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010476f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010477{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010478 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010479
10480 rettv->v_type = VAR_FLOAT;
10481 if (get_float_arg(argvars, &f) == OK)
10482 rettv->vval.v_float = cosh(f);
10483 else
10484 rettv->vval.v_float = 0.0;
10485}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010486#endif
10487
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010489 * "count()" function
10490 */
10491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010492f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010493{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010494 long n = 0;
10495 int ic = FALSE;
10496
Bram Moolenaare9a41262005-01-15 22:18:47 +000010497 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010498 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010499 listitem_T *li;
10500 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010501 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010502
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 if ((l = argvars[0].vval.v_list) != NULL)
10504 {
10505 li = l->lv_first;
10506 if (argvars[2].v_type != VAR_UNKNOWN)
10507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 int error = FALSE;
10509
10510 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511 if (argvars[3].v_type != VAR_UNKNOWN)
10512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 idx = get_tv_number_chk(&argvars[3], &error);
10514 if (!error)
10515 {
10516 li = list_find(l, idx);
10517 if (li == NULL)
10518 EMSGN(_(e_listidx), idx);
10519 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 if (error)
10522 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010523 }
10524
10525 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010526 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010527 ++n;
10528 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010529 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010530 else if (argvars[0].v_type == VAR_DICT)
10531 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 int todo;
10533 dict_T *d;
10534 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010535
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010536 if ((d = argvars[0].vval.v_dict) != NULL)
10537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 int error = FALSE;
10539
Bram Moolenaare9a41262005-01-15 22:18:47 +000010540 if (argvars[2].v_type != VAR_UNKNOWN)
10541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010543 if (argvars[3].v_type != VAR_UNKNOWN)
10544 EMSG(_(e_invarg));
10545 }
10546
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010547 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010548 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010549 {
10550 if (!HASHITEM_EMPTY(hi))
10551 {
10552 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010553 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010554 ++n;
10555 }
10556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 }
10558 }
10559 else
10560 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010561 rettv->vval.v_number = n;
10562}
10563
10564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10566 *
10567 * Checks the existence of a cscope connection.
10568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010570f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571{
10572#ifdef FEAT_CSCOPE
10573 int num = 0;
10574 char_u *dbpath = NULL;
10575 char_u *prepend = NULL;
10576 char_u buf[NUMBUFLEN];
10577
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010578 if (argvars[0].v_type != VAR_UNKNOWN
10579 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010581 num = (int)get_tv_number(&argvars[0]);
10582 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 }
10586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588#endif
10589}
10590
10591/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010592 * "cursor(lnum, col)" function, or
10593 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010595 * Moves the cursor to the specified line and column.
10596 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010599f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600{
10601 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010602#ifdef FEAT_VIRTUALEDIT
10603 long coladd = 0;
10604#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010605 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010607 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010608 if (argvars[1].v_type == VAR_UNKNOWN)
10609 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010610 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010611 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010612
Bram Moolenaar493c1782014-05-28 14:34:46 +020010613 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010614 {
10615 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010616 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010617 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010618 line = pos.lnum;
10619 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010620#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010621 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010622#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010623 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010624 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010625 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010626 set_curswant = FALSE;
10627 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010628 }
10629 else
10630 {
10631 line = get_tv_lnum(argvars);
10632 col = get_tv_number_chk(&argvars[1], NULL);
10633#ifdef FEAT_VIRTUALEDIT
10634 if (argvars[2].v_type != VAR_UNKNOWN)
10635 coladd = get_tv_number_chk(&argvars[2], NULL);
10636#endif
10637 }
10638 if (line < 0 || col < 0
10639#ifdef FEAT_VIRTUALEDIT
10640 || coladd < 0
10641#endif
10642 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 if (line > 0)
10645 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 if (col > 0)
10647 curwin->w_cursor.col = col - 1;
10648#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010649 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650#endif
10651
10652 /* Make sure the cursor is in a valid position. */
10653 check_cursor();
10654#ifdef FEAT_MBYTE
10655 /* Correct cursor for multi-byte character. */
10656 if (has_mbyte)
10657 mb_adjust_cursor();
10658#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010659
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010660 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010661 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662}
10663
10664/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010665 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 */
10667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010668f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010670 int noref = 0;
10671
10672 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010673 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010674 if (noref < 0 || noref > 1)
10675 EMSG(_(e_invarg));
10676 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010677 {
10678 current_copyID += COPYID_INC;
10679 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681}
10682
10683/*
10684 * "delete()" function
10685 */
10686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010687f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010689 char_u nbuf[NUMBUFLEN];
10690 char_u *name;
10691 char_u *flags;
10692
10693 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010695 return;
10696
10697 name = get_tv_string(&argvars[0]);
10698 if (name == NULL || *name == NUL)
10699 {
10700 EMSG(_(e_invarg));
10701 return;
10702 }
10703
10704 if (argvars[1].v_type != VAR_UNKNOWN)
10705 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010707 flags = (char_u *)"";
10708
10709 if (*flags == NUL)
10710 /* delete a file */
10711 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10712 else if (STRCMP(flags, "d") == 0)
10713 /* delete an empty directory */
10714 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10715 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010716 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010717 rettv->vval.v_number = delete_recursive(name);
10718 else
10719 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720}
10721
10722/*
10723 * "did_filetype()" function
10724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010726f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727{
10728#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730#endif
10731}
10732
10733/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010734 * "diff_filler()" function
10735 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010737f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010738{
10739#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010740 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010741#endif
10742}
10743
10744/*
10745 * "diff_hlID()" function
10746 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010748f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010749{
10750#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010752 static linenr_T prev_lnum = 0;
10753 static int changedtick = 0;
10754 static int fnum = 0;
10755 static int change_start = 0;
10756 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010757 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010758 int filler_lines;
10759 int col;
10760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 if (lnum < 0) /* ignore type error in {lnum} arg */
10762 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010763 if (lnum != prev_lnum
10764 || changedtick != curbuf->b_changedtick
10765 || fnum != curbuf->b_fnum)
10766 {
10767 /* New line, buffer, change: need to get the values. */
10768 filler_lines = diff_check(curwin, lnum);
10769 if (filler_lines < 0)
10770 {
10771 if (filler_lines == -1)
10772 {
10773 change_start = MAXCOL;
10774 change_end = -1;
10775 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10776 hlID = HLF_ADD; /* added line */
10777 else
10778 hlID = HLF_CHD; /* changed line */
10779 }
10780 else
10781 hlID = HLF_ADD; /* added line */
10782 }
10783 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010784 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010785 prev_lnum = lnum;
10786 changedtick = curbuf->b_changedtick;
10787 fnum = curbuf->b_fnum;
10788 }
10789
10790 if (hlID == HLF_CHD || hlID == HLF_TXD)
10791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010792 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010793 if (col >= change_start && col <= change_end)
10794 hlID = HLF_TXD; /* changed text */
10795 else
10796 hlID = HLF_CHD; /* changed line */
10797 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010798 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010799#endif
10800}
10801
10802/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010803 * "disable_char_avail_for_testing({expr})" function
10804 */
10805 static void
10806f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10807{
10808 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10809}
10810
10811/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010812 * "empty({expr})" function
10813 */
10814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010815f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010816{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010817 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010818
10819 switch (argvars[0].v_type)
10820 {
10821 case VAR_STRING:
10822 case VAR_FUNC:
10823 n = argvars[0].vval.v_string == NULL
10824 || *argvars[0].vval.v_string == NUL;
10825 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010826 case VAR_PARTIAL:
10827 n = FALSE;
10828 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010829 case VAR_NUMBER:
10830 n = argvars[0].vval.v_number == 0;
10831 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010832 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010833#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010834 n = argvars[0].vval.v_float == 0.0;
10835 break;
10836#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010837 case VAR_LIST:
10838 n = argvars[0].vval.v_list == NULL
10839 || argvars[0].vval.v_list->lv_first == NULL;
10840 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010841 case VAR_DICT:
10842 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010843 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010844 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010845 case VAR_SPECIAL:
10846 n = argvars[0].vval.v_number != VVAL_TRUE;
10847 break;
10848
Bram Moolenaar835dc632016-02-07 14:27:38 +010010849 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010850#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010851 n = argvars[0].vval.v_job == NULL
10852 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10853 break;
10854#endif
10855 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010856#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010857 n = argvars[0].vval.v_channel == NULL
10858 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010859 break;
10860#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010861 case VAR_UNKNOWN:
10862 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10863 n = TRUE;
10864 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010865 }
10866
10867 rettv->vval.v_number = n;
10868}
10869
10870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 * "escape({string}, {chars})" function
10872 */
10873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010874f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875{
10876 char_u buf[NUMBUFLEN];
10877
Bram Moolenaar758711c2005-02-02 23:11:38 +000010878 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10879 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881}
10882
10883/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010884 * "eval()" function
10885 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010887f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010888{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010889 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 s = get_tv_string_chk(&argvars[0]);
10892 if (s != NULL)
10893 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010894
Bram Moolenaar615b9972015-01-14 17:15:05 +010010895 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10897 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010898 if (p != NULL && !aborting())
10899 EMSG2(_(e_invexpr2), p);
10900 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010902 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010904 else if (*s != NUL)
10905 EMSG(_(e_trailing));
10906}
10907
10908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 * "eventhandler()" function
10910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010912f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915}
10916
10917/*
10918 * "executable()" function
10919 */
10920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010921f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010923 char_u *name = get_tv_string(&argvars[0]);
10924
10925 /* Check in $PATH and also check directly if there is a directory name. */
10926 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10927 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010928}
10929
10930/*
10931 * "exepath()" function
10932 */
10933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010934f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010935{
10936 char_u *p = NULL;
10937
Bram Moolenaarb5971142015-03-21 17:32:19 +010010938 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010939 rettv->v_type = VAR_STRING;
10940 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941}
10942
10943/*
10944 * "exists()" function
10945 */
10946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010947f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948{
10949 char_u *p;
10950 char_u *name;
10951 int n = FALSE;
10952 int len = 0;
10953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 if (*p == '$') /* environment variable */
10956 {
10957 /* first try "normal" environment variables (fast) */
10958 if (mch_getenv(p + 1) != NULL)
10959 n = TRUE;
10960 else
10961 {
10962 /* try expanding things like $VIM and ${HOME} */
10963 p = expand_env_save(p);
10964 if (p != NULL && *p != '$')
10965 n = TRUE;
10966 vim_free(p);
10967 }
10968 }
10969 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010972 if (*skipwhite(p) != NUL)
10973 n = FALSE; /* trailing garbage */
10974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 else if (*p == '*') /* internal or user defined function */
10976 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010977 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 }
10979 else if (*p == ':')
10980 {
10981 n = cmd_exists(p + 1);
10982 }
10983 else if (*p == '#')
10984 {
10985#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010986 if (p[1] == '#')
10987 n = autocmd_supported(p + 2);
10988 else
10989 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990#endif
10991 }
10992 else /* internal variable */
10993 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010994 char_u *tofree;
10995 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010997 /* get_name_len() takes care of expanding curly braces */
10998 name = p;
10999 len = get_name_len(&p, &tofree, TRUE, FALSE);
11000 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011002 if (tofree != NULL)
11003 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011004 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011005 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011007 /* handle d.key, l[idx], f(expr) */
11008 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11009 if (n)
11010 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 }
11012 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011013 if (*p != NUL)
11014 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011016 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 }
11018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020}
11021
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011022#ifdef FEAT_FLOAT
11023/*
11024 * "exp()" function
11025 */
11026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011027f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011028{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011029 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011030
11031 rettv->v_type = VAR_FLOAT;
11032 if (get_float_arg(argvars, &f) == OK)
11033 rettv->vval.v_float = exp(f);
11034 else
11035 rettv->vval.v_float = 0.0;
11036}
11037#endif
11038
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039/*
11040 * "expand()" function
11041 */
11042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011043f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044{
11045 char_u *s;
11046 int len;
11047 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011048 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011051 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011054 if (argvars[1].v_type != VAR_UNKNOWN
11055 && argvars[2].v_type != VAR_UNKNOWN
11056 && get_tv_number_chk(&argvars[2], &error)
11057 && !error)
11058 {
11059 rettv->v_type = VAR_LIST;
11060 rettv->vval.v_list = NULL;
11061 }
11062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 if (*s == '%' || *s == '#' || *s == '<')
11065 {
11066 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011067 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011069 if (rettv->v_type == VAR_LIST)
11070 {
11071 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11072 list_append_string(rettv->vval.v_list, result, -1);
11073 else
11074 vim_free(result);
11075 }
11076 else
11077 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 }
11079 else
11080 {
11081 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011082 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 if (argvars[1].v_type != VAR_UNKNOWN
11084 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011085 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011086 if (!error)
11087 {
11088 ExpandInit(&xpc);
11089 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011090 if (p_wic)
11091 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011092 if (rettv->v_type == VAR_STRING)
11093 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11094 options, WILD_ALL);
11095 else if (rettv_list_alloc(rettv) != FAIL)
11096 {
11097 int i;
11098
11099 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11100 for (i = 0; i < xpc.xp_numfiles; i++)
11101 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11102 ExpandCleanup(&xpc);
11103 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011104 }
11105 else
11106 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 }
11108}
11109
11110/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011111 * Go over all entries in "d2" and add them to "d1".
11112 * When "action" is "error" then a duplicate key is an error.
11113 * When "action" is "force" then a duplicate key is overwritten.
11114 * Otherwise duplicate keys are ignored ("action" is "keep").
11115 */
11116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011117dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011118{
11119 dictitem_T *di1;
11120 hashitem_T *hi2;
11121 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011122 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011123
11124 todo = (int)d2->dv_hashtab.ht_used;
11125 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11126 {
11127 if (!HASHITEM_EMPTY(hi2))
11128 {
11129 --todo;
11130 di1 = dict_find(d1, hi2->hi_key, -1);
11131 if (d1->dv_scope != 0)
11132 {
11133 /* Disallow replacing a builtin function in l: and g:.
11134 * Check the key to be valid when adding to any
11135 * scope. */
11136 if (d1->dv_scope == VAR_DEF_SCOPE
11137 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11138 && var_check_func_name(hi2->hi_key,
11139 di1 == NULL))
11140 break;
11141 if (!valid_varname(hi2->hi_key))
11142 break;
11143 }
11144 if (di1 == NULL)
11145 {
11146 di1 = dictitem_copy(HI2DI(hi2));
11147 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11148 dictitem_free(di1);
11149 }
11150 else if (*action == 'e')
11151 {
11152 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11153 break;
11154 }
11155 else if (*action == 'f' && HI2DI(hi2) != di1)
11156 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011157 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11158 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011159 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011160 clear_tv(&di1->di_tv);
11161 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11162 }
11163 }
11164 }
11165}
11166
11167/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011168 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011169 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011170 */
11171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011172f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011173{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011174 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011175
Bram Moolenaare9a41262005-01-15 22:18:47 +000011176 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011177 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011178 list_T *l1, *l2;
11179 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011180 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011181 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011182
Bram Moolenaare9a41262005-01-15 22:18:47 +000011183 l1 = argvars[0].vval.v_list;
11184 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011185 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011186 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011187 {
11188 if (argvars[2].v_type != VAR_UNKNOWN)
11189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 before = get_tv_number_chk(&argvars[2], &error);
11191 if (error)
11192 return; /* type error; errmsg already given */
11193
Bram Moolenaar758711c2005-02-02 23:11:38 +000011194 if (before == l1->lv_len)
11195 item = NULL;
11196 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011197 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011198 item = list_find(l1, before);
11199 if (item == NULL)
11200 {
11201 EMSGN(_(e_listidx), before);
11202 return;
11203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011204 }
11205 }
11206 else
11207 item = NULL;
11208 list_extend(l1, l2, item);
11209
Bram Moolenaare9a41262005-01-15 22:18:47 +000011210 copy_tv(&argvars[0], rettv);
11211 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011212 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011213 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11214 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011215 dict_T *d1, *d2;
11216 char_u *action;
11217 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011218
11219 d1 = argvars[0].vval.v_dict;
11220 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011221 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011222 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011223 {
11224 /* Check the third argument. */
11225 if (argvars[2].v_type != VAR_UNKNOWN)
11226 {
11227 static char *(av[]) = {"keep", "force", "error"};
11228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229 action = get_tv_string_chk(&argvars[2]);
11230 if (action == NULL)
11231 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011232 for (i = 0; i < 3; ++i)
11233 if (STRCMP(action, av[i]) == 0)
11234 break;
11235 if (i == 3)
11236 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011237 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011238 return;
11239 }
11240 }
11241 else
11242 action = (char_u *)"force";
11243
Bram Moolenaara9922d62013-05-30 13:01:18 +020011244 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011245
Bram Moolenaare9a41262005-01-15 22:18:47 +000011246 copy_tv(&argvars[0], rettv);
11247 }
11248 }
11249 else
11250 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011251}
11252
11253/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011254 * "feedkeys()" function
11255 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011257f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011258{
11259 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011260 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011261 char_u *keys, *flags;
11262 char_u nbuf[NUMBUFLEN];
11263 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011264 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011265 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011266
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011267 /* This is not allowed in the sandbox. If the commands would still be
11268 * executed in the sandbox it would be OK, but it probably happens later,
11269 * when "sandbox" is no longer set. */
11270 if (check_secure())
11271 return;
11272
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011273 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011274
11275 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011276 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011277 flags = get_tv_string_buf(&argvars[1], nbuf);
11278 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011279 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011280 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011281 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011282 case 'n': remap = FALSE; break;
11283 case 'm': remap = TRUE; break;
11284 case 't': typed = TRUE; break;
11285 case 'i': insert = TRUE; break;
11286 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011287 }
11288 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011289 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011290
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011291 if (*keys != NUL || execute)
11292 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011293 /* Need to escape K_SPECIAL and CSI before putting the string in the
11294 * typeahead buffer. */
11295 keys_esc = vim_strsave_escape_csi(keys);
11296 if (keys_esc != NULL)
11297 {
11298 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011299 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011300 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011301 if (vgetc_busy)
11302 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011303 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011304 {
11305 int save_msg_scroll = msg_scroll;
11306
11307 /* Avoid a 1 second delay when the keys start Insert mode. */
11308 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011309 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011310 msg_scroll |= save_msg_scroll;
11311 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011312 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011313 }
11314}
11315
11316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 * "filereadable()" function
11318 */
11319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011320f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011322 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 char_u *p;
11324 int n;
11325
Bram Moolenaarc236c162008-07-13 17:41:49 +000011326#ifndef O_NONBLOCK
11327# define O_NONBLOCK 0
11328#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011330 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11331 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 {
11333 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011334 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 }
11336 else
11337 n = FALSE;
11338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340}
11341
11342/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011343 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 * rights to write into.
11345 */
11346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011347f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011349 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350}
11351
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011352 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011353findfilendir(
11354 typval_T *argvars UNUSED,
11355 typval_T *rettv,
11356 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011357{
11358#ifdef FEAT_SEARCHPATH
11359 char_u *fname;
11360 char_u *fresult = NULL;
11361 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11362 char_u *p;
11363 char_u pathbuf[NUMBUFLEN];
11364 int count = 1;
11365 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011366 int error = FALSE;
11367#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011368
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011369 rettv->vval.v_string = NULL;
11370 rettv->v_type = VAR_STRING;
11371
11372#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011375 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11378 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011379 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 else
11381 {
11382 if (*p != NUL)
11383 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011385 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011386 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011387 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011388 }
11389
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011390 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11391 error = TRUE;
11392
11393 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 do
11396 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011397 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011398 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011399 fresult = find_file_in_path_option(first ? fname : NULL,
11400 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011401 0, first, path,
11402 find_what,
11403 curbuf->b_ffname,
11404 find_what == FINDFILE_DIR
11405 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011406 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011407
11408 if (fresult != NULL && rettv->v_type == VAR_LIST)
11409 list_append_string(rettv->vval.v_list, fresult, -1);
11410
11411 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011413
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011414 if (rettv->v_type == VAR_STRING)
11415 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011416#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011417}
11418
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011419static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11420static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011421
11422/*
11423 * Implementation of map() and filter().
11424 */
11425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011426filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011427{
11428 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011429 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011430 listitem_T *li, *nli;
11431 list_T *l = NULL;
11432 dictitem_T *di;
11433 hashtab_T *ht;
11434 hashitem_T *hi;
11435 dict_T *d = NULL;
11436 typval_T save_val;
11437 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011438 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011439 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011440 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011441 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011442 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011443 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011444 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011445
Bram Moolenaare9a41262005-01-15 22:18:47 +000011446 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011447 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011448 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011449 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011450 return;
11451 }
11452 else if (argvars[0].v_type == VAR_DICT)
11453 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011454 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011455 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011456 return;
11457 }
11458 else
11459 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011460 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011461 return;
11462 }
11463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 expr = get_tv_string_buf_chk(&argvars[1], buf);
11465 /* On type errors, the preceding call has already displayed an error
11466 * message. Avoid a misleading error message for an empty string that
11467 * was not passed as argument. */
11468 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011470 prepare_vimvar(VV_VAL, &save_val);
11471 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011472
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011473 /* We reset "did_emsg" to be able to detect whether an error
11474 * occurred during evaluation of the expression. */
11475 save_did_emsg = did_emsg;
11476 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011477
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011478 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011479 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011481 vimvars[VV_KEY].vv_type = VAR_STRING;
11482
11483 ht = &d->dv_hashtab;
11484 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011485 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011486 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011488 if (!HASHITEM_EMPTY(hi))
11489 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011490 int r;
11491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011492 --todo;
11493 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011494 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011495 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11496 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011497 break;
11498 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011499 r = filter_map_one(&di->di_tv, expr, map, &rem);
11500 clear_tv(&vimvars[VV_KEY].vv_tv);
11501 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011502 break;
11503 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011504 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011505 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11506 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011507 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011508 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011509 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011510 }
11511 }
11512 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011513 }
11514 else
11515 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011516 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011518 for (li = l->lv_first; li != NULL; li = nli)
11519 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011520 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011521 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011522 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011523 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011524 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011525 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011526 break;
11527 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011529 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011530 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011531 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011532
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011533 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011534 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011535
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011536 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011538
11539 copy_tv(&argvars[0], rettv);
11540}
11541
11542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011543filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011544{
Bram Moolenaar33570922005-01-25 22:26:29 +000011545 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011546 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011547 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011550 s = expr;
11551 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011552 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011553 if (*s != NUL) /* check for trailing chars after expr */
11554 {
11555 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011556 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011557 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011558 }
11559 if (map)
11560 {
11561 /* map(): replace the list item value */
11562 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011563 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011564 *tv = rettv;
11565 }
11566 else
11567 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 int error = FALSE;
11569
Bram Moolenaare9a41262005-01-15 22:18:47 +000011570 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011571 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011572 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 /* On type error, nothing has been removed; return FAIL to stop the
11574 * loop. The error message was given by get_tv_number_chk(). */
11575 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011576 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011577 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011578 retval = OK;
11579theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011580 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011581 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011582}
11583
11584/*
11585 * "filter()" function
11586 */
11587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011588f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011589{
11590 filter_map(argvars, rettv, FALSE);
11591}
11592
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011593/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011594 * "finddir({fname}[, {path}[, {count}]])" function
11595 */
11596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011597f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011598{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011599 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011600}
11601
11602/*
11603 * "findfile({fname}[, {path}[, {count}]])" function
11604 */
11605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011606f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011607{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011608 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011609}
11610
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011611#ifdef FEAT_FLOAT
11612/*
11613 * "float2nr({float})" function
11614 */
11615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011616f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011617{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011618 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011619
11620 if (get_float_arg(argvars, &f) == OK)
11621 {
11622 if (f < -0x7fffffff)
11623 rettv->vval.v_number = -0x7fffffff;
11624 else if (f > 0x7fffffff)
11625 rettv->vval.v_number = 0x7fffffff;
11626 else
11627 rettv->vval.v_number = (varnumber_T)f;
11628 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011629}
11630
11631/*
11632 * "floor({float})" function
11633 */
11634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011635f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011636{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011637 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011638
11639 rettv->v_type = VAR_FLOAT;
11640 if (get_float_arg(argvars, &f) == OK)
11641 rettv->vval.v_float = floor(f);
11642 else
11643 rettv->vval.v_float = 0.0;
11644}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011645
11646/*
11647 * "fmod()" function
11648 */
11649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011650f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011651{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011652 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011653
11654 rettv->v_type = VAR_FLOAT;
11655 if (get_float_arg(argvars, &fx) == OK
11656 && get_float_arg(&argvars[1], &fy) == OK)
11657 rettv->vval.v_float = fmod(fx, fy);
11658 else
11659 rettv->vval.v_float = 0.0;
11660}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011661#endif
11662
Bram Moolenaar0d660222005-01-07 21:51:51 +000011663/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011664 * "fnameescape({string})" function
11665 */
11666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011667f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011668{
11669 rettv->vval.v_string = vim_strsave_fnameescape(
11670 get_tv_string(&argvars[0]), FALSE);
11671 rettv->v_type = VAR_STRING;
11672}
11673
11674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 * "fnamemodify({fname}, {mods})" function
11676 */
11677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011678f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679{
11680 char_u *fname;
11681 char_u *mods;
11682 int usedlen = 0;
11683 int len;
11684 char_u *fbuf = NULL;
11685 char_u buf[NUMBUFLEN];
11686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 fname = get_tv_string_chk(&argvars[0]);
11688 mods = get_tv_string_buf_chk(&argvars[1], buf);
11689 if (fname == NULL || mods == NULL)
11690 fname = NULL;
11691 else
11692 {
11693 len = (int)STRLEN(fname);
11694 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011697 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 vim_free(fbuf);
11703}
11704
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011705static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706
11707/*
11708 * "foldclosed()" function
11709 */
11710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011711foldclosed_both(
11712 typval_T *argvars UNUSED,
11713 typval_T *rettv,
11714 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715{
11716#ifdef FEAT_FOLDING
11717 linenr_T lnum;
11718 linenr_T first, last;
11719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11722 {
11723 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11724 {
11725 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011728 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 return;
11730 }
11731 }
11732#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734}
11735
11736/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011737 * "foldclosed()" function
11738 */
11739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011740f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741{
11742 foldclosed_both(argvars, rettv, FALSE);
11743}
11744
11745/*
11746 * "foldclosedend()" function
11747 */
11748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011749f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011750{
11751 foldclosed_both(argvars, rettv, TRUE);
11752}
11753
11754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 * "foldlevel()" function
11756 */
11757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011758f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759{
11760#ifdef FEAT_FOLDING
11761 linenr_T lnum;
11762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767}
11768
11769/*
11770 * "foldtext()" function
11771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011773f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774{
11775#ifdef FEAT_FOLDING
11776 linenr_T lnum;
11777 char_u *s;
11778 char_u *r;
11779 int len;
11780 char *txt;
11781#endif
11782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783 rettv->v_type = VAR_STRING;
11784 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011786 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11787 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11788 <= curbuf->b_ml.ml_line_count
11789 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 {
11791 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011792 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11793 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 {
11795 if (!linewhite(lnum))
11796 break;
11797 ++lnum;
11798 }
11799
11800 /* Find interesting text in this line. */
11801 s = skipwhite(ml_get(lnum));
11802 /* skip C comment-start */
11803 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011806 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011807 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011808 {
11809 s = skipwhite(ml_get(lnum + 1));
11810 if (*s == '*')
11811 s = skipwhite(s + 1);
11812 }
11813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 txt = _("+-%s%3ld lines: ");
11815 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011816 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 + 20 /* for %3ld */
11818 + STRLEN(s))); /* concatenated */
11819 if (r != NULL)
11820 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011821 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11822 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11823 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 len = (int)STRLEN(r);
11825 STRCAT(r, s);
11826 /* remove 'foldmarker' and 'commentstring' */
11827 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 }
11830 }
11831#endif
11832}
11833
11834/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011835 * "foldtextresult(lnum)" function
11836 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011838f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011839{
11840#ifdef FEAT_FOLDING
11841 linenr_T lnum;
11842 char_u *text;
11843 char_u buf[51];
11844 foldinfo_T foldinfo;
11845 int fold_count;
11846#endif
11847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 rettv->v_type = VAR_STRING;
11849 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011850#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011851 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 /* treat illegal types and illegal string values for {lnum} the same */
11853 if (lnum < 0)
11854 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011855 fold_count = foldedCount(curwin, lnum, &foldinfo);
11856 if (fold_count > 0)
11857 {
11858 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11859 &foldinfo, buf);
11860 if (text == buf)
11861 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011863 }
11864#endif
11865}
11866
11867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 * "foreground()" function
11869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011871f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873#ifdef FEAT_GUI
11874 if (gui.in_use)
11875 gui_mch_set_foreground();
11876#else
11877# ifdef WIN32
11878 win32_set_foreground();
11879# endif
11880#endif
11881}
11882
11883/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011884 * "function()" function
11885 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011887f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011888{
11889 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011890 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011891 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011892 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011893
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011894 if (argvars[0].v_type == VAR_FUNC)
11895 {
11896 /* function(MyFunc, [arg], dict) */
11897 s = argvars[0].vval.v_string;
11898 }
11899 else if (argvars[0].v_type == VAR_PARTIAL
11900 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011901 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011902 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011903 arg_pt = argvars[0].vval.v_partial;
11904 s = arg_pt->pt_name;
11905 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011906 else
11907 {
11908 /* function('MyFunc', [arg], dict) */
11909 s = get_tv_string(&argvars[0]);
11910 use_string = TRUE;
11911 }
11912
11913 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011914 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011915 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011916 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11917 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011918 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011919 else
11920 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011921 int dict_idx = 0;
11922 int arg_idx = 0;
11923 list_T *list = NULL;
11924
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011925 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011926 {
11927 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011928 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011929
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011930 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11931 * also be called from another script. Using trans_function_name()
11932 * would also work, but some plugins depend on the name being
11933 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011934 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011935 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11936 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011937 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011938 STRCPY(name, sid_buf);
11939 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011940 }
11941 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011942 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011943 name = vim_strsave(s);
11944
11945 if (argvars[1].v_type != VAR_UNKNOWN)
11946 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011947 if (argvars[2].v_type != VAR_UNKNOWN)
11948 {
11949 /* function(name, [args], dict) */
11950 arg_idx = 1;
11951 dict_idx = 2;
11952 }
11953 else if (argvars[1].v_type == VAR_DICT)
11954 /* function(name, dict) */
11955 dict_idx = 1;
11956 else
11957 /* function(name, [args]) */
11958 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011959 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011960 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011961 if (argvars[dict_idx].v_type != VAR_DICT)
11962 {
11963 EMSG(_("E922: expected a dict"));
11964 vim_free(name);
11965 return;
11966 }
11967 if (argvars[dict_idx].vval.v_dict == NULL)
11968 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011969 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011970 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011971 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011972 if (argvars[arg_idx].v_type != VAR_LIST)
11973 {
11974 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11975 vim_free(name);
11976 return;
11977 }
11978 list = argvars[arg_idx].vval.v_list;
11979 if (list == NULL || list->lv_len == 0)
11980 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011981 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011982 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011983 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010011984 {
11985 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011986
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011987 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010011988 if (pt == NULL)
11989 vim_free(name);
11990 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011991 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011992 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011993 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011994 listitem_T *li;
11995 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011996 int arg_len = 0;
11997 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011998
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011999 if (arg_pt != NULL)
12000 arg_len = arg_pt->pt_argc;
12001 if (list != NULL)
12002 lv_len = list->lv_len;
12003 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012004 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012005 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012006 if (pt->pt_argv == NULL)
12007 {
12008 vim_free(pt);
12009 vim_free(name);
12010 return;
12011 }
12012 else
12013 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012014 for (i = 0; i < arg_len; i++)
12015 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12016 if (lv_len > 0)
12017 for (li = list->lv_first; li != NULL;
12018 li = li->li_next)
12019 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012020 }
12021 }
12022
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012023 /* For "function(dict.func, [], dict)" and "func" is a partial
12024 * use "dict". That is backwards compatible. */
12025 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012026 {
12027 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12028 ++pt->pt_dict->dv_refcount;
12029 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012030 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012031 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012032 pt->pt_dict = arg_pt->pt_dict;
12033 if (pt->pt_dict != NULL)
12034 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012035 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012036
12037 pt->pt_refcount = 1;
12038 pt->pt_name = name;
12039 func_ref(pt->pt_name);
12040 }
12041 rettv->v_type = VAR_PARTIAL;
12042 rettv->vval.v_partial = pt;
12043 }
12044 else
12045 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012046 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012047 rettv->v_type = VAR_FUNC;
12048 rettv->vval.v_string = name;
12049 func_ref(name);
12050 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012051 }
12052}
12053
12054/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012055 * "garbagecollect()" function
12056 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012058f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012059{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012060 /* This is postponed until we are back at the toplevel, because we may be
12061 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12062 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012063
12064 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12065 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012066}
12067
12068/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012069 * "get()" function
12070 */
12071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012072f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073{
Bram Moolenaar33570922005-01-25 22:26:29 +000012074 listitem_T *li;
12075 list_T *l;
12076 dictitem_T *di;
12077 dict_T *d;
12078 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012079
Bram Moolenaare9a41262005-01-15 22:18:47 +000012080 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012081 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012082 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012084 int error = FALSE;
12085
12086 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12087 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012088 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012089 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012090 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012091 else if (argvars[0].v_type == VAR_DICT)
12092 {
12093 if ((d = argvars[0].vval.v_dict) != NULL)
12094 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012095 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012096 if (di != NULL)
12097 tv = &di->di_tv;
12098 }
12099 }
12100 else
12101 EMSG2(_(e_listdictarg), "get()");
12102
12103 if (tv == NULL)
12104 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012105 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012106 copy_tv(&argvars[2], rettv);
12107 }
12108 else
12109 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012110}
12111
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012112static 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 +000012113
12114/*
12115 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012116 * Return a range (from start to end) of lines in rettv from the specified
12117 * buffer.
12118 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012119 */
12120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012121get_buffer_lines(
12122 buf_T *buf,
12123 linenr_T start,
12124 linenr_T end,
12125 int retlist,
12126 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012127{
12128 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012129
Bram Moolenaar959a1432013-12-14 12:17:38 +010012130 rettv->v_type = VAR_STRING;
12131 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012132 if (retlist && rettv_list_alloc(rettv) == FAIL)
12133 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012134
12135 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12136 return;
12137
12138 if (!retlist)
12139 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012140 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12141 p = ml_get_buf(buf, start, FALSE);
12142 else
12143 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012144 rettv->vval.v_string = vim_strsave(p);
12145 }
12146 else
12147 {
12148 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012149 return;
12150
12151 if (start < 1)
12152 start = 1;
12153 if (end > buf->b_ml.ml_line_count)
12154 end = buf->b_ml.ml_line_count;
12155 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012156 if (list_append_string(rettv->vval.v_list,
12157 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012158 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012159 }
12160}
12161
12162/*
12163 * "getbufline()" function
12164 */
12165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012166f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012167{
12168 linenr_T lnum;
12169 linenr_T end;
12170 buf_T *buf;
12171
12172 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12173 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012174 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012175 --emsg_off;
12176
Bram Moolenaar661b1822005-07-28 22:36:45 +000012177 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012178 if (argvars[2].v_type == VAR_UNKNOWN)
12179 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012180 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012181 end = get_tv_lnum_buf(&argvars[2], buf);
12182
Bram Moolenaar342337a2005-07-21 21:11:17 +000012183 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012184}
12185
Bram Moolenaar0d660222005-01-07 21:51:51 +000012186/*
12187 * "getbufvar()" function
12188 */
12189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012190f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012191{
12192 buf_T *buf;
12193 buf_T *save_curbuf;
12194 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012195 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012196 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012198 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12199 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012200 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012201 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012202
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012203 rettv->v_type = VAR_STRING;
12204 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012205
12206 if (buf != NULL && varname != NULL)
12207 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012208 /* set curbuf to be our buf, temporarily */
12209 save_curbuf = curbuf;
12210 curbuf = buf;
12211
Bram Moolenaar0d660222005-01-07 21:51:51 +000012212 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012213 {
12214 if (get_option_tv(&varname, rettv, TRUE) == OK)
12215 done = TRUE;
12216 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012217 else if (STRCMP(varname, "changedtick") == 0)
12218 {
12219 rettv->v_type = VAR_NUMBER;
12220 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012221 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012222 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012223 else
12224 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012225 /* Look up the variable. */
12226 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12227 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12228 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012229 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012231 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012232 done = TRUE;
12233 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012234 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012235
12236 /* restore previous notion of curbuf */
12237 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012238 }
12239
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012240 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12241 /* use the default value */
12242 copy_tv(&argvars[2], rettv);
12243
Bram Moolenaar0d660222005-01-07 21:51:51 +000012244 --emsg_off;
12245}
12246
12247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 * "getchar()" function
12249 */
12250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012251f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252{
12253 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012254 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012256 /* Position the cursor. Needed after a message that ends in a space. */
12257 windgoto(msg_row, msg_col);
12258
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 ++no_mapping;
12260 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012261 for (;;)
12262 {
12263 if (argvars[0].v_type == VAR_UNKNOWN)
12264 /* getchar(): blocking wait. */
12265 n = safe_vgetc();
12266 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12267 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012268 n = vpeekc_any();
12269 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012270 /* illegal argument or getchar(0) and no char avail: return zero */
12271 n = 0;
12272 else
12273 /* getchar(0) and char avail: return char */
12274 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012275
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012276 if (n == K_IGNORE)
12277 continue;
12278 break;
12279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 --no_mapping;
12281 --allow_keys;
12282
Bram Moolenaar219b8702006-11-01 14:32:36 +000012283 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12284 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12285 vimvars[VV_MOUSE_COL].vv_nr = 0;
12286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012287 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 if (IS_SPECIAL(n) || mod_mask != 0)
12289 {
12290 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12291 int i = 0;
12292
12293 /* Turn a special key into three bytes, plus modifier. */
12294 if (mod_mask != 0)
12295 {
12296 temp[i++] = K_SPECIAL;
12297 temp[i++] = KS_MODIFIER;
12298 temp[i++] = mod_mask;
12299 }
12300 if (IS_SPECIAL(n))
12301 {
12302 temp[i++] = K_SPECIAL;
12303 temp[i++] = K_SECOND(n);
12304 temp[i++] = K_THIRD(n);
12305 }
12306#ifdef FEAT_MBYTE
12307 else if (has_mbyte)
12308 i += (*mb_char2bytes)(n, temp + i);
12309#endif
12310 else
12311 temp[i++] = n;
12312 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313 rettv->v_type = VAR_STRING;
12314 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012315
12316#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012317 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012318 {
12319 int row = mouse_row;
12320 int col = mouse_col;
12321 win_T *win;
12322 linenr_T lnum;
12323# ifdef FEAT_WINDOWS
12324 win_T *wp;
12325# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012326 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012327
12328 if (row >= 0 && col >= 0)
12329 {
12330 /* Find the window at the mouse coordinates and compute the
12331 * text position. */
12332 win = mouse_find_win(&row, &col);
12333 (void)mouse_comp_pos(win, &row, &col, &lnum);
12334# ifdef FEAT_WINDOWS
12335 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012336 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012337# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012338 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012339 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12340 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12341 }
12342 }
12343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 }
12345}
12346
12347/*
12348 * "getcharmod()" function
12349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012351f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354}
12355
12356/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012357 * "getcharsearch()" function
12358 */
12359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012360f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012361{
12362 if (rettv_dict_alloc(rettv) != FAIL)
12363 {
12364 dict_T *dict = rettv->vval.v_dict;
12365
12366 dict_add_nr_str(dict, "char", 0L, last_csearch());
12367 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12368 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12369 }
12370}
12371
12372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 * "getcmdline()" function
12374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012376f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012378 rettv->v_type = VAR_STRING;
12379 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380}
12381
12382/*
12383 * "getcmdpos()" function
12384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012386f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389}
12390
12391/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012392 * "getcmdtype()" function
12393 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012395f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012396{
12397 rettv->v_type = VAR_STRING;
12398 rettv->vval.v_string = alloc(2);
12399 if (rettv->vval.v_string != NULL)
12400 {
12401 rettv->vval.v_string[0] = get_cmdline_type();
12402 rettv->vval.v_string[1] = NUL;
12403 }
12404}
12405
12406/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012407 * "getcmdwintype()" function
12408 */
12409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012410f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012411{
12412 rettv->v_type = VAR_STRING;
12413 rettv->vval.v_string = NULL;
12414#ifdef FEAT_CMDWIN
12415 rettv->vval.v_string = alloc(2);
12416 if (rettv->vval.v_string != NULL)
12417 {
12418 rettv->vval.v_string[0] = cmdwin_type;
12419 rettv->vval.v_string[1] = NUL;
12420 }
12421#endif
12422}
12423
12424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 * "getcwd()" function
12426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012428f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012430 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012431 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012433 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012434 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012435
12436 wp = find_tabwin(&argvars[0], &argvars[1]);
12437 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012439 if (wp->w_localdir != NULL)
12440 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12441 else if(globaldir != NULL)
12442 rettv->vval.v_string = vim_strsave(globaldir);
12443 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012444 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012445 cwd = alloc(MAXPATHL);
12446 if (cwd != NULL)
12447 {
12448 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12449 rettv->vval.v_string = vim_strsave(cwd);
12450 vim_free(cwd);
12451 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012452 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012453#ifdef BACKSLASH_IN_FILENAME
12454 if (rettv->vval.v_string != NULL)
12455 slash_adjust(rettv->vval.v_string);
12456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 }
12458}
12459
12460/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012461 * "getfontname()" function
12462 */
12463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012464f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466 rettv->v_type = VAR_STRING;
12467 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012468#ifdef FEAT_GUI
12469 if (gui.in_use)
12470 {
12471 GuiFont font;
12472 char_u *name = NULL;
12473
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012474 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012475 {
12476 /* Get the "Normal" font. Either the name saved by
12477 * hl_set_font_name() or from the font ID. */
12478 font = gui.norm_font;
12479 name = hl_get_font_name();
12480 }
12481 else
12482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012484 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12485 return;
12486 font = gui_mch_get_font(name, FALSE);
12487 if (font == NOFONT)
12488 return; /* Invalid font name, return empty string. */
12489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012491 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012492 gui_mch_free_font(font);
12493 }
12494#endif
12495}
12496
12497/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012498 * "getfperm({fname})" function
12499 */
12500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012501f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012502{
12503 char_u *fname;
12504 struct stat st;
12505 char_u *perm = NULL;
12506 char_u flags[] = "rwx";
12507 int i;
12508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012512 if (mch_stat((char *)fname, &st) >= 0)
12513 {
12514 perm = vim_strsave((char_u *)"---------");
12515 if (perm != NULL)
12516 {
12517 for (i = 0; i < 9; i++)
12518 {
12519 if (st.st_mode & (1 << (8 - i)))
12520 perm[i] = flags[i % 3];
12521 }
12522 }
12523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012525}
12526
12527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 * "getfsize({fname})" function
12529 */
12530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012531f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532{
12533 char_u *fname;
12534 struct stat st;
12535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012536 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012538 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539
12540 if (mch_stat((char *)fname, &st) >= 0)
12541 {
12542 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012543 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012546 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012547
12548 /* non-perfect check for overflow */
12549 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12550 rettv->vval.v_number = -2;
12551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 }
12553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555}
12556
12557/*
12558 * "getftime({fname})" function
12559 */
12560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012561f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562{
12563 char_u *fname;
12564 struct stat st;
12565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012566 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567
12568 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012569 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012571 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572}
12573
12574/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012575 * "getftype({fname})" function
12576 */
12577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012578f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012579{
12580 char_u *fname;
12581 struct stat st;
12582 char_u *type = NULL;
12583 char *t;
12584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012585 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012587 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012588 if (mch_lstat((char *)fname, &st) >= 0)
12589 {
12590#ifdef S_ISREG
12591 if (S_ISREG(st.st_mode))
12592 t = "file";
12593 else if (S_ISDIR(st.st_mode))
12594 t = "dir";
12595# ifdef S_ISLNK
12596 else if (S_ISLNK(st.st_mode))
12597 t = "link";
12598# endif
12599# ifdef S_ISBLK
12600 else if (S_ISBLK(st.st_mode))
12601 t = "bdev";
12602# endif
12603# ifdef S_ISCHR
12604 else if (S_ISCHR(st.st_mode))
12605 t = "cdev";
12606# endif
12607# ifdef S_ISFIFO
12608 else if (S_ISFIFO(st.st_mode))
12609 t = "fifo";
12610# endif
12611# ifdef S_ISSOCK
12612 else if (S_ISSOCK(st.st_mode))
12613 t = "fifo";
12614# endif
12615 else
12616 t = "other";
12617#else
12618# ifdef S_IFMT
12619 switch (st.st_mode & S_IFMT)
12620 {
12621 case S_IFREG: t = "file"; break;
12622 case S_IFDIR: t = "dir"; break;
12623# ifdef S_IFLNK
12624 case S_IFLNK: t = "link"; break;
12625# endif
12626# ifdef S_IFBLK
12627 case S_IFBLK: t = "bdev"; break;
12628# endif
12629# ifdef S_IFCHR
12630 case S_IFCHR: t = "cdev"; break;
12631# endif
12632# ifdef S_IFIFO
12633 case S_IFIFO: t = "fifo"; break;
12634# endif
12635# ifdef S_IFSOCK
12636 case S_IFSOCK: t = "socket"; break;
12637# endif
12638 default: t = "other";
12639 }
12640# else
12641 if (mch_isdir(fname))
12642 t = "dir";
12643 else
12644 t = "file";
12645# endif
12646#endif
12647 type = vim_strsave((char_u *)t);
12648 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012650}
12651
12652/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012653 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012654 */
12655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012656f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012657{
12658 linenr_T lnum;
12659 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012660 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661
12662 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012663 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012664 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012665 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012666 retlist = FALSE;
12667 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012668 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012669 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012670 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012671 retlist = TRUE;
12672 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012673
Bram Moolenaar342337a2005-07-21 21:11:17 +000012674 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012675}
12676
12677/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012678 * "getmatches()" function
12679 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012681f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012682{
12683#ifdef FEAT_SEARCH_EXTRA
12684 dict_T *dict;
12685 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012686 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012687
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012688 if (rettv_list_alloc(rettv) == OK)
12689 {
12690 while (cur != NULL)
12691 {
12692 dict = dict_alloc();
12693 if (dict == NULL)
12694 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012695 if (cur->match.regprog == NULL)
12696 {
12697 /* match added with matchaddpos() */
12698 for (i = 0; i < MAXPOSMATCH; ++i)
12699 {
12700 llpos_T *llpos;
12701 char buf[6];
12702 list_T *l;
12703
12704 llpos = &cur->pos.pos[i];
12705 if (llpos->lnum == 0)
12706 break;
12707 l = list_alloc();
12708 if (l == NULL)
12709 break;
12710 list_append_number(l, (varnumber_T)llpos->lnum);
12711 if (llpos->col > 0)
12712 {
12713 list_append_number(l, (varnumber_T)llpos->col);
12714 list_append_number(l, (varnumber_T)llpos->len);
12715 }
12716 sprintf(buf, "pos%d", i + 1);
12717 dict_add_list(dict, buf, l);
12718 }
12719 }
12720 else
12721 {
12722 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12723 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012724 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012725 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12726 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012727# ifdef FEAT_CONCEAL
12728 if (cur->conceal_char)
12729 {
12730 char_u buf[MB_MAXBYTES + 1];
12731
12732 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12733 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12734 }
12735# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012736 list_append_dict(rettv->vval.v_list, dict);
12737 cur = cur->next;
12738 }
12739 }
12740#endif
12741}
12742
12743/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012744 * "getpid()" function
12745 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012747f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012748{
12749 rettv->vval.v_number = mch_get_pid();
12750}
12751
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012752static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012753
12754/*
12755 * "getcurpos()" function
12756 */
12757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012758f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012759{
12760 getpos_both(argvars, rettv, TRUE);
12761}
12762
Bram Moolenaar18081e32008-02-20 19:11:07 +000012763/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012764 * "getpos(string)" function
12765 */
12766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012767f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012768{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012769 getpos_both(argvars, rettv, FALSE);
12770}
12771
12772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012773getpos_both(
12774 typval_T *argvars,
12775 typval_T *rettv,
12776 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012777{
Bram Moolenaara5525202006-03-02 22:52:09 +000012778 pos_T *fp;
12779 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012780 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012781
12782 if (rettv_list_alloc(rettv) == OK)
12783 {
12784 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012785 if (getcurpos)
12786 fp = &curwin->w_cursor;
12787 else
12788 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012789 if (fnum != -1)
12790 list_append_number(l, (varnumber_T)fnum);
12791 else
12792 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012793 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12794 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012795 list_append_number(l, (fp != NULL)
12796 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012797 : (varnumber_T)0);
12798 list_append_number(l,
12799#ifdef FEAT_VIRTUALEDIT
12800 (fp != NULL) ? (varnumber_T)fp->coladd :
12801#endif
12802 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012803 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012804 {
12805 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012806 list_append_number(l, curwin->w_curswant == MAXCOL ?
12807 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012808 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012809 }
12810 else
12811 rettv->vval.v_number = FALSE;
12812}
12813
12814/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012815 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012816 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012818f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012819{
12820#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012821 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012822#endif
12823
Bram Moolenaar2641f772005-03-25 21:58:17 +000012824#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012825 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012826 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012827 wp = NULL;
12828 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12829 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012830 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012831 if (wp == NULL)
12832 return;
12833 }
12834
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012835 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012836 }
12837#endif
12838}
12839
12840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 * "getreg()" function
12842 */
12843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012844f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845{
12846 char_u *strregname;
12847 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012848 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012849 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012850 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012852 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012854 strregname = get_tv_string_chk(&argvars[0]);
12855 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012856 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012858 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012859 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12860 return_list = get_tv_number_chk(&argvars[2], &error);
12861 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012864 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012865
12866 if (error)
12867 return;
12868
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 regname = (strregname == NULL ? '"' : *strregname);
12870 if (regname == 0)
12871 regname = '"';
12872
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012873 if (return_list)
12874 {
12875 rettv->v_type = VAR_LIST;
12876 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12877 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012878 if (rettv->vval.v_list != NULL)
12879 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012880 }
12881 else
12882 {
12883 rettv->v_type = VAR_STRING;
12884 rettv->vval.v_string = get_reg_contents(regname,
12885 arg2 ? GREG_EXPR_SRC : 0);
12886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887}
12888
12889/*
12890 * "getregtype()" function
12891 */
12892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012893f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894{
12895 char_u *strregname;
12896 int regname;
12897 char_u buf[NUMBUFLEN + 2];
12898 long reglen = 0;
12899
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012900 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 {
12902 strregname = get_tv_string_chk(&argvars[0]);
12903 if (strregname == NULL) /* type error; errmsg already given */
12904 {
12905 rettv->v_type = VAR_STRING;
12906 rettv->vval.v_string = NULL;
12907 return;
12908 }
12909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910 else
12911 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012912 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913
12914 regname = (strregname == NULL ? '"' : *strregname);
12915 if (regname == 0)
12916 regname = '"';
12917
12918 buf[0] = NUL;
12919 buf[1] = NUL;
12920 switch (get_reg_type(regname, &reglen))
12921 {
12922 case MLINE: buf[0] = 'V'; break;
12923 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924 case MBLOCK:
12925 buf[0] = Ctrl_V;
12926 sprintf((char *)buf + 1, "%ld", reglen + 1);
12927 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->v_type = VAR_STRING;
12930 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931}
12932
12933/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012934 * "gettabvar()" function
12935 */
12936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012937f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012938{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012939 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012940 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012941 dictitem_T *v;
12942 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012943 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012944
12945 rettv->v_type = VAR_STRING;
12946 rettv->vval.v_string = NULL;
12947
12948 varname = get_tv_string_chk(&argvars[1]);
12949 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12950 if (tp != NULL && varname != NULL)
12951 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012952 /* Set tp to be our tabpage, temporarily. Also set the window to the
12953 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012954 if (switch_win(&oldcurwin, &oldtabpage,
12955 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012956 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012957 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012958 /* look up the variable */
12959 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12960 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12961 if (v != NULL)
12962 {
12963 copy_tv(&v->di_tv, rettv);
12964 done = TRUE;
12965 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012966 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012967
12968 /* restore previous notion of curwin */
12969 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012970 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012971
12972 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012973 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012974}
12975
12976/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012977 * "gettabwinvar()" function
12978 */
12979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012980f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012981{
12982 getwinvar(argvars, rettv, 1);
12983}
12984
12985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986 * "getwinposx()" function
12987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012989f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012991 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992#ifdef FEAT_GUI
12993 if (gui.in_use)
12994 {
12995 int x, y;
12996
12997 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 }
13000#endif
13001}
13002
13003/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013004 * "win_findbuf()" function
13005 */
13006 static void
13007f_win_findbuf(typval_T *argvars, typval_T *rettv)
13008{
13009 if (rettv_list_alloc(rettv) != FAIL)
13010 win_findbuf(argvars, rettv->vval.v_list);
13011}
13012
13013/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013014 * "win_getid()" function
13015 */
13016 static void
13017f_win_getid(typval_T *argvars, typval_T *rettv)
13018{
13019 rettv->vval.v_number = win_getid(argvars);
13020}
13021
13022/*
13023 * "win_gotoid()" function
13024 */
13025 static void
13026f_win_gotoid(typval_T *argvars, typval_T *rettv)
13027{
13028 rettv->vval.v_number = win_gotoid(argvars);
13029}
13030
13031/*
13032 * "win_id2tabwin()" function
13033 */
13034 static void
13035f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13036{
13037 if (rettv_list_alloc(rettv) != FAIL)
13038 win_id2tabwin(argvars, rettv->vval.v_list);
13039}
13040
13041/*
13042 * "win_id2win()" function
13043 */
13044 static void
13045f_win_id2win(typval_T *argvars, typval_T *rettv)
13046{
13047 rettv->vval.v_number = win_id2win(argvars);
13048}
13049
13050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 * "getwinposy()" function
13052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013054f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057#ifdef FEAT_GUI
13058 if (gui.in_use)
13059 {
13060 int x, y;
13061
13062 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013063 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064 }
13065#endif
13066}
13067
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013068/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013069 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013070 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013071 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013072find_win_by_nr(
13073 typval_T *vp,
13074 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013075{
13076#ifdef FEAT_WINDOWS
13077 win_T *wp;
13078#endif
13079 int nr;
13080
13081 nr = get_tv_number_chk(vp, NULL);
13082
13083#ifdef FEAT_WINDOWS
13084 if (nr < 0)
13085 return NULL;
13086 if (nr == 0)
13087 return curwin;
13088
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013089 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13090 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013091 if (--nr <= 0)
13092 break;
13093 return wp;
13094#else
13095 if (nr == 0 || nr == 1)
13096 return curwin;
13097 return NULL;
13098#endif
13099}
13100
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013102 * Find window specified by "wvp" in tabpage "tvp".
13103 */
13104 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013105find_tabwin(
13106 typval_T *wvp, /* VAR_UNKNOWN for current window */
13107 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013108{
13109 win_T *wp = NULL;
13110 tabpage_T *tp = NULL;
13111 long n;
13112
13113 if (wvp->v_type != VAR_UNKNOWN)
13114 {
13115 if (tvp->v_type != VAR_UNKNOWN)
13116 {
13117 n = get_tv_number(tvp);
13118 if (n >= 0)
13119 tp = find_tabpage(n);
13120 }
13121 else
13122 tp = curtab;
13123
13124 if (tp != NULL)
13125 wp = find_win_by_nr(wvp, tp);
13126 }
13127 else
13128 wp = curwin;
13129
13130 return wp;
13131}
13132
13133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 * "getwinvar()" function
13135 */
13136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013137f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013139 getwinvar(argvars, rettv, 0);
13140}
13141
13142/*
13143 * getwinvar() and gettabwinvar()
13144 */
13145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013146getwinvar(
13147 typval_T *argvars,
13148 typval_T *rettv,
13149 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013150{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013151 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013153 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013154 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013155 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013156#ifdef FEAT_WINDOWS
13157 win_T *oldcurwin;
13158 tabpage_T *oldtabpage;
13159 int need_switch_win;
13160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013162#ifdef FEAT_WINDOWS
13163 if (off == 1)
13164 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13165 else
13166 tp = curtab;
13167#endif
13168 win = find_win_by_nr(&argvars[off], tp);
13169 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013170 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013172 rettv->v_type = VAR_STRING;
13173 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174
13175 if (win != NULL && varname != NULL)
13176 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013177#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013178 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013179 * otherwise the window is not valid. Only do this when needed,
13180 * autocommands get blocked. */
13181 need_switch_win = !(tp == curtab && win == curwin);
13182 if (!need_switch_win
13183 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13184#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013185 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013186 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013187 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013188 if (get_option_tv(&varname, rettv, 1) == OK)
13189 done = TRUE;
13190 }
13191 else
13192 {
13193 /* Look up the variable. */
13194 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13195 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13196 varname, FALSE);
13197 if (v != NULL)
13198 {
13199 copy_tv(&v->di_tv, rettv);
13200 done = TRUE;
13201 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013204
Bram Moolenaarba117c22015-09-29 16:53:22 +020013205#ifdef FEAT_WINDOWS
13206 if (need_switch_win)
13207 /* restore previous notion of curwin */
13208 restore_win(oldcurwin, oldtabpage, TRUE);
13209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 }
13211
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013212 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13213 /* use the default return value */
13214 copy_tv(&argvars[off + 2], rettv);
13215
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 --emsg_off;
13217}
13218
13219/*
13220 * "glob()" function
13221 */
13222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013223f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013225 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013227 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013229 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013230 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013232 if (argvars[1].v_type != VAR_UNKNOWN)
13233 {
13234 if (get_tv_number_chk(&argvars[1], &error))
13235 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013236 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013237 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013238 if (get_tv_number_chk(&argvars[2], &error))
13239 {
13240 rettv->v_type = VAR_LIST;
13241 rettv->vval.v_list = NULL;
13242 }
13243 if (argvars[3].v_type != VAR_UNKNOWN
13244 && get_tv_number_chk(&argvars[3], &error))
13245 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013246 }
13247 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013248 if (!error)
13249 {
13250 ExpandInit(&xpc);
13251 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013252 if (p_wic)
13253 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013254 if (rettv->v_type == VAR_STRING)
13255 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013256 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013257 else if (rettv_list_alloc(rettv) != FAIL)
13258 {
13259 int i;
13260
13261 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13262 NULL, options, WILD_ALL_KEEP);
13263 for (i = 0; i < xpc.xp_numfiles; i++)
13264 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13265
13266 ExpandCleanup(&xpc);
13267 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013268 }
13269 else
13270 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271}
13272
13273/*
13274 * "globpath()" function
13275 */
13276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013277f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013279 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013281 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013282 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013283 garray_T ga;
13284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013286 /* When the optional second argument is non-zero, don't remove matches
13287 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013289 if (argvars[2].v_type != VAR_UNKNOWN)
13290 {
13291 if (get_tv_number_chk(&argvars[2], &error))
13292 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013293 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013294 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013295 if (get_tv_number_chk(&argvars[3], &error))
13296 {
13297 rettv->v_type = VAR_LIST;
13298 rettv->vval.v_list = NULL;
13299 }
13300 if (argvars[4].v_type != VAR_UNKNOWN
13301 && get_tv_number_chk(&argvars[4], &error))
13302 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013303 }
13304 }
13305 if (file != NULL && !error)
13306 {
13307 ga_init2(&ga, (int)sizeof(char_u *), 10);
13308 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13309 if (rettv->v_type == VAR_STRING)
13310 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13311 else if (rettv_list_alloc(rettv) != FAIL)
13312 for (i = 0; i < ga.ga_len; ++i)
13313 list_append_string(rettv->vval.v_list,
13314 ((char_u **)(ga.ga_data))[i], -1);
13315 ga_clear_strings(&ga);
13316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013318 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319}
13320
13321/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013322 * "glob2regpat()" function
13323 */
13324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013325f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013326{
13327 char_u *pat = get_tv_string_chk(&argvars[0]);
13328
13329 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013330 rettv->vval.v_string = (pat == NULL)
13331 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013332}
13333
13334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 * "has()" function
13336 */
13337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013338f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339{
13340 int i;
13341 char_u *name;
13342 int n = FALSE;
13343 static char *(has_list[]) =
13344 {
13345#ifdef AMIGA
13346 "amiga",
13347# ifdef FEAT_ARP
13348 "arp",
13349# endif
13350#endif
13351#ifdef __BEOS__
13352 "beos",
13353#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013354#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355 "mac",
13356#endif
13357#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013358 "macunix", /* built with 'darwin' enabled */
13359#endif
13360#if defined(__APPLE__) && __APPLE__ == 1
13361 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363#ifdef __QNX__
13364 "qnx",
13365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366#ifdef UNIX
13367 "unix",
13368#endif
13369#ifdef VMS
13370 "vms",
13371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372#ifdef WIN32
13373 "win32",
13374#endif
13375#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13376 "win32unix",
13377#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013378#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 "win64",
13380#endif
13381#ifdef EBCDIC
13382 "ebcdic",
13383#endif
13384#ifndef CASE_INSENSITIVE_FILENAME
13385 "fname_case",
13386#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013387#ifdef HAVE_ACL
13388 "acl",
13389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390#ifdef FEAT_ARABIC
13391 "arabic",
13392#endif
13393#ifdef FEAT_AUTOCMD
13394 "autocmd",
13395#endif
13396#ifdef FEAT_BEVAL
13397 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013398# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13399 "balloon_multiline",
13400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401#endif
13402#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13403 "builtin_terms",
13404# ifdef ALL_BUILTIN_TCAPS
13405 "all_builtin_terms",
13406# endif
13407#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013408#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13409 || defined(FEAT_GUI_W32) \
13410 || defined(FEAT_GUI_MOTIF))
13411 "browsefilter",
13412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413#ifdef FEAT_BYTEOFF
13414 "byte_offset",
13415#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013416#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013417 "channel",
13418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419#ifdef FEAT_CINDENT
13420 "cindent",
13421#endif
13422#ifdef FEAT_CLIENTSERVER
13423 "clientserver",
13424#endif
13425#ifdef FEAT_CLIPBOARD
13426 "clipboard",
13427#endif
13428#ifdef FEAT_CMDL_COMPL
13429 "cmdline_compl",
13430#endif
13431#ifdef FEAT_CMDHIST
13432 "cmdline_hist",
13433#endif
13434#ifdef FEAT_COMMENTS
13435 "comments",
13436#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013437#ifdef FEAT_CONCEAL
13438 "conceal",
13439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440#ifdef FEAT_CRYPT
13441 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013442 "crypt-blowfish",
13443 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444#endif
13445#ifdef FEAT_CSCOPE
13446 "cscope",
13447#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013448#ifdef FEAT_CURSORBIND
13449 "cursorbind",
13450#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013451#ifdef CURSOR_SHAPE
13452 "cursorshape",
13453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454#ifdef DEBUG
13455 "debug",
13456#endif
13457#ifdef FEAT_CON_DIALOG
13458 "dialog_con",
13459#endif
13460#ifdef FEAT_GUI_DIALOG
13461 "dialog_gui",
13462#endif
13463#ifdef FEAT_DIFF
13464 "diff",
13465#endif
13466#ifdef FEAT_DIGRAPHS
13467 "digraphs",
13468#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013469#ifdef FEAT_DIRECTX
13470 "directx",
13471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472#ifdef FEAT_DND
13473 "dnd",
13474#endif
13475#ifdef FEAT_EMACS_TAGS
13476 "emacs_tags",
13477#endif
13478 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013479 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480#ifdef FEAT_SEARCH_EXTRA
13481 "extra_search",
13482#endif
13483#ifdef FEAT_FKMAP
13484 "farsi",
13485#endif
13486#ifdef FEAT_SEARCHPATH
13487 "file_in_path",
13488#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013489#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013490 "filterpipe",
13491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492#ifdef FEAT_FIND_ID
13493 "find_in_path",
13494#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013495#ifdef FEAT_FLOAT
13496 "float",
13497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498#ifdef FEAT_FOLDING
13499 "folding",
13500#endif
13501#ifdef FEAT_FOOTER
13502 "footer",
13503#endif
13504#if !defined(USE_SYSTEM) && defined(UNIX)
13505 "fork",
13506#endif
13507#ifdef FEAT_GETTEXT
13508 "gettext",
13509#endif
13510#ifdef FEAT_GUI
13511 "gui",
13512#endif
13513#ifdef FEAT_GUI_ATHENA
13514# ifdef FEAT_GUI_NEXTAW
13515 "gui_neXtaw",
13516# else
13517 "gui_athena",
13518# endif
13519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520#ifdef FEAT_GUI_GTK
13521 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013522# ifdef USE_GTK3
13523 "gui_gtk3",
13524# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013528#ifdef FEAT_GUI_GNOME
13529 "gui_gnome",
13530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531#ifdef FEAT_GUI_MAC
13532 "gui_mac",
13533#endif
13534#ifdef FEAT_GUI_MOTIF
13535 "gui_motif",
13536#endif
13537#ifdef FEAT_GUI_PHOTON
13538 "gui_photon",
13539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540#ifdef FEAT_GUI_W32
13541 "gui_win32",
13542#endif
13543#ifdef FEAT_HANGULIN
13544 "hangul_input",
13545#endif
13546#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13547 "iconv",
13548#endif
13549#ifdef FEAT_INS_EXPAND
13550 "insert_expand",
13551#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013552#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013553 "job",
13554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555#ifdef FEAT_JUMPLIST
13556 "jumplist",
13557#endif
13558#ifdef FEAT_KEYMAP
13559 "keymap",
13560#endif
13561#ifdef FEAT_LANGMAP
13562 "langmap",
13563#endif
13564#ifdef FEAT_LIBCALL
13565 "libcall",
13566#endif
13567#ifdef FEAT_LINEBREAK
13568 "linebreak",
13569#endif
13570#ifdef FEAT_LISP
13571 "lispindent",
13572#endif
13573#ifdef FEAT_LISTCMDS
13574 "listcmds",
13575#endif
13576#ifdef FEAT_LOCALMAP
13577 "localmap",
13578#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013579#ifdef FEAT_LUA
13580# ifndef DYNAMIC_LUA
13581 "lua",
13582# endif
13583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584#ifdef FEAT_MENU
13585 "menu",
13586#endif
13587#ifdef FEAT_SESSION
13588 "mksession",
13589#endif
13590#ifdef FEAT_MODIFY_FNAME
13591 "modify_fname",
13592#endif
13593#ifdef FEAT_MOUSE
13594 "mouse",
13595#endif
13596#ifdef FEAT_MOUSESHAPE
13597 "mouseshape",
13598#endif
13599#if defined(UNIX) || defined(VMS)
13600# ifdef FEAT_MOUSE_DEC
13601 "mouse_dec",
13602# endif
13603# ifdef FEAT_MOUSE_GPM
13604 "mouse_gpm",
13605# endif
13606# ifdef FEAT_MOUSE_JSB
13607 "mouse_jsbterm",
13608# endif
13609# ifdef FEAT_MOUSE_NET
13610 "mouse_netterm",
13611# endif
13612# ifdef FEAT_MOUSE_PTERM
13613 "mouse_pterm",
13614# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013615# ifdef FEAT_MOUSE_SGR
13616 "mouse_sgr",
13617# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013618# ifdef FEAT_SYSMOUSE
13619 "mouse_sysmouse",
13620# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013621# ifdef FEAT_MOUSE_URXVT
13622 "mouse_urxvt",
13623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624# ifdef FEAT_MOUSE_XTERM
13625 "mouse_xterm",
13626# endif
13627#endif
13628#ifdef FEAT_MBYTE
13629 "multi_byte",
13630#endif
13631#ifdef FEAT_MBYTE_IME
13632 "multi_byte_ime",
13633#endif
13634#ifdef FEAT_MULTI_LANG
13635 "multi_lang",
13636#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013637#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013638#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013639 "mzscheme",
13640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642#ifdef FEAT_OLE
13643 "ole",
13644#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013645 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646#ifdef FEAT_PATH_EXTRA
13647 "path_extra",
13648#endif
13649#ifdef FEAT_PERL
13650#ifndef DYNAMIC_PERL
13651 "perl",
13652#endif
13653#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013654#ifdef FEAT_PERSISTENT_UNDO
13655 "persistent_undo",
13656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657#ifdef FEAT_PYTHON
13658#ifndef DYNAMIC_PYTHON
13659 "python",
13660#endif
13661#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013662#ifdef FEAT_PYTHON3
13663#ifndef DYNAMIC_PYTHON3
13664 "python3",
13665#endif
13666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667#ifdef FEAT_POSTSCRIPT
13668 "postscript",
13669#endif
13670#ifdef FEAT_PRINTER
13671 "printer",
13672#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013673#ifdef FEAT_PROFILE
13674 "profile",
13675#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013676#ifdef FEAT_RELTIME
13677 "reltime",
13678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679#ifdef FEAT_QUICKFIX
13680 "quickfix",
13681#endif
13682#ifdef FEAT_RIGHTLEFT
13683 "rightleft",
13684#endif
13685#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13686 "ruby",
13687#endif
13688#ifdef FEAT_SCROLLBIND
13689 "scrollbind",
13690#endif
13691#ifdef FEAT_CMDL_INFO
13692 "showcmd",
13693 "cmdline_info",
13694#endif
13695#ifdef FEAT_SIGNS
13696 "signs",
13697#endif
13698#ifdef FEAT_SMARTINDENT
13699 "smartindent",
13700#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013701#ifdef STARTUPTIME
13702 "startuptime",
13703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704#ifdef FEAT_STL_OPT
13705 "statusline",
13706#endif
13707#ifdef FEAT_SUN_WORKSHOP
13708 "sun_workshop",
13709#endif
13710#ifdef FEAT_NETBEANS_INTG
13711 "netbeans_intg",
13712#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013713#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013714 "spell",
13715#endif
13716#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 "syntax",
13718#endif
13719#if defined(USE_SYSTEM) || !defined(UNIX)
13720 "system",
13721#endif
13722#ifdef FEAT_TAG_BINS
13723 "tag_binary",
13724#endif
13725#ifdef FEAT_TAG_OLDSTATIC
13726 "tag_old_static",
13727#endif
13728#ifdef FEAT_TAG_ANYWHITE
13729 "tag_any_white",
13730#endif
13731#ifdef FEAT_TCL
13732# ifndef DYNAMIC_TCL
13733 "tcl",
13734# endif
13735#endif
13736#ifdef TERMINFO
13737 "terminfo",
13738#endif
13739#ifdef FEAT_TERMRESPONSE
13740 "termresponse",
13741#endif
13742#ifdef FEAT_TEXTOBJ
13743 "textobjects",
13744#endif
13745#ifdef HAVE_TGETENT
13746 "tgetent",
13747#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013748#ifdef FEAT_TIMERS
13749 "timers",
13750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751#ifdef FEAT_TITLE
13752 "title",
13753#endif
13754#ifdef FEAT_TOOLBAR
13755 "toolbar",
13756#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013757#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13758 "unnamedplus",
13759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760#ifdef FEAT_USR_CMDS
13761 "user-commands", /* was accidentally included in 5.4 */
13762 "user_commands",
13763#endif
13764#ifdef FEAT_VIMINFO
13765 "viminfo",
13766#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013767#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 "vertsplit",
13769#endif
13770#ifdef FEAT_VIRTUALEDIT
13771 "virtualedit",
13772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774#ifdef FEAT_VISUALEXTRA
13775 "visualextra",
13776#endif
13777#ifdef FEAT_VREPLACE
13778 "vreplace",
13779#endif
13780#ifdef FEAT_WILDIGN
13781 "wildignore",
13782#endif
13783#ifdef FEAT_WILDMENU
13784 "wildmenu",
13785#endif
13786#ifdef FEAT_WINDOWS
13787 "windows",
13788#endif
13789#ifdef FEAT_WAK
13790 "winaltkeys",
13791#endif
13792#ifdef FEAT_WRITEBACKUP
13793 "writebackup",
13794#endif
13795#ifdef FEAT_XIM
13796 "xim",
13797#endif
13798#ifdef FEAT_XFONTSET
13799 "xfontset",
13800#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013801#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013802 "xpm",
13803 "xpm_w32", /* for backward compatibility */
13804#else
13805# if defined(HAVE_XPM)
13806 "xpm",
13807# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809#ifdef USE_XSMP
13810 "xsmp",
13811#endif
13812#ifdef USE_XSMP_INTERACT
13813 "xsmp_interact",
13814#endif
13815#ifdef FEAT_XCLIPBOARD
13816 "xterm_clipboard",
13817#endif
13818#ifdef FEAT_XTERM_SAVE
13819 "xterm_save",
13820#endif
13821#if defined(UNIX) && defined(FEAT_X11)
13822 "X11",
13823#endif
13824 NULL
13825 };
13826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013827 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 for (i = 0; has_list[i] != NULL; ++i)
13829 if (STRICMP(name, has_list[i]) == 0)
13830 {
13831 n = TRUE;
13832 break;
13833 }
13834
13835 if (n == FALSE)
13836 {
13837 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013838 {
13839 if (name[5] == '-'
13840 && STRLEN(name) > 11
13841 && vim_isdigit(name[6])
13842 && vim_isdigit(name[8])
13843 && vim_isdigit(name[10]))
13844 {
13845 int major = atoi((char *)name + 6);
13846 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013847
13848 /* Expect "patch-9.9.01234". */
13849 n = (major < VIM_VERSION_MAJOR
13850 || (major == VIM_VERSION_MAJOR
13851 && (minor < VIM_VERSION_MINOR
13852 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013853 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013854 }
13855 else
13856 n = has_patch(atoi((char *)name + 5));
13857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 else if (STRICMP(name, "vim_starting") == 0)
13859 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013860#ifdef FEAT_MBYTE
13861 else if (STRICMP(name, "multi_byte_encoding") == 0)
13862 n = has_mbyte;
13863#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013864#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13865 else if (STRICMP(name, "balloon_multiline") == 0)
13866 n = multiline_balloon_available();
13867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868#ifdef DYNAMIC_TCL
13869 else if (STRICMP(name, "tcl") == 0)
13870 n = tcl_enabled(FALSE);
13871#endif
13872#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13873 else if (STRICMP(name, "iconv") == 0)
13874 n = iconv_enabled(FALSE);
13875#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013876#ifdef DYNAMIC_LUA
13877 else if (STRICMP(name, "lua") == 0)
13878 n = lua_enabled(FALSE);
13879#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013880#ifdef DYNAMIC_MZSCHEME
13881 else if (STRICMP(name, "mzscheme") == 0)
13882 n = mzscheme_enabled(FALSE);
13883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884#ifdef DYNAMIC_RUBY
13885 else if (STRICMP(name, "ruby") == 0)
13886 n = ruby_enabled(FALSE);
13887#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013888#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889#ifdef DYNAMIC_PYTHON
13890 else if (STRICMP(name, "python") == 0)
13891 n = python_enabled(FALSE);
13892#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013893#endif
13894#ifdef FEAT_PYTHON3
13895#ifdef DYNAMIC_PYTHON3
13896 else if (STRICMP(name, "python3") == 0)
13897 n = python3_enabled(FALSE);
13898#endif
13899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900#ifdef DYNAMIC_PERL
13901 else if (STRICMP(name, "perl") == 0)
13902 n = perl_enabled(FALSE);
13903#endif
13904#ifdef FEAT_GUI
13905 else if (STRICMP(name, "gui_running") == 0)
13906 n = (gui.in_use || gui.starting);
13907# ifdef FEAT_GUI_W32
13908 else if (STRICMP(name, "gui_win32s") == 0)
13909 n = gui_is_win32s();
13910# endif
13911# ifdef FEAT_BROWSE
13912 else if (STRICMP(name, "browse") == 0)
13913 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13914# endif
13915#endif
13916#ifdef FEAT_SYN_HL
13917 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013918 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919#endif
13920#if defined(WIN3264)
13921 else if (STRICMP(name, "win95") == 0)
13922 n = mch_windows95();
13923#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013924#ifdef FEAT_NETBEANS_INTG
13925 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013926 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 }
13929
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013930 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931}
13932
13933/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013934 * "has_key()" function
13935 */
13936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013937f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013938{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013939 if (argvars[0].v_type != VAR_DICT)
13940 {
13941 EMSG(_(e_dictreq));
13942 return;
13943 }
13944 if (argvars[0].vval.v_dict == NULL)
13945 return;
13946
13947 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013948 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013949}
13950
13951/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013952 * "haslocaldir()" function
13953 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013955f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013956{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013957 win_T *wp = NULL;
13958
13959 wp = find_tabwin(&argvars[0], &argvars[1]);
13960 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013961}
13962
13963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964 * "hasmapto()" function
13965 */
13966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013967f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968{
13969 char_u *name;
13970 char_u *mode;
13971 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013972 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013975 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 mode = (char_u *)"nvo";
13977 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013980 if (argvars[2].v_type != VAR_UNKNOWN)
13981 abbr = get_tv_number(&argvars[2]);
13982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983
Bram Moolenaar2c932302006-03-18 21:42:09 +000013984 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013987 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988}
13989
13990/*
13991 * "histadd()" function
13992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013994f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995{
13996#ifdef FEAT_CMDHIST
13997 int histype;
13998 char_u *str;
13999 char_u buf[NUMBUFLEN];
14000#endif
14001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014002 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 if (check_restricted() || check_secure())
14004 return;
14005#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014006 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14007 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 if (histype >= 0)
14009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014010 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 if (*str != NUL)
14012 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014013 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014015 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 return;
14017 }
14018 }
14019#endif
14020}
14021
14022/*
14023 * "histdel()" function
14024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014026f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027{
14028#ifdef FEAT_CMDHIST
14029 int n;
14030 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014031 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014033 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14034 if (str == NULL)
14035 n = 0;
14036 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014038 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014039 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 else
14044 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014046 get_tv_string_buf(&argvars[1], buf));
14047 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048#endif
14049}
14050
14051/*
14052 * "histget()" function
14053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014055f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056{
14057#ifdef FEAT_CMDHIST
14058 int type;
14059 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014062 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14063 if (str == NULL)
14064 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014066 {
14067 type = get_histtype(str);
14068 if (argvars[1].v_type == VAR_UNKNOWN)
14069 idx = get_history_idx(type);
14070 else
14071 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14072 /* -1 on type error */
14073 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079}
14080
14081/*
14082 * "histnr()" function
14083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014085f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086{
14087 int i;
14088
14089#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014090 char_u *history = get_tv_string_chk(&argvars[0]);
14091
14092 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 if (i >= HIST_CMD && i < HIST_COUNT)
14094 i = get_history_idx(i);
14095 else
14096#endif
14097 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014098 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099}
14100
14101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 * "highlightID(name)" function
14103 */
14104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014105f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108}
14109
14110/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014111 * "highlight_exists()" function
14112 */
14113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014114f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014115{
14116 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14117}
14118
14119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 * "hostname()" function
14121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014123f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124{
14125 char_u hostname[256];
14126
14127 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014128 rettv->v_type = VAR_STRING;
14129 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130}
14131
14132/*
14133 * iconv() function
14134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014136f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137{
14138#ifdef FEAT_MBYTE
14139 char_u buf1[NUMBUFLEN];
14140 char_u buf2[NUMBUFLEN];
14141 char_u *from, *to, *str;
14142 vimconv_T vimconv;
14143#endif
14144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014145 rettv->v_type = VAR_STRING;
14146 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147
14148#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 str = get_tv_string(&argvars[0]);
14150 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14151 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 vimconv.vc_type = CONV_NONE;
14153 convert_setup(&vimconv, from, to);
14154
14155 /* If the encodings are equal, no conversion needed. */
14156 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160
14161 convert_setup(&vimconv, NULL, NULL);
14162 vim_free(from);
14163 vim_free(to);
14164#endif
14165}
14166
14167/*
14168 * "indent()" function
14169 */
14170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014171f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172{
14173 linenr_T lnum;
14174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014177 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180}
14181
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014182/*
14183 * "index()" function
14184 */
14185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014186f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014187{
Bram Moolenaar33570922005-01-25 22:26:29 +000014188 list_T *l;
14189 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014190 long idx = 0;
14191 int ic = FALSE;
14192
14193 rettv->vval.v_number = -1;
14194 if (argvars[0].v_type != VAR_LIST)
14195 {
14196 EMSG(_(e_listreq));
14197 return;
14198 }
14199 l = argvars[0].vval.v_list;
14200 if (l != NULL)
14201 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014202 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014203 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 int error = FALSE;
14206
Bram Moolenaar758711c2005-02-02 23:11:38 +000014207 /* Start at specified item. Use the cached index that list_find()
14208 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014210 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014211 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 ic = get_tv_number_chk(&argvars[3], &error);
14213 if (error)
14214 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014215 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014216
Bram Moolenaar758711c2005-02-02 23:11:38 +000014217 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014218 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014219 {
14220 rettv->vval.v_number = idx;
14221 break;
14222 }
14223 }
14224}
14225
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226static int inputsecret_flag = 0;
14227
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014228static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014229
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014231 * This function is used by f_input() and f_inputdialog() functions. The third
14232 * argument to f_input() specifies the type of completion to use at the
14233 * prompt. The third argument to f_inputdialog() specifies the value to return
14234 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 */
14236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014237get_user_input(
14238 typval_T *argvars,
14239 typval_T *rettv,
14240 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 char_u *p = NULL;
14244 int c;
14245 char_u buf[NUMBUFLEN];
14246 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014248 int xp_type = EXPAND_NOTHING;
14249 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014251 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253
14254#ifdef NO_CONSOLE_INPUT
14255 /* While starting up, there is no place to enter text. */
14256 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258#endif
14259
14260 cmd_silent = FALSE; /* Want to see the prompt. */
14261 if (prompt != NULL)
14262 {
14263 /* Only the part of the message after the last NL is considered as
14264 * prompt for the command line */
14265 p = vim_strrchr(prompt, '\n');
14266 if (p == NULL)
14267 p = prompt;
14268 else
14269 {
14270 ++p;
14271 c = *p;
14272 *p = NUL;
14273 msg_start();
14274 msg_clr_eos();
14275 msg_puts_attr(prompt, echo_attr);
14276 msg_didout = FALSE;
14277 msg_starthere();
14278 *p = c;
14279 }
14280 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014282 if (argvars[1].v_type != VAR_UNKNOWN)
14283 {
14284 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14285 if (defstr != NULL)
14286 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014288 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014289 {
14290 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014291 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014292 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014293
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014294 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014295 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014296
Bram Moolenaar4463f292005-09-25 22:20:24 +000014297 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14298 if (xp_name == NULL)
14299 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014300
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014301 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014302
Bram Moolenaar4463f292005-09-25 22:20:24 +000014303 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14304 &xp_arg) == FAIL)
14305 return;
14306 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014307 }
14308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014310 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014311 int save_ex_normal_busy = ex_normal_busy;
14312 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014313 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014314 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14315 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014316 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014317 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014318 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014319 && argvars[1].v_type != VAR_UNKNOWN
14320 && argvars[2].v_type != VAR_UNKNOWN)
14321 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14322 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014323
14324 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014326 /* since the user typed this, no need to wait for return */
14327 need_wait_return = FALSE;
14328 msg_didout = FALSE;
14329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 cmd_silent = cmd_silent_save;
14331}
14332
14333/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014334 * "input()" function
14335 * Also handles inputsecret() when inputsecret is set.
14336 */
14337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014338f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014339{
14340 get_user_input(argvars, rettv, FALSE);
14341}
14342
14343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 * "inputdialog()" function
14345 */
14346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014347f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348{
14349#if defined(FEAT_GUI_TEXTDIALOG)
14350 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14351 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14352 {
14353 char_u *message;
14354 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 message = get_tv_string_chk(&argvars[0]);
14358 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014359 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014360 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 else
14362 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 if (message != NULL && defstr != NULL
14364 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014365 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 else
14368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 if (message != NULL && defstr != NULL
14370 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014371 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372 rettv->vval.v_string = vim_strsave(
14373 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 }
14379 else
14380#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014381 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382}
14383
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014384/*
14385 * "inputlist()" function
14386 */
14387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014388f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014389{
14390 listitem_T *li;
14391 int selected;
14392 int mouse_used;
14393
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014394#ifdef NO_CONSOLE_INPUT
14395 /* While starting up, there is no place to enter text. */
14396 if (no_console_input())
14397 return;
14398#endif
14399 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14400 {
14401 EMSG2(_(e_listarg), "inputlist()");
14402 return;
14403 }
14404
14405 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014406 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014407 lines_left = Rows; /* avoid more prompt */
14408 msg_scroll = TRUE;
14409 msg_clr_eos();
14410
14411 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14412 {
14413 msg_puts(get_tv_string(&li->li_tv));
14414 msg_putchar('\n');
14415 }
14416
14417 /* Ask for choice. */
14418 selected = prompt_for_number(&mouse_used);
14419 if (mouse_used)
14420 selected -= lines_left;
14421
14422 rettv->vval.v_number = selected;
14423}
14424
14425
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14427
14428/*
14429 * "inputrestore()" function
14430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014432f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433{
14434 if (ga_userinput.ga_len > 0)
14435 {
14436 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14438 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014439 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 }
14441 else if (p_verbose > 1)
14442 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014443 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014444 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 }
14446}
14447
14448/*
14449 * "inputsave()" function
14450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014452f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014454 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 if (ga_grow(&ga_userinput, 1) == OK)
14456 {
14457 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14458 + ga_userinput.ga_len);
14459 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014460 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 }
14462 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014463 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464}
14465
14466/*
14467 * "inputsecret()" function
14468 */
14469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014470f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471{
14472 ++cmdline_star;
14473 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 --cmdline_star;
14476 --inputsecret_flag;
14477}
14478
14479/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014480 * "insert()" function
14481 */
14482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014483f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014484{
14485 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014486 listitem_T *item;
14487 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014488 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014489
14490 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014492 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014493 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014494 {
14495 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 before = get_tv_number_chk(&argvars[2], &error);
14497 if (error)
14498 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014499
Bram Moolenaar758711c2005-02-02 23:11:38 +000014500 if (before == l->lv_len)
14501 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014502 else
14503 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014504 item = list_find(l, before);
14505 if (item == NULL)
14506 {
14507 EMSGN(_(e_listidx), before);
14508 l = NULL;
14509 }
14510 }
14511 if (l != NULL)
14512 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014513 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014514 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014515 }
14516 }
14517}
14518
14519/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014520 * "invert(expr)" function
14521 */
14522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014523f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014524{
14525 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14526}
14527
14528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 * "isdirectory()" function
14530 */
14531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014532f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535}
14536
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014537/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014538 * "islocked()" function
14539 */
14540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014541f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014542{
14543 lval_T lv;
14544 char_u *end;
14545 dictitem_T *di;
14546
14547 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014548 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14549 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014550 if (end != NULL && lv.ll_name != NULL)
14551 {
14552 if (*end != NUL)
14553 EMSG(_(e_trailing));
14554 else
14555 {
14556 if (lv.ll_tv == NULL)
14557 {
14558 if (check_changedtick(lv.ll_name))
14559 rettv->vval.v_number = 1; /* always locked */
14560 else
14561 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014562 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014563 if (di != NULL)
14564 {
14565 /* Consider a variable locked when:
14566 * 1. the variable itself is locked
14567 * 2. the value of the variable is locked.
14568 * 3. the List or Dict value is locked.
14569 */
14570 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14571 || tv_islocked(&di->di_tv));
14572 }
14573 }
14574 }
14575 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014576 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014577 else if (lv.ll_newkey != NULL)
14578 EMSG2(_(e_dictkey), lv.ll_newkey);
14579 else if (lv.ll_list != NULL)
14580 /* List item. */
14581 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14582 else
14583 /* Dictionary item. */
14584 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14585 }
14586 }
14587
14588 clear_lval(&lv);
14589}
14590
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014591#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14592/*
14593 * "isnan()" function
14594 */
14595 static void
14596f_isnan(typval_T *argvars, typval_T *rettv)
14597{
14598 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14599 && isnan(argvars[0].vval.v_float);
14600}
14601#endif
14602
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014603static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014604
14605/*
14606 * Turn a dict into a list:
14607 * "what" == 0: list of keys
14608 * "what" == 1: list of values
14609 * "what" == 2: list of items
14610 */
14611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014612dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014613{
Bram Moolenaar33570922005-01-25 22:26:29 +000014614 list_T *l2;
14615 dictitem_T *di;
14616 hashitem_T *hi;
14617 listitem_T *li;
14618 listitem_T *li2;
14619 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014620 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014621
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622 if (argvars[0].v_type != VAR_DICT)
14623 {
14624 EMSG(_(e_dictreq));
14625 return;
14626 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014627 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 return;
14629
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014630 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014631 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014632
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014633 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014634 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014636 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014638 --todo;
14639 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014640
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014641 li = listitem_alloc();
14642 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014643 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014644 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014645
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014646 if (what == 0)
14647 {
14648 /* keys() */
14649 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014650 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014651 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14652 }
14653 else if (what == 1)
14654 {
14655 /* values() */
14656 copy_tv(&di->di_tv, &li->li_tv);
14657 }
14658 else
14659 {
14660 /* items() */
14661 l2 = list_alloc();
14662 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014663 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014664 li->li_tv.vval.v_list = l2;
14665 if (l2 == NULL)
14666 break;
14667 ++l2->lv_refcount;
14668
14669 li2 = listitem_alloc();
14670 if (li2 == NULL)
14671 break;
14672 list_append(l2, li2);
14673 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014674 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014675 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14676
14677 li2 = listitem_alloc();
14678 if (li2 == NULL)
14679 break;
14680 list_append(l2, li2);
14681 copy_tv(&di->di_tv, &li2->li_tv);
14682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014683 }
14684 }
14685}
14686
14687/*
14688 * "items(dict)" function
14689 */
14690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014691f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014692{
14693 dict_list(argvars, rettv, 2);
14694}
14695
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014696#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014697/*
14698 * Get the job from the argument.
14699 * Returns NULL if the job is invalid.
14700 */
14701 static job_T *
14702get_job_arg(typval_T *tv)
14703{
14704 job_T *job;
14705
14706 if (tv->v_type != VAR_JOB)
14707 {
14708 EMSG2(_(e_invarg2), get_tv_string(tv));
14709 return NULL;
14710 }
14711 job = tv->vval.v_job;
14712
14713 if (job == NULL)
14714 EMSG(_("E916: not a valid job"));
14715 return job;
14716}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014717
Bram Moolenaar835dc632016-02-07 14:27:38 +010014718/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014719 * "job_getchannel()" function
14720 */
14721 static void
14722f_job_getchannel(typval_T *argvars, typval_T *rettv)
14723{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014724 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014725
Bram Moolenaar65edff82016-02-21 16:40:11 +010014726 if (job != NULL)
14727 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014728 rettv->v_type = VAR_CHANNEL;
14729 rettv->vval.v_channel = job->jv_channel;
14730 if (job->jv_channel != NULL)
14731 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014732 }
14733}
14734
14735/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014736 * "job_info()" function
14737 */
14738 static void
14739f_job_info(typval_T *argvars, typval_T *rettv)
14740{
14741 job_T *job = get_job_arg(&argvars[0]);
14742
14743 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14744 job_info(job, rettv->vval.v_dict);
14745}
14746
14747/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014748 * "job_setoptions()" function
14749 */
14750 static void
14751f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14752{
14753 job_T *job = get_job_arg(&argvars[0]);
14754 jobopt_T opt;
14755
14756 if (job == NULL)
14757 return;
14758 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014759 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014760 return;
14761 job_set_options(job, &opt);
14762}
14763
14764/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014765 * "job_start()" function
14766 */
14767 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014768f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014769{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014770 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014771 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014772}
14773
14774/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014775 * "job_status()" function
14776 */
14777 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014778f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014779{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014780 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014781
Bram Moolenaar65edff82016-02-21 16:40:11 +010014782 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014783 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014784 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014785 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014786 }
14787}
14788
14789/*
14790 * "job_stop()" function
14791 */
14792 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014793f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014794{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014795 job_T *job = get_job_arg(&argvars[0]);
14796
14797 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014798 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014799}
14800#endif
14801
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014803 * "join()" function
14804 */
14805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014806f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014807{
14808 garray_T ga;
14809 char_u *sep;
14810
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014811 if (argvars[0].v_type != VAR_LIST)
14812 {
14813 EMSG(_(e_listreq));
14814 return;
14815 }
14816 if (argvars[0].vval.v_list == NULL)
14817 return;
14818 if (argvars[1].v_type == VAR_UNKNOWN)
14819 sep = (char_u *)" ";
14820 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014821 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014822
14823 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014824
14825 if (sep != NULL)
14826 {
14827 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014828 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014829 ga_append(&ga, NUL);
14830 rettv->vval.v_string = (char_u *)ga.ga_data;
14831 }
14832 else
14833 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014834}
14835
14836/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014837 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014838 */
14839 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014840f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014841{
14842 js_read_T reader;
14843
14844 reader.js_buf = get_tv_string(&argvars[0]);
14845 reader.js_fill = NULL;
14846 reader.js_used = 0;
14847 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14848 EMSG(_(e_invarg));
14849}
14850
14851/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014852 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014853 */
14854 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014855f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014856{
14857 rettv->v_type = VAR_STRING;
14858 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14859}
14860
14861/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014862 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014863 */
14864 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014865f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014866{
14867 js_read_T reader;
14868
14869 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014870 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014871 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014872 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014873 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014874}
14875
14876/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014877 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014878 */
14879 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014880f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014881{
14882 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014883 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014884}
14885
14886/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014887 * "keys()" function
14888 */
14889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014890f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014891{
14892 dict_list(argvars, rettv, 0);
14893}
14894
14895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896 * "last_buffer_nr()" function.
14897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014899f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900{
14901 int n = 0;
14902 buf_T *buf;
14903
14904 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14905 if (n < buf->b_fnum)
14906 n = buf->b_fnum;
14907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014909}
14910
14911/*
14912 * "len()" function
14913 */
14914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014915f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014916{
14917 switch (argvars[0].v_type)
14918 {
14919 case VAR_STRING:
14920 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014921 rettv->vval.v_number = (varnumber_T)STRLEN(
14922 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014923 break;
14924 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014926 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014927 case VAR_DICT:
14928 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14929 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014930 case VAR_UNKNOWN:
14931 case VAR_SPECIAL:
14932 case VAR_FLOAT:
14933 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014934 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014935 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014936 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014937 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014938 break;
14939 }
14940}
14941
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014942static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014943
14944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014945libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014946{
14947#ifdef FEAT_LIBCALL
14948 char_u *string_in;
14949 char_u **string_result;
14950 int nr_result;
14951#endif
14952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014954 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014956
14957 if (check_restricted() || check_secure())
14958 return;
14959
14960#ifdef FEAT_LIBCALL
14961 /* The first two args must be strings, otherwise its meaningless */
14962 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14963 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014964 string_in = NULL;
14965 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014966 string_in = argvars[2].vval.v_string;
14967 if (type == VAR_NUMBER)
14968 string_result = NULL;
14969 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014970 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014971 if (mch_libcall(argvars[0].vval.v_string,
14972 argvars[1].vval.v_string,
14973 string_in,
14974 argvars[2].vval.v_number,
14975 string_result,
14976 &nr_result) == OK
14977 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014979 }
14980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981}
14982
14983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 * "libcall()" function
14985 */
14986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014987f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988{
14989 libcall_common(argvars, rettv, VAR_STRING);
14990}
14991
14992/*
14993 * "libcallnr()" function
14994 */
14995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014996f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997{
14998 libcall_common(argvars, rettv, VAR_NUMBER);
14999}
15000
15001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 * "line(string)" function
15003 */
15004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015005f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006{
15007 linenr_T lnum = 0;
15008 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015009 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015011 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012 if (fp != NULL)
15013 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015}
15016
15017/*
15018 * "line2byte(lnum)" function
15019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015021f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022{
15023#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025#else
15026 linenr_T lnum;
15027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015030 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15033 if (rettv->vval.v_number >= 0)
15034 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035#endif
15036}
15037
15038/*
15039 * "lispindent(lnum)" function
15040 */
15041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015042f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043{
15044#ifdef FEAT_LISP
15045 pos_T pos;
15046 linenr_T lnum;
15047
15048 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015049 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15051 {
15052 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 curwin->w_cursor = pos;
15055 }
15056 else
15057#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015058 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059}
15060
15061/*
15062 * "localtime()" function
15063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015065f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015067 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068}
15069
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015070static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071
15072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015073get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074{
15075 char_u *keys;
15076 char_u *which;
15077 char_u buf[NUMBUFLEN];
15078 char_u *keys_buf = NULL;
15079 char_u *rhs;
15080 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015081 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015082 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015083 mapblock_T *mp;
15084 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085
15086 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015087 rettv->v_type = VAR_STRING;
15088 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091 if (*keys == NUL)
15092 return;
15093
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015094 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015095 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015096 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015097 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015098 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015099 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015100 if (argvars[3].v_type != VAR_UNKNOWN)
15101 get_dict = get_tv_number(&argvars[3]);
15102 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 else
15105 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015106 if (which == NULL)
15107 return;
15108
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 mode = get_map_mode(&which, 0);
15110
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015111 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015112 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015114
15115 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015117 /* Return a string. */
15118 if (rhs != NULL)
15119 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120
Bram Moolenaarbd743252010-10-20 21:23:33 +020015121 }
15122 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15123 {
15124 /* Return a dictionary. */
15125 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15126 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15127 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128
Bram Moolenaarbd743252010-10-20 21:23:33 +020015129 dict_add_nr_str(dict, "lhs", 0L, lhs);
15130 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15131 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15132 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15133 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15134 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15135 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015136 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015137 dict_add_nr_str(dict, "mode", 0L, mapmode);
15138
15139 vim_free(lhs);
15140 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 }
15142}
15143
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015144#ifdef FEAT_FLOAT
15145/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015146 * "log()" function
15147 */
15148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015149f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015150{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015151 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015152
15153 rettv->v_type = VAR_FLOAT;
15154 if (get_float_arg(argvars, &f) == OK)
15155 rettv->vval.v_float = log(f);
15156 else
15157 rettv->vval.v_float = 0.0;
15158}
15159
15160/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015161 * "log10()" function
15162 */
15163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015164f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015165{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015166 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015167
15168 rettv->v_type = VAR_FLOAT;
15169 if (get_float_arg(argvars, &f) == OK)
15170 rettv->vval.v_float = log10(f);
15171 else
15172 rettv->vval.v_float = 0.0;
15173}
15174#endif
15175
Bram Moolenaar1dced572012-04-05 16:54:08 +020015176#ifdef FEAT_LUA
15177/*
15178 * "luaeval()" function
15179 */
15180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015181f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015182{
15183 char_u *str;
15184 char_u buf[NUMBUFLEN];
15185
15186 str = get_tv_string_buf(&argvars[0], buf);
15187 do_luaeval(str, argvars + 1, rettv);
15188}
15189#endif
15190
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015192 * "map()" function
15193 */
15194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015195f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015196{
15197 filter_map(argvars, rettv, TRUE);
15198}
15199
15200/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 */
15203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015204f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207}
15208
15209/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015210 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 */
15212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015213f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015215 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216}
15217
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015218static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219
15220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015221find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015223 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015224 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015225 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 char_u *pat;
15227 regmatch_T regmatch;
15228 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015229 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 char_u *save_cpo;
15231 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015232 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015233 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015234 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 list_T *l = NULL;
15236 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015237 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015238 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239
15240 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15241 save_cpo = p_cpo;
15242 p_cpo = (char_u *)"";
15243
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015244 rettv->vval.v_number = -1;
15245 if (type == 3)
15246 {
15247 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015248 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015249 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015250 }
15251 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015253 rettv->v_type = VAR_STRING;
15254 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015257 if (argvars[0].v_type == VAR_LIST)
15258 {
15259 if ((l = argvars[0].vval.v_list) == NULL)
15260 goto theend;
15261 li = l->lv_first;
15262 }
15263 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015264 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015265 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015266 len = (long)STRLEN(str);
15267 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015269 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15270 if (pat == NULL)
15271 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015272
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015273 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015275 int error = FALSE;
15276
15277 start = get_tv_number_chk(&argvars[2], &error);
15278 if (error)
15279 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015280 if (l != NULL)
15281 {
15282 li = list_find(l, start);
15283 if (li == NULL)
15284 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015285 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015286 }
15287 else
15288 {
15289 if (start < 0)
15290 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015291 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015292 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015293 /* When "count" argument is there ignore matches before "start",
15294 * otherwise skip part of the string. Differs when pattern is "^"
15295 * or "\<". */
15296 if (argvars[3].v_type != VAR_UNKNOWN)
15297 startcol = start;
15298 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015299 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015300 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015301 len -= start;
15302 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015303 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015304
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015305 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015306 nth = get_tv_number_chk(&argvars[3], &error);
15307 if (error)
15308 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 }
15310
15311 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15312 if (regmatch.regprog != NULL)
15313 {
15314 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015315
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015316 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015317 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015318 if (l != NULL)
15319 {
15320 if (li == NULL)
15321 {
15322 match = FALSE;
15323 break;
15324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015325 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015326 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015327 if (str == NULL)
15328 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015329 }
15330
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015331 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015332
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015333 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015334 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015335 if (l == NULL && !match)
15336 break;
15337
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015338 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015339 if (l != NULL)
15340 {
15341 li = li->li_next;
15342 ++idx;
15343 }
15344 else
15345 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015346#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015347 startcol = (colnr_T)(regmatch.startp[0]
15348 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015349#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015350 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015351#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015352 if (startcol > (colnr_T)len
15353 || str + startcol <= regmatch.startp[0])
15354 {
15355 match = FALSE;
15356 break;
15357 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015358 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015359 }
15360
15361 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015363 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015364 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015365 int i;
15366
15367 /* return list with matched string and submatches */
15368 for (i = 0; i < NSUBEXP; ++i)
15369 {
15370 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015371 {
15372 if (list_append_string(rettv->vval.v_list,
15373 (char_u *)"", 0) == FAIL)
15374 break;
15375 }
15376 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015377 regmatch.startp[i],
15378 (int)(regmatch.endp[i] - regmatch.startp[i]))
15379 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015380 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015381 }
15382 }
15383 else if (type == 2)
15384 {
15385 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015386 if (l != NULL)
15387 copy_tv(&li->li_tv, rettv);
15388 else
15389 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015391 }
15392 else if (l != NULL)
15393 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 else
15395 {
15396 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 (varnumber_T)(regmatch.startp[0] - str);
15399 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015402 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 }
15404 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015405 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 }
15407
15408theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015409 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 p_cpo = save_cpo;
15411}
15412
15413/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015414 * "match()" function
15415 */
15416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015417f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015418{
15419 find_some_match(argvars, rettv, 1);
15420}
15421
15422/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015423 * "matchadd()" function
15424 */
15425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015426f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015427{
15428#ifdef FEAT_SEARCH_EXTRA
15429 char_u buf[NUMBUFLEN];
15430 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15431 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15432 int prio = 10; /* default priority */
15433 int id = -1;
15434 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015435 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015436
15437 rettv->vval.v_number = -1;
15438
15439 if (grp == NULL || pat == NULL)
15440 return;
15441 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015442 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015443 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015444 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015445 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015446 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015447 if (argvars[4].v_type != VAR_UNKNOWN)
15448 {
15449 if (argvars[4].v_type != VAR_DICT)
15450 {
15451 EMSG(_(e_dictreq));
15452 return;
15453 }
15454 if (dict_find(argvars[4].vval.v_dict,
15455 (char_u *)"conceal", -1) != NULL)
15456 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15457 (char_u *)"conceal", FALSE);
15458 }
15459 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015460 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015461 if (error == TRUE)
15462 return;
15463 if (id >= 1 && id <= 3)
15464 {
15465 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15466 return;
15467 }
15468
Bram Moolenaar6561d522015-07-21 15:48:27 +020015469 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15470 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015471#endif
15472}
15473
15474/*
15475 * "matchaddpos()" function
15476 */
15477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015478f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015479{
15480#ifdef FEAT_SEARCH_EXTRA
15481 char_u buf[NUMBUFLEN];
15482 char_u *group;
15483 int prio = 10;
15484 int id = -1;
15485 int error = FALSE;
15486 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015487 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015488
15489 rettv->vval.v_number = -1;
15490
15491 group = get_tv_string_buf_chk(&argvars[0], buf);
15492 if (group == NULL)
15493 return;
15494
15495 if (argvars[1].v_type != VAR_LIST)
15496 {
15497 EMSG2(_(e_listarg), "matchaddpos()");
15498 return;
15499 }
15500 l = argvars[1].vval.v_list;
15501 if (l == NULL)
15502 return;
15503
15504 if (argvars[2].v_type != VAR_UNKNOWN)
15505 {
15506 prio = get_tv_number_chk(&argvars[2], &error);
15507 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015508 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015509 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015510 if (argvars[4].v_type != VAR_UNKNOWN)
15511 {
15512 if (argvars[4].v_type != VAR_DICT)
15513 {
15514 EMSG(_(e_dictreq));
15515 return;
15516 }
15517 if (dict_find(argvars[4].vval.v_dict,
15518 (char_u *)"conceal", -1) != NULL)
15519 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15520 (char_u *)"conceal", FALSE);
15521 }
15522 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015523 }
15524 if (error == TRUE)
15525 return;
15526
15527 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15528 if (id == 1 || id == 2)
15529 {
15530 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15531 return;
15532 }
15533
Bram Moolenaar6561d522015-07-21 15:48:27 +020015534 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15535 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015536#endif
15537}
15538
15539/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015540 * "matcharg()" function
15541 */
15542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015543f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015544{
15545 if (rettv_list_alloc(rettv) == OK)
15546 {
15547#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015548 int id = get_tv_number(&argvars[0]);
15549 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015550
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015551 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015552 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015553 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15554 {
15555 list_append_string(rettv->vval.v_list,
15556 syn_id2name(m->hlg_id), -1);
15557 list_append_string(rettv->vval.v_list, m->pattern, -1);
15558 }
15559 else
15560 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015561 list_append_string(rettv->vval.v_list, NULL, -1);
15562 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015563 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015564 }
15565#endif
15566 }
15567}
15568
15569/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015570 * "matchdelete()" function
15571 */
15572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015573f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015574{
15575#ifdef FEAT_SEARCH_EXTRA
15576 rettv->vval.v_number = match_delete(curwin,
15577 (int)get_tv_number(&argvars[0]), TRUE);
15578#endif
15579}
15580
15581/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 * "matchend()" function
15583 */
15584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015585f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015586{
15587 find_some_match(argvars, rettv, 0);
15588}
15589
15590/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015591 * "matchlist()" function
15592 */
15593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015594f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015595{
15596 find_some_match(argvars, rettv, 3);
15597}
15598
15599/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600 * "matchstr()" function
15601 */
15602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015603f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604{
15605 find_some_match(argvars, rettv, 2);
15606}
15607
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015608static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015609
15610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015611max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015612{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015613 long n = 0;
15614 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015615 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015616
15617 if (argvars[0].v_type == VAR_LIST)
15618 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015619 list_T *l;
15620 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015621
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015622 l = argvars[0].vval.v_list;
15623 if (l != NULL)
15624 {
15625 li = l->lv_first;
15626 if (li != NULL)
15627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015628 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015629 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015630 {
15631 li = li->li_next;
15632 if (li == NULL)
15633 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015634 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015635 if (domax ? i > n : i < n)
15636 n = i;
15637 }
15638 }
15639 }
15640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015641 else if (argvars[0].v_type == VAR_DICT)
15642 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015643 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015644 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015645 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015646 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015647
15648 d = argvars[0].vval.v_dict;
15649 if (d != NULL)
15650 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015651 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015652 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015653 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015654 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015655 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015656 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015657 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015658 if (first)
15659 {
15660 n = i;
15661 first = FALSE;
15662 }
15663 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015664 n = i;
15665 }
15666 }
15667 }
15668 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015669 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015670 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015671 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015672}
15673
15674/*
15675 * "max()" function
15676 */
15677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015678f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015679{
15680 max_min(argvars, rettv, TRUE);
15681}
15682
15683/*
15684 * "min()" function
15685 */
15686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015687f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015688{
15689 max_min(argvars, rettv, FALSE);
15690}
15691
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015692static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015693
15694/*
15695 * Create the directory in which "dir" is located, and higher levels when
15696 * needed.
15697 */
15698 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015699mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015700{
15701 char_u *p;
15702 char_u *updir;
15703 int r = FAIL;
15704
15705 /* Get end of directory name in "dir".
15706 * We're done when it's "/" or "c:/". */
15707 p = gettail_sep(dir);
15708 if (p <= get_past_head(dir))
15709 return OK;
15710
15711 /* If the directory exists we're done. Otherwise: create it.*/
15712 updir = vim_strnsave(dir, (int)(p - dir));
15713 if (updir == NULL)
15714 return FAIL;
15715 if (mch_isdir(updir))
15716 r = OK;
15717 else if (mkdir_recurse(updir, prot) == OK)
15718 r = vim_mkdir_emsg(updir, prot);
15719 vim_free(updir);
15720 return r;
15721}
15722
15723#ifdef vim_mkdir
15724/*
15725 * "mkdir()" function
15726 */
15727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015728f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015729{
15730 char_u *dir;
15731 char_u buf[NUMBUFLEN];
15732 int prot = 0755;
15733
15734 rettv->vval.v_number = FAIL;
15735 if (check_restricted() || check_secure())
15736 return;
15737
15738 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015739 if (*dir == NUL)
15740 rettv->vval.v_number = FAIL;
15741 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015742 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015743 if (*gettail(dir) == NUL)
15744 /* remove trailing slashes */
15745 *gettail_sep(dir) = NUL;
15746
15747 if (argvars[1].v_type != VAR_UNKNOWN)
15748 {
15749 if (argvars[2].v_type != VAR_UNKNOWN)
15750 prot = get_tv_number_chk(&argvars[2], NULL);
15751 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15752 mkdir_recurse(dir, prot);
15753 }
15754 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015755 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015756}
15757#endif
15758
Bram Moolenaar0d660222005-01-07 21:51:51 +000015759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 * "mode()" function
15761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015763f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015765 char_u buf[3];
15766
15767 buf[1] = NUL;
15768 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 if (VIsual_active)
15771 {
15772 if (VIsual_select)
15773 buf[0] = VIsual_mode + 's' - 'v';
15774 else
15775 buf[0] = VIsual_mode;
15776 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015777 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015778 || State == CONFIRM)
15779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015781 if (State == ASKMORE)
15782 buf[1] = 'm';
15783 else if (State == CONFIRM)
15784 buf[1] = '?';
15785 }
15786 else if (State == EXTERNCMD)
15787 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 else if (State & INSERT)
15789 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015790#ifdef FEAT_VREPLACE
15791 if (State & VREPLACE_FLAG)
15792 {
15793 buf[0] = 'R';
15794 buf[1] = 'v';
15795 }
15796 else
15797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 if (State & REPLACE_FLAG)
15799 buf[0] = 'R';
15800 else
15801 buf[0] = 'i';
15802 }
15803 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015806 if (exmode_active)
15807 buf[1] = 'v';
15808 }
15809 else if (exmode_active)
15810 {
15811 buf[0] = 'c';
15812 buf[1] = 'e';
15813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015817 if (finish_op)
15818 buf[1] = 'o';
15819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015821 /* Clear out the minor mode when the argument is not a non-zero number or
15822 * non-empty string. */
15823 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015824 buf[1] = NUL;
15825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015826 rettv->vval.v_string = vim_strsave(buf);
15827 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828}
15829
Bram Moolenaar429fa852013-04-15 12:27:36 +020015830#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015831/*
15832 * "mzeval()" function
15833 */
15834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015835f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015836{
15837 char_u *str;
15838 char_u buf[NUMBUFLEN];
15839
15840 str = get_tv_string_buf(&argvars[0], buf);
15841 do_mzeval(str, rettv);
15842}
Bram Moolenaar75676462013-01-30 14:55:42 +010015843
15844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015845mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015846{
15847 typval_T argvars[3];
15848
15849 argvars[0].v_type = VAR_STRING;
15850 argvars[0].vval.v_string = name;
15851 copy_tv(args, &argvars[1]);
15852 argvars[2].v_type = VAR_UNKNOWN;
15853 f_call(argvars, rettv);
15854 clear_tv(&argvars[1]);
15855}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015856#endif
15857
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015859 * "nextnonblank()" function
15860 */
15861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015862f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863{
15864 linenr_T lnum;
15865
15866 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015868 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015869 {
15870 lnum = 0;
15871 break;
15872 }
15873 if (*skipwhite(ml_get(lnum)) != NUL)
15874 break;
15875 }
15876 rettv->vval.v_number = lnum;
15877}
15878
15879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 * "nr2char()" function
15881 */
15882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015883f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884{
15885 char_u buf[NUMBUFLEN];
15886
15887#ifdef FEAT_MBYTE
15888 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015889 {
15890 int utf8 = 0;
15891
15892 if (argvars[1].v_type != VAR_UNKNOWN)
15893 utf8 = get_tv_number_chk(&argvars[1], NULL);
15894 if (utf8)
15895 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15896 else
15897 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899 else
15900#endif
15901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015902 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 buf[1] = NUL;
15904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015905 rettv->v_type = VAR_STRING;
15906 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015907}
15908
15909/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015910 * "or(expr, expr)" function
15911 */
15912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015913f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015914{
15915 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15916 | get_tv_number_chk(&argvars[1], NULL);
15917}
15918
15919/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015920 * "pathshorten()" function
15921 */
15922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015923f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015924{
15925 char_u *p;
15926
15927 rettv->v_type = VAR_STRING;
15928 p = get_tv_string_chk(&argvars[0]);
15929 if (p == NULL)
15930 rettv->vval.v_string = NULL;
15931 else
15932 {
15933 p = vim_strsave(p);
15934 rettv->vval.v_string = p;
15935 if (p != NULL)
15936 shorten_dir(p);
15937 }
15938}
15939
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015940#ifdef FEAT_PERL
15941/*
15942 * "perleval()" function
15943 */
15944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015945f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015946{
15947 char_u *str;
15948 char_u buf[NUMBUFLEN];
15949
15950 str = get_tv_string_buf(&argvars[0], buf);
15951 do_perleval(str, rettv);
15952}
15953#endif
15954
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015955#ifdef FEAT_FLOAT
15956/*
15957 * "pow()" function
15958 */
15959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015960f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015961{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015962 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015963
15964 rettv->v_type = VAR_FLOAT;
15965 if (get_float_arg(argvars, &fx) == OK
15966 && get_float_arg(&argvars[1], &fy) == OK)
15967 rettv->vval.v_float = pow(fx, fy);
15968 else
15969 rettv->vval.v_float = 0.0;
15970}
15971#endif
15972
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015973/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015974 * "prevnonblank()" function
15975 */
15976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015977f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015978{
15979 linenr_T lnum;
15980
15981 lnum = get_tv_lnum(argvars);
15982 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15983 lnum = 0;
15984 else
15985 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15986 --lnum;
15987 rettv->vval.v_number = lnum;
15988}
15989
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015990/* This dummy va_list is here because:
15991 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15992 * - locally in the function results in a "used before set" warning
15993 * - using va_start() to initialize it gives "function with fixed args" error */
15994static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015995
Bram Moolenaar8c711452005-01-14 21:53:12 +000015996/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015997 * "printf()" function
15998 */
15999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016000f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016001{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016002 char_u buf[NUMBUFLEN];
16003 int len;
16004 char_u *s;
16005 int saved_did_emsg = did_emsg;
16006 char *fmt;
16007
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016008 rettv->v_type = VAR_STRING;
16009 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016010
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016011 /* Get the required length, allocate the buffer and do it for real. */
16012 did_emsg = FALSE;
16013 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16014 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16015 if (!did_emsg)
16016 {
16017 s = alloc(len + 1);
16018 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016019 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016020 rettv->vval.v_string = s;
16021 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016022 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016023 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016024 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016025}
16026
16027/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016028 * "pumvisible()" function
16029 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016031f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016032{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016033#ifdef FEAT_INS_EXPAND
16034 if (pum_visible())
16035 rettv->vval.v_number = 1;
16036#endif
16037}
16038
Bram Moolenaardb913952012-06-29 12:54:53 +020016039#ifdef FEAT_PYTHON3
16040/*
16041 * "py3eval()" function
16042 */
16043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016044f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016045{
16046 char_u *str;
16047 char_u buf[NUMBUFLEN];
16048
16049 str = get_tv_string_buf(&argvars[0], buf);
16050 do_py3eval(str, rettv);
16051}
16052#endif
16053
16054#ifdef FEAT_PYTHON
16055/*
16056 * "pyeval()" function
16057 */
16058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016059f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016060{
16061 char_u *str;
16062 char_u buf[NUMBUFLEN];
16063
16064 str = get_tv_string_buf(&argvars[0], buf);
16065 do_pyeval(str, rettv);
16066}
16067#endif
16068
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016069/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016070 * "range()" function
16071 */
16072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016073f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016074{
16075 long start;
16076 long end;
16077 long stride = 1;
16078 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016079 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016081 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016082 if (argvars[1].v_type == VAR_UNKNOWN)
16083 {
16084 end = start - 1;
16085 start = 0;
16086 }
16087 else
16088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016089 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016090 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016091 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016092 }
16093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016094 if (error)
16095 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016096 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016097 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016098 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016099 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016100 else
16101 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016102 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016103 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016104 if (list_append_number(rettv->vval.v_list,
16105 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016106 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016107 }
16108}
16109
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016110/*
16111 * "readfile()" function
16112 */
16113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016114f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016115{
16116 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016117 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016118 char_u *fname;
16119 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016120 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16121 int io_size = sizeof(buf);
16122 int readlen; /* size of last fread() */
16123 char_u *prev = NULL; /* previously read bytes, if any */
16124 long prevlen = 0; /* length of data in prev */
16125 long prevsize = 0; /* size of prev buffer */
16126 long maxline = MAXLNUM;
16127 long cnt = 0;
16128 char_u *p; /* position in buf */
16129 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016131 if (argvars[1].v_type != VAR_UNKNOWN)
16132 {
16133 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16134 binary = TRUE;
16135 if (argvars[2].v_type != VAR_UNKNOWN)
16136 maxline = get_tv_number(&argvars[2]);
16137 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016138
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016139 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016140 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016141
16142 /* Always open the file in binary mode, library functions have a mind of
16143 * their own about CR-LF conversion. */
16144 fname = get_tv_string(&argvars[0]);
16145 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16146 {
16147 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16148 return;
16149 }
16150
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016151 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016152 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016153 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016154
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016155 /* This for loop processes what was read, but is also entered at end
16156 * of file so that either:
16157 * - an incomplete line gets written
16158 * - a "binary" file gets an empty line at the end if it ends in a
16159 * newline. */
16160 for (p = buf, start = buf;
16161 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16162 ++p)
16163 {
16164 if (*p == '\n' || readlen <= 0)
16165 {
16166 listitem_T *li;
16167 char_u *s = NULL;
16168 long_u len = p - start;
16169
16170 /* Finished a line. Remove CRs before NL. */
16171 if (readlen > 0 && !binary)
16172 {
16173 while (len > 0 && start[len - 1] == '\r')
16174 --len;
16175 /* removal may cross back to the "prev" string */
16176 if (len == 0)
16177 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16178 --prevlen;
16179 }
16180 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016181 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016182 else
16183 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016184 /* Change "prev" buffer to be the right size. This way
16185 * the bytes are only copied once, and very long lines are
16186 * allocated only once. */
16187 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016188 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016189 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016190 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016191 prev = NULL; /* the list will own the string */
16192 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016193 }
16194 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016195 if (s == NULL)
16196 {
16197 do_outofmem_msg((long_u) prevlen + len + 1);
16198 failed = TRUE;
16199 break;
16200 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016201
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016202 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016203 {
16204 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016205 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016206 break;
16207 }
16208 li->li_tv.v_type = VAR_STRING;
16209 li->li_tv.v_lock = 0;
16210 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016211 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016212
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016213 start = p + 1; /* step over newline */
16214 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016215 break;
16216 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016217 else if (*p == NUL)
16218 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016219#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016220 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16221 * when finding the BF and check the previous two bytes. */
16222 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016223 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016224 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16225 * + 1, these may be in the "prev" string. */
16226 char_u back1 = p >= buf + 1 ? p[-1]
16227 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16228 char_u back2 = p >= buf + 2 ? p[-2]
16229 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16230 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016231
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016232 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016233 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016234 char_u *dest = p - 2;
16235
16236 /* Usually a BOM is at the beginning of a file, and so at
16237 * the beginning of a line; then we can just step over it.
16238 */
16239 if (start == dest)
16240 start = p + 1;
16241 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016242 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016243 /* have to shuffle buf to close gap */
16244 int adjust_prevlen = 0;
16245
16246 if (dest < buf)
16247 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016248 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016249 dest = buf;
16250 }
16251 if (readlen > p - buf + 1)
16252 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16253 readlen -= 3 - adjust_prevlen;
16254 prevlen -= adjust_prevlen;
16255 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016256 }
16257 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016258 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016259#endif
16260 } /* for */
16261
16262 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16263 break;
16264 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016265 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016266 /* There's part of a line in buf, store it in "prev". */
16267 if (p - start + prevlen >= prevsize)
16268 {
16269 /* need bigger "prev" buffer */
16270 char_u *newprev;
16271
16272 /* A common use case is ordinary text files and "prev" gets a
16273 * fragment of a line, so the first allocation is made
16274 * small, to avoid repeatedly 'allocing' large and
16275 * 'reallocing' small. */
16276 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016277 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016278 else
16279 {
16280 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016281 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016282 prevsize = grow50pc > growmin ? grow50pc : growmin;
16283 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016284 newprev = prev == NULL ? alloc(prevsize)
16285 : vim_realloc(prev, prevsize);
16286 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016287 {
16288 do_outofmem_msg((long_u)prevsize);
16289 failed = TRUE;
16290 break;
16291 }
16292 prev = newprev;
16293 }
16294 /* Add the line part to end of "prev". */
16295 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016296 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016297 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016298 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016299
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016300 /*
16301 * For a negative line count use only the lines at the end of the file,
16302 * free the rest.
16303 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016304 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016305 while (cnt > -maxline)
16306 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016307 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016308 --cnt;
16309 }
16310
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016311 if (failed)
16312 {
16313 list_free(rettv->vval.v_list, TRUE);
16314 /* readfile doc says an empty list is returned on error */
16315 rettv->vval.v_list = list_alloc();
16316 }
16317
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016318 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016319 fclose(fd);
16320}
16321
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016322#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016323static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016324
16325/*
16326 * Convert a List to proftime_T.
16327 * Return FAIL when there is something wrong.
16328 */
16329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016330list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016331{
16332 long n1, n2;
16333 int error = FALSE;
16334
16335 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16336 || arg->vval.v_list->lv_len != 2)
16337 return FAIL;
16338 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16339 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16340# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016341 tm->HighPart = n1;
16342 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016343# else
16344 tm->tv_sec = n1;
16345 tm->tv_usec = n2;
16346# endif
16347 return error ? FAIL : OK;
16348}
16349#endif /* FEAT_RELTIME */
16350
16351/*
16352 * "reltime()" function
16353 */
16354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016355f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016356{
16357#ifdef FEAT_RELTIME
16358 proftime_T res;
16359 proftime_T start;
16360
16361 if (argvars[0].v_type == VAR_UNKNOWN)
16362 {
16363 /* No arguments: get current time. */
16364 profile_start(&res);
16365 }
16366 else if (argvars[1].v_type == VAR_UNKNOWN)
16367 {
16368 if (list2proftime(&argvars[0], &res) == FAIL)
16369 return;
16370 profile_end(&res);
16371 }
16372 else
16373 {
16374 /* Two arguments: compute the difference. */
16375 if (list2proftime(&argvars[0], &start) == FAIL
16376 || list2proftime(&argvars[1], &res) == FAIL)
16377 return;
16378 profile_sub(&res, &start);
16379 }
16380
16381 if (rettv_list_alloc(rettv) == OK)
16382 {
16383 long n1, n2;
16384
16385# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016386 n1 = res.HighPart;
16387 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016388# else
16389 n1 = res.tv_sec;
16390 n2 = res.tv_usec;
16391# endif
16392 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16393 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16394 }
16395#endif
16396}
16397
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016398#ifdef FEAT_FLOAT
16399/*
16400 * "reltimefloat()" function
16401 */
16402 static void
16403f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16404{
16405# ifdef FEAT_RELTIME
16406 proftime_T tm;
16407# endif
16408
16409 rettv->v_type = VAR_FLOAT;
16410 rettv->vval.v_float = 0;
16411# ifdef FEAT_RELTIME
16412 if (list2proftime(&argvars[0], &tm) == OK)
16413 rettv->vval.v_float = profile_float(&tm);
16414# endif
16415}
16416#endif
16417
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016418/*
16419 * "reltimestr()" function
16420 */
16421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016422f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016423{
16424#ifdef FEAT_RELTIME
16425 proftime_T tm;
16426#endif
16427
16428 rettv->v_type = VAR_STRING;
16429 rettv->vval.v_string = NULL;
16430#ifdef FEAT_RELTIME
16431 if (list2proftime(&argvars[0], &tm) == OK)
16432 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16433#endif
16434}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016435
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016437static void make_connection(void);
16438static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016439
16440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016441make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016442{
16443 if (X_DISPLAY == NULL
16444# ifdef FEAT_GUI
16445 && !gui.in_use
16446# endif
16447 )
16448 {
16449 x_force_connect = TRUE;
16450 setup_term_clip();
16451 x_force_connect = FALSE;
16452 }
16453}
16454
16455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016456check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457{
16458 make_connection();
16459 if (X_DISPLAY == NULL)
16460 {
16461 EMSG(_("E240: No connection to Vim server"));
16462 return FAIL;
16463 }
16464 return OK;
16465}
16466#endif
16467
16468#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016469static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016470
16471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016472remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016473{
16474 char_u *server_name;
16475 char_u *keys;
16476 char_u *r = NULL;
16477 char_u buf[NUMBUFLEN];
16478# ifdef WIN32
16479 HWND w;
16480# else
16481 Window w;
16482# endif
16483
16484 if (check_restricted() || check_secure())
16485 return;
16486
16487# ifdef FEAT_X11
16488 if (check_connection() == FAIL)
16489 return;
16490# endif
16491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492 server_name = get_tv_string_chk(&argvars[0]);
16493 if (server_name == NULL)
16494 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016495 keys = get_tv_string_buf(&argvars[1], buf);
16496# ifdef WIN32
16497 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16498# else
16499 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16500 < 0)
16501# endif
16502 {
16503 if (r != NULL)
16504 EMSG(r); /* sending worked but evaluation failed */
16505 else
16506 EMSG2(_("E241: Unable to send to %s"), server_name);
16507 return;
16508 }
16509
16510 rettv->vval.v_string = r;
16511
16512 if (argvars[2].v_type != VAR_UNKNOWN)
16513 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016514 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016515 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016516 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016518 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016519 v.di_tv.v_type = VAR_STRING;
16520 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016521 idvar = get_tv_string_chk(&argvars[2]);
16522 if (idvar != NULL)
16523 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 }
16526}
16527#endif
16528
16529/*
16530 * "remote_expr()" function
16531 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016533f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016534{
16535 rettv->v_type = VAR_STRING;
16536 rettv->vval.v_string = NULL;
16537#ifdef FEAT_CLIENTSERVER
16538 remote_common(argvars, rettv, TRUE);
16539#endif
16540}
16541
16542/*
16543 * "remote_foreground()" function
16544 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016546f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016547{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016548#ifdef FEAT_CLIENTSERVER
16549# ifdef WIN32
16550 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016551 {
16552 char_u *server_name = get_tv_string_chk(&argvars[0]);
16553
16554 if (server_name != NULL)
16555 serverForeground(server_name);
16556 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016557# else
16558 /* Send a foreground() expression to the server. */
16559 argvars[1].v_type = VAR_STRING;
16560 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16561 argvars[2].v_type = VAR_UNKNOWN;
16562 remote_common(argvars, rettv, TRUE);
16563 vim_free(argvars[1].vval.v_string);
16564# endif
16565#endif
16566}
16567
Bram Moolenaar0d660222005-01-07 21:51:51 +000016568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016569f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016570{
16571#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016572 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016573 char_u *s = NULL;
16574# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016575 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016576# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016577 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016578
16579 if (check_restricted() || check_secure())
16580 {
16581 rettv->vval.v_number = -1;
16582 return;
16583 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016584 serverid = get_tv_string_chk(&argvars[0]);
16585 if (serverid == NULL)
16586 {
16587 rettv->vval.v_number = -1;
16588 return; /* type error; errmsg already given */
16589 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016591 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016592 if (n == 0)
16593 rettv->vval.v_number = -1;
16594 else
16595 {
16596 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16597 rettv->vval.v_number = (s != NULL);
16598 }
16599# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016600 if (check_connection() == FAIL)
16601 return;
16602
16603 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016604 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016605# endif
16606
16607 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016609 char_u *retvar;
16610
Bram Moolenaar33570922005-01-25 22:26:29 +000016611 v.di_tv.v_type = VAR_STRING;
16612 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016613 retvar = get_tv_string_chk(&argvars[1]);
16614 if (retvar != NULL)
16615 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016616 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016617 }
16618#else
16619 rettv->vval.v_number = -1;
16620#endif
16621}
16622
Bram Moolenaar0d660222005-01-07 21:51:51 +000016623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016624f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016625{
16626 char_u *r = NULL;
16627
16628#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016629 char_u *serverid = get_tv_string_chk(&argvars[0]);
16630
16631 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016632 {
16633# ifdef WIN32
16634 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016635 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016636
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016637 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016638 if (n != 0)
16639 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16640 if (r == NULL)
16641# else
16642 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016643 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016644# endif
16645 EMSG(_("E277: Unable to read a server reply"));
16646 }
16647#endif
16648 rettv->v_type = VAR_STRING;
16649 rettv->vval.v_string = r;
16650}
16651
16652/*
16653 * "remote_send()" function
16654 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016656f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016657{
16658 rettv->v_type = VAR_STRING;
16659 rettv->vval.v_string = NULL;
16660#ifdef FEAT_CLIENTSERVER
16661 remote_common(argvars, rettv, FALSE);
16662#endif
16663}
16664
16665/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016666 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016667 */
16668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016669f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016670{
Bram Moolenaar33570922005-01-25 22:26:29 +000016671 list_T *l;
16672 listitem_T *item, *item2;
16673 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016674 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016675 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016676 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016677 dict_T *d;
16678 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016679 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016680
Bram Moolenaar8c711452005-01-14 21:53:12 +000016681 if (argvars[0].v_type == VAR_DICT)
16682 {
16683 if (argvars[2].v_type != VAR_UNKNOWN)
16684 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016685 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016686 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016688 key = get_tv_string_chk(&argvars[1]);
16689 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016691 di = dict_find(d, key, -1);
16692 if (di == NULL)
16693 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016694 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16695 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016696 {
16697 *rettv = di->di_tv;
16698 init_tv(&di->di_tv);
16699 dictitem_remove(d, di);
16700 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016702 }
16703 }
16704 else if (argvars[0].v_type != VAR_LIST)
16705 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016706 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016707 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016709 int error = FALSE;
16710
16711 idx = get_tv_number_chk(&argvars[1], &error);
16712 if (error)
16713 ; /* type error: do nothing, errmsg already given */
16714 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016715 EMSGN(_(e_listidx), idx);
16716 else
16717 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016718 if (argvars[2].v_type == VAR_UNKNOWN)
16719 {
16720 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016721 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016722 *rettv = item->li_tv;
16723 vim_free(item);
16724 }
16725 else
16726 {
16727 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016728 end = get_tv_number_chk(&argvars[2], &error);
16729 if (error)
16730 ; /* type error: do nothing */
16731 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016732 EMSGN(_(e_listidx), end);
16733 else
16734 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016735 int cnt = 0;
16736
16737 for (li = item; li != NULL; li = li->li_next)
16738 {
16739 ++cnt;
16740 if (li == item2)
16741 break;
16742 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016743 if (li == NULL) /* didn't find "item2" after "item" */
16744 EMSG(_(e_invrange));
16745 else
16746 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016747 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016748 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016749 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016750 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016751 l->lv_first = item;
16752 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016753 item->li_prev = NULL;
16754 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016755 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016756 }
16757 }
16758 }
16759 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016760 }
16761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762}
16763
16764/*
16765 * "rename({from}, {to})" function
16766 */
16767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016768f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769{
16770 char_u buf[NUMBUFLEN];
16771
16772 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016773 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016775 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16776 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777}
16778
16779/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016780 * "repeat()" function
16781 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016783f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016784{
16785 char_u *p;
16786 int n;
16787 int slen;
16788 int len;
16789 char_u *r;
16790 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016791
16792 n = get_tv_number(&argvars[1]);
16793 if (argvars[0].v_type == VAR_LIST)
16794 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016795 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016796 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016797 if (list_extend(rettv->vval.v_list,
16798 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016799 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016800 }
16801 else
16802 {
16803 p = get_tv_string(&argvars[0]);
16804 rettv->v_type = VAR_STRING;
16805 rettv->vval.v_string = NULL;
16806
16807 slen = (int)STRLEN(p);
16808 len = slen * n;
16809 if (len <= 0)
16810 return;
16811
16812 r = alloc(len + 1);
16813 if (r != NULL)
16814 {
16815 for (i = 0; i < n; i++)
16816 mch_memmove(r + i * slen, p, (size_t)slen);
16817 r[len] = NUL;
16818 }
16819
16820 rettv->vval.v_string = r;
16821 }
16822}
16823
16824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 * "resolve()" function
16826 */
16827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016828f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829{
16830 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016831#ifdef HAVE_READLINK
16832 char_u *buf = NULL;
16833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016835 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836#ifdef FEAT_SHORTCUT
16837 {
16838 char_u *v = NULL;
16839
16840 v = mch_resolve_shortcut(p);
16841 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016842 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016844 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 }
16846#else
16847# ifdef HAVE_READLINK
16848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 char_u *cpy;
16850 int len;
16851 char_u *remain = NULL;
16852 char_u *q;
16853 int is_relative_to_current = FALSE;
16854 int has_trailing_pathsep = FALSE;
16855 int limit = 100;
16856
16857 p = vim_strsave(p);
16858
16859 if (p[0] == '.' && (vim_ispathsep(p[1])
16860 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16861 is_relative_to_current = TRUE;
16862
16863 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016864 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016867 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869
16870 q = getnextcomp(p);
16871 if (*q != NUL)
16872 {
16873 /* Separate the first path component in "p", and keep the
16874 * remainder (beginning with the path separator). */
16875 remain = vim_strsave(q - 1);
16876 q[-1] = NUL;
16877 }
16878
Bram Moolenaard9462e32011-04-11 21:35:11 +020016879 buf = alloc(MAXPATHL + 1);
16880 if (buf == NULL)
16881 goto fail;
16882
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 for (;;)
16884 {
16885 for (;;)
16886 {
16887 len = readlink((char *)p, (char *)buf, MAXPATHL);
16888 if (len <= 0)
16889 break;
16890 buf[len] = NUL;
16891
16892 if (limit-- == 0)
16893 {
16894 vim_free(p);
16895 vim_free(remain);
16896 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016897 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 goto fail;
16899 }
16900
16901 /* Ensure that the result will have a trailing path separator
16902 * if the argument has one. */
16903 if (remain == NULL && has_trailing_pathsep)
16904 add_pathsep(buf);
16905
16906 /* Separate the first path component in the link value and
16907 * concatenate the remainders. */
16908 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16909 if (*q != NUL)
16910 {
16911 if (remain == NULL)
16912 remain = vim_strsave(q - 1);
16913 else
16914 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016915 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 if (cpy != NULL)
16917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 vim_free(remain);
16919 remain = cpy;
16920 }
16921 }
16922 q[-1] = NUL;
16923 }
16924
16925 q = gettail(p);
16926 if (q > p && *q == NUL)
16927 {
16928 /* Ignore trailing path separator. */
16929 q[-1] = NUL;
16930 q = gettail(p);
16931 }
16932 if (q > p && !mch_isFullName(buf))
16933 {
16934 /* symlink is relative to directory of argument */
16935 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16936 if (cpy != NULL)
16937 {
16938 STRCPY(cpy, p);
16939 STRCPY(gettail(cpy), buf);
16940 vim_free(p);
16941 p = cpy;
16942 }
16943 }
16944 else
16945 {
16946 vim_free(p);
16947 p = vim_strsave(buf);
16948 }
16949 }
16950
16951 if (remain == NULL)
16952 break;
16953
16954 /* Append the first path component of "remain" to "p". */
16955 q = getnextcomp(remain + 1);
16956 len = q - remain - (*q != NUL);
16957 cpy = vim_strnsave(p, STRLEN(p) + len);
16958 if (cpy != NULL)
16959 {
16960 STRNCAT(cpy, remain, len);
16961 vim_free(p);
16962 p = cpy;
16963 }
16964 /* Shorten "remain". */
16965 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016966 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 else
16968 {
16969 vim_free(remain);
16970 remain = NULL;
16971 }
16972 }
16973
16974 /* If the result is a relative path name, make it explicitly relative to
16975 * the current directory if and only if the argument had this form. */
16976 if (!vim_ispathsep(*p))
16977 {
16978 if (is_relative_to_current
16979 && *p != NUL
16980 && !(p[0] == '.'
16981 && (p[1] == NUL
16982 || vim_ispathsep(p[1])
16983 || (p[1] == '.'
16984 && (p[2] == NUL
16985 || vim_ispathsep(p[2]))))))
16986 {
16987 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016988 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 if (cpy != NULL)
16990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 vim_free(p);
16992 p = cpy;
16993 }
16994 }
16995 else if (!is_relative_to_current)
16996 {
16997 /* Strip leading "./". */
16998 q = p;
16999 while (q[0] == '.' && vim_ispathsep(q[1]))
17000 q += 2;
17001 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017002 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 }
17004 }
17005
17006 /* Ensure that the result will have no trailing path separator
17007 * if the argument had none. But keep "/" or "//". */
17008 if (!has_trailing_pathsep)
17009 {
17010 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017011 if (after_pathsep(p, q))
17012 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 }
17014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017015 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 }
17017# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017018 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019# endif
17020#endif
17021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023
17024#ifdef HAVE_READLINK
17025fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017026 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017028 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029}
17030
17031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 */
17034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017035f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036{
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 list_T *l;
17038 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040 if (argvars[0].v_type != VAR_LIST)
17041 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017042 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017043 && !tv_check_lock(l->lv_lock,
17044 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017045 {
17046 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017047 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017048 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049 while (li != NULL)
17050 {
17051 ni = li->li_prev;
17052 list_append(l, li);
17053 li = ni;
17054 }
17055 rettv->vval.v_list = l;
17056 rettv->v_type = VAR_LIST;
17057 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017058 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060}
17061
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017062#define SP_NOMOVE 0x01 /* don't move cursor */
17063#define SP_REPEAT 0x02 /* repeat to find outer pair */
17064#define SP_RETCOUNT 0x04 /* return matchcount */
17065#define SP_SETPCMARK 0x08 /* set previous context mark */
17066#define SP_START 0x10 /* accept match at start position */
17067#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17068#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017069#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017070
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017071static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072
17073/*
17074 * Get flags for a search function.
17075 * Possibly sets "p_ws".
17076 * Returns BACKWARD, FORWARD or zero (for an error).
17077 */
17078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017079get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017080{
17081 int dir = FORWARD;
17082 char_u *flags;
17083 char_u nbuf[NUMBUFLEN];
17084 int mask;
17085
17086 if (varp->v_type != VAR_UNKNOWN)
17087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 flags = get_tv_string_buf_chk(varp, nbuf);
17089 if (flags == NULL)
17090 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091 while (*flags != NUL)
17092 {
17093 switch (*flags)
17094 {
17095 case 'b': dir = BACKWARD; break;
17096 case 'w': p_ws = TRUE; break;
17097 case 'W': p_ws = FALSE; break;
17098 default: mask = 0;
17099 if (flagsp != NULL)
17100 switch (*flags)
17101 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017102 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017103 case 'e': mask = SP_END; break;
17104 case 'm': mask = SP_RETCOUNT; break;
17105 case 'n': mask = SP_NOMOVE; break;
17106 case 'p': mask = SP_SUBPAT; break;
17107 case 'r': mask = SP_REPEAT; break;
17108 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017109 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110 }
17111 if (mask == 0)
17112 {
17113 EMSG2(_(e_invarg2), flags);
17114 dir = 0;
17115 }
17116 else
17117 *flagsp |= mask;
17118 }
17119 if (dir == 0)
17120 break;
17121 ++flags;
17122 }
17123 }
17124 return dir;
17125}
17126
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017128 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017131search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017133 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 char_u *pat;
17135 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017136 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137 int save_p_ws = p_ws;
17138 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017139 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017140 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017141 proftime_T tm;
17142#ifdef FEAT_RELTIME
17143 long time_limit = 0;
17144#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017145 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017146 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017148 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017149 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017150 if (dir == 0)
17151 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017152 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017153 if (flags & SP_START)
17154 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017155 if (flags & SP_END)
17156 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017157 if (flags & SP_COLUMN)
17158 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017159
Bram Moolenaar76929292008-01-06 19:07:36 +000017160 /* Optional arguments: line number to stop searching and timeout. */
17161 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017162 {
17163 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17164 if (lnum_stop < 0)
17165 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017166#ifdef FEAT_RELTIME
17167 if (argvars[3].v_type != VAR_UNKNOWN)
17168 {
17169 time_limit = get_tv_number_chk(&argvars[3], NULL);
17170 if (time_limit < 0)
17171 goto theend;
17172 }
17173#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017174 }
17175
Bram Moolenaar76929292008-01-06 19:07:36 +000017176#ifdef FEAT_RELTIME
17177 /* Set the time limit, if there is one. */
17178 profile_setlimit(time_limit, &tm);
17179#endif
17180
Bram Moolenaar231334e2005-07-25 20:46:57 +000017181 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017182 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017183 * Check to make sure only those flags are set.
17184 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17185 * flags cannot be set. Check for that condition also.
17186 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017187 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017188 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017190 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017191 goto theend;
17192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017194 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017195 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017196 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017197 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017199 if (flags & SP_SUBPAT)
17200 retval = subpatnum;
17201 else
17202 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017203 if (flags & SP_SETPCMARK)
17204 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017206 if (match_pos != NULL)
17207 {
17208 /* Store the match cursor position */
17209 match_pos->lnum = pos.lnum;
17210 match_pos->col = pos.col + 1;
17211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 /* "/$" will put the cursor after the end of the line, may need to
17213 * correct that here */
17214 check_cursor();
17215 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017216
17217 /* If 'n' flag is used: restore cursor position. */
17218 if (flags & SP_NOMOVE)
17219 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017220 else
17221 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017222theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017224
17225 return retval;
17226}
17227
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017228#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017229
17230/*
17231 * round() is not in C90, use ceil() or floor() instead.
17232 */
17233 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017234vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017235{
17236 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17237}
17238
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017239/*
17240 * "round({float})" function
17241 */
17242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017243f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017244{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017245 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017246
17247 rettv->v_type = VAR_FLOAT;
17248 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017249 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017250 else
17251 rettv->vval.v_float = 0.0;
17252}
17253#endif
17254
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017255/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017256 * "screenattr()" function
17257 */
17258 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017259f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017260{
17261 int row;
17262 int col;
17263 int c;
17264
17265 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17266 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17267 if (row < 0 || row >= screen_Rows
17268 || col < 0 || col >= screen_Columns)
17269 c = -1;
17270 else
17271 c = ScreenAttrs[LineOffset[row] + col];
17272 rettv->vval.v_number = c;
17273}
17274
17275/*
17276 * "screenchar()" function
17277 */
17278 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017279f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017280{
17281 int row;
17282 int col;
17283 int off;
17284 int c;
17285
17286 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17287 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17288 if (row < 0 || row >= screen_Rows
17289 || col < 0 || col >= screen_Columns)
17290 c = -1;
17291 else
17292 {
17293 off = LineOffset[row] + col;
17294#ifdef FEAT_MBYTE
17295 if (enc_utf8 && ScreenLinesUC[off] != 0)
17296 c = ScreenLinesUC[off];
17297 else
17298#endif
17299 c = ScreenLines[off];
17300 }
17301 rettv->vval.v_number = c;
17302}
17303
17304/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017305 * "screencol()" function
17306 *
17307 * First column is 1 to be consistent with virtcol().
17308 */
17309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017310f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017311{
17312 rettv->vval.v_number = screen_screencol() + 1;
17313}
17314
17315/*
17316 * "screenrow()" function
17317 */
17318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017319f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017320{
17321 rettv->vval.v_number = screen_screenrow() + 1;
17322}
17323
17324/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017325 * "search()" function
17326 */
17327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017328f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017329{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017330 int flags = 0;
17331
17332 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333}
17334
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017336 * "searchdecl()" function
17337 */
17338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017339f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017340{
17341 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017342 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017343 int error = FALSE;
17344 char_u *name;
17345
17346 rettv->vval.v_number = 1; /* default: FAIL */
17347
17348 name = get_tv_string_chk(&argvars[0]);
17349 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017350 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017351 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017352 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17353 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17354 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017355 if (!error && name != NULL)
17356 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017357 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017358}
17359
17360/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017361 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017364searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365{
17366 char_u *spat, *mpat, *epat;
17367 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 int dir;
17370 int flags = 0;
17371 char_u nbuf1[NUMBUFLEN];
17372 char_u nbuf2[NUMBUFLEN];
17373 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017374 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017375 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017376 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017379 spat = get_tv_string_chk(&argvars[0]);
17380 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17381 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17382 if (spat == NULL || mpat == NULL || epat == NULL)
17383 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 /* Handle the optional fourth argument: flags */
17386 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017387 if (dir == 0)
17388 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017389
17390 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017391 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17392 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017393 if ((flags & (SP_END | SP_SUBPAT)) != 0
17394 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017395 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017396 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017397 goto theend;
17398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017400 /* Using 'r' implies 'W', otherwise it doesn't work. */
17401 if (flags & SP_REPEAT)
17402 p_ws = FALSE;
17403
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017404 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017405 if (argvars[3].v_type == VAR_UNKNOWN
17406 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 skip = (char_u *)"";
17408 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017410 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017411 if (argvars[5].v_type != VAR_UNKNOWN)
17412 {
17413 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17414 if (lnum_stop < 0)
17415 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017416#ifdef FEAT_RELTIME
17417 if (argvars[6].v_type != VAR_UNKNOWN)
17418 {
17419 time_limit = get_tv_number_chk(&argvars[6], NULL);
17420 if (time_limit < 0)
17421 goto theend;
17422 }
17423#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017424 }
17425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017426 if (skip == NULL)
17427 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017429 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017430 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017431
17432theend:
17433 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017434
17435 return retval;
17436}
17437
17438/*
17439 * "searchpair()" function
17440 */
17441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017442f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017443{
17444 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17445}
17446
17447/*
17448 * "searchpairpos()" function
17449 */
17450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017451f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017452{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017453 pos_T match_pos;
17454 int lnum = 0;
17455 int col = 0;
17456
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017457 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017458 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017459
17460 if (searchpair_cmn(argvars, &match_pos) > 0)
17461 {
17462 lnum = match_pos.lnum;
17463 col = match_pos.col;
17464 }
17465
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017466 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17467 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017468}
17469
17470/*
17471 * Search for a start/middle/end thing.
17472 * Used by searchpair(), see its documentation for the details.
17473 * Returns 0 or -1 for no match,
17474 */
17475 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017476do_searchpair(
17477 char_u *spat, /* start pattern */
17478 char_u *mpat, /* middle pattern */
17479 char_u *epat, /* end pattern */
17480 int dir, /* BACKWARD or FORWARD */
17481 char_u *skip, /* skip expression */
17482 int flags, /* SP_SETPCMARK and other SP_ values */
17483 pos_T *match_pos,
17484 linenr_T lnum_stop, /* stop at this line if not zero */
17485 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017486{
17487 char_u *save_cpo;
17488 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17489 long retval = 0;
17490 pos_T pos;
17491 pos_T firstpos;
17492 pos_T foundpos;
17493 pos_T save_cursor;
17494 pos_T save_pos;
17495 int n;
17496 int r;
17497 int nest = 1;
17498 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017499 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017500 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017501
17502 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17503 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017504 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017505
Bram Moolenaar76929292008-01-06 19:07:36 +000017506#ifdef FEAT_RELTIME
17507 /* Set the time limit, if there is one. */
17508 profile_setlimit(time_limit, &tm);
17509#endif
17510
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017511 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17512 * start/middle/end (pat3, for the top pair). */
17513 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17514 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17515 if (pat2 == NULL || pat3 == NULL)
17516 goto theend;
17517 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17518 if (*mpat == NUL)
17519 STRCPY(pat3, pat2);
17520 else
17521 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17522 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017523 if (flags & SP_START)
17524 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017525
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 save_cursor = curwin->w_cursor;
17527 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017528 clearpos(&firstpos);
17529 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 pat = pat3;
17531 for (;;)
17532 {
17533 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017534 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17536 /* didn't find it or found the first match again: FAIL */
17537 break;
17538
17539 if (firstpos.lnum == 0)
17540 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017541 if (equalpos(pos, foundpos))
17542 {
17543 /* Found the same position again. Can happen with a pattern that
17544 * has "\zs" at the end and searching backwards. Advance one
17545 * character and try again. */
17546 if (dir == BACKWARD)
17547 decl(&pos);
17548 else
17549 incl(&pos);
17550 }
17551 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017553 /* clear the start flag to avoid getting stuck here */
17554 options &= ~SEARCH_START;
17555
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 /* If the skip pattern matches, ignore this match. */
17557 if (*skip != NUL)
17558 {
17559 save_pos = curwin->w_cursor;
17560 curwin->w_cursor = pos;
17561 r = eval_to_bool(skip, &err, NULL, FALSE);
17562 curwin->w_cursor = save_pos;
17563 if (err)
17564 {
17565 /* Evaluating {skip} caused an error, break here. */
17566 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017567 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 break;
17569 }
17570 if (r)
17571 continue;
17572 }
17573
17574 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17575 {
17576 /* Found end when searching backwards or start when searching
17577 * forward: nested pair. */
17578 ++nest;
17579 pat = pat2; /* nested, don't search for middle */
17580 }
17581 else
17582 {
17583 /* Found end when searching forward or start when searching
17584 * backward: end of (nested) pair; or found middle in outer pair. */
17585 if (--nest == 1)
17586 pat = pat3; /* outer level, search for middle */
17587 }
17588
17589 if (nest == 0)
17590 {
17591 /* Found the match: return matchcount or line number. */
17592 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017593 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017595 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017596 if (flags & SP_SETPCMARK)
17597 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 curwin->w_cursor = pos;
17599 if (!(flags & SP_REPEAT))
17600 break;
17601 nest = 1; /* search for next unmatched */
17602 }
17603 }
17604
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017605 if (match_pos != NULL)
17606 {
17607 /* Store the match cursor position */
17608 match_pos->lnum = curwin->w_cursor.lnum;
17609 match_pos->col = curwin->w_cursor.col + 1;
17610 }
17611
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017613 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 curwin->w_cursor = save_cursor;
17615
17616theend:
17617 vim_free(pat2);
17618 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017619 if (p_cpo == empty_option)
17620 p_cpo = save_cpo;
17621 else
17622 /* Darn, evaluating the {skip} expression changed the value. */
17623 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017624
17625 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626}
17627
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017628/*
17629 * "searchpos()" function
17630 */
17631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017632f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017633{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017634 pos_T match_pos;
17635 int lnum = 0;
17636 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017637 int n;
17638 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017639
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017640 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017641 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017642
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017643 n = search_cmn(argvars, &match_pos, &flags);
17644 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017645 {
17646 lnum = match_pos.lnum;
17647 col = match_pos.col;
17648 }
17649
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017650 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17651 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017652 if (flags & SP_SUBPAT)
17653 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017654}
17655
Bram Moolenaar0d660222005-01-07 21:51:51 +000017656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017657f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017659#ifdef FEAT_CLIENTSERVER
17660 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017661 char_u *server = get_tv_string_chk(&argvars[0]);
17662 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663
Bram Moolenaar0d660222005-01-07 21:51:51 +000017664 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017665 if (server == NULL || reply == NULL)
17666 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017667 if (check_restricted() || check_secure())
17668 return;
17669# ifdef FEAT_X11
17670 if (check_connection() == FAIL)
17671 return;
17672# endif
17673
17674 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017676 EMSG(_("E258: Unable to send to client"));
17677 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017679 rettv->vval.v_number = 0;
17680#else
17681 rettv->vval.v_number = -1;
17682#endif
17683}
17684
Bram Moolenaar0d660222005-01-07 21:51:51 +000017685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017686f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017687{
17688 char_u *r = NULL;
17689
17690#ifdef FEAT_CLIENTSERVER
17691# ifdef WIN32
17692 r = serverGetVimNames();
17693# else
17694 make_connection();
17695 if (X_DISPLAY != NULL)
17696 r = serverGetVimNames(X_DISPLAY);
17697# endif
17698#endif
17699 rettv->v_type = VAR_STRING;
17700 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701}
17702
17703/*
17704 * "setbufvar()" function
17705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017707f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708{
17709 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017712 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 char_u nbuf[NUMBUFLEN];
17714
17715 if (check_restricted() || check_secure())
17716 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017717 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17718 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017719 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 varp = &argvars[2];
17721
17722 if (buf != NULL && varname != NULL && varp != NULL)
17723 {
17724 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726
17727 if (*varname == '&')
17728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017729 long numval;
17730 char_u *strval;
17731 int error = FALSE;
17732
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017734 numval = get_tv_number_chk(varp, &error);
17735 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017736 if (!error && strval != NULL)
17737 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 }
17739 else
17740 {
17741 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17742 if (bufvarname != NULL)
17743 {
17744 STRCPY(bufvarname, "b:");
17745 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017746 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 vim_free(bufvarname);
17748 }
17749 }
17750
17751 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754}
17755
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017757f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017758{
17759 dict_T *d;
17760 dictitem_T *di;
17761 char_u *csearch;
17762
17763 if (argvars[0].v_type != VAR_DICT)
17764 {
17765 EMSG(_(e_dictreq));
17766 return;
17767 }
17768
17769 if ((d = argvars[0].vval.v_dict) != NULL)
17770 {
17771 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17772 if (csearch != NULL)
17773 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017774#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017775 if (enc_utf8)
17776 {
17777 int pcc[MAX_MCO];
17778 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017779
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017780 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17781 }
17782 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017783#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017784 set_last_csearch(PTR2CHAR(csearch),
17785 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017786 }
17787
17788 di = dict_find(d, (char_u *)"forward", -1);
17789 if (di != NULL)
17790 set_csearch_direction(get_tv_number(&di->di_tv)
17791 ? FORWARD : BACKWARD);
17792
17793 di = dict_find(d, (char_u *)"until", -1);
17794 if (di != NULL)
17795 set_csearch_until(!!get_tv_number(&di->di_tv));
17796 }
17797}
17798
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799/*
17800 * "setcmdpos()" function
17801 */
17802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017803f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017805 int pos = (int)get_tv_number(&argvars[0]) - 1;
17806
17807 if (pos >= 0)
17808 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809}
17810
17811/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017812 * "setfperm({fname}, {mode})" function
17813 */
17814 static void
17815f_setfperm(typval_T *argvars, typval_T *rettv)
17816{
17817 char_u *fname;
17818 char_u modebuf[NUMBUFLEN];
17819 char_u *mode_str;
17820 int i;
17821 int mask;
17822 int mode = 0;
17823
17824 rettv->vval.v_number = 0;
17825 fname = get_tv_string_chk(&argvars[0]);
17826 if (fname == NULL)
17827 return;
17828 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17829 if (mode_str == NULL)
17830 return;
17831 if (STRLEN(mode_str) != 9)
17832 {
17833 EMSG2(_(e_invarg2), mode_str);
17834 return;
17835 }
17836
17837 mask = 1;
17838 for (i = 8; i >= 0; --i)
17839 {
17840 if (mode_str[i] != '-')
17841 mode |= mask;
17842 mask = mask << 1;
17843 }
17844 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17845}
17846
17847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 * "setline()" function
17849 */
17850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017851f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852{
17853 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017854 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017855 list_T *l = NULL;
17856 listitem_T *li = NULL;
17857 long added = 0;
17858 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017860 lnum = get_tv_lnum(&argvars[0]);
17861 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017863 l = argvars[1].vval.v_list;
17864 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017866 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017867 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017868
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017869 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017870 for (;;)
17871 {
17872 if (l != NULL)
17873 {
17874 /* list argument, get next string */
17875 if (li == NULL)
17876 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017877 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017878 li = li->li_next;
17879 }
17880
17881 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017882 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017883 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017884
17885 /* When coming here from Insert mode, sync undo, so that this can be
17886 * undone separately from what was previously inserted. */
17887 if (u_sync_once == 2)
17888 {
17889 u_sync_once = 1; /* notify that u_sync() was called */
17890 u_sync(TRUE);
17891 }
17892
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017893 if (lnum <= curbuf->b_ml.ml_line_count)
17894 {
17895 /* existing line, replace it */
17896 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17897 {
17898 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017899 if (lnum == curwin->w_cursor.lnum)
17900 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017901 rettv->vval.v_number = 0; /* OK */
17902 }
17903 }
17904 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17905 {
17906 /* lnum is one past the last line, append the line */
17907 ++added;
17908 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17909 rettv->vval.v_number = 0; /* OK */
17910 }
17911
17912 if (l == NULL) /* only one string argument */
17913 break;
17914 ++lnum;
17915 }
17916
17917 if (added > 0)
17918 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919}
17920
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017921static 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 +000017922
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017924 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017925 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017927set_qf_ll_list(
17928 win_T *wp UNUSED,
17929 typval_T *list_arg UNUSED,
17930 typval_T *action_arg UNUSED,
17931 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017932{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017933#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017934 char_u *act;
17935 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017936#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017937
Bram Moolenaar2641f772005-03-25 21:58:17 +000017938 rettv->vval.v_number = -1;
17939
17940#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017941 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017942 EMSG(_(e_listreq));
17943 else
17944 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017945 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017946
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017947 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017948 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017949 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017950 if (act == NULL)
17951 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017952 if (*act == 'a' || *act == 'r')
17953 action = *act;
17954 }
17955
Bram Moolenaar81484f42012-12-05 15:16:47 +010017956 if (l != NULL && set_errorlist(wp, l, action,
17957 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017958 rettv->vval.v_number = 0;
17959 }
17960#endif
17961}
17962
17963/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017964 * "setloclist()" function
17965 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017967f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017968{
17969 win_T *win;
17970
17971 rettv->vval.v_number = -1;
17972
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017973 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017974 if (win != NULL)
17975 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17976}
17977
17978/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017979 * "setmatches()" function
17980 */
17981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017982f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017983{
17984#ifdef FEAT_SEARCH_EXTRA
17985 list_T *l;
17986 listitem_T *li;
17987 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017988 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017989
17990 rettv->vval.v_number = -1;
17991 if (argvars[0].v_type != VAR_LIST)
17992 {
17993 EMSG(_(e_listreq));
17994 return;
17995 }
17996 if ((l = argvars[0].vval.v_list) != NULL)
17997 {
17998
17999 /* To some extent make sure that we are dealing with a list from
18000 * "getmatches()". */
18001 li = l->lv_first;
18002 while (li != NULL)
18003 {
18004 if (li->li_tv.v_type != VAR_DICT
18005 || (d = li->li_tv.vval.v_dict) == NULL)
18006 {
18007 EMSG(_(e_invarg));
18008 return;
18009 }
18010 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018011 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18012 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018013 && dict_find(d, (char_u *)"priority", -1) != NULL
18014 && dict_find(d, (char_u *)"id", -1) != NULL))
18015 {
18016 EMSG(_(e_invarg));
18017 return;
18018 }
18019 li = li->li_next;
18020 }
18021
18022 clear_matches(curwin);
18023 li = l->lv_first;
18024 while (li != NULL)
18025 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018026 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018027 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018028 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018029 char_u *group;
18030 int priority;
18031 int id;
18032 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018033
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018034 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018035 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18036 {
18037 if (s == NULL)
18038 {
18039 s = list_alloc();
18040 if (s == NULL)
18041 return;
18042 }
18043
18044 /* match from matchaddpos() */
18045 for (i = 1; i < 9; i++)
18046 {
18047 sprintf((char *)buf, (char *)"pos%d", i);
18048 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18049 {
18050 if (di->di_tv.v_type != VAR_LIST)
18051 return;
18052
18053 list_append_tv(s, &di->di_tv);
18054 s->lv_refcount++;
18055 }
18056 else
18057 break;
18058 }
18059 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018060
18061 group = get_dict_string(d, (char_u *)"group", FALSE);
18062 priority = (int)get_dict_number(d, (char_u *)"priority");
18063 id = (int)get_dict_number(d, (char_u *)"id");
18064 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18065 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18066 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018067 if (i == 0)
18068 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018069 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018070 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018071 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018072 }
18073 else
18074 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018075 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018076 list_unref(s);
18077 s = NULL;
18078 }
18079
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018080 li = li->li_next;
18081 }
18082 rettv->vval.v_number = 0;
18083 }
18084#endif
18085}
18086
18087/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018088 * "setpos()" function
18089 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018091f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018092{
18093 pos_T pos;
18094 int fnum;
18095 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018096 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018097
Bram Moolenaar08250432008-02-13 11:42:46 +000018098 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018099 name = get_tv_string_chk(argvars);
18100 if (name != NULL)
18101 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018102 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018103 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018104 if (--pos.col < 0)
18105 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018106 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018107 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018108 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018109 if (fnum == curbuf->b_fnum)
18110 {
18111 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018112 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018113 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018114 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018115 curwin->w_set_curswant = FALSE;
18116 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018117 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018118 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018119 }
18120 else
18121 EMSG(_(e_invarg));
18122 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018123 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18124 {
18125 /* set mark */
18126 if (setmark_pos(name[1], &pos, fnum) == OK)
18127 rettv->vval.v_number = 0;
18128 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018129 else
18130 EMSG(_(e_invarg));
18131 }
18132 }
18133}
18134
18135/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018136 * "setqflist()" function
18137 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018139f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018140{
18141 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18142}
18143
18144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 * "setreg()" function
18146 */
18147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018148f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149{
18150 int regname;
18151 char_u *strregname;
18152 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018153 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 int append;
18155 char_u yank_type;
18156 long block_len;
18157
18158 block_len = -1;
18159 yank_type = MAUTO;
18160 append = FALSE;
18161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018162 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018163 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018165 if (strregname == NULL)
18166 return; /* type error; errmsg already given */
18167 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 if (regname == 0 || regname == '@')
18169 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018171 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018173 stropt = get_tv_string_chk(&argvars[2]);
18174 if (stropt == NULL)
18175 return; /* type error */
18176 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 switch (*stropt)
18178 {
18179 case 'a': case 'A': /* append */
18180 append = TRUE;
18181 break;
18182 case 'v': case 'c': /* character-wise selection */
18183 yank_type = MCHAR;
18184 break;
18185 case 'V': case 'l': /* line-wise selection */
18186 yank_type = MLINE;
18187 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 case 'b': case Ctrl_V: /* block-wise selection */
18189 yank_type = MBLOCK;
18190 if (VIM_ISDIGIT(stropt[1]))
18191 {
18192 ++stropt;
18193 block_len = getdigits(&stropt) - 1;
18194 --stropt;
18195 }
18196 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 }
18198 }
18199
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018200 if (argvars[1].v_type == VAR_LIST)
18201 {
18202 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018203 char_u **allocval;
18204 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018205 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018206 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018207 int len = argvars[1].vval.v_list->lv_len;
18208 listitem_T *li;
18209
Bram Moolenaar7d647822014-04-05 21:28:56 +020018210 /* First half: use for pointers to result lines; second half: use for
18211 * pointers to allocated copies. */
18212 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018213 if (lstval == NULL)
18214 return;
18215 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018216 allocval = lstval + len + 2;
18217 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018218
18219 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18220 li = li->li_next)
18221 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018222 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018223 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018224 goto free_lstval;
18225 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018226 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018227 /* Need to make a copy, next get_tv_string_buf_chk() will
18228 * overwrite the string. */
18229 strval = vim_strsave(buf);
18230 if (strval == NULL)
18231 goto free_lstval;
18232 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018233 }
18234 *curval++ = strval;
18235 }
18236 *curval++ = NULL;
18237
18238 write_reg_contents_lst(regname, lstval, -1,
18239 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018240free_lstval:
18241 while (curallocval > allocval)
18242 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018243 vim_free(lstval);
18244 }
18245 else
18246 {
18247 strval = get_tv_string_chk(&argvars[1]);
18248 if (strval == NULL)
18249 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018250 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018253 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254}
18255
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018256/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018257 * "settabvar()" function
18258 */
18259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018260f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018261{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018262#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018263 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018264 tabpage_T *tp;
18265#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018266 char_u *varname, *tabvarname;
18267 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018268
18269 rettv->vval.v_number = 0;
18270
18271 if (check_restricted() || check_secure())
18272 return;
18273
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018274#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018275 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018276#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018277 varname = get_tv_string_chk(&argvars[1]);
18278 varp = &argvars[2];
18279
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018280 if (varname != NULL && varp != NULL
18281#ifdef FEAT_WINDOWS
18282 && tp != NULL
18283#endif
18284 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018285 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018286#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018287 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018288 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018289#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018290
18291 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18292 if (tabvarname != NULL)
18293 {
18294 STRCPY(tabvarname, "t:");
18295 STRCPY(tabvarname + 2, varname);
18296 set_var(tabvarname, varp, TRUE);
18297 vim_free(tabvarname);
18298 }
18299
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018300#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018301 /* Restore current tabpage */
18302 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018303 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018304#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018305 }
18306}
18307
18308/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018309 * "settabwinvar()" function
18310 */
18311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018312f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018313{
18314 setwinvar(argvars, rettv, 1);
18315}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316
18317/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018318 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018321f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018323 setwinvar(argvars, rettv, 0);
18324}
18325
18326/*
18327 * "setwinvar()" and "settabwinvar()" functions
18328 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018329
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018331setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018332{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 win_T *win;
18334#ifdef FEAT_WINDOWS
18335 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018336 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018337 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338#endif
18339 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018340 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018342 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343
18344 if (check_restricted() || check_secure())
18345 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018346
18347#ifdef FEAT_WINDOWS
18348 if (off == 1)
18349 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18350 else
18351 tp = curtab;
18352#endif
18353 win = find_win_by_nr(&argvars[off], tp);
18354 varname = get_tv_string_chk(&argvars[off + 1]);
18355 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356
18357 if (win != NULL && varname != NULL && varp != NULL)
18358 {
18359#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018360 need_switch_win = !(tp == curtab && win == curwin);
18361 if (!need_switch_win
18362 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018365 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018367 long numval;
18368 char_u *strval;
18369 int error = FALSE;
18370
18371 ++varname;
18372 numval = get_tv_number_chk(varp, &error);
18373 strval = get_tv_string_buf_chk(varp, nbuf);
18374 if (!error && strval != NULL)
18375 set_option_value(varname, numval, strval, OPT_LOCAL);
18376 }
18377 else
18378 {
18379 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18380 if (winvarname != NULL)
18381 {
18382 STRCPY(winvarname, "w:");
18383 STRCPY(winvarname + 2, varname);
18384 set_var(winvarname, varp, TRUE);
18385 vim_free(winvarname);
18386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 }
18388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018390 if (need_switch_win)
18391 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392#endif
18393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394}
18395
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018396#ifdef FEAT_CRYPT
18397/*
18398 * "sha256({string})" function
18399 */
18400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018401f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018402{
18403 char_u *p;
18404
18405 p = get_tv_string(&argvars[0]);
18406 rettv->vval.v_string = vim_strsave(
18407 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18408 rettv->v_type = VAR_STRING;
18409}
18410#endif /* FEAT_CRYPT */
18411
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018413 * "shellescape({string})" function
18414 */
18415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018416f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018417{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018418 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018419 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018420 rettv->v_type = VAR_STRING;
18421}
18422
18423/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018424 * shiftwidth() function
18425 */
18426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018427f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018428{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018429 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018430}
18431
18432/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018433 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 */
18435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018436f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018438 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439
Bram Moolenaar0d660222005-01-07 21:51:51 +000018440 p = get_tv_string(&argvars[0]);
18441 rettv->vval.v_string = vim_strsave(p);
18442 simplify_filename(rettv->vval.v_string); /* simplify in place */
18443 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444}
18445
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018446#ifdef FEAT_FLOAT
18447/*
18448 * "sin()" function
18449 */
18450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018451f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018452{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018453 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018454
18455 rettv->v_type = VAR_FLOAT;
18456 if (get_float_arg(argvars, &f) == OK)
18457 rettv->vval.v_float = sin(f);
18458 else
18459 rettv->vval.v_float = 0.0;
18460}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018461
18462/*
18463 * "sinh()" function
18464 */
18465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018466f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018467{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018468 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018469
18470 rettv->v_type = VAR_FLOAT;
18471 if (get_float_arg(argvars, &f) == OK)
18472 rettv->vval.v_float = sinh(f);
18473 else
18474 rettv->vval.v_float = 0.0;
18475}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018476#endif
18477
Bram Moolenaar0d660222005-01-07 21:51:51 +000018478static int
18479#ifdef __BORLANDC__
18480 _RTLENTRYF
18481#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018482 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018483static int
18484#ifdef __BORLANDC__
18485 _RTLENTRYF
18486#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018487 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018488
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018489/* struct used in the array that's given to qsort() */
18490typedef struct
18491{
18492 listitem_T *item;
18493 int idx;
18494} sortItem_T;
18495
Bram Moolenaar0b962472016-02-22 22:51:33 +010018496/* struct storing information about current sort */
18497typedef struct
18498{
18499 int item_compare_ic;
18500 int item_compare_numeric;
18501 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018502#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018503 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018504#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018505 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018506 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018507 dict_T *item_compare_selfdict;
18508 int item_compare_func_err;
18509 int item_compare_keep_zero;
18510} sortinfo_T;
18511static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018512static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018513#define ITEM_COMPARE_FAIL 999
18514
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018516 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018518 static int
18519#ifdef __BORLANDC__
18520_RTLENTRYF
18521#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018522item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018524 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018525 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018526 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018527 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018528 int res;
18529 char_u numbuf1[NUMBUFLEN];
18530 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018532 si1 = (sortItem_T *)s1;
18533 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018534 tv1 = &si1->item->li_tv;
18535 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018536
Bram Moolenaar0b962472016-02-22 22:51:33 +010018537 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018538 {
18539 long v1 = get_tv_number(tv1);
18540 long v2 = get_tv_number(tv2);
18541
18542 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18543 }
18544
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018545#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018546 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018547 {
18548 float_T v1 = get_tv_float(tv1);
18549 float_T v2 = get_tv_float(tv2);
18550
18551 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18552 }
18553#endif
18554
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018555 /* tv2string() puts quotes around a string and allocates memory. Don't do
18556 * that for string variables. Use a single quote when comparing with a
18557 * non-string to do what the docs promise. */
18558 if (tv1->v_type == VAR_STRING)
18559 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018560 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018561 p1 = (char_u *)"'";
18562 else
18563 p1 = tv1->vval.v_string;
18564 }
18565 else
18566 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18567 if (tv2->v_type == VAR_STRING)
18568 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018569 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018570 p2 = (char_u *)"'";
18571 else
18572 p2 = tv2->vval.v_string;
18573 }
18574 else
18575 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018576 if (p1 == NULL)
18577 p1 = (char_u *)"";
18578 if (p2 == NULL)
18579 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018580 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018581 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018582 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018583 res = STRICMP(p1, p2);
18584 else
18585 res = STRCMP(p1, p2);
18586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018588 {
18589 double n1, n2;
18590 n1 = strtod((char *)p1, (char **)&p1);
18591 n2 = strtod((char *)p2, (char **)&p2);
18592 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18593 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018594
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018595 /* When the result would be zero, compare the item indexes. Makes the
18596 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018597 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018598 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018599
Bram Moolenaar0d660222005-01-07 21:51:51 +000018600 vim_free(tofree1);
18601 vim_free(tofree2);
18602 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603}
18604
18605 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018606#ifdef __BORLANDC__
18607_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018609item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018610{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018611 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018612 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018613 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018614 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018615 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018616 char_u *func_name;
18617 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018619 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018620 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018621 return 0;
18622
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018623 si1 = (sortItem_T *)s1;
18624 si2 = (sortItem_T *)s2;
18625
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018626 if (partial == NULL)
18627 func_name = sortinfo->item_compare_func;
18628 else
18629 func_name = partial->pt_name;
18630
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018631 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018632 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018633 copy_tv(&si1->item->li_tv, &argv[0]);
18634 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018635
18636 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018637 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018638 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018639 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640 clear_tv(&argv[0]);
18641 clear_tv(&argv[1]);
18642
18643 if (res == FAIL)
18644 res = ITEM_COMPARE_FAIL;
18645 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018646 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18647 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018648 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018649 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018650
18651 /* When the result would be zero, compare the pointers themselves. Makes
18652 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018653 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018654 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018655
Bram Moolenaar0d660222005-01-07 21:51:51 +000018656 return res;
18657}
18658
18659/*
18660 * "sort({list})" function
18661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018663do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664{
Bram Moolenaar33570922005-01-25 22:26:29 +000018665 list_T *l;
18666 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018667 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018668 sortinfo_T *old_sortinfo;
18669 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018670 long len;
18671 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672
Bram Moolenaar0b962472016-02-22 22:51:33 +010018673 /* Pointer to current info struct used in compare function. Save and
18674 * restore the current one for nested calls. */
18675 old_sortinfo = sortinfo;
18676 sortinfo = &info;
18677
Bram Moolenaar0d660222005-01-07 21:51:51 +000018678 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018679 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 else
18681 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018682 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018683 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018684 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18685 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018686 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018687 rettv->vval.v_list = l;
18688 rettv->v_type = VAR_LIST;
18689 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690
Bram Moolenaar0d660222005-01-07 21:51:51 +000018691 len = list_len(l);
18692 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018693 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694
Bram Moolenaar0b962472016-02-22 22:51:33 +010018695 info.item_compare_ic = FALSE;
18696 info.item_compare_numeric = FALSE;
18697 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018698#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018699 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018700#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018701 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018702 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018703 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018704 if (argvars[1].v_type != VAR_UNKNOWN)
18705 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018706 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018707 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018708 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018709 else if (argvars[1].v_type == VAR_PARTIAL)
18710 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018711 else
18712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018713 int error = FALSE;
18714
18715 i = get_tv_number_chk(&argvars[1], &error);
18716 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018717 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018718 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018719 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018720 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018721 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018722 else if (i != 0)
18723 {
18724 EMSG(_(e_invarg));
18725 goto theend;
18726 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018727 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018728 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018729 if (*info.item_compare_func == NUL)
18730 {
18731 /* empty string means default sort */
18732 info.item_compare_func = NULL;
18733 }
18734 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018735 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018736 info.item_compare_func = NULL;
18737 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018738 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018739 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018740 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018741 info.item_compare_func = NULL;
18742 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018743 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018744#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018745 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018746 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018747 info.item_compare_func = NULL;
18748 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018749 }
18750#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018751 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018752 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018753 info.item_compare_func = NULL;
18754 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018755 }
18756 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018757 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018758
18759 if (argvars[2].v_type != VAR_UNKNOWN)
18760 {
18761 /* optional third argument: {dict} */
18762 if (argvars[2].v_type != VAR_DICT)
18763 {
18764 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018765 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018766 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018767 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018768 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770
Bram Moolenaar0d660222005-01-07 21:51:51 +000018771 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018772 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018773 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018774 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775
Bram Moolenaar327aa022014-03-25 18:24:23 +010018776 i = 0;
18777 if (sort)
18778 {
18779 /* sort(): ptrs will be the list to sort */
18780 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018781 {
18782 ptrs[i].item = li;
18783 ptrs[i].idx = i;
18784 ++i;
18785 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018786
Bram Moolenaar0b962472016-02-22 22:51:33 +010018787 info.item_compare_func_err = FALSE;
18788 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018789 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018790 if ((info.item_compare_func != NULL
18791 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018792 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018793 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018794 EMSG(_("E702: Sort compare function failed"));
18795 else
18796 {
18797 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018798 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018799 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018800 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018801 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018802
Bram Moolenaar0b962472016-02-22 22:51:33 +010018803 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018804 {
18805 /* Clear the List and append the items in sorted order. */
18806 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18807 l->lv_len = 0;
18808 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018809 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018810 }
18811 }
18812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018814 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018815 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018816
18817 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018818 info.item_compare_func_err = FALSE;
18819 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018820 item_compare_func_ptr = info.item_compare_func != NULL
18821 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018822 ? item_compare2 : item_compare;
18823
18824 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18825 li = li->li_next)
18826 {
18827 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18828 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018829 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018830 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018831 {
18832 EMSG(_("E882: Uniq compare function failed"));
18833 break;
18834 }
18835 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018836
Bram Moolenaar0b962472016-02-22 22:51:33 +010018837 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018838 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018839 while (--i >= 0)
18840 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018841 li = ptrs[i].item->li_next;
18842 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018843 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018844 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018845 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018846 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018847 list_fix_watch(l, li);
18848 listitem_free(li);
18849 l->lv_len--;
18850 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018851 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018852 }
18853
18854 vim_free(ptrs);
18855 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018856theend:
18857 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018858}
18859
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018860/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018861 * "sort({list})" function
18862 */
18863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018864f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018865{
18866 do_sort_uniq(argvars, rettv, TRUE);
18867}
18868
18869/*
18870 * "uniq({list})" function
18871 */
18872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018873f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018874{
18875 do_sort_uniq(argvars, rettv, FALSE);
18876}
18877
18878/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018879 * "soundfold({word})" function
18880 */
18881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018882f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018883{
18884 char_u *s;
18885
18886 rettv->v_type = VAR_STRING;
18887 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018888#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018889 rettv->vval.v_string = eval_soundfold(s);
18890#else
18891 rettv->vval.v_string = vim_strsave(s);
18892#endif
18893}
18894
18895/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018896 * "spellbadword()" function
18897 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018899f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018900{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018901 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018902 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018903 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018904
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018905 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018906 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018907
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018908#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018909 if (argvars[0].v_type == VAR_UNKNOWN)
18910 {
18911 /* Find the start and length of the badly spelled word. */
18912 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18913 if (len != 0)
18914 word = ml_get_cursor();
18915 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018916 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018917 {
18918 char_u *str = get_tv_string_chk(&argvars[0]);
18919 int capcol = -1;
18920
18921 if (str != NULL)
18922 {
18923 /* Check the argument for spelling. */
18924 while (*str != NUL)
18925 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018926 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018927 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018928 {
18929 word = str;
18930 break;
18931 }
18932 str += len;
18933 }
18934 }
18935 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018936#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018937
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018938 list_append_string(rettv->vval.v_list, word, len);
18939 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018940 attr == HLF_SPB ? "bad" :
18941 attr == HLF_SPR ? "rare" :
18942 attr == HLF_SPL ? "local" :
18943 attr == HLF_SPC ? "caps" :
18944 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018945}
18946
18947/*
18948 * "spellsuggest()" function
18949 */
18950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018951f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018952{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018953#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018954 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018955 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018956 int maxcount;
18957 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018958 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018959 listitem_T *li;
18960 int need_capital = FALSE;
18961#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018962
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018963 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018964 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018965
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018966#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018967 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018968 {
18969 str = get_tv_string(&argvars[0]);
18970 if (argvars[1].v_type != VAR_UNKNOWN)
18971 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018972 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018973 if (maxcount <= 0)
18974 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018975 if (argvars[2].v_type != VAR_UNKNOWN)
18976 {
18977 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18978 if (typeerr)
18979 return;
18980 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018981 }
18982 else
18983 maxcount = 25;
18984
Bram Moolenaar4770d092006-01-12 23:22:24 +000018985 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018986
18987 for (i = 0; i < ga.ga_len; ++i)
18988 {
18989 str = ((char_u **)ga.ga_data)[i];
18990
18991 li = listitem_alloc();
18992 if (li == NULL)
18993 vim_free(str);
18994 else
18995 {
18996 li->li_tv.v_type = VAR_STRING;
18997 li->li_tv.v_lock = 0;
18998 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018999 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019000 }
19001 }
19002 ga_clear(&ga);
19003 }
19004#endif
19005}
19006
Bram Moolenaar0d660222005-01-07 21:51:51 +000019007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019008f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019009{
19010 char_u *str;
19011 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019012 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013 regmatch_T regmatch;
19014 char_u patbuf[NUMBUFLEN];
19015 char_u *save_cpo;
19016 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019017 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019018 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019019 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019020
19021 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19022 save_cpo = p_cpo;
19023 p_cpo = (char_u *)"";
19024
19025 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019026 if (argvars[1].v_type != VAR_UNKNOWN)
19027 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019028 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19029 if (pat == NULL)
19030 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019031 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019032 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019033 }
19034 if (pat == NULL || *pat == NUL)
19035 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019036
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019037 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019039 if (typeerr)
19040 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041
Bram Moolenaar0d660222005-01-07 21:51:51 +000019042 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19043 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019046 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019047 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019048 if (*str == NUL)
19049 match = FALSE; /* empty item at the end */
19050 else
19051 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019052 if (match)
19053 end = regmatch.startp[0];
19054 else
19055 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019056 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19057 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019058 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019059 if (list_append_string(rettv->vval.v_list, str,
19060 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019061 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019062 }
19063 if (!match)
19064 break;
19065 /* Advance to just after the match. */
19066 if (regmatch.endp[0] > str)
19067 col = 0;
19068 else
19069 {
19070 /* Don't get stuck at the same match. */
19071#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019072 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073#else
19074 col = 1;
19075#endif
19076 }
19077 str = regmatch.endp[0];
19078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079
Bram Moolenaar473de612013-06-08 18:19:48 +020019080 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082
Bram Moolenaar0d660222005-01-07 21:51:51 +000019083 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084}
19085
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019086#ifdef FEAT_FLOAT
19087/*
19088 * "sqrt()" function
19089 */
19090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019091f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019092{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019093 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019094
19095 rettv->v_type = VAR_FLOAT;
19096 if (get_float_arg(argvars, &f) == OK)
19097 rettv->vval.v_float = sqrt(f);
19098 else
19099 rettv->vval.v_float = 0.0;
19100}
19101
19102/*
19103 * "str2float()" function
19104 */
19105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019106f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019107{
19108 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19109
19110 if (*p == '+')
19111 p = skipwhite(p + 1);
19112 (void)string2float(p, &rettv->vval.v_float);
19113 rettv->v_type = VAR_FLOAT;
19114}
19115#endif
19116
Bram Moolenaar2c932302006-03-18 21:42:09 +000019117/*
19118 * "str2nr()" function
19119 */
19120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019121f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019122{
19123 int base = 10;
19124 char_u *p;
19125 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019126 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019127
19128 if (argvars[1].v_type != VAR_UNKNOWN)
19129 {
19130 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019131 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019132 {
19133 EMSG(_(e_invarg));
19134 return;
19135 }
19136 }
19137
19138 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019139 if (*p == '+')
19140 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019141 switch (base)
19142 {
19143 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19144 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19145 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19146 default: what = 0;
19147 }
19148 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019149 rettv->vval.v_number = n;
19150}
19151
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152#ifdef HAVE_STRFTIME
19153/*
19154 * "strftime({format}[, {time}])" function
19155 */
19156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019157f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158{
19159 char_u result_buf[256];
19160 struct tm *curtime;
19161 time_t seconds;
19162 char_u *p;
19163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019164 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019166 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019167 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 seconds = time(NULL);
19169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171 curtime = localtime(&seconds);
19172 /* MSVC returns NULL for an invalid value of seconds. */
19173 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 else
19176 {
19177# ifdef FEAT_MBYTE
19178 vimconv_T conv;
19179 char_u *enc;
19180
19181 conv.vc_type = CONV_NONE;
19182 enc = enc_locale();
19183 convert_setup(&conv, p_enc, enc);
19184 if (conv.vc_type != CONV_NONE)
19185 p = string_convert(&conv, p, NULL);
19186# endif
19187 if (p != NULL)
19188 (void)strftime((char *)result_buf, sizeof(result_buf),
19189 (char *)p, curtime);
19190 else
19191 result_buf[0] = NUL;
19192
19193# ifdef FEAT_MBYTE
19194 if (conv.vc_type != CONV_NONE)
19195 vim_free(p);
19196 convert_setup(&conv, enc, p_enc);
19197 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019198 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199 else
19200# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202
19203# ifdef FEAT_MBYTE
19204 /* Release conversion descriptors */
19205 convert_setup(&conv, NULL, NULL);
19206 vim_free(enc);
19207# endif
19208 }
19209}
19210#endif
19211
19212/*
19213 * "stridx()" function
19214 */
19215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019216f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217{
19218 char_u buf[NUMBUFLEN];
19219 char_u *needle;
19220 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019221 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019223 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019225 needle = get_tv_string_chk(&argvars[1]);
19226 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019228 if (needle == NULL || haystack == NULL)
19229 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230
Bram Moolenaar33570922005-01-25 22:26:29 +000019231 if (argvars[2].v_type != VAR_UNKNOWN)
19232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019233 int error = FALSE;
19234
19235 start_idx = get_tv_number_chk(&argvars[2], &error);
19236 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019237 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019238 if (start_idx >= 0)
19239 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019240 }
19241
19242 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19243 if (pos != NULL)
19244 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245}
19246
19247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019248 * "string()" function
19249 */
19250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019251f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019252{
19253 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019254 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019256 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019257 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19258 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019259 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019260 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019261 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262}
19263
19264/*
19265 * "strlen()" function
19266 */
19267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019268f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019270 rettv->vval.v_number = (varnumber_T)(STRLEN(
19271 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272}
19273
19274/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019275 * "strchars()" function
19276 */
19277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019278f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019279{
19280 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019281 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019282#ifdef FEAT_MBYTE
19283 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019284 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019285#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019286
19287 if (argvars[1].v_type != VAR_UNKNOWN)
19288 skipcc = get_tv_number_chk(&argvars[1], NULL);
19289 if (skipcc < 0 || skipcc > 1)
19290 EMSG(_(e_invarg));
19291 else
19292 {
19293#ifdef FEAT_MBYTE
19294 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19295 while (*s != NUL)
19296 {
19297 func_mb_ptr2char_adv(&s);
19298 ++len;
19299 }
19300 rettv->vval.v_number = len;
19301#else
19302 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19303#endif
19304 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019305}
19306
19307/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019308 * "strdisplaywidth()" function
19309 */
19310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019311f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019312{
19313 char_u *s = get_tv_string(&argvars[0]);
19314 int col = 0;
19315
19316 if (argvars[1].v_type != VAR_UNKNOWN)
19317 col = get_tv_number(&argvars[1]);
19318
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019319 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019320}
19321
19322/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019323 * "strwidth()" function
19324 */
19325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019326f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019327{
19328 char_u *s = get_tv_string(&argvars[0]);
19329
19330 rettv->vval.v_number = (varnumber_T)(
19331#ifdef FEAT_MBYTE
19332 mb_string2cells(s, -1)
19333#else
19334 STRLEN(s)
19335#endif
19336 );
19337}
19338
19339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340 * "strpart()" function
19341 */
19342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019343f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344{
19345 char_u *p;
19346 int n;
19347 int len;
19348 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019349 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019351 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 slen = (int)STRLEN(p);
19353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019354 n = get_tv_number_chk(&argvars[1], &error);
19355 if (error)
19356 len = 0;
19357 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019358 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 else
19360 len = slen - n; /* default len: all bytes that are available. */
19361
19362 /*
19363 * Only return the overlap between the specified part and the actual
19364 * string.
19365 */
19366 if (n < 0)
19367 {
19368 len += n;
19369 n = 0;
19370 }
19371 else if (n > slen)
19372 n = slen;
19373 if (len < 0)
19374 len = 0;
19375 else if (n + len > slen)
19376 len = slen - n;
19377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019378 rettv->v_type = VAR_STRING;
19379 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380}
19381
19382/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019383 * "strridx()" function
19384 */
19385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019386f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019387{
19388 char_u buf[NUMBUFLEN];
19389 char_u *needle;
19390 char_u *haystack;
19391 char_u *rest;
19392 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019393 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019395 needle = get_tv_string_chk(&argvars[1]);
19396 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019397
19398 rettv->vval.v_number = -1;
19399 if (needle == NULL || haystack == NULL)
19400 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019401
19402 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019403 if (argvars[2].v_type != VAR_UNKNOWN)
19404 {
19405 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019406 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019407 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019408 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019409 }
19410 else
19411 end_idx = haystack_len;
19412
Bram Moolenaar0d660222005-01-07 21:51:51 +000019413 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019414 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019415 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019416 lastmatch = haystack + end_idx;
19417 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019418 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019419 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019420 for (rest = haystack; *rest != '\0'; ++rest)
19421 {
19422 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019423 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019424 break;
19425 lastmatch = rest;
19426 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019427 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019428
19429 if (lastmatch == NULL)
19430 rettv->vval.v_number = -1;
19431 else
19432 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19433}
19434
19435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 * "strtrans()" function
19437 */
19438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019439f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019441 rettv->v_type = VAR_STRING;
19442 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443}
19444
19445/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019446 * "submatch()" function
19447 */
19448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019449f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019450{
Bram Moolenaar41571762014-04-02 19:00:58 +020019451 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019452 int no;
19453 int retList = 0;
19454
19455 no = (int)get_tv_number_chk(&argvars[0], &error);
19456 if (error)
19457 return;
19458 error = FALSE;
19459 if (argvars[1].v_type != VAR_UNKNOWN)
19460 retList = get_tv_number_chk(&argvars[1], &error);
19461 if (error)
19462 return;
19463
19464 if (retList == 0)
19465 {
19466 rettv->v_type = VAR_STRING;
19467 rettv->vval.v_string = reg_submatch(no);
19468 }
19469 else
19470 {
19471 rettv->v_type = VAR_LIST;
19472 rettv->vval.v_list = reg_submatch_list(no);
19473 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019474}
19475
19476/*
19477 * "substitute()" function
19478 */
19479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019480f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019481{
19482 char_u patbuf[NUMBUFLEN];
19483 char_u subbuf[NUMBUFLEN];
19484 char_u flagsbuf[NUMBUFLEN];
19485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019486 char_u *str = get_tv_string_chk(&argvars[0]);
19487 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19488 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19489 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19490
Bram Moolenaar0d660222005-01-07 21:51:51 +000019491 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019492 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19493 rettv->vval.v_string = NULL;
19494 else
19495 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019496}
19497
19498/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019499 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019502f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503{
19504 int id = 0;
19505#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019506 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507 long col;
19508 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019509 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019511 lnum = get_tv_lnum(argvars); /* -1 on type error */
19512 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19513 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019515 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019516 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019517 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518#endif
19519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019520 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521}
19522
19523/*
19524 * "synIDattr(id, what [, mode])" function
19525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019527f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528{
19529 char_u *p = NULL;
19530#ifdef FEAT_SYN_HL
19531 int id;
19532 char_u *what;
19533 char_u *mode;
19534 char_u modebuf[NUMBUFLEN];
19535 int modec;
19536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 id = get_tv_number(&argvars[0]);
19538 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019539 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019541 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019543 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 modec = 0; /* replace invalid with current */
19545 }
19546 else
19547 {
19548#ifdef FEAT_GUI
19549 if (gui.in_use)
19550 modec = 'g';
19551 else
19552#endif
19553 if (t_colors > 1)
19554 modec = 'c';
19555 else
19556 modec = 't';
19557 }
19558
19559
19560 switch (TOLOWER_ASC(what[0]))
19561 {
19562 case 'b':
19563 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19564 p = highlight_color(id, what, modec);
19565 else /* bold */
19566 p = highlight_has_attr(id, HL_BOLD, modec);
19567 break;
19568
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019569 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 p = highlight_color(id, what, modec);
19571 break;
19572
19573 case 'i':
19574 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19575 p = highlight_has_attr(id, HL_INVERSE, modec);
19576 else /* italic */
19577 p = highlight_has_attr(id, HL_ITALIC, modec);
19578 break;
19579
19580 case 'n': /* name */
19581 p = get_highlight_name(NULL, id - 1);
19582 break;
19583
19584 case 'r': /* reverse */
19585 p = highlight_has_attr(id, HL_INVERSE, modec);
19586 break;
19587
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019588 case 's':
19589 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19590 p = highlight_color(id, what, modec);
19591 else /* standout */
19592 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 break;
19594
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019595 case 'u':
19596 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19597 /* underline */
19598 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19599 else
19600 /* undercurl */
19601 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 break;
19603 }
19604
19605 if (p != NULL)
19606 p = vim_strsave(p);
19607#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019608 rettv->v_type = VAR_STRING;
19609 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610}
19611
19612/*
19613 * "synIDtrans(id)" function
19614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019616f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617{
19618 int id;
19619
19620#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019621 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622
19623 if (id > 0)
19624 id = syn_get_final_id(id);
19625 else
19626#endif
19627 id = 0;
19628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019629 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630}
19631
19632/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019633 * "synconcealed(lnum, col)" function
19634 */
19635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019636f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019637{
19638#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19639 long lnum;
19640 long col;
19641 int syntax_flags = 0;
19642 int cchar;
19643 int matchid = 0;
19644 char_u str[NUMBUFLEN];
19645#endif
19646
19647 rettv->v_type = VAR_LIST;
19648 rettv->vval.v_list = NULL;
19649
19650#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19651 lnum = get_tv_lnum(argvars); /* -1 on type error */
19652 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19653
19654 vim_memset(str, NUL, sizeof(str));
19655
19656 if (rettv_list_alloc(rettv) != FAIL)
19657 {
19658 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19659 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19660 && curwin->w_p_cole > 0)
19661 {
19662 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19663 syntax_flags = get_syntax_info(&matchid);
19664
19665 /* get the conceal character */
19666 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19667 {
19668 cchar = syn_get_sub_char();
19669 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19670 cchar = lcs_conceal;
19671 if (cchar != NUL)
19672 {
19673# ifdef FEAT_MBYTE
19674 if (has_mbyte)
19675 (*mb_char2bytes)(cchar, str);
19676 else
19677# endif
19678 str[0] = cchar;
19679 }
19680 }
19681 }
19682
19683 list_append_number(rettv->vval.v_list,
19684 (syntax_flags & HL_CONCEAL) != 0);
19685 /* -1 to auto-determine strlen */
19686 list_append_string(rettv->vval.v_list, str, -1);
19687 list_append_number(rettv->vval.v_list, matchid);
19688 }
19689#endif
19690}
19691
19692/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019693 * "synstack(lnum, col)" function
19694 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019696f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019697{
19698#ifdef FEAT_SYN_HL
19699 long lnum;
19700 long col;
19701 int i;
19702 int id;
19703#endif
19704
19705 rettv->v_type = VAR_LIST;
19706 rettv->vval.v_list = NULL;
19707
19708#ifdef FEAT_SYN_HL
19709 lnum = get_tv_lnum(argvars); /* -1 on type error */
19710 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19711
19712 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019713 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019714 && rettv_list_alloc(rettv) != FAIL)
19715 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019716 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019717 for (i = 0; ; ++i)
19718 {
19719 id = syn_get_stack_item(i);
19720 if (id < 0)
19721 break;
19722 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19723 break;
19724 }
19725 }
19726#endif
19727}
19728
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019730get_cmd_output_as_rettv(
19731 typval_T *argvars,
19732 typval_T *rettv,
19733 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019735 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019737 char_u *infile = NULL;
19738 char_u buf[NUMBUFLEN];
19739 int err = FALSE;
19740 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019741 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019742 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019744 rettv->v_type = VAR_STRING;
19745 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019746 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019747 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019748
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019749 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019750 {
19751 /*
19752 * Write the string to a temp file, to be used for input of the shell
19753 * command.
19754 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019755 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019756 {
19757 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019758 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019759 }
19760
19761 fd = mch_fopen((char *)infile, WRITEBIN);
19762 if (fd == NULL)
19763 {
19764 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019765 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019766 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019767 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019768 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019769 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19770 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019771 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019772 else
19773 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019774 size_t len;
19775
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019776 p = get_tv_string_buf_chk(&argvars[1], buf);
19777 if (p == NULL)
19778 {
19779 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019780 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019781 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019782 len = STRLEN(p);
19783 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019784 err = TRUE;
19785 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019786 if (fclose(fd) != 0)
19787 err = TRUE;
19788 if (err)
19789 {
19790 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019791 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019792 }
19793 }
19794
Bram Moolenaar52a72462014-08-29 15:53:52 +020019795 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19796 * echoes typeahead, that messes up the display. */
19797 if (!msg_silent)
19798 flags += SHELL_COOKED;
19799
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019800 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019802 int len;
19803 listitem_T *li;
19804 char_u *s = NULL;
19805 char_u *start;
19806 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019807 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808
Bram Moolenaar52a72462014-08-29 15:53:52 +020019809 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019810 if (res == NULL)
19811 goto errret;
19812
19813 list = list_alloc();
19814 if (list == NULL)
19815 goto errret;
19816
19817 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019819 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019820 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019821 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019822 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019823
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019824 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019825 if (s == NULL)
19826 goto errret;
19827
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019828 for (p = s; start < end; ++p, ++start)
19829 *p = *start == NUL ? NL : *start;
19830 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019831
19832 li = listitem_alloc();
19833 if (li == NULL)
19834 {
19835 vim_free(s);
19836 goto errret;
19837 }
19838 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019839 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019840 li->li_tv.vval.v_string = s;
19841 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019843
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019844 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019845 rettv->v_type = VAR_LIST;
19846 rettv->vval.v_list = list;
19847 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019849 else
19850 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019851 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019852#ifdef USE_CR
19853 /* translate <CR> into <NL> */
19854 if (res != NULL)
19855 {
19856 char_u *s;
19857
19858 for (s = res; *s; ++s)
19859 {
19860 if (*s == CAR)
19861 *s = NL;
19862 }
19863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864#else
19865# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019866 /* translate <CR><NL> into <NL> */
19867 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019869 char_u *s, *d;
19870
19871 d = res;
19872 for (s = res; *s; ++s)
19873 {
19874 if (s[0] == CAR && s[1] == NL)
19875 ++s;
19876 *d++ = *s;
19877 }
19878 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880# endif
19881#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019882 rettv->vval.v_string = res;
19883 res = NULL;
19884 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019885
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019886errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019887 if (infile != NULL)
19888 {
19889 mch_remove(infile);
19890 vim_free(infile);
19891 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019892 if (res != NULL)
19893 vim_free(res);
19894 if (list != NULL)
19895 list_free(list, TRUE);
19896}
19897
19898/*
19899 * "system()" function
19900 */
19901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019902f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019903{
19904 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19905}
19906
19907/*
19908 * "systemlist()" function
19909 */
19910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019911f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019912{
19913 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914}
19915
19916/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019917 * "tabpagebuflist()" function
19918 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019920f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019921{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019922#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019923 tabpage_T *tp;
19924 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019925
19926 if (argvars[0].v_type == VAR_UNKNOWN)
19927 wp = firstwin;
19928 else
19929 {
19930 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19931 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019932 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019933 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019934 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019935 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019936 for (; wp != NULL; wp = wp->w_next)
19937 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019938 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019939 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019940 }
19941#endif
19942}
19943
19944
19945/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019946 * "tabpagenr()" function
19947 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019949f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019950{
19951 int nr = 1;
19952#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019953 char_u *arg;
19954
19955 if (argvars[0].v_type != VAR_UNKNOWN)
19956 {
19957 arg = get_tv_string_chk(&argvars[0]);
19958 nr = 0;
19959 if (arg != NULL)
19960 {
19961 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019962 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019963 else
19964 EMSG2(_(e_invexpr2), arg);
19965 }
19966 }
19967 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019968 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019969#endif
19970 rettv->vval.v_number = nr;
19971}
19972
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019973
19974#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019975static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019976
19977/*
19978 * Common code for tabpagewinnr() and winnr().
19979 */
19980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019981get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019982{
19983 win_T *twin;
19984 int nr = 1;
19985 win_T *wp;
19986 char_u *arg;
19987
19988 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19989 if (argvar->v_type != VAR_UNKNOWN)
19990 {
19991 arg = get_tv_string_chk(argvar);
19992 if (arg == NULL)
19993 nr = 0; /* type error; errmsg already given */
19994 else if (STRCMP(arg, "$") == 0)
19995 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19996 else if (STRCMP(arg, "#") == 0)
19997 {
19998 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19999 if (twin == NULL)
20000 nr = 0;
20001 }
20002 else
20003 {
20004 EMSG2(_(e_invexpr2), arg);
20005 nr = 0;
20006 }
20007 }
20008
20009 if (nr > 0)
20010 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20011 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020012 {
20013 if (wp == NULL)
20014 {
20015 /* didn't find it in this tabpage */
20016 nr = 0;
20017 break;
20018 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020019 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020020 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020021 return nr;
20022}
20023#endif
20024
20025/*
20026 * "tabpagewinnr()" function
20027 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020029f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020030{
20031 int nr = 1;
20032#ifdef FEAT_WINDOWS
20033 tabpage_T *tp;
20034
20035 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20036 if (tp == NULL)
20037 nr = 0;
20038 else
20039 nr = get_winnr(tp, &argvars[1]);
20040#endif
20041 rettv->vval.v_number = nr;
20042}
20043
20044
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020045/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020046 * "tagfiles()" function
20047 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020049f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020050{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020051 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020052 tagname_T tn;
20053 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020054
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020055 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020056 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020057 fname = alloc(MAXPATHL);
20058 if (fname == NULL)
20059 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020060
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020061 for (first = TRUE; ; first = FALSE)
20062 if (get_tagfname(&tn, first, fname) == FAIL
20063 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020064 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020065 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020066 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020067}
20068
20069/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020070 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020071 */
20072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020073f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020074{
20075 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020076
20077 tag_pattern = get_tv_string(&argvars[0]);
20078
20079 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020080 if (*tag_pattern == NUL)
20081 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020082
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020083 if (rettv_list_alloc(rettv) == OK)
20084 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020085}
20086
20087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 * "tempname()" function
20089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020091f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092{
20093 static int x = 'A';
20094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020095 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020096 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097
20098 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20099 * names. Skip 'I' and 'O', they are used for shell redirection. */
20100 do
20101 {
20102 if (x == 'Z')
20103 x = '0';
20104 else if (x == '9')
20105 x = 'A';
20106 else
20107 {
20108#ifdef EBCDIC
20109 if (x == 'I')
20110 x = 'J';
20111 else if (x == 'R')
20112 x = 'S';
20113 else
20114#endif
20115 ++x;
20116 }
20117 } while (x == 'I' || x == 'O');
20118}
20119
20120/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020121 * "test(list)" function: Just checking the walls...
20122 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020124f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020125{
20126 /* Used for unit testing. Change the code below to your liking. */
20127#if 0
20128 listitem_T *li;
20129 list_T *l;
20130 char_u *bad, *good;
20131
20132 if (argvars[0].v_type != VAR_LIST)
20133 return;
20134 l = argvars[0].vval.v_list;
20135 if (l == NULL)
20136 return;
20137 li = l->lv_first;
20138 if (li == NULL)
20139 return;
20140 bad = get_tv_string(&li->li_tv);
20141 li = li->li_next;
20142 if (li == NULL)
20143 return;
20144 good = get_tv_string(&li->li_tv);
20145 rettv->vval.v_number = test_edit_score(bad, good);
20146#endif
20147}
20148
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020149#ifdef FEAT_FLOAT
20150/*
20151 * "tan()" function
20152 */
20153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020154f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020155{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020156 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020157
20158 rettv->v_type = VAR_FLOAT;
20159 if (get_float_arg(argvars, &f) == OK)
20160 rettv->vval.v_float = tan(f);
20161 else
20162 rettv->vval.v_float = 0.0;
20163}
20164
20165/*
20166 * "tanh()" function
20167 */
20168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020169f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020170{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020171 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020172
20173 rettv->v_type = VAR_FLOAT;
20174 if (get_float_arg(argvars, &f) == OK)
20175 rettv->vval.v_float = tanh(f);
20176 else
20177 rettv->vval.v_float = 0.0;
20178}
20179#endif
20180
Bram Moolenaar975b5272016-03-15 23:10:59 +010020181#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20182/*
20183 * Get a callback from "arg". It can be a Funcref or a function name.
20184 * When "arg" is zero return an empty string.
20185 * Return NULL for an invalid argument.
20186 */
20187 char_u *
20188get_callback(typval_T *arg, partial_T **pp)
20189{
20190 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20191 {
20192 *pp = arg->vval.v_partial;
20193 return (*pp)->pt_name;
20194 }
20195 *pp = NULL;
20196 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20197 return arg->vval.v_string;
20198 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20199 return (char_u *)"";
20200 EMSG(_("E921: Invalid callback argument"));
20201 return NULL;
20202}
20203#endif
20204
20205#ifdef FEAT_TIMERS
20206/*
20207 * "timer_start(time, callback [, options])" function
20208 */
20209 static void
20210f_timer_start(typval_T *argvars, typval_T *rettv)
20211{
20212 long msec = get_tv_number(&argvars[0]);
20213 timer_T *timer;
20214 int repeat = 0;
20215 char_u *callback;
20216 dict_T *dict;
20217
20218 if (argvars[2].v_type != VAR_UNKNOWN)
20219 {
20220 if (argvars[2].v_type != VAR_DICT
20221 || (dict = argvars[2].vval.v_dict) == NULL)
20222 {
20223 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20224 return;
20225 }
20226 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20227 repeat = get_dict_number(dict, (char_u *)"repeat");
20228 }
20229
20230 timer = create_timer(msec, repeat);
20231 callback = get_callback(&argvars[1], &timer->tr_partial);
20232 if (callback == NULL)
20233 {
20234 stop_timer(timer);
20235 rettv->vval.v_number = -1;
20236 }
20237 else
20238 {
20239 timer->tr_callback = vim_strsave(callback);
20240 rettv->vval.v_number = timer->tr_id;
20241 }
20242}
20243
20244/*
20245 * "timer_stop(timer)" function
20246 */
20247 static void
20248f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20249{
20250 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20251
20252 if (timer != NULL)
20253 stop_timer(timer);
20254}
20255#endif
20256
Bram Moolenaard52d9742005-08-21 22:20:28 +000020257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 * "tolower(string)" function
20259 */
20260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020261f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262{
20263 char_u *p;
20264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020265 p = vim_strsave(get_tv_string(&argvars[0]));
20266 rettv->v_type = VAR_STRING;
20267 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268
20269 if (p != NULL)
20270 while (*p != NUL)
20271 {
20272#ifdef FEAT_MBYTE
20273 int l;
20274
20275 if (enc_utf8)
20276 {
20277 int c, lc;
20278
20279 c = utf_ptr2char(p);
20280 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020281 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 /* TODO: reallocate string when byte count changes. */
20283 if (utf_char2len(lc) == l)
20284 utf_char2bytes(lc, p);
20285 p += l;
20286 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020287 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 p += l; /* skip multi-byte character */
20289 else
20290#endif
20291 {
20292 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20293 ++p;
20294 }
20295 }
20296}
20297
20298/*
20299 * "toupper(string)" function
20300 */
20301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020302f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020304 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020305 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306}
20307
20308/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020309 * "tr(string, fromstr, tostr)" function
20310 */
20311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020312f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020313{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020314 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020315 char_u *fromstr;
20316 char_u *tostr;
20317 char_u *p;
20318#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020319 int inlen;
20320 int fromlen;
20321 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020322 int idx;
20323 char_u *cpstr;
20324 int cplen;
20325 int first = TRUE;
20326#endif
20327 char_u buf[NUMBUFLEN];
20328 char_u buf2[NUMBUFLEN];
20329 garray_T ga;
20330
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020331 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020332 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20333 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020334
20335 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020336 rettv->v_type = VAR_STRING;
20337 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020338 if (fromstr == NULL || tostr == NULL)
20339 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020340 ga_init2(&ga, (int)sizeof(char), 80);
20341
20342#ifdef FEAT_MBYTE
20343 if (!has_mbyte)
20344#endif
20345 /* not multi-byte: fromstr and tostr must be the same length */
20346 if (STRLEN(fromstr) != STRLEN(tostr))
20347 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020348#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020349error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020350#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020351 EMSG2(_(e_invarg2), fromstr);
20352 ga_clear(&ga);
20353 return;
20354 }
20355
20356 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020357 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020358 {
20359#ifdef FEAT_MBYTE
20360 if (has_mbyte)
20361 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020362 inlen = (*mb_ptr2len)(in_str);
20363 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020364 cplen = inlen;
20365 idx = 0;
20366 for (p = fromstr; *p != NUL; p += fromlen)
20367 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020368 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020369 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020370 {
20371 for (p = tostr; *p != NUL; p += tolen)
20372 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020373 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020374 if (idx-- == 0)
20375 {
20376 cplen = tolen;
20377 cpstr = p;
20378 break;
20379 }
20380 }
20381 if (*p == NUL) /* tostr is shorter than fromstr */
20382 goto error;
20383 break;
20384 }
20385 ++idx;
20386 }
20387
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020388 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020389 {
20390 /* Check that fromstr and tostr have the same number of
20391 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020392 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020393 first = FALSE;
20394 for (p = tostr; *p != NUL; p += tolen)
20395 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020396 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020397 --idx;
20398 }
20399 if (idx != 0)
20400 goto error;
20401 }
20402
Bram Moolenaarcde88542015-08-11 19:14:00 +020020403 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020404 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020405 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020406
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020407 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020408 }
20409 else
20410#endif
20411 {
20412 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020413 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020414 if (p != NULL)
20415 ga_append(&ga, tostr[p - fromstr]);
20416 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020417 ga_append(&ga, *in_str);
20418 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020419 }
20420 }
20421
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020422 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020423 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020424 ga_append(&ga, NUL);
20425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020426 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020427}
20428
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020429#ifdef FEAT_FLOAT
20430/*
20431 * "trunc({float})" function
20432 */
20433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020434f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020435{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020436 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020437
20438 rettv->v_type = VAR_FLOAT;
20439 if (get_float_arg(argvars, &f) == OK)
20440 /* trunc() is not in C90, use floor() or ceil() instead. */
20441 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20442 else
20443 rettv->vval.v_float = 0.0;
20444}
20445#endif
20446
Bram Moolenaar8299df92004-07-10 09:47:34 +000020447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 * "type(expr)" function
20449 */
20450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020451f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020453 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020454
20455 switch (argvars[0].v_type)
20456 {
20457 case VAR_NUMBER: n = 0; break;
20458 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020459 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020460 case VAR_FUNC: n = 2; break;
20461 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020462 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020463 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020464 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020465 if (argvars[0].vval.v_number == VVAL_FALSE
20466 || argvars[0].vval.v_number == VVAL_TRUE)
20467 n = 6;
20468 else
20469 n = 7;
20470 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020471 case VAR_JOB: n = 8; break;
20472 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020473 case VAR_UNKNOWN:
20474 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20475 n = -1;
20476 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020477 }
20478 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479}
20480
20481/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020482 * "undofile(name)" function
20483 */
20484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020485f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020486{
20487 rettv->v_type = VAR_STRING;
20488#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020489 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020490 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020491
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020492 if (*fname == NUL)
20493 {
20494 /* If there is no file name there will be no undo file. */
20495 rettv->vval.v_string = NULL;
20496 }
20497 else
20498 {
20499 char_u *ffname = FullName_save(fname, FALSE);
20500
20501 if (ffname != NULL)
20502 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20503 vim_free(ffname);
20504 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020505 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020506#else
20507 rettv->vval.v_string = NULL;
20508#endif
20509}
20510
20511/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020512 * "undotree()" function
20513 */
20514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020515f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020516{
20517 if (rettv_dict_alloc(rettv) == OK)
20518 {
20519 dict_T *dict = rettv->vval.v_dict;
20520 list_T *list;
20521
Bram Moolenaar730cde92010-06-27 05:18:54 +020020522 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020523 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020524 dict_add_nr_str(dict, "save_last",
20525 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020526 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20527 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020528 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020529
20530 list = list_alloc();
20531 if (list != NULL)
20532 {
20533 u_eval_tree(curbuf->b_u_oldhead, list);
20534 dict_add_list(dict, "entries", list);
20535 }
20536 }
20537}
20538
20539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020540 * "values(dict)" function
20541 */
20542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020543f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020544{
20545 dict_list(argvars, rettv, 1);
20546}
20547
20548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 * "virtcol(string)" function
20550 */
20551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020552f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553{
20554 colnr_T vcol = 0;
20555 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020556 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020558 fp = var2fpos(&argvars[0], FALSE, &fnum);
20559 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20560 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 {
20562 getvvcol(curwin, fp, NULL, NULL, &vcol);
20563 ++vcol;
20564 }
20565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020566 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567}
20568
20569/*
20570 * "visualmode()" function
20571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020573f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 char_u str[2];
20576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020577 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 str[0] = curbuf->b_visual_mode_eval;
20579 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020580 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581
20582 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020583 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585}
20586
20587/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020588 * "wildmenumode()" function
20589 */
20590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020591f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020592{
20593#ifdef FEAT_WILDMENU
20594 if (wild_menu_showing)
20595 rettv->vval.v_number = 1;
20596#endif
20597}
20598
20599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 * "winbufnr(nr)" function
20601 */
20602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020603f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604{
20605 win_T *wp;
20606
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020607 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020609 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612}
20613
20614/*
20615 * "wincol()" function
20616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020618f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619{
20620 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020621 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622}
20623
20624/*
20625 * "winheight(nr)" function
20626 */
20627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020628f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629{
20630 win_T *wp;
20631
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020632 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020636 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637}
20638
20639/*
20640 * "winline()" function
20641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020643f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644{
20645 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020646 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647}
20648
20649/*
20650 * "winnr()" function
20651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020653f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654{
20655 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020656
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020658 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020660 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661}
20662
20663/*
20664 * "winrestcmd()" function
20665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020667f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668{
20669#ifdef FEAT_WINDOWS
20670 win_T *wp;
20671 int winnr = 1;
20672 garray_T ga;
20673 char_u buf[50];
20674
20675 ga_init2(&ga, (int)sizeof(char), 70);
20676 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20677 {
20678 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20679 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20681 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 ++winnr;
20683 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020684 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020686 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020688 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020690 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691}
20692
20693/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020694 * "winrestview()" function
20695 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020697f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020698{
20699 dict_T *dict;
20700
20701 if (argvars[0].v_type != VAR_DICT
20702 || (dict = argvars[0].vval.v_dict) == NULL)
20703 EMSG(_(e_invarg));
20704 else
20705 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020706 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20707 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20708 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20709 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020710#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020711 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20712 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020713#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020714 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20715 {
20716 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20717 curwin->w_set_curswant = FALSE;
20718 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020719
Bram Moolenaar82c25852014-05-28 16:47:16 +020020720 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20721 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020722#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020723 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20724 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020725#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020726 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20727 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20728 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20729 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020730
20731 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020732 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020733# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020734 win_new_width(curwin, W_WIDTH(curwin));
20735# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020736 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020737
Bram Moolenaarb851a962014-10-31 15:45:52 +010020738 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020739 curwin->w_topline = 1;
20740 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20741 curwin->w_topline = curbuf->b_ml.ml_line_count;
20742#ifdef FEAT_DIFF
20743 check_topfill(curwin, TRUE);
20744#endif
20745 }
20746}
20747
20748/*
20749 * "winsaveview()" function
20750 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020752f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020753{
20754 dict_T *dict;
20755
Bram Moolenaara800b422010-06-27 01:15:55 +020020756 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020757 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020758 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020759
20760 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20761 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20762#ifdef FEAT_VIRTUALEDIT
20763 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20764#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020765 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020766 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20767
20768 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20769#ifdef FEAT_DIFF
20770 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20771#endif
20772 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20773 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20774}
20775
20776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 * "winwidth(nr)" function
20778 */
20779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020780f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781{
20782 win_T *wp;
20783
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020784 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020788#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020789 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020791 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792#endif
20793}
20794
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020796 * "wordcount()" function
20797 */
20798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020799f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020800{
20801 if (rettv_dict_alloc(rettv) == FAIL)
20802 return;
20803 cursor_pos_info(rettv->vval.v_dict);
20804}
20805
20806/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020807 * Write list of strings to file
20808 */
20809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020810write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020811{
20812 listitem_T *li;
20813 int c;
20814 int ret = OK;
20815 char_u *s;
20816
20817 for (li = list->lv_first; li != NULL; li = li->li_next)
20818 {
20819 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20820 {
20821 if (*s == '\n')
20822 c = putc(NUL, fd);
20823 else
20824 c = putc(*s, fd);
20825 if (c == EOF)
20826 {
20827 ret = FAIL;
20828 break;
20829 }
20830 }
20831 if (!binary || li->li_next != NULL)
20832 if (putc('\n', fd) == EOF)
20833 {
20834 ret = FAIL;
20835 break;
20836 }
20837 if (ret == FAIL)
20838 {
20839 EMSG(_(e_write));
20840 break;
20841 }
20842 }
20843 return ret;
20844}
20845
20846/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020847 * "writefile()" function
20848 */
20849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020850f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020851{
20852 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020853 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020854 char_u *fname;
20855 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020856 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020857
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020858 if (check_restricted() || check_secure())
20859 return;
20860
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020861 if (argvars[0].v_type != VAR_LIST)
20862 {
20863 EMSG2(_(e_listarg), "writefile()");
20864 return;
20865 }
20866 if (argvars[0].vval.v_list == NULL)
20867 return;
20868
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020869 if (argvars[2].v_type != VAR_UNKNOWN)
20870 {
20871 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20872 binary = TRUE;
20873 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20874 append = TRUE;
20875 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020876
20877 /* Always open the file in binary mode, library functions have a mind of
20878 * their own about CR-LF conversion. */
20879 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020880 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20881 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020882 {
20883 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20884 ret = -1;
20885 }
20886 else
20887 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020888 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20889 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020890 fclose(fd);
20891 }
20892
20893 rettv->vval.v_number = ret;
20894}
20895
20896/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020897 * "xor(expr, expr)" function
20898 */
20899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020900f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020901{
20902 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20903 ^ get_tv_number_chk(&argvars[1], NULL);
20904}
20905
20906
20907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020909 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 */
20911 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020912var2fpos(
20913 typval_T *varp,
20914 int dollar_lnum, /* TRUE when $ is last line */
20915 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020917 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020919 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920
Bram Moolenaara5525202006-03-02 22:52:09 +000020921 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020922 if (varp->v_type == VAR_LIST)
20923 {
20924 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020925 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020926 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020927 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020928
20929 l = varp->vval.v_list;
20930 if (l == NULL)
20931 return NULL;
20932
20933 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020934 pos.lnum = list_find_nr(l, 0L, &error);
20935 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020936 return NULL; /* invalid line number */
20937
20938 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020939 pos.col = list_find_nr(l, 1L, &error);
20940 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020941 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020942 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020943
20944 /* We accept "$" for the column number: last column. */
20945 li = list_find(l, 1L);
20946 if (li != NULL && li->li_tv.v_type == VAR_STRING
20947 && li->li_tv.vval.v_string != NULL
20948 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20949 pos.col = len + 1;
20950
Bram Moolenaara5525202006-03-02 22:52:09 +000020951 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020952 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020953 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020954 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020955
Bram Moolenaara5525202006-03-02 22:52:09 +000020956#ifdef FEAT_VIRTUALEDIT
20957 /* Get the virtual offset. Defaults to zero. */
20958 pos.coladd = list_find_nr(l, 2L, &error);
20959 if (error)
20960 pos.coladd = 0;
20961#endif
20962
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020963 return &pos;
20964 }
20965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020966 name = get_tv_string_chk(varp);
20967 if (name == NULL)
20968 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020969 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020971 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20972 {
20973 if (VIsual_active)
20974 return &VIsual;
20975 return &curwin->w_cursor;
20976 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020977 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020979 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20981 return NULL;
20982 return pp;
20983 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020984
20985#ifdef FEAT_VIRTUALEDIT
20986 pos.coladd = 0;
20987#endif
20988
Bram Moolenaar477933c2007-07-17 14:32:23 +000020989 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020990 {
20991 pos.col = 0;
20992 if (name[1] == '0') /* "w0": first visible line */
20993 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020994 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020995 pos.lnum = curwin->w_topline;
20996 return &pos;
20997 }
20998 else if (name[1] == '$') /* "w$": last visible line */
20999 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021000 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021001 pos.lnum = curwin->w_botline - 1;
21002 return &pos;
21003 }
21004 }
21005 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021007 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 {
21009 pos.lnum = curbuf->b_ml.ml_line_count;
21010 pos.col = 0;
21011 }
21012 else
21013 {
21014 pos.lnum = curwin->w_cursor.lnum;
21015 pos.col = (colnr_T)STRLEN(ml_get_curline());
21016 }
21017 return &pos;
21018 }
21019 return NULL;
21020}
21021
21022/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021023 * Convert list in "arg" into a position and optional file number.
21024 * When "fnump" is NULL there is no file number, only 3 items.
21025 * Note that the column is passed on as-is, the caller may want to decrement
21026 * it to use 1 for the first column.
21027 * Return FAIL when conversion is not possible, doesn't check the position for
21028 * validity.
21029 */
21030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021031list2fpos(
21032 typval_T *arg,
21033 pos_T *posp,
21034 int *fnump,
21035 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021036{
21037 list_T *l = arg->vval.v_list;
21038 long i = 0;
21039 long n;
21040
Bram Moolenaar493c1782014-05-28 14:34:46 +020021041 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21042 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021043 if (arg->v_type != VAR_LIST
21044 || l == NULL
21045 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021046 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021047 return FAIL;
21048
21049 if (fnump != NULL)
21050 {
21051 n = list_find_nr(l, i++, NULL); /* fnum */
21052 if (n < 0)
21053 return FAIL;
21054 if (n == 0)
21055 n = curbuf->b_fnum; /* current buffer */
21056 *fnump = n;
21057 }
21058
21059 n = list_find_nr(l, i++, NULL); /* lnum */
21060 if (n < 0)
21061 return FAIL;
21062 posp->lnum = n;
21063
21064 n = list_find_nr(l, i++, NULL); /* col */
21065 if (n < 0)
21066 return FAIL;
21067 posp->col = n;
21068
21069#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021070 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021071 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021072 posp->coladd = 0;
21073 else
21074 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021075#endif
21076
Bram Moolenaar493c1782014-05-28 14:34:46 +020021077 if (curswantp != NULL)
21078 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21079
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021080 return OK;
21081}
21082
21083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 * Get the length of an environment variable name.
21085 * Advance "arg" to the first character after the name.
21086 * Return 0 for error.
21087 */
21088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021089get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090{
21091 char_u *p;
21092 int len;
21093
21094 for (p = *arg; vim_isIDc(*p); ++p)
21095 ;
21096 if (p == *arg) /* no name found */
21097 return 0;
21098
21099 len = (int)(p - *arg);
21100 *arg = p;
21101 return len;
21102}
21103
21104/*
21105 * Get the length of the name of a function or internal variable.
21106 * "arg" is advanced to the first non-white character after the name.
21107 * Return 0 if something is wrong.
21108 */
21109 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021110get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111{
21112 char_u *p;
21113 int len;
21114
21115 /* Find the end of the name. */
21116 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021117 {
21118 if (*p == ':')
21119 {
21120 /* "s:" is start of "s:var", but "n:" is not and can be used in
21121 * slice "[n:]". Also "xx:" is not a namespace. */
21122 len = (int)(p - *arg);
21123 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21124 || len > 1)
21125 break;
21126 }
21127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 if (p == *arg) /* no name found */
21129 return 0;
21130
21131 len = (int)(p - *arg);
21132 *arg = skipwhite(p);
21133
21134 return len;
21135}
21136
21137/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021138 * Get the length of the name of a variable or function.
21139 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021141 * Return -1 if curly braces expansion failed.
21142 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 * If the name contains 'magic' {}'s, expand them and return the
21144 * expanded name in an allocated string via 'alias' - caller must free.
21145 */
21146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021147get_name_len(
21148 char_u **arg,
21149 char_u **alias,
21150 int evaluate,
21151 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152{
21153 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 char_u *p;
21155 char_u *expr_start;
21156 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157
21158 *alias = NULL; /* default to no alias */
21159
21160 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21161 && (*arg)[2] == (int)KE_SNR)
21162 {
21163 /* hard coded <SNR>, already translated */
21164 *arg += 3;
21165 return get_id_len(arg) + 3;
21166 }
21167 len = eval_fname_script(*arg);
21168 if (len > 0)
21169 {
21170 /* literal "<SID>", "s:" or "<SNR>" */
21171 *arg += len;
21172 }
21173
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021175 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021177 p = find_name_end(*arg, &expr_start, &expr_end,
21178 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 if (expr_start != NULL)
21180 {
21181 char_u *temp_string;
21182
21183 if (!evaluate)
21184 {
21185 len += (int)(p - *arg);
21186 *arg = skipwhite(p);
21187 return len;
21188 }
21189
21190 /*
21191 * Include any <SID> etc in the expanded string:
21192 * Thus the -len here.
21193 */
21194 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21195 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021196 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 *alias = temp_string;
21198 *arg = skipwhite(p);
21199 return (int)STRLEN(temp_string);
21200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201
21202 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021203 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 EMSG2(_(e_invexpr2), *arg);
21205
21206 return len;
21207}
21208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021209/*
21210 * Find the end of a variable or function name, taking care of magic braces.
21211 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21212 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021213 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 * Return a pointer to just after the name. Equal to "arg" if there is no
21215 * valid name.
21216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021218find_name_end(
21219 char_u *arg,
21220 char_u **expr_start,
21221 char_u **expr_end,
21222 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021224 int mb_nest = 0;
21225 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021227 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021229 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021231 *expr_start = NULL;
21232 *expr_end = NULL;
21233 }
21234
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021235 /* Quick check for valid starting character. */
21236 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21237 return arg;
21238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021239 for (p = arg; *p != NUL
21240 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021241 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021242 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021243 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021244 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021245 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021246 if (*p == '\'')
21247 {
21248 /* skip over 'string' to avoid counting [ and ] inside it. */
21249 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21250 ;
21251 if (*p == NUL)
21252 break;
21253 }
21254 else if (*p == '"')
21255 {
21256 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21257 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21258 if (*p == '\\' && p[1] != NUL)
21259 ++p;
21260 if (*p == NUL)
21261 break;
21262 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021263 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21264 {
21265 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021266 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021267 len = (int)(p - arg);
21268 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021269 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021270 break;
21271 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021273 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021275 if (*p == '[')
21276 ++br_nest;
21277 else if (*p == ']')
21278 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021281 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 if (*p == '{')
21284 {
21285 mb_nest++;
21286 if (expr_start != NULL && *expr_start == NULL)
21287 *expr_start = p;
21288 }
21289 else if (*p == '}')
21290 {
21291 mb_nest--;
21292 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21293 *expr_end = p;
21294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 }
21297
21298 return p;
21299}
21300
21301/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021302 * Expands out the 'magic' {}'s in a variable/function name.
21303 * Note that this can call itself recursively, to deal with
21304 * constructs like foo{bar}{baz}{bam}
21305 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21306 * "in_start" ^
21307 * "expr_start" ^
21308 * "expr_end" ^
21309 * "in_end" ^
21310 *
21311 * Returns a new allocated string, which the caller must free.
21312 * Returns NULL for failure.
21313 */
21314 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021315make_expanded_name(
21316 char_u *in_start,
21317 char_u *expr_start,
21318 char_u *expr_end,
21319 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021320{
21321 char_u c1;
21322 char_u *retval = NULL;
21323 char_u *temp_result;
21324 char_u *nextcmd = NULL;
21325
21326 if (expr_end == NULL || in_end == NULL)
21327 return NULL;
21328 *expr_start = NUL;
21329 *expr_end = NUL;
21330 c1 = *in_end;
21331 *in_end = NUL;
21332
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021333 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021334 if (temp_result != NULL && nextcmd == NULL)
21335 {
21336 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21337 + (in_end - expr_end) + 1));
21338 if (retval != NULL)
21339 {
21340 STRCPY(retval, in_start);
21341 STRCAT(retval, temp_result);
21342 STRCAT(retval, expr_end + 1);
21343 }
21344 }
21345 vim_free(temp_result);
21346
21347 *in_end = c1; /* put char back for error messages */
21348 *expr_start = '{';
21349 *expr_end = '}';
21350
21351 if (retval != NULL)
21352 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021353 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021354 if (expr_start != NULL)
21355 {
21356 /* Further expansion! */
21357 temp_result = make_expanded_name(retval, expr_start,
21358 expr_end, temp_result);
21359 vim_free(retval);
21360 retval = temp_result;
21361 }
21362 }
21363
21364 return retval;
21365}
21366
21367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021369 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 */
21371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021372eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021374 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21375}
21376
21377/*
21378 * Return TRUE if character "c" can be used as the first character in a
21379 * variable or function name (excluding '{' and '}').
21380 */
21381 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021382eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021383{
21384 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385}
21386
21387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 * Set number v: variable to "val".
21389 */
21390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021391set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021393 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394}
21395
21396/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021397 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 */
21399 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021400get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021402 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403}
21404
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021405/*
21406 * Get string v: variable value. Uses a static buffer, can only be used once.
21407 */
21408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021409get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021410{
21411 return get_tv_string(&vimvars[idx].vv_tv);
21412}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021413
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021415 * Get List v: variable value. Caller must take care of reference count when
21416 * needed.
21417 */
21418 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021419get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021420{
21421 return vimvars[idx].vv_list;
21422}
21423
21424/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021425 * Set v:char to character "c".
21426 */
21427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021428set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021429{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021430 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021431
21432#ifdef FEAT_MBYTE
21433 if (has_mbyte)
21434 buf[(*mb_char2bytes)(c, buf)] = NUL;
21435 else
21436#endif
21437 {
21438 buf[0] = c;
21439 buf[1] = NUL;
21440 }
21441 set_vim_var_string(VV_CHAR, buf, -1);
21442}
21443
21444/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021445 * Set v:count to "count" and v:count1 to "count1".
21446 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 */
21448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021449set_vcount(
21450 long count,
21451 long count1,
21452 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021454 if (set_prevcount)
21455 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021456 vimvars[VV_COUNT].vv_nr = count;
21457 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458}
21459
21460/*
21461 * Set string v: variable to a copy of "val".
21462 */
21463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021464set_vim_var_string(
21465 int idx,
21466 char_u *val,
21467 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468{
Bram Moolenaara542c682016-01-31 16:28:04 +010021469 clear_tv(&vimvars[idx].vv_di.di_tv);
21470 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021472 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021474 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021476 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477}
21478
21479/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021480 * Set List v: variable to "val".
21481 */
21482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021483set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021484{
Bram Moolenaara542c682016-01-31 16:28:04 +010021485 clear_tv(&vimvars[idx].vv_di.di_tv);
21486 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021487 vimvars[idx].vv_list = val;
21488 if (val != NULL)
21489 ++val->lv_refcount;
21490}
21491
21492/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021493 * Set Dictionary v: variable to "val".
21494 */
21495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021496set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021497{
21498 int todo;
21499 hashitem_T *hi;
21500
Bram Moolenaara542c682016-01-31 16:28:04 +010021501 clear_tv(&vimvars[idx].vv_di.di_tv);
21502 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021503 vimvars[idx].vv_dict = val;
21504 if (val != NULL)
21505 {
21506 ++val->dv_refcount;
21507
21508 /* Set readonly */
21509 todo = (int)val->dv_hashtab.ht_used;
21510 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21511 {
21512 if (HASHITEM_EMPTY(hi))
21513 continue;
21514 --todo;
21515 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21516 }
21517 }
21518}
21519
21520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521 * Set v:register if needed.
21522 */
21523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021524set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525{
21526 char_u regname;
21527
21528 if (c == 0 || c == ' ')
21529 regname = '"';
21530 else
21531 regname = c;
21532 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021533 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 set_vim_var_string(VV_REG, &regname, 1);
21535}
21536
21537/*
21538 * Get or set v:exception. If "oldval" == NULL, return the current value.
21539 * Otherwise, restore the value to "oldval" and return NULL.
21540 * Must always be called in pairs to save and restore v:exception! Does not
21541 * take care of memory allocations.
21542 */
21543 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021544v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545{
21546 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021547 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548
Bram Moolenaare9a41262005-01-15 22:18:47 +000021549 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 return NULL;
21551}
21552
21553/*
21554 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21555 * Otherwise, restore the value to "oldval" and return NULL.
21556 * Must always be called in pairs to save and restore v:throwpoint! Does not
21557 * take care of memory allocations.
21558 */
21559 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021560v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561{
21562 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021563 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564
Bram Moolenaare9a41262005-01-15 22:18:47 +000021565 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 return NULL;
21567}
21568
21569#if defined(FEAT_AUTOCMD) || defined(PROTO)
21570/*
21571 * Set v:cmdarg.
21572 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21573 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21574 * Must always be called in pairs!
21575 */
21576 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021577set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578{
21579 char_u *oldval;
21580 char_u *newval;
21581 unsigned len;
21582
Bram Moolenaare9a41262005-01-15 22:18:47 +000021583 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021584 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021586 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021587 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021588 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 }
21590
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021591 if (eap->force_bin == FORCE_BIN)
21592 len = 6;
21593 else if (eap->force_bin == FORCE_NOBIN)
21594 len = 8;
21595 else
21596 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021597
21598 if (eap->read_edit)
21599 len += 7;
21600
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021601 if (eap->force_ff != 0)
21602 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21603# ifdef FEAT_MBYTE
21604 if (eap->force_enc != 0)
21605 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021606 if (eap->bad_char != 0)
21607 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021608# endif
21609
21610 newval = alloc(len + 1);
21611 if (newval == NULL)
21612 return NULL;
21613
21614 if (eap->force_bin == FORCE_BIN)
21615 sprintf((char *)newval, " ++bin");
21616 else if (eap->force_bin == FORCE_NOBIN)
21617 sprintf((char *)newval, " ++nobin");
21618 else
21619 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021620
21621 if (eap->read_edit)
21622 STRCAT(newval, " ++edit");
21623
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021624 if (eap->force_ff != 0)
21625 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21626 eap->cmd + eap->force_ff);
21627# ifdef FEAT_MBYTE
21628 if (eap->force_enc != 0)
21629 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21630 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021631 if (eap->bad_char == BAD_KEEP)
21632 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21633 else if (eap->bad_char == BAD_DROP)
21634 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21635 else if (eap->bad_char != 0)
21636 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021637# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021638 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021639 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640}
21641#endif
21642
21643/*
21644 * Get the value of internal variable "name".
21645 * Return OK or FAIL.
21646 */
21647 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021648get_var_tv(
21649 char_u *name,
21650 int len, /* length of "name" */
21651 typval_T *rettv, /* NULL when only checking existence */
21652 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21653 int verbose, /* may give error message */
21654 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655{
21656 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021657 typval_T *tv = NULL;
21658 typval_T atv;
21659 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661
21662 /* truncate the name, so that we can use strcmp() */
21663 cc = name[len];
21664 name[len] = NUL;
21665
21666 /*
21667 * Check for "b:changedtick".
21668 */
21669 if (STRCMP(name, "b:changedtick") == 0)
21670 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021671 atv.v_type = VAR_NUMBER;
21672 atv.vval.v_number = curbuf->b_changedtick;
21673 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 }
21675
21676 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 * Check for user-defined variables.
21678 */
21679 else
21680 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021681 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021683 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021684 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021685 if (dip != NULL)
21686 *dip = v;
21687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688 }
21689
Bram Moolenaare9a41262005-01-15 22:18:47 +000021690 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021692 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021693 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 ret = FAIL;
21695 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021696 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021697 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698
21699 name[len] = cc;
21700
21701 return ret;
21702}
21703
21704/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021705 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21706 * Also handle function call with Funcref variable: func(expr)
21707 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21708 */
21709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021710handle_subscript(
21711 char_u **arg,
21712 typval_T *rettv,
21713 int evaluate, /* do more than finding the end */
21714 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021715{
21716 int ret = OK;
21717 dict_T *selfdict = NULL;
21718 char_u *s;
21719 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021720 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021721
21722 while (ret == OK
21723 && (**arg == '['
21724 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021725 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21726 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021727 && !vim_iswhite(*(*arg - 1)))
21728 {
21729 if (**arg == '(')
21730 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021731 partial_T *pt = NULL;
21732
Bram Moolenaard9fba312005-06-26 22:34:35 +000021733 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021734 if (evaluate)
21735 {
21736 functv = *rettv;
21737 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021738
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021739 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021740 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021741 {
21742 pt = functv.vval.v_partial;
21743 s = pt->pt_name;
21744 }
21745 else
21746 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021747 }
21748 else
21749 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021750 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021751 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021752 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021753
21754 /* Clear the funcref afterwards, so that deleting it while
21755 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021756 if (evaluate)
21757 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021758
21759 /* Stop the expression evaluation when immediately aborting on
21760 * error, or when an interrupt occurred or an exception was thrown
21761 * but not caught. */
21762 if (aborting())
21763 {
21764 if (ret == OK)
21765 clear_tv(rettv);
21766 ret = FAIL;
21767 }
21768 dict_unref(selfdict);
21769 selfdict = NULL;
21770 }
21771 else /* **arg == '[' || **arg == '.' */
21772 {
21773 dict_unref(selfdict);
21774 if (rettv->v_type == VAR_DICT)
21775 {
21776 selfdict = rettv->vval.v_dict;
21777 if (selfdict != NULL)
21778 ++selfdict->dv_refcount;
21779 }
21780 else
21781 selfdict = NULL;
21782 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21783 {
21784 clear_tv(rettv);
21785 ret = FAIL;
21786 }
21787 }
21788 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021789
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021790 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21791 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021792 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021793 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21794 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021795 char_u *tofree = NULL;
21796 ufunc_T *fp;
21797 char_u fname_buf[FLEN_FIXED + 1];
21798 int error;
21799
21800 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021801 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021802 fp = find_func(fname);
21803 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021804
21805 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021806 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021807 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021808 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21809
21810 if (pt != NULL)
21811 {
21812 pt->pt_refcount = 1;
21813 pt->pt_dict = selfdict;
21814 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021815 if (rettv->v_type == VAR_FUNC)
21816 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021817 /* Just a function: Take over the function name and use
21818 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021819 pt->pt_name = rettv->vval.v_string;
21820 }
21821 else
21822 {
21823 partial_T *ret_pt = rettv->vval.v_partial;
21824 int i;
21825
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021826 /* Partial: copy the function name, use selfdict and copy
21827 * args. Can't take over name or args, the partial might
21828 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021829 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021830 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021831 if (ret_pt->pt_argc > 0)
21832 {
21833 pt->pt_argv = (typval_T *)alloc(
21834 sizeof(typval_T) * ret_pt->pt_argc);
21835 if (pt->pt_argv == NULL)
21836 /* out of memory: drop the arguments */
21837 pt->pt_argc = 0;
21838 else
21839 {
21840 pt->pt_argc = ret_pt->pt_argc;
21841 for (i = 0; i < pt->pt_argc; i++)
21842 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21843 }
21844 }
21845 partial_unref(ret_pt);
21846 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021847 rettv->v_type = VAR_PARTIAL;
21848 rettv->vval.v_partial = pt;
21849 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021850 }
21851 }
21852
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021853 dict_unref(selfdict);
21854 return ret;
21855}
21856
21857/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021858 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021859 * value).
21860 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021861 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021862alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021863{
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021865}
21866
21867/*
21868 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 * The string "s" must have been allocated, it is consumed.
21870 * Return NULL for out of memory, the variable otherwise.
21871 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021872 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021873alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874{
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021877 rettv = alloc_tv();
21878 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021880 rettv->v_type = VAR_STRING;
21881 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 }
21883 else
21884 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021885 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886}
21887
21888/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021889 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021892free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893{
21894 if (varp != NULL)
21895 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021896 switch (varp->v_type)
21897 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021898 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021899 func_unref(varp->vval.v_string);
21900 /*FALLTHROUGH*/
21901 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021902 vim_free(varp->vval.v_string);
21903 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021904 case VAR_PARTIAL:
21905 partial_unref(varp->vval.v_partial);
21906 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021907 case VAR_LIST:
21908 list_unref(varp->vval.v_list);
21909 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021910 case VAR_DICT:
21911 dict_unref(varp->vval.v_dict);
21912 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021913 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021914#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021915 job_unref(varp->vval.v_job);
21916 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021917#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021918 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021919#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021920 channel_unref(varp->vval.v_channel);
21921 break;
21922#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021923 case VAR_NUMBER:
21924 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021925 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021926 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021927 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 vim_free(varp);
21930 }
21931}
21932
21933/*
21934 * Free the memory for a variable value and set the value to NULL or 0.
21935 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021936 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021937clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938{
21939 if (varp != NULL)
21940 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021941 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021943 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021944 func_unref(varp->vval.v_string);
21945 /*FALLTHROUGH*/
21946 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021947 vim_free(varp->vval.v_string);
21948 varp->vval.v_string = NULL;
21949 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021950 case VAR_PARTIAL:
21951 partial_unref(varp->vval.v_partial);
21952 varp->vval.v_partial = NULL;
21953 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021954 case VAR_LIST:
21955 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021956 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021957 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021958 case VAR_DICT:
21959 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021960 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021961 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021962 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021963 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021964 varp->vval.v_number = 0;
21965 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021966 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021967#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021968 varp->vval.v_float = 0.0;
21969 break;
21970#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021971 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021972#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021973 job_unref(varp->vval.v_job);
21974 varp->vval.v_job = NULL;
21975#endif
21976 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021977 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021978#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021979 channel_unref(varp->vval.v_channel);
21980 varp->vval.v_channel = NULL;
21981#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021982 case VAR_UNKNOWN:
21983 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021985 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 }
21987}
21988
21989/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021990 * Set the value of a variable to NULL without freeing items.
21991 */
21992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021993init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021994{
21995 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021996 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021997}
21998
21999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 * Get the number value of a variable.
22001 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022002 * For incompatible types, return 0.
22003 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22004 * caller of incompatible types: it sets *denote to TRUE if "denote"
22005 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022007 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022008get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022010 int error = FALSE;
22011
22012 return get_tv_number_chk(varp, &error); /* return 0L on error */
22013}
22014
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022015 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022016get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022017{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022018 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022020 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022022 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022023 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022024 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022025#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022026 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022027 break;
22028#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022029 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022030 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022031 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022032 break;
22033 case VAR_STRING:
22034 if (varp->vval.v_string != NULL)
22035 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022036 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022037 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022038 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022039 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022040 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022041 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022042 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022043 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022044 case VAR_SPECIAL:
22045 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22046 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022047 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022048#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022049 EMSG(_("E910: Using a Job as a Number"));
22050 break;
22051#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022052 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022053#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022054 EMSG(_("E913: Using a Channel as a Number"));
22055 break;
22056#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022057 case VAR_UNKNOWN:
22058 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022059 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022061 if (denote == NULL) /* useful for values that must be unsigned */
22062 n = -1;
22063 else
22064 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022065 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066}
22067
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022068#ifdef FEAT_FLOAT
22069 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022070get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022071{
22072 switch (varp->v_type)
22073 {
22074 case VAR_NUMBER:
22075 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022076 case VAR_FLOAT:
22077 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022078 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022079 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022080 EMSG(_("E891: Using a Funcref as a Float"));
22081 break;
22082 case VAR_STRING:
22083 EMSG(_("E892: Using a String as a Float"));
22084 break;
22085 case VAR_LIST:
22086 EMSG(_("E893: Using a List as a Float"));
22087 break;
22088 case VAR_DICT:
22089 EMSG(_("E894: Using a Dictionary as a Float"));
22090 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022091 case VAR_SPECIAL:
22092 EMSG(_("E907: Using a special value as a Float"));
22093 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022094 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022095# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022096 EMSG(_("E911: Using a Job as a Float"));
22097 break;
22098# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022099 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022100# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022101 EMSG(_("E914: Using a Channel as a Float"));
22102 break;
22103# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022104 case VAR_UNKNOWN:
22105 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022106 break;
22107 }
22108 return 0;
22109}
22110#endif
22111
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022113 * Get the lnum from the first argument.
22114 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022115 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 */
22117 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022118get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119{
Bram Moolenaar33570922005-01-25 22:26:29 +000022120 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121 linenr_T lnum;
22122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022123 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124 if (lnum == 0) /* no valid number, try using line() */
22125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022126 rettv.v_type = VAR_NUMBER;
22127 f_line(argvars, &rettv);
22128 lnum = rettv.vval.v_number;
22129 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130 }
22131 return lnum;
22132}
22133
22134/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022135 * Get the lnum from the first argument.
22136 * Also accepts "$", then "buf" is used.
22137 * Returns 0 on error.
22138 */
22139 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022140get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022141{
22142 if (argvars[0].v_type == VAR_STRING
22143 && argvars[0].vval.v_string != NULL
22144 && argvars[0].vval.v_string[0] == '$'
22145 && buf != NULL)
22146 return buf->b_ml.ml_line_count;
22147 return get_tv_number_chk(&argvars[0], NULL);
22148}
22149
22150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 * Get the string value of a variable.
22152 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022153 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22154 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 * If the String variable has never been set, return an empty string.
22156 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022157 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22158 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022160 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022161get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162{
22163 static char_u mybuf[NUMBUFLEN];
22164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022165 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166}
22167
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022168 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022169get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022171 char_u *res = get_tv_string_buf_chk(varp, buf);
22172
22173 return res != NULL ? res : (char_u *)"";
22174}
22175
Bram Moolenaar7d647822014-04-05 21:28:56 +020022176/*
22177 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22178 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022179 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022180get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022181{
22182 static char_u mybuf[NUMBUFLEN];
22183
22184 return get_tv_string_buf_chk(varp, mybuf);
22185}
22186
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022187 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022188get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022189{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022190 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022192 case VAR_NUMBER:
22193 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22194 return buf;
22195 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022196 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022197 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022198 break;
22199 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022200 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022201 break;
22202 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022203 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022204 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022205 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022206#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022207 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022208 break;
22209#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022210 case VAR_STRING:
22211 if (varp->vval.v_string != NULL)
22212 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022213 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022214 case VAR_SPECIAL:
22215 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22216 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022217 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022218#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022219 {
22220 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022221 char *status;
22222
22223 if (job == NULL)
22224 return (char_u *)"no process";
22225 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022226 : job->jv_status == JOB_ENDED ? "dead"
22227 : "run";
22228# ifdef UNIX
22229 vim_snprintf((char *)buf, NUMBUFLEN,
22230 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022231# elif defined(WIN32)
22232 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022233 "process %ld %s",
22234 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022235 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022236# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022237 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022238 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22239# endif
22240 return buf;
22241 }
22242#endif
22243 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022244 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022245#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022246 {
22247 channel_T *channel = varp->vval.v_channel;
22248 char *status = channel_status(channel);
22249
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022250 if (channel == NULL)
22251 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22252 else
22253 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022254 "channel %d %s", channel->ch_id, status);
22255 return buf;
22256 }
22257#endif
22258 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022259 case VAR_UNKNOWN:
22260 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022263 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264}
22265
22266/*
22267 * Find variable "name" in the list of variables.
22268 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022269 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022270 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022271 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022273 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022274find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022277 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278
Bram Moolenaara7043832005-01-21 11:56:39 +000022279 ht = find_var_ht(name, &varname);
22280 if (htp != NULL)
22281 *htp = ht;
22282 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022284 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285}
22286
22287/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022288 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022289 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022291 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022292find_var_in_ht(
22293 hashtab_T *ht,
22294 int htname,
22295 char_u *varname,
22296 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022297{
Bram Moolenaar33570922005-01-25 22:26:29 +000022298 hashitem_T *hi;
22299
22300 if (*varname == NUL)
22301 {
22302 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022303 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022304 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022305 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022306 case 'g': return &globvars_var;
22307 case 'v': return &vimvars_var;
22308 case 'b': return &curbuf->b_bufvar;
22309 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022310#ifdef FEAT_WINDOWS
22311 case 't': return &curtab->tp_winvar;
22312#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022313 case 'l': return current_funccal == NULL
22314 ? NULL : &current_funccal->l_vars_var;
22315 case 'a': return current_funccal == NULL
22316 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022317 }
22318 return NULL;
22319 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022320
22321 hi = hash_find(ht, varname);
22322 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022323 {
22324 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022325 * worked find the variable again. Don't auto-load a script if it was
22326 * loaded already, otherwise it would be loaded every time when
22327 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022328 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022329 {
22330 /* Note: script_autoload() may make "hi" invalid. It must either
22331 * be obtained again or not used. */
22332 if (!script_autoload(varname, FALSE) || aborting())
22333 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022334 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022335 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022336 if (HASHITEM_EMPTY(hi))
22337 return NULL;
22338 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022339 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022340}
22341
22342/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022343 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022344 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022345 * Set "varname" to the start of name without ':'.
22346 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022347 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022348find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022350 hashitem_T *hi;
22351
Bram Moolenaar73627d02015-08-11 15:46:09 +020022352 if (name[0] == NUL)
22353 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 if (name[1] != ':')
22355 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022356 /* The name must not start with a colon or #. */
22357 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 return NULL;
22359 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022360
22361 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022362 hi = hash_find(&compat_hashtab, name);
22363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022364 return &compat_hashtab;
22365
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022367 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022368 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 }
22370 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022371 if (*name == 'g') /* global variable */
22372 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022373 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22374 */
22375 if (vim_strchr(name + 2, ':') != NULL
22376 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022377 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022379 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022381 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022382#ifdef FEAT_WINDOWS
22383 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022384 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022385#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022386 if (*name == 'v') /* v: variable */
22387 return &vimvarht;
22388 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022389 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022390 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022391 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 if (*name == 's' /* script variable */
22393 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22394 return &SCRIPT_VARS(current_SID);
22395 return NULL;
22396}
22397
22398/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022399 * Get function call environment based on bactrace debug level
22400 */
22401 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022402get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022403{
22404 int i;
22405 funccall_T *funccal;
22406 funccall_T *temp_funccal;
22407
22408 funccal = current_funccal;
22409 if (debug_backtrace_level > 0)
22410 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022411 for (i = 0; i < debug_backtrace_level; i++)
22412 {
22413 temp_funccal = funccal->caller;
22414 if (temp_funccal)
22415 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022416 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022417 /* backtrace level overflow. reset to max */
22418 debug_backtrace_level = i;
22419 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022420 }
22421 return funccal;
22422}
22423
22424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022426 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 * Returns NULL when it doesn't exist.
22428 */
22429 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022430get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431{
Bram Moolenaar33570922005-01-25 22:26:29 +000022432 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022434 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 if (v == NULL)
22436 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022437 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438}
22439
22440/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022441 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 * sourcing this script and when executing functions defined in the script.
22443 */
22444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022445new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446{
Bram Moolenaara7043832005-01-21 11:56:39 +000022447 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022448 hashtab_T *ht;
22449 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022450
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22452 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022453 /* Re-allocating ga_data means that an ht_array pointing to
22454 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022455 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022456 for (i = 1; i <= ga_scripts.ga_len; ++i)
22457 {
22458 ht = &SCRIPT_VARS(i);
22459 if (ht->ht_mask == HT_INIT_SIZE - 1)
22460 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022461 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022462 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022463 }
22464
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 while (ga_scripts.ga_len < id)
22466 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022467 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022468 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022469 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 }
22472 }
22473}
22474
22475/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022476 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22477 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 */
22479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022480init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481{
Bram Moolenaar33570922005-01-25 22:26:29 +000022482 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022483 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022484 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022485 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022486 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022487 dict_var->di_tv.vval.v_dict = dict;
22488 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022489 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22491 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492}
22493
22494/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022495 * Unreference a dictionary initialized by init_var_dict().
22496 */
22497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022498unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022499{
22500 /* Now the dict needs to be freed if no one else is using it, go back to
22501 * normal reference counting. */
22502 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22503 dict_unref(dict);
22504}
22505
22506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022508 * Frees all allocated variables and the value they contain.
22509 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 */
22511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022512vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022513{
22514 vars_clear_ext(ht, TRUE);
22515}
22516
22517/*
22518 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22519 */
22520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022521vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522{
Bram Moolenaara7043832005-01-21 11:56:39 +000022523 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022524 hashitem_T *hi;
22525 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526
Bram Moolenaar33570922005-01-25 22:26:29 +000022527 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022528 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022529 for (hi = ht->ht_array; todo > 0; ++hi)
22530 {
22531 if (!HASHITEM_EMPTY(hi))
22532 {
22533 --todo;
22534
Bram Moolenaar33570922005-01-25 22:26:29 +000022535 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022536 * ht_array might change then. hash_clear() takes care of it
22537 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022538 v = HI2DI(hi);
22539 if (free_val)
22540 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022541 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022542 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022543 }
22544 }
22545 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022546 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547}
22548
Bram Moolenaara7043832005-01-21 11:56:39 +000022549/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 * Delete a variable from hashtab "ht" at item "hi".
22551 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022554delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555{
Bram Moolenaar33570922005-01-25 22:26:29 +000022556 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022557
22558 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022559 clear_tv(&di->di_tv);
22560 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561}
22562
22563/*
22564 * List the value of one internal variable.
22565 */
22566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022567list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022569 char_u *tofree;
22570 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022571 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022572
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022573 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022574 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022575 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022576 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577}
22578
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022580list_one_var_a(
22581 char_u *prefix,
22582 char_u *name,
22583 int type,
22584 char_u *string,
22585 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586{
Bram Moolenaar31859182007-08-14 20:41:13 +000022587 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22588 msg_start();
22589 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 if (name != NULL) /* "a:" vars don't have a name stored */
22591 msg_puts(name);
22592 msg_putchar(' ');
22593 msg_advance(22);
22594 if (type == VAR_NUMBER)
22595 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022596 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022597 msg_putchar('*');
22598 else if (type == VAR_LIST)
22599 {
22600 msg_putchar('[');
22601 if (*string == '[')
22602 ++string;
22603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022604 else if (type == VAR_DICT)
22605 {
22606 msg_putchar('{');
22607 if (*string == '{')
22608 ++string;
22609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 else
22611 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022612
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022614
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022615 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022616 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022617 if (*first)
22618 {
22619 msg_clr_eos();
22620 *first = FALSE;
22621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622}
22623
22624/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022625 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 * If the variable already exists, the value is updated.
22627 * Otherwise the variable is created.
22628 */
22629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022630set_var(
22631 char_u *name,
22632 typval_T *tv,
22633 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634{
Bram Moolenaar33570922005-01-25 22:26:29 +000022635 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022637 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022639 ht = find_var_ht(name, &varname);
22640 if (ht == NULL || *varname == NUL)
22641 {
22642 EMSG2(_(e_illvar), name);
22643 return;
22644 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022645 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022646
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022647 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22648 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022649 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022650
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022653 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022654 if (var_check_ro(v->di_flags, name, FALSE)
22655 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022657
22658 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022659 * Handle setting internal v: variables separately where needed to
22660 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022661 */
22662 if (ht == &vimvarht)
22663 {
22664 if (v->di_tv.v_type == VAR_STRING)
22665 {
22666 vim_free(v->di_tv.vval.v_string);
22667 if (copy || tv->v_type != VAR_STRING)
22668 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22669 else
22670 {
22671 /* Take over the string to avoid an extra alloc/free. */
22672 v->di_tv.vval.v_string = tv->vval.v_string;
22673 tv->vval.v_string = NULL;
22674 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022675 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022676 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022677 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022678 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022679 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022680 if (STRCMP(varname, "searchforward") == 0)
22681 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022682#ifdef FEAT_SEARCH_EXTRA
22683 else if (STRCMP(varname, "hlsearch") == 0)
22684 {
22685 no_hlsearch = !v->di_tv.vval.v_number;
22686 redraw_all_later(SOME_VALID);
22687 }
22688#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022689 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022690 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022691 else if (v->di_tv.v_type != tv->v_type)
22692 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022693 }
22694
22695 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 }
22697 else /* add a new variable */
22698 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022699 /* Can't add "v:" variable. */
22700 if (ht == &vimvarht)
22701 {
22702 EMSG2(_(e_illvar), name);
22703 return;
22704 }
22705
Bram Moolenaar92124a32005-06-17 22:03:40 +000022706 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022707 if (!valid_varname(varname))
22708 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022709
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022710 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22711 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022712 if (v == NULL)
22713 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022714 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022715 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022717 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 return;
22719 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022720 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022722
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022723 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022724 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022725 else
22726 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022728 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022729 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731}
22732
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022733/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022734 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022735 * Also give an error message.
22736 */
22737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022738var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022739{
22740 if (flags & DI_FLAGS_RO)
22741 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022742 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022743 return TRUE;
22744 }
22745 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22746 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022747 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022748 return TRUE;
22749 }
22750 return FALSE;
22751}
22752
22753/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022754 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22755 * Also give an error message.
22756 */
22757 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022758var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022759{
22760 if (flags & DI_FLAGS_FIX)
22761 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022762 EMSG2(_("E795: Cannot delete variable %s"),
22763 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022764 return TRUE;
22765 }
22766 return FALSE;
22767}
22768
22769/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022770 * Check if a funcref is assigned to a valid variable name.
22771 * Return TRUE and give an error if not.
22772 */
22773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022774var_check_func_name(
22775 char_u *name, /* points to start of variable name */
22776 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022777{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022778 /* Allow for w: b: s: and t:. */
22779 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022780 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22781 ? name[2] : name[0]))
22782 {
22783 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22784 name);
22785 return TRUE;
22786 }
22787 /* Don't allow hiding a function. When "v" is not NULL we might be
22788 * assigning another function to the same var, the type is checked
22789 * below. */
22790 if (new_var && function_exists(name))
22791 {
22792 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22793 name);
22794 return TRUE;
22795 }
22796 return FALSE;
22797}
22798
22799/*
22800 * Check if a variable name is valid.
22801 * Return FALSE and give an error if not.
22802 */
22803 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022804valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022805{
22806 char_u *p;
22807
22808 for (p = varname; *p != NUL; ++p)
22809 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22810 && *p != AUTOLOAD_CHAR)
22811 {
22812 EMSG2(_(e_illvar), varname);
22813 return FALSE;
22814 }
22815 return TRUE;
22816}
22817
22818/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022819 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022820 * Also give an error message, using "name" or _("name") when use_gettext is
22821 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022822 */
22823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022824tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022825{
22826 if (lock & VAR_LOCKED)
22827 {
22828 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022829 name == NULL ? (char_u *)_("Unknown")
22830 : use_gettext ? (char_u *)_(name)
22831 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022832 return TRUE;
22833 }
22834 if (lock & VAR_FIXED)
22835 {
22836 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022837 name == NULL ? (char_u *)_("Unknown")
22838 : use_gettext ? (char_u *)_(name)
22839 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022840 return TRUE;
22841 }
22842 return FALSE;
22843}
22844
22845/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022846 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022847 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022848 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022849 * It is OK for "from" and "to" to point to the same item. This is used to
22850 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022851 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022853copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022855 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022856 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022857 switch (from->v_type)
22858 {
22859 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022860 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022861 to->vval.v_number = from->vval.v_number;
22862 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022863 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022864#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022865 to->vval.v_float = from->vval.v_float;
22866 break;
22867#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022868 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022869#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022870 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022871 if (to->vval.v_job != NULL)
22872 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022873 break;
22874#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022875 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022876#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022877 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022878 if (to->vval.v_channel != NULL)
22879 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022880 break;
22881#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022882 case VAR_STRING:
22883 case VAR_FUNC:
22884 if (from->vval.v_string == NULL)
22885 to->vval.v_string = NULL;
22886 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022887 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022888 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022889 if (from->v_type == VAR_FUNC)
22890 func_ref(to->vval.v_string);
22891 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022892 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022893 case VAR_PARTIAL:
22894 if (from->vval.v_partial == NULL)
22895 to->vval.v_partial = NULL;
22896 else
22897 {
22898 to->vval.v_partial = from->vval.v_partial;
22899 ++to->vval.v_partial->pt_refcount;
22900 }
22901 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022902 case VAR_LIST:
22903 if (from->vval.v_list == NULL)
22904 to->vval.v_list = NULL;
22905 else
22906 {
22907 to->vval.v_list = from->vval.v_list;
22908 ++to->vval.v_list->lv_refcount;
22909 }
22910 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022911 case VAR_DICT:
22912 if (from->vval.v_dict == NULL)
22913 to->vval.v_dict = NULL;
22914 else
22915 {
22916 to->vval.v_dict = from->vval.v_dict;
22917 ++to->vval.v_dict->dv_refcount;
22918 }
22919 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022920 case VAR_UNKNOWN:
22921 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022922 break;
22923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924}
22925
22926/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022927 * Make a copy of an item.
22928 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022929 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22930 * reference to an already copied list/dict can be used.
22931 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022932 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022934item_copy(
22935 typval_T *from,
22936 typval_T *to,
22937 int deep,
22938 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022939{
22940 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022941 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022942
Bram Moolenaar33570922005-01-25 22:26:29 +000022943 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022944 {
22945 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022946 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022947 }
22948 ++recurse;
22949
22950 switch (from->v_type)
22951 {
22952 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022953 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022954 case VAR_STRING:
22955 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022956 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022957 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022958 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022959 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022960 copy_tv(from, to);
22961 break;
22962 case VAR_LIST:
22963 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022964 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022965 if (from->vval.v_list == NULL)
22966 to->vval.v_list = NULL;
22967 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22968 {
22969 /* use the copy made earlier */
22970 to->vval.v_list = from->vval.v_list->lv_copylist;
22971 ++to->vval.v_list->lv_refcount;
22972 }
22973 else
22974 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22975 if (to->vval.v_list == NULL)
22976 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022977 break;
22978 case VAR_DICT:
22979 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022980 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022981 if (from->vval.v_dict == NULL)
22982 to->vval.v_dict = NULL;
22983 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22984 {
22985 /* use the copy made earlier */
22986 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22987 ++to->vval.v_dict->dv_refcount;
22988 }
22989 else
22990 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22991 if (to->vval.v_dict == NULL)
22992 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022993 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022994 case VAR_UNKNOWN:
22995 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022996 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022997 }
22998 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022999 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023000}
23001
23002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 * ":echo expr1 ..." print each argument separated with a space, add a
23004 * newline at the end.
23005 * ":echon expr1 ..." print each argument plain.
23006 */
23007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023008ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009{
23010 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023012 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 char_u *p;
23014 int needclr = TRUE;
23015 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023016 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017
23018 if (eap->skip)
23019 ++emsg_skip;
23020 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23021 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023022 /* If eval1() causes an error message the text from the command may
23023 * still need to be cleared. E.g., "echo 22,44". */
23024 need_clr_eos = needclr;
23025
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023027 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 {
23029 /*
23030 * Report the invalid expression unless the expression evaluation
23031 * has been cancelled due to an aborting error, an interrupt, or an
23032 * exception.
23033 */
23034 if (!aborting())
23035 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023036 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 break;
23038 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023039 need_clr_eos = FALSE;
23040
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 if (!eap->skip)
23042 {
23043 if (atstart)
23044 {
23045 atstart = FALSE;
23046 /* Call msg_start() after eval1(), evaluating the expression
23047 * may cause a message to appear. */
23048 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023049 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023050 /* Mark the saved text as finishing the line, so that what
23051 * follows is displayed on a new line when scrolling back
23052 * at the more prompt. */
23053 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 }
23057 else if (eap->cmdidx == CMD_echo)
23058 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023059 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023060 if (p != NULL)
23061 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023063 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023065 if (*p != TAB && needclr)
23066 {
23067 /* remove any text still there from the command */
23068 msg_clr_eos();
23069 needclr = FALSE;
23070 }
23071 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072 }
23073 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023074 {
23075#ifdef FEAT_MBYTE
23076 if (has_mbyte)
23077 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023078 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023079
23080 (void)msg_outtrans_len_attr(p, i, echo_attr);
23081 p += i - 1;
23082 }
23083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023085 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023088 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023090 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 arg = skipwhite(arg);
23092 }
23093 eap->nextcmd = check_nextcmd(arg);
23094
23095 if (eap->skip)
23096 --emsg_skip;
23097 else
23098 {
23099 /* remove text that may still be there from the command */
23100 if (needclr)
23101 msg_clr_eos();
23102 if (eap->cmdidx == CMD_echo)
23103 msg_end();
23104 }
23105}
23106
23107/*
23108 * ":echohl {name}".
23109 */
23110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023111ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112{
23113 int id;
23114
23115 id = syn_name2id(eap->arg);
23116 if (id == 0)
23117 echo_attr = 0;
23118 else
23119 echo_attr = syn_id2attr(id);
23120}
23121
23122/*
23123 * ":execute expr1 ..." execute the result of an expression.
23124 * ":echomsg expr1 ..." Print a message
23125 * ":echoerr expr1 ..." Print an error
23126 * Each gets spaces around each argument and a newline at the end for
23127 * echo commands
23128 */
23129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023130ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131{
23132 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023133 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 int ret = OK;
23135 char_u *p;
23136 garray_T ga;
23137 int len;
23138 int save_did_emsg;
23139
23140 ga_init2(&ga, 1, 80);
23141
23142 if (eap->skip)
23143 ++emsg_skip;
23144 while (*arg != NUL && *arg != '|' && *arg != '\n')
23145 {
23146 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023147 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 {
23149 /*
23150 * Report the invalid expression unless the expression evaluation
23151 * has been cancelled due to an aborting error, an interrupt, or an
23152 * exception.
23153 */
23154 if (!aborting())
23155 EMSG2(_(e_invexpr2), p);
23156 ret = FAIL;
23157 break;
23158 }
23159
23160 if (!eap->skip)
23161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023162 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 len = (int)STRLEN(p);
23164 if (ga_grow(&ga, len + 2) == FAIL)
23165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023166 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023167 ret = FAIL;
23168 break;
23169 }
23170 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 ga.ga_len += len;
23174 }
23175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023176 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 arg = skipwhite(arg);
23178 }
23179
23180 if (ret != FAIL && ga.ga_data != NULL)
23181 {
23182 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023185 out_flush();
23186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 else if (eap->cmdidx == CMD_echoerr)
23188 {
23189 /* We don't want to abort following commands, restore did_emsg. */
23190 save_did_emsg = did_emsg;
23191 EMSG((char_u *)ga.ga_data);
23192 if (!force_abort)
23193 did_emsg = save_did_emsg;
23194 }
23195 else if (eap->cmdidx == CMD_execute)
23196 do_cmdline((char_u *)ga.ga_data,
23197 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23198 }
23199
23200 ga_clear(&ga);
23201
23202 if (eap->skip)
23203 --emsg_skip;
23204
23205 eap->nextcmd = check_nextcmd(arg);
23206}
23207
23208/*
23209 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23210 * "arg" points to the "&" or '+' when called, to "option" when returning.
23211 * Returns NULL when no option name found. Otherwise pointer to the char
23212 * after the option name.
23213 */
23214 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023215find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216{
23217 char_u *p = *arg;
23218
23219 ++p;
23220 if (*p == 'g' && p[1] == ':')
23221 {
23222 *opt_flags = OPT_GLOBAL;
23223 p += 2;
23224 }
23225 else if (*p == 'l' && p[1] == ':')
23226 {
23227 *opt_flags = OPT_LOCAL;
23228 p += 2;
23229 }
23230 else
23231 *opt_flags = 0;
23232
23233 if (!ASCII_ISALPHA(*p))
23234 return NULL;
23235 *arg = p;
23236
23237 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23238 p += 4; /* termcap option */
23239 else
23240 while (ASCII_ISALPHA(*p))
23241 ++p;
23242 return p;
23243}
23244
23245/*
23246 * ":function"
23247 */
23248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023249ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250{
23251 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023252 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 int j;
23254 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023256 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 char_u *name = NULL;
23258 char_u *p;
23259 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023260 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 garray_T newargs;
23262 garray_T newlines;
23263 int varargs = FALSE;
23264 int mustend = FALSE;
23265 int flags = 0;
23266 ufunc_T *fp;
23267 int indent;
23268 int nesting;
23269 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023270 dictitem_T *v;
23271 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023272 static int func_nr = 0; /* number for nameless function */
23273 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023274 hashtab_T *ht;
23275 int todo;
23276 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023277 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278
23279 /*
23280 * ":function" without argument: list functions.
23281 */
23282 if (ends_excmd(*eap->arg))
23283 {
23284 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023285 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023286 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023287 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023288 {
23289 if (!HASHITEM_EMPTY(hi))
23290 {
23291 --todo;
23292 fp = HI2UF(hi);
23293 if (!isdigit(*fp->uf_name))
23294 list_func_head(fp, FALSE);
23295 }
23296 }
23297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 eap->nextcmd = check_nextcmd(eap->arg);
23299 return;
23300 }
23301
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023302 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023303 * ":function /pat": list functions matching pattern.
23304 */
23305 if (*eap->arg == '/')
23306 {
23307 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23308 if (!eap->skip)
23309 {
23310 regmatch_T regmatch;
23311
23312 c = *p;
23313 *p = NUL;
23314 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23315 *p = c;
23316 if (regmatch.regprog != NULL)
23317 {
23318 regmatch.rm_ic = p_ic;
23319
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023320 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023321 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23322 {
23323 if (!HASHITEM_EMPTY(hi))
23324 {
23325 --todo;
23326 fp = HI2UF(hi);
23327 if (!isdigit(*fp->uf_name)
23328 && vim_regexec(&regmatch, fp->uf_name, 0))
23329 list_func_head(fp, FALSE);
23330 }
23331 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023332 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023333 }
23334 }
23335 if (*p == '/')
23336 ++p;
23337 eap->nextcmd = check_nextcmd(p);
23338 return;
23339 }
23340
23341 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023342 * Get the function name. There are these situations:
23343 * func normal function name
23344 * "name" == func, "fudi.fd_dict" == NULL
23345 * dict.func new dictionary entry
23346 * "name" == NULL, "fudi.fd_dict" set,
23347 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23348 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023349 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023350 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23351 * dict.func existing dict entry that's not a Funcref
23352 * "name" == NULL, "fudi.fd_dict" set,
23353 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023354 * s:func script-local function name
23355 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023358 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023359 paren = (vim_strchr(p, '(') != NULL);
23360 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 {
23362 /*
23363 * Return on an invalid expression in braces, unless the expression
23364 * evaluation has been cancelled due to an aborting error, an
23365 * interrupt, or an exception.
23366 */
23367 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023369 if (!eap->skip && fudi.fd_newkey != NULL)
23370 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023371 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 else
23375 eap->skip = TRUE;
23376 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023377
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 /* An error in a function call during evaluation of an expression in magic
23379 * braces should not cause the function not to be defined. */
23380 saved_did_emsg = did_emsg;
23381 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382
23383 /*
23384 * ":function func" with only function name: list function.
23385 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023386 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 {
23388 if (!ends_excmd(*skipwhite(p)))
23389 {
23390 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023391 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 }
23393 eap->nextcmd = check_nextcmd(p);
23394 if (eap->nextcmd != NULL)
23395 *p = NUL;
23396 if (!eap->skip && !got_int)
23397 {
23398 fp = find_func(name);
23399 if (fp != NULL)
23400 {
23401 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023402 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023404 if (FUNCLINE(fp, j) == NULL)
23405 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 msg_putchar('\n');
23407 msg_outnum((long)(j + 1));
23408 if (j < 9)
23409 msg_putchar(' ');
23410 if (j < 99)
23411 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023412 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 out_flush(); /* show a line at a time */
23414 ui_breakcheck();
23415 }
23416 if (!got_int)
23417 {
23418 msg_putchar('\n');
23419 msg_puts((char_u *)" endfunction");
23420 }
23421 }
23422 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023423 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023425 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 }
23427
23428 /*
23429 * ":function name(arg1, arg2)" Define function.
23430 */
23431 p = skipwhite(p);
23432 if (*p != '(')
23433 {
23434 if (!eap->skip)
23435 {
23436 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023437 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 }
23439 /* attempt to continue by skipping some text */
23440 if (vim_strchr(p, '(') != NULL)
23441 p = vim_strchr(p, '(');
23442 }
23443 p = skipwhite(p + 1);
23444
23445 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23446 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23447
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023448 if (!eap->skip)
23449 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023450 /* Check the name of the function. Unless it's a dictionary function
23451 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023452 if (name != NULL)
23453 arg = name;
23454 else
23455 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023456 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023457 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23458 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023459 {
23460 if (*arg == K_SPECIAL)
23461 j = 3;
23462 else
23463 j = 0;
23464 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23465 : eval_isnamec(arg[j])))
23466 ++j;
23467 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023468 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023469 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023470 /* Disallow using the g: dict. */
23471 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23472 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023473 }
23474
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 /*
23476 * Isolate the arguments: "arg1, arg2, ...)"
23477 */
23478 while (*p != ')')
23479 {
23480 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23481 {
23482 varargs = TRUE;
23483 p += 3;
23484 mustend = TRUE;
23485 }
23486 else
23487 {
23488 arg = p;
23489 while (ASCII_ISALNUM(*p) || *p == '_')
23490 ++p;
23491 if (arg == p || isdigit(*arg)
23492 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23493 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23494 {
23495 if (!eap->skip)
23496 EMSG2(_("E125: Illegal argument: %s"), arg);
23497 break;
23498 }
23499 if (ga_grow(&newargs, 1) == FAIL)
23500 goto erret;
23501 c = *p;
23502 *p = NUL;
23503 arg = vim_strsave(arg);
23504 if (arg == NULL)
23505 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023506
23507 /* Check for duplicate argument name. */
23508 for (i = 0; i < newargs.ga_len; ++i)
23509 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23510 {
23511 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023512 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023513 goto erret;
23514 }
23515
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23517 *p = c;
23518 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 if (*p == ',')
23520 ++p;
23521 else
23522 mustend = TRUE;
23523 }
23524 p = skipwhite(p);
23525 if (mustend && *p != ')')
23526 {
23527 if (!eap->skip)
23528 EMSG2(_(e_invarg2), eap->arg);
23529 break;
23530 }
23531 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023532 if (*p != ')')
23533 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534 ++p; /* skip the ')' */
23535
Bram Moolenaare9a41262005-01-15 22:18:47 +000023536 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 for (;;)
23538 {
23539 p = skipwhite(p);
23540 if (STRNCMP(p, "range", 5) == 0)
23541 {
23542 flags |= FC_RANGE;
23543 p += 5;
23544 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023545 else if (STRNCMP(p, "dict", 4) == 0)
23546 {
23547 flags |= FC_DICT;
23548 p += 4;
23549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550 else if (STRNCMP(p, "abort", 5) == 0)
23551 {
23552 flags |= FC_ABORT;
23553 p += 5;
23554 }
23555 else
23556 break;
23557 }
23558
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023559 /* When there is a line break use what follows for the function body.
23560 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23561 if (*p == '\n')
23562 line_arg = p + 1;
23563 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 EMSG(_(e_trailing));
23565
23566 /*
23567 * Read the body of the function, until ":endfunction" is found.
23568 */
23569 if (KeyTyped)
23570 {
23571 /* Check if the function already exists, don't let the user type the
23572 * whole function before telling him it doesn't work! For a script we
23573 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023574 if (!eap->skip && !eap->forceit)
23575 {
23576 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23577 EMSG(_(e_funcdict));
23578 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023579 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023582 if (!eap->skip && did_emsg)
23583 goto erret;
23584
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585 msg_putchar('\n'); /* don't overwrite the function name */
23586 cmdline_row = msg_row;
23587 }
23588
23589 indent = 2;
23590 nesting = 0;
23591 for (;;)
23592 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023593 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023594 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023595 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023596 saved_wait_return = FALSE;
23597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023599 sourcing_lnum_off = sourcing_lnum;
23600
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023601 if (line_arg != NULL)
23602 {
23603 /* Use eap->arg, split up in parts by line breaks. */
23604 theline = line_arg;
23605 p = vim_strchr(theline, '\n');
23606 if (p == NULL)
23607 line_arg += STRLEN(line_arg);
23608 else
23609 {
23610 *p = NUL;
23611 line_arg = p + 1;
23612 }
23613 }
23614 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 theline = getcmdline(':', 0L, indent);
23616 else
23617 theline = eap->getline(':', eap->cookie, indent);
23618 if (KeyTyped)
23619 lines_left = Rows - 1;
23620 if (theline == NULL)
23621 {
23622 EMSG(_("E126: Missing :endfunction"));
23623 goto erret;
23624 }
23625
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023626 /* Detect line continuation: sourcing_lnum increased more than one. */
23627 if (sourcing_lnum > sourcing_lnum_off + 1)
23628 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23629 else
23630 sourcing_lnum_off = 0;
23631
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 if (skip_until != NULL)
23633 {
23634 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23635 * don't check for ":endfunc". */
23636 if (STRCMP(theline, skip_until) == 0)
23637 {
23638 vim_free(skip_until);
23639 skip_until = NULL;
23640 }
23641 }
23642 else
23643 {
23644 /* skip ':' and blanks*/
23645 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23646 ;
23647
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023648 /* Check for "endfunction". */
23649 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023651 if (line_arg == NULL)
23652 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 break;
23654 }
23655
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023656 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 * at "end". */
23658 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23659 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023660 else if (STRNCMP(p, "if", 2) == 0
23661 || STRNCMP(p, "wh", 2) == 0
23662 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 || STRNCMP(p, "try", 3) == 0)
23664 indent += 2;
23665
23666 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023667 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023669 if (*p == '!')
23670 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023672 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023673 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023674 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023675 ++nesting;
23676 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677 }
23678 }
23679
23680 /* Check for ":append" or ":insert". */
23681 p = skip_range(p, NULL);
23682 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23683 || (p[0] == 'i'
23684 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23685 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23686 skip_until = vim_strsave((char_u *)".");
23687
23688 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23689 arg = skipwhite(skiptowhite(p));
23690 if (arg[0] == '<' && arg[1] =='<'
23691 && ((p[0] == 'p' && p[1] == 'y'
23692 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23693 || (p[0] == 'p' && p[1] == 'e'
23694 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23695 || (p[0] == 't' && p[1] == 'c'
23696 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023697 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23698 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23700 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023701 || (p[0] == 'm' && p[1] == 'z'
23702 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 ))
23704 {
23705 /* ":python <<" continues until a dot, like ":append" */
23706 p = skipwhite(arg + 2);
23707 if (*p == NUL)
23708 skip_until = vim_strsave((char_u *)".");
23709 else
23710 skip_until = vim_strsave(p);
23711 }
23712 }
23713
23714 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023715 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023716 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023717 if (line_arg == NULL)
23718 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023720 }
23721
23722 /* Copy the line to newly allocated memory. get_one_sourceline()
23723 * allocates 250 bytes per line, this saves 80% on average. The cost
23724 * is an extra alloc/free. */
23725 p = vim_strsave(theline);
23726 if (p != NULL)
23727 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023728 if (line_arg == NULL)
23729 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023730 theline = p;
23731 }
23732
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023733 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23734
23735 /* Add NULL lines for continuation lines, so that the line count is
23736 * equal to the index in the growarray. */
23737 while (sourcing_lnum_off-- > 0)
23738 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023739
23740 /* Check for end of eap->arg. */
23741 if (line_arg != NULL && *line_arg == NUL)
23742 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 }
23744
23745 /* Don't define the function when skipping commands or when an error was
23746 * detected. */
23747 if (eap->skip || did_emsg)
23748 goto erret;
23749
23750 /*
23751 * If there are no errors, add the function
23752 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023753 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023754 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023755 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023756 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023757 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023758 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023759 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023760 goto erret;
23761 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023762
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023763 fp = find_func(name);
23764 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023766 if (!eap->forceit)
23767 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023768 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023769 goto erret;
23770 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023771 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023772 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023773 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023774 name);
23775 goto erret;
23776 }
23777 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023778 ga_clear_strings(&(fp->uf_args));
23779 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023780 vim_free(name);
23781 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783 }
23784 else
23785 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023786 char numbuf[20];
23787
23788 fp = NULL;
23789 if (fudi.fd_newkey == NULL && !eap->forceit)
23790 {
23791 EMSG(_(e_funcdict));
23792 goto erret;
23793 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023794 if (fudi.fd_di == NULL)
23795 {
23796 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023797 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023798 goto erret;
23799 }
23800 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023801 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023802 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023803
23804 /* Give the function a sequential number. Can only be used with a
23805 * Funcref! */
23806 vim_free(name);
23807 sprintf(numbuf, "%d", ++func_nr);
23808 name = vim_strsave((char_u *)numbuf);
23809 if (name == NULL)
23810 goto erret;
23811 }
23812
23813 if (fp == NULL)
23814 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023815 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023816 {
23817 int slen, plen;
23818 char_u *scriptname;
23819
23820 /* Check that the autoload name matches the script name. */
23821 j = FAIL;
23822 if (sourcing_name != NULL)
23823 {
23824 scriptname = autoload_name(name);
23825 if (scriptname != NULL)
23826 {
23827 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023828 plen = (int)STRLEN(p);
23829 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023830 if (slen > plen && fnamecmp(p,
23831 sourcing_name + slen - plen) == 0)
23832 j = OK;
23833 vim_free(scriptname);
23834 }
23835 }
23836 if (j == FAIL)
23837 {
23838 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23839 goto erret;
23840 }
23841 }
23842
23843 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 if (fp == NULL)
23845 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023846
23847 if (fudi.fd_dict != NULL)
23848 {
23849 if (fudi.fd_di == NULL)
23850 {
23851 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023852 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023853 if (fudi.fd_di == NULL)
23854 {
23855 vim_free(fp);
23856 goto erret;
23857 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023858 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23859 {
23860 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023861 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023862 goto erret;
23863 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023864 }
23865 else
23866 /* overwrite existing dict entry */
23867 clear_tv(&fudi.fd_di->di_tv);
23868 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023869 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023870 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023871 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023872
23873 /* behave like "dict" was used */
23874 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023875 }
23876
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023878 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023879 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23880 {
23881 vim_free(fp);
23882 goto erret;
23883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023885 fp->uf_args = newargs;
23886 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023887#ifdef FEAT_PROFILE
23888 fp->uf_tml_count = NULL;
23889 fp->uf_tml_total = NULL;
23890 fp->uf_tml_self = NULL;
23891 fp->uf_profiling = FALSE;
23892 if (prof_def_func())
23893 func_do_profile(fp);
23894#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023895 fp->uf_varargs = varargs;
23896 fp->uf_flags = flags;
23897 fp->uf_calls = 0;
23898 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023899 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900
23901erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 ga_clear_strings(&newargs);
23903 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023904ret_free:
23905 vim_free(skip_until);
23906 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023908 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023909 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910}
23911
23912/*
23913 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023914 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023916 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023917 * TFN_INT: internal function name OK
23918 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023919 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 * Advances "pp" to just after the function name (if no error).
23921 */
23922 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023923trans_function_name(
23924 char_u **pp,
23925 int skip, /* only find the end, don't evaluate */
23926 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023927 funcdict_T *fdp, /* return: info about dictionary used */
23928 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023930 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931 char_u *start;
23932 char_u *end;
23933 int lead;
23934 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023936 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023937
23938 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023939 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023941
23942 /* Check for hard coded <SNR>: already translated function ID (from a user
23943 * command). */
23944 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23945 && (*pp)[2] == (int)KE_SNR)
23946 {
23947 *pp += 3;
23948 len = get_id_len(pp) + 3;
23949 return vim_strnsave(start, len);
23950 }
23951
23952 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23953 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023955 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023957
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023958 /* Note that TFN_ flags use the same values as GLV_ flags. */
23959 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023960 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023961 if (end == start)
23962 {
23963 if (!skip)
23964 EMSG(_("E129: Function name required"));
23965 goto theend;
23966 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023967 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023968 {
23969 /*
23970 * Report an invalid expression in braces, unless the expression
23971 * evaluation has been cancelled due to an aborting error, an
23972 * interrupt, or an exception.
23973 */
23974 if (!aborting())
23975 {
23976 if (end != NULL)
23977 EMSG2(_(e_invarg2), start);
23978 }
23979 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023980 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023981 goto theend;
23982 }
23983
23984 if (lv.ll_tv != NULL)
23985 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023986 if (fdp != NULL)
23987 {
23988 fdp->fd_dict = lv.ll_dict;
23989 fdp->fd_newkey = lv.ll_newkey;
23990 lv.ll_newkey = NULL;
23991 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023992 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023993 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23994 {
23995 name = vim_strsave(lv.ll_tv->vval.v_string);
23996 *pp = end;
23997 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010023998 else if (lv.ll_tv->v_type == VAR_PARTIAL
23999 && lv.ll_tv->vval.v_partial != NULL)
24000 {
24001 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24002 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024003 if (partial != NULL)
24004 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024006 else
24007 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024008 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24009 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024010 EMSG(_(e_funcref));
24011 else
24012 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024013 name = NULL;
24014 }
24015 goto theend;
24016 }
24017
24018 if (lv.ll_name == NULL)
24019 {
24020 /* Error found, but continue after the function name. */
24021 *pp = end;
24022 goto theend;
24023 }
24024
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024025 /* Check if the name is a Funcref. If so, use the value. */
24026 if (lv.ll_exp_name != NULL)
24027 {
24028 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024029 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024030 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024031 if (name == lv.ll_exp_name)
24032 name = NULL;
24033 }
24034 else
24035 {
24036 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024037 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024038 if (name == *pp)
24039 name = NULL;
24040 }
24041 if (name != NULL)
24042 {
24043 name = vim_strsave(name);
24044 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024045 if (STRNCMP(name, "<SNR>", 5) == 0)
24046 {
24047 /* Change "<SNR>" to the byte sequence. */
24048 name[0] = K_SPECIAL;
24049 name[1] = KS_EXTRA;
24050 name[2] = (int)KE_SNR;
24051 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24052 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024053 goto theend;
24054 }
24055
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024056 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024057 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024058 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024059 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24060 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24061 {
24062 /* When there was "s:" already or the name expanded to get a
24063 * leading "s:" then remove it. */
24064 lv.ll_name += 2;
24065 len -= 2;
24066 lead = 2;
24067 }
24068 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024069 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024070 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024071 /* skip over "s:" and "g:" */
24072 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024073 lv.ll_name += 2;
24074 len = (int)(end - lv.ll_name);
24075 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024076
24077 /*
24078 * Copy the function name to allocated memory.
24079 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24080 * Accept <SNR>123_name() outside a script.
24081 */
24082 if (skip)
24083 lead = 0; /* do nothing */
24084 else if (lead > 0)
24085 {
24086 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024087 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24088 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024089 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024090 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024091 if (current_SID <= 0)
24092 {
24093 EMSG(_(e_usingsid));
24094 goto theend;
24095 }
24096 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24097 lead += (int)STRLEN(sid_buf);
24098 }
24099 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024100 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024101 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024102 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024103 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024104 goto theend;
24105 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024106 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024107 {
24108 char_u *cp = vim_strchr(lv.ll_name, ':');
24109
24110 if (cp != NULL && cp < end)
24111 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024112 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024113 goto theend;
24114 }
24115 }
24116
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024117 name = alloc((unsigned)(len + lead + 1));
24118 if (name != NULL)
24119 {
24120 if (lead > 0)
24121 {
24122 name[0] = K_SPECIAL;
24123 name[1] = KS_EXTRA;
24124 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024125 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024126 STRCPY(name + 3, sid_buf);
24127 }
24128 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024129 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024130 }
24131 *pp = end;
24132
24133theend:
24134 clear_lval(&lv);
24135 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136}
24137
24138/*
24139 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24140 * Return 2 if "p" starts with "s:".
24141 * Return 0 otherwise.
24142 */
24143 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024144eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024146 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24147 * the standard library function. */
24148 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24149 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 return 5;
24151 if (p[0] == 's' && p[1] == ':')
24152 return 2;
24153 return 0;
24154}
24155
24156/*
24157 * Return TRUE if "p" starts with "<SID>" or "s:".
24158 * Only works if eval_fname_script() returned non-zero for "p"!
24159 */
24160 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024161eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162{
24163 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24164}
24165
24166/*
24167 * List the head of the function: "name(arg1, arg2)".
24168 */
24169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024170list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171{
24172 int j;
24173
24174 msg_start();
24175 if (indent)
24176 MSG_PUTS(" ");
24177 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024178 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 {
24180 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024181 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 }
24183 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024184 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024186 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 {
24188 if (j)
24189 MSG_PUTS(", ");
24190 msg_puts(FUNCARG(fp, j));
24191 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024192 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193 {
24194 if (j)
24195 MSG_PUTS(", ");
24196 MSG_PUTS("...");
24197 }
24198 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024199 if (fp->uf_flags & FC_ABORT)
24200 MSG_PUTS(" abort");
24201 if (fp->uf_flags & FC_RANGE)
24202 MSG_PUTS(" range");
24203 if (fp->uf_flags & FC_DICT)
24204 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024205 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024206 if (p_verbose > 0)
24207 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208}
24209
24210/*
24211 * Find a function by name, return pointer to it in ufuncs.
24212 * Return NULL for unknown function.
24213 */
24214 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024215find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024217 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024219 hi = hash_find(&func_hashtab, name);
24220 if (!HASHITEM_EMPTY(hi))
24221 return HI2UF(hi);
24222 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223}
24224
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024225#if defined(EXITFREE) || defined(PROTO)
24226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024227free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024228{
24229 hashitem_T *hi;
24230
24231 /* Need to start all over every time, because func_free() may change the
24232 * hash table. */
24233 while (func_hashtab.ht_used > 0)
24234 for (hi = func_hashtab.ht_array; ; ++hi)
24235 if (!HASHITEM_EMPTY(hi))
24236 {
24237 func_free(HI2UF(hi));
24238 break;
24239 }
24240}
24241#endif
24242
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024244translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024245{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024246 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024247 return find_internal_func(name) >= 0;
24248 return find_func(name) != NULL;
24249}
24250
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024251/*
24252 * Return TRUE if a function "name" exists.
24253 */
24254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024255function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024256{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024257 char_u *nm = name;
24258 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024259 int n = FALSE;
24260
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024261 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024262 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024263 nm = skipwhite(nm);
24264
24265 /* Only accept "funcname", "funcname ", "funcname (..." and
24266 * "funcname(...", not "funcname!...". */
24267 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024268 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024269 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024270 return n;
24271}
24272
Bram Moolenaara1544c02013-05-30 12:35:52 +020024273 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024274get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024275{
24276 char_u *nm = name;
24277 char_u *p;
24278
Bram Moolenaar65639032016-03-16 21:40:30 +010024279 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024280
24281 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024282 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024283 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024284
Bram Moolenaara1544c02013-05-30 12:35:52 +020024285 vim_free(p);
24286 return NULL;
24287}
24288
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024289/*
24290 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024291 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24292 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024293 */
24294 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024295builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024296{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024297 char_u *p;
24298
24299 if (!ASCII_ISLOWER(name[0]))
24300 return FALSE;
24301 p = vim_strchr(name, AUTOLOAD_CHAR);
24302 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024303}
24304
Bram Moolenaar05159a02005-02-26 23:04:13 +000024305#if defined(FEAT_PROFILE) || defined(PROTO)
24306/*
24307 * Start profiling function "fp".
24308 */
24309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024310func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024311{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024312 int len = fp->uf_lines.ga_len;
24313
24314 if (len == 0)
24315 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024316 fp->uf_tm_count = 0;
24317 profile_zero(&fp->uf_tm_self);
24318 profile_zero(&fp->uf_tm_total);
24319 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024320 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024321 if (fp->uf_tml_total == NULL)
24322 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024323 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024324 if (fp->uf_tml_self == NULL)
24325 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024326 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024327 fp->uf_tml_idx = -1;
24328 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24329 || fp->uf_tml_self == NULL)
24330 return; /* out of memory */
24331
24332 fp->uf_profiling = TRUE;
24333}
24334
24335/*
24336 * Dump the profiling results for all functions in file "fd".
24337 */
24338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024339func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024340{
24341 hashitem_T *hi;
24342 int todo;
24343 ufunc_T *fp;
24344 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024345 ufunc_T **sorttab;
24346 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024347
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024348 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024349 if (todo == 0)
24350 return; /* nothing to dump */
24351
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024352 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024353
Bram Moolenaar05159a02005-02-26 23:04:13 +000024354 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24355 {
24356 if (!HASHITEM_EMPTY(hi))
24357 {
24358 --todo;
24359 fp = HI2UF(hi);
24360 if (fp->uf_profiling)
24361 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024362 if (sorttab != NULL)
24363 sorttab[st_len++] = fp;
24364
Bram Moolenaar05159a02005-02-26 23:04:13 +000024365 if (fp->uf_name[0] == K_SPECIAL)
24366 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24367 else
24368 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24369 if (fp->uf_tm_count == 1)
24370 fprintf(fd, "Called 1 time\n");
24371 else
24372 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24373 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24374 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24375 fprintf(fd, "\n");
24376 fprintf(fd, "count total (s) self (s)\n");
24377
24378 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24379 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024380 if (FUNCLINE(fp, i) == NULL)
24381 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024382 prof_func_line(fd, fp->uf_tml_count[i],
24383 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024384 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24385 }
24386 fprintf(fd, "\n");
24387 }
24388 }
24389 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024390
24391 if (sorttab != NULL && st_len > 0)
24392 {
24393 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24394 prof_total_cmp);
24395 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24396 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24397 prof_self_cmp);
24398 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24399 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024400
24401 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024402}
Bram Moolenaar73830342005-02-28 22:48:19 +000024403
24404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024405prof_sort_list(
24406 FILE *fd,
24407 ufunc_T **sorttab,
24408 int st_len,
24409 char *title,
24410 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024411{
24412 int i;
24413 ufunc_T *fp;
24414
24415 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24416 fprintf(fd, "count total (s) self (s) function\n");
24417 for (i = 0; i < 20 && i < st_len; ++i)
24418 {
24419 fp = sorttab[i];
24420 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24421 prefer_self);
24422 if (fp->uf_name[0] == K_SPECIAL)
24423 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24424 else
24425 fprintf(fd, " %s()\n", fp->uf_name);
24426 }
24427 fprintf(fd, "\n");
24428}
24429
24430/*
24431 * Print the count and times for one function or function line.
24432 */
24433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024434prof_func_line(
24435 FILE *fd,
24436 int count,
24437 proftime_T *total,
24438 proftime_T *self,
24439 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024440{
24441 if (count > 0)
24442 {
24443 fprintf(fd, "%5d ", count);
24444 if (prefer_self && profile_equal(total, self))
24445 fprintf(fd, " ");
24446 else
24447 fprintf(fd, "%s ", profile_msg(total));
24448 if (!prefer_self && profile_equal(total, self))
24449 fprintf(fd, " ");
24450 else
24451 fprintf(fd, "%s ", profile_msg(self));
24452 }
24453 else
24454 fprintf(fd, " ");
24455}
24456
24457/*
24458 * Compare function for total time sorting.
24459 */
24460 static int
24461#ifdef __BORLANDC__
24462_RTLENTRYF
24463#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024464prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024465{
24466 ufunc_T *p1, *p2;
24467
24468 p1 = *(ufunc_T **)s1;
24469 p2 = *(ufunc_T **)s2;
24470 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24471}
24472
24473/*
24474 * Compare function for self time sorting.
24475 */
24476 static int
24477#ifdef __BORLANDC__
24478_RTLENTRYF
24479#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024480prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024481{
24482 ufunc_T *p1, *p2;
24483
24484 p1 = *(ufunc_T **)s1;
24485 p2 = *(ufunc_T **)s2;
24486 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24487}
24488
Bram Moolenaar05159a02005-02-26 23:04:13 +000024489#endif
24490
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024491/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024492 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024493 * Return TRUE if a package was loaded.
24494 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024495 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024496script_autoload(
24497 char_u *name,
24498 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024499{
24500 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024501 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024502 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024503 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024504
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024505 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024506 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024507 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024508 return FALSE;
24509
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024510 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024511
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024512 /* Find the name in the list of previously loaded package names. Skip
24513 * "autoload/", it's always the same. */
24514 for (i = 0; i < ga_loaded.ga_len; ++i)
24515 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24516 break;
24517 if (!reload && i < ga_loaded.ga_len)
24518 ret = FALSE; /* was loaded already */
24519 else
24520 {
24521 /* Remember the name if it wasn't loaded already. */
24522 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24523 {
24524 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24525 tofree = NULL;
24526 }
24527
24528 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024529 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024530 ret = TRUE;
24531 }
24532
24533 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024534 return ret;
24535}
24536
24537/*
24538 * Return the autoload script name for a function or variable name.
24539 * Returns NULL when out of memory.
24540 */
24541 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024542autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024543{
24544 char_u *p;
24545 char_u *scriptname;
24546
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024547 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024548 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24549 if (scriptname == NULL)
24550 return FALSE;
24551 STRCPY(scriptname, "autoload/");
24552 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024553 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024554 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024555 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024556 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024557 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024558}
24559
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24561
24562/*
24563 * Function given to ExpandGeneric() to obtain the list of user defined
24564 * function names.
24565 */
24566 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024567get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024568{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024569 static long_u done;
24570 static hashitem_T *hi;
24571 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024572
24573 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024575 done = 0;
24576 hi = func_hashtab.ht_array;
24577 }
24578 if (done < func_hashtab.ht_used)
24579 {
24580 if (done++ > 0)
24581 ++hi;
24582 while (HASHITEM_EMPTY(hi))
24583 ++hi;
24584 fp = HI2UF(hi);
24585
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024586 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024587 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024588
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024589 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24590 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591
24592 cat_func_name(IObuff, fp);
24593 if (xp->xp_context != EXPAND_USER_FUNC)
24594 {
24595 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024596 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597 STRCAT(IObuff, ")");
24598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 return IObuff;
24600 }
24601 return NULL;
24602}
24603
24604#endif /* FEAT_CMDL_COMPL */
24605
24606/*
24607 * Copy the function name of "fp" to buffer "buf".
24608 * "buf" must be able to hold the function name plus three bytes.
24609 * Takes care of script-local function names.
24610 */
24611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024612cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024614 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615 {
24616 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024617 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618 }
24619 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024620 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621}
24622
24623/*
24624 * ":delfunction {name}"
24625 */
24626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024627ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024628{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024629 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 char_u *p;
24631 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024632 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633
24634 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024635 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024636 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024638 {
24639 if (fudi.fd_dict != NULL && !eap->skip)
24640 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 if (!ends_excmd(*skipwhite(p)))
24644 {
24645 vim_free(name);
24646 EMSG(_(e_trailing));
24647 return;
24648 }
24649 eap->nextcmd = check_nextcmd(p);
24650 if (eap->nextcmd != NULL)
24651 *p = NUL;
24652
24653 if (!eap->skip)
24654 fp = find_func(name);
24655 vim_free(name);
24656
24657 if (!eap->skip)
24658 {
24659 if (fp == NULL)
24660 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024661 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662 return;
24663 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024664 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665 {
24666 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24667 return;
24668 }
24669
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024670 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024672 /* Delete the dict item that refers to the function, it will
24673 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024674 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024676 else
24677 func_free(fp);
24678 }
24679}
24680
24681/*
24682 * Free a function and remove it from the list of functions.
24683 */
24684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024685func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024686{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024687 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024688
24689 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024690 ga_clear_strings(&(fp->uf_args));
24691 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024692#ifdef FEAT_PROFILE
24693 vim_free(fp->uf_tml_count);
24694 vim_free(fp->uf_tml_total);
24695 vim_free(fp->uf_tml_self);
24696#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024697
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024698 /* remove the function from the function hashtable */
24699 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24700 if (HASHITEM_EMPTY(hi))
24701 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024702 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024703 hash_remove(&func_hashtab, hi);
24704
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024705 vim_free(fp);
24706}
24707
24708/*
24709 * Unreference a Function: decrement the reference count and free it when it
24710 * becomes zero. Only for numbered functions.
24711 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024713func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024714{
24715 ufunc_T *fp;
24716
24717 if (name != NULL && isdigit(*name))
24718 {
24719 fp = find_func(name);
24720 if (fp == NULL)
24721 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024722 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024723 {
24724 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024725 * when "uf_calls" becomes zero. */
24726 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024727 func_free(fp);
24728 }
24729 }
24730}
24731
24732/*
24733 * Count a reference to a Function.
24734 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024736func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024737{
24738 ufunc_T *fp;
24739
24740 if (name != NULL && isdigit(*name))
24741 {
24742 fp = find_func(name);
24743 if (fp == NULL)
24744 EMSG2(_(e_intern2), "func_ref()");
24745 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024746 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747 }
24748}
24749
24750/*
24751 * Call a user function.
24752 */
24753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024754call_user_func(
24755 ufunc_T *fp, /* pointer to function */
24756 int argcount, /* nr of args */
24757 typval_T *argvars, /* arguments */
24758 typval_T *rettv, /* return value */
24759 linenr_T firstline, /* first line of range */
24760 linenr_T lastline, /* last line of range */
24761 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762{
Bram Moolenaar33570922005-01-25 22:26:29 +000024763 char_u *save_sourcing_name;
24764 linenr_T save_sourcing_lnum;
24765 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024766 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024767 int save_did_emsg;
24768 static int depth = 0;
24769 dictitem_T *v;
24770 int fixvar_idx = 0; /* index in fixvar[] */
24771 int i;
24772 int ai;
24773 char_u numbuf[NUMBUFLEN];
24774 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024775 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024776#ifdef FEAT_PROFILE
24777 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024778 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780
24781 /* If depth of calling is getting too high, don't execute the function */
24782 if (depth >= p_mfd)
24783 {
24784 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024785 rettv->v_type = VAR_NUMBER;
24786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 return;
24788 }
24789 ++depth;
24790
24791 line_breakcheck(); /* check for CTRL-C hit */
24792
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024793 fc = (funccall_T *)alloc(sizeof(funccall_T));
24794 fc->caller = current_funccal;
24795 current_funccal = fc;
24796 fc->func = fp;
24797 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024798 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024799 fc->linenr = 0;
24800 fc->returned = FALSE;
24801 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024803 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24804 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024805
Bram Moolenaar33570922005-01-25 22:26:29 +000024806 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024807 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024808 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24809 * each argument variable and saves a lot of time.
24810 */
24811 /*
24812 * Init l: variables.
24813 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024814 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024815 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024816 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024817 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24818 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024819 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024820 name = v->di_key;
24821 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024822 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024823 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024824 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024825 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024826 v->di_tv.vval.v_dict = selfdict;
24827 ++selfdict->dv_refcount;
24828 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024829
Bram Moolenaar33570922005-01-25 22:26:29 +000024830 /*
24831 * Init a: variables.
24832 * Set a:0 to "argcount".
24833 * Set a:000 to a list with room for the "..." arguments.
24834 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024835 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024836 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024837 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024838 /* Use "name" to avoid a warning from some compiler that checks the
24839 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024840 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024841 name = v->di_key;
24842 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024843 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024844 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024845 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024846 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024847 v->di_tv.vval.v_list = &fc->l_varlist;
24848 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24849 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24850 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024851
24852 /*
24853 * Set a:firstline to "firstline" and a:lastline to "lastline".
24854 * Set a:name to named arguments.
24855 * Set a:N to the "..." arguments.
24856 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024857 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024858 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024859 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024860 (varnumber_T)lastline);
24861 for (i = 0; i < argcount; ++i)
24862 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024863 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024864 if (ai < 0)
24865 /* named argument a:name */
24866 name = FUNCARG(fp, i);
24867 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024868 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024869 /* "..." argument a:1, a:2, etc. */
24870 sprintf((char *)numbuf, "%d", ai + 1);
24871 name = numbuf;
24872 }
24873 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24874 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024875 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024876 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24877 }
24878 else
24879 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024880 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24881 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024882 if (v == NULL)
24883 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024884 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024885 }
24886 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024887 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024888
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024889 /* Note: the values are copied directly to avoid alloc/free.
24890 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024891 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024892 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024893
24894 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24895 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024896 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24897 fc->l_listitems[ai].li_tv = argvars[i];
24898 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024899 }
24900 }
24901
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902 /* Don't redraw while executing the function. */
24903 ++RedrawingDisabled;
24904 save_sourcing_name = sourcing_name;
24905 save_sourcing_lnum = sourcing_lnum;
24906 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024907 /* need space for function name + ("function " + 3) or "[number]" */
24908 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24909 + STRLEN(fp->uf_name) + 20;
24910 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 if (sourcing_name != NULL)
24912 {
24913 if (save_sourcing_name != NULL
24914 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024915 sprintf((char *)sourcing_name, "%s[%d]..",
24916 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917 else
24918 STRCPY(sourcing_name, "function ");
24919 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24920
24921 if (p_verbose >= 12)
24922 {
24923 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024924 verbose_enter_scroll();
24925
Bram Moolenaar555b2802005-05-19 21:08:39 +000024926 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 if (p_verbose >= 14)
24928 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024930 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024931 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024932 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933
24934 msg_puts((char_u *)"(");
24935 for (i = 0; i < argcount; ++i)
24936 {
24937 if (i > 0)
24938 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024939 if (argvars[i].v_type == VAR_NUMBER)
24940 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941 else
24942 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024943 /* Do not want errors such as E724 here. */
24944 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024945 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024946 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024947 if (s != NULL)
24948 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024949 if (vim_strsize(s) > MSG_BUF_CLEN)
24950 {
24951 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24952 s = buf;
24953 }
24954 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024955 vim_free(tofree);
24956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 }
24958 }
24959 msg_puts((char_u *)")");
24960 }
24961 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024962
24963 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 --no_wait_return;
24965 }
24966 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024967#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024968 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024969 {
24970 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24971 func_do_profile(fp);
24972 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024973 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024974 {
24975 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024976 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024977 profile_zero(&fp->uf_tm_children);
24978 }
24979 script_prof_save(&wait_start);
24980 }
24981#endif
24982
Bram Moolenaar071d4272004-06-13 20:20:40 +000024983 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024984 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 save_did_emsg = did_emsg;
24986 did_emsg = FALSE;
24987
24988 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024989 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24991
24992 --RedrawingDisabled;
24993
24994 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024995 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024997 clear_tv(rettv);
24998 rettv->v_type = VAR_NUMBER;
24999 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000 }
25001
Bram Moolenaar05159a02005-02-26 23:04:13 +000025002#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025003 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025004 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025005 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025006 profile_end(&call_start);
25007 profile_sub_wait(&wait_start, &call_start);
25008 profile_add(&fp->uf_tm_total, &call_start);
25009 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025010 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025011 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025012 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25013 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025014 }
25015 }
25016#endif
25017
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018 /* when being verbose, mention the return value */
25019 if (p_verbose >= 12)
25020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025022 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025025 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025026 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025027 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025028 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025031 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025032 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025033 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025034 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025035
Bram Moolenaar555b2802005-05-19 21:08:39 +000025036 /* The value may be very long. Skip the middle part, so that we
25037 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025038 * truncate it at the end. Don't want errors such as E724 here. */
25039 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025040 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025041 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025042 if (s != NULL)
25043 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025044 if (vim_strsize(s) > MSG_BUF_CLEN)
25045 {
25046 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25047 s = buf;
25048 }
25049 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025050 vim_free(tofree);
25051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052 }
25053 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025054
25055 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025056 --no_wait_return;
25057 }
25058
25059 vim_free(sourcing_name);
25060 sourcing_name = save_sourcing_name;
25061 sourcing_lnum = save_sourcing_lnum;
25062 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025063#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025064 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025065 script_prof_restore(&wait_start);
25066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067
25068 if (p_verbose >= 12 && sourcing_name != NULL)
25069 {
25070 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025071 verbose_enter_scroll();
25072
Bram Moolenaar555b2802005-05-19 21:08:39 +000025073 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025075
25076 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077 --no_wait_return;
25078 }
25079
25080 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025081 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025083
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025084 /* If the a:000 list and the l: and a: dicts are not referenced we can
25085 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025086 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25087 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25088 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25089 {
25090 free_funccal(fc, FALSE);
25091 }
25092 else
25093 {
25094 hashitem_T *hi;
25095 listitem_T *li;
25096 int todo;
25097
25098 /* "fc" is still in use. This can happen when returning "a:000" or
25099 * assigning "l:" to a global variable.
25100 * Link "fc" in the list for garbage collection later. */
25101 fc->caller = previous_funccal;
25102 previous_funccal = fc;
25103
25104 /* Make a copy of the a: variables, since we didn't do that above. */
25105 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25106 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25107 {
25108 if (!HASHITEM_EMPTY(hi))
25109 {
25110 --todo;
25111 v = HI2DI(hi);
25112 copy_tv(&v->di_tv, &v->di_tv);
25113 }
25114 }
25115
25116 /* Make a copy of the a:000 items, since we didn't do that above. */
25117 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25118 copy_tv(&li->li_tv, &li->li_tv);
25119 }
25120}
25121
25122/*
25123 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025124 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025125 */
25126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025127can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025128{
25129 return (fc->l_varlist.lv_copyID != copyID
25130 && fc->l_vars.dv_copyID != copyID
25131 && fc->l_avars.dv_copyID != copyID);
25132}
25133
25134/*
25135 * Free "fc" and what it contains.
25136 */
25137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025138free_funccal(
25139 funccall_T *fc,
25140 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025141{
25142 listitem_T *li;
25143
25144 /* The a: variables typevals may not have been allocated, only free the
25145 * allocated variables. */
25146 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25147
25148 /* free all l: variables */
25149 vars_clear(&fc->l_vars.dv_hashtab);
25150
25151 /* Free the a:000 variables if they were allocated. */
25152 if (free_val)
25153 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25154 clear_tv(&li->li_tv);
25155
25156 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025157}
25158
25159/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025160 * Add a number variable "name" to dict "dp" with value "nr".
25161 */
25162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025163add_nr_var(
25164 dict_T *dp,
25165 dictitem_T *v,
25166 char *name,
25167 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025168{
25169 STRCPY(v->di_key, name);
25170 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25171 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25172 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025173 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025174 v->di_tv.vval.v_number = nr;
25175}
25176
25177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178 * ":return [expr]"
25179 */
25180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025181ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182{
25183 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025184 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185 int returning = FALSE;
25186
25187 if (current_funccal == NULL)
25188 {
25189 EMSG(_("E133: :return not inside a function"));
25190 return;
25191 }
25192
25193 if (eap->skip)
25194 ++emsg_skip;
25195
25196 eap->nextcmd = NULL;
25197 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025198 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199 {
25200 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025201 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025203 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 }
25205 /* It's safer to return also on error. */
25206 else if (!eap->skip)
25207 {
25208 /*
25209 * Return unless the expression evaluation has been cancelled due to an
25210 * aborting error, an interrupt, or an exception.
25211 */
25212 if (!aborting())
25213 returning = do_return(eap, FALSE, TRUE, NULL);
25214 }
25215
25216 /* When skipping or the return gets pending, advance to the next command
25217 * in this line (!returning). Otherwise, ignore the rest of the line.
25218 * Following lines will be ignored by get_func_line(). */
25219 if (returning)
25220 eap->nextcmd = NULL;
25221 else if (eap->nextcmd == NULL) /* no argument */
25222 eap->nextcmd = check_nextcmd(arg);
25223
25224 if (eap->skip)
25225 --emsg_skip;
25226}
25227
25228/*
25229 * Return from a function. Possibly makes the return pending. Also called
25230 * for a pending return at the ":endtry" or after returning from an extra
25231 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025232 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025233 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 * FALSE when the return gets pending.
25235 */
25236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025237do_return(
25238 exarg_T *eap,
25239 int reanimate,
25240 int is_cmd,
25241 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242{
25243 int idx;
25244 struct condstack *cstack = eap->cstack;
25245
25246 if (reanimate)
25247 /* Undo the return. */
25248 current_funccal->returned = FALSE;
25249
25250 /*
25251 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25252 * not in its finally clause (which then is to be executed next) is found.
25253 * In this case, make the ":return" pending for execution at the ":endtry".
25254 * Otherwise, return normally.
25255 */
25256 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25257 if (idx >= 0)
25258 {
25259 cstack->cs_pending[idx] = CSTP_RETURN;
25260
25261 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025262 /* A pending return again gets pending. "rettv" points to an
25263 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025265 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266 else
25267 {
25268 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025269 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025271 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025273 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 {
25275 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025276 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025277 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025278 else
25279 EMSG(_(e_outofmem));
25280 }
25281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025282 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283
25284 if (reanimate)
25285 {
25286 /* The pending return value could be overwritten by a ":return"
25287 * without argument in a finally clause; reset the default
25288 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025289 current_funccal->rettv->v_type = VAR_NUMBER;
25290 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291 }
25292 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025293 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294 }
25295 else
25296 {
25297 current_funccal->returned = TRUE;
25298
25299 /* If the return is carried out now, store the return value. For
25300 * a return immediately after reanimation, the value is already
25301 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025302 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025304 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025305 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025307 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 }
25309 }
25310
25311 return idx < 0;
25312}
25313
25314/*
25315 * Free the variable with a pending return value.
25316 */
25317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025318discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319{
Bram Moolenaar33570922005-01-25 22:26:29 +000025320 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321}
25322
25323/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025324 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025325 * is an allocated string. Used by report_pending() for verbose messages.
25326 */
25327 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025328get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025330 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025331 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025332 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025334 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025335 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025336 if (s == NULL)
25337 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025338
25339 STRCPY(IObuff, ":return ");
25340 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25341 if (STRLEN(s) + 8 >= IOSIZE)
25342 STRCPY(IObuff + IOSIZE - 4, "...");
25343 vim_free(tofree);
25344 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345}
25346
25347/*
25348 * Get next function line.
25349 * Called by do_cmdline() to get the next line.
25350 * Returns allocated string, or NULL for end of function.
25351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025353get_func_line(
25354 int c UNUSED,
25355 void *cookie,
25356 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357{
Bram Moolenaar33570922005-01-25 22:26:29 +000025358 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025359 ufunc_T *fp = fcp->func;
25360 char_u *retval;
25361 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362
25363 /* If breakpoints have been added/deleted need to check for it. */
25364 if (fcp->dbg_tick != debug_tick)
25365 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025366 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367 sourcing_lnum);
25368 fcp->dbg_tick = debug_tick;
25369 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025370#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025371 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025372 func_line_end(cookie);
25373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374
Bram Moolenaar05159a02005-02-26 23:04:13 +000025375 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025376 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25377 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025378 retval = NULL;
25379 else
25380 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025381 /* Skip NULL lines (continuation lines). */
25382 while (fcp->linenr < gap->ga_len
25383 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25384 ++fcp->linenr;
25385 if (fcp->linenr >= gap->ga_len)
25386 retval = NULL;
25387 else
25388 {
25389 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25390 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025391#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025392 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025393 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025394#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025396 }
25397
25398 /* Did we encounter a breakpoint? */
25399 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25400 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025401 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025402 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025403 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 sourcing_lnum);
25405 fcp->dbg_tick = debug_tick;
25406 }
25407
25408 return retval;
25409}
25410
Bram Moolenaar05159a02005-02-26 23:04:13 +000025411#if defined(FEAT_PROFILE) || defined(PROTO)
25412/*
25413 * Called when starting to read a function line.
25414 * "sourcing_lnum" must be correct!
25415 * When skipping lines it may not actually be executed, but we won't find out
25416 * until later and we need to store the time now.
25417 */
25418 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025419func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025420{
25421 funccall_T *fcp = (funccall_T *)cookie;
25422 ufunc_T *fp = fcp->func;
25423
25424 if (fp->uf_profiling && sourcing_lnum >= 1
25425 && sourcing_lnum <= fp->uf_lines.ga_len)
25426 {
25427 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025428 /* Skip continuation lines. */
25429 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25430 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025431 fp->uf_tml_execed = FALSE;
25432 profile_start(&fp->uf_tml_start);
25433 profile_zero(&fp->uf_tml_children);
25434 profile_get_wait(&fp->uf_tml_wait);
25435 }
25436}
25437
25438/*
25439 * Called when actually executing a function line.
25440 */
25441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025442func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025443{
25444 funccall_T *fcp = (funccall_T *)cookie;
25445 ufunc_T *fp = fcp->func;
25446
25447 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25448 fp->uf_tml_execed = TRUE;
25449}
25450
25451/*
25452 * Called when done with a function line.
25453 */
25454 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025455func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025456{
25457 funccall_T *fcp = (funccall_T *)cookie;
25458 ufunc_T *fp = fcp->func;
25459
25460 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25461 {
25462 if (fp->uf_tml_execed)
25463 {
25464 ++fp->uf_tml_count[fp->uf_tml_idx];
25465 profile_end(&fp->uf_tml_start);
25466 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025467 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025468 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25469 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025470 }
25471 fp->uf_tml_idx = -1;
25472 }
25473}
25474#endif
25475
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476/*
25477 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025478 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025479 */
25480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025481func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482{
Bram Moolenaar33570922005-01-25 22:26:29 +000025483 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484
25485 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25486 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025487 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488 || fcp->returned);
25489}
25490
25491/*
25492 * return TRUE if cookie indicates a function which "abort"s on errors.
25493 */
25494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025495func_has_abort(
25496 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025498 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499}
25500
25501#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25502typedef enum
25503{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025504 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25505 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25506 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025507} var_flavour_T;
25508
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025509static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510
25511 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025512var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513{
25514 char_u *p = varname;
25515
25516 if (ASCII_ISUPPER(*p))
25517 {
25518 while (*(++p))
25519 if (ASCII_ISLOWER(*p))
25520 return VAR_FLAVOUR_SESSION;
25521 return VAR_FLAVOUR_VIMINFO;
25522 }
25523 else
25524 return VAR_FLAVOUR_DEFAULT;
25525}
25526#endif
25527
25528#if defined(FEAT_VIMINFO) || defined(PROTO)
25529/*
25530 * Restore global vars that start with a capital from the viminfo file
25531 */
25532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025533read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534{
25535 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025536 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025537 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025538 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539
25540 if (!writing && (find_viminfo_parameter('!') != NULL))
25541 {
25542 tab = vim_strchr(virp->vir_line + 1, '\t');
25543 if (tab != NULL)
25544 {
25545 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025546 switch (*tab)
25547 {
25548 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025549#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025550 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025551#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025552 case 'D': type = VAR_DICT; break;
25553 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025554 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556
25557 tab = vim_strchr(tab, '\t');
25558 if (tab != NULL)
25559 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025560 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025561 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025562 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025564#ifdef FEAT_FLOAT
25565 else if (type == VAR_FLOAT)
25566 (void)string2float(tab + 1, &tv.vval.v_float);
25567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025569 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025570 if (type == VAR_DICT || type == VAR_LIST)
25571 {
25572 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25573
25574 if (etv == NULL)
25575 /* Failed to parse back the dict or list, use it as a
25576 * string. */
25577 tv.v_type = VAR_STRING;
25578 else
25579 {
25580 vim_free(tv.vval.v_string);
25581 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025582 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025583 }
25584 }
25585
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025586 /* when in a function use global variables */
25587 save_funccal = current_funccal;
25588 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025589 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025590 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025591
25592 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025593 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025594 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25595 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596 }
25597 }
25598 }
25599
25600 return viminfo_readline(virp);
25601}
25602
25603/*
25604 * Write global vars that start with a capital to the viminfo file
25605 */
25606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025607write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608{
Bram Moolenaar33570922005-01-25 22:26:29 +000025609 hashitem_T *hi;
25610 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025611 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025612 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025613 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025614 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025615 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616
25617 if (find_viminfo_parameter('!') == NULL)
25618 return;
25619
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025620 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025621
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025622 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025623 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025625 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025626 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025627 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025628 this_var = HI2DI(hi);
25629 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025630 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025631 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025632 {
25633 case VAR_STRING: s = "STR"; break;
25634 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025635 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025636 case VAR_DICT: s = "DIC"; break;
25637 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025638 case VAR_SPECIAL: s = "XPL"; break;
25639
25640 case VAR_UNKNOWN:
25641 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025642 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025643 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025644 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025645 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025646 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025647 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025648 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025649 if (p != NULL)
25650 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025651 vim_free(tofree);
25652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653 }
25654 }
25655}
25656#endif
25657
25658#if defined(FEAT_SESSION) || defined(PROTO)
25659 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025660store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661{
Bram Moolenaar33570922005-01-25 22:26:29 +000025662 hashitem_T *hi;
25663 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025664 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025665 char_u *p, *t;
25666
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025667 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025668 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025670 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025672 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025673 this_var = HI2DI(hi);
25674 if ((this_var->di_tv.v_type == VAR_NUMBER
25675 || this_var->di_tv.v_type == VAR_STRING)
25676 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025677 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025678 /* Escape special characters with a backslash. Turn a LF and
25679 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025680 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025681 (char_u *)"\\\"\n\r");
25682 if (p == NULL) /* out of memory */
25683 break;
25684 for (t = p; *t != NUL; ++t)
25685 if (*t == '\n')
25686 *t = 'n';
25687 else if (*t == '\r')
25688 *t = 'r';
25689 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025690 this_var->di_key,
25691 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25692 : ' ',
25693 p,
25694 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25695 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025696 || put_eol(fd) == FAIL)
25697 {
25698 vim_free(p);
25699 return FAIL;
25700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701 vim_free(p);
25702 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025703#ifdef FEAT_FLOAT
25704 else if (this_var->di_tv.v_type == VAR_FLOAT
25705 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25706 {
25707 float_T f = this_var->di_tv.vval.v_float;
25708 int sign = ' ';
25709
25710 if (f < 0)
25711 {
25712 f = -f;
25713 sign = '-';
25714 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025715 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025716 this_var->di_key, sign, f) < 0)
25717 || put_eol(fd) == FAIL)
25718 return FAIL;
25719 }
25720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025721 }
25722 }
25723 return OK;
25724}
25725#endif
25726
Bram Moolenaar661b1822005-07-28 22:36:45 +000025727/*
25728 * Display script name where an item was last set.
25729 * Should only be invoked when 'verbose' is non-zero.
25730 */
25731 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025732last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025733{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025734 char_u *p;
25735
Bram Moolenaar661b1822005-07-28 22:36:45 +000025736 if (scriptID != 0)
25737 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025738 p = home_replace_save(NULL, get_scriptname(scriptID));
25739 if (p != NULL)
25740 {
25741 verbose_enter();
25742 MSG_PUTS(_("\n\tLast set from "));
25743 MSG_PUTS(p);
25744 vim_free(p);
25745 verbose_leave();
25746 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025747 }
25748}
25749
Bram Moolenaard812df62008-11-09 12:46:09 +000025750/*
25751 * List v:oldfiles in a nice way.
25752 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025753 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025754ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025755{
25756 list_T *l = vimvars[VV_OLDFILES].vv_list;
25757 listitem_T *li;
25758 int nr = 0;
25759
25760 if (l == NULL)
25761 msg((char_u *)_("No old files"));
25762 else
25763 {
25764 msg_start();
25765 msg_scroll = TRUE;
25766 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25767 {
25768 msg_outnum((long)++nr);
25769 MSG_PUTS(": ");
25770 msg_outtrans(get_tv_string(&li->li_tv));
25771 msg_putchar('\n');
25772 out_flush(); /* output one line at a time */
25773 ui_breakcheck();
25774 }
25775 /* Assume "got_int" was set to truncate the listing. */
25776 got_int = FALSE;
25777
25778#ifdef FEAT_BROWSE_CMD
25779 if (cmdmod.browse)
25780 {
25781 quit_more = FALSE;
25782 nr = prompt_for_number(FALSE);
25783 msg_starthere();
25784 if (nr > 0)
25785 {
25786 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25787 (long)nr);
25788
25789 if (p != NULL)
25790 {
25791 p = expand_env_save(p);
25792 eap->arg = p;
25793 eap->cmdidx = CMD_edit;
25794 cmdmod.browse = FALSE;
25795 do_exedit(eap, NULL);
25796 vim_free(p);
25797 }
25798 }
25799 }
25800#endif
25801 }
25802}
25803
Bram Moolenaar53744302015-07-17 17:38:22 +020025804/* reset v:option_new, v:option_old and v:option_type */
25805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025806reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025807{
25808 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25809 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25810 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25811}
25812
25813
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814#endif /* FEAT_EVAL */
25815
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025817#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818
25819#ifdef WIN3264
25820/*
25821 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25822 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025823static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25824static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25825static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826
25827/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025828 * Get the short path (8.3) for the filename in "fnamep".
25829 * Only works for a valid file name.
25830 * When the path gets longer "fnamep" is changed and the allocated buffer
25831 * is put in "bufp".
25832 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25833 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834 */
25835 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025836get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025837{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025838 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839 char_u *newbuf;
25840
25841 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025842 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025843 if (l > len - 1)
25844 {
25845 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025846 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025847 newbuf = vim_strnsave(*fnamep, l);
25848 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025850
25851 vim_free(*bufp);
25852 *fnamep = *bufp = newbuf;
25853
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025854 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025855 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025856 }
25857
25858 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025859 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860}
25861
25862/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025863 * Get the short path (8.3) for the filename in "fname". The converted
25864 * path is returned in "bufp".
25865 *
25866 * Some of the directories specified in "fname" may not exist. This function
25867 * will shorten the existing directories at the beginning of the path and then
25868 * append the remaining non-existing path.
25869 *
25870 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025871 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025872 * bufp - Pointer to an allocated buffer for the filename.
25873 * fnamelen - Length of the filename pointed to by fname
25874 *
25875 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025876 */
25877 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025878shortpath_for_invalid_fname(
25879 char_u **fname,
25880 char_u **bufp,
25881 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025882{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025883 char_u *short_fname, *save_fname, *pbuf_unused;
25884 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025885 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025886 int old_len, len;
25887 int new_len, sfx_len;
25888 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025889
25890 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025891 old_len = *fnamelen;
25892 save_fname = vim_strnsave(*fname, old_len);
25893 pbuf_unused = NULL;
25894 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025895
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025896 endp = save_fname + old_len - 1; /* Find the end of the copy */
25897 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025898
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025899 /*
25900 * Try shortening the supplied path till it succeeds by removing one
25901 * directory at a time from the tail of the path.
25902 */
25903 len = 0;
25904 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025905 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025906 /* go back one path-separator */
25907 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25908 --endp;
25909 if (endp <= save_fname)
25910 break; /* processed the complete path */
25911
25912 /*
25913 * Replace the path separator with a NUL and try to shorten the
25914 * resulting path.
25915 */
25916 ch = *endp;
25917 *endp = 0;
25918 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025919 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025920 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25921 {
25922 retval = FAIL;
25923 goto theend;
25924 }
25925 *endp = ch; /* preserve the string */
25926
25927 if (len > 0)
25928 break; /* successfully shortened the path */
25929
25930 /* failed to shorten the path. Skip the path separator */
25931 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025932 }
25933
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025934 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025935 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025936 /*
25937 * Succeeded in shortening the path. Now concatenate the shortened
25938 * path with the remaining path at the tail.
25939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025940
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025941 /* Compute the length of the new path. */
25942 sfx_len = (int)(save_endp - endp) + 1;
25943 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025944
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025945 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025947 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025949 /* There is not enough space in the currently allocated string,
25950 * copy it to a buffer big enough. */
25951 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025953 {
25954 retval = FAIL;
25955 goto theend;
25956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025957 }
25958 else
25959 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025960 /* Transfer short_fname to the main buffer (it's big enough),
25961 * unless get_short_pathname() did its work in-place. */
25962 *fname = *bufp = save_fname;
25963 if (short_fname != save_fname)
25964 vim_strncpy(save_fname, short_fname, len);
25965 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025967
25968 /* concat the not-shortened part of the path */
25969 vim_strncpy(*fname + len, endp, sfx_len);
25970 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025971 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025972
25973theend:
25974 vim_free(pbuf_unused);
25975 vim_free(save_fname);
25976
25977 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025978}
25979
25980/*
25981 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025982 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025983 */
25984 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025985shortpath_for_partial(
25986 char_u **fnamep,
25987 char_u **bufp,
25988 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989{
25990 int sepcount, len, tflen;
25991 char_u *p;
25992 char_u *pbuf, *tfname;
25993 int hasTilde;
25994
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025995 /* Count up the path separators from the RHS.. so we know which part
25996 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025998 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 if (vim_ispathsep(*p))
26000 ++sepcount;
26001
26002 /* Need full path first (use expand_env() to remove a "~/") */
26003 hasTilde = (**fnamep == '~');
26004 if (hasTilde)
26005 pbuf = tfname = expand_env_save(*fnamep);
26006 else
26007 pbuf = tfname = FullName_save(*fnamep, FALSE);
26008
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026009 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026010
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026011 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26012 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013
26014 if (len == 0)
26015 {
26016 /* Don't have a valid filename, so shorten the rest of the
26017 * path if we can. This CAN give us invalid 8.3 filenames, but
26018 * there's not a lot of point in guessing what it might be.
26019 */
26020 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026021 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26022 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023 }
26024
26025 /* Count the paths backward to find the beginning of the desired string. */
26026 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026027 {
26028#ifdef FEAT_MBYTE
26029 if (has_mbyte)
26030 p -= mb_head_off(tfname, p);
26031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026032 if (vim_ispathsep(*p))
26033 {
26034 if (sepcount == 0 || (hasTilde && sepcount == 1))
26035 break;
26036 else
26037 sepcount --;
26038 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026040 if (hasTilde)
26041 {
26042 --p;
26043 if (p >= tfname)
26044 *p = '~';
26045 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026046 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026047 }
26048 else
26049 ++p;
26050
26051 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26052 vim_free(*bufp);
26053 *fnamelen = (int)STRLEN(p);
26054 *bufp = pbuf;
26055 *fnamep = p;
26056
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026057 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058}
26059#endif /* WIN3264 */
26060
26061/*
26062 * Adjust a filename, according to a string of modifiers.
26063 * *fnamep must be NUL terminated when called. When returning, the length is
26064 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026065 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026066 * When there is an error, *fnamep is set to NULL.
26067 */
26068 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026069modify_fname(
26070 char_u *src, /* string with modifiers */
26071 int *usedlen, /* characters after src that are used */
26072 char_u **fnamep, /* file name so far */
26073 char_u **bufp, /* buffer for allocated file name or NULL */
26074 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026075{
26076 int valid = 0;
26077 char_u *tail;
26078 char_u *s, *p, *pbuf;
26079 char_u dirname[MAXPATHL];
26080 int c;
26081 int has_fullname = 0;
26082#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026083 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084 int has_shortname = 0;
26085#endif
26086
26087repeat:
26088 /* ":p" - full path/file_name */
26089 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26090 {
26091 has_fullname = 1;
26092
26093 valid |= VALID_PATH;
26094 *usedlen += 2;
26095
26096 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26097 if ((*fnamep)[0] == '~'
26098#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26099 && ((*fnamep)[1] == '/'
26100# ifdef BACKSLASH_IN_FILENAME
26101 || (*fnamep)[1] == '\\'
26102# endif
26103 || (*fnamep)[1] == NUL)
26104
26105#endif
26106 )
26107 {
26108 *fnamep = expand_env_save(*fnamep);
26109 vim_free(*bufp); /* free any allocated file name */
26110 *bufp = *fnamep;
26111 if (*fnamep == NULL)
26112 return -1;
26113 }
26114
26115 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026116 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026117 {
26118 if (vim_ispathsep(*p)
26119 && p[1] == '.'
26120 && (p[2] == NUL
26121 || vim_ispathsep(p[2])
26122 || (p[2] == '.'
26123 && (p[3] == NUL || vim_ispathsep(p[3])))))
26124 break;
26125 }
26126
26127 /* FullName_save() is slow, don't use it when not needed. */
26128 if (*p != NUL || !vim_isAbsName(*fnamep))
26129 {
26130 *fnamep = FullName_save(*fnamep, *p != NUL);
26131 vim_free(*bufp); /* free any allocated file name */
26132 *bufp = *fnamep;
26133 if (*fnamep == NULL)
26134 return -1;
26135 }
26136
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026137#ifdef WIN3264
26138# if _WIN32_WINNT >= 0x0500
26139 if (vim_strchr(*fnamep, '~') != NULL)
26140 {
26141 /* Expand 8.3 filename to full path. Needed to make sure the same
26142 * file does not have two different names.
26143 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26144 p = alloc(_MAX_PATH + 1);
26145 if (p != NULL)
26146 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026147 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026148 {
26149 vim_free(*bufp);
26150 *bufp = *fnamep = p;
26151 }
26152 else
26153 vim_free(p);
26154 }
26155 }
26156# endif
26157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026158 /* Append a path separator to a directory. */
26159 if (mch_isdir(*fnamep))
26160 {
26161 /* Make room for one or two extra characters. */
26162 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26163 vim_free(*bufp); /* free any allocated file name */
26164 *bufp = *fnamep;
26165 if (*fnamep == NULL)
26166 return -1;
26167 add_pathsep(*fnamep);
26168 }
26169 }
26170
26171 /* ":." - path relative to the current directory */
26172 /* ":~" - path relative to the home directory */
26173 /* ":8" - shortname path - postponed till after */
26174 while (src[*usedlen] == ':'
26175 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26176 {
26177 *usedlen += 2;
26178 if (c == '8')
26179 {
26180#ifdef WIN3264
26181 has_shortname = 1; /* Postpone this. */
26182#endif
26183 continue;
26184 }
26185 pbuf = NULL;
26186 /* Need full path first (use expand_env() to remove a "~/") */
26187 if (!has_fullname)
26188 {
26189 if (c == '.' && **fnamep == '~')
26190 p = pbuf = expand_env_save(*fnamep);
26191 else
26192 p = pbuf = FullName_save(*fnamep, FALSE);
26193 }
26194 else
26195 p = *fnamep;
26196
26197 has_fullname = 0;
26198
26199 if (p != NULL)
26200 {
26201 if (c == '.')
26202 {
26203 mch_dirname(dirname, MAXPATHL);
26204 s = shorten_fname(p, dirname);
26205 if (s != NULL)
26206 {
26207 *fnamep = s;
26208 if (pbuf != NULL)
26209 {
26210 vim_free(*bufp); /* free any allocated file name */
26211 *bufp = pbuf;
26212 pbuf = NULL;
26213 }
26214 }
26215 }
26216 else
26217 {
26218 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26219 /* Only replace it when it starts with '~' */
26220 if (*dirname == '~')
26221 {
26222 s = vim_strsave(dirname);
26223 if (s != NULL)
26224 {
26225 *fnamep = s;
26226 vim_free(*bufp);
26227 *bufp = s;
26228 }
26229 }
26230 }
26231 vim_free(pbuf);
26232 }
26233 }
26234
26235 tail = gettail(*fnamep);
26236 *fnamelen = (int)STRLEN(*fnamep);
26237
26238 /* ":h" - head, remove "/file_name", can be repeated */
26239 /* Don't remove the first "/" or "c:\" */
26240 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26241 {
26242 valid |= VALID_HEAD;
26243 *usedlen += 2;
26244 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026245 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026246 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026247 *fnamelen = (int)(tail - *fnamep);
26248#ifdef VMS
26249 if (*fnamelen > 0)
26250 *fnamelen += 1; /* the path separator is part of the path */
26251#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026252 if (*fnamelen == 0)
26253 {
26254 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26255 p = vim_strsave((char_u *)".");
26256 if (p == NULL)
26257 return -1;
26258 vim_free(*bufp);
26259 *bufp = *fnamep = tail = p;
26260 *fnamelen = 1;
26261 }
26262 else
26263 {
26264 while (tail > s && !after_pathsep(s, tail))
26265 mb_ptr_back(*fnamep, tail);
26266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267 }
26268
26269 /* ":8" - shortname */
26270 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26271 {
26272 *usedlen += 2;
26273#ifdef WIN3264
26274 has_shortname = 1;
26275#endif
26276 }
26277
26278#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026279 /*
26280 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026281 */
26282 if (has_shortname)
26283 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026284 /* Copy the string if it is shortened by :h and when it wasn't copied
26285 * yet, because we are going to change it in place. Avoids changing
26286 * the buffer name for "%:8". */
26287 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026288 {
26289 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026290 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026291 return -1;
26292 vim_free(*bufp);
26293 *bufp = *fnamep = p;
26294 }
26295
26296 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026297 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026298 if (!has_fullname && !vim_isAbsName(*fnamep))
26299 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026300 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026301 return -1;
26302 }
26303 else
26304 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026305 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026306
Bram Moolenaardc935552011-08-17 15:23:23 +020026307 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026309 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310 return -1;
26311
26312 if (l == 0)
26313 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026314 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026315 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026316 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 return -1;
26318 }
26319 *fnamelen = l;
26320 }
26321 }
26322#endif /* WIN3264 */
26323
26324 /* ":t" - tail, just the basename */
26325 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26326 {
26327 *usedlen += 2;
26328 *fnamelen -= (int)(tail - *fnamep);
26329 *fnamep = tail;
26330 }
26331
26332 /* ":e" - extension, can be repeated */
26333 /* ":r" - root, without extension, can be repeated */
26334 while (src[*usedlen] == ':'
26335 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26336 {
26337 /* find a '.' in the tail:
26338 * - for second :e: before the current fname
26339 * - otherwise: The last '.'
26340 */
26341 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26342 s = *fnamep - 2;
26343 else
26344 s = *fnamep + *fnamelen - 1;
26345 for ( ; s > tail; --s)
26346 if (s[0] == '.')
26347 break;
26348 if (src[*usedlen + 1] == 'e') /* :e */
26349 {
26350 if (s > tail)
26351 {
26352 *fnamelen += (int)(*fnamep - (s + 1));
26353 *fnamep = s + 1;
26354#ifdef VMS
26355 /* cut version from the extension */
26356 s = *fnamep + *fnamelen - 1;
26357 for ( ; s > *fnamep; --s)
26358 if (s[0] == ';')
26359 break;
26360 if (s > *fnamep)
26361 *fnamelen = s - *fnamep;
26362#endif
26363 }
26364 else if (*fnamep <= tail)
26365 *fnamelen = 0;
26366 }
26367 else /* :r */
26368 {
26369 if (s > tail) /* remove one extension */
26370 *fnamelen = (int)(s - *fnamep);
26371 }
26372 *usedlen += 2;
26373 }
26374
26375 /* ":s?pat?foo?" - substitute */
26376 /* ":gs?pat?foo?" - global substitute */
26377 if (src[*usedlen] == ':'
26378 && (src[*usedlen + 1] == 's'
26379 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26380 {
26381 char_u *str;
26382 char_u *pat;
26383 char_u *sub;
26384 int sep;
26385 char_u *flags;
26386 int didit = FALSE;
26387
26388 flags = (char_u *)"";
26389 s = src + *usedlen + 2;
26390 if (src[*usedlen + 1] == 'g')
26391 {
26392 flags = (char_u *)"g";
26393 ++s;
26394 }
26395
26396 sep = *s++;
26397 if (sep)
26398 {
26399 /* find end of pattern */
26400 p = vim_strchr(s, sep);
26401 if (p != NULL)
26402 {
26403 pat = vim_strnsave(s, (int)(p - s));
26404 if (pat != NULL)
26405 {
26406 s = p + 1;
26407 /* find end of substitution */
26408 p = vim_strchr(s, sep);
26409 if (p != NULL)
26410 {
26411 sub = vim_strnsave(s, (int)(p - s));
26412 str = vim_strnsave(*fnamep, *fnamelen);
26413 if (sub != NULL && str != NULL)
26414 {
26415 *usedlen = (int)(p + 1 - src);
26416 s = do_string_sub(str, pat, sub, flags);
26417 if (s != NULL)
26418 {
26419 *fnamep = s;
26420 *fnamelen = (int)STRLEN(s);
26421 vim_free(*bufp);
26422 *bufp = s;
26423 didit = TRUE;
26424 }
26425 }
26426 vim_free(sub);
26427 vim_free(str);
26428 }
26429 vim_free(pat);
26430 }
26431 }
26432 /* after using ":s", repeat all the modifiers */
26433 if (didit)
26434 goto repeat;
26435 }
26436 }
26437
Bram Moolenaar26df0922014-02-23 23:39:13 +010026438 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26439 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026440 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026441 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026442 if (c != NUL)
26443 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026444 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026445 if (c != NUL)
26446 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026447 if (p == NULL)
26448 return -1;
26449 vim_free(*bufp);
26450 *bufp = *fnamep = p;
26451 *fnamelen = (int)STRLEN(p);
26452 *usedlen += 2;
26453 }
26454
Bram Moolenaar071d4272004-06-13 20:20:40 +000026455 return valid;
26456}
26457
26458/*
26459 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26460 * "flags" can be "g" to do a global substitute.
26461 * Returns an allocated string, NULL for error.
26462 */
26463 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026464do_string_sub(
26465 char_u *str,
26466 char_u *pat,
26467 char_u *sub,
26468 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469{
26470 int sublen;
26471 regmatch_T regmatch;
26472 int i;
26473 int do_all;
26474 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026475 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026476 garray_T ga;
26477 char_u *ret;
26478 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026479 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026480
26481 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26482 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026483 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026484
26485 ga_init2(&ga, 1, 200);
26486
26487 do_all = (flags[0] == 'g');
26488
26489 regmatch.rm_ic = p_ic;
26490 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26491 if (regmatch.regprog != NULL)
26492 {
26493 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026494 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026495 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26496 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026497 /* Skip empty match except for first match. */
26498 if (regmatch.startp[0] == regmatch.endp[0])
26499 {
26500 if (zero_width == regmatch.startp[0])
26501 {
26502 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026503 i = MB_PTR2LEN(tail);
26504 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26505 (size_t)i);
26506 ga.ga_len += i;
26507 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026508 continue;
26509 }
26510 zero_width = regmatch.startp[0];
26511 }
26512
Bram Moolenaar071d4272004-06-13 20:20:40 +000026513 /*
26514 * Get some space for a temporary buffer to do the substitution
26515 * into. It will contain:
26516 * - The text up to where the match is.
26517 * - The substituted text.
26518 * - The text after the match.
26519 */
26520 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026521 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026522 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26523 {
26524 ga_clear(&ga);
26525 break;
26526 }
26527
26528 /* copy the text up to where the match is */
26529 i = (int)(regmatch.startp[0] - tail);
26530 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26531 /* add the substituted text */
26532 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26533 + ga.ga_len + i, TRUE, TRUE, FALSE);
26534 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026535 tail = regmatch.endp[0];
26536 if (*tail == NUL)
26537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026538 if (!do_all)
26539 break;
26540 }
26541
26542 if (ga.ga_data != NULL)
26543 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26544
Bram Moolenaar473de612013-06-08 18:19:48 +020026545 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026546 }
26547
26548 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26549 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026550 if (p_cpo == empty_option)
26551 p_cpo = save_cpo;
26552 else
26553 /* Darn, evaluating {sub} expression changed the value. */
26554 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026555
26556 return ret;
26557}
26558
26559#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */