blob: 49289070d22d86d03e099691d8906303e3e623a2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000113
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100114#define NAMESPACE_CHAR (char_u *)"abglstvw"
115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000128 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 */
130static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131#define COPYID_INC 2
132#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133
Bram Moolenaar8502c702014-06-17 12:51:16 +0200134/* Abort conversion to string after a recursion error. */
135static int did_echo_string_emsg = FALSE;
136
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100212/* List heads for garbage collection. Although there can be a reference loop
213 * from partial to dict to partial, we don't need to keep track of the partial,
214 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100300 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
302} vimvars[VV_LEN] =
303{
304 /*
305 * The order here must match the VV_ defines in vim.h!
306 * Initializing a union does not work, leave tv.vval empty to get zero's.
307 */
308 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("count1", VAR_NUMBER), VV_RO},
310 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
311 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
312 {VV_NAME("warningmsg", VAR_STRING), 0},
313 {VV_NAME("statusmsg", VAR_STRING), 0},
314 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
316 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
318 {VV_NAME("termresponse", VAR_STRING), VV_RO},
319 {VV_NAME("fname", VAR_STRING), VV_RO},
320 {VV_NAME("lang", VAR_STRING), VV_RO},
321 {VV_NAME("lc_time", VAR_STRING), VV_RO},
322 {VV_NAME("ctype", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
325 {VV_NAME("fname_in", VAR_STRING), VV_RO},
326 {VV_NAME("fname_out", VAR_STRING), VV_RO},
327 {VV_NAME("fname_new", VAR_STRING), VV_RO},
328 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
329 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
330 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
333 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("progname", VAR_STRING), VV_RO},
335 {VV_NAME("servername", VAR_STRING), VV_RO},
336 {VV_NAME("dying", VAR_NUMBER), VV_RO},
337 {VV_NAME("exception", VAR_STRING), VV_RO},
338 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
339 {VV_NAME("register", VAR_STRING), VV_RO},
340 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
341 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000342 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
343 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000344 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000345 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
346 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000347 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000352 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000353 {VV_NAME("swapname", VAR_STRING), VV_RO},
354 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000355 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200356 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000357 {VV_NAME("mouse_win", VAR_NUMBER), 0},
358 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
359 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000360 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100362 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000363 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200364 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200365 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200366 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200367 {VV_NAME("option_new", VAR_STRING), VV_RO},
368 {VV_NAME("option_old", VAR_STRING), VV_RO},
369 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100370 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371 {VV_NAME("false", VAR_SPECIAL), VV_RO},
372 {VV_NAME("true", VAR_SPECIAL), VV_RO},
373 {VV_NAME("null", VAR_SPECIAL), VV_RO},
374 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100375 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000376};
377
378/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000379#define vv_type vv_di.di_tv.v_type
380#define vv_nr vv_di.di_tv.vval.v_number
381#define vv_float vv_di.di_tv.vval.v_float
382#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000383#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200384#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000386
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200387static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000388#define vimvarht vimvardict.dv_hashtab
389
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100390static void prepare_vimvar(int idx, typval_T *save_tv);
391static void restore_vimvar(int idx, typval_T *save_tv);
392static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
393static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
394static char_u *skip_var_one(char_u *arg);
395static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
396static void list_glob_vars(int *first);
397static void list_buf_vars(int *first);
398static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000401#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100402static void list_vim_vars(int *first);
403static void list_script_vars(int *first);
404static void list_func_vars(int *first);
405static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
406static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
407static int check_changedtick(char_u *arg);
408static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
409static void clear_lval(lval_T *lp);
410static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
411static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
412static void list_fix_watch(list_T *l, listitem_T *item);
413static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
414static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
415static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
416static void item_lock(typval_T *tv, int deep, int lock);
417static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100419static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
420static int eval1(char_u **arg, typval_T *rettv, int evaluate);
421static int eval2(char_u **arg, typval_T *rettv, int evaluate);
422static int eval3(char_u **arg, typval_T *rettv, int evaluate);
423static int eval4(char_u **arg, typval_T *rettv, int evaluate);
424static int eval5(char_u **arg, typval_T *rettv, int evaluate);
425static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
426static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000427
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100428static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
429static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
433static long list_len(list_T *l);
434static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
435static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
436static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
437static long list_find_nr(list_T *l, long idx, int *errorp);
438static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
440static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
441static list_T *list_copy(list_T *orig, int deep, int copyID);
442static char_u *list2string(typval_T *tv, int copyID);
443static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
444static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
445static int free_unref_items(int copyID);
446static dictitem_T *dictitem_copy(dictitem_T *org);
447static void dictitem_remove(dict_T *dict, dictitem_T *item);
448static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
449static long dict_len(dict_T *d);
450static char_u *dict2string(typval_T *tv, int copyID);
451static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
452static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
454static char_u *string_quote(char_u *str, int function);
455static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
456static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100457static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
458static 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 +0100459static void emsg_funcname(char *ermsg, char_u *name);
460static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000461
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100463static void f_abs(typval_T *argvars, typval_T *rettv);
464static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100466static void f_add(typval_T *argvars, typval_T *rettv);
467static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
468static void f_and(typval_T *argvars, typval_T *rettv);
469static void f_append(typval_T *argvars, typval_T *rettv);
470static void f_argc(typval_T *argvars, typval_T *rettv);
471static void f_argidx(typval_T *argvars, typval_T *rettv);
472static void f_arglistid(typval_T *argvars, typval_T *rettv);
473static void f_argv(typval_T *argvars, typval_T *rettv);
474static void f_assert_equal(typval_T *argvars, typval_T *rettv);
475static void f_assert_exception(typval_T *argvars, typval_T *rettv);
476static void f_assert_fails(typval_T *argvars, typval_T *rettv);
477static void f_assert_false(typval_T *argvars, typval_T *rettv);
478static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100480static void f_asin(typval_T *argvars, typval_T *rettv);
481static void f_atan(typval_T *argvars, typval_T *rettv);
482static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100484static void f_browse(typval_T *argvars, typval_T *rettv);
485static void f_browsedir(typval_T *argvars, typval_T *rettv);
486static void f_bufexists(typval_T *argvars, typval_T *rettv);
487static void f_buflisted(typval_T *argvars, typval_T *rettv);
488static void f_bufloaded(typval_T *argvars, typval_T *rettv);
489static void f_bufname(typval_T *argvars, typval_T *rettv);
490static void f_bufnr(typval_T *argvars, typval_T *rettv);
491static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
492static void f_byte2line(typval_T *argvars, typval_T *rettv);
493static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
494static void f_byteidx(typval_T *argvars, typval_T *rettv);
495static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
496static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100498static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100500#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100501static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100502static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
503static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100504static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100505static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100506static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100507static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
509static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100510static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100514static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100516#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_changenr(typval_T *argvars, typval_T *rettv);
518static void f_char2nr(typval_T *argvars, typval_T *rettv);
519static void f_cindent(typval_T *argvars, typval_T *rettv);
520static void f_clearmatches(typval_T *argvars, typval_T *rettv);
521static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000522#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_complete(typval_T *argvars, typval_T *rettv);
524static void f_complete_add(typval_T *argvars, typval_T *rettv);
525static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_confirm(typval_T *argvars, typval_T *rettv);
528static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_cos(typval_T *argvars, typval_T *rettv);
531static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_count(typval_T *argvars, typval_T *rettv);
534static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
535static void f_cursor(typval_T *argsvars, typval_T *rettv);
536static void f_deepcopy(typval_T *argvars, typval_T *rettv);
537static void f_delete(typval_T *argvars, typval_T *rettv);
538static void f_did_filetype(typval_T *argvars, typval_T *rettv);
539static void f_diff_filler(typval_T *argvars, typval_T *rettv);
540static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100541static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_empty(typval_T *argvars, typval_T *rettv);
543static void f_escape(typval_T *argvars, typval_T *rettv);
544static void f_eval(typval_T *argvars, typval_T *rettv);
545static void f_eventhandler(typval_T *argvars, typval_T *rettv);
546static void f_executable(typval_T *argvars, typval_T *rettv);
547static void f_exepath(typval_T *argvars, typval_T *rettv);
548static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100552static void f_expand(typval_T *argvars, typval_T *rettv);
553static void f_extend(typval_T *argvars, typval_T *rettv);
554static void f_feedkeys(typval_T *argvars, typval_T *rettv);
555static void f_filereadable(typval_T *argvars, typval_T *rettv);
556static void f_filewritable(typval_T *argvars, typval_T *rettv);
557static void f_filter(typval_T *argvars, typval_T *rettv);
558static void f_finddir(typval_T *argvars, typval_T *rettv);
559static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_float2nr(typval_T *argvars, typval_T *rettv);
562static void f_floor(typval_T *argvars, typval_T *rettv);
563static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_fnameescape(typval_T *argvars, typval_T *rettv);
566static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
567static void f_foldclosed(typval_T *argvars, typval_T *rettv);
568static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
569static void f_foldlevel(typval_T *argvars, typval_T *rettv);
570static void f_foldtext(typval_T *argvars, typval_T *rettv);
571static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
572static void f_foreground(typval_T *argvars, typval_T *rettv);
573static void f_function(typval_T *argvars, typval_T *rettv);
574static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
575static void f_get(typval_T *argvars, typval_T *rettv);
576static void f_getbufline(typval_T *argvars, typval_T *rettv);
577static void f_getbufvar(typval_T *argvars, typval_T *rettv);
578static void f_getchar(typval_T *argvars, typval_T *rettv);
579static void f_getcharmod(typval_T *argvars, typval_T *rettv);
580static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
581static void f_getcmdline(typval_T *argvars, typval_T *rettv);
582static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
583static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
584static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
585static void f_getcwd(typval_T *argvars, typval_T *rettv);
586static void f_getfontname(typval_T *argvars, typval_T *rettv);
587static void f_getfperm(typval_T *argvars, typval_T *rettv);
588static void f_getfsize(typval_T *argvars, typval_T *rettv);
589static void f_getftime(typval_T *argvars, typval_T *rettv);
590static void f_getftype(typval_T *argvars, typval_T *rettv);
591static void f_getline(typval_T *argvars, typval_T *rettv);
592static void f_getmatches(typval_T *argvars, typval_T *rettv);
593static void f_getpid(typval_T *argvars, typval_T *rettv);
594static void f_getcurpos(typval_T *argvars, typval_T *rettv);
595static void f_getpos(typval_T *argvars, typval_T *rettv);
596static void f_getqflist(typval_T *argvars, typval_T *rettv);
597static void f_getreg(typval_T *argvars, typval_T *rettv);
598static void f_getregtype(typval_T *argvars, typval_T *rettv);
599static void f_gettabvar(typval_T *argvars, typval_T *rettv);
600static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
601static void f_getwinposx(typval_T *argvars, typval_T *rettv);
602static void f_getwinposy(typval_T *argvars, typval_T *rettv);
603static void f_getwinvar(typval_T *argvars, typval_T *rettv);
604static void f_glob(typval_T *argvars, typval_T *rettv);
605static void f_globpath(typval_T *argvars, typval_T *rettv);
606static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
607static void f_has(typval_T *argvars, typval_T *rettv);
608static void f_has_key(typval_T *argvars, typval_T *rettv);
609static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
610static void f_hasmapto(typval_T *argvars, typval_T *rettv);
611static void f_histadd(typval_T *argvars, typval_T *rettv);
612static void f_histdel(typval_T *argvars, typval_T *rettv);
613static void f_histget(typval_T *argvars, typval_T *rettv);
614static void f_histnr(typval_T *argvars, typval_T *rettv);
615static void f_hlID(typval_T *argvars, typval_T *rettv);
616static void f_hlexists(typval_T *argvars, typval_T *rettv);
617static void f_hostname(typval_T *argvars, typval_T *rettv);
618static void f_iconv(typval_T *argvars, typval_T *rettv);
619static void f_indent(typval_T *argvars, typval_T *rettv);
620static void f_index(typval_T *argvars, typval_T *rettv);
621static void f_input(typval_T *argvars, typval_T *rettv);
622static void f_inputdialog(typval_T *argvars, typval_T *rettv);
623static void f_inputlist(typval_T *argvars, typval_T *rettv);
624static void f_inputrestore(typval_T *argvars, typval_T *rettv);
625static void f_inputsave(typval_T *argvars, typval_T *rettv);
626static void f_inputsecret(typval_T *argvars, typval_T *rettv);
627static void f_insert(typval_T *argvars, typval_T *rettv);
628static void f_invert(typval_T *argvars, typval_T *rettv);
629static void f_isdirectory(typval_T *argvars, typval_T *rettv);
630static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100631#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
632static void f_isnan(typval_T *argvars, typval_T *rettv);
633#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100635#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100636static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100637static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100638static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100639static void f_job_start(typval_T *argvars, typval_T *rettv);
640static void f_job_stop(typval_T *argvars, typval_T *rettv);
641static void f_job_status(typval_T *argvars, typval_T *rettv);
642#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100643static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100644static void f_js_decode(typval_T *argvars, typval_T *rettv);
645static void f_js_encode(typval_T *argvars, typval_T *rettv);
646static void f_json_decode(typval_T *argvars, typval_T *rettv);
647static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100648static void f_keys(typval_T *argvars, typval_T *rettv);
649static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
650static void f_len(typval_T *argvars, typval_T *rettv);
651static void f_libcall(typval_T *argvars, typval_T *rettv);
652static void f_libcallnr(typval_T *argvars, typval_T *rettv);
653static void f_line(typval_T *argvars, typval_T *rettv);
654static void f_line2byte(typval_T *argvars, typval_T *rettv);
655static void f_lispindent(typval_T *argvars, typval_T *rettv);
656static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_log(typval_T *argvars, typval_T *rettv);
659static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000660#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200663#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100664static void f_map(typval_T *argvars, typval_T *rettv);
665static void f_maparg(typval_T *argvars, typval_T *rettv);
666static void f_mapcheck(typval_T *argvars, typval_T *rettv);
667static void f_match(typval_T *argvars, typval_T *rettv);
668static void f_matchadd(typval_T *argvars, typval_T *rettv);
669static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
670static void f_matcharg(typval_T *argvars, typval_T *rettv);
671static void f_matchdelete(typval_T *argvars, typval_T *rettv);
672static void f_matchend(typval_T *argvars, typval_T *rettv);
673static void f_matchlist(typval_T *argvars, typval_T *rettv);
674static void f_matchstr(typval_T *argvars, typval_T *rettv);
675static void f_max(typval_T *argvars, typval_T *rettv);
676static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100681#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100683#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100684static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
685static void f_nr2char(typval_T *argvars, typval_T *rettv);
686static void f_or(typval_T *argvars, typval_T *rettv);
687static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100688#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100690#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
695static void f_printf(typval_T *argvars, typval_T *rettv);
696static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200697#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200699#endif
700#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200702#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_range(typval_T *argvars, typval_T *rettv);
704static void f_readfile(typval_T *argvars, typval_T *rettv);
705static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100706#ifdef FEAT_FLOAT
707static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
708#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_reltimestr(typval_T *argvars, typval_T *rettv);
710static void f_remote_expr(typval_T *argvars, typval_T *rettv);
711static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
712static void f_remote_peek(typval_T *argvars, typval_T *rettv);
713static void f_remote_read(typval_T *argvars, typval_T *rettv);
714static void f_remote_send(typval_T *argvars, typval_T *rettv);
715static void f_remove(typval_T *argvars, typval_T *rettv);
716static void f_rename(typval_T *argvars, typval_T *rettv);
717static void f_repeat(typval_T *argvars, typval_T *rettv);
718static void f_resolve(typval_T *argvars, typval_T *rettv);
719static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000720#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000722#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_screenattr(typval_T *argvars, typval_T *rettv);
724static void f_screenchar(typval_T *argvars, typval_T *rettv);
725static void f_screencol(typval_T *argvars, typval_T *rettv);
726static void f_screenrow(typval_T *argvars, typval_T *rettv);
727static void f_search(typval_T *argvars, typval_T *rettv);
728static void f_searchdecl(typval_T *argvars, typval_T *rettv);
729static void f_searchpair(typval_T *argvars, typval_T *rettv);
730static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
731static void f_searchpos(typval_T *argvars, typval_T *rettv);
732static void f_server2client(typval_T *argvars, typval_T *rettv);
733static void f_serverlist(typval_T *argvars, typval_T *rettv);
734static void f_setbufvar(typval_T *argvars, typval_T *rettv);
735static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
736static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100737static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100738static void f_setline(typval_T *argvars, typval_T *rettv);
739static void f_setloclist(typval_T *argvars, typval_T *rettv);
740static void f_setmatches(typval_T *argvars, typval_T *rettv);
741static void f_setpos(typval_T *argvars, typval_T *rettv);
742static void f_setqflist(typval_T *argvars, typval_T *rettv);
743static void f_setreg(typval_T *argvars, typval_T *rettv);
744static void f_settabvar(typval_T *argvars, typval_T *rettv);
745static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
746static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100749#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_shellescape(typval_T *argvars, typval_T *rettv);
751static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
752static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sin(typval_T *argvars, typval_T *rettv);
755static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_sort(typval_T *argvars, typval_T *rettv);
758static void f_soundfold(typval_T *argvars, typval_T *rettv);
759static void f_spellbadword(typval_T *argvars, typval_T *rettv);
760static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
761static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sqrt(typval_T *argvars, typval_T *rettv);
764static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_str2nr(typval_T *argvars, typval_T *rettv);
767static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_stridx(typval_T *argvars, typval_T *rettv);
772static void f_string(typval_T *argvars, typval_T *rettv);
773static void f_strlen(typval_T *argvars, typval_T *rettv);
774static void f_strpart(typval_T *argvars, typval_T *rettv);
775static void f_strridx(typval_T *argvars, typval_T *rettv);
776static void f_strtrans(typval_T *argvars, typval_T *rettv);
777static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
778static void f_strwidth(typval_T *argvars, typval_T *rettv);
779static void f_submatch(typval_T *argvars, typval_T *rettv);
780static void f_substitute(typval_T *argvars, typval_T *rettv);
781static void f_synID(typval_T *argvars, typval_T *rettv);
782static void f_synIDattr(typval_T *argvars, typval_T *rettv);
783static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
784static void f_synstack(typval_T *argvars, typval_T *rettv);
785static void f_synconcealed(typval_T *argvars, typval_T *rettv);
786static void f_system(typval_T *argvars, typval_T *rettv);
787static void f_systemlist(typval_T *argvars, typval_T *rettv);
788static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
790static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
791static void f_taglist(typval_T *argvars, typval_T *rettv);
792static void f_tagfiles(typval_T *argvars, typval_T *rettv);
793static void f_tempname(typval_T *argvars, typval_T *rettv);
794static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tan(typval_T *argvars, typval_T *rettv);
797static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200798#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100799#ifdef FEAT_TIMERS
800static void f_timer_start(typval_T *argvars, typval_T *rettv);
801static void f_timer_stop(typval_T *argvars, typval_T *rettv);
802#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static void f_tolower(typval_T *argvars, typval_T *rettv);
804static void f_toupper(typval_T *argvars, typval_T *rettv);
805static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000806#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100807static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000808#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100809static void f_type(typval_T *argvars, typval_T *rettv);
810static void f_undofile(typval_T *argvars, typval_T *rettv);
811static void f_undotree(typval_T *argvars, typval_T *rettv);
812static void f_uniq(typval_T *argvars, typval_T *rettv);
813static void f_values(typval_T *argvars, typval_T *rettv);
814static void f_virtcol(typval_T *argvars, typval_T *rettv);
815static void f_visualmode(typval_T *argvars, typval_T *rettv);
816static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100817static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100818static void f_win_getid(typval_T *argvars, typval_T *rettv);
819static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
820static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
821static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100822static void f_winbufnr(typval_T *argvars, typval_T *rettv);
823static void f_wincol(typval_T *argvars, typval_T *rettv);
824static void f_winheight(typval_T *argvars, typval_T *rettv);
825static void f_winline(typval_T *argvars, typval_T *rettv);
826static void f_winnr(typval_T *argvars, typval_T *rettv);
827static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
828static void f_winrestview(typval_T *argvars, typval_T *rettv);
829static void f_winsaveview(typval_T *argvars, typval_T *rettv);
830static void f_winwidth(typval_T *argvars, typval_T *rettv);
831static void f_writefile(typval_T *argvars, typval_T *rettv);
832static void f_wordcount(typval_T *argvars, typval_T *rettv);
833static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
836static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
837static int get_env_len(char_u **arg);
838static int get_id_len(char_u **arg);
839static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
840static 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 +0000841#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
842#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
843 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100844static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
845static int eval_isnamec(int c);
846static int eval_isnamec1(int c);
847static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
848static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100849static typval_T *alloc_string_tv(char_u *string);
850static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100851#ifdef FEAT_FLOAT
852static float_T get_tv_float(typval_T *varp);
853#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100854static linenr_T get_tv_lnum(typval_T *argvars);
855static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100856static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
857static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
858static hashtab_T *find_var_ht(char_u *name, char_u **varname);
859static funccall_T *get_funccal(void);
860static void vars_clear_ext(hashtab_T *ht, int free_val);
861static void delete_var(hashtab_T *ht, hashitem_T *hi);
862static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
863static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
864static void set_var(char_u *name, typval_T *varp, int copy);
865static int var_check_ro(int flags, char_u *name, int use_gettext);
866static int var_check_fixed(int flags, char_u *name, int use_gettext);
867static int var_check_func_name(char_u *name, int new_var);
868static int valid_varname(char_u *varname);
869static int tv_check_lock(int lock, char_u *name, int use_gettext);
870static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
871static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100872static 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 +0100873static int eval_fname_script(char_u *p);
874static int eval_fname_sid(char_u *p);
875static void list_func_head(ufunc_T *fp, int indent);
876static ufunc_T *find_func(char_u *name);
877static int function_exists(char_u *name);
878static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000879#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static void func_do_profile(ufunc_T *fp);
881static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
882static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000883static int
884# ifdef __BORLANDC__
885 _RTLENTRYF
886# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100887 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000888static int
889# ifdef __BORLANDC__
890 _RTLENTRYF
891# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100892 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100894static int script_autoload(char_u *name, int reload);
895static char_u *autoload_name(char_u *name);
896static void cat_func_name(char_u *buf, ufunc_T *fp);
897static void func_free(ufunc_T *fp);
898static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
899static int can_free_funccal(funccall_T *fc, int copyID) ;
900static void free_funccal(funccall_T *fc, int free_val);
901static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
902static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
903static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
904static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
905static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
906static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
907static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
908static int write_list(FILE *fd, list_T *list, int binary);
909static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000910
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200911
912#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100913static int compare_func_name(const void *s1, const void *s2);
914static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200915#endif
916
Bram Moolenaar33570922005-01-25 22:26:29 +0000917/*
918 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000919 */
920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100921eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000922{
Bram Moolenaar33570922005-01-25 22:26:29 +0000923 int i;
924 struct vimvar *p;
925
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200926 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
927 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200928 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000929 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000930 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000931
932 for (i = 0; i < VV_LEN; ++i)
933 {
934 p = &vimvars[i];
935 STRCPY(p->vv_di.di_key, p->vv_name);
936 if (p->vv_flags & VV_RO)
937 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
938 else if (p->vv_flags & VV_RO_SBX)
939 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
940 else
941 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000942
943 /* add to v: scope dict, unless the value is not always available */
944 if (p->vv_type != VAR_UNKNOWN)
945 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000947 /* add to compat scope dict */
948 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000949 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100950 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
951
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000952 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100953 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200954 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100955 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100956
957 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
958 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
959 set_vim_var_nr(VV_NONE, VVAL_NONE);
960 set_vim_var_nr(VV_NULL, VVAL_NULL);
961
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200962 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200963
964#ifdef EBCDIC
965 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100966 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200967 */
968 sortFunctions();
969#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000970}
971
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972#if defined(EXITFREE) || defined(PROTO)
973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100974eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000975{
976 int i;
977 struct vimvar *p;
978
979 for (i = 0; i < VV_LEN; ++i)
980 {
981 p = &vimvars[i];
982 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000983 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000984 vim_free(p->vv_str);
985 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000986 }
987 else if (p->vv_di.di_tv.v_type == VAR_LIST)
988 {
989 list_unref(p->vv_list);
990 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992 }
993 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000994 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995 hash_clear(&compat_hashtab);
996
Bram Moolenaard9fba312005-06-26 22:34:35 +0000997 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100998# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200999 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001000# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001
1002 /* global variables */
1003 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001004
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001005 /* autoloaded script names */
1006 ga_clear_strings(&ga_loaded);
1007
Bram Moolenaarcca74132013-09-25 21:00:28 +02001008 /* Script-local variables. First clear all the variables and in a second
1009 * loop free the scriptvar_T, because a variable in one script might hold
1010 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001011 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001012 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001013 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001014 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001015 ga_clear(&ga_scripts);
1016
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001017 /* unreferenced lists and dicts */
1018 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001019
1020 /* functions */
1021 free_all_functions();
1022 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001023}
1024#endif
1025
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Return the name of the executed function.
1028 */
1029 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001030func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
1034
1035/*
1036 * Return the address holding the next breakpoint line for a funccall cookie.
1037 */
1038 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001039func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar33570922005-01-25 22:26:29 +00001041 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042}
1043
1044/*
1045 * Return the address holding the debug tick for a funccall cookie.
1046 */
1047 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001048func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar33570922005-01-25 22:26:29 +00001050 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053/*
1054 * Return the nesting level for a funccall cookie.
1055 */
1056 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001057func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060}
1061
1062/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001063funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001065/* pointer to list of previously used funccal, still around because some
1066 * item in it is still being used. */
1067funccall_T *previous_funccal = NULL;
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069/*
1070 * Return TRUE when a function was ended by a ":return" command.
1071 */
1072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
1075 return current_funccal->returned;
1076}
1077
1078
1079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * Set an internal variable to a string value. Creates the variable if it does
1081 * not already exist.
1082 */
1083 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001084set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001086 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001087 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
1089 val = vim_strsave(value);
1090 if (val != NULL)
1091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001092 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001093 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001095 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001096 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 }
1099}
1100
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103static char_u *redir_endp = NULL;
1104static char_u *redir_varname = NULL;
1105
1106/*
1107 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001108 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 * Returns OK if successfully completed the setup. FAIL otherwise.
1110 */
1111 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001112var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113{
1114 int save_emsg;
1115 int err;
1116 typval_T tv;
1117
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001119 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 {
1121 EMSG(_(e_invarg));
1122 return FAIL;
1123 }
1124
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 redir_varname = vim_strsave(name);
1127 if (redir_varname == NULL)
1128 return FAIL;
1129
1130 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1131 if (redir_lval == NULL)
1132 {
1133 var_redir_stop();
1134 return FAIL;
1135 }
1136
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 /* The output is stored in growarray "redir_ga" until redirection ends. */
1138 ga_init2(&redir_ga, (int)sizeof(char), 500);
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001141 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001142 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1144 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 if (redir_endp != NULL && *redir_endp != NUL)
1147 /* Trailing characters are present after the variable name */
1148 EMSG(_(e_trailing));
1149 else
1150 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001151 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 var_redir_stop();
1153 return FAIL;
1154 }
1155
1156 /* check if we can write to the variable: set it to or append an empty
1157 * string */
1158 save_emsg = did_emsg;
1159 did_emsg = FALSE;
1160 tv.v_type = VAR_STRING;
1161 tv.vval.v_string = (char_u *)"";
1162 if (append)
1163 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1164 else
1165 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001166 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001168 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 if (err)
1170 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 var_redir_stop();
1173 return FAIL;
1174 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175
1176 return OK;
1177}
1178
1179/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001180 * Append "value[value_len]" to the variable set by var_redir_start().
1181 * The actual appending is postponed until redirection ends, because the value
1182 * appended may in fact be the string we write to, changing it may cause freed
1183 * memory to be used:
1184 * :redir => foo
1185 * :let foo
1186 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 */
1188 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001189var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192
1193 if (redir_lval == NULL)
1194 return;
1195
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001197 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001199 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001201 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202 {
1203 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001204 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001205 }
1206 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208}
1209
1210/*
1211 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001212 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213 */
1214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001215var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001217 typval_T tv;
1218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219 if (redir_lval != NULL)
1220 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001221 /* If there was no error: assign the text to the variable. */
1222 if (redir_endp != NULL)
1223 {
1224 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1225 tv.v_type = VAR_STRING;
1226 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001227 /* Call get_lval() again, if it's inside a Dict or List it may
1228 * have changed. */
1229 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001230 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001231 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1232 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1233 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001234 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001235
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001236 /* free the collected output */
1237 vim_free(redir_ga.ga_data);
1238 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001239
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001240 vim_free(redir_lval);
1241 redir_lval = NULL;
1242 }
1243 vim_free(redir_varname);
1244 redir_varname = NULL;
1245}
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247# if defined(FEAT_MBYTE) || defined(PROTO)
1248 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001249eval_charconvert(
1250 char_u *enc_from,
1251 char_u *enc_to,
1252 char_u *fname_from,
1253 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254{
1255 int err = FALSE;
1256
1257 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1258 set_vim_var_string(VV_CC_TO, enc_to, -1);
1259 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1260 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1261 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1262 err = TRUE;
1263 set_vim_var_string(VV_CC_FROM, NULL, -1);
1264 set_vim_var_string(VV_CC_TO, NULL, -1);
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1267
1268 if (err)
1269 return FAIL;
1270 return OK;
1271}
1272# endif
1273
1274# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001276eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 int err = FALSE;
1279
1280 set_vim_var_string(VV_FNAME_IN, fname, -1);
1281 set_vim_var_string(VV_CMDARG, args, -1);
1282 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1283 err = TRUE;
1284 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1285 set_vim_var_string(VV_CMDARG, NULL, -1);
1286
1287 if (err)
1288 {
1289 mch_remove(fname);
1290 return FAIL;
1291 }
1292 return OK;
1293}
1294# endif
1295
1296# if defined(FEAT_DIFF) || defined(PROTO)
1297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001298eval_diff(
1299 char_u *origfile,
1300 char_u *newfile,
1301 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
1303 int err = FALSE;
1304
1305 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1306 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1307 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1308 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1309 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1310 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1311 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1312}
1313
1314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001315eval_patch(
1316 char_u *origfile,
1317 char_u *difffile,
1318 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int err;
1321
1322 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1323 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1324 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1325 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1326 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1327 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1328 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1329}
1330# endif
1331
1332/*
1333 * Top level evaluation function, returning a boolean.
1334 * Sets "error" to TRUE if there was an error.
1335 * Return TRUE or FALSE.
1336 */
1337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001338eval_to_bool(
1339 char_u *arg,
1340 int *error,
1341 char_u **nextcmd,
1342 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 int retval = FALSE;
1346
1347 if (skip)
1348 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 else
1352 {
1353 *error = FALSE;
1354 if (!skip)
1355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001356 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 }
1360 if (skip)
1361 --emsg_skip;
1362
1363 return retval;
1364}
1365
1366/*
1367 * Top level evaluation function, returning a string. If "skip" is TRUE,
1368 * only parsing to "nextcmd" is done, without reporting errors. Return
1369 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1370 */
1371 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001372eval_to_string_skip(
1373 char_u *arg,
1374 char_u **nextcmd,
1375 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char_u *retval;
1379
1380 if (skip)
1381 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 retval = NULL;
1384 else
1385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 retval = vim_strsave(get_tv_string(&tv));
1387 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 if (skip)
1390 --emsg_skip;
1391
1392 return retval;
1393}
1394
1395/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001396 * Skip over an expression at "*pp".
1397 * Return FAIL for an error, OK otherwise.
1398 */
1399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001400skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001401{
Bram Moolenaar33570922005-01-25 22:26:29 +00001402 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001403
1404 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001405 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001406}
1407
1408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001410 * When "convert" is TRUE convert a List into a sequence of lines and convert
1411 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 * Return pointer to allocated memory, or NULL for failure.
1413 */
1414 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001415eval_to_string(
1416 char_u *arg,
1417 char_u **nextcmd,
1418 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001422 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001423#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001424 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 retval = NULL;
1429 else
1430 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001431 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 {
1433 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001434 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001435 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001436 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001437 if (tv.vval.v_list->lv_len > 0)
1438 ga_append(&ga, NL);
1439 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001440 ga_append(&ga, NUL);
1441 retval = (char_u *)ga.ga_data;
1442 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001443#ifdef FEAT_FLOAT
1444 else if (convert && tv.v_type == VAR_FLOAT)
1445 {
1446 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1447 retval = vim_strsave(numbuf);
1448 }
1449#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001450 else
1451 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454
1455 return retval;
1456}
1457
1458/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001459 * Call eval_to_string() without using current local variables and using
1460 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 */
1462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001463eval_to_string_safe(
1464 char_u *arg,
1465 char_u **nextcmd,
1466 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 char_u *retval;
1469 void *save_funccalp;
1470
1471 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001472 if (use_sandbox)
1473 ++sandbox;
1474 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001475 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001476 if (use_sandbox)
1477 --sandbox;
1478 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 restore_funccal(save_funccalp);
1480 return retval;
1481}
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
1484 * Top level evaluation function, returning a number.
1485 * Evaluates "expr" silently.
1486 * Returns -1 for an error.
1487 */
1488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001489eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
Bram Moolenaar33570922005-01-25 22:26:29 +00001491 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001493 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495 ++emsg_off;
1496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 retval = -1;
1499 else
1500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001501 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 --emsg_off;
1505
1506 return retval;
1507}
1508
Bram Moolenaara40058a2005-07-11 22:42:07 +00001509/*
1510 * Prepare v: variable "idx" to be used.
1511 * Save the current typeval in "save_tv".
1512 * When not used yet add the variable to the v: hashtable.
1513 */
1514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001515prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001516{
1517 *save_tv = vimvars[idx].vv_tv;
1518 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1519 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1520}
1521
1522/*
1523 * Restore v: variable "idx" to typeval "save_tv".
1524 * When no longer defined, remove the variable from the v: hashtable.
1525 */
1526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001527restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001528{
1529 hashitem_T *hi;
1530
Bram Moolenaara40058a2005-07-11 22:42:07 +00001531 vimvars[idx].vv_tv = *save_tv;
1532 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1533 {
1534 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1535 if (HASHITEM_EMPTY(hi))
1536 EMSG2(_(e_intern2), "restore_vimvar()");
1537 else
1538 hash_remove(&vimvarht, hi);
1539 }
1540}
1541
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001542#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001543/*
1544 * Evaluate an expression to a list with suggestions.
1545 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001546 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001547 */
1548 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001549eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001550{
1551 typval_T save_val;
1552 typval_T rettv;
1553 list_T *list = NULL;
1554 char_u *p = skipwhite(expr);
1555
1556 /* Set "v:val" to the bad word. */
1557 prepare_vimvar(VV_VAL, &save_val);
1558 vimvars[VV_VAL].vv_type = VAR_STRING;
1559 vimvars[VV_VAL].vv_str = badword;
1560 if (p_verbose == 0)
1561 ++emsg_off;
1562
1563 if (eval1(&p, &rettv, TRUE) == OK)
1564 {
1565 if (rettv.v_type != VAR_LIST)
1566 clear_tv(&rettv);
1567 else
1568 list = rettv.vval.v_list;
1569 }
1570
1571 if (p_verbose == 0)
1572 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001573 restore_vimvar(VV_VAL, &save_val);
1574
1575 return list;
1576}
1577
1578/*
1579 * "list" is supposed to contain two items: a word and a number. Return the
1580 * word in "pp" and the number as the return value.
1581 * Return -1 if anything isn't right.
1582 * Used to get the good word and score from the eval_spell_expr() result.
1583 */
1584 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001585get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001586{
1587 listitem_T *li;
1588
1589 li = list->lv_first;
1590 if (li == NULL)
1591 return -1;
1592 *pp = get_tv_string(&li->li_tv);
1593
1594 li = li->li_next;
1595 if (li == NULL)
1596 return -1;
1597 return get_tv_number(&li->li_tv);
1598}
1599#endif
1600
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001601/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001602 * Top level evaluation function.
1603 * Returns an allocated typval_T with the result.
1604 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605 */
1606 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001607eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001608{
1609 typval_T *tv;
1610
1611 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001612 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001613 {
1614 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001615 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001616 }
1617
1618 return tv;
1619}
1620
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 * Uses argv[argc] for the function arguments. Only Number and String
1625 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001629call_vim_function(
1630 char_u *func,
1631 int argc,
1632 char_u **argv,
1633 int safe, /* use the sandbox */
1634 int str_arg_only, /* all arguments are strings */
1635 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
Bram Moolenaar33570922005-01-25 22:26:29 +00001637 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 long n;
1639 int len;
1640 int i;
1641 int doesrange;
1642 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001645 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 for (i = 0; i < argc; i++)
1650 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001651 /* Pass a NULL or empty argument as an empty string */
1652 if (argv[i] == NULL || *argv[i] == NUL)
1653 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001654 argvars[i].v_type = VAR_STRING;
1655 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001656 continue;
1657 }
1658
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001659 if (str_arg_only)
1660 len = 0;
1661 else
1662 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001663 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (len != 0 && len == (int)STRLEN(argv[i]))
1665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001666 argvars[i].v_type = VAR_NUMBER;
1667 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 else
1670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001671 argvars[i].v_type = VAR_STRING;
1672 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 }
1674 }
1675
1676 if (safe)
1677 {
1678 save_funccalp = save_funccal();
1679 ++sandbox;
1680 }
1681
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1683 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001685 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (safe)
1687 {
1688 --sandbox;
1689 restore_funccal(save_funccalp);
1690 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 vim_free(argvars);
1692
1693 if (ret == FAIL)
1694 clear_tv(rettv);
1695
1696 return ret;
1697}
1698
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001699/*
1700 * Call vimL function "func" and return the result as a number.
1701 * Returns -1 when calling the function fails.
1702 * Uses argv[argc] for the function arguments.
1703 */
1704 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001705call_func_retnr(
1706 char_u *func,
1707 int argc,
1708 char_u **argv,
1709 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001710{
1711 typval_T rettv;
1712 long retval;
1713
1714 /* All arguments are passed as strings, no conversion to number. */
1715 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1716 return -1;
1717
1718 retval = get_tv_number_chk(&rettv, NULL);
1719 clear_tv(&rettv);
1720 return retval;
1721}
1722
1723#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1724 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1725
Bram Moolenaar4f688582007-07-24 12:34:30 +00001726# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001728 * Call vimL function "func" and return the result as a string.
1729 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730 * Uses argv[argc] for the function arguments.
1731 */
1732 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001733call_func_retstr(
1734 char_u *func,
1735 int argc,
1736 char_u **argv,
1737 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738{
1739 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001740 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001742 /* All arguments are passed as strings, no conversion to number. */
1743 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 return NULL;
1745
1746 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 return retval;
1749}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001750# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001752/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001753 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001755 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756 */
1757 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001758call_func_retlist(
1759 char_u *func,
1760 int argc,
1761 char_u **argv,
1762 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763{
1764 typval_T rettv;
1765
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001766 /* All arguments are passed as strings, no conversion to number. */
1767 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001768 return NULL;
1769
1770 if (rettv.v_type != VAR_LIST)
1771 {
1772 clear_tv(&rettv);
1773 return NULL;
1774 }
1775
1776 return rettv.vval.v_list;
1777}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778#endif
1779
1780/*
1781 * Save the current function call pointer, and set it to NULL.
1782 * Used when executing autocommands and for ":source".
1783 */
1784 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001787 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 current_funccal = NULL;
1790 return (void *)fc;
1791}
1792
1793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001794restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001796 funccall_T *fc = (funccall_T *)vfc;
1797
1798 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799}
1800
Bram Moolenaar05159a02005-02-26 23:04:13 +00001801#if defined(FEAT_PROFILE) || defined(PROTO)
1802/*
1803 * Prepare profiling for entering a child or something else that is not
1804 * counted for the script/function itself.
1805 * Should always be called in pair with prof_child_exit().
1806 */
1807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808prof_child_enter(
1809 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001810{
1811 funccall_T *fc = current_funccal;
1812
1813 if (fc != NULL && fc->func->uf_profiling)
1814 profile_start(&fc->prof_child);
1815 script_prof_save(tm);
1816}
1817
1818/*
1819 * Take care of time spent in a child.
1820 * Should always be called after prof_child_enter().
1821 */
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823prof_child_exit(
1824 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001825{
1826 funccall_T *fc = current_funccal;
1827
1828 if (fc != NULL && fc->func->uf_profiling)
1829 {
1830 profile_end(&fc->prof_child);
1831 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1832 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1833 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1834 }
1835 script_prof_restore(tm);
1836}
1837#endif
1838
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#ifdef FEAT_FOLDING
1841/*
1842 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1843 * it in "*cp". Doesn't give error messages.
1844 */
1845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001846eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 int retval;
1850 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001851 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1852 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001855 if (use_sandbox)
1856 ++sandbox;
1857 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 retval = 0;
1861 else
1862 {
1863 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 if (tv.v_type == VAR_NUMBER)
1865 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001866 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 retval = 0;
1868 else
1869 {
1870 /* If the result is a string, check if there is a non-digit before
1871 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (!VIM_ISDIGIT(*s) && *s != '-')
1874 *cp = *s++;
1875 retval = atol((char *)s);
1876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
1879 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001880 if (use_sandbox)
1881 --sandbox;
1882 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 return retval;
1885}
1886#endif
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 * ":let" list all variable values
1890 * ":let var1 var2" list variable values
1891 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 * ":let var += expr" assignment command.
1893 * ":let var -= expr" assignment command.
1894 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 */
1897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899{
1900 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001902 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 int var_count = 0;
1905 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001907 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001908 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
Bram Moolenaardb552d602006-03-23 22:59:57 +00001910 argend = skip_var_list(arg, &var_count, &semicolon);
1911 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001913 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1914 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001915 expr = skipwhite(argend);
1916 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1917 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001919 /*
1920 * ":let" without "=": list variables
1921 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 if (*arg == '[')
1923 EMSG(_(e_invarg));
1924 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001926 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001930 list_glob_vars(&first);
1931 list_buf_vars(&first);
1932 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001933#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001934 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001935#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001936 list_script_vars(&first);
1937 list_func_vars(&first);
1938 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 eap->nextcmd = check_nextcmd(arg);
1941 }
1942 else
1943 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 op[0] = '=';
1945 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001948 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1949 op[0] = *expr; /* +=, -= or .= */
1950 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001952 else
1953 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (eap->skip)
1956 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (eap->skip)
1959 {
1960 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 --emsg_skip;
1963 }
1964 else if (i != FAIL)
1965 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970 }
1971}
1972
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973/*
1974 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1975 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 * When "nextchars" is not NULL it points to a string with characters that
1977 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1978 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 * Returns OK or FAIL;
1980 */
1981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001982ex_let_vars(
1983 char_u *arg_start,
1984 typval_T *tv,
1985 int copy, /* copy values from "tv", don't move */
1986 int semicolon, /* from skip_var_list() */
1987 int var_count, /* from skip_var_list() */
1988 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989{
1990 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001991 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001993 listitem_T *item;
1994 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995
1996 if (*arg != '[')
1997 {
1998 /*
1999 * ":let var = expr" or ":for var in list"
2000 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 return FAIL;
2003 return OK;
2004 }
2005
2006 /*
2007 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2008 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002009 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 {
2011 EMSG(_(e_listreq));
2012 return FAIL;
2013 }
2014
2015 i = list_len(l);
2016 if (semicolon == 0 && var_count < i)
2017 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002018 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 return FAIL;
2020 }
2021 if (var_count - semicolon > i)
2022 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002023 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 return FAIL;
2025 }
2026
2027 item = l->lv_first;
2028 while (*arg != ']')
2029 {
2030 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 item = item->li_next;
2033 if (arg == NULL)
2034 return FAIL;
2035
2036 arg = skipwhite(arg);
2037 if (*arg == ';')
2038 {
2039 /* Put the rest of the list (may be empty) in the var after ';'.
2040 * Create a new list for this. */
2041 l = list_alloc();
2042 if (l == NULL)
2043 return FAIL;
2044 while (item != NULL)
2045 {
2046 list_append_tv(l, &item->li_tv);
2047 item = item->li_next;
2048 }
2049
2050 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002051 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 ltv.vval.v_list = l;
2053 l->lv_refcount = 1;
2054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002055 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2056 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 clear_tv(&ltv);
2058 if (arg == NULL)
2059 return FAIL;
2060 break;
2061 }
2062 else if (*arg != ',' && *arg != ']')
2063 {
2064 EMSG2(_(e_intern2), "ex_let_vars()");
2065 return FAIL;
2066 }
2067 }
2068
2069 return OK;
2070}
2071
2072/*
2073 * Skip over assignable variable "var" or list of variables "[var, var]".
2074 * Used for ":let varvar = expr" and ":for varvar in expr".
2075 * For "[var, var]" increment "*var_count" for each variable.
2076 * for "[var, var; var]" set "semicolon".
2077 * Return NULL for an error.
2078 */
2079 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002080skip_var_list(
2081 char_u *arg,
2082 int *var_count,
2083 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084{
2085 char_u *p, *s;
2086
2087 if (*arg == '[')
2088 {
2089 /* "[var, var]": find the matching ']'. */
2090 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002091 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092 {
2093 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2094 s = skip_var_one(p);
2095 if (s == p)
2096 {
2097 EMSG2(_(e_invarg2), p);
2098 return NULL;
2099 }
2100 ++*var_count;
2101
2102 p = skipwhite(s);
2103 if (*p == ']')
2104 break;
2105 else if (*p == ';')
2106 {
2107 if (*semicolon == 1)
2108 {
2109 EMSG(_("Double ; in list of variables"));
2110 return NULL;
2111 }
2112 *semicolon = 1;
2113 }
2114 else if (*p != ',')
2115 {
2116 EMSG2(_(e_invarg2), p);
2117 return NULL;
2118 }
2119 }
2120 return p + 1;
2121 }
2122 else
2123 return skip_var_one(arg);
2124}
2125
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002127 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002128 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002129 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002131skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002132{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002133 if (*arg == '@' && arg[1] != NUL)
2134 return arg + 2;
2135 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2136 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002137}
2138
Bram Moolenaara7043832005-01-21 11:56:39 +00002139/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 * List variables for hashtab "ht" with prefix "prefix".
2141 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002144list_hashtable_vars(
2145 hashtab_T *ht,
2146 char_u *prefix,
2147 int empty,
2148 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar33570922005-01-25 22:26:29 +00002150 hashitem_T *hi;
2151 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002152 int todo;
2153
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002154 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002155 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2156 {
2157 if (!HASHITEM_EMPTY(hi))
2158 {
2159 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 di = HI2DI(hi);
2161 if (empty || di->di_tv.v_type != VAR_STRING
2162 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002164 }
2165 }
2166}
2167
2168/*
2169 * List global variables.
2170 */
2171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002172list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002173{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002175}
2176
2177/*
2178 * List buffer variables.
2179 */
2180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002181list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002182{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 char_u numbuf[NUMBUFLEN];
2184
Bram Moolenaar429fa852013-04-15 12:27:36 +02002185 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002187
2188 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2190 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002191}
2192
2193/*
2194 * List window variables.
2195 */
2196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002197list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002198{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002199 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002201}
2202
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203#ifdef FEAT_WINDOWS
2204/*
2205 * List tab page variables.
2206 */
2207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002209{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002210 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002212}
2213#endif
2214
Bram Moolenaara7043832005-01-21 11:56:39 +00002215/*
2216 * List Vim variables.
2217 */
2218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002219list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222}
2223
2224/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002225 * List script-local variables, if there is a script.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002229{
2230 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2232 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002233}
2234
2235/*
2236 * List function variables, if there is a function.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002240{
2241 if (current_funccal != NULL)
2242 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002243 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002244}
2245
2246/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 * List variables in "arg".
2248 */
2249 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251{
2252 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 char_u *name_start;
2256 char_u *arg_subsc;
2257 char_u *tofree;
2258 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259
2260 while (!ends_excmd(*arg) && !got_int)
2261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002264 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2266 {
2267 emsg_severe = TRUE;
2268 EMSG(_(e_trailing));
2269 break;
2270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 /* get_name_len() takes care of expanding curly braces */
2275 name_start = name = arg;
2276 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2277 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 /* This is mainly to keep test 49 working: when expanding
2280 * curly braces fails overrule the exception error message. */
2281 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 emsg_severe = TRUE;
2284 EMSG2(_(e_invarg2), arg);
2285 break;
2286 }
2287 error = TRUE;
2288 }
2289 else
2290 {
2291 if (tofree != NULL)
2292 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002293 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 else
2296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 /* handle d.key, l[idx], f(expr) */
2298 arg_subsc = arg;
2299 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 case 'g': list_glob_vars(first); break;
2308 case 'b': list_buf_vars(first); break;
2309 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002310#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002311 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002312#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002313 case 'v': list_vim_vars(first); break;
2314 case 's': list_script_vars(first); break;
2315 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 default:
2317 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 }
2320 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 {
2322 char_u numbuf[NUMBUFLEN];
2323 char_u *tf;
2324 int c;
2325 char_u *s;
2326
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002327 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 c = *arg;
2329 *arg = NUL;
2330 list_one_var_a((char_u *)"",
2331 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002332 tv.v_type,
2333 s == NULL ? (char_u *)"" : s,
2334 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 *arg = c;
2336 vim_free(tf);
2337 }
2338 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
2341 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342
2343 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345
2346 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 }
2348
2349 return arg;
2350}
2351
2352/*
2353 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2354 * Returns a pointer to the char just after the var name.
2355 * Returns NULL if there is an error.
2356 */
2357 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002358ex_let_one(
2359 char_u *arg, /* points to variable name */
2360 typval_T *tv, /* value to assign to variable */
2361 int copy, /* copy value from "tv" */
2362 char_u *endchars, /* valid chars after variable name or NULL */
2363 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364{
2365 int c1;
2366 char_u *name;
2367 char_u *p;
2368 char_u *arg_end = NULL;
2369 int len;
2370 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372
2373 /*
2374 * ":let $VAR = expr": Set environment variable.
2375 */
2376 if (*arg == '$')
2377 {
2378 /* Find the end of the name. */
2379 ++arg;
2380 name = arg;
2381 len = get_env_len(&arg);
2382 if (len == 0)
2383 EMSG2(_(e_invarg2), name - 1);
2384 else
2385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 if (op != NULL && (*op == '+' || *op == '-'))
2387 EMSG2(_(e_letwrong), op);
2388 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002389 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002391 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 {
2393 c1 = name[len];
2394 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 p = get_tv_string_chk(tv);
2396 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 {
2398 int mustfree = FALSE;
2399 char_u *s = vim_getenv(name, &mustfree);
2400
2401 if (s != NULL)
2402 {
2403 p = tofree = concat_str(s, p);
2404 if (mustfree)
2405 vim_free(s);
2406 }
2407 }
2408 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002411 if (STRICMP(name, "HOME") == 0)
2412 init_homedir();
2413 else if (didset_vim && STRICMP(name, "VIM") == 0)
2414 didset_vim = FALSE;
2415 else if (didset_vimruntime
2416 && STRICMP(name, "VIMRUNTIME") == 0)
2417 didset_vimruntime = FALSE;
2418 arg_end = arg;
2419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 }
2423 }
2424 }
2425
2426 /*
2427 * ":let &option = expr": Set option value.
2428 * ":let &l:option = expr": Set local option value.
2429 * ":let &g:option = expr": Set global option value.
2430 */
2431 else if (*arg == '&')
2432 {
2433 /* Find the end of the name. */
2434 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435 if (p == NULL || (endchars != NULL
2436 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 EMSG(_(e_letunexp));
2438 else
2439 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 long n;
2441 int opt_type;
2442 long numval;
2443 char_u *stringval = NULL;
2444 char_u *s;
2445
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 c1 = *p;
2447 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448
2449 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 s = get_tv_string_chk(tv); /* != NULL if number or string */
2451 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 {
2453 opt_type = get_option_value(arg, &numval,
2454 &stringval, opt_flags);
2455 if ((opt_type == 1 && *op == '.')
2456 || (opt_type == 0 && *op != '.'))
2457 EMSG2(_(e_letwrong), op);
2458 else
2459 {
2460 if (opt_type == 1) /* number */
2461 {
2462 if (*op == '+')
2463 n = numval + n;
2464 else
2465 n = numval - n;
2466 }
2467 else if (opt_type == 0 && stringval != NULL) /* string */
2468 {
2469 s = concat_str(stringval, s);
2470 vim_free(stringval);
2471 stringval = s;
2472 }
2473 }
2474 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 if (s != NULL)
2476 {
2477 set_option_value(arg, n, s, opt_flags);
2478 arg_end = p;
2479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
2483 }
2484
2485 /*
2486 * ":let @r = expr": Set register contents.
2487 */
2488 else if (*arg == '@')
2489 {
2490 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 if (op != NULL && (*op == '+' || *op == '-'))
2492 EMSG2(_(e_letwrong), op);
2493 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002494 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 EMSG(_(e_letunexp));
2496 else
2497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 char_u *s;
2500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 p = get_tv_string_chk(tv);
2502 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002504 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 if (s != NULL)
2506 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 vim_free(s);
2509 }
2510 }
2511 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 arg_end = arg + 1;
2515 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 }
2518 }
2519
2520 /*
2521 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002528 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2532 EMSG(_(e_letunexp));
2533 else
2534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 arg_end = p;
2537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 }
2541
2542 else
2543 EMSG2(_(e_invarg2), arg);
2544
2545 return arg_end;
2546}
2547
2548/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2550 */
2551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002552check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553{
2554 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2555 {
2556 EMSG2(_(e_readonlyvar), arg);
2557 return TRUE;
2558 }
2559 return FALSE;
2560}
2561
2562/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 * Get an lval: variable, Dict item or List item that can be assigned a value
2564 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2565 * "name.key", "name.key[expr]" etc.
2566 * Indexing only works if "name" is an existing List or Dictionary.
2567 * "name" points to the start of the name.
2568 * If "rettv" is not NULL it points to the value to be assigned.
2569 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2570 * wrong; must end in space or cmd separator.
2571 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002572 * flags:
2573 * GLV_QUIET: do not give error messages
2574 * GLV_NO_AUTOLOAD: do not use script autoloading
2575 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002577 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 */
2580 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002581get_lval(
2582 char_u *name,
2583 typval_T *rettv,
2584 lval_T *lp,
2585 int unlet,
2586 int skip,
2587 int flags, /* GLV_ values */
2588 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 char_u *expr_start, *expr_end;
2592 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 dictitem_T *v;
2594 typval_T var1;
2595 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002600 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002604 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605
2606 if (skip)
2607 {
2608 /* When skipping just find the end of the name. */
2609 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002610 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 }
2612
2613 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002614 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (expr_start != NULL)
2616 {
2617 /* Don't expand the name when we already know there is an error. */
2618 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2619 && *p != '[' && *p != '.')
2620 {
2621 EMSG(_(e_trailing));
2622 return NULL;
2623 }
2624
2625 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2626 if (lp->ll_exp_name == NULL)
2627 {
2628 /* Report an invalid expression in braces, unless the
2629 * expression evaluation has been cancelled due to an
2630 * aborting error, an interrupt, or an exception. */
2631 if (!aborting() && !quiet)
2632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002633 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 EMSG2(_(e_invarg2), name);
2635 return NULL;
2636 }
2637 }
2638 lp->ll_name = lp->ll_exp_name;
2639 }
2640 else
2641 lp->ll_name = name;
2642
2643 /* Without [idx] or .key we are done. */
2644 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2645 return p;
2646
2647 cc = *p;
2648 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002649 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (v == NULL && !quiet)
2651 EMSG2(_(e_undefvar), lp->ll_name);
2652 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 if (v == NULL)
2654 return NULL;
2655
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 /*
2657 * Loop until no more [idx] or .key is following.
2658 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002659 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2663 && !(lp->ll_tv->v_type == VAR_DICT
2664 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_("E689: Can only index a List or Dictionary"));
2668 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!quiet)
2673 EMSG(_("E708: [:] must come last"));
2674 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002675 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 len = -1;
2678 if (*p == '.')
2679 {
2680 key = p + 1;
2681 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2682 ;
2683 if (len == 0)
2684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_(e_emptykey));
2687 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689 p = key + len;
2690 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 else
2692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (*p == ':')
2696 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 else
2698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 empty1 = FALSE;
2700 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002702 if (get_tv_string_chk(&var1) == NULL)
2703 {
2704 /* not a number or string */
2705 clear_tv(&var1);
2706 return NULL;
2707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
2709
2710 /* Optionally get the second index [ :expr]. */
2711 if (*p == ':')
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 if (!empty1)
2718 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (rettv != NULL && (rettv->v_type != VAR_LIST
2722 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
2725 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (!empty1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 }
2730 p = skipwhite(p + 1);
2731 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 else
2734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (!empty1)
2739 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002742 if (get_tv_string_chk(&var2) == NULL)
2743 {
2744 /* not a number or string */
2745 if (!empty1)
2746 clear_tv(&var1);
2747 clear_tv(&var2);
2748 return NULL;
2749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002755
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (!quiet)
2759 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 if (!empty1)
2761 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 }
2766
2767 /* Skip to past ']'. */
2768 ++p;
2769 }
2770
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 {
2773 if (len == -1)
2774 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 if (*key == NUL)
2778 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 if (!quiet)
2780 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 lp->ll_list = NULL;
2786 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002787 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 /* When assigning to a scope dictionary check that a function and
2790 * variable name is valid (only variable name unless it is l: or
2791 * g: dictionary). Disallow overwriting a builtin function. */
2792 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002794 int prevval;
2795 int wrong;
2796
2797 if (len != -1)
2798 {
2799 prevval = key[len];
2800 key[len] = NUL;
2801 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002802 else
2803 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002804 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2805 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002806 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002807 || !valid_varname(key);
2808 if (len != -1)
2809 key[len] = prevval;
2810 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 return NULL;
2812 }
2813
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002816 /* Can't add "v:" variable. */
2817 if (lp->ll_dict == &vimvardict)
2818 {
2819 EMSG2(_(e_illvar), name);
2820 return NULL;
2821 }
2822
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002823 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002827 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (len == -1)
2829 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 if (len == -1)
2837 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 p = NULL;
2840 break;
2841 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002842 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002843 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002844 return NULL;
2845
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (len == -1)
2847 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 }
2850 else
2851 {
2852 /*
2853 * Get the number and item for the only or first index of the List.
2854 */
2855 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 else
2858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002859 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 clear_tv(&var1);
2861 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002862 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_list = lp->ll_tv->vval.v_list;
2864 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2865 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002867 if (lp->ll_n1 < 0)
2868 {
2869 lp->ll_n1 = 0;
2870 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2871 }
2872 }
2873 if (lp->ll_li == NULL)
2874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 }
2881
2882 /*
2883 * May need to find the item or absolute index for the second
2884 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 * When no index given: "lp->ll_empty2" is TRUE.
2886 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002890 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002896 {
2897 if (!quiet)
2898 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002900 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2905 if (lp->ll_n1 < 0)
2906 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2907 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002908 {
2909 if (!quiet)
2910 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002912 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002913 }
2914
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 }
2918
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 return p;
2920}
2921
2922/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 */
2925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927{
2928 vim_free(lp->ll_exp_name);
2929 vim_free(lp->ll_newkey);
2930}
2931
2932/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002933 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 */
2937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938set_var_lval(
2939 lval_T *lp,
2940 char_u *endp,
2941 typval_T *rettv,
2942 int copy,
2943 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944{
2945 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 listitem_T *ri;
2947 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948
2949 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 cc = *endp;
2954 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 if (op != NULL && *op != '=')
2956 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002957 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002960 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002961 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002962 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002964 if ((di == NULL
2965 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2966 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2967 FALSE)))
2968 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002969 set_var(lp->ll_name, &tv, FALSE);
2970 clear_tv(&tv);
2971 }
2972 }
2973 else
2974 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002978 else if (tv_check_lock(lp->ll_newkey == NULL
2979 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002980 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002981 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 else if (lp->ll_range)
2983 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 listitem_T *ll_li = lp->ll_li;
2985 int ll_n1 = lp->ll_n1;
2986
2987 /*
2988 * Check whether any of the list items is locked
2989 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002990 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002991 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002992 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002993 return;
2994 ri = ri->li_next;
2995 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2996 break;
2997 ll_li = ll_li->li_next;
2998 ++ll_n1;
2999 }
3000
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 /*
3002 * Assign the List values to the list items.
3003 */
3004 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 if (op != NULL && *op != '=')
3007 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3008 else
3009 {
3010 clear_tv(&lp->ll_li->li_tv);
3011 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 ri = ri->li_next;
3014 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3015 break;
3016 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003019 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 ri = NULL;
3022 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003023 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003024 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 lp->ll_li = lp->ll_li->li_next;
3026 ++lp->ll_n1;
3027 }
3028 if (ri != NULL)
3029 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003030 else if (lp->ll_empty2
3031 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 : lp->ll_n1 != lp->ll_n2)
3033 EMSG(_("E711: List value has not enough items"));
3034 }
3035 else
3036 {
3037 /*
3038 * Assign to a List or Dictionary item.
3039 */
3040 if (lp->ll_newkey != NULL)
3041 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 if (op != NULL && *op != '=')
3043 {
3044 EMSG2(_(e_letwrong), op);
3045 return;
3046 }
3047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003049 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 if (di == NULL)
3051 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003052 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3053 {
3054 vim_free(di);
3055 return;
3056 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 else if (op != NULL && *op != '=')
3060 {
3061 tv_op(lp->ll_tv, rettv, op);
3062 return;
3063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003065 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003066
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 /*
3068 * Assign the value to the variable or list item.
3069 */
3070 if (copy)
3071 copy_tv(rettv, lp->ll_tv);
3072 else
3073 {
3074 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003075 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003076 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077 }
3078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079}
3080
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003081/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3083 * Returns OK or FAIL.
3084 */
3085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087{
3088 long n;
3089 char_u numbuf[NUMBUFLEN];
3090 char_u *s;
3091
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003092 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3093 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3094 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 {
3096 switch (tv1->v_type)
3097 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003098 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 case VAR_DICT:
3100 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003101 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003102 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003103 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003104 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 break;
3106
3107 case VAR_LIST:
3108 if (*op != '+' || tv2->v_type != VAR_LIST)
3109 break;
3110 /* List += List */
3111 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3112 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3113 return OK;
3114
3115 case VAR_NUMBER:
3116 case VAR_STRING:
3117 if (tv2->v_type == VAR_LIST)
3118 break;
3119 if (*op == '+' || *op == '-')
3120 {
3121 /* nr += nr or nr -= nr*/
3122 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123#ifdef FEAT_FLOAT
3124 if (tv2->v_type == VAR_FLOAT)
3125 {
3126 float_T f = n;
3127
3128 if (*op == '+')
3129 f += tv2->vval.v_float;
3130 else
3131 f -= tv2->vval.v_float;
3132 clear_tv(tv1);
3133 tv1->v_type = VAR_FLOAT;
3134 tv1->vval.v_float = f;
3135 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003136 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003137#endif
3138 {
3139 if (*op == '+')
3140 n += get_tv_number(tv2);
3141 else
3142 n -= get_tv_number(tv2);
3143 clear_tv(tv1);
3144 tv1->v_type = VAR_NUMBER;
3145 tv1->vval.v_number = n;
3146 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 }
3148 else
3149 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150 if (tv2->v_type == VAR_FLOAT)
3151 break;
3152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003153 /* str .= str */
3154 s = get_tv_string(tv1);
3155 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3156 clear_tv(tv1);
3157 tv1->v_type = VAR_STRING;
3158 tv1->vval.v_string = s;
3159 }
3160 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003163#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164 {
3165 float_T f;
3166
3167 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3168 && tv2->v_type != VAR_NUMBER
3169 && tv2->v_type != VAR_STRING))
3170 break;
3171 if (tv2->v_type == VAR_FLOAT)
3172 f = tv2->vval.v_float;
3173 else
3174 f = get_tv_number(tv2);
3175 if (*op == '+')
3176 tv1->vval.v_float += f;
3177 else
3178 tv1->vval.v_float -= f;
3179 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003181 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003182 }
3183 }
3184
3185 EMSG2(_(e_letwrong), op);
3186 return FAIL;
3187}
3188
3189/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190 * Add a watcher to a list.
3191 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003193list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194{
3195 lw->lw_next = l->lv_watch;
3196 l->lv_watch = lw;
3197}
3198
3199/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003200 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 * No warning when it isn't found...
3202 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003204list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205{
Bram Moolenaar33570922005-01-25 22:26:29 +00003206 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207
3208 lwp = &l->lv_watch;
3209 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3210 {
3211 if (lw == lwrem)
3212 {
3213 *lwp = lw->lw_next;
3214 break;
3215 }
3216 lwp = &lw->lw_next;
3217 }
3218}
3219
3220/*
3221 * Just before removing an item from a list: advance watchers to the next
3222 * item.
3223 */
3224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226{
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228
3229 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3230 if (lw->lw_item == item)
3231 lw->lw_item = item->li_next;
3232}
3233
3234/*
3235 * Evaluate the expression used in a ":for var in expr" command.
3236 * "arg" points to "var".
3237 * Set "*errp" to TRUE for an error, FALSE otherwise;
3238 * Return a pointer that holds the info. Null when there is an error.
3239 */
3240 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003241eval_for_line(
3242 char_u *arg,
3243 int *errp,
3244 char_u **nextcmdp,
3245 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246{
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003249 typval_T tv;
3250 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251
3252 *errp = TRUE; /* default: there is an error */
3253
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 if (fi == NULL)
3256 return NULL;
3257
3258 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3259 if (expr == NULL)
3260 return fi;
3261
3262 expr = skipwhite(expr);
3263 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3264 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003265 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 return fi;
3267 }
3268
3269 if (skip)
3270 ++emsg_skip;
3271 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3272 {
3273 *errp = FALSE;
3274 if (!skip)
3275 {
3276 l = tv.vval.v_list;
3277 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 clear_tv(&tv);
3281 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 else
3283 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003284 /* No need to increment the refcount, it's already set for the
3285 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 fi->fi_list = l;
3287 list_add_watch(l, &fi->fi_lw);
3288 fi->fi_lw.lw_item = l->lv_first;
3289 }
3290 }
3291 }
3292 if (skip)
3293 --emsg_skip;
3294
3295 return fi;
3296}
3297
3298/*
3299 * Use the first item in a ":for" list. Advance to the next.
3300 * Assign the values to the variable (list). "arg" points to the first one.
3301 * Return TRUE when a valid item was found, FALSE when at end of list or
3302 * something wrong.
3303 */
3304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003307 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003309 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310
3311 item = fi->fi_lw.lw_item;
3312 if (item == NULL)
3313 result = FALSE;
3314 else
3315 {
3316 fi->fi_lw.lw_item = item->li_next;
3317 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3318 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3319 }
3320 return result;
3321}
3322
3323/*
3324 * Free the structure used to store info used by ":for".
3325 */
3326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328{
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003331 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003332 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003334 list_unref(fi->fi_list);
3335 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 vim_free(fi);
3337}
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3340
3341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342set_context_for_expression(
3343 expand_T *xp,
3344 char_u *arg,
3345 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 int got_eq = FALSE;
3348 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 if (cmdidx == CMD_let)
3352 {
3353 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003354 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 {
3356 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003357 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 {
3359 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003360 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361 if (vim_iswhite(*p))
3362 break;
3363 }
3364 return;
3365 }
3366 }
3367 else
3368 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3369 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 while ((xp->xp_pattern = vim_strpbrk(arg,
3371 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3372 {
3373 c = *xp->xp_pattern;
3374 if (c == '&')
3375 {
3376 c = xp->xp_pattern[1];
3377 if (c == '&')
3378 {
3379 ++xp->xp_pattern;
3380 xp->xp_context = cmdidx != CMD_let || got_eq
3381 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3382 }
3383 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003386 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3387 xp->xp_pattern += 2;
3388
3389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
3391 else if (c == '$')
3392 {
3393 /* environment variable */
3394 xp->xp_context = EXPAND_ENV_VARS;
3395 }
3396 else if (c == '=')
3397 {
3398 got_eq = TRUE;
3399 xp->xp_context = EXPAND_EXPRESSION;
3400 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003401 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 && xp->xp_context == EXPAND_FUNCTIONS
3403 && vim_strchr(xp->xp_pattern, '(') == NULL)
3404 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003405 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 break;
3407 }
3408 else if (cmdidx != CMD_let || got_eq)
3409 {
3410 if (c == '"') /* string */
3411 {
3412 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3413 if (c == '\\' && xp->xp_pattern[1] != NUL)
3414 ++xp->xp_pattern;
3415 xp->xp_context = EXPAND_NOTHING;
3416 }
3417 else if (c == '\'') /* literal string */
3418 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003419 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3421 /* skip */ ;
3422 xp->xp_context = EXPAND_NOTHING;
3423 }
3424 else if (c == '|')
3425 {
3426 if (xp->xp_pattern[1] == '|')
3427 {
3428 ++xp->xp_pattern;
3429 xp->xp_context = EXPAND_EXPRESSION;
3430 }
3431 else
3432 xp->xp_context = EXPAND_COMMANDS;
3433 }
3434 else
3435 xp->xp_context = EXPAND_EXPRESSION;
3436 }
3437 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003438 /* Doesn't look like something valid, expand as an expression
3439 * anyway. */
3440 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 arg = xp->xp_pattern;
3442 if (*arg != NUL)
3443 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3444 /* skip */ ;
3445 }
3446 xp->xp_pattern = arg;
3447}
3448
3449#endif /* FEAT_CMDL_COMPL */
3450
3451/*
3452 * ":1,25call func(arg1, arg2)" function call.
3453 */
3454 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003455ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 char_u *arg = eap->arg;
3458 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003460 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003462 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 linenr_T lnum;
3464 int doesrange;
3465 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003466 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003467 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003469 if (eap->skip)
3470 {
3471 /* trans_function_name() doesn't work well when skipping, use eval0()
3472 * instead to skip to any following command, e.g. for:
3473 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003474 ++emsg_skip;
3475 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3476 clear_tv(&rettv);
3477 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003478 return;
3479 }
3480
Bram Moolenaar65639032016-03-16 21:40:30 +01003481 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003482 if (fudi.fd_newkey != NULL)
3483 {
3484 /* Still need to give an error message for missing key. */
3485 EMSG2(_(e_dictkey), fudi.fd_newkey);
3486 vim_free(fudi.fd_newkey);
3487 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 if (tofree == NULL)
3489 return;
3490
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003491 /* Increase refcount on dictionary, it could get deleted when evaluating
3492 * the arguments. */
3493 if (fudi.fd_dict != NULL)
3494 ++fudi.fd_dict->dv_refcount;
3495
Bram Moolenaar65639032016-03-16 21:40:30 +01003496 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3497 * contents. For VAR_PARTIAL get its partial, unless we already have one
3498 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003499 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003500 name = deref_func_name(tofree, &len,
3501 partial != NULL ? NULL : &partial, FALSE);
3502
Bram Moolenaar532c7802005-01-27 14:44:31 +00003503 /* Skip white space to allow ":call func ()". Not good, but required for
3504 * backward compatibility. */
3505 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003506 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 if (*startarg != '(')
3509 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003510 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 goto end;
3512 }
3513
3514 /*
3515 * When skipping, evaluate the function once, to find the end of the
3516 * arguments.
3517 * When the function takes a range, this is discovered after the first
3518 * call, and the loop is broken.
3519 */
3520 if (eap->skip)
3521 {
3522 ++emsg_skip;
3523 lnum = eap->line2; /* do it once, also with an invalid range */
3524 }
3525 else
3526 lnum = eap->line1;
3527 for ( ; lnum <= eap->line2; ++lnum)
3528 {
3529 if (!eap->skip && eap->addr_count > 0)
3530 {
3531 curwin->w_cursor.lnum = lnum;
3532 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003533#ifdef FEAT_VIRTUALEDIT
3534 curwin->w_cursor.coladd = 0;
3535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003538 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003539 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003540 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 failed = TRUE;
3543 break;
3544 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003545
3546 /* Handle a function returning a Funcref, Dictionary or List. */
3547 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3548 {
3549 failed = TRUE;
3550 break;
3551 }
3552
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003553 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 if (doesrange || eap->skip)
3555 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003558 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003559 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003560 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (aborting())
3562 break;
3563 }
3564 if (eap->skip)
3565 --emsg_skip;
3566
3567 if (!failed)
3568 {
3569 /* Check for trailing illegal characters and a following command. */
3570 if (!ends_excmd(*arg))
3571 {
3572 emsg_severe = TRUE;
3573 EMSG(_(e_trailing));
3574 }
3575 else
3576 eap->nextcmd = check_nextcmd(arg);
3577 }
3578
3579end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003580 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003581 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582}
3583
3584/*
3585 * ":unlet[!] var1 ... " command.
3586 */
3587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003588ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 ex_unletlock(eap, eap->arg, 0);
3591}
3592
3593/*
3594 * ":lockvar" and ":unlockvar" commands
3595 */
3596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003597ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003600 int deep = 2;
3601
3602 if (eap->forceit)
3603 deep = -1;
3604 else if (vim_isdigit(*arg))
3605 {
3606 deep = getdigits(&arg);
3607 arg = skipwhite(arg);
3608 }
3609
3610 ex_unletlock(eap, arg, deep);
3611}
3612
3613/*
3614 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3615 */
3616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617ex_unletlock(
3618 exarg_T *eap,
3619 char_u *argstart,
3620 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003621{
3622 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003625 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
3627 do
3628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003630 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003631 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 if (lv.ll_name == NULL)
3633 error = TRUE; /* error but continue parsing */
3634 if (name_end == NULL || (!vim_iswhite(*name_end)
3635 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 if (name_end != NULL)
3638 {
3639 emsg_severe = TRUE;
3640 EMSG(_(e_trailing));
3641 }
3642 if (!(eap->skip || error))
3643 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 break;
3645 }
3646
3647 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648 {
3649 if (eap->cmdidx == CMD_unlet)
3650 {
3651 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3652 error = TRUE;
3653 }
3654 else
3655 {
3656 if (do_lock_var(&lv, name_end, deep,
3657 eap->cmdidx == CMD_lockvar) == FAIL)
3658 error = TRUE;
3659 }
3660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 if (!eap->skip)
3663 clear_lval(&lv);
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 arg = skipwhite(name_end);
3666 } while (!ends_excmd(*arg));
3667
3668 eap->nextcmd = check_nextcmd(arg);
3669}
3670
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672do_unlet_var(
3673 lval_T *lp,
3674 char_u *name_end,
3675 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003676{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 int ret = OK;
3678 int cc;
3679
3680 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 cc = *name_end;
3683 *name_end = NUL;
3684
3685 /* Normal name or expanded name. */
3686 if (check_changedtick(lp->ll_name))
3687 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003692 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003693 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003694 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003695 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003696 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 else if (lp->ll_range)
3698 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003700 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003701 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003702
3703 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3704 {
3705 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003706 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003707 return FAIL;
3708 ll_li = li;
3709 ++ll_n1;
3710 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711
3712 /* Delete a range of List items. */
3713 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3714 {
3715 li = lp->ll_li->li_next;
3716 listitem_remove(lp->ll_list, lp->ll_li);
3717 lp->ll_li = li;
3718 ++lp->ll_n1;
3719 }
3720 }
3721 else
3722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003723 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 /* unlet a List item. */
3725 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003728 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 }
3730
3731 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732}
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734/*
3735 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003736 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 */
3738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003739do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 hashtab_T *ht;
3742 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003743 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003744 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003745 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
Bram Moolenaar33570922005-01-25 22:26:29 +00003747 ht = find_var_ht(name, &varname);
3748 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003750 if (ht == &globvarht)
3751 d = &globvardict;
3752 else if (current_funccal != NULL
3753 && ht == &current_funccal->l_vars.dv_hashtab)
3754 d = &current_funccal->l_vars;
3755 else if (ht == &compat_hashtab)
3756 d = &vimvardict;
3757 else
3758 {
3759 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3760 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3761 }
3762 if (d == NULL)
3763 {
3764 EMSG2(_(e_intern2), "do_unlet()");
3765 return FAIL;
3766 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 hi = hash_find(ht, varname);
3768 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003769 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003770 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003771 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003772 || var_check_ro(di->di_flags, name, FALSE)
3773 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003774 return FAIL;
3775
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003776 delete_var(ht, hi);
3777 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003780 if (forceit)
3781 return OK;
3782 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 return FAIL;
3784}
3785
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003786/*
3787 * Lock or unlock variable indicated by "lp".
3788 * "deep" is the levels to go (-1 for unlimited);
3789 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3790 */
3791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003792do_lock_var(
3793 lval_T *lp,
3794 char_u *name_end,
3795 int deep,
3796 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797{
3798 int ret = OK;
3799 int cc;
3800 dictitem_T *di;
3801
3802 if (deep == 0) /* nothing to do */
3803 return OK;
3804
3805 if (lp->ll_tv == NULL)
3806 {
3807 cc = *name_end;
3808 *name_end = NUL;
3809
3810 /* Normal name or expanded name. */
3811 if (check_changedtick(lp->ll_name))
3812 ret = FAIL;
3813 else
3814 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003815 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003816 if (di == NULL)
3817 ret = FAIL;
3818 else
3819 {
3820 if (lock)
3821 di->di_flags |= DI_FLAGS_LOCK;
3822 else
3823 di->di_flags &= ~DI_FLAGS_LOCK;
3824 item_lock(&di->di_tv, deep, lock);
3825 }
3826 }
3827 *name_end = cc;
3828 }
3829 else if (lp->ll_range)
3830 {
3831 listitem_T *li = lp->ll_li;
3832
3833 /* (un)lock a range of List items. */
3834 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3835 {
3836 item_lock(&li->li_tv, deep, lock);
3837 li = li->li_next;
3838 ++lp->ll_n1;
3839 }
3840 }
3841 else if (lp->ll_list != NULL)
3842 /* (un)lock a List item. */
3843 item_lock(&lp->ll_li->li_tv, deep, lock);
3844 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003845 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003846 item_lock(&lp->ll_di->di_tv, deep, lock);
3847
3848 return ret;
3849}
3850
3851/*
3852 * Lock or unlock an item. "deep" is nr of levels to go.
3853 */
3854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003855item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003856{
3857 static int recurse = 0;
3858 list_T *l;
3859 listitem_T *li;
3860 dict_T *d;
3861 hashitem_T *hi;
3862 int todo;
3863
3864 if (recurse >= DICT_MAXNEST)
3865 {
3866 EMSG(_("E743: variable nested too deep for (un)lock"));
3867 return;
3868 }
3869 if (deep == 0)
3870 return;
3871 ++recurse;
3872
3873 /* lock/unlock the item itself */
3874 if (lock)
3875 tv->v_lock |= VAR_LOCKED;
3876 else
3877 tv->v_lock &= ~VAR_LOCKED;
3878
3879 switch (tv->v_type)
3880 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003881 case VAR_UNKNOWN:
3882 case VAR_NUMBER:
3883 case VAR_STRING:
3884 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003885 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003886 case VAR_FLOAT:
3887 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003888 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003889 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003890 break;
3891
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003892 case VAR_LIST:
3893 if ((l = tv->vval.v_list) != NULL)
3894 {
3895 if (lock)
3896 l->lv_lock |= VAR_LOCKED;
3897 else
3898 l->lv_lock &= ~VAR_LOCKED;
3899 if (deep < 0 || deep > 1)
3900 /* recursive: lock/unlock the items the List contains */
3901 for (li = l->lv_first; li != NULL; li = li->li_next)
3902 item_lock(&li->li_tv, deep - 1, lock);
3903 }
3904 break;
3905 case VAR_DICT:
3906 if ((d = tv->vval.v_dict) != NULL)
3907 {
3908 if (lock)
3909 d->dv_lock |= VAR_LOCKED;
3910 else
3911 d->dv_lock &= ~VAR_LOCKED;
3912 if (deep < 0 || deep > 1)
3913 {
3914 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003915 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003916 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3917 {
3918 if (!HASHITEM_EMPTY(hi))
3919 {
3920 --todo;
3921 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3922 }
3923 }
3924 }
3925 }
3926 }
3927 --recurse;
3928}
3929
Bram Moolenaara40058a2005-07-11 22:42:07 +00003930/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003931 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3932 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003933 */
3934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003935tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003936{
3937 return (tv->v_lock & VAR_LOCKED)
3938 || (tv->v_type == VAR_LIST
3939 && tv->vval.v_list != NULL
3940 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3941 || (tv->v_type == VAR_DICT
3942 && tv->vval.v_dict != NULL
3943 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3944}
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3947/*
3948 * Delete all "menutrans_" variables.
3949 */
3950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003951del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003957 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 {
3960 if (!HASHITEM_EMPTY(hi))
3961 {
3962 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3964 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 }
3966 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968}
3969#endif
3970
3971#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3972
3973/*
3974 * Local string buffer for the next two functions to store a variable name
3975 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3976 * get_user_var_name().
3977 */
3978
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003979static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981static char_u *varnamebuf = NULL;
3982static int varnamebuflen = 0;
3983
3984/*
3985 * Function to concatenate a prefix and a variable name.
3986 */
3987 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003988cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
3990 int len;
3991
3992 len = (int)STRLEN(name) + 3;
3993 if (len > varnamebuflen)
3994 {
3995 vim_free(varnamebuf);
3996 len += 10; /* some additional space */
3997 varnamebuf = alloc(len);
3998 if (varnamebuf == NULL)
3999 {
4000 varnamebuflen = 0;
4001 return NULL;
4002 }
4003 varnamebuflen = len;
4004 }
4005 *varnamebuf = prefix;
4006 varnamebuf[1] = ':';
4007 STRCPY(varnamebuf + 2, name);
4008 return varnamebuf;
4009}
4010
4011/*
4012 * Function given to ExpandGeneric() to obtain the list of user defined
4013 * (global/buffer/window/built-in) variable names.
4014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004016get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004018 static long_u gdone;
4019 static long_u bdone;
4020 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004021#ifdef FEAT_WINDOWS
4022 static long_u tdone;
4023#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004024 static int vidx;
4025 static hashitem_T *hi;
4026 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
4028 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004030 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004031#ifdef FEAT_WINDOWS
4032 tdone = 0;
4033#endif
4034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035
4036 /* Global variables */
4037 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004041 else
4042 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 while (HASHITEM_EMPTY(hi))
4044 ++hi;
4045 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4046 return cat_prefix_varname('g', hi->hi_key);
4047 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004049
4050 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004051 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 else
4057 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004058 while (HASHITEM_EMPTY(hi))
4059 ++hi;
4060 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004064 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 return (char_u *)"b:changedtick";
4066 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004067
4068 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004069 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004072 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004074 else
4075 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004076 while (HASHITEM_EMPTY(hi))
4077 ++hi;
4078 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004080
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004081#ifdef FEAT_WINDOWS
4082 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004083 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084 if (tdone < ht->ht_used)
4085 {
4086 if (tdone++ == 0)
4087 hi = ht->ht_array;
4088 else
4089 ++hi;
4090 while (HASHITEM_EMPTY(hi))
4091 ++hi;
4092 return cat_prefix_varname('t', hi->hi_key);
4093 }
4094#endif
4095
Bram Moolenaar33570922005-01-25 22:26:29 +00004096 /* v: variables */
4097 if (vidx < VV_LEN)
4098 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100 vim_free(varnamebuf);
4101 varnamebuf = NULL;
4102 varnamebuflen = 0;
4103 return NULL;
4104}
4105
4106#endif /* FEAT_CMDL_COMPL */
4107
4108/*
4109 * types for expressions.
4110 */
4111typedef enum
4112{
4113 TYPE_UNKNOWN = 0
4114 , TYPE_EQUAL /* == */
4115 , TYPE_NEQUAL /* != */
4116 , TYPE_GREATER /* > */
4117 , TYPE_GEQUAL /* >= */
4118 , TYPE_SMALLER /* < */
4119 , TYPE_SEQUAL /* <= */
4120 , TYPE_MATCH /* =~ */
4121 , TYPE_NOMATCH /* !~ */
4122} exptype_T;
4123
4124/*
4125 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4128 */
4129
4130/*
4131 * Handle zero level expression.
4132 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004134 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 * Return OK or FAIL.
4136 */
4137 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004138eval0(
4139 char_u *arg,
4140 typval_T *rettv,
4141 char_u **nextcmd,
4142 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 int ret;
4145 char_u *p;
4146
4147 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if (ret == FAIL || !ends_excmd(*p))
4150 {
4151 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 /*
4154 * Report the invalid expression unless the expression evaluation has
4155 * been cancelled due to an aborting error, an interrupt, or an
4156 * exception.
4157 */
4158 if (!aborting())
4159 EMSG2(_(e_invexpr2), arg);
4160 ret = FAIL;
4161 }
4162 if (nextcmd != NULL)
4163 *nextcmd = check_nextcmd(p);
4164
4165 return ret;
4166}
4167
4168/*
4169 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004170 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 *
4172 * "arg" must point to the first non-white of the expression.
4173 * "arg" is advanced to the next non-white after the recognized expression.
4174 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004175 * Note: "rettv.v_lock" is not set.
4176 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 * Return OK or FAIL.
4178 */
4179 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004180eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
4182 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004183 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
4185 /*
4186 * Get the first variable.
4187 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 return FAIL;
4190
4191 if ((*arg)[0] == '?')
4192 {
4193 result = FALSE;
4194 if (evaluate)
4195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 int error = FALSE;
4197
4198 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 if (error)
4202 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204
4205 /*
4206 * Get the second variable.
4207 */
4208 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return FAIL;
4211
4212 /*
4213 * Check for the ":".
4214 */
4215 if ((*arg)[0] != ':')
4216 {
4217 EMSG(_("E109: Missing ':' after '?'"));
4218 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 return FAIL;
4221 }
4222
4223 /*
4224 * Get the third variable.
4225 */
4226 *arg = skipwhite(*arg + 1);
4227 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4228 {
4229 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 return FAIL;
4232 }
4233 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236
4237 return OK;
4238}
4239
4240/*
4241 * Handle first level expression:
4242 * expr2 || expr2 || expr2 logical OR
4243 *
4244 * "arg" must point to the first non-white of the expression.
4245 * "arg" is advanced to the next non-white after the recognized expression.
4246 *
4247 * Return OK or FAIL.
4248 */
4249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
Bram Moolenaar33570922005-01-25 22:26:29 +00004252 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 long result;
4254 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 /*
4258 * Get the first variable.
4259 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 return FAIL;
4262
4263 /*
4264 * Repeat until there is no following "||".
4265 */
4266 first = TRUE;
4267 result = FALSE;
4268 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4269 {
4270 if (evaluate && first)
4271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 if (error)
4276 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 first = FALSE;
4278 }
4279
4280 /*
4281 * Get the second variable.
4282 */
4283 *arg = skipwhite(*arg + 2);
4284 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4285 return FAIL;
4286
4287 /*
4288 * Compute the result.
4289 */
4290 if (evaluate && !result)
4291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 if (error)
4296 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 if (evaluate)
4299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 rettv->v_type = VAR_NUMBER;
4301 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 }
4304
4305 return OK;
4306}
4307
4308/*
4309 * Handle second level expression:
4310 * expr3 && expr3 && expr3 logical AND
4311 *
4312 * "arg" must point to the first non-white of the expression.
4313 * "arg" is advanced to the next non-white after the recognized expression.
4314 *
4315 * Return OK or FAIL.
4316 */
4317 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004318eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319{
Bram Moolenaar33570922005-01-25 22:26:29 +00004320 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 long result;
4322 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
4325 /*
4326 * Get the first variable.
4327 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 return FAIL;
4330
4331 /*
4332 * Repeat until there is no following "&&".
4333 */
4334 first = TRUE;
4335 result = TRUE;
4336 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4337 {
4338 if (evaluate && first)
4339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (error)
4344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 first = FALSE;
4346 }
4347
4348 /*
4349 * Get the second variable.
4350 */
4351 *arg = skipwhite(*arg + 2);
4352 if (eval4(arg, &var2, evaluate && result) == FAIL)
4353 return FAIL;
4354
4355 /*
4356 * Compute the result.
4357 */
4358 if (evaluate && result)
4359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004362 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 if (error)
4364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 if (evaluate)
4367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 rettv->v_type = VAR_NUMBER;
4369 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 }
4372
4373 return OK;
4374}
4375
4376/*
4377 * Handle third level expression:
4378 * var1 == var2
4379 * var1 =~ var2
4380 * var1 != var2
4381 * var1 !~ var2
4382 * var1 > var2
4383 * var1 >= var2
4384 * var1 < var2
4385 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 * var1 is var2
4387 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 *
4389 * "arg" must point to the first non-white of the expression.
4390 * "arg" is advanced to the next non-white after the recognized expression.
4391 *
4392 * Return OK or FAIL.
4393 */
4394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004395eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
Bram Moolenaar33570922005-01-25 22:26:29 +00004397 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 char_u *p;
4399 int i;
4400 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004401 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int len = 2;
4403 long n1, n2;
4404 char_u *s1, *s2;
4405 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4406 regmatch_T regmatch;
4407 int ic;
4408 char_u *save_cpo;
4409
4410 /*
4411 * Get the first variable.
4412 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004413 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 return FAIL;
4415
4416 p = *arg;
4417 switch (p[0])
4418 {
4419 case '=': if (p[1] == '=')
4420 type = TYPE_EQUAL;
4421 else if (p[1] == '~')
4422 type = TYPE_MATCH;
4423 break;
4424 case '!': if (p[1] == '=')
4425 type = TYPE_NEQUAL;
4426 else if (p[1] == '~')
4427 type = TYPE_NOMATCH;
4428 break;
4429 case '>': if (p[1] != '=')
4430 {
4431 type = TYPE_GREATER;
4432 len = 1;
4433 }
4434 else
4435 type = TYPE_GEQUAL;
4436 break;
4437 case '<': if (p[1] != '=')
4438 {
4439 type = TYPE_SMALLER;
4440 len = 1;
4441 }
4442 else
4443 type = TYPE_SEQUAL;
4444 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 case 'i': if (p[1] == 's')
4446 {
4447 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4448 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004449 i = p[len];
4450 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 {
4452 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4453 type_is = TRUE;
4454 }
4455 }
4456 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458
4459 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004460 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 */
4462 if (type != TYPE_UNKNOWN)
4463 {
4464 /* extra question mark appended: ignore case */
4465 if (p[len] == '?')
4466 {
4467 ic = TRUE;
4468 ++len;
4469 }
4470 /* extra '#' appended: match case */
4471 else if (p[len] == '#')
4472 {
4473 ic = FALSE;
4474 ++len;
4475 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004476 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 else
4478 ic = p_ic;
4479
4480 /*
4481 * Get the second variable.
4482 */
4483 *arg = skipwhite(p + len);
4484 if (eval5(arg, &var2, evaluate) == FAIL)
4485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004486 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 return FAIL;
4488 }
4489
4490 if (evaluate)
4491 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004492 if (type_is && rettv->v_type != var2.v_type)
4493 {
4494 /* For "is" a different type always means FALSE, for "notis"
4495 * it means TRUE. */
4496 n1 = (type == TYPE_NEQUAL);
4497 }
4498 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4499 {
4500 if (type_is)
4501 {
4502 n1 = (rettv->v_type == var2.v_type
4503 && rettv->vval.v_list == var2.vval.v_list);
4504 if (type == TYPE_NEQUAL)
4505 n1 = !n1;
4506 }
4507 else if (rettv->v_type != var2.v_type
4508 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4509 {
4510 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004511 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004513 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004514 clear_tv(rettv);
4515 clear_tv(&var2);
4516 return FAIL;
4517 }
4518 else
4519 {
4520 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004521 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4522 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 if (type == TYPE_NEQUAL)
4524 n1 = !n1;
4525 }
4526 }
4527
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004528 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4529 {
4530 if (type_is)
4531 {
4532 n1 = (rettv->v_type == var2.v_type
4533 && rettv->vval.v_dict == var2.vval.v_dict);
4534 if (type == TYPE_NEQUAL)
4535 n1 = !n1;
4536 }
4537 else if (rettv->v_type != var2.v_type
4538 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4539 {
4540 if (rettv->v_type != var2.v_type)
4541 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4542 else
4543 EMSG(_("E736: Invalid operation for Dictionary"));
4544 clear_tv(rettv);
4545 clear_tv(&var2);
4546 return FAIL;
4547 }
4548 else
4549 {
4550 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004551 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4552 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004553 if (type == TYPE_NEQUAL)
4554 n1 = !n1;
4555 }
4556 }
4557
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004558 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4559 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004560 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004561 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004562 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004563 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004564 clear_tv(rettv);
4565 clear_tv(&var2);
4566 return FAIL;
4567 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004568 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004569 if (type == TYPE_NEQUAL)
4570 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004571 }
4572
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004573#ifdef FEAT_FLOAT
4574 /*
4575 * If one of the two variables is a float, compare as a float.
4576 * When using "=~" or "!~", always compare as string.
4577 */
4578 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4579 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4580 {
4581 float_T f1, f2;
4582
4583 if (rettv->v_type == VAR_FLOAT)
4584 f1 = rettv->vval.v_float;
4585 else
4586 f1 = get_tv_number(rettv);
4587 if (var2.v_type == VAR_FLOAT)
4588 f2 = var2.vval.v_float;
4589 else
4590 f2 = get_tv_number(&var2);
4591 n1 = FALSE;
4592 switch (type)
4593 {
4594 case TYPE_EQUAL: n1 = (f1 == f2); break;
4595 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4596 case TYPE_GREATER: n1 = (f1 > f2); break;
4597 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4598 case TYPE_SMALLER: n1 = (f1 < f2); break;
4599 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4600 case TYPE_UNKNOWN:
4601 case TYPE_MATCH:
4602 case TYPE_NOMATCH: break; /* avoid gcc warning */
4603 }
4604 }
4605#endif
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /*
4608 * If one of the two variables is a number, compare as a number.
4609 * When using "=~" or "!~", always compare as string.
4610 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004611 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 n1 = get_tv_number(rettv);
4615 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 switch (type)
4617 {
4618 case TYPE_EQUAL: n1 = (n1 == n2); break;
4619 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4620 case TYPE_GREATER: n1 = (n1 > n2); break;
4621 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4622 case TYPE_SMALLER: n1 = (n1 < n2); break;
4623 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4624 case TYPE_UNKNOWN:
4625 case TYPE_MATCH:
4626 case TYPE_NOMATCH: break; /* avoid gcc warning */
4627 }
4628 }
4629 else
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 s1 = get_tv_string_buf(rettv, buf1);
4632 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4634 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4635 else
4636 i = 0;
4637 n1 = FALSE;
4638 switch (type)
4639 {
4640 case TYPE_EQUAL: n1 = (i == 0); break;
4641 case TYPE_NEQUAL: n1 = (i != 0); break;
4642 case TYPE_GREATER: n1 = (i > 0); break;
4643 case TYPE_GEQUAL: n1 = (i >= 0); break;
4644 case TYPE_SMALLER: n1 = (i < 0); break;
4645 case TYPE_SEQUAL: n1 = (i <= 0); break;
4646
4647 case TYPE_MATCH:
4648 case TYPE_NOMATCH:
4649 /* avoid 'l' flag in 'cpoptions' */
4650 save_cpo = p_cpo;
4651 p_cpo = (char_u *)"";
4652 regmatch.regprog = vim_regcomp(s2,
4653 RE_MAGIC + RE_STRING);
4654 regmatch.rm_ic = ic;
4655 if (regmatch.regprog != NULL)
4656 {
4657 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004658 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (type == TYPE_NOMATCH)
4660 n1 = !n1;
4661 }
4662 p_cpo = save_cpo;
4663 break;
4664
4665 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4666 }
4667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
4669 clear_tv(&var2);
4670 rettv->v_type = VAR_NUMBER;
4671 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 }
4674
4675 return OK;
4676}
4677
4678/*
4679 * Handle fourth level expression:
4680 * + number addition
4681 * - number subtraction
4682 * . string concatenation
4683 *
4684 * "arg" must point to the first non-white of the expression.
4685 * "arg" is advanced to the next non-white after the recognized expression.
4686 *
4687 * Return OK or FAIL.
4688 */
4689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004690eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 typval_T var2;
4693 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int op;
4695 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 float_T f1 = 0, f2 = 0;
4698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 char_u *s1, *s2;
4700 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4701 char_u *p;
4702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '+', '-' or '.' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '+' && op != '-' && op != '.')
4716 break;
4717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 if ((op != '+' || rettv->v_type != VAR_LIST)
4719#ifdef FEAT_FLOAT
4720 && (op == '.' || rettv->v_type != VAR_FLOAT)
4721#endif
4722 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 {
4724 /* For "list + ...", an illegal use of the first operand as
4725 * a number cannot be determined before evaluating the 2nd
4726 * operand: if this is also a list, all is ok.
4727 * For "something . ...", "something - ..." or "non-list + ...",
4728 * we know that the first operand needs to be a string or number
4729 * without evaluating the 2nd operand. So check before to avoid
4730 * side effects after an error. */
4731 if (evaluate && get_tv_string_chk(rettv) == NULL)
4732 {
4733 clear_tv(rettv);
4734 return FAIL;
4735 }
4736 }
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /*
4739 * Get the second variable.
4740 */
4741 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return FAIL;
4746 }
4747
4748 if (evaluate)
4749 {
4750 /*
4751 * Compute the result.
4752 */
4753 if (op == '.')
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4756 s2 = get_tv_string_buf_chk(&var2, buf2);
4757 if (s2 == NULL) /* type error ? */
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004763 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768 else if (op == '+' && rettv->v_type == VAR_LIST
4769 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004770 {
4771 /* concatenate Lists */
4772 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4773 &var3) == FAIL)
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
4779 clear_tv(rettv);
4780 *rettv = var3;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
4783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 int error = FALSE;
4785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#ifdef FEAT_FLOAT
4787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 f1 = rettv->vval.v_float;
4790 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 else
4793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 n1 = get_tv_number_chk(rettv, &error);
4796 if (error)
4797 {
4798 /* This can only happen for "list + non-list". For
4799 * "non-list + ..." or "something - ...", we returned
4800 * before evaluating the 2nd operand. */
4801 clear_tv(rettv);
4802 return FAIL;
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 f1 = n1;
4807#endif
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 {
4812 f2 = var2.vval.v_float;
4813 n2 = 0;
4814 }
4815 else
4816#endif
4817 {
4818 n2 = get_tv_number_chk(&var2, &error);
4819 if (error)
4820 {
4821 clear_tv(rettv);
4822 clear_tv(&var2);
4823 return FAIL;
4824 }
4825#ifdef FEAT_FLOAT
4826 if (rettv->v_type == VAR_FLOAT)
4827 f2 = n2;
4828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831
4832#ifdef FEAT_FLOAT
4833 /* If there is a float on either side the result is a float. */
4834 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4835 {
4836 if (op == '+')
4837 f1 = f1 + f2;
4838 else
4839 f1 = f1 - f2;
4840 rettv->v_type = VAR_FLOAT;
4841 rettv->vval.v_float = f1;
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#endif
4845 {
4846 if (op == '+')
4847 n1 = n1 + n2;
4848 else
4849 n1 = n1 - n2;
4850 rettv->v_type = VAR_NUMBER;
4851 rettv->vval.v_number = n1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 return OK;
4858}
4859
4860/*
4861 * Handle fifth level expression:
4862 * * number multiplication
4863 * / number division
4864 * % number modulo
4865 *
4866 * "arg" must point to the first non-white of the expression.
4867 * "arg" is advanced to the next non-white after the recognized expression.
4868 *
4869 * Return OK or FAIL.
4870 */
4871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872eval6(
4873 char_u **arg,
4874 typval_T *rettv,
4875 int evaluate,
4876 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 int op;
4880 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881#ifdef FEAT_FLOAT
4882 int use_float = FALSE;
4883 float_T f1 = 0, f2;
4884#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Get the first variable.
4889 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004890 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892
4893 /*
4894 * Repeat computing, until no '*', '/' or '%' is following.
4895 */
4896 for (;;)
4897 {
4898 op = **arg;
4899 if (op != '*' && op != '/' && op != '%')
4900 break;
4901
4902 if (evaluate)
4903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#ifdef FEAT_FLOAT
4905 if (rettv->v_type == VAR_FLOAT)
4906 {
4907 f1 = rettv->vval.v_float;
4908 use_float = TRUE;
4909 n1 = 0;
4910 }
4911 else
4912#endif
4913 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 if (error)
4916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
4919 n1 = 0;
4920
4921 /*
4922 * Get the second variable.
4923 */
4924 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004925 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return FAIL;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (var2.v_type == VAR_FLOAT)
4932 {
4933 if (!use_float)
4934 {
4935 f1 = n1;
4936 use_float = TRUE;
4937 }
4938 f2 = var2.vval.v_float;
4939 n2 = 0;
4940 }
4941 else
4942#endif
4943 {
4944 n2 = get_tv_number_chk(&var2, &error);
4945 clear_tv(&var2);
4946 if (error)
4947 return FAIL;
4948#ifdef FEAT_FLOAT
4949 if (use_float)
4950 f2 = n2;
4951#endif
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /*
4955 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958#ifdef FEAT_FLOAT
4959 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 if (op == '*')
4962 f1 = f1 * f2;
4963 else if (op == '/')
4964 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965# ifdef VMS
4966 /* VMS crashes on divide by zero, work around it */
4967 if (f2 == 0.0)
4968 {
4969 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004974 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975 }
4976 else
4977 f1 = f1 / f2;
4978# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 /* We rely on the floating point library to handle divide
4980 * by zero to result in "inf" and not a crash. */
4981 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004986 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 return FAIL;
4988 }
4989 rettv->v_type = VAR_FLOAT;
4990 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 if (op == '*')
4996 n1 = n1 * n2;
4997 else if (op == '/')
4998 {
4999 if (n2 == 0) /* give an error message? */
5000 {
5001 if (n1 == 0)
5002 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5003 else if (n1 < 0)
5004 n1 = -0x7fffffffL;
5005 else
5006 n1 = 0x7fffffffL;
5007 }
5008 else
5009 n1 = n1 / n2;
5010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013 if (n2 == 0) /* give an error message? */
5014 n1 = 0;
5015 else
5016 n1 = n1 % n2;
5017 }
5018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
5023
5024 return OK;
5025}
5026
5027/*
5028 * Handle sixth level expression:
5029 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005030 * "string" string constant
5031 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * &option-name option value
5033 * @r register contents
5034 * identifier variable value
5035 * function() function call
5036 * $VAR environment variable
5037 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005038 * [expr, expr] List
5039 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * Also handle:
5042 * ! in front logical NOT
5043 * - in front unary minus
5044 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * trailing [] subscript in String or List
5046 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 *
5048 * "arg" must point to the first non-white of the expression.
5049 * "arg" is advanced to the next non-white after the recognized expression.
5050 *
5051 * Return OK or FAIL.
5052 */
5053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005054eval7(
5055 char_u **arg,
5056 typval_T *rettv,
5057 int evaluate,
5058 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 long n;
5061 int len;
5062 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *start_leader, *end_leader;
5064 int ret = OK;
5065 char_u *alias;
5066
5067 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005069 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * Skip '!' and '-' characters. They are handled later.
5075 */
5076 start_leader = *arg;
5077 while (**arg == '!' || **arg == '-' || **arg == '+')
5078 *arg = skipwhite(*arg + 1);
5079 end_leader = *arg;
5080
5081 switch (**arg)
5082 {
5083 /*
5084 * Number constant.
5085 */
5086 case '0':
5087 case '1':
5088 case '2':
5089 case '3':
5090 case '4':
5091 case '5':
5092 case '6':
5093 case '7':
5094 case '8':
5095 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 {
5097#ifdef FEAT_FLOAT
5098 char_u *p = skipdigits(*arg + 1);
5099 int get_float = FALSE;
5100
5101 /* We accept a float when the format matches
5102 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005103 * strict to avoid backwards compatibility problems.
5104 * Don't look for a float after the "." operator, so that
5105 * ":let vers = 1.2.3" doesn't fail. */
5106 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 get_float = TRUE;
5109 p = skipdigits(p + 2);
5110 if (*p == 'e' || *p == 'E')
5111 {
5112 ++p;
5113 if (*p == '-' || *p == '+')
5114 ++p;
5115 if (!vim_isdigit(*p))
5116 get_float = FALSE;
5117 else
5118 p = skipdigits(p + 1);
5119 }
5120 if (ASCII_ISALPHA(*p) || *p == '.')
5121 get_float = FALSE;
5122 }
5123 if (get_float)
5124 {
5125 float_T f;
5126
5127 *arg += string2float(*arg, &f);
5128 if (evaluate)
5129 {
5130 rettv->v_type = VAR_FLOAT;
5131 rettv->vval.v_float = f;
5132 }
5133 }
5134 else
5135#endif
5136 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005137 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 *arg += len;
5139 if (evaluate)
5140 {
5141 rettv->v_type = VAR_NUMBER;
5142 rettv->vval.v_number = n;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * String constant: "string".
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 break;
5159
5160 /*
5161 * List: [expr, expr]
5162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * Dictionary: {key: val, key: val}
5168 */
5169 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5170 break;
5171
5172 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Environment variable: $VAR.
5180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Register contents: @r.
5186 */
5187 case '@': ++*arg;
5188 if (evaluate)
5189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005191 rettv->vval.v_string = get_reg_contents(**arg,
5192 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 if (**arg != NUL)
5195 ++*arg;
5196 break;
5197
5198 /*
5199 * nested expression: (expression).
5200 */
5201 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (**arg == ')')
5204 ++*arg;
5205 else if (ret == OK)
5206 {
5207 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ret = FAIL;
5210 }
5211 break;
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 if (ret == NOTDONE)
5218 {
5219 /*
5220 * Must be a variable or function name.
5221 * Can also be a curly-braces kind of name: {expr}.
5222 */
5223 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (alias != NULL)
5226 s = alias;
5227
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 ret = FAIL;
5230 else
5231 {
5232 if (**arg == '(') /* recursive! */
5233 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005234 partial_T *partial;
5235
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 /* If "s" is the name of a variable of type VAR_FUNC
5237 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005238 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239
5240 /* Invoke the function. */
5241 ret = get_func_tv(s, len, rettv, arg,
5242 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005243 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005244
5245 /* If evaluate is FALSE rettv->v_type was not set in
5246 * get_func_tv, but it's needed in handle_subscript() to parse
5247 * what follows. So set it here. */
5248 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5249 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005250 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005251 rettv->v_type = VAR_FUNC;
5252 }
5253
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 /* Stop the expression evaluation when immediately
5255 * aborting on error, or when an interrupt occurred or
5256 * an exception was thrown but not caught. */
5257 if (aborting())
5258 {
5259 if (ret == OK)
5260 clear_tv(rettv);
5261 ret = FAIL;
5262 }
5263 }
5264 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005265 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005266 else
5267 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005269 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 }
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 *arg = skipwhite(*arg);
5273
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5275 * expr(expr). */
5276 if (ret == OK)
5277 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
5279 /*
5280 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5281 */
5282 if (ret == OK && evaluate && end_leader > start_leader)
5283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005284 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005285 int val = 0;
5286#ifdef FEAT_FLOAT
5287 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005289 if (rettv->v_type == VAR_FLOAT)
5290 f = rettv->vval.v_float;
5291 else
5292#endif
5293 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 clear_tv(rettv);
5297 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 else
5300 {
5301 while (end_leader > start_leader)
5302 {
5303 --end_leader;
5304 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005305 {
5306#ifdef FEAT_FLOAT
5307 if (rettv->v_type == VAR_FLOAT)
5308 f = !f;
5309 else
5310#endif
5311 val = !val;
5312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005314 {
5315#ifdef FEAT_FLOAT
5316 if (rettv->v_type == VAR_FLOAT)
5317 f = -f;
5318 else
5319#endif
5320 val = -val;
5321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005323#ifdef FEAT_FLOAT
5324 if (rettv->v_type == VAR_FLOAT)
5325 {
5326 clear_tv(rettv);
5327 rettv->vval.v_float = f;
5328 }
5329 else
5330#endif
5331 {
5332 clear_tv(rettv);
5333 rettv->v_type = VAR_NUMBER;
5334 rettv->vval.v_number = val;
5335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338
5339 return ret;
5340}
5341
5342/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005343 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5344 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5346 */
5347 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005348eval_index(
5349 char_u **arg,
5350 typval_T *rettv,
5351 int evaluate,
5352 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353{
5354 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005355 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 long len = -1;
5358 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005364 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005365 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 if (verbose)
5367 EMSG(_("E695: Cannot index a Funcref"));
5368 return FAIL;
5369 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005370#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005371 if (verbose)
5372 EMSG(_(e_float_as_string));
5373 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005374#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005376 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005377 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005378 if (verbose)
5379 EMSG(_("E909: Cannot index a special variable"));
5380 return FAIL;
5381 case VAR_UNKNOWN:
5382 if (evaluate)
5383 return FAIL;
5384 /* FALLTHROUGH */
5385
5386 case VAR_STRING:
5387 case VAR_NUMBER:
5388 case VAR_LIST:
5389 case VAR_DICT:
5390 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005391 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005393 init_tv(&var1);
5394 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 /*
5398 * dict.name
5399 */
5400 key = *arg + 1;
5401 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5402 ;
5403 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 }
5407 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409 /*
5410 * something[idx]
5411 *
5412 * Get the (first) variable from inside the [].
5413 */
5414 *arg = skipwhite(*arg + 1);
5415 if (**arg == ':')
5416 empty1 = TRUE;
5417 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5418 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005419 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5420 {
5421 /* not a number or string */
5422 clear_tv(&var1);
5423 return FAIL;
5424 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005425
5426 /*
5427 * Get the second variable from inside the [:].
5428 */
5429 if (**arg == ':')
5430 {
5431 range = TRUE;
5432 *arg = skipwhite(*arg + 1);
5433 if (**arg == ']')
5434 empty2 = TRUE;
5435 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005437 if (!empty1)
5438 clear_tv(&var1);
5439 return FAIL;
5440 }
5441 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5442 {
5443 /* not a number or string */
5444 if (!empty1)
5445 clear_tv(&var1);
5446 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 return FAIL;
5448 }
5449 }
5450
5451 /* Check for the ']'. */
5452 if (**arg != ']')
5453 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005454 if (verbose)
5455 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 clear_tv(&var1);
5457 if (range)
5458 clear_tv(&var2);
5459 return FAIL;
5460 }
5461 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463
5464 if (evaluate)
5465 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 n1 = 0;
5467 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 n1 = get_tv_number(&var1);
5470 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 }
5472 if (range)
5473 {
5474 if (empty2)
5475 n2 = -1;
5476 else
5477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 n2 = get_tv_number(&var2);
5479 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 }
5481 }
5482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005486 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005487 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005488 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005489 case VAR_SPECIAL:
5490 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005491 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005492 break; /* not evaluating, skipping over subscript */
5493
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 case VAR_NUMBER:
5495 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005496 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497 len = (long)STRLEN(s);
5498 if (range)
5499 {
5500 /* The resulting variable is a substring. If the indexes
5501 * are out of range the result is empty. */
5502 if (n1 < 0)
5503 {
5504 n1 = len + n1;
5505 if (n1 < 0)
5506 n1 = 0;
5507 }
5508 if (n2 < 0)
5509 n2 = len + n2;
5510 else if (n2 >= len)
5511 n2 = len;
5512 if (n1 >= len || n2 < 0 || n1 > n2)
5513 s = NULL;
5514 else
5515 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5516 }
5517 else
5518 {
5519 /* The resulting variable is a string of a single
5520 * character. If the index is too big or negative the
5521 * result is empty. */
5522 if (n1 >= len || n1 < 0)
5523 s = NULL;
5524 else
5525 s = vim_strnsave(s + n1, 1);
5526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 clear_tv(rettv);
5528 rettv->v_type = VAR_STRING;
5529 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 break;
5531
5532 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 if (n1 < 0)
5535 n1 = len + n1;
5536 if (!empty1 && (n1 < 0 || n1 >= len))
5537 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005538 /* For a range we allow invalid values and return an empty
5539 * list. A list index out of range is an error. */
5540 if (!range)
5541 {
5542 if (verbose)
5543 EMSGN(_(e_listidx), n1);
5544 return FAIL;
5545 }
5546 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 }
5548 if (range)
5549 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005550 list_T *l;
5551 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552
5553 if (n2 < 0)
5554 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005555 else if (n2 >= len)
5556 n2 = len - 1;
5557 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005558 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 l = list_alloc();
5560 if (l == NULL)
5561 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 n1 <= n2; ++n1)
5564 {
5565 if (list_append_tv(l, &item->li_tv) == FAIL)
5566 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005567 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 return FAIL;
5569 }
5570 item = item->li_next;
5571 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 clear_tv(rettv);
5573 rettv->v_type = VAR_LIST;
5574 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005575 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005576 }
5577 else
5578 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005579 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 clear_tv(rettv);
5581 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582 }
5583 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584
5585 case VAR_DICT:
5586 if (range)
5587 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005588 if (verbose)
5589 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005590 if (len == -1)
5591 clear_tv(&var1);
5592 return FAIL;
5593 }
5594 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596
5597 if (len == -1)
5598 {
5599 key = get_tv_string(&var1);
5600 if (*key == NUL)
5601 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005602 if (verbose)
5603 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 clear_tv(&var1);
5605 return FAIL;
5606 }
5607 }
5608
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005609 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005611 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005612 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613 if (len == -1)
5614 clear_tv(&var1);
5615 if (item == NULL)
5616 return FAIL;
5617
5618 copy_tv(&item->di_tv, &var1);
5619 clear_tv(rettv);
5620 *rettv = var1;
5621 }
5622 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 }
5624 }
5625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 return OK;
5627}
5628
5629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 * Get an option value.
5631 * "arg" points to the '&' or '+' before the option name.
5632 * "arg" is advanced to character after the option name.
5633 * Return OK or FAIL.
5634 */
5635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005636get_option_tv(
5637 char_u **arg,
5638 typval_T *rettv, /* when NULL, only check if option exists */
5639 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 char_u *option_end;
5642 long numval;
5643 char_u *stringval;
5644 int opt_type;
5645 int c;
5646 int working = (**arg == '+'); /* has("+option") */
5647 int ret = OK;
5648 int opt_flags;
5649
5650 /*
5651 * Isolate the option name and find its value.
5652 */
5653 option_end = find_option_end(arg, &opt_flags);
5654 if (option_end == NULL)
5655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 EMSG2(_("E112: Option name missing: %s"), *arg);
5658 return FAIL;
5659 }
5660
5661 if (!evaluate)
5662 {
5663 *arg = option_end;
5664 return OK;
5665 }
5666
5667 c = *option_end;
5668 *option_end = NUL;
5669 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 if (opt_type == -3) /* invalid name */
5673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 EMSG2(_("E113: Unknown option: %s"), *arg);
5676 ret = FAIL;
5677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {
5680 if (opt_type == -2) /* hidden string option */
5681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005682 rettv->v_type = VAR_STRING;
5683 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 else if (opt_type == -1) /* hidden number option */
5686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 rettv->v_type = VAR_NUMBER;
5688 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 else if (opt_type == 1) /* number option */
5691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 rettv->v_type = VAR_NUMBER;
5693 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 else /* string option */
5696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 rettv->v_type = VAR_STRING;
5698 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 }
5701 else if (working && (opt_type == -2 || opt_type == -1))
5702 ret = FAIL;
5703
5704 *option_end = c; /* put back for error messages */
5705 *arg = option_end;
5706
5707 return ret;
5708}
5709
5710/*
5711 * Allocate a variable for a string constant.
5712 * Return OK or FAIL.
5713 */
5714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005715get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 char_u *p;
5718 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 int extra = 0;
5720
5721 /*
5722 * Find the end of the string, skipping backslashed characters.
5723 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 if (*p == '\\' && p[1] != NUL)
5727 {
5728 ++p;
5729 /* A "\<x>" form occupies at least 4 characters, and produces up
5730 * to 6 characters: reserve space for 2 extra */
5731 if (*p == '<')
5732 extra += 2;
5733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735
5736 if (*p != '"')
5737 {
5738 EMSG2(_("E114: Missing quote: %s"), *arg);
5739 return FAIL;
5740 }
5741
5742 /* If only parsing, set *arg and return here */
5743 if (!evaluate)
5744 {
5745 *arg = p + 1;
5746 return OK;
5747 }
5748
5749 /*
5750 * Copy the string into allocated memory, handling backslashed
5751 * characters.
5752 */
5753 name = alloc((unsigned)(p - *arg + extra));
5754 if (name == NULL)
5755 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 rettv->v_type = VAR_STRING;
5757 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
5761 if (*p == '\\')
5762 {
5763 switch (*++p)
5764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 case 'b': *name++ = BS; ++p; break;
5766 case 'e': *name++ = ESC; ++p; break;
5767 case 'f': *name++ = FF; ++p; break;
5768 case 'n': *name++ = NL; ++p; break;
5769 case 'r': *name++ = CAR; ++p; break;
5770 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772 case 'X': /* hex: "\x1", "\x12" */
5773 case 'x':
5774 case 'u': /* Unicode: "\u0023" */
5775 case 'U':
5776 if (vim_isxdigit(p[1]))
5777 {
5778 int n, nr;
5779 int c = toupper(*p);
5780
5781 if (c == 'X')
5782 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005783 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005785 else
5786 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 nr = 0;
5788 while (--n >= 0 && vim_isxdigit(p[1]))
5789 {
5790 ++p;
5791 nr = (nr << 4) + hex2nr(*p);
5792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794#ifdef FEAT_MBYTE
5795 /* For "\u" store the number according to
5796 * 'encoding'. */
5797 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 else
5800#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 break;
5804
5805 /* octal: "\1", "\12", "\123" */
5806 case '0':
5807 case '1':
5808 case '2':
5809 case '3':
5810 case '4':
5811 case '5':
5812 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case '7': *name = *p++ - '0';
5814 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 *name = (*name << 3) + *p++ - '0';
5817 if (*p >= '0' && *p <= '7')
5818 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 break;
5822
5823 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 if (extra != 0)
5826 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 break;
5829 }
5830 /* FALLTHROUGH */
5831
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 break;
5834 }
5835 }
5836 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 *arg = p + 1;
5842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 return OK;
5844}
5845
5846/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 * Return OK or FAIL.
5849 */
5850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005851get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
5853 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 int reduce = 0;
5856
5857 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 */
5860 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 break;
5866 ++reduce;
5867 ++p;
5868 }
5869 }
5870
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 return FAIL;
5875 }
5876
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 if (!evaluate)
5879 {
5880 *arg = p + 1;
5881 return OK;
5882 }
5883
5884 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 */
5887 str = alloc((unsigned)((p - *arg) - reduce));
5888 if (str == NULL)
5889 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 rettv->v_type = VAR_STRING;
5891 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 break;
5899 ++p;
5900 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 *arg = p + 1;
5905
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 return OK;
5907}
5908
5909/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 * Allocate a variable for a List and fill it from "*arg".
5911 * Return OK or FAIL.
5912 */
5913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005914get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915{
Bram Moolenaar33570922005-01-25 22:26:29 +00005916 list_T *l = NULL;
5917 typval_T tv;
5918 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919
5920 if (evaluate)
5921 {
5922 l = list_alloc();
5923 if (l == NULL)
5924 return FAIL;
5925 }
5926
5927 *arg = skipwhite(*arg + 1);
5928 while (**arg != ']' && **arg != NUL)
5929 {
5930 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5931 goto failret;
5932 if (evaluate)
5933 {
5934 item = listitem_alloc();
5935 if (item != NULL)
5936 {
5937 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005938 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 list_append(l, item);
5940 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005941 else
5942 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 }
5944
5945 if (**arg == ']')
5946 break;
5947 if (**arg != ',')
5948 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 goto failret;
5951 }
5952 *arg = skipwhite(*arg + 1);
5953 }
5954
5955 if (**arg != ']')
5956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005957 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958failret:
5959 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005960 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 return FAIL;
5962 }
5963
5964 *arg = skipwhite(*arg + 1);
5965 if (evaluate)
5966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005967 rettv->v_type = VAR_LIST;
5968 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 ++l->lv_refcount;
5970 }
5971
5972 return OK;
5973}
5974
5975/*
5976 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005977 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005979 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005980list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005982 list_T *l;
5983
5984 l = (list_T *)alloc_clear(sizeof(list_T));
5985 if (l != NULL)
5986 {
5987 /* Prepend the list to the list of lists for garbage collection. */
5988 if (first_list != NULL)
5989 first_list->lv_used_prev = l;
5990 l->lv_used_prev = NULL;
5991 l->lv_used_next = first_list;
5992 first_list = l;
5993 }
5994 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995}
5996
5997/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005998 * Allocate an empty list for a return value.
5999 * Returns OK or FAIL.
6000 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006001 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006002rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006003{
6004 list_T *l = list_alloc();
6005
6006 if (l == NULL)
6007 return FAIL;
6008
6009 rettv->vval.v_list = l;
6010 rettv->v_type = VAR_LIST;
6011 ++l->lv_refcount;
6012 return OK;
6013}
6014
6015/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 * Unreference a list: decrement the reference count and free it when it
6017 * becomes zero.
6018 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006020list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006022 if (l != NULL && --l->lv_refcount <= 0)
6023 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024}
6025
6026/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006027 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Ignores the reference count.
6029 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006030 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006031list_free(
6032 list_T *l,
6033 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 /* Remove the list from the list of lists for garbage collection. */
6038 if (l->lv_used_prev == NULL)
6039 first_list = l->lv_used_next;
6040 else
6041 l->lv_used_prev->lv_used_next = l->lv_used_next;
6042 if (l->lv_used_next != NULL)
6043 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6044
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006047 /* Remove the item before deleting it. */
6048 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006049 if (recurse || (item->li_tv.v_type != VAR_LIST
6050 && item->li_tv.v_type != VAR_DICT))
6051 clear_tv(&item->li_tv);
6052 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 }
6054 vim_free(l);
6055}
6056
6057/*
6058 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006059 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006061 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006062listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063{
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065}
6066
6067/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006068 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006071listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006073 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 vim_free(item);
6075}
6076
6077/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078 * Remove a list item from a List and free it. Also clears the value.
6079 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006080 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006081listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006082{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006083 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006084 listitem_free(item);
6085}
6086
6087/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 * Get the number of items in a list.
6089 */
6090 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 if (l == NULL)
6094 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006095 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096}
6097
6098/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 * Return TRUE when two lists have exactly the same values.
6100 */
6101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006102list_equal(
6103 list_T *l1,
6104 list_T *l2,
6105 int ic, /* ignore case for strings */
6106 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107{
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006110 if (l1 == NULL || l2 == NULL)
6111 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006112 if (l1 == l2)
6113 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006114 if (list_len(l1) != list_len(l2))
6115 return FALSE;
6116
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 for (item1 = l1->lv_first, item2 = l2->lv_first;
6118 item1 != NULL && item2 != NULL;
6119 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006120 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 return FALSE;
6122 return item1 == NULL && item2 == NULL;
6123}
6124
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006125/*
6126 * Return the dictitem that an entry in a hashtable points to.
6127 */
6128 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006130{
6131 return HI2DI(hi);
6132}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006133
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006134/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006135 * Return TRUE when two dictionaries have exactly the same key/values.
6136 */
6137 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138dict_equal(
6139 dict_T *d1,
6140 dict_T *d2,
6141 int ic, /* ignore case for strings */
6142 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143{
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 hashitem_T *hi;
6145 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006146 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006147
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006148 if (d1 == NULL || d2 == NULL)
6149 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006150 if (d1 == d2)
6151 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006152 if (dict_len(d1) != dict_len(d2))
6153 return FALSE;
6154
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006155 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006157 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006158 if (!HASHITEM_EMPTY(hi))
6159 {
6160 item2 = dict_find(d2, hi->hi_key, -1);
6161 if (item2 == NULL)
6162 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006163 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006164 return FALSE;
6165 --todo;
6166 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006167 }
6168 return TRUE;
6169}
6170
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171static int tv_equal_recurse_limit;
6172
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006173/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006175 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006176 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006177 */
6178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006179tv_equal(
6180 typval_T *tv1,
6181 typval_T *tv2,
6182 int ic, /* ignore case */
6183 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184{
6185 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006186 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006188 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006189
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006190 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6191 if ((tv1->v_type == VAR_FUNC
6192 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6193 && (tv2->v_type == VAR_FUNC
6194 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6195 {
6196 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6197 : tv1->vval.v_partial->pt_name;
6198 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6199 : tv2->vval.v_partial->pt_name;
6200 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6201 }
6202
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006203 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006204 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006206 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 * recursiveness to a limit. We guess they are equal then.
6208 * A fixed limit has the problem of still taking an awful long time.
6209 * Reduce the limit every time running into it. That should work fine for
6210 * deeply linked structures that are not recursively linked and catch
6211 * recursiveness quickly. */
6212 if (!recursive)
6213 tv_equal_recurse_limit = 1000;
6214 if (recursive_cnt >= tv_equal_recurse_limit)
6215 {
6216 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006217 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006218 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006219
6220 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006221 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006223 ++recursive_cnt;
6224 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6225 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006226 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006227
6228 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006229 ++recursive_cnt;
6230 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6231 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006232 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006233
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006234 case VAR_NUMBER:
6235 return tv1->vval.v_number == tv2->vval.v_number;
6236
6237 case VAR_STRING:
6238 s1 = get_tv_string_buf(tv1, buf1);
6239 s2 = get_tv_string_buf(tv2, buf2);
6240 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006241
6242 case VAR_SPECIAL:
6243 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006244
6245 case VAR_FLOAT:
6246#ifdef FEAT_FLOAT
6247 return tv1->vval.v_float == tv2->vval.v_float;
6248#endif
6249 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006250#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006251 return tv1->vval.v_job == tv2->vval.v_job;
6252#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006253 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006254#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006255 return tv1->vval.v_channel == tv2->vval.v_channel;
6256#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006257 case VAR_FUNC:
6258 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006259 case VAR_UNKNOWN:
6260 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006261 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006262
Bram Moolenaara03f2332016-02-06 18:09:59 +01006263 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6264 * does not equal anything, not even itself. */
6265 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006266}
6267
6268/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 * Locate item with index "n" in list "l" and return it.
6270 * A negative index is counted from the end; -1 is the last item.
6271 * Returns NULL when "n" is out of range.
6272 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006273 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006274list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275{
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 long idx;
6278
6279 if (l == NULL)
6280 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006281
6282 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284 n = l->lv_len + n;
6285
6286 /* Check for index out of range. */
6287 if (n < 0 || n >= l->lv_len)
6288 return NULL;
6289
6290 /* When there is a cached index may start search from there. */
6291 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 if (n < l->lv_idx / 2)
6294 {
6295 /* closest to the start of the list */
6296 item = l->lv_first;
6297 idx = 0;
6298 }
6299 else if (n > (l->lv_idx + l->lv_len) / 2)
6300 {
6301 /* closest to the end of the list */
6302 item = l->lv_last;
6303 idx = l->lv_len - 1;
6304 }
6305 else
6306 {
6307 /* closest to the cached index */
6308 item = l->lv_idx_item;
6309 idx = l->lv_idx;
6310 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 }
6312 else
6313 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006314 if (n < l->lv_len / 2)
6315 {
6316 /* closest to the start of the list */
6317 item = l->lv_first;
6318 idx = 0;
6319 }
6320 else
6321 {
6322 /* closest to the end of the list */
6323 item = l->lv_last;
6324 idx = l->lv_len - 1;
6325 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006327
6328 while (n > idx)
6329 {
6330 /* search forward */
6331 item = item->li_next;
6332 ++idx;
6333 }
6334 while (n < idx)
6335 {
6336 /* search backward */
6337 item = item->li_prev;
6338 --idx;
6339 }
6340
6341 /* cache the used index */
6342 l->lv_idx = idx;
6343 l->lv_idx_item = item;
6344
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 return item;
6346}
6347
6348/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006349 * Get list item "l[idx]" as a number.
6350 */
6351 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006352list_find_nr(
6353 list_T *l,
6354 long idx,
6355 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006356{
6357 listitem_T *li;
6358
6359 li = list_find(l, idx);
6360 if (li == NULL)
6361 {
6362 if (errorp != NULL)
6363 *errorp = TRUE;
6364 return -1L;
6365 }
6366 return get_tv_number_chk(&li->li_tv, errorp);
6367}
6368
6369/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006370 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6371 */
6372 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006373list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006374{
6375 listitem_T *li;
6376
6377 li = list_find(l, idx - 1);
6378 if (li == NULL)
6379 {
6380 EMSGN(_(e_listidx), idx);
6381 return NULL;
6382 }
6383 return get_tv_string(&li->li_tv);
6384}
6385
6386/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006387 * Locate "item" list "l" and return its index.
6388 * Returns -1 when "item" is not in the list.
6389 */
6390 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006391list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006392{
6393 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006395
6396 if (l == NULL)
6397 return -1;
6398 idx = 0;
6399 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6400 ++idx;
6401 if (li == NULL)
6402 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006403 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006404}
6405
6406/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 * Append item "item" to the end of list "l".
6408 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006409 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006410list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411{
6412 if (l->lv_last == NULL)
6413 {
6414 /* empty list */
6415 l->lv_first = item;
6416 l->lv_last = item;
6417 item->li_prev = NULL;
6418 }
6419 else
6420 {
6421 l->lv_last->li_next = item;
6422 item->li_prev = l->lv_last;
6423 l->lv_last = item;
6424 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 item->li_next = NULL;
6427}
6428
6429/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006436 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006437
Bram Moolenaar05159a02005-02-26 23:04:13 +00006438 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006440 copy_tv(tv, &li->li_tv);
6441 list_append(l, li);
6442 return OK;
6443}
6444
6445/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006446 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006447 * Return FAIL when out of memory.
6448 */
6449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006450list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006451{
6452 listitem_T *li = listitem_alloc();
6453
6454 if (li == NULL)
6455 return FAIL;
6456 li->li_tv.v_type = VAR_DICT;
6457 li->li_tv.v_lock = 0;
6458 li->li_tv.vval.v_dict = dict;
6459 list_append(list, li);
6460 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461 return OK;
6462}
6463
6464/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006465 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006466 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006467 * Returns FAIL when out of memory.
6468 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006470list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006471{
6472 listitem_T *li = listitem_alloc();
6473
6474 if (li == NULL)
6475 return FAIL;
6476 list_append(l, li);
6477 li->li_tv.v_type = VAR_STRING;
6478 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006479 if (str == NULL)
6480 li->li_tv.vval.v_string = NULL;
6481 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006482 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006483 return FAIL;
6484 return OK;
6485}
6486
6487/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006488 * Append "n" to list "l".
6489 * Returns FAIL when out of memory.
6490 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006491 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006492list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006493{
6494 listitem_T *li;
6495
6496 li = listitem_alloc();
6497 if (li == NULL)
6498 return FAIL;
6499 li->li_tv.v_type = VAR_NUMBER;
6500 li->li_tv.v_lock = 0;
6501 li->li_tv.vval.v_number = n;
6502 list_append(l, li);
6503 return OK;
6504}
6505
6506/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 * If "item" is NULL append at the end.
6509 * Return FAIL when out of memory.
6510 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006511 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006512list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513{
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515
6516 if (ni == NULL)
6517 return FAIL;
6518 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006519 list_insert(l, ni, item);
6520 return OK;
6521}
6522
6523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006524list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006525{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 if (item == NULL)
6527 /* Append new item at end of list. */
6528 list_append(l, ni);
6529 else
6530 {
6531 /* Insert new item before existing item. */
6532 ni->li_prev = item->li_prev;
6533 ni->li_next = item;
6534 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006535 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 ++l->lv_idx;
6538 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006540 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 l->lv_idx_item = NULL;
6543 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547}
6548
6549/*
6550 * Extend "l1" with "l2".
6551 * If "bef" is NULL append at the end, otherwise insert before this item.
6552 * Returns FAIL when out of memory.
6553 */
6554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556{
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006558 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006560 /* We also quit the loop when we have inserted the original item count of
6561 * the list, avoid a hang when we extend a list with itself. */
6562 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6564 return FAIL;
6565 return OK;
6566}
6567
6568/*
6569 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6570 * Return FAIL when out of memory.
6571 */
6572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574{
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006577 if (l1 == NULL || l2 == NULL)
6578 return FAIL;
6579
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006581 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582 if (l == NULL)
6583 return FAIL;
6584 tv->v_type = VAR_LIST;
6585 tv->vval.v_list = l;
6586
6587 /* append all items from the second list */
6588 return list_extend(l, l2, NULL);
6589}
6590
6591/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006592 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 * Returns NULL when out of memory.
6596 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006598list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599{
Bram Moolenaar33570922005-01-25 22:26:29 +00006600 list_T *copy;
6601 listitem_T *item;
6602 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603
6604 if (orig == NULL)
6605 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606
6607 copy = list_alloc();
6608 if (copy != NULL)
6609 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 if (copyID != 0)
6611 {
6612 /* Do this before adding the items, because one of the items may
6613 * refer back to this list. */
6614 orig->lv_copyID = copyID;
6615 orig->lv_copylist = copy;
6616 }
6617 for (item = orig->lv_first; item != NULL && !got_int;
6618 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 {
6620 ni = listitem_alloc();
6621 if (ni == NULL)
6622 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006623 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006624 {
6625 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6626 {
6627 vim_free(ni);
6628 break;
6629 }
6630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006632 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 list_append(copy, ni);
6634 }
6635 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006636 if (item != NULL)
6637 {
6638 list_unref(copy);
6639 copy = NULL;
6640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 }
6642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 return copy;
6644}
6645
6646/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006648 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006649 * This used to be called list_remove, but that conflicts with a Sun header
6650 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006652 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006653vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006654{
Bram Moolenaar33570922005-01-25 22:26:29 +00006655 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657 /* notify watchers */
6658 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006660 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661 list_fix_watch(l, ip);
6662 if (ip == item2)
6663 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665
6666 if (item2->li_next == NULL)
6667 l->lv_last = item->li_prev;
6668 else
6669 item2->li_next->li_prev = item->li_prev;
6670 if (item->li_prev == NULL)
6671 l->lv_first = item2->li_next;
6672 else
6673 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006674 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675}
6676
6677/*
6678 * Return an allocated string with the string representation of a list.
6679 * May return NULL.
6680 */
6681 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006682list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683{
6684 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685
6686 if (tv->vval.v_list == NULL)
6687 return NULL;
6688 ga_init2(&ga, (int)sizeof(char), 80);
6689 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006691 {
6692 vim_free(ga.ga_data);
6693 return NULL;
6694 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695 ga_append(&ga, ']');
6696 ga_append(&ga, NUL);
6697 return (char_u *)ga.ga_data;
6698}
6699
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700typedef struct join_S {
6701 char_u *s;
6702 char_u *tofree;
6703} join_T;
6704
6705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706list_join_inner(
6707 garray_T *gap, /* to store the result in */
6708 list_T *l,
6709 char_u *sep,
6710 int echo_style,
6711 int copyID,
6712 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006713{
6714 int i;
6715 join_T *p;
6716 int len;
6717 int sumlen = 0;
6718 int first = TRUE;
6719 char_u *tofree;
6720 char_u numbuf[NUMBUFLEN];
6721 listitem_T *item;
6722 char_u *s;
6723
6724 /* Stringify each item in the list. */
6725 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6726 {
6727 if (echo_style)
6728 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6729 else
6730 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6731 if (s == NULL)
6732 return FAIL;
6733
6734 len = (int)STRLEN(s);
6735 sumlen += len;
6736
Bram Moolenaarcde88542015-08-11 19:14:00 +02006737 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006738 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6739 if (tofree != NULL || s != numbuf)
6740 {
6741 p->s = s;
6742 p->tofree = tofree;
6743 }
6744 else
6745 {
6746 p->s = vim_strnsave(s, len);
6747 p->tofree = p->s;
6748 }
6749
6750 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006751 if (did_echo_string_emsg) /* recursion error, bail out */
6752 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006753 }
6754
6755 /* Allocate result buffer with its total size, avoid re-allocation and
6756 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6757 if (join_gap->ga_len >= 2)
6758 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6759 if (ga_grow(gap, sumlen + 2) == FAIL)
6760 return FAIL;
6761
6762 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6763 {
6764 if (first)
6765 first = FALSE;
6766 else
6767 ga_concat(gap, sep);
6768 p = ((join_T *)join_gap->ga_data) + i;
6769
6770 if (p->s != NULL)
6771 ga_concat(gap, p->s);
6772 line_breakcheck();
6773 }
6774
6775 return OK;
6776}
6777
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006778/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006780 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006781 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006784list_join(
6785 garray_T *gap,
6786 list_T *l,
6787 char_u *sep,
6788 int echo_style,
6789 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 garray_T join_ga;
6792 int retval;
6793 join_T *p;
6794 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795
Bram Moolenaard39a7512015-04-16 22:51:22 +02006796 if (l->lv_len < 1)
6797 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6799 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6800
6801 /* Dispose each item in join_ga. */
6802 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006804 p = (join_T *)join_ga.ga_data;
6805 for (i = 0; i < join_ga.ga_len; ++i)
6806 {
6807 vim_free(p->tofree);
6808 ++p;
6809 }
6810 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812
6813 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814}
6815
6816/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006817 * Return the next (unique) copy ID.
6818 * Used for serializing nested structures.
6819 */
6820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006822{
6823 current_copyID += COPYID_INC;
6824 return current_copyID;
6825}
6826
6827/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828 * Garbage collection for lists and dictionaries.
6829 *
6830 * We use reference counts to be able to free most items right away when they
6831 * are no longer used. But for composite items it's possible that it becomes
6832 * unused while the reference count is > 0: When there is a recursive
6833 * reference. Example:
6834 * :let l = [1, 2, 3]
6835 * :let d = {9: l}
6836 * :let l[1] = d
6837 *
6838 * Since this is quite unusual we handle this with garbage collection: every
6839 * once in a while find out which lists and dicts are not referenced from any
6840 * variable.
6841 *
6842 * Here is a good reference text about garbage collection (refers to Python
6843 * but it applies to all reference-counting mechanisms):
6844 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006846
6847/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848 * Do garbage collection for lists and dicts.
6849 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006852garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006853{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006855 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 buf_T *buf;
6857 win_T *wp;
6858 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006859 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006860 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006862#ifdef FEAT_WINDOWS
6863 tabpage_T *tp;
6864#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006866 /* Only do this once. */
6867 want_garbage_collect = FALSE;
6868 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006869 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006870
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 /* We advance by two because we add one for items referenced through
6872 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006873 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 /*
6876 * 1. Go through all accessible variables and mark all lists and dicts
6877 * with copyID.
6878 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006879
6880 /* Don't free variables in the previous_funccal list unless they are only
6881 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006882 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6884 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6886 NULL);
6887 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6888 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 }
6890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 /* script-local variables */
6892 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894
6895 /* buffer-local variables */
6896 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6898 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899
6900 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006901 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6903 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006904#ifdef FEAT_AUTOCMD
6905 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6907 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006908#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006910#ifdef FEAT_WINDOWS
6911 /* tabpage-local variables */
6912 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006915#endif
6916
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919
6920 /* function-local variables */
6921 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6922 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6924 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 }
6926
Bram Moolenaard812df62008-11-09 12:46:09 +00006927 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006929
Bram Moolenaar1dced572012-04-05 16:54:08 +02006930#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006932#endif
6933
Bram Moolenaardb913952012-06-29 12:54:53 +02006934#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006936#endif
6937
6938#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006940#endif
6941
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006942#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006943 abort = abort || set_ref_in_channel(copyID);
6944#endif
6945
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 /*
6949 * 2. Free lists and dictionaries that are not referenced.
6950 */
6951 did_free = free_unref_items(copyID);
6952
6953 /*
6954 * 3. Check if any funccal can be freed now.
6955 */
6956 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 if (can_free_funccal(*pfc, copyID))
6959 {
6960 fc = *pfc;
6961 *pfc = fc->caller;
6962 free_funccal(fc, TRUE);
6963 did_free = TRUE;
6964 did_free_funccal = TRUE;
6965 }
6966 else
6967 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006969 if (did_free_funccal)
6970 /* When a funccal was freed some more items might be garbage
6971 * collected, so run again. */
6972 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006974 else if (p_verbose > 0)
6975 {
6976 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6977 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978
6979 return did_free;
6980}
6981
6982/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006983 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 */
6985 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006988 dict_T *dd, *dd_next;
6989 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 int did_free = FALSE;
6991
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006993 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 */
6995 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 {
6997 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007000 /* Free the Dictionary and ordinary items it contains, but don't
7001 * recurse into Lists and Dictionaries, they will be in the list
7002 * of dicts or list of lists. */
7003 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007006 dd = dd_next;
7007 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008
7009 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007010 * Go through the list of lists and free items without the copyID.
7011 * But don't free a list that has a watcher (used in a for loop), these
7012 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 */
7014 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007015 {
7016 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007017 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7018 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007020 /* Free the List and ordinary items it contains, but don't recurse
7021 * into Lists and Dictionaries, they will be in the list of dicts
7022 * or list of lists. */
7023 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007026 ll = ll_next;
7027 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007028
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 return did_free;
7030}
7031
7032/*
7033 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 * "list_stack" is used to add lists to be marked. Can be NULL.
7035 *
7036 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007039set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040{
7041 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 hashtab_T *cur_ht;
7045 ht_stack_T *ht_stack = NULL;
7046 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 cur_ht = ht;
7049 for (;;)
7050 {
7051 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007052 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 /* Mark each item in the hashtab. If the item contains a hashtab
7054 * it is added to ht_stack, if it contains a list it is added to
7055 * list_stack. */
7056 todo = (int)cur_ht->ht_used;
7057 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7058 if (!HASHITEM_EMPTY(hi))
7059 {
7060 --todo;
7061 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7062 &ht_stack, list_stack);
7063 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065
7066 if (ht_stack == NULL)
7067 break;
7068
7069 /* take an item from the stack */
7070 cur_ht = ht_stack->ht;
7071 tempitem = ht_stack;
7072 ht_stack = ht_stack->prev;
7073 free(tempitem);
7074 }
7075
7076 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077}
7078
7079/*
7080 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7082 *
7083 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007086set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 listitem_T *li;
7089 int abort = FALSE;
7090 list_T *cur_l;
7091 list_stack_T *list_stack = NULL;
7092 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 cur_l = l;
7095 for (;;)
7096 {
7097 if (!abort)
7098 /* Mark each item in the list. If the item contains a hashtab
7099 * it is added to ht_stack, if it contains a list it is added to
7100 * list_stack. */
7101 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7102 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7103 ht_stack, &list_stack);
7104 if (list_stack == NULL)
7105 break;
7106
7107 /* take an item from the stack */
7108 cur_l = list_stack->list;
7109 tempitem = list_stack;
7110 list_stack = list_stack->prev;
7111 free(tempitem);
7112 }
7113
7114 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115}
7116
7117/*
7118 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 * "list_stack" is used to add lists to be marked. Can be NULL.
7120 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7121 *
7122 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007123 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007125set_ref_in_item(
7126 typval_T *tv,
7127 int copyID,
7128 ht_stack_T **ht_stack,
7129 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007130{
7131 dict_T *dd;
7132 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007133 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007134
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007135 if (tv->v_type == VAR_DICT || tv->v_type == VAR_PARTIAL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007136 {
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007137 if (tv->v_type == VAR_DICT)
7138 dd = tv->vval.v_dict;
7139 else if (tv->vval.v_partial != NULL)
7140 dd = tv->vval.v_partial->pt_dict;
7141 else
7142 dd = NULL;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007143 if (dd != NULL && dd->dv_copyID != copyID)
7144 {
7145 /* Didn't see this dict yet. */
7146 dd->dv_copyID = copyID;
7147 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007148 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007149 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7150 }
7151 else
7152 {
7153 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7154 if (newitem == NULL)
7155 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 else
7157 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007158 newitem->ht = &dd->dv_hashtab;
7159 newitem->prev = *ht_stack;
7160 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007163 }
7164 }
7165 else if (tv->v_type == VAR_LIST)
7166 {
7167 ll = tv->vval.v_list;
7168 if (ll != NULL && ll->lv_copyID != copyID)
7169 {
7170 /* Didn't see this list yet. */
7171 ll->lv_copyID = copyID;
7172 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007174 abort = set_ref_in_list(ll, copyID, ht_stack);
7175 }
7176 else
7177 {
7178 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007179 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007180 if (newitem == NULL)
7181 abort = TRUE;
7182 else
7183 {
7184 newitem->list = ll;
7185 newitem->prev = *list_stack;
7186 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007187 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007188 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007189 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007190 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007191 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192}
7193
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007194 static void
7195partial_free(partial_T *pt, int free_dict)
7196{
7197 int i;
7198
7199 for (i = 0; i < pt->pt_argc; ++i)
7200 clear_tv(&pt->pt_argv[i]);
7201 vim_free(pt->pt_argv);
7202 if (free_dict)
7203 dict_unref(pt->pt_dict);
7204 func_unref(pt->pt_name);
7205 vim_free(pt->pt_name);
7206 vim_free(pt);
7207}
7208
7209/*
7210 * Unreference a closure: decrement the reference count and free it when it
7211 * becomes zero.
7212 */
7213 void
7214partial_unref(partial_T *pt)
7215{
7216 if (pt != NULL && --pt->pt_refcount <= 0)
7217 partial_free(pt, TRUE);
7218}
7219
Bram Moolenaard9fba312005-06-26 22:34:35 +00007220/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 * Allocate an empty header for a dictionary.
7222 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007223 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007224dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225{
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007227
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007230 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007231 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007232 if (first_dict != NULL)
7233 first_dict->dv_used_prev = d;
7234 d->dv_used_next = first_dict;
7235 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007236 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007237
Bram Moolenaar33570922005-01-25 22:26:29 +00007238 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007239 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007240 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007241 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007242 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007243 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007244 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245}
7246
7247/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007248 * Allocate an empty dict for a return value.
7249 * Returns OK or FAIL.
7250 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007252rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007253{
7254 dict_T *d = dict_alloc();
7255
7256 if (d == NULL)
7257 return FAIL;
7258
7259 rettv->vval.v_dict = d;
7260 rettv->v_type = VAR_DICT;
7261 ++d->dv_refcount;
7262 return OK;
7263}
7264
7265
7266/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 * Unreference a Dictionary: decrement the reference count and free it when it
7268 * becomes zero.
7269 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007271dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007273 if (d != NULL && --d->dv_refcount <= 0)
7274 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275}
7276
7277/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007278 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 * Ignores the reference count.
7280 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282dict_free(
7283 dict_T *d,
7284 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007288 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007290 /* Remove the dict from the list of dicts for garbage collection. */
7291 if (d->dv_used_prev == NULL)
7292 first_dict = d->dv_used_next;
7293 else
7294 d->dv_used_prev->dv_used_next = d->dv_used_next;
7295 if (d->dv_used_next != NULL)
7296 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7297
7298 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007299 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 if (!HASHITEM_EMPTY(hi))
7304 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007305 /* Remove the item before deleting it, just in case there is
7306 * something recursive causing trouble. */
7307 di = HI2DI(hi);
7308 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007309 if (recurse || (di->di_tv.v_type != VAR_LIST
7310 && di->di_tv.v_type != VAR_DICT))
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007311 {
7312 if (!recurse && di->di_tv.v_type == VAR_PARTIAL)
7313 {
7314 partial_T *pt = di->di_tv.vval.v_partial;
7315
7316 /* We unref the partial but not the dict it refers to. */
7317 if (pt != NULL && --pt->pt_refcount == 0)
7318 partial_free(pt, FALSE);
7319 }
7320 else
7321 clear_tv(&di->di_tv);
7322 }
Bram Moolenaar685295c2006-10-15 20:37:38 +00007323 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 --todo;
7325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 vim_free(d);
7329}
7330
7331/*
7332 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 * The "key" is copied to the new item.
7334 * Note that the value of the item "di_tv" still needs to be initialized!
7335 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007337 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007338dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339{
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007342 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007346 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007347 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349}
7350
7351/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352 * Make a copy of a Dictionary item.
7353 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007355dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007356{
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007359 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7360 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 if (di != NULL)
7362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007364 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 copy_tv(&org->di_tv, &di->di_tv);
7366 }
7367 return di;
7368}
7369
7370/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 * Remove item "item" from Dictionary "dict" and free it.
7372 */
7373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007374dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375{
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007379 if (HASHITEM_EMPTY(hi))
7380 EMSG2(_(e_intern2), "dictitem_remove()");
7381 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007383 dictitem_free(item);
7384}
7385
7386/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 * Free a dict item. Also clears the value.
7388 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007390dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007393 if (item->di_flags & DI_FLAGS_ALLOC)
7394 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395}
7396
7397/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7399 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007400 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 * Returns NULL when out of memory.
7402 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007403 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007404dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405{
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 dict_T *copy;
7407 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410
7411 if (orig == NULL)
7412 return NULL;
7413
7414 copy = dict_alloc();
7415 if (copy != NULL)
7416 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 if (copyID != 0)
7418 {
7419 orig->dv_copyID = copyID;
7420 orig->dv_copydict = copy;
7421 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007422 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007423 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007424 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427 --todo;
7428
7429 di = dictitem_alloc(hi->hi_key);
7430 if (di == NULL)
7431 break;
7432 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 {
7434 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7435 copyID) == FAIL)
7436 {
7437 vim_free(di);
7438 break;
7439 }
7440 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 else
7442 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7443 if (dict_add(copy, di) == FAIL)
7444 {
7445 dictitem_free(di);
7446 break;
7447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007449 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450
Bram Moolenaare9a41262005-01-15 22:18:47 +00007451 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007452 if (todo > 0)
7453 {
7454 dict_unref(copy);
7455 copy = NULL;
7456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 }
7458
7459 return copy;
7460}
7461
7462/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007464 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007467dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468{
Bram Moolenaar33570922005-01-25 22:26:29 +00007469 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470}
7471
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007473 * Add a number or string entry to dictionary "d".
7474 * When "str" is NULL use number "nr", otherwise use "str".
7475 * Returns FAIL when out of memory and when key already exists.
7476 */
7477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007478dict_add_nr_str(
7479 dict_T *d,
7480 char *key,
7481 long nr,
7482 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007483{
7484 dictitem_T *item;
7485
7486 item = dictitem_alloc((char_u *)key);
7487 if (item == NULL)
7488 return FAIL;
7489 item->di_tv.v_lock = 0;
7490 if (str == NULL)
7491 {
7492 item->di_tv.v_type = VAR_NUMBER;
7493 item->di_tv.vval.v_number = nr;
7494 }
7495 else
7496 {
7497 item->di_tv.v_type = VAR_STRING;
7498 item->di_tv.vval.v_string = vim_strsave(str);
7499 }
7500 if (dict_add(d, item) == FAIL)
7501 {
7502 dictitem_free(item);
7503 return FAIL;
7504 }
7505 return OK;
7506}
7507
7508/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007509 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007510 * Returns FAIL when out of memory and when key already exists.
7511 */
7512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007513dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007514{
7515 dictitem_T *item;
7516
7517 item = dictitem_alloc((char_u *)key);
7518 if (item == NULL)
7519 return FAIL;
7520 item->di_tv.v_lock = 0;
7521 item->di_tv.v_type = VAR_LIST;
7522 item->di_tv.vval.v_list = list;
7523 if (dict_add(d, item) == FAIL)
7524 {
7525 dictitem_free(item);
7526 return FAIL;
7527 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007528 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007529 return OK;
7530}
7531
7532/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007533 * Get the number of items in a Dictionary.
7534 */
7535 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007537{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007538 if (d == NULL)
7539 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007540 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007541}
7542
7543/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 * Find item "key[len]" in Dictionary "d".
7545 * If "len" is negative use strlen(key).
7546 * Returns NULL when not found.
7547 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007548 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551#define AKEYLEN 200
7552 char_u buf[AKEYLEN];
7553 char_u *akey;
7554 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007555 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 if (len < 0)
7558 akey = key;
7559 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007560 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 tofree = akey = vim_strnsave(key, len);
7562 if (akey == NULL)
7563 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007564 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 else
7566 {
7567 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007568 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 akey = buf;
7570 }
7571
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007573 vim_free(tofree);
7574 if (HASHITEM_EMPTY(hi))
7575 return NULL;
7576 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577}
7578
7579/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007580 * Get a string item from a dictionary.
7581 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007582 * Returns NULL if the entry doesn't exist or out of memory.
7583 */
7584 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007585get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007586{
7587 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007588 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007589
7590 di = dict_find(d, key, -1);
7591 if (di == NULL)
7592 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007593 s = get_tv_string(&di->di_tv);
7594 if (save && s != NULL)
7595 s = vim_strsave(s);
7596 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007597}
7598
7599/*
7600 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007601 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007602 */
7603 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007604get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007605{
7606 dictitem_T *di;
7607
7608 di = dict_find(d, key, -1);
7609 if (di == NULL)
7610 return 0;
7611 return get_tv_number(&di->di_tv);
7612}
7613
7614/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 * Return an allocated string with the string representation of a Dictionary.
7616 * May return NULL.
7617 */
7618 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007619dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620{
7621 garray_T ga;
7622 int first = TRUE;
7623 char_u *tofree;
7624 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 return NULL;
7632 ga_init2(&ga, (int)sizeof(char), 80);
7633 ga_append(&ga, '{');
7634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007635 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007636 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 --todo;
7641
7642 if (first)
7643 first = FALSE;
7644 else
7645 ga_concat(&ga, (char_u *)", ");
7646
7647 tofree = string_quote(hi->hi_key, FALSE);
7648 if (tofree != NULL)
7649 {
7650 ga_concat(&ga, tofree);
7651 vim_free(tofree);
7652 }
7653 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007654 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (s != NULL)
7656 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007658 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007659 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007660 line_breakcheck();
7661
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664 if (todo > 0)
7665 {
7666 vim_free(ga.ga_data);
7667 return NULL;
7668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669
7670 ga_append(&ga, '}');
7671 ga_append(&ga, NUL);
7672 return (char_u *)ga.ga_data;
7673}
7674
7675/*
7676 * Allocate a variable for a Dictionary and fill it from "*arg".
7677 * Return OK or FAIL. Returns NOTDONE for {expr}.
7678 */
7679 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007680get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681{
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 dict_T *d = NULL;
7683 typval_T tvkey;
7684 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007685 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689
7690 /*
7691 * First check if it's not a curly-braces thing: {expr}.
7692 * Must do this without evaluating, otherwise a function may be called
7693 * twice. Unfortunately this means we need to call eval1() twice for the
7694 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007695 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697 if (*start != '}')
7698 {
7699 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7700 return FAIL;
7701 if (*start == '}')
7702 return NOTDONE;
7703 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704
7705 if (evaluate)
7706 {
7707 d = dict_alloc();
7708 if (d == NULL)
7709 return FAIL;
7710 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007711 tvkey.v_type = VAR_UNKNOWN;
7712 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713
7714 *arg = skipwhite(*arg + 1);
7715 while (**arg != '}' && **arg != NUL)
7716 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007717 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 goto failret;
7719 if (**arg != ':')
7720 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007721 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007722 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 goto failret;
7724 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007725 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007727 key = get_tv_string_buf_chk(&tvkey, buf);
7728 if (key == NULL || *key == NUL)
7729 {
7730 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7731 if (key != NULL)
7732 EMSG(_(e_emptykey));
7733 clear_tv(&tvkey);
7734 goto failret;
7735 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737
7738 *arg = skipwhite(*arg + 1);
7739 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7740 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007741 if (evaluate)
7742 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 goto failret;
7744 }
7745 if (evaluate)
7746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007748 if (item != NULL)
7749 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007750 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 clear_tv(&tv);
7753 goto failret;
7754 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007755 item = dictitem_alloc(key);
7756 clear_tv(&tvkey);
7757 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007760 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007761 if (dict_add(d, item) == FAIL)
7762 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763 }
7764 }
7765
7766 if (**arg == '}')
7767 break;
7768 if (**arg != ',')
7769 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007770 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771 goto failret;
7772 }
7773 *arg = skipwhite(*arg + 1);
7774 }
7775
7776 if (**arg != '}')
7777 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007778 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779failret:
7780 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007781 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782 return FAIL;
7783 }
7784
7785 *arg = skipwhite(*arg + 1);
7786 if (evaluate)
7787 {
7788 rettv->v_type = VAR_DICT;
7789 rettv->vval.v_dict = d;
7790 ++d->dv_refcount;
7791 }
7792
7793 return OK;
7794}
7795
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007796#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007797#endif
7798
Bram Moolenaar17a13432016-01-24 14:22:10 +01007799 static char *
7800get_var_special_name(int nr)
7801{
7802 switch (nr)
7803 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007804 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007805 case VVAL_TRUE: return "v:true";
7806 case VVAL_NONE: return "v:none";
7807 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007808 }
7809 EMSG2(_(e_intern2), "get_var_special_name()");
7810 return "42";
7811}
7812
Bram Moolenaar8c711452005-01-14 21:53:12 +00007813/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814 * Return a string with the string representation of a variable.
7815 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007816 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007817 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007818 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007819 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 */
7821 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007822echo_string(
7823 typval_T *tv,
7824 char_u **tofree,
7825 char_u *numbuf,
7826 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 static int recurse = 0;
7829 char_u *r = NULL;
7830
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007833 if (!did_echo_string_emsg)
7834 {
7835 /* Only give this message once for a recursive call to avoid
7836 * flooding the user with errors. And stop iterating over lists
7837 * and dicts. */
7838 did_echo_string_emsg = TRUE;
7839 EMSG(_("E724: variable nested too deep for displaying"));
7840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007842 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843 }
7844 ++recurse;
7845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007846 switch (tv->v_type)
7847 {
7848 case VAR_FUNC:
7849 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 r = tv->vval.v_string;
7851 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007852
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007853 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007854 {
7855 partial_T *pt = tv->vval.v_partial;
7856 char_u *fname = string_quote(pt == NULL ? NULL
7857 : pt->pt_name, FALSE);
7858 garray_T ga;
7859 int i;
7860 char_u *tf;
7861
7862 ga_init2(&ga, 1, 100);
7863 ga_concat(&ga, (char_u *)"function(");
7864 if (fname != NULL)
7865 {
7866 ga_concat(&ga, fname);
7867 vim_free(fname);
7868 }
7869 if (pt != NULL && pt->pt_argc > 0)
7870 {
7871 ga_concat(&ga, (char_u *)", [");
7872 for (i = 0; i < pt->pt_argc; ++i)
7873 {
7874 if (i > 0)
7875 ga_concat(&ga, (char_u *)", ");
7876 ga_concat(&ga,
7877 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7878 vim_free(tf);
7879 }
7880 ga_concat(&ga, (char_u *)"]");
7881 }
7882 if (pt != NULL && pt->pt_dict != NULL)
7883 {
7884 typval_T dtv;
7885
7886 ga_concat(&ga, (char_u *)", ");
7887 dtv.v_type = VAR_DICT;
7888 dtv.vval.v_dict = pt->pt_dict;
7889 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7890 vim_free(tf);
7891 }
7892 ga_concat(&ga, (char_u *)")");
7893
7894 *tofree = ga.ga_data;
7895 r = *tofree;
7896 break;
7897 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007898
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007899 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007900 if (tv->vval.v_list == NULL)
7901 {
7902 *tofree = NULL;
7903 r = NULL;
7904 }
7905 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7906 {
7907 *tofree = NULL;
7908 r = (char_u *)"[...]";
7909 }
7910 else
7911 {
7912 tv->vval.v_list->lv_copyID = copyID;
7913 *tofree = list2string(tv, copyID);
7914 r = *tofree;
7915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007917
Bram Moolenaar8c711452005-01-14 21:53:12 +00007918 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007919 if (tv->vval.v_dict == NULL)
7920 {
7921 *tofree = NULL;
7922 r = NULL;
7923 }
7924 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7925 {
7926 *tofree = NULL;
7927 r = (char_u *)"{...}";
7928 }
7929 else
7930 {
7931 tv->vval.v_dict->dv_copyID = copyID;
7932 *tofree = dict2string(tv, copyID);
7933 r = *tofree;
7934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007935 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007936
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937 case VAR_STRING:
7938 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007939 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007940 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007941 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007942 *tofree = NULL;
7943 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007945
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007946 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007947#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007948 *tofree = NULL;
7949 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7950 r = numbuf;
7951 break;
7952#endif
7953
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007954 case VAR_SPECIAL:
7955 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007956 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007957 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007958 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007959
Bram Moolenaar8502c702014-06-17 12:51:16 +02007960 if (--recurse == 0)
7961 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007962 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007963}
7964
7965/*
7966 * Return a string with the string representation of a variable.
7967 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7968 * "numbuf" is used for a number.
7969 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007970 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007971 */
7972 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007973tv2string(
7974 typval_T *tv,
7975 char_u **tofree,
7976 char_u *numbuf,
7977 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978{
7979 switch (tv->v_type)
7980 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007981 case VAR_FUNC:
7982 *tofree = string_quote(tv->vval.v_string, TRUE);
7983 return *tofree;
7984 case VAR_STRING:
7985 *tofree = string_quote(tv->vval.v_string, FALSE);
7986 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007987 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007988#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007989 *tofree = NULL;
7990 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7991 return numbuf;
7992#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007993 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007994 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007995 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007996 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007997 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007998 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007999 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008000 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008001 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008002 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008003 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008004}
8005
8006/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 * Return string "str" in ' quotes, doubling ' characters.
8008 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008009 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008010 */
8011 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008012string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008013{
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008015 char_u *p, *r, *s;
8016
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 len = (function ? 13 : 3);
8018 if (str != NULL)
8019 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008020 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008021 for (p = str; *p != NUL; mb_ptr_adv(p))
8022 if (*p == '\'')
8023 ++len;
8024 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008025 s = r = alloc(len);
8026 if (r != NULL)
8027 {
8028 if (function)
8029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008030 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008031 r += 10;
8032 }
8033 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008034 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008035 if (str != NULL)
8036 for (p = str; *p != NUL; )
8037 {
8038 if (*p == '\'')
8039 *r++ = '\'';
8040 MB_COPY_CHAR(p, r);
8041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008042 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008043 if (function)
8044 *r++ = ')';
8045 *r++ = NUL;
8046 }
8047 return s;
8048}
8049
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008050#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008051/*
8052 * Convert the string "text" to a floating point number.
8053 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8054 * this always uses a decimal point.
8055 * Returns the length of the text that was consumed.
8056 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008058string2float(
8059 char_u *text,
8060 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008061{
8062 char *s = (char *)text;
8063 float_T f;
8064
8065 f = strtod(s, &s);
8066 *value = f;
8067 return (int)((char_u *)s - text);
8068}
8069#endif
8070
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 * Get the value of an environment variable.
8073 * "arg" is pointing to the '$'. It is advanced to after the name.
8074 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008075 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 */
8077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008078get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
8080 char_u *string = NULL;
8081 int len;
8082 int cc;
8083 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008084 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085
8086 ++*arg;
8087 name = *arg;
8088 len = get_env_len(arg);
8089 if (evaluate)
8090 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008091 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008092 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008093
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008094 cc = name[len];
8095 name[len] = NUL;
8096 /* first try vim_getenv(), fast for normal environment vars */
8097 string = vim_getenv(name, &mustfree);
8098 if (string != NULL && *string != NUL)
8099 {
8100 if (!mustfree)
8101 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008103 else
8104 {
8105 if (mustfree)
8106 vim_free(string);
8107
8108 /* next try expanding things like $VIM and ${HOME} */
8109 string = expand_env_save(name - 1);
8110 if (string != NULL && *string == '$')
8111 {
8112 vim_free(string);
8113 string = NULL;
8114 }
8115 }
8116 name[len] = cc;
8117
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 rettv->v_type = VAR_STRING;
8119 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 }
8121
8122 return OK;
8123}
8124
8125/*
8126 * Array with names and number of arguments of all internal functions
8127 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8128 */
8129static struct fst
8130{
8131 char *f_name; /* function name */
8132 char f_min_argc; /* minimal number of arguments */
8133 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008134 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008135 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136} functions[] =
8137{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008138#ifdef FEAT_FLOAT
8139 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008140 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008142 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008143 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008144 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"append", 2, 2, f_append},
8146 {"argc", 0, 0, f_argc},
8147 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008148 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008149 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008150#ifdef FEAT_FLOAT
8151 {"asin", 1, 1, f_asin}, /* WJMc */
8152#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008153 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008154 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008155 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008156 {"assert_false", 1, 2, f_assert_false},
8157 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008158#ifdef FEAT_FLOAT
8159 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008160 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008163 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"bufexists", 1, 1, f_bufexists},
8165 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8166 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8167 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8168 {"buflisted", 1, 1, f_buflisted},
8169 {"bufloaded", 1, 1, f_bufloaded},
8170 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008171 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"bufwinnr", 1, 1, f_bufwinnr},
8173 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008174 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008175 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008176 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008177#ifdef FEAT_FLOAT
8178 {"ceil", 1, 1, f_ceil},
8179#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008180#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008181 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008182 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8183 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008184 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008185 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008186 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008187 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008188 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008189 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008190 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008191 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008192 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8193 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008194 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008195 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008196#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008197 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008198 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008200 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008202#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008203 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008204 {"complete_add", 1, 1, f_complete_add},
8205 {"complete_check", 0, 0, f_complete_check},
8206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008208 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008209#ifdef FEAT_FLOAT
8210 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008211 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008212#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008213 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008215 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008216 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008217 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008219 {"diff_filler", 1, 1, f_diff_filler},
8220 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008221 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008222 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008224 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"eventhandler", 0, 0, f_eventhandler},
8226 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008227 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008229#ifdef FEAT_FLOAT
8230 {"exp", 1, 1, f_exp},
8231#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008232 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008233 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008234 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8236 {"filereadable", 1, 1, f_filereadable},
8237 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008238 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008239 {"finddir", 1, 3, f_finddir},
8240 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008241#ifdef FEAT_FLOAT
8242 {"float2nr", 1, 1, f_float2nr},
8243 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008244 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008245#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008246 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"fnamemodify", 2, 2, f_fnamemodify},
8248 {"foldclosed", 1, 1, f_foldclosed},
8249 {"foldclosedend", 1, 1, f_foldclosedend},
8250 {"foldlevel", 1, 1, f_foldlevel},
8251 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008252 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008254 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008255 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008256 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008257 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008258 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {"getchar", 0, 1, f_getchar},
8260 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008261 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {"getcmdline", 0, 0, f_getcmdline},
8263 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008264 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008265 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008266 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008267 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008268 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008269 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"getfsize", 1, 1, f_getfsize},
8271 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008272 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008273 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008274 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008275 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008276 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008277 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008278 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008279 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008281 {"gettabvar", 2, 3, f_gettabvar},
8282 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"getwinposx", 0, 0, f_getwinposx},
8284 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008285 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008286 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008287 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008288 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008290 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008291 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008292 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8294 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8295 {"histadd", 2, 2, f_histadd},
8296 {"histdel", 1, 2, f_histdel},
8297 {"histget", 1, 2, f_histget},
8298 {"histnr", 1, 1, f_histnr},
8299 {"hlID", 1, 1, f_hlID},
8300 {"hlexists", 1, 1, f_hlexists},
8301 {"hostname", 0, 0, f_hostname},
8302 {"iconv", 3, 3, f_iconv},
8303 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008304 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008305 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008307 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {"inputrestore", 0, 0, f_inputrestore},
8309 {"inputsave", 0, 0, f_inputsave},
8310 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008312 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008314 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008315#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8316 {"isnan", 1, 1, f_isnan},
8317#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008318 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008319#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008320 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008321 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008322 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008323 {"job_start", 1, 2, f_job_start},
8324 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008325 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008326#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008327 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008328 {"js_decode", 1, 1, f_js_decode},
8329 {"js_encode", 1, 1, f_js_encode},
8330 {"json_decode", 1, 1, f_json_decode},
8331 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008332 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008334 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"libcall", 3, 3, f_libcall},
8336 {"libcallnr", 3, 3, f_libcallnr},
8337 {"line", 1, 1, f_line},
8338 {"line2byte", 1, 1, f_line2byte},
8339 {"lispindent", 1, 1, f_lispindent},
8340 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008341#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008342 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008343 {"log10", 1, 1, f_log10},
8344#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008345#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008346 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008347#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008348 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008349 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008350 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008351 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008352 {"matchadd", 2, 5, f_matchadd},
8353 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008354 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008355 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008356 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008357 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008358 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008359 {"max", 1, 1, f_max},
8360 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008361#ifdef vim_mkdir
8362 {"mkdir", 1, 3, f_mkdir},
8363#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008364 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008365#ifdef FEAT_MZSCHEME
8366 {"mzeval", 1, 1, f_mzeval},
8367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008369 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008370 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008371 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008372#ifdef FEAT_PERL
8373 {"perleval", 1, 1, f_perleval},
8374#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008375#ifdef FEAT_FLOAT
8376 {"pow", 2, 2, f_pow},
8377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008379 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008380 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008381#ifdef FEAT_PYTHON3
8382 {"py3eval", 1, 1, f_py3eval},
8383#endif
8384#ifdef FEAT_PYTHON
8385 {"pyeval", 1, 1, f_pyeval},
8386#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008387 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008388 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008389 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008390#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008391 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008392#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008393 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"remote_expr", 2, 3, f_remote_expr},
8395 {"remote_foreground", 1, 1, f_remote_foreground},
8396 {"remote_peek", 1, 2, f_remote_peek},
8397 {"remote_read", 1, 1, f_remote_read},
8398 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008399 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008401 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008403 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404#ifdef FEAT_FLOAT
8405 {"round", 1, 1, f_round},
8406#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008407 {"screenattr", 2, 2, f_screenattr},
8408 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008409 {"screencol", 0, 0, f_screencol},
8410 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008411 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008412 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008413 {"searchpair", 3, 7, f_searchpair},
8414 {"searchpairpos", 3, 7, f_searchpairpos},
8415 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"server2client", 2, 2, f_server2client},
8417 {"serverlist", 0, 0, f_serverlist},
8418 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008419 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008421 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008423 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008424 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008425 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008426 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008428 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008429 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008431#ifdef FEAT_CRYPT
8432 {"sha256", 1, 1, f_sha256},
8433#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008434 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008435 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008437#ifdef FEAT_FLOAT
8438 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008439 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008440#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008441 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008442 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008443 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008444 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008445 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008446#ifdef FEAT_FLOAT
8447 {"sqrt", 1, 1, f_sqrt},
8448 {"str2float", 1, 1, f_str2float},
8449#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008450 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008451 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008452 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453#ifdef HAVE_STRFTIME
8454 {"strftime", 1, 2, f_strftime},
8455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008456 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008457 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {"strlen", 1, 1, f_strlen},
8459 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008460 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008462 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008463 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {"substitute", 4, 4, f_substitute},
8465 {"synID", 3, 3, f_synID},
8466 {"synIDattr", 2, 3, f_synIDattr},
8467 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008468 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008469 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008470 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008471 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008472 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008473 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008474 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008475 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008476 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008477#ifdef FEAT_FLOAT
8478 {"tan", 1, 1, f_tan},
8479 {"tanh", 1, 1, f_tanh},
8480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008482 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008483#ifdef FEAT_TIMERS
8484 {"timer_start", 2, 3, f_timer_start},
8485 {"timer_stop", 1, 1, f_timer_stop},
8486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {"tolower", 1, 1, f_tolower},
8488 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008489 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008490#ifdef FEAT_FLOAT
8491 {"trunc", 1, 1, f_trunc},
8492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008494 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008495 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008496 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008497 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"virtcol", 1, 1, f_virtcol},
8499 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008500 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008501 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008502 {"win_getid", 0, 2, f_win_getid},
8503 {"win_gotoid", 1, 1, f_win_gotoid},
8504 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8505 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"winbufnr", 1, 1, f_winbufnr},
8507 {"wincol", 0, 0, f_wincol},
8508 {"winheight", 1, 1, f_winheight},
8509 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008510 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008512 {"winrestview", 1, 1, f_winrestview},
8513 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008515 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008516 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008517 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518};
8519
8520#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8521
8522/*
8523 * Function given to ExpandGeneric() to obtain the list of internal
8524 * or user defined function names.
8525 */
8526 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008527get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 static int intidx = -1;
8530 char_u *name;
8531
8532 if (idx == 0)
8533 intidx = -1;
8534 if (intidx < 0)
8535 {
8536 name = get_user_func_name(xp, idx);
8537 if (name != NULL)
8538 return name;
8539 }
8540 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8541 {
8542 STRCPY(IObuff, functions[intidx].f_name);
8543 STRCAT(IObuff, "(");
8544 if (functions[intidx].f_max_argc == 0)
8545 STRCAT(IObuff, ")");
8546 return IObuff;
8547 }
8548
8549 return NULL;
8550}
8551
8552/*
8553 * Function given to ExpandGeneric() to obtain the list of internal or
8554 * user defined variable or function names.
8555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008557get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
8559 static int intidx = -1;
8560 char_u *name;
8561
8562 if (idx == 0)
8563 intidx = -1;
8564 if (intidx < 0)
8565 {
8566 name = get_function_name(xp, idx);
8567 if (name != NULL)
8568 return name;
8569 }
8570 return get_user_var_name(xp, ++intidx);
8571}
8572
8573#endif /* FEAT_CMDL_COMPL */
8574
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008575#if defined(EBCDIC) || defined(PROTO)
8576/*
8577 * Compare struct fst by function name.
8578 */
8579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008580compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008581{
8582 struct fst *p1 = (struct fst *)s1;
8583 struct fst *p2 = (struct fst *)s2;
8584
8585 return STRCMP(p1->f_name, p2->f_name);
8586}
8587
8588/*
8589 * Sort the function table by function name.
8590 * The sorting of the table above is ASCII dependant.
8591 * On machines using EBCDIC we have to sort it.
8592 */
8593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008594sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008595{
8596 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8597
8598 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8599}
8600#endif
8601
8602
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603/*
8604 * Find internal function in table above.
8605 * Return index, or -1 if not found
8606 */
8607 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008608find_internal_func(
8609 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
8611 int first = 0;
8612 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8613 int cmp;
8614 int x;
8615
8616 /*
8617 * Find the function name in the table. Binary search.
8618 */
8619 while (first <= last)
8620 {
8621 x = first + ((unsigned)(last - first) >> 1);
8622 cmp = STRCMP(name, functions[x].f_name);
8623 if (cmp < 0)
8624 last = x - 1;
8625 else if (cmp > 0)
8626 first = x + 1;
8627 else
8628 return x;
8629 }
8630 return -1;
8631}
8632
8633/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008634 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8635 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008636 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8637 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008638 */
8639 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008640deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008641{
Bram Moolenaar33570922005-01-25 22:26:29 +00008642 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008643 int cc;
8644
Bram Moolenaar65639032016-03-16 21:40:30 +01008645 if (partialp != NULL)
8646 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008647
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008648 cc = name[*lenp];
8649 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008650 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008651 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008652 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008653 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008654 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008655 {
8656 *lenp = 0;
8657 return (char_u *)""; /* just in case */
8658 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008659 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 }
8662
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008663 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8664 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008665 partial_T *pt = v->di_tv.vval.v_partial;
8666
8667 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008668 {
8669 *lenp = 0;
8670 return (char_u *)""; /* just in case */
8671 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008672 if (partialp != NULL)
8673 *partialp = pt;
8674 *lenp = (int)STRLEN(pt->pt_name);
8675 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008676 }
8677
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008678 return name;
8679}
8680
8681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 * Allocate a variable for the result of a function.
8683 * Return OK or FAIL.
8684 */
8685 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008686get_func_tv(
8687 char_u *name, /* name of the function */
8688 int len, /* length of "name" */
8689 typval_T *rettv,
8690 char_u **arg, /* argument, pointing to the '(' */
8691 linenr_T firstline, /* first line of range */
8692 linenr_T lastline, /* last line of range */
8693 int *doesrange, /* return: function handled range */
8694 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008695 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008696 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
8698 char_u *argp;
8699 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008700 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 int argcount = 0; /* number of arguments found */
8702
8703 /*
8704 * Get the arguments.
8705 */
8706 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008707 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
8709 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8710 if (*argp == ')' || *argp == ',' || *argp == NUL)
8711 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8713 {
8714 ret = FAIL;
8715 break;
8716 }
8717 ++argcount;
8718 if (*argp != ',')
8719 break;
8720 }
8721 if (*argp == ')')
8722 ++argp;
8723 else
8724 ret = FAIL;
8725
8726 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008728 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 {
8731 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008732 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008734 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
8737 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739
8740 *arg = skipwhite(argp);
8741 return ret;
8742}
8743
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008744#define ERROR_UNKNOWN 0
8745#define ERROR_TOOMANY 1
8746#define ERROR_TOOFEW 2
8747#define ERROR_SCRIPT 3
8748#define ERROR_DICT 4
8749#define ERROR_NONE 5
8750#define ERROR_OTHER 6
8751#define FLEN_FIXED 40
8752
8753/*
8754 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8755 * Change <SNR>123_name() to K_SNR 123_name().
8756 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8757 * (slow).
8758 */
8759 static char_u *
8760fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8761{
8762 int llen;
8763 char_u *fname;
8764 int i;
8765
8766 llen = eval_fname_script(name);
8767 if (llen > 0)
8768 {
8769 fname_buf[0] = K_SPECIAL;
8770 fname_buf[1] = KS_EXTRA;
8771 fname_buf[2] = (int)KE_SNR;
8772 i = 3;
8773 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8774 {
8775 if (current_SID <= 0)
8776 *error = ERROR_SCRIPT;
8777 else
8778 {
8779 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8780 i = (int)STRLEN(fname_buf);
8781 }
8782 }
8783 if (i + STRLEN(name + llen) < FLEN_FIXED)
8784 {
8785 STRCPY(fname_buf + i, name + llen);
8786 fname = fname_buf;
8787 }
8788 else
8789 {
8790 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8791 if (fname == NULL)
8792 *error = ERROR_OTHER;
8793 else
8794 {
8795 *tofree = fname;
8796 mch_memmove(fname, fname_buf, (size_t)i);
8797 STRCPY(fname + i, name + llen);
8798 }
8799 }
8800 }
8801 else
8802 fname = name;
8803 return fname;
8804}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
8806/*
8807 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008808 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008809 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008811 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008812call_func(
8813 char_u *funcname, /* name of the function */
8814 int len, /* length of "name" */
8815 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008816 int argcount_in, /* number of "argvars" */
8817 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008818 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008819 linenr_T firstline, /* first line of range */
8820 linenr_T lastline, /* last line of range */
8821 int *doesrange, /* return: function handled range */
8822 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008823 partial_T *partial, /* optional, can be NULL */
8824 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825{
8826 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 int error = ERROR_NONE;
8828 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008831 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008833 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008834 int argcount = argcount_in;
8835 typval_T *argvars = argvars_in;
8836 dict_T *selfdict = selfdict_in;
8837 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8838 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008839
8840 /* Make a copy of the name, if it comes from a funcref variable it could
8841 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008842 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008843 if (name == NULL)
8844 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008846 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
8848 *doesrange = FALSE;
8849
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008850 if (partial != NULL)
8851 {
8852 if (partial->pt_dict != NULL)
8853 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008854 /* When the function has a partial with a dict and there is a dict
8855 * argument, use the dict argument. That is backwards compatible.
8856 */
8857 if (selfdict_in == NULL)
8858 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008859 }
8860 if (error == ERROR_NONE && partial->pt_argc > 0)
8861 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008862 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8863 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8864 for (i = 0; i < argcount_in; ++i)
8865 argv[i + argv_clear] = argvars_in[i];
8866 argvars = argv;
8867 argcount = partial->pt_argc + argcount_in;
8868 }
8869 }
8870
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871
8872 /* execute the function if no errors detected and executing */
8873 if (evaluate && error == ERROR_NONE)
8874 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008875 char_u *rfname = fname;
8876
8877 /* Ignore "g:" before a function name. */
8878 if (fname[0] == 'g' && fname[1] == ':')
8879 rfname = fname + 2;
8880
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008881 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8882 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 error = ERROR_UNKNOWN;
8884
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008885 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 {
8887 /*
8888 * User defined function.
8889 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008890 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008891
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008893 /* Trigger FuncUndefined event, may load the function. */
8894 if (fp == NULL
8895 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008896 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008897 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008899 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008900 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 }
8902#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008903 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008904 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008905 {
8906 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008907 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008908 }
8909
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 if (fp != NULL)
8911 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008912 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008914 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008916 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008918 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008919 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 else
8921 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008922 int did_save_redo = FALSE;
8923
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 /*
8925 * Call the user function.
8926 * Save and restore search patterns, script variables and
8927 * redo buffer.
8928 */
8929 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008930#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008931 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008932#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008933 {
8934 saveRedobuff();
8935 did_save_redo = TRUE;
8936 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008937 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008939 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008940 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8941 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8942 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008943 /* Function was unreferenced while being used, free it
8944 * now. */
8945 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008946 if (did_save_redo)
8947 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 restore_search_patterns();
8949 error = ERROR_NONE;
8950 }
8951 }
8952 }
8953 else
8954 {
8955 /*
8956 * Find the function name in the table, call its implementation.
8957 */
8958 i = find_internal_func(fname);
8959 if (i >= 0)
8960 {
8961 if (argcount < functions[i].f_min_argc)
8962 error = ERROR_TOOFEW;
8963 else if (argcount > functions[i].f_max_argc)
8964 error = ERROR_TOOMANY;
8965 else
8966 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008967 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 error = ERROR_NONE;
8970 }
8971 }
8972 }
8973 /*
8974 * The function call (or "FuncUndefined" autocommand sequence) might
8975 * have been aborted by an error, an interrupt, or an explicitly thrown
8976 * exception that has not been caught so far. This situation can be
8977 * tested for by calling aborting(). For an error in an internal
8978 * function or for the "E132" error in call_user_func(), however, the
8979 * throw point at which the "force_abort" flag (temporarily reset by
8980 * emsg()) is normally updated has not been reached yet. We need to
8981 * update that flag first to make aborting() reliable.
8982 */
8983 update_force_abort();
8984 }
8985 if (error == ERROR_NONE)
8986 ret = OK;
8987
8988 /*
8989 * Report an error unless the argument evaluation or function call has been
8990 * cancelled due to an aborting error, an interrupt, or an exception.
8991 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008992 if (!aborting())
8993 {
8994 switch (error)
8995 {
8996 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008997 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008998 break;
8999 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009000 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009001 break;
9002 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009003 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009004 name);
9005 break;
9006 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009007 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009008 name);
9009 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009010 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009011 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009012 name);
9013 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009014 }
9015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009017 while (argv_clear > 0)
9018 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009019 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009020 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021
9022 return ret;
9023}
9024
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009025/*
9026 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009027 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009028 */
9029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009030emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009031{
9032 char_u *p;
9033
9034 if (*name == K_SPECIAL)
9035 p = concat_str((char_u *)"<SNR>", name + 3);
9036 else
9037 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009038 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009039 if (p != name)
9040 vim_free(p);
9041}
9042
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009043/*
9044 * Return TRUE for a non-zero Number and a non-empty String.
9045 */
9046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009047non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009048{
9049 return ((argvars[0].v_type == VAR_NUMBER
9050 && argvars[0].vval.v_number != 0)
9051 || (argvars[0].v_type == VAR_STRING
9052 && argvars[0].vval.v_string != NULL
9053 && *argvars[0].vval.v_string != NUL));
9054}
9055
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056/*********************************************
9057 * Implementation of the built-in functions
9058 */
9059
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009060#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009061static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009062
9063/*
9064 * Get the float value of "argvars[0]" into "f".
9065 * Returns FAIL when the argument is not a Number or Float.
9066 */
9067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009068get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009069{
9070 if (argvars[0].v_type == VAR_FLOAT)
9071 {
9072 *f = argvars[0].vval.v_float;
9073 return OK;
9074 }
9075 if (argvars[0].v_type == VAR_NUMBER)
9076 {
9077 *f = (float_T)argvars[0].vval.v_number;
9078 return OK;
9079 }
9080 EMSG(_("E808: Number or Float required"));
9081 return FAIL;
9082}
9083
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009084/*
9085 * "abs(expr)" function
9086 */
9087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009088f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009089{
9090 if (argvars[0].v_type == VAR_FLOAT)
9091 {
9092 rettv->v_type = VAR_FLOAT;
9093 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9094 }
9095 else
9096 {
9097 varnumber_T n;
9098 int error = FALSE;
9099
9100 n = get_tv_number_chk(&argvars[0], &error);
9101 if (error)
9102 rettv->vval.v_number = -1;
9103 else if (n > 0)
9104 rettv->vval.v_number = n;
9105 else
9106 rettv->vval.v_number = -n;
9107 }
9108}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009109
9110/*
9111 * "acos()" function
9112 */
9113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009114f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009115{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009116 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009117
9118 rettv->v_type = VAR_FLOAT;
9119 if (get_float_arg(argvars, &f) == OK)
9120 rettv->vval.v_float = acos(f);
9121 else
9122 rettv->vval.v_float = 0.0;
9123}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009124#endif
9125
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009127 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 */
9129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009130f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009135 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009137 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009138 && !tv_check_lock(l->lv_lock,
9139 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009140 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009141 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009142 }
9143 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009144 EMSG(_(e_listreq));
9145}
9146
9147/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009148 * "alloc_fail(id, countdown, repeat)" function
9149 */
9150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009151f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009152{
9153 if (argvars[0].v_type != VAR_NUMBER
9154 || argvars[0].vval.v_number <= 0
9155 || argvars[1].v_type != VAR_NUMBER
9156 || argvars[1].vval.v_number < 0
9157 || argvars[2].v_type != VAR_NUMBER)
9158 EMSG(_(e_invarg));
9159 else
9160 {
9161 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009162 if (alloc_fail_id >= aid_last)
9163 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009164 alloc_fail_countdown = argvars[1].vval.v_number;
9165 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009166 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009167 }
9168}
9169
9170/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009171 * "and(expr, expr)" function
9172 */
9173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009174f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009175{
9176 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9177 & get_tv_number_chk(&argvars[1], NULL);
9178}
9179
9180/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009181 * "append(lnum, string/list)" function
9182 */
9183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009184f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009185{
9186 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009187 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009188 list_T *l = NULL;
9189 listitem_T *li = NULL;
9190 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009191 long added = 0;
9192
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009193 /* When coming here from Insert mode, sync undo, so that this can be
9194 * undone separately from what was previously inserted. */
9195 if (u_sync_once == 2)
9196 {
9197 u_sync_once = 1; /* notify that u_sync() was called */
9198 u_sync(TRUE);
9199 }
9200
Bram Moolenaar0d660222005-01-07 21:51:51 +00009201 lnum = get_tv_lnum(argvars);
9202 if (lnum >= 0
9203 && lnum <= curbuf->b_ml.ml_line_count
9204 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009205 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009206 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009207 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009208 l = argvars[1].vval.v_list;
9209 if (l == NULL)
9210 return;
9211 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009212 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009213 for (;;)
9214 {
9215 if (l == NULL)
9216 tv = &argvars[1]; /* append a string */
9217 else if (li == NULL)
9218 break; /* end of list */
9219 else
9220 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 line = get_tv_string_chk(tv);
9222 if (line == NULL) /* type error */
9223 {
9224 rettv->vval.v_number = 1; /* Failed */
9225 break;
9226 }
9227 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009228 ++added;
9229 if (l == NULL)
9230 break;
9231 li = li->li_next;
9232 }
9233
9234 appended_lines_mark(lnum, added);
9235 if (curwin->w_cursor.lnum > lnum)
9236 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 else
9239 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240}
9241
9242/*
9243 * "argc()" function
9244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009246f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009248 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
9250
9251/*
9252 * "argidx()" function
9253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009255f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009257 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258}
9259
9260/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009261 * "arglistid()" function
9262 */
9263 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009264f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009265{
9266 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009267
9268 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009269 wp = find_tabwin(&argvars[0], &argvars[1]);
9270 if (wp != NULL)
9271 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009272}
9273
9274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 * "argv(nr)" function
9276 */
9277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009278f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
9280 int idx;
9281
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009282 if (argvars[0].v_type != VAR_UNKNOWN)
9283 {
9284 idx = get_tv_number_chk(&argvars[0], NULL);
9285 if (idx >= 0 && idx < ARGCOUNT)
9286 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9287 else
9288 rettv->vval.v_string = NULL;
9289 rettv->v_type = VAR_STRING;
9290 }
9291 else if (rettv_list_alloc(rettv) == OK)
9292 for (idx = 0; idx < ARGCOUNT; ++idx)
9293 list_append_string(rettv->vval.v_list,
9294 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295}
9296
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009297static void prepare_assert_error(garray_T*gap);
9298static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9299static void assert_error(garray_T *gap);
9300static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009301
9302/*
9303 * Prepare "gap" for an assert error and add the sourcing position.
9304 */
9305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009306prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009307{
9308 char buf[NUMBUFLEN];
9309
9310 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009311 if (sourcing_name != NULL)
9312 {
9313 ga_concat(gap, sourcing_name);
9314 if (sourcing_lnum > 0)
9315 ga_concat(gap, (char_u *)" ");
9316 }
9317 if (sourcing_lnum > 0)
9318 {
9319 sprintf(buf, "line %ld", (long)sourcing_lnum);
9320 ga_concat(gap, (char_u *)buf);
9321 }
9322 if (sourcing_name != NULL || sourcing_lnum > 0)
9323 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009324}
9325
9326/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009327 * Append "str" to "gap", escaping unprintable characters.
9328 * Changes NL to \n, CR to \r, etc.
9329 */
9330 static void
9331ga_concat_esc(garray_T *gap, char_u *str)
9332{
9333 char_u *p;
9334 char_u buf[NUMBUFLEN];
9335
Bram Moolenaarf1551962016-03-15 12:55:58 +01009336 if (str == NULL)
9337 {
9338 ga_concat(gap, (char_u *)"NULL");
9339 return;
9340 }
9341
Bram Moolenaar23689172016-02-15 22:37:37 +01009342 for (p = str; *p != NUL; ++p)
9343 switch (*p)
9344 {
9345 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9346 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9347 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9348 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9349 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9350 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9351 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9352 default:
9353 if (*p < ' ')
9354 {
9355 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9356 ga_concat(gap, buf);
9357 }
9358 else
9359 ga_append(gap, *p);
9360 break;
9361 }
9362}
9363
9364/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009365 * Fill "gap" with information about an assert error.
9366 */
9367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009368fill_assert_error(
9369 garray_T *gap,
9370 typval_T *opt_msg_tv,
9371 char_u *exp_str,
9372 typval_T *exp_tv,
9373 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009374{
9375 char_u numbuf[NUMBUFLEN];
9376 char_u *tofree;
9377
9378 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9379 {
9380 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9381 vim_free(tofree);
9382 }
9383 else
9384 {
9385 ga_concat(gap, (char_u *)"Expected ");
9386 if (exp_str == NULL)
9387 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009388 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009389 vim_free(tofree);
9390 }
9391 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009392 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009393 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009394 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009395 vim_free(tofree);
9396 }
9397}
Bram Moolenaar43345542015-11-29 17:35:35 +01009398
9399/*
9400 * Add an assert error to v:errors.
9401 */
9402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009403assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009404{
9405 struct vimvar *vp = &vimvars[VV_ERRORS];
9406
9407 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9408 /* Make sure v:errors is a list. */
9409 set_vim_var_list(VV_ERRORS, list_alloc());
9410 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9411}
9412
Bram Moolenaar43345542015-11-29 17:35:35 +01009413/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009414 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009415 */
9416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009417f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009418{
9419 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009420
9421 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9422 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009423 prepare_assert_error(&ga);
9424 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9425 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009426 ga_clear(&ga);
9427 }
9428}
9429
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009430/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009431 * "assert_exception(string[, msg])" function
9432 */
9433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009434f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009435{
9436 garray_T ga;
9437 char *error;
9438
9439 error = (char *)get_tv_string_chk(&argvars[0]);
9440 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9441 {
9442 prepare_assert_error(&ga);
9443 ga_concat(&ga, (char_u *)"v:exception is not set");
9444 assert_error(&ga);
9445 ga_clear(&ga);
9446 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009447 else if (error != NULL
9448 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009449 {
9450 prepare_assert_error(&ga);
9451 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9452 &vimvars[VV_EXCEPTION].vv_tv);
9453 assert_error(&ga);
9454 ga_clear(&ga);
9455 }
9456}
9457
9458/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009459 * "assert_fails(cmd [, error])" function
9460 */
9461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009462f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009463{
9464 char_u *cmd = get_tv_string_chk(&argvars[0]);
9465 garray_T ga;
9466
9467 called_emsg = FALSE;
9468 suppress_errthrow = TRUE;
9469 emsg_silent = TRUE;
9470 do_cmdline_cmd(cmd);
9471 if (!called_emsg)
9472 {
9473 prepare_assert_error(&ga);
9474 ga_concat(&ga, (char_u *)"command did not fail: ");
9475 ga_concat(&ga, cmd);
9476 assert_error(&ga);
9477 ga_clear(&ga);
9478 }
9479 else if (argvars[1].v_type != VAR_UNKNOWN)
9480 {
9481 char_u buf[NUMBUFLEN];
9482 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9483
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009484 if (error == NULL
9485 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009486 {
9487 prepare_assert_error(&ga);
9488 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9489 &vimvars[VV_ERRMSG].vv_tv);
9490 assert_error(&ga);
9491 ga_clear(&ga);
9492 }
9493 }
9494
9495 called_emsg = FALSE;
9496 suppress_errthrow = FALSE;
9497 emsg_silent = FALSE;
9498 emsg_on_display = FALSE;
9499 set_vim_var_string(VV_ERRMSG, NULL, 0);
9500}
9501
9502/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009503 * Common for assert_true() and assert_false().
9504 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009506assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009507{
9508 int error = FALSE;
9509 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009510
Bram Moolenaar37127922016-02-06 20:29:28 +01009511 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009512 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009513 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009514 if (argvars[0].v_type != VAR_NUMBER
9515 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9516 || error)
9517 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009518 prepare_assert_error(&ga);
9519 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009520 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009521 NULL, &argvars[0]);
9522 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009523 ga_clear(&ga);
9524 }
9525}
9526
9527/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009528 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009529 */
9530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009531f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009532{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009533 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009534}
9535
9536/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009537 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009538 */
9539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009540f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009541{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009542 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009543}
9544
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009545#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009546/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009547 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009548 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009550f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009551{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009552 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009553
9554 rettv->v_type = VAR_FLOAT;
9555 if (get_float_arg(argvars, &f) == OK)
9556 rettv->vval.v_float = asin(f);
9557 else
9558 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009559}
9560
9561/*
9562 * "atan()" function
9563 */
9564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009565f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009566{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009567 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009568
9569 rettv->v_type = VAR_FLOAT;
9570 if (get_float_arg(argvars, &f) == OK)
9571 rettv->vval.v_float = atan(f);
9572 else
9573 rettv->vval.v_float = 0.0;
9574}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009575
9576/*
9577 * "atan2()" function
9578 */
9579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009580f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009581{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009582 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009583
9584 rettv->v_type = VAR_FLOAT;
9585 if (get_float_arg(argvars, &fx) == OK
9586 && get_float_arg(&argvars[1], &fy) == OK)
9587 rettv->vval.v_float = atan2(fx, fy);
9588 else
9589 rettv->vval.v_float = 0.0;
9590}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009591#endif
9592
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593/*
9594 * "browse(save, title, initdir, default)" function
9595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009597f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
9599#ifdef FEAT_BROWSE
9600 int save;
9601 char_u *title;
9602 char_u *initdir;
9603 char_u *defname;
9604 char_u buf[NUMBUFLEN];
9605 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009606 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009608 save = get_tv_number_chk(&argvars[0], &error);
9609 title = get_tv_string_chk(&argvars[1]);
9610 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9611 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009613 if (error || title == NULL || initdir == NULL || defname == NULL)
9614 rettv->vval.v_string = NULL;
9615 else
9616 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009617 do_browse(save ? BROWSE_SAVE : 0,
9618 title, defname, NULL, initdir, NULL, curbuf);
9619#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009621#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009623}
9624
9625/*
9626 * "browsedir(title, initdir)" function
9627 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009629f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009630{
9631#ifdef FEAT_BROWSE
9632 char_u *title;
9633 char_u *initdir;
9634 char_u buf[NUMBUFLEN];
9635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009636 title = get_tv_string_chk(&argvars[0]);
9637 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 if (title == NULL || initdir == NULL)
9640 rettv->vval.v_string = NULL;
9641 else
9642 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009643 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648}
9649
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009650static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009651
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652/*
9653 * Find a buffer by number or exact name.
9654 */
9655 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009656find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009660 if (avar->v_type == VAR_NUMBER)
9661 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009662 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009664 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009665 if (buf == NULL)
9666 {
9667 /* No full path name match, try a match with a URL or a "nofile"
9668 * buffer, these don't use the full path. */
9669 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9670 if (buf->b_fname != NULL
9671 && (path_with_url(buf->b_fname)
9672#ifdef FEAT_QUICKFIX
9673 || bt_nofile(buf)
9674#endif
9675 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009676 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009677 break;
9678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 }
9680 return buf;
9681}
9682
9683/*
9684 * "bufexists(expr)" function
9685 */
9686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009687f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690}
9691
9692/*
9693 * "buflisted(expr)" function
9694 */
9695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009696f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697{
9698 buf_T *buf;
9699
9700 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702}
9703
9704/*
9705 * "bufloaded(expr)" function
9706 */
9707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009708f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 buf_T *buf;
9711
9712 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714}
9715
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009716 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009717buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 int save_magic;
9720 char_u *save_cpo;
9721 buf_T *buf;
9722
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9724 save_magic = p_magic;
9725 p_magic = TRUE;
9726 save_cpo = p_cpo;
9727 p_cpo = (char_u *)"";
9728
9729 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009730 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731
9732 p_magic = save_magic;
9733 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009734 return buf;
9735}
9736
9737/*
9738 * Get buffer by number or pattern.
9739 */
9740 static buf_T *
9741get_buf_tv(typval_T *tv, int curtab_only)
9742{
9743 char_u *name = tv->vval.v_string;
9744 buf_T *buf;
9745
9746 if (tv->v_type == VAR_NUMBER)
9747 return buflist_findnr((int)tv->vval.v_number);
9748 if (tv->v_type != VAR_STRING)
9749 return NULL;
9750 if (name == NULL || *name == NUL)
9751 return curbuf;
9752 if (name[0] == '$' && name[1] == NUL)
9753 return lastbuf;
9754
9755 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756
9757 /* If not found, try expanding the name, like done for bufexists(). */
9758 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760
9761 return buf;
9762}
9763
9764/*
9765 * "bufname(expr)" function
9766 */
9767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009768f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769{
9770 buf_T *buf;
9771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009774 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 --emsg_off;
9781}
9782
9783/*
9784 * "bufnr(expr)" function
9785 */
9786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009787f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788{
9789 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009790 int error = FALSE;
9791 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009795 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009796 --emsg_off;
9797
9798 /* If the buffer isn't found and the second argument is not zero create a
9799 * new buffer. */
9800 if (buf == NULL
9801 && argvars[1].v_type != VAR_UNKNOWN
9802 && get_tv_number_chk(&argvars[1], &error) != 0
9803 && !error
9804 && (name = get_tv_string_chk(&argvars[0])) != NULL
9805 && !error)
9806 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9807
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812}
9813
9814/*
9815 * "bufwinnr(nr)" function
9816 */
9817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009818f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819{
9820#ifdef FEAT_WINDOWS
9821 win_T *wp;
9822 int winnr = 0;
9823#endif
9824 buf_T *buf;
9825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009828 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829#ifdef FEAT_WINDOWS
9830 for (wp = firstwin; wp; wp = wp->w_next)
9831 {
9832 ++winnr;
9833 if (wp->w_buffer == buf)
9834 break;
9835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839#endif
9840 --emsg_off;
9841}
9842
9843/*
9844 * "byte2line(byte)" function
9845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009847f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848{
9849#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851#else
9852 long boff = 0;
9853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009854 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 (linenr_T)0, &boff);
9860#endif
9861}
9862
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009864byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009865{
9866#ifdef FEAT_MBYTE
9867 char_u *t;
9868#endif
9869 char_u *str;
9870 long idx;
9871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009872 str = get_tv_string_chk(&argvars[0]);
9873 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009875 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009876 return;
9877
9878#ifdef FEAT_MBYTE
9879 t = str;
9880 for ( ; idx > 0; idx--)
9881 {
9882 if (*t == NUL) /* EOL reached */
9883 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009884 if (enc_utf8 && comp)
9885 t += utf_ptr2len(t);
9886 else
9887 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009888 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009889 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009890#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009891 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009893#endif
9894}
9895
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009896/*
9897 * "byteidx()" function
9898 */
9899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009900f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009901{
9902 byteidx(argvars, rettv, FALSE);
9903}
9904
9905/*
9906 * "byteidxcomp()" function
9907 */
9908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009909f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009910{
9911 byteidx(argvars, rettv, TRUE);
9912}
9913
Bram Moolenaardb913952012-06-29 12:54:53 +02009914 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009915func_call(
9916 char_u *name,
9917 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009918 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009919 dict_T *selfdict,
9920 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009921{
9922 listitem_T *item;
9923 typval_T argv[MAX_FUNC_ARGS + 1];
9924 int argc = 0;
9925 int dummy;
9926 int r = 0;
9927
9928 for (item = args->vval.v_list->lv_first; item != NULL;
9929 item = item->li_next)
9930 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009931 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009932 {
9933 EMSG(_("E699: Too many arguments"));
9934 break;
9935 }
9936 /* Make a copy of each argument. This is needed to be able to set
9937 * v_lock to VAR_FIXED in the copy without changing the original list.
9938 */
9939 copy_tv(&item->li_tv, &argv[argc++]);
9940 }
9941
9942 if (item == NULL)
9943 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9944 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009945 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009946
9947 /* Free the arguments. */
9948 while (argc > 0)
9949 clear_tv(&argv[--argc]);
9950
9951 return r;
9952}
9953
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009954/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009955 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009956 */
9957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009958f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009959{
9960 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009961 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009962 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009963
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009964 if (argvars[1].v_type != VAR_LIST)
9965 {
9966 EMSG(_(e_listreq));
9967 return;
9968 }
9969 if (argvars[1].vval.v_list == NULL)
9970 return;
9971
9972 if (argvars[0].v_type == VAR_FUNC)
9973 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009974 else if (argvars[0].v_type == VAR_PARTIAL)
9975 {
9976 partial = argvars[0].vval.v_partial;
9977 func = partial->pt_name;
9978 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009979 else
9980 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009981 if (*func == NUL)
9982 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009983
Bram Moolenaare9a41262005-01-15 22:18:47 +00009984 if (argvars[2].v_type != VAR_UNKNOWN)
9985 {
9986 if (argvars[2].v_type != VAR_DICT)
9987 {
9988 EMSG(_(e_dictreq));
9989 return;
9990 }
9991 selfdict = argvars[2].vval.v_dict;
9992 }
9993
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009994 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009995}
9996
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009997#ifdef FEAT_FLOAT
9998/*
9999 * "ceil({float})" function
10000 */
10001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010002f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010003{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010004 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010005
10006 rettv->v_type = VAR_FLOAT;
10007 if (get_float_arg(argvars, &f) == OK)
10008 rettv->vval.v_float = ceil(f);
10009 else
10010 rettv->vval.v_float = 0.0;
10011}
10012#endif
10013
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010014#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010015/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010016 * "ch_close()" function
10017 */
10018 static void
10019f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10020{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010021 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010022
10023 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010024 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010025 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010026 channel_clear(channel);
10027 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010028}
10029
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010030/*
10031 * "ch_getbufnr()" function
10032 */
10033 static void
10034f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10035{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010036 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010037
10038 rettv->vval.v_number = -1;
10039 if (channel != NULL)
10040 {
10041 char_u *what = get_tv_string(&argvars[1]);
10042 int part;
10043
10044 if (STRCMP(what, "err") == 0)
10045 part = PART_ERR;
10046 else if (STRCMP(what, "out") == 0)
10047 part = PART_OUT;
10048 else if (STRCMP(what, "in") == 0)
10049 part = PART_IN;
10050 else
10051 part = PART_SOCK;
10052 if (channel->ch_part[part].ch_buffer != NULL)
10053 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10054 }
10055}
10056
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010057/*
10058 * "ch_getjob()" function
10059 */
10060 static void
10061f_ch_getjob(typval_T *argvars, typval_T *rettv)
10062{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010063 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010064
10065 if (channel != NULL)
10066 {
10067 rettv->v_type = VAR_JOB;
10068 rettv->vval.v_job = channel->ch_job;
10069 if (channel->ch_job != NULL)
10070 ++channel->ch_job->jv_refcount;
10071 }
10072}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010073
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010074/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010075 * "ch_info()" function
10076 */
10077 static void
10078f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10079{
10080 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10081
10082 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10083 channel_info(channel, rettv->vval.v_dict);
10084}
10085
10086/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010087 * "ch_log()" function
10088 */
10089 static void
10090f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10091{
10092 char_u *msg = get_tv_string(&argvars[0]);
10093 channel_T *channel = NULL;
10094
10095 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010096 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010097
10098 ch_log(channel, (char *)msg);
10099}
10100
10101/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010102 * "ch_logfile()" function
10103 */
10104 static void
10105f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10106{
10107 char_u *fname;
10108 char_u *opt = (char_u *)"";
10109 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010110
10111 fname = get_tv_string(&argvars[0]);
10112 if (argvars[1].v_type == VAR_STRING)
10113 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010114 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010115}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010116
10117/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010118 * "ch_open()" function
10119 */
10120 static void
10121f_ch_open(typval_T *argvars, typval_T *rettv)
10122{
Bram Moolenaar77073442016-02-13 23:23:53 +010010123 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010124 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010125}
10126
10127/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010128 * "ch_read()" function
10129 */
10130 static void
10131f_ch_read(typval_T *argvars, typval_T *rettv)
10132{
10133 common_channel_read(argvars, rettv, FALSE);
10134}
10135
10136/*
10137 * "ch_readraw()" function
10138 */
10139 static void
10140f_ch_readraw(typval_T *argvars, typval_T *rettv)
10141{
10142 common_channel_read(argvars, rettv, TRUE);
10143}
10144
10145/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010146 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010147 */
10148 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010149f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10150{
10151 ch_expr_common(argvars, rettv, TRUE);
10152}
10153
10154/*
10155 * "ch_sendexpr()" function
10156 */
10157 static void
10158f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10159{
10160 ch_expr_common(argvars, rettv, FALSE);
10161}
10162
10163/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010164 * "ch_evalraw()" function
10165 */
10166 static void
10167f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10168{
10169 ch_raw_common(argvars, rettv, TRUE);
10170}
10171
10172/*
10173 * "ch_sendraw()" function
10174 */
10175 static void
10176f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10177{
10178 ch_raw_common(argvars, rettv, FALSE);
10179}
10180
10181/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010182 * "ch_setoptions()" function
10183 */
10184 static void
10185f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10186{
10187 channel_T *channel;
10188 jobopt_T opt;
10189
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010190 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010191 if (channel == NULL)
10192 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010193 clear_job_options(&opt);
10194 if (get_job_options(&argvars[1], &opt,
10195 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010196 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010197 channel_set_options(channel, &opt);
10198}
10199
10200/*
10201 * "ch_status()" function
10202 */
10203 static void
10204f_ch_status(typval_T *argvars, typval_T *rettv)
10205{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010206 channel_T *channel;
10207
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010208 /* return an empty string by default */
10209 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010210 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010211
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010212 channel = get_channel_arg(&argvars[0], FALSE);
10213 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010214}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010215#endif
10216
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010217/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010218 * "changenr()" function
10219 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010221f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010222{
10223 rettv->vval.v_number = curbuf->b_u_seq_cur;
10224}
10225
10226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 * "char2nr(string)" function
10228 */
10229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010230f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231{
10232#ifdef FEAT_MBYTE
10233 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010234 {
10235 int utf8 = 0;
10236
10237 if (argvars[1].v_type != VAR_UNKNOWN)
10238 utf8 = get_tv_number_chk(&argvars[1], NULL);
10239
10240 if (utf8)
10241 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10242 else
10243 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 else
10246#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248}
10249
10250/*
10251 * "cindent(lnum)" function
10252 */
10253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010254f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255{
10256#ifdef FEAT_CINDENT
10257 pos_T pos;
10258 linenr_T lnum;
10259
10260 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010261 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10263 {
10264 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010265 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 curwin->w_cursor = pos;
10267 }
10268 else
10269#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010270 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271}
10272
10273/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010274 * "clearmatches()" function
10275 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010277f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010278{
10279#ifdef FEAT_SEARCH_EXTRA
10280 clear_matches(curwin);
10281#endif
10282}
10283
10284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 * "col(string)" function
10286 */
10287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010288f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289{
10290 colnr_T col = 0;
10291 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010292 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010294 fp = var2fpos(&argvars[0], FALSE, &fnum);
10295 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 {
10297 if (fp->col == MAXCOL)
10298 {
10299 /* '> can be MAXCOL, get the length of the line then */
10300 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010301 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 else
10303 col = MAXCOL;
10304 }
10305 else
10306 {
10307 col = fp->col + 1;
10308#ifdef FEAT_VIRTUALEDIT
10309 /* col(".") when the cursor is on the NUL at the end of the line
10310 * because of "coladd" can be seen as an extra column. */
10311 if (virtual_active() && fp == &curwin->w_cursor)
10312 {
10313 char_u *p = ml_get_cursor();
10314
10315 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10316 curwin->w_virtcol - curwin->w_cursor.coladd))
10317 {
10318# ifdef FEAT_MBYTE
10319 int l;
10320
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010321 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 col += l;
10323# else
10324 if (*p != NUL && p[1] == NUL)
10325 ++col;
10326# endif
10327 }
10328 }
10329#endif
10330 }
10331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010332 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333}
10334
Bram Moolenaar572cb562005-08-05 21:35:02 +000010335#if defined(FEAT_INS_EXPAND)
10336/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010337 * "complete()" function
10338 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010340f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010341{
10342 int startcol;
10343
10344 if ((State & INSERT) == 0)
10345 {
10346 EMSG(_("E785: complete() can only be used in Insert mode"));
10347 return;
10348 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010349
10350 /* Check for undo allowed here, because if something was already inserted
10351 * the line was already saved for undo and this check isn't done. */
10352 if (!undo_allowed())
10353 return;
10354
Bram Moolenaarade00832006-03-10 21:46:58 +000010355 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10356 {
10357 EMSG(_(e_invarg));
10358 return;
10359 }
10360
10361 startcol = get_tv_number_chk(&argvars[0], NULL);
10362 if (startcol <= 0)
10363 return;
10364
10365 set_completion(startcol - 1, argvars[1].vval.v_list);
10366}
10367
10368/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010369 * "complete_add()" function
10370 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010372f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010373{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010374 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010375}
10376
10377/*
10378 * "complete_check()" function
10379 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010381f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010382{
10383 int saved = RedrawingDisabled;
10384
10385 RedrawingDisabled = 0;
10386 ins_compl_check_keys(0);
10387 rettv->vval.v_number = compl_interrupted;
10388 RedrawingDisabled = saved;
10389}
10390#endif
10391
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392/*
10393 * "confirm(message, buttons[, default [, type]])" function
10394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010396f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397{
10398#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10399 char_u *message;
10400 char_u *buttons = NULL;
10401 char_u buf[NUMBUFLEN];
10402 char_u buf2[NUMBUFLEN];
10403 int def = 1;
10404 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 char_u *typestr;
10406 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010408 message = get_tv_string_chk(&argvars[0]);
10409 if (message == NULL)
10410 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010411 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010413 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10414 if (buttons == NULL)
10415 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010416 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010419 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10422 if (typestr == NULL)
10423 error = TRUE;
10424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010426 switch (TOUPPER_ASC(*typestr))
10427 {
10428 case 'E': type = VIM_ERROR; break;
10429 case 'Q': type = VIM_QUESTION; break;
10430 case 'I': type = VIM_INFO; break;
10431 case 'W': type = VIM_WARNING; break;
10432 case 'G': type = VIM_GENERIC; break;
10433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 }
10435 }
10436 }
10437 }
10438
10439 if (buttons == NULL || *buttons == NUL)
10440 buttons = (char_u *)_("&Ok");
10441
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010442 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010444 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445#endif
10446}
10447
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010448/*
10449 * "copy()" function
10450 */
10451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010452f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010453{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010454 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010455}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010457#ifdef FEAT_FLOAT
10458/*
10459 * "cos()" function
10460 */
10461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010462f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010463{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010464 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010465
10466 rettv->v_type = VAR_FLOAT;
10467 if (get_float_arg(argvars, &f) == OK)
10468 rettv->vval.v_float = cos(f);
10469 else
10470 rettv->vval.v_float = 0.0;
10471}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010472
10473/*
10474 * "cosh()" function
10475 */
10476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010477f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010478{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010479 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010480
10481 rettv->v_type = VAR_FLOAT;
10482 if (get_float_arg(argvars, &f) == OK)
10483 rettv->vval.v_float = cosh(f);
10484 else
10485 rettv->vval.v_float = 0.0;
10486}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010487#endif
10488
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010490 * "count()" function
10491 */
10492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010493f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010494{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010495 long n = 0;
10496 int ic = FALSE;
10497
Bram Moolenaare9a41262005-01-15 22:18:47 +000010498 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010499 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010500 listitem_T *li;
10501 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010502 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010503
Bram Moolenaare9a41262005-01-15 22:18:47 +000010504 if ((l = argvars[0].vval.v_list) != NULL)
10505 {
10506 li = l->lv_first;
10507 if (argvars[2].v_type != VAR_UNKNOWN)
10508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 int error = FALSE;
10510
10511 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010512 if (argvars[3].v_type != VAR_UNKNOWN)
10513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 idx = get_tv_number_chk(&argvars[3], &error);
10515 if (!error)
10516 {
10517 li = list_find(l, idx);
10518 if (li == NULL)
10519 EMSGN(_(e_listidx), idx);
10520 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010521 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 if (error)
10523 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010524 }
10525
10526 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010527 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010528 ++n;
10529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010530 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010531 else if (argvars[0].v_type == VAR_DICT)
10532 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010533 int todo;
10534 dict_T *d;
10535 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010536
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010537 if ((d = argvars[0].vval.v_dict) != NULL)
10538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 int error = FALSE;
10540
Bram Moolenaare9a41262005-01-15 22:18:47 +000010541 if (argvars[2].v_type != VAR_UNKNOWN)
10542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010543 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 if (argvars[3].v_type != VAR_UNKNOWN)
10545 EMSG(_(e_invarg));
10546 }
10547
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010548 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010549 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010550 {
10551 if (!HASHITEM_EMPTY(hi))
10552 {
10553 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010554 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010555 ++n;
10556 }
10557 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010558 }
10559 }
10560 else
10561 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010562 rettv->vval.v_number = n;
10563}
10564
10565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10567 *
10568 * Checks the existence of a cscope connection.
10569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010571f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572{
10573#ifdef FEAT_CSCOPE
10574 int num = 0;
10575 char_u *dbpath = NULL;
10576 char_u *prepend = NULL;
10577 char_u buf[NUMBUFLEN];
10578
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010579 if (argvars[0].v_type != VAR_UNKNOWN
10580 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582 num = (int)get_tv_number(&argvars[0]);
10583 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010584 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010585 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 }
10587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589#endif
10590}
10591
10592/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010593 * "cursor(lnum, col)" function, or
10594 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010596 * Moves the cursor to the specified line and column.
10597 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010600f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
10602 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010603#ifdef FEAT_VIRTUALEDIT
10604 long coladd = 0;
10605#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010606 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010608 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010609 if (argvars[1].v_type == VAR_UNKNOWN)
10610 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010611 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010612 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010613
Bram Moolenaar493c1782014-05-28 14:34:46 +020010614 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010615 {
10616 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010617 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010618 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010619 line = pos.lnum;
10620 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010621#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010622 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010623#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010624 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010625 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010626 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010627 set_curswant = FALSE;
10628 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010629 }
10630 else
10631 {
10632 line = get_tv_lnum(argvars);
10633 col = get_tv_number_chk(&argvars[1], NULL);
10634#ifdef FEAT_VIRTUALEDIT
10635 if (argvars[2].v_type != VAR_UNKNOWN)
10636 coladd = get_tv_number_chk(&argvars[2], NULL);
10637#endif
10638 }
10639 if (line < 0 || col < 0
10640#ifdef FEAT_VIRTUALEDIT
10641 || coladd < 0
10642#endif
10643 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 if (line > 0)
10646 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 if (col > 0)
10648 curwin->w_cursor.col = col - 1;
10649#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010650 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651#endif
10652
10653 /* Make sure the cursor is in a valid position. */
10654 check_cursor();
10655#ifdef FEAT_MBYTE
10656 /* Correct cursor for multi-byte character. */
10657 if (has_mbyte)
10658 mb_adjust_cursor();
10659#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010660
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010661 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010662 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663}
10664
10665/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010666 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 */
10668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010669f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010671 int noref = 0;
10672
10673 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010675 if (noref < 0 || noref > 1)
10676 EMSG(_(e_invarg));
10677 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010678 {
10679 current_copyID += COPYID_INC;
10680 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682}
10683
10684/*
10685 * "delete()" function
10686 */
10687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010688f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010690 char_u nbuf[NUMBUFLEN];
10691 char_u *name;
10692 char_u *flags;
10693
10694 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010696 return;
10697
10698 name = get_tv_string(&argvars[0]);
10699 if (name == NULL || *name == NUL)
10700 {
10701 EMSG(_(e_invarg));
10702 return;
10703 }
10704
10705 if (argvars[1].v_type != VAR_UNKNOWN)
10706 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010708 flags = (char_u *)"";
10709
10710 if (*flags == NUL)
10711 /* delete a file */
10712 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10713 else if (STRCMP(flags, "d") == 0)
10714 /* delete an empty directory */
10715 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10716 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010717 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010718 rettv->vval.v_number = delete_recursive(name);
10719 else
10720 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721}
10722
10723/*
10724 * "did_filetype()" function
10725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010727f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728{
10729#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731#endif
10732}
10733
10734/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010735 * "diff_filler()" function
10736 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010738f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010739{
10740#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010742#endif
10743}
10744
10745/*
10746 * "diff_hlID()" function
10747 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010749f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010750{
10751#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010753 static linenr_T prev_lnum = 0;
10754 static int changedtick = 0;
10755 static int fnum = 0;
10756 static int change_start = 0;
10757 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010758 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010759 int filler_lines;
10760 int col;
10761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010762 if (lnum < 0) /* ignore type error in {lnum} arg */
10763 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010764 if (lnum != prev_lnum
10765 || changedtick != curbuf->b_changedtick
10766 || fnum != curbuf->b_fnum)
10767 {
10768 /* New line, buffer, change: need to get the values. */
10769 filler_lines = diff_check(curwin, lnum);
10770 if (filler_lines < 0)
10771 {
10772 if (filler_lines == -1)
10773 {
10774 change_start = MAXCOL;
10775 change_end = -1;
10776 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10777 hlID = HLF_ADD; /* added line */
10778 else
10779 hlID = HLF_CHD; /* changed line */
10780 }
10781 else
10782 hlID = HLF_ADD; /* added line */
10783 }
10784 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010785 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010786 prev_lnum = lnum;
10787 changedtick = curbuf->b_changedtick;
10788 fnum = curbuf->b_fnum;
10789 }
10790
10791 if (hlID == HLF_CHD || hlID == HLF_TXD)
10792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010793 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010794 if (col >= change_start && col <= change_end)
10795 hlID = HLF_TXD; /* changed text */
10796 else
10797 hlID = HLF_CHD; /* changed line */
10798 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010799 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010800#endif
10801}
10802
10803/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010804 * "disable_char_avail_for_testing({expr})" function
10805 */
10806 static void
10807f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10808{
10809 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10810}
10811
10812/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010813 * "empty({expr})" function
10814 */
10815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010816f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010817{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010818 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010819
10820 switch (argvars[0].v_type)
10821 {
10822 case VAR_STRING:
10823 case VAR_FUNC:
10824 n = argvars[0].vval.v_string == NULL
10825 || *argvars[0].vval.v_string == NUL;
10826 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010827 case VAR_PARTIAL:
10828 n = FALSE;
10829 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010830 case VAR_NUMBER:
10831 n = argvars[0].vval.v_number == 0;
10832 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010833 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010834#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010835 n = argvars[0].vval.v_float == 0.0;
10836 break;
10837#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010838 case VAR_LIST:
10839 n = argvars[0].vval.v_list == NULL
10840 || argvars[0].vval.v_list->lv_first == NULL;
10841 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 case VAR_DICT:
10843 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010844 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010846 case VAR_SPECIAL:
10847 n = argvars[0].vval.v_number != VVAL_TRUE;
10848 break;
10849
Bram Moolenaar835dc632016-02-07 14:27:38 +010010850 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010851#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010852 n = argvars[0].vval.v_job == NULL
10853 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10854 break;
10855#endif
10856 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010857#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010858 n = argvars[0].vval.v_channel == NULL
10859 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010860 break;
10861#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010862 case VAR_UNKNOWN:
10863 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10864 n = TRUE;
10865 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010866 }
10867
10868 rettv->vval.v_number = n;
10869}
10870
10871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 * "escape({string}, {chars})" function
10873 */
10874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010875f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876{
10877 char_u buf[NUMBUFLEN];
10878
Bram Moolenaar758711c2005-02-02 23:11:38 +000010879 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10880 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882}
10883
10884/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010885 * "eval()" function
10886 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010888f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010889{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010890 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 s = get_tv_string_chk(&argvars[0]);
10893 if (s != NULL)
10894 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010895
Bram Moolenaar615b9972015-01-14 17:15:05 +010010896 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10898 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010899 if (p != NULL && !aborting())
10900 EMSG2(_(e_invexpr2), p);
10901 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010902 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010903 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010905 else if (*s != NUL)
10906 EMSG(_(e_trailing));
10907}
10908
10909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 * "eventhandler()" function
10911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010913f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916}
10917
10918/*
10919 * "executable()" function
10920 */
10921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010922f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010924 char_u *name = get_tv_string(&argvars[0]);
10925
10926 /* Check in $PATH and also check directly if there is a directory name. */
10927 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10928 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010929}
10930
10931/*
10932 * "exepath()" function
10933 */
10934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010935f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010936{
10937 char_u *p = NULL;
10938
Bram Moolenaarb5971142015-03-21 17:32:19 +010010939 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010940 rettv->v_type = VAR_STRING;
10941 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942}
10943
10944/*
10945 * "exists()" function
10946 */
10947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010948f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949{
10950 char_u *p;
10951 char_u *name;
10952 int n = FALSE;
10953 int len = 0;
10954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 if (*p == '$') /* environment variable */
10957 {
10958 /* first try "normal" environment variables (fast) */
10959 if (mch_getenv(p + 1) != NULL)
10960 n = TRUE;
10961 else
10962 {
10963 /* try expanding things like $VIM and ${HOME} */
10964 p = expand_env_save(p);
10965 if (p != NULL && *p != '$')
10966 n = TRUE;
10967 vim_free(p);
10968 }
10969 }
10970 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010972 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010973 if (*skipwhite(p) != NUL)
10974 n = FALSE; /* trailing garbage */
10975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 else if (*p == '*') /* internal or user defined function */
10977 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010978 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 }
10980 else if (*p == ':')
10981 {
10982 n = cmd_exists(p + 1);
10983 }
10984 else if (*p == '#')
10985 {
10986#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010987 if (p[1] == '#')
10988 n = autocmd_supported(p + 2);
10989 else
10990 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991#endif
10992 }
10993 else /* internal variable */
10994 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010995 char_u *tofree;
10996 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010998 /* get_name_len() takes care of expanding curly braces */
10999 name = p;
11000 len = get_name_len(&p, &tofree, TRUE, FALSE);
11001 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011003 if (tofree != NULL)
11004 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011005 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011006 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011008 /* handle d.key, l[idx], f(expr) */
11009 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11010 if (n)
11011 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 }
11013 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011014 if (*p != NUL)
11015 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011017 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 }
11019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011020 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021}
11022
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011023#ifdef FEAT_FLOAT
11024/*
11025 * "exp()" function
11026 */
11027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011028f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011029{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011030 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011031
11032 rettv->v_type = VAR_FLOAT;
11033 if (get_float_arg(argvars, &f) == OK)
11034 rettv->vval.v_float = exp(f);
11035 else
11036 rettv->vval.v_float = 0.0;
11037}
11038#endif
11039
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040/*
11041 * "expand()" function
11042 */
11043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011044f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045{
11046 char_u *s;
11047 int len;
11048 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011049 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011052 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011055 if (argvars[1].v_type != VAR_UNKNOWN
11056 && argvars[2].v_type != VAR_UNKNOWN
11057 && get_tv_number_chk(&argvars[2], &error)
11058 && !error)
11059 {
11060 rettv->v_type = VAR_LIST;
11061 rettv->vval.v_list = NULL;
11062 }
11063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 if (*s == '%' || *s == '#' || *s == '<')
11066 {
11067 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011068 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011070 if (rettv->v_type == VAR_LIST)
11071 {
11072 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11073 list_append_string(rettv->vval.v_list, result, -1);
11074 else
11075 vim_free(result);
11076 }
11077 else
11078 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 }
11080 else
11081 {
11082 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011083 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 if (argvars[1].v_type != VAR_UNKNOWN
11085 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011086 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087 if (!error)
11088 {
11089 ExpandInit(&xpc);
11090 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011091 if (p_wic)
11092 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011093 if (rettv->v_type == VAR_STRING)
11094 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11095 options, WILD_ALL);
11096 else if (rettv_list_alloc(rettv) != FAIL)
11097 {
11098 int i;
11099
11100 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11101 for (i = 0; i < xpc.xp_numfiles; i++)
11102 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11103 ExpandCleanup(&xpc);
11104 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 }
11106 else
11107 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 }
11109}
11110
11111/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011112 * Go over all entries in "d2" and add them to "d1".
11113 * When "action" is "error" then a duplicate key is an error.
11114 * When "action" is "force" then a duplicate key is overwritten.
11115 * Otherwise duplicate keys are ignored ("action" is "keep").
11116 */
11117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011118dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011119{
11120 dictitem_T *di1;
11121 hashitem_T *hi2;
11122 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011123 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011124
11125 todo = (int)d2->dv_hashtab.ht_used;
11126 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11127 {
11128 if (!HASHITEM_EMPTY(hi2))
11129 {
11130 --todo;
11131 di1 = dict_find(d1, hi2->hi_key, -1);
11132 if (d1->dv_scope != 0)
11133 {
11134 /* Disallow replacing a builtin function in l: and g:.
11135 * Check the key to be valid when adding to any
11136 * scope. */
11137 if (d1->dv_scope == VAR_DEF_SCOPE
11138 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11139 && var_check_func_name(hi2->hi_key,
11140 di1 == NULL))
11141 break;
11142 if (!valid_varname(hi2->hi_key))
11143 break;
11144 }
11145 if (di1 == NULL)
11146 {
11147 di1 = dictitem_copy(HI2DI(hi2));
11148 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11149 dictitem_free(di1);
11150 }
11151 else if (*action == 'e')
11152 {
11153 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11154 break;
11155 }
11156 else if (*action == 'f' && HI2DI(hi2) != di1)
11157 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011158 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11159 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011160 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011161 clear_tv(&di1->di_tv);
11162 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11163 }
11164 }
11165 }
11166}
11167
11168/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011169 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011170 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011171 */
11172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011173f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011174{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011175 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011176
Bram Moolenaare9a41262005-01-15 22:18:47 +000011177 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011178 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011179 list_T *l1, *l2;
11180 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011181 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011182 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011183
Bram Moolenaare9a41262005-01-15 22:18:47 +000011184 l1 = argvars[0].vval.v_list;
11185 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011186 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011187 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011188 {
11189 if (argvars[2].v_type != VAR_UNKNOWN)
11190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011191 before = get_tv_number_chk(&argvars[2], &error);
11192 if (error)
11193 return; /* type error; errmsg already given */
11194
Bram Moolenaar758711c2005-02-02 23:11:38 +000011195 if (before == l1->lv_len)
11196 item = NULL;
11197 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011198 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011199 item = list_find(l1, before);
11200 if (item == NULL)
11201 {
11202 EMSGN(_(e_listidx), before);
11203 return;
11204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011205 }
11206 }
11207 else
11208 item = NULL;
11209 list_extend(l1, l2, item);
11210
Bram Moolenaare9a41262005-01-15 22:18:47 +000011211 copy_tv(&argvars[0], rettv);
11212 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011214 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11215 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011216 dict_T *d1, *d2;
11217 char_u *action;
11218 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011219
11220 d1 = argvars[0].vval.v_dict;
11221 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011222 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011223 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011224 {
11225 /* Check the third argument. */
11226 if (argvars[2].v_type != VAR_UNKNOWN)
11227 {
11228 static char *(av[]) = {"keep", "force", "error"};
11229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 action = get_tv_string_chk(&argvars[2]);
11231 if (action == NULL)
11232 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011233 for (i = 0; i < 3; ++i)
11234 if (STRCMP(action, av[i]) == 0)
11235 break;
11236 if (i == 3)
11237 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011238 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239 return;
11240 }
11241 }
11242 else
11243 action = (char_u *)"force";
11244
Bram Moolenaara9922d62013-05-30 13:01:18 +020011245 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011246
Bram Moolenaare9a41262005-01-15 22:18:47 +000011247 copy_tv(&argvars[0], rettv);
11248 }
11249 }
11250 else
11251 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011252}
11253
11254/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011255 * "feedkeys()" function
11256 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011258f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011259{
11260 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011261 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011262 char_u *keys, *flags;
11263 char_u nbuf[NUMBUFLEN];
11264 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011265 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011266 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011267
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011268 /* This is not allowed in the sandbox. If the commands would still be
11269 * executed in the sandbox it would be OK, but it probably happens later,
11270 * when "sandbox" is no longer set. */
11271 if (check_secure())
11272 return;
11273
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011274 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011275
11276 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011277 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011278 flags = get_tv_string_buf(&argvars[1], nbuf);
11279 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011280 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011281 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011282 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011283 case 'n': remap = FALSE; break;
11284 case 'm': remap = TRUE; break;
11285 case 't': typed = TRUE; break;
11286 case 'i': insert = TRUE; break;
11287 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011288 }
11289 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011290 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011291
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011292 if (*keys != NUL || execute)
11293 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011294 /* Need to escape K_SPECIAL and CSI before putting the string in the
11295 * typeahead buffer. */
11296 keys_esc = vim_strsave_escape_csi(keys);
11297 if (keys_esc != NULL)
11298 {
11299 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011300 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011301 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011302 if (vgetc_busy)
11303 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011304 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011305 {
11306 int save_msg_scroll = msg_scroll;
11307
11308 /* Avoid a 1 second delay when the keys start Insert mode. */
11309 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011310 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011311 msg_scroll |= save_msg_scroll;
11312 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011313 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011314 }
11315}
11316
11317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 * "filereadable()" function
11319 */
11320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011321f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011323 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 char_u *p;
11325 int n;
11326
Bram Moolenaarc236c162008-07-13 17:41:49 +000011327#ifndef O_NONBLOCK
11328# define O_NONBLOCK 0
11329#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011331 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11332 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 {
11334 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011335 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 }
11337 else
11338 n = FALSE;
11339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341}
11342
11343/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011344 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 * rights to write into.
11346 */
11347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011348f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011350 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351}
11352
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011353 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011354findfilendir(
11355 typval_T *argvars UNUSED,
11356 typval_T *rettv,
11357 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011358{
11359#ifdef FEAT_SEARCHPATH
11360 char_u *fname;
11361 char_u *fresult = NULL;
11362 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11363 char_u *p;
11364 char_u pathbuf[NUMBUFLEN];
11365 int count = 1;
11366 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011367 int error = FALSE;
11368#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011369
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011370 rettv->vval.v_string = NULL;
11371 rettv->v_type = VAR_STRING;
11372
11373#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011375
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011376 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11379 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011380 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 else
11382 {
11383 if (*p != NUL)
11384 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011387 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011389 }
11390
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011391 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11392 error = TRUE;
11393
11394 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011396 do
11397 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011398 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011399 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 fresult = find_file_in_path_option(first ? fname : NULL,
11401 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011402 0, first, path,
11403 find_what,
11404 curbuf->b_ffname,
11405 find_what == FINDFILE_DIR
11406 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011408
11409 if (fresult != NULL && rettv->v_type == VAR_LIST)
11410 list_append_string(rettv->vval.v_list, fresult, -1);
11411
11412 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011414
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011415 if (rettv->v_type == VAR_STRING)
11416 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011417#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011418}
11419
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011420static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11421static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011422
11423/*
11424 * Implementation of map() and filter().
11425 */
11426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011427filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011428{
11429 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011430 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011431 listitem_T *li, *nli;
11432 list_T *l = NULL;
11433 dictitem_T *di;
11434 hashtab_T *ht;
11435 hashitem_T *hi;
11436 dict_T *d = NULL;
11437 typval_T save_val;
11438 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011439 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011440 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011441 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011442 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011443 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011444 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011445 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011446
Bram Moolenaare9a41262005-01-15 22:18:47 +000011447 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011448 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011449 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011450 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011451 return;
11452 }
11453 else if (argvars[0].v_type == VAR_DICT)
11454 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011455 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011456 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011457 return;
11458 }
11459 else
11460 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011461 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011462 return;
11463 }
11464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 expr = get_tv_string_buf_chk(&argvars[1], buf);
11466 /* On type errors, the preceding call has already displayed an error
11467 * message. Avoid a misleading error message for an empty string that
11468 * was not passed as argument. */
11469 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011471 prepare_vimvar(VV_VAL, &save_val);
11472 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011473
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011474 /* We reset "did_emsg" to be able to detect whether an error
11475 * occurred during evaluation of the expression. */
11476 save_did_emsg = did_emsg;
11477 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011478
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011479 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011482 vimvars[VV_KEY].vv_type = VAR_STRING;
11483
11484 ht = &d->dv_hashtab;
11485 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011486 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011487 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011489 if (!HASHITEM_EMPTY(hi))
11490 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011491 int r;
11492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011493 --todo;
11494 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011495 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011496 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11497 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498 break;
11499 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011500 r = filter_map_one(&di->di_tv, expr, map, &rem);
11501 clear_tv(&vimvars[VV_KEY].vv_tv);
11502 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011503 break;
11504 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011505 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011506 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11507 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011508 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011509 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011510 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011511 }
11512 }
11513 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 }
11515 else
11516 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011517 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 for (li = l->lv_first; li != NULL; li = nli)
11520 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011521 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011522 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011523 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011524 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011525 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011526 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011527 break;
11528 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011529 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011530 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011531 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011532 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011533
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011534 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011535 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011536
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011537 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011538 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011539
11540 copy_tv(&argvars[0], rettv);
11541}
11542
11543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011544filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011545{
Bram Moolenaar33570922005-01-25 22:26:29 +000011546 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011547 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011548 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011549
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011551 s = expr;
11552 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011553 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011554 if (*s != NUL) /* check for trailing chars after expr */
11555 {
11556 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011557 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011558 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011559 }
11560 if (map)
11561 {
11562 /* map(): replace the list item value */
11563 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011564 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011565 *tv = rettv;
11566 }
11567 else
11568 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011569 int error = FALSE;
11570
Bram Moolenaare9a41262005-01-15 22:18:47 +000011571 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011572 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011573 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011574 /* On type error, nothing has been removed; return FAIL to stop the
11575 * loop. The error message was given by get_tv_number_chk(). */
11576 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011577 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011578 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011579 retval = OK;
11580theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011582 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011583}
11584
11585/*
11586 * "filter()" function
11587 */
11588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011589f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011590{
11591 filter_map(argvars, rettv, FALSE);
11592}
11593
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011595 * "finddir({fname}[, {path}[, {count}]])" function
11596 */
11597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011598f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011599{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011600 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011601}
11602
11603/*
11604 * "findfile({fname}[, {path}[, {count}]])" function
11605 */
11606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011607f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011608{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011609 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011610}
11611
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011612#ifdef FEAT_FLOAT
11613/*
11614 * "float2nr({float})" function
11615 */
11616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011617f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011618{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011619 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011620
11621 if (get_float_arg(argvars, &f) == OK)
11622 {
11623 if (f < -0x7fffffff)
11624 rettv->vval.v_number = -0x7fffffff;
11625 else if (f > 0x7fffffff)
11626 rettv->vval.v_number = 0x7fffffff;
11627 else
11628 rettv->vval.v_number = (varnumber_T)f;
11629 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011630}
11631
11632/*
11633 * "floor({float})" function
11634 */
11635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011636f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011637{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011638 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011639
11640 rettv->v_type = VAR_FLOAT;
11641 if (get_float_arg(argvars, &f) == OK)
11642 rettv->vval.v_float = floor(f);
11643 else
11644 rettv->vval.v_float = 0.0;
11645}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011646
11647/*
11648 * "fmod()" function
11649 */
11650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011651f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011652{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011653 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011654
11655 rettv->v_type = VAR_FLOAT;
11656 if (get_float_arg(argvars, &fx) == OK
11657 && get_float_arg(&argvars[1], &fy) == OK)
11658 rettv->vval.v_float = fmod(fx, fy);
11659 else
11660 rettv->vval.v_float = 0.0;
11661}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011662#endif
11663
Bram Moolenaar0d660222005-01-07 21:51:51 +000011664/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011665 * "fnameescape({string})" function
11666 */
11667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011668f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011669{
11670 rettv->vval.v_string = vim_strsave_fnameescape(
11671 get_tv_string(&argvars[0]), FALSE);
11672 rettv->v_type = VAR_STRING;
11673}
11674
11675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 * "fnamemodify({fname}, {mods})" function
11677 */
11678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011679f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680{
11681 char_u *fname;
11682 char_u *mods;
11683 int usedlen = 0;
11684 int len;
11685 char_u *fbuf = NULL;
11686 char_u buf[NUMBUFLEN];
11687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011688 fname = get_tv_string_chk(&argvars[0]);
11689 mods = get_tv_string_buf_chk(&argvars[1], buf);
11690 if (fname == NULL || mods == NULL)
11691 fname = NULL;
11692 else
11693 {
11694 len = (int)STRLEN(fname);
11695 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011698 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011700 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 vim_free(fbuf);
11704}
11705
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011706static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707
11708/*
11709 * "foldclosed()" function
11710 */
11711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011712foldclosed_both(
11713 typval_T *argvars UNUSED,
11714 typval_T *rettv,
11715 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717#ifdef FEAT_FOLDING
11718 linenr_T lnum;
11719 linenr_T first, last;
11720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011721 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11723 {
11724 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11725 {
11726 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 return;
11731 }
11732 }
11733#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735}
11736
11737/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011738 * "foldclosed()" function
11739 */
11740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011741f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011742{
11743 foldclosed_both(argvars, rettv, FALSE);
11744}
11745
11746/*
11747 * "foldclosedend()" function
11748 */
11749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011750f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011751{
11752 foldclosed_both(argvars, rettv, TRUE);
11753}
11754
11755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 * "foldlevel()" function
11757 */
11758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011759f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760{
11761#ifdef FEAT_FOLDING
11762 linenr_T lnum;
11763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768}
11769
11770/*
11771 * "foldtext()" function
11772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011774f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775{
11776#ifdef FEAT_FOLDING
11777 linenr_T lnum;
11778 char_u *s;
11779 char_u *r;
11780 int len;
11781 char *txt;
11782#endif
11783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 rettv->v_type = VAR_STRING;
11785 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011787 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11788 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11789 <= curbuf->b_ml.ml_line_count
11790 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 {
11792 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011793 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11794 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 {
11796 if (!linewhite(lnum))
11797 break;
11798 ++lnum;
11799 }
11800
11801 /* Find interesting text in this line. */
11802 s = skipwhite(ml_get(lnum));
11803 /* skip C comment-start */
11804 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011807 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011808 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011809 {
11810 s = skipwhite(ml_get(lnum + 1));
11811 if (*s == '*')
11812 s = skipwhite(s + 1);
11813 }
11814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 txt = _("+-%s%3ld lines: ");
11816 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011817 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 + 20 /* for %3ld */
11819 + STRLEN(s))); /* concatenated */
11820 if (r != NULL)
11821 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011822 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11823 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11824 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 len = (int)STRLEN(r);
11826 STRCAT(r, s);
11827 /* remove 'foldmarker' and 'commentstring' */
11828 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 }
11831 }
11832#endif
11833}
11834
11835/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011836 * "foldtextresult(lnum)" function
11837 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011839f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011840{
11841#ifdef FEAT_FOLDING
11842 linenr_T lnum;
11843 char_u *text;
11844 char_u buf[51];
11845 foldinfo_T foldinfo;
11846 int fold_count;
11847#endif
11848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849 rettv->v_type = VAR_STRING;
11850 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011851#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011853 /* treat illegal types and illegal string values for {lnum} the same */
11854 if (lnum < 0)
11855 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011856 fold_count = foldedCount(curwin, lnum, &foldinfo);
11857 if (fold_count > 0)
11858 {
11859 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11860 &foldinfo, buf);
11861 if (text == buf)
11862 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011863 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011864 }
11865#endif
11866}
11867
11868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 * "foreground()" function
11870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011872f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874#ifdef FEAT_GUI
11875 if (gui.in_use)
11876 gui_mch_set_foreground();
11877#else
11878# ifdef WIN32
11879 win32_set_foreground();
11880# endif
11881#endif
11882}
11883
11884/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011885 * "function()" function
11886 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011888f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011889{
11890 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011891 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011892 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011893 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011894
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011895 if (argvars[0].v_type == VAR_FUNC)
11896 {
11897 /* function(MyFunc, [arg], dict) */
11898 s = argvars[0].vval.v_string;
11899 }
11900 else if (argvars[0].v_type == VAR_PARTIAL
11901 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011902 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011903 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011904 arg_pt = argvars[0].vval.v_partial;
11905 s = arg_pt->pt_name;
11906 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011907 else
11908 {
11909 /* function('MyFunc', [arg], dict) */
11910 s = get_tv_string(&argvars[0]);
11911 use_string = TRUE;
11912 }
11913
11914 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011915 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011916 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011917 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11918 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011919 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011920 else
11921 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011922 int dict_idx = 0;
11923 int arg_idx = 0;
11924 list_T *list = NULL;
11925
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011926 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011927 {
11928 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011929 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011930
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011931 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11932 * also be called from another script. Using trans_function_name()
11933 * would also work, but some plugins depend on the name being
11934 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011935 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011936 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11937 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011938 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011939 STRCPY(name, sid_buf);
11940 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011941 }
11942 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011943 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011944 name = vim_strsave(s);
11945
11946 if (argvars[1].v_type != VAR_UNKNOWN)
11947 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011948 if (argvars[2].v_type != VAR_UNKNOWN)
11949 {
11950 /* function(name, [args], dict) */
11951 arg_idx = 1;
11952 dict_idx = 2;
11953 }
11954 else if (argvars[1].v_type == VAR_DICT)
11955 /* function(name, dict) */
11956 dict_idx = 1;
11957 else
11958 /* function(name, [args]) */
11959 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011960 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011961 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011962 if (argvars[dict_idx].v_type != VAR_DICT)
11963 {
11964 EMSG(_("E922: expected a dict"));
11965 vim_free(name);
11966 return;
11967 }
11968 if (argvars[dict_idx].vval.v_dict == NULL)
11969 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011970 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011971 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011972 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011973 if (argvars[arg_idx].v_type != VAR_LIST)
11974 {
11975 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11976 vim_free(name);
11977 return;
11978 }
11979 list = argvars[arg_idx].vval.v_list;
11980 if (list == NULL || list->lv_len == 0)
11981 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011982 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011983 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011984 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010011985 {
11986 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011987
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011988 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010011989 if (pt == NULL)
11990 vim_free(name);
11991 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011992 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011993 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011994 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011995 listitem_T *li;
11996 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011997 int arg_len = 0;
11998 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011999
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012000 if (arg_pt != NULL)
12001 arg_len = arg_pt->pt_argc;
12002 if (list != NULL)
12003 lv_len = list->lv_len;
12004 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012005 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012006 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012007 if (pt->pt_argv == NULL)
12008 {
12009 vim_free(pt);
12010 vim_free(name);
12011 return;
12012 }
12013 else
12014 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012015 for (i = 0; i < arg_len; i++)
12016 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12017 if (lv_len > 0)
12018 for (li = list->lv_first; li != NULL;
12019 li = li->li_next)
12020 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012021 }
12022 }
12023
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012024 /* For "function(dict.func, [], dict)" and "func" is a partial
12025 * use "dict". That is backwards compatible. */
12026 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012027 {
12028 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12029 ++pt->pt_dict->dv_refcount;
12030 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012031 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012032 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012033 pt->pt_dict = arg_pt->pt_dict;
12034 if (pt->pt_dict != NULL)
12035 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012036 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012037
12038 pt->pt_refcount = 1;
12039 pt->pt_name = name;
12040 func_ref(pt->pt_name);
12041 }
12042 rettv->v_type = VAR_PARTIAL;
12043 rettv->vval.v_partial = pt;
12044 }
12045 else
12046 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012047 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012048 rettv->v_type = VAR_FUNC;
12049 rettv->vval.v_string = name;
12050 func_ref(name);
12051 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012052 }
12053}
12054
12055/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012056 * "garbagecollect()" function
12057 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012059f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012060{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012061 /* This is postponed until we are back at the toplevel, because we may be
12062 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12063 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012064
12065 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12066 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012067}
12068
12069/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012070 * "get()" function
12071 */
12072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012073f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012074{
Bram Moolenaar33570922005-01-25 22:26:29 +000012075 listitem_T *li;
12076 list_T *l;
12077 dictitem_T *di;
12078 dict_T *d;
12079 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012080
Bram Moolenaare9a41262005-01-15 22:18:47 +000012081 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012082 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012083 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012085 int error = FALSE;
12086
12087 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12088 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012089 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012090 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012091 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012092 else if (argvars[0].v_type == VAR_DICT)
12093 {
12094 if ((d = argvars[0].vval.v_dict) != NULL)
12095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012096 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012097 if (di != NULL)
12098 tv = &di->di_tv;
12099 }
12100 }
12101 else
12102 EMSG2(_(e_listdictarg), "get()");
12103
12104 if (tv == NULL)
12105 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012106 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012107 copy_tv(&argvars[2], rettv);
12108 }
12109 else
12110 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012111}
12112
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012113static 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 +000012114
12115/*
12116 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012117 * Return a range (from start to end) of lines in rettv from the specified
12118 * buffer.
12119 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012120 */
12121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012122get_buffer_lines(
12123 buf_T *buf,
12124 linenr_T start,
12125 linenr_T end,
12126 int retlist,
12127 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012128{
12129 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012130
Bram Moolenaar959a1432013-12-14 12:17:38 +010012131 rettv->v_type = VAR_STRING;
12132 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012133 if (retlist && rettv_list_alloc(rettv) == FAIL)
12134 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012135
12136 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12137 return;
12138
12139 if (!retlist)
12140 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012141 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12142 p = ml_get_buf(buf, start, FALSE);
12143 else
12144 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012145 rettv->vval.v_string = vim_strsave(p);
12146 }
12147 else
12148 {
12149 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012150 return;
12151
12152 if (start < 1)
12153 start = 1;
12154 if (end > buf->b_ml.ml_line_count)
12155 end = buf->b_ml.ml_line_count;
12156 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012157 if (list_append_string(rettv->vval.v_list,
12158 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012159 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012160 }
12161}
12162
12163/*
12164 * "getbufline()" function
12165 */
12166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012167f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012168{
12169 linenr_T lnum;
12170 linenr_T end;
12171 buf_T *buf;
12172
12173 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12174 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012175 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012176 --emsg_off;
12177
Bram Moolenaar661b1822005-07-28 22:36:45 +000012178 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012179 if (argvars[2].v_type == VAR_UNKNOWN)
12180 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012181 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012182 end = get_tv_lnum_buf(&argvars[2], buf);
12183
Bram Moolenaar342337a2005-07-21 21:11:17 +000012184 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012185}
12186
Bram Moolenaar0d660222005-01-07 21:51:51 +000012187/*
12188 * "getbufvar()" function
12189 */
12190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012191f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012192{
12193 buf_T *buf;
12194 buf_T *save_curbuf;
12195 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012196 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012197 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012199 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12200 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012201 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012202 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012203
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012204 rettv->v_type = VAR_STRING;
12205 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206
12207 if (buf != NULL && varname != NULL)
12208 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012209 /* set curbuf to be our buf, temporarily */
12210 save_curbuf = curbuf;
12211 curbuf = buf;
12212
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012214 {
12215 if (get_option_tv(&varname, rettv, TRUE) == OK)
12216 done = TRUE;
12217 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012218 else if (STRCMP(varname, "changedtick") == 0)
12219 {
12220 rettv->v_type = VAR_NUMBER;
12221 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012222 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012223 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012224 else
12225 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012226 /* Look up the variable. */
12227 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12228 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12229 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012230 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012231 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012232 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012233 done = TRUE;
12234 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012235 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012236
12237 /* restore previous notion of curbuf */
12238 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012239 }
12240
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012241 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12242 /* use the default value */
12243 copy_tv(&argvars[2], rettv);
12244
Bram Moolenaar0d660222005-01-07 21:51:51 +000012245 --emsg_off;
12246}
12247
12248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249 * "getchar()" function
12250 */
12251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012252f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253{
12254 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012255 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012257 /* Position the cursor. Needed after a message that ends in a space. */
12258 windgoto(msg_row, msg_col);
12259
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 ++no_mapping;
12261 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012262 for (;;)
12263 {
12264 if (argvars[0].v_type == VAR_UNKNOWN)
12265 /* getchar(): blocking wait. */
12266 n = safe_vgetc();
12267 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12268 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012269 n = vpeekc_any();
12270 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012271 /* illegal argument or getchar(0) and no char avail: return zero */
12272 n = 0;
12273 else
12274 /* getchar(0) and char avail: return char */
12275 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012276
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012277 if (n == K_IGNORE)
12278 continue;
12279 break;
12280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 --no_mapping;
12282 --allow_keys;
12283
Bram Moolenaar219b8702006-11-01 14:32:36 +000012284 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12285 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12286 vimvars[VV_MOUSE_COL].vv_nr = 0;
12287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 if (IS_SPECIAL(n) || mod_mask != 0)
12290 {
12291 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12292 int i = 0;
12293
12294 /* Turn a special key into three bytes, plus modifier. */
12295 if (mod_mask != 0)
12296 {
12297 temp[i++] = K_SPECIAL;
12298 temp[i++] = KS_MODIFIER;
12299 temp[i++] = mod_mask;
12300 }
12301 if (IS_SPECIAL(n))
12302 {
12303 temp[i++] = K_SPECIAL;
12304 temp[i++] = K_SECOND(n);
12305 temp[i++] = K_THIRD(n);
12306 }
12307#ifdef FEAT_MBYTE
12308 else if (has_mbyte)
12309 i += (*mb_char2bytes)(n, temp + i);
12310#endif
12311 else
12312 temp[i++] = n;
12313 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012314 rettv->v_type = VAR_STRING;
12315 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012316
12317#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012318 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012319 {
12320 int row = mouse_row;
12321 int col = mouse_col;
12322 win_T *win;
12323 linenr_T lnum;
12324# ifdef FEAT_WINDOWS
12325 win_T *wp;
12326# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012327 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012328
12329 if (row >= 0 && col >= 0)
12330 {
12331 /* Find the window at the mouse coordinates and compute the
12332 * text position. */
12333 win = mouse_find_win(&row, &col);
12334 (void)mouse_comp_pos(win, &row, &col, &lnum);
12335# ifdef FEAT_WINDOWS
12336 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012337 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012338# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012339 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012340 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12341 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12342 }
12343 }
12344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 }
12346}
12347
12348/*
12349 * "getcharmod()" function
12350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012352f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355}
12356
12357/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012358 * "getcharsearch()" function
12359 */
12360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012361f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012362{
12363 if (rettv_dict_alloc(rettv) != FAIL)
12364 {
12365 dict_T *dict = rettv->vval.v_dict;
12366
12367 dict_add_nr_str(dict, "char", 0L, last_csearch());
12368 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12369 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12370 }
12371}
12372
12373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 * "getcmdline()" function
12375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012377f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 rettv->v_type = VAR_STRING;
12380 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381}
12382
12383/*
12384 * "getcmdpos()" function
12385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012387f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390}
12391
12392/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012393 * "getcmdtype()" function
12394 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012396f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012397{
12398 rettv->v_type = VAR_STRING;
12399 rettv->vval.v_string = alloc(2);
12400 if (rettv->vval.v_string != NULL)
12401 {
12402 rettv->vval.v_string[0] = get_cmdline_type();
12403 rettv->vval.v_string[1] = NUL;
12404 }
12405}
12406
12407/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012408 * "getcmdwintype()" function
12409 */
12410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012411f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012412{
12413 rettv->v_type = VAR_STRING;
12414 rettv->vval.v_string = NULL;
12415#ifdef FEAT_CMDWIN
12416 rettv->vval.v_string = alloc(2);
12417 if (rettv->vval.v_string != NULL)
12418 {
12419 rettv->vval.v_string[0] = cmdwin_type;
12420 rettv->vval.v_string[1] = NUL;
12421 }
12422#endif
12423}
12424
12425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426 * "getcwd()" function
12427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012429f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012431 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012432 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012434 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012435 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012436
12437 wp = find_tabwin(&argvars[0], &argvars[1]);
12438 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012440 if (wp->w_localdir != NULL)
12441 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12442 else if(globaldir != NULL)
12443 rettv->vval.v_string = vim_strsave(globaldir);
12444 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012445 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012446 cwd = alloc(MAXPATHL);
12447 if (cwd != NULL)
12448 {
12449 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12450 rettv->vval.v_string = vim_strsave(cwd);
12451 vim_free(cwd);
12452 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012453 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012454#ifdef BACKSLASH_IN_FILENAME
12455 if (rettv->vval.v_string != NULL)
12456 slash_adjust(rettv->vval.v_string);
12457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 }
12459}
12460
12461/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012462 * "getfontname()" function
12463 */
12464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012465f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012466{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467 rettv->v_type = VAR_STRING;
12468 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012469#ifdef FEAT_GUI
12470 if (gui.in_use)
12471 {
12472 GuiFont font;
12473 char_u *name = NULL;
12474
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012475 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012476 {
12477 /* Get the "Normal" font. Either the name saved by
12478 * hl_set_font_name() or from the font ID. */
12479 font = gui.norm_font;
12480 name = hl_get_font_name();
12481 }
12482 else
12483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012484 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012485 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12486 return;
12487 font = gui_mch_get_font(name, FALSE);
12488 if (font == NOFONT)
12489 return; /* Invalid font name, return empty string. */
12490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012492 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012493 gui_mch_free_font(font);
12494 }
12495#endif
12496}
12497
12498/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012499 * "getfperm({fname})" function
12500 */
12501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012502f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012503{
12504 char_u *fname;
12505 struct stat st;
12506 char_u *perm = NULL;
12507 char_u flags[] = "rwx";
12508 int i;
12509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012513 if (mch_stat((char *)fname, &st) >= 0)
12514 {
12515 perm = vim_strsave((char_u *)"---------");
12516 if (perm != NULL)
12517 {
12518 for (i = 0; i < 9; i++)
12519 {
12520 if (st.st_mode & (1 << (8 - i)))
12521 perm[i] = flags[i % 3];
12522 }
12523 }
12524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012526}
12527
12528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 * "getfsize({fname})" function
12530 */
12531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012532f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533{
12534 char_u *fname;
12535 struct stat st;
12536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012537 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540
12541 if (mch_stat((char *)fname, &st) >= 0)
12542 {
12543 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012544 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012548
12549 /* non-perfect check for overflow */
12550 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12551 rettv->vval.v_number = -2;
12552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 }
12554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556}
12557
12558/*
12559 * "getftime({fname})" function
12560 */
12561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012562f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563{
12564 char_u *fname;
12565 struct stat st;
12566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012567 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568
12569 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573}
12574
12575/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012576 * "getftype({fname})" function
12577 */
12578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012579f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012580{
12581 char_u *fname;
12582 struct stat st;
12583 char_u *type = NULL;
12584 char *t;
12585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012586 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012588 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012589 if (mch_lstat((char *)fname, &st) >= 0)
12590 {
12591#ifdef S_ISREG
12592 if (S_ISREG(st.st_mode))
12593 t = "file";
12594 else if (S_ISDIR(st.st_mode))
12595 t = "dir";
12596# ifdef S_ISLNK
12597 else if (S_ISLNK(st.st_mode))
12598 t = "link";
12599# endif
12600# ifdef S_ISBLK
12601 else if (S_ISBLK(st.st_mode))
12602 t = "bdev";
12603# endif
12604# ifdef S_ISCHR
12605 else if (S_ISCHR(st.st_mode))
12606 t = "cdev";
12607# endif
12608# ifdef S_ISFIFO
12609 else if (S_ISFIFO(st.st_mode))
12610 t = "fifo";
12611# endif
12612# ifdef S_ISSOCK
12613 else if (S_ISSOCK(st.st_mode))
12614 t = "fifo";
12615# endif
12616 else
12617 t = "other";
12618#else
12619# ifdef S_IFMT
12620 switch (st.st_mode & S_IFMT)
12621 {
12622 case S_IFREG: t = "file"; break;
12623 case S_IFDIR: t = "dir"; break;
12624# ifdef S_IFLNK
12625 case S_IFLNK: t = "link"; break;
12626# endif
12627# ifdef S_IFBLK
12628 case S_IFBLK: t = "bdev"; break;
12629# endif
12630# ifdef S_IFCHR
12631 case S_IFCHR: t = "cdev"; break;
12632# endif
12633# ifdef S_IFIFO
12634 case S_IFIFO: t = "fifo"; break;
12635# endif
12636# ifdef S_IFSOCK
12637 case S_IFSOCK: t = "socket"; break;
12638# endif
12639 default: t = "other";
12640 }
12641# else
12642 if (mch_isdir(fname))
12643 t = "dir";
12644 else
12645 t = "file";
12646# endif
12647#endif
12648 type = vim_strsave((char_u *)t);
12649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012651}
12652
12653/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012654 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012655 */
12656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012657f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012658{
12659 linenr_T lnum;
12660 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012661 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012662
12663 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012664 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012665 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012666 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012667 retlist = FALSE;
12668 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012669 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012670 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012671 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012672 retlist = TRUE;
12673 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012674
Bram Moolenaar342337a2005-07-21 21:11:17 +000012675 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012676}
12677
12678/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012679 * "getmatches()" function
12680 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012682f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012683{
12684#ifdef FEAT_SEARCH_EXTRA
12685 dict_T *dict;
12686 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012687 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012688
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012689 if (rettv_list_alloc(rettv) == OK)
12690 {
12691 while (cur != NULL)
12692 {
12693 dict = dict_alloc();
12694 if (dict == NULL)
12695 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012696 if (cur->match.regprog == NULL)
12697 {
12698 /* match added with matchaddpos() */
12699 for (i = 0; i < MAXPOSMATCH; ++i)
12700 {
12701 llpos_T *llpos;
12702 char buf[6];
12703 list_T *l;
12704
12705 llpos = &cur->pos.pos[i];
12706 if (llpos->lnum == 0)
12707 break;
12708 l = list_alloc();
12709 if (l == NULL)
12710 break;
12711 list_append_number(l, (varnumber_T)llpos->lnum);
12712 if (llpos->col > 0)
12713 {
12714 list_append_number(l, (varnumber_T)llpos->col);
12715 list_append_number(l, (varnumber_T)llpos->len);
12716 }
12717 sprintf(buf, "pos%d", i + 1);
12718 dict_add_list(dict, buf, l);
12719 }
12720 }
12721 else
12722 {
12723 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12724 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012725 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012726 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12727 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012728# ifdef FEAT_CONCEAL
12729 if (cur->conceal_char)
12730 {
12731 char_u buf[MB_MAXBYTES + 1];
12732
12733 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12734 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12735 }
12736# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012737 list_append_dict(rettv->vval.v_list, dict);
12738 cur = cur->next;
12739 }
12740 }
12741#endif
12742}
12743
12744/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012745 * "getpid()" function
12746 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012748f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012749{
12750 rettv->vval.v_number = mch_get_pid();
12751}
12752
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012753static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012754
12755/*
12756 * "getcurpos()" function
12757 */
12758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012759f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012760{
12761 getpos_both(argvars, rettv, TRUE);
12762}
12763
Bram Moolenaar18081e32008-02-20 19:11:07 +000012764/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012765 * "getpos(string)" function
12766 */
12767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012768f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012769{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012770 getpos_both(argvars, rettv, FALSE);
12771}
12772
12773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012774getpos_both(
12775 typval_T *argvars,
12776 typval_T *rettv,
12777 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012778{
Bram Moolenaara5525202006-03-02 22:52:09 +000012779 pos_T *fp;
12780 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012781 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012782
12783 if (rettv_list_alloc(rettv) == OK)
12784 {
12785 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012786 if (getcurpos)
12787 fp = &curwin->w_cursor;
12788 else
12789 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012790 if (fnum != -1)
12791 list_append_number(l, (varnumber_T)fnum);
12792 else
12793 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012794 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12795 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012796 list_append_number(l, (fp != NULL)
12797 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012798 : (varnumber_T)0);
12799 list_append_number(l,
12800#ifdef FEAT_VIRTUALEDIT
12801 (fp != NULL) ? (varnumber_T)fp->coladd :
12802#endif
12803 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012804 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012805 {
12806 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012807 list_append_number(l, curwin->w_curswant == MAXCOL ?
12808 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012809 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012810 }
12811 else
12812 rettv->vval.v_number = FALSE;
12813}
12814
12815/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012816 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012817 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012819f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012820{
12821#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012822 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012823#endif
12824
Bram Moolenaar2641f772005-03-25 21:58:17 +000012825#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012826 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012827 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012828 wp = NULL;
12829 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12830 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012831 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012832 if (wp == NULL)
12833 return;
12834 }
12835
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012836 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012837 }
12838#endif
12839}
12840
12841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 * "getreg()" function
12843 */
12844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012845f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846{
12847 char_u *strregname;
12848 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012849 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012850 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012851 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012853 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012855 strregname = get_tv_string_chk(&argvars[0]);
12856 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012857 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012859 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012860 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12861 return_list = get_tv_number_chk(&argvars[2], &error);
12862 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012865 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012866
12867 if (error)
12868 return;
12869
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 regname = (strregname == NULL ? '"' : *strregname);
12871 if (regname == 0)
12872 regname = '"';
12873
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012874 if (return_list)
12875 {
12876 rettv->v_type = VAR_LIST;
12877 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12878 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012879 if (rettv->vval.v_list != NULL)
12880 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012881 }
12882 else
12883 {
12884 rettv->v_type = VAR_STRING;
12885 rettv->vval.v_string = get_reg_contents(regname,
12886 arg2 ? GREG_EXPR_SRC : 0);
12887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888}
12889
12890/*
12891 * "getregtype()" function
12892 */
12893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012894f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895{
12896 char_u *strregname;
12897 int regname;
12898 char_u buf[NUMBUFLEN + 2];
12899 long reglen = 0;
12900
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012901 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012902 {
12903 strregname = get_tv_string_chk(&argvars[0]);
12904 if (strregname == NULL) /* type error; errmsg already given */
12905 {
12906 rettv->v_type = VAR_STRING;
12907 rettv->vval.v_string = NULL;
12908 return;
12909 }
12910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 else
12912 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012913 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914
12915 regname = (strregname == NULL ? '"' : *strregname);
12916 if (regname == 0)
12917 regname = '"';
12918
12919 buf[0] = NUL;
12920 buf[1] = NUL;
12921 switch (get_reg_type(regname, &reglen))
12922 {
12923 case MLINE: buf[0] = 'V'; break;
12924 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925 case MBLOCK:
12926 buf[0] = Ctrl_V;
12927 sprintf((char *)buf + 1, "%ld", reglen + 1);
12928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930 rettv->v_type = VAR_STRING;
12931 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932}
12933
12934/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012935 * "gettabvar()" function
12936 */
12937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012938f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012939{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012940 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012941 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012942 dictitem_T *v;
12943 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012944 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012945
12946 rettv->v_type = VAR_STRING;
12947 rettv->vval.v_string = NULL;
12948
12949 varname = get_tv_string_chk(&argvars[1]);
12950 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12951 if (tp != NULL && varname != NULL)
12952 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012953 /* Set tp to be our tabpage, temporarily. Also set the window to the
12954 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012955 if (switch_win(&oldcurwin, &oldtabpage,
12956 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012957 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012958 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012959 /* look up the variable */
12960 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12961 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12962 if (v != NULL)
12963 {
12964 copy_tv(&v->di_tv, rettv);
12965 done = TRUE;
12966 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012967 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012968
12969 /* restore previous notion of curwin */
12970 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012971 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012972
12973 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012974 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012975}
12976
12977/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012978 * "gettabwinvar()" function
12979 */
12980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012981f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012982{
12983 getwinvar(argvars, rettv, 1);
12984}
12985
12986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987 * "getwinposx()" function
12988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012990f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993#ifdef FEAT_GUI
12994 if (gui.in_use)
12995 {
12996 int x, y;
12997
12998 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 }
13001#endif
13002}
13003
13004/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013005 * "win_findbuf()" function
13006 */
13007 static void
13008f_win_findbuf(typval_T *argvars, typval_T *rettv)
13009{
13010 if (rettv_list_alloc(rettv) != FAIL)
13011 win_findbuf(argvars, rettv->vval.v_list);
13012}
13013
13014/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013015 * "win_getid()" function
13016 */
13017 static void
13018f_win_getid(typval_T *argvars, typval_T *rettv)
13019{
13020 rettv->vval.v_number = win_getid(argvars);
13021}
13022
13023/*
13024 * "win_gotoid()" function
13025 */
13026 static void
13027f_win_gotoid(typval_T *argvars, typval_T *rettv)
13028{
13029 rettv->vval.v_number = win_gotoid(argvars);
13030}
13031
13032/*
13033 * "win_id2tabwin()" function
13034 */
13035 static void
13036f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13037{
13038 if (rettv_list_alloc(rettv) != FAIL)
13039 win_id2tabwin(argvars, rettv->vval.v_list);
13040}
13041
13042/*
13043 * "win_id2win()" function
13044 */
13045 static void
13046f_win_id2win(typval_T *argvars, typval_T *rettv)
13047{
13048 rettv->vval.v_number = win_id2win(argvars);
13049}
13050
13051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 * "getwinposy()" function
13053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013055f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058#ifdef FEAT_GUI
13059 if (gui.in_use)
13060 {
13061 int x, y;
13062
13063 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065 }
13066#endif
13067}
13068
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013069/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013070 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013071 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013072 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013073find_win_by_nr(
13074 typval_T *vp,
13075 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013076{
13077#ifdef FEAT_WINDOWS
13078 win_T *wp;
13079#endif
13080 int nr;
13081
13082 nr = get_tv_number_chk(vp, NULL);
13083
13084#ifdef FEAT_WINDOWS
13085 if (nr < 0)
13086 return NULL;
13087 if (nr == 0)
13088 return curwin;
13089
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013090 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13091 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013092 if (--nr <= 0)
13093 break;
13094 return wp;
13095#else
13096 if (nr == 0 || nr == 1)
13097 return curwin;
13098 return NULL;
13099#endif
13100}
13101
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013103 * Find window specified by "wvp" in tabpage "tvp".
13104 */
13105 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013106find_tabwin(
13107 typval_T *wvp, /* VAR_UNKNOWN for current window */
13108 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013109{
13110 win_T *wp = NULL;
13111 tabpage_T *tp = NULL;
13112 long n;
13113
13114 if (wvp->v_type != VAR_UNKNOWN)
13115 {
13116 if (tvp->v_type != VAR_UNKNOWN)
13117 {
13118 n = get_tv_number(tvp);
13119 if (n >= 0)
13120 tp = find_tabpage(n);
13121 }
13122 else
13123 tp = curtab;
13124
13125 if (tp != NULL)
13126 wp = find_win_by_nr(wvp, tp);
13127 }
13128 else
13129 wp = curwin;
13130
13131 return wp;
13132}
13133
13134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 * "getwinvar()" function
13136 */
13137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013138f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013140 getwinvar(argvars, rettv, 0);
13141}
13142
13143/*
13144 * getwinvar() and gettabwinvar()
13145 */
13146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013147getwinvar(
13148 typval_T *argvars,
13149 typval_T *rettv,
13150 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013151{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013152 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013154 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013155 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013156 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013157#ifdef FEAT_WINDOWS
13158 win_T *oldcurwin;
13159 tabpage_T *oldtabpage;
13160 int need_switch_win;
13161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013163#ifdef FEAT_WINDOWS
13164 if (off == 1)
13165 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13166 else
13167 tp = curtab;
13168#endif
13169 win = find_win_by_nr(&argvars[off], tp);
13170 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013171 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013173 rettv->v_type = VAR_STRING;
13174 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175
13176 if (win != NULL && varname != NULL)
13177 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013178#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013179 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013180 * otherwise the window is not valid. Only do this when needed,
13181 * autocommands get blocked. */
13182 need_switch_win = !(tp == curtab && win == curwin);
13183 if (!need_switch_win
13184 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13185#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013186 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013187 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013188 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013189 if (get_option_tv(&varname, rettv, 1) == OK)
13190 done = TRUE;
13191 }
13192 else
13193 {
13194 /* Look up the variable. */
13195 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13196 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13197 varname, FALSE);
13198 if (v != NULL)
13199 {
13200 copy_tv(&v->di_tv, rettv);
13201 done = TRUE;
13202 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013205
Bram Moolenaarba117c22015-09-29 16:53:22 +020013206#ifdef FEAT_WINDOWS
13207 if (need_switch_win)
13208 /* restore previous notion of curwin */
13209 restore_win(oldcurwin, oldtabpage, TRUE);
13210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 }
13212
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013213 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13214 /* use the default return value */
13215 copy_tv(&argvars[off + 2], rettv);
13216
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 --emsg_off;
13218}
13219
13220/*
13221 * "glob()" function
13222 */
13223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013224f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013226 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013228 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013230 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013231 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013233 if (argvars[1].v_type != VAR_UNKNOWN)
13234 {
13235 if (get_tv_number_chk(&argvars[1], &error))
13236 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013237 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013238 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013239 if (get_tv_number_chk(&argvars[2], &error))
13240 {
13241 rettv->v_type = VAR_LIST;
13242 rettv->vval.v_list = NULL;
13243 }
13244 if (argvars[3].v_type != VAR_UNKNOWN
13245 && get_tv_number_chk(&argvars[3], &error))
13246 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013247 }
13248 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013249 if (!error)
13250 {
13251 ExpandInit(&xpc);
13252 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013253 if (p_wic)
13254 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013255 if (rettv->v_type == VAR_STRING)
13256 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013257 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013258 else if (rettv_list_alloc(rettv) != FAIL)
13259 {
13260 int i;
13261
13262 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13263 NULL, options, WILD_ALL_KEEP);
13264 for (i = 0; i < xpc.xp_numfiles; i++)
13265 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13266
13267 ExpandCleanup(&xpc);
13268 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013269 }
13270 else
13271 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272}
13273
13274/*
13275 * "globpath()" function
13276 */
13277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013278f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013280 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013282 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013283 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013284 garray_T ga;
13285 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013287 /* When the optional second argument is non-zero, don't remove matches
13288 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013289 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013290 if (argvars[2].v_type != VAR_UNKNOWN)
13291 {
13292 if (get_tv_number_chk(&argvars[2], &error))
13293 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013294 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013295 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013296 if (get_tv_number_chk(&argvars[3], &error))
13297 {
13298 rettv->v_type = VAR_LIST;
13299 rettv->vval.v_list = NULL;
13300 }
13301 if (argvars[4].v_type != VAR_UNKNOWN
13302 && get_tv_number_chk(&argvars[4], &error))
13303 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013304 }
13305 }
13306 if (file != NULL && !error)
13307 {
13308 ga_init2(&ga, (int)sizeof(char_u *), 10);
13309 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13310 if (rettv->v_type == VAR_STRING)
13311 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13312 else if (rettv_list_alloc(rettv) != FAIL)
13313 for (i = 0; i < ga.ga_len; ++i)
13314 list_append_string(rettv->vval.v_list,
13315 ((char_u **)(ga.ga_data))[i], -1);
13316 ga_clear_strings(&ga);
13317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013318 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013319 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320}
13321
13322/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013323 * "glob2regpat()" function
13324 */
13325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013326f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013327{
13328 char_u *pat = get_tv_string_chk(&argvars[0]);
13329
13330 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013331 rettv->vval.v_string = (pat == NULL)
13332 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013333}
13334
13335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 * "has()" function
13337 */
13338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013339f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340{
13341 int i;
13342 char_u *name;
13343 int n = FALSE;
13344 static char *(has_list[]) =
13345 {
13346#ifdef AMIGA
13347 "amiga",
13348# ifdef FEAT_ARP
13349 "arp",
13350# endif
13351#endif
13352#ifdef __BEOS__
13353 "beos",
13354#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013355#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 "mac",
13357#endif
13358#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013359 "macunix", /* built with 'darwin' enabled */
13360#endif
13361#if defined(__APPLE__) && __APPLE__ == 1
13362 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364#ifdef __QNX__
13365 "qnx",
13366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367#ifdef UNIX
13368 "unix",
13369#endif
13370#ifdef VMS
13371 "vms",
13372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373#ifdef WIN32
13374 "win32",
13375#endif
13376#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13377 "win32unix",
13378#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013379#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 "win64",
13381#endif
13382#ifdef EBCDIC
13383 "ebcdic",
13384#endif
13385#ifndef CASE_INSENSITIVE_FILENAME
13386 "fname_case",
13387#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013388#ifdef HAVE_ACL
13389 "acl",
13390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391#ifdef FEAT_ARABIC
13392 "arabic",
13393#endif
13394#ifdef FEAT_AUTOCMD
13395 "autocmd",
13396#endif
13397#ifdef FEAT_BEVAL
13398 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013399# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13400 "balloon_multiline",
13401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402#endif
13403#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13404 "builtin_terms",
13405# ifdef ALL_BUILTIN_TCAPS
13406 "all_builtin_terms",
13407# endif
13408#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013409#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13410 || defined(FEAT_GUI_W32) \
13411 || defined(FEAT_GUI_MOTIF))
13412 "browsefilter",
13413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414#ifdef FEAT_BYTEOFF
13415 "byte_offset",
13416#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013417#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013418 "channel",
13419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420#ifdef FEAT_CINDENT
13421 "cindent",
13422#endif
13423#ifdef FEAT_CLIENTSERVER
13424 "clientserver",
13425#endif
13426#ifdef FEAT_CLIPBOARD
13427 "clipboard",
13428#endif
13429#ifdef FEAT_CMDL_COMPL
13430 "cmdline_compl",
13431#endif
13432#ifdef FEAT_CMDHIST
13433 "cmdline_hist",
13434#endif
13435#ifdef FEAT_COMMENTS
13436 "comments",
13437#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013438#ifdef FEAT_CONCEAL
13439 "conceal",
13440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441#ifdef FEAT_CRYPT
13442 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013443 "crypt-blowfish",
13444 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445#endif
13446#ifdef FEAT_CSCOPE
13447 "cscope",
13448#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013449#ifdef FEAT_CURSORBIND
13450 "cursorbind",
13451#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013452#ifdef CURSOR_SHAPE
13453 "cursorshape",
13454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455#ifdef DEBUG
13456 "debug",
13457#endif
13458#ifdef FEAT_CON_DIALOG
13459 "dialog_con",
13460#endif
13461#ifdef FEAT_GUI_DIALOG
13462 "dialog_gui",
13463#endif
13464#ifdef FEAT_DIFF
13465 "diff",
13466#endif
13467#ifdef FEAT_DIGRAPHS
13468 "digraphs",
13469#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013470#ifdef FEAT_DIRECTX
13471 "directx",
13472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473#ifdef FEAT_DND
13474 "dnd",
13475#endif
13476#ifdef FEAT_EMACS_TAGS
13477 "emacs_tags",
13478#endif
13479 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013480 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481#ifdef FEAT_SEARCH_EXTRA
13482 "extra_search",
13483#endif
13484#ifdef FEAT_FKMAP
13485 "farsi",
13486#endif
13487#ifdef FEAT_SEARCHPATH
13488 "file_in_path",
13489#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013490#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013491 "filterpipe",
13492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493#ifdef FEAT_FIND_ID
13494 "find_in_path",
13495#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013496#ifdef FEAT_FLOAT
13497 "float",
13498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499#ifdef FEAT_FOLDING
13500 "folding",
13501#endif
13502#ifdef FEAT_FOOTER
13503 "footer",
13504#endif
13505#if !defined(USE_SYSTEM) && defined(UNIX)
13506 "fork",
13507#endif
13508#ifdef FEAT_GETTEXT
13509 "gettext",
13510#endif
13511#ifdef FEAT_GUI
13512 "gui",
13513#endif
13514#ifdef FEAT_GUI_ATHENA
13515# ifdef FEAT_GUI_NEXTAW
13516 "gui_neXtaw",
13517# else
13518 "gui_athena",
13519# endif
13520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521#ifdef FEAT_GUI_GTK
13522 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013523# ifdef USE_GTK3
13524 "gui_gtk3",
13525# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013529#ifdef FEAT_GUI_GNOME
13530 "gui_gnome",
13531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532#ifdef FEAT_GUI_MAC
13533 "gui_mac",
13534#endif
13535#ifdef FEAT_GUI_MOTIF
13536 "gui_motif",
13537#endif
13538#ifdef FEAT_GUI_PHOTON
13539 "gui_photon",
13540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541#ifdef FEAT_GUI_W32
13542 "gui_win32",
13543#endif
13544#ifdef FEAT_HANGULIN
13545 "hangul_input",
13546#endif
13547#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13548 "iconv",
13549#endif
13550#ifdef FEAT_INS_EXPAND
13551 "insert_expand",
13552#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013553#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013554 "job",
13555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556#ifdef FEAT_JUMPLIST
13557 "jumplist",
13558#endif
13559#ifdef FEAT_KEYMAP
13560 "keymap",
13561#endif
13562#ifdef FEAT_LANGMAP
13563 "langmap",
13564#endif
13565#ifdef FEAT_LIBCALL
13566 "libcall",
13567#endif
13568#ifdef FEAT_LINEBREAK
13569 "linebreak",
13570#endif
13571#ifdef FEAT_LISP
13572 "lispindent",
13573#endif
13574#ifdef FEAT_LISTCMDS
13575 "listcmds",
13576#endif
13577#ifdef FEAT_LOCALMAP
13578 "localmap",
13579#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013580#ifdef FEAT_LUA
13581# ifndef DYNAMIC_LUA
13582 "lua",
13583# endif
13584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585#ifdef FEAT_MENU
13586 "menu",
13587#endif
13588#ifdef FEAT_SESSION
13589 "mksession",
13590#endif
13591#ifdef FEAT_MODIFY_FNAME
13592 "modify_fname",
13593#endif
13594#ifdef FEAT_MOUSE
13595 "mouse",
13596#endif
13597#ifdef FEAT_MOUSESHAPE
13598 "mouseshape",
13599#endif
13600#if defined(UNIX) || defined(VMS)
13601# ifdef FEAT_MOUSE_DEC
13602 "mouse_dec",
13603# endif
13604# ifdef FEAT_MOUSE_GPM
13605 "mouse_gpm",
13606# endif
13607# ifdef FEAT_MOUSE_JSB
13608 "mouse_jsbterm",
13609# endif
13610# ifdef FEAT_MOUSE_NET
13611 "mouse_netterm",
13612# endif
13613# ifdef FEAT_MOUSE_PTERM
13614 "mouse_pterm",
13615# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013616# ifdef FEAT_MOUSE_SGR
13617 "mouse_sgr",
13618# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013619# ifdef FEAT_SYSMOUSE
13620 "mouse_sysmouse",
13621# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013622# ifdef FEAT_MOUSE_URXVT
13623 "mouse_urxvt",
13624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625# ifdef FEAT_MOUSE_XTERM
13626 "mouse_xterm",
13627# endif
13628#endif
13629#ifdef FEAT_MBYTE
13630 "multi_byte",
13631#endif
13632#ifdef FEAT_MBYTE_IME
13633 "multi_byte_ime",
13634#endif
13635#ifdef FEAT_MULTI_LANG
13636 "multi_lang",
13637#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013638#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013639#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013640 "mzscheme",
13641#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643#ifdef FEAT_OLE
13644 "ole",
13645#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013646 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647#ifdef FEAT_PATH_EXTRA
13648 "path_extra",
13649#endif
13650#ifdef FEAT_PERL
13651#ifndef DYNAMIC_PERL
13652 "perl",
13653#endif
13654#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013655#ifdef FEAT_PERSISTENT_UNDO
13656 "persistent_undo",
13657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658#ifdef FEAT_PYTHON
13659#ifndef DYNAMIC_PYTHON
13660 "python",
13661#endif
13662#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013663#ifdef FEAT_PYTHON3
13664#ifndef DYNAMIC_PYTHON3
13665 "python3",
13666#endif
13667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668#ifdef FEAT_POSTSCRIPT
13669 "postscript",
13670#endif
13671#ifdef FEAT_PRINTER
13672 "printer",
13673#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013674#ifdef FEAT_PROFILE
13675 "profile",
13676#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013677#ifdef FEAT_RELTIME
13678 "reltime",
13679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680#ifdef FEAT_QUICKFIX
13681 "quickfix",
13682#endif
13683#ifdef FEAT_RIGHTLEFT
13684 "rightleft",
13685#endif
13686#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13687 "ruby",
13688#endif
13689#ifdef FEAT_SCROLLBIND
13690 "scrollbind",
13691#endif
13692#ifdef FEAT_CMDL_INFO
13693 "showcmd",
13694 "cmdline_info",
13695#endif
13696#ifdef FEAT_SIGNS
13697 "signs",
13698#endif
13699#ifdef FEAT_SMARTINDENT
13700 "smartindent",
13701#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013702#ifdef STARTUPTIME
13703 "startuptime",
13704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705#ifdef FEAT_STL_OPT
13706 "statusline",
13707#endif
13708#ifdef FEAT_SUN_WORKSHOP
13709 "sun_workshop",
13710#endif
13711#ifdef FEAT_NETBEANS_INTG
13712 "netbeans_intg",
13713#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013714#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013715 "spell",
13716#endif
13717#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 "syntax",
13719#endif
13720#if defined(USE_SYSTEM) || !defined(UNIX)
13721 "system",
13722#endif
13723#ifdef FEAT_TAG_BINS
13724 "tag_binary",
13725#endif
13726#ifdef FEAT_TAG_OLDSTATIC
13727 "tag_old_static",
13728#endif
13729#ifdef FEAT_TAG_ANYWHITE
13730 "tag_any_white",
13731#endif
13732#ifdef FEAT_TCL
13733# ifndef DYNAMIC_TCL
13734 "tcl",
13735# endif
13736#endif
13737#ifdef TERMINFO
13738 "terminfo",
13739#endif
13740#ifdef FEAT_TERMRESPONSE
13741 "termresponse",
13742#endif
13743#ifdef FEAT_TEXTOBJ
13744 "textobjects",
13745#endif
13746#ifdef HAVE_TGETENT
13747 "tgetent",
13748#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013749#ifdef FEAT_TIMERS
13750 "timers",
13751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752#ifdef FEAT_TITLE
13753 "title",
13754#endif
13755#ifdef FEAT_TOOLBAR
13756 "toolbar",
13757#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013758#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13759 "unnamedplus",
13760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761#ifdef FEAT_USR_CMDS
13762 "user-commands", /* was accidentally included in 5.4 */
13763 "user_commands",
13764#endif
13765#ifdef FEAT_VIMINFO
13766 "viminfo",
13767#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013768#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 "vertsplit",
13770#endif
13771#ifdef FEAT_VIRTUALEDIT
13772 "virtualedit",
13773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775#ifdef FEAT_VISUALEXTRA
13776 "visualextra",
13777#endif
13778#ifdef FEAT_VREPLACE
13779 "vreplace",
13780#endif
13781#ifdef FEAT_WILDIGN
13782 "wildignore",
13783#endif
13784#ifdef FEAT_WILDMENU
13785 "wildmenu",
13786#endif
13787#ifdef FEAT_WINDOWS
13788 "windows",
13789#endif
13790#ifdef FEAT_WAK
13791 "winaltkeys",
13792#endif
13793#ifdef FEAT_WRITEBACKUP
13794 "writebackup",
13795#endif
13796#ifdef FEAT_XIM
13797 "xim",
13798#endif
13799#ifdef FEAT_XFONTSET
13800 "xfontset",
13801#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013802#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013803 "xpm",
13804 "xpm_w32", /* for backward compatibility */
13805#else
13806# if defined(HAVE_XPM)
13807 "xpm",
13808# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810#ifdef USE_XSMP
13811 "xsmp",
13812#endif
13813#ifdef USE_XSMP_INTERACT
13814 "xsmp_interact",
13815#endif
13816#ifdef FEAT_XCLIPBOARD
13817 "xterm_clipboard",
13818#endif
13819#ifdef FEAT_XTERM_SAVE
13820 "xterm_save",
13821#endif
13822#if defined(UNIX) && defined(FEAT_X11)
13823 "X11",
13824#endif
13825 NULL
13826 };
13827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013828 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 for (i = 0; has_list[i] != NULL; ++i)
13830 if (STRICMP(name, has_list[i]) == 0)
13831 {
13832 n = TRUE;
13833 break;
13834 }
13835
13836 if (n == FALSE)
13837 {
13838 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013839 {
13840 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010013841 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013842 && vim_isdigit(name[6])
13843 && vim_isdigit(name[8])
13844 && vim_isdigit(name[10]))
13845 {
13846 int major = atoi((char *)name + 6);
13847 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013848
13849 /* Expect "patch-9.9.01234". */
13850 n = (major < VIM_VERSION_MAJOR
13851 || (major == VIM_VERSION_MAJOR
13852 && (minor < VIM_VERSION_MINOR
13853 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013854 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013855 }
13856 else
13857 n = has_patch(atoi((char *)name + 5));
13858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859 else if (STRICMP(name, "vim_starting") == 0)
13860 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013861#ifdef FEAT_MBYTE
13862 else if (STRICMP(name, "multi_byte_encoding") == 0)
13863 n = has_mbyte;
13864#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013865#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13866 else if (STRICMP(name, "balloon_multiline") == 0)
13867 n = multiline_balloon_available();
13868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869#ifdef DYNAMIC_TCL
13870 else if (STRICMP(name, "tcl") == 0)
13871 n = tcl_enabled(FALSE);
13872#endif
13873#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13874 else if (STRICMP(name, "iconv") == 0)
13875 n = iconv_enabled(FALSE);
13876#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013877#ifdef DYNAMIC_LUA
13878 else if (STRICMP(name, "lua") == 0)
13879 n = lua_enabled(FALSE);
13880#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013881#ifdef DYNAMIC_MZSCHEME
13882 else if (STRICMP(name, "mzscheme") == 0)
13883 n = mzscheme_enabled(FALSE);
13884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885#ifdef DYNAMIC_RUBY
13886 else if (STRICMP(name, "ruby") == 0)
13887 n = ruby_enabled(FALSE);
13888#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013889#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890#ifdef DYNAMIC_PYTHON
13891 else if (STRICMP(name, "python") == 0)
13892 n = python_enabled(FALSE);
13893#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013894#endif
13895#ifdef FEAT_PYTHON3
13896#ifdef DYNAMIC_PYTHON3
13897 else if (STRICMP(name, "python3") == 0)
13898 n = python3_enabled(FALSE);
13899#endif
13900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901#ifdef DYNAMIC_PERL
13902 else if (STRICMP(name, "perl") == 0)
13903 n = perl_enabled(FALSE);
13904#endif
13905#ifdef FEAT_GUI
13906 else if (STRICMP(name, "gui_running") == 0)
13907 n = (gui.in_use || gui.starting);
13908# ifdef FEAT_GUI_W32
13909 else if (STRICMP(name, "gui_win32s") == 0)
13910 n = gui_is_win32s();
13911# endif
13912# ifdef FEAT_BROWSE
13913 else if (STRICMP(name, "browse") == 0)
13914 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13915# endif
13916#endif
13917#ifdef FEAT_SYN_HL
13918 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013919 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920#endif
13921#if defined(WIN3264)
13922 else if (STRICMP(name, "win95") == 0)
13923 n = mch_windows95();
13924#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013925#ifdef FEAT_NETBEANS_INTG
13926 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013927 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 }
13930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013931 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932}
13933
13934/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013935 * "has_key()" function
13936 */
13937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013938f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013939{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013940 if (argvars[0].v_type != VAR_DICT)
13941 {
13942 EMSG(_(e_dictreq));
13943 return;
13944 }
13945 if (argvars[0].vval.v_dict == NULL)
13946 return;
13947
13948 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013949 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013950}
13951
13952/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013953 * "haslocaldir()" function
13954 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013956f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013957{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013958 win_T *wp = NULL;
13959
13960 wp = find_tabwin(&argvars[0], &argvars[1]);
13961 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013962}
13963
13964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 * "hasmapto()" function
13966 */
13967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013968f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969{
13970 char_u *name;
13971 char_u *mode;
13972 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013973 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013976 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 mode = (char_u *)"nvo";
13978 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013980 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013981 if (argvars[2].v_type != VAR_UNKNOWN)
13982 abbr = get_tv_number(&argvars[2]);
13983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984
Bram Moolenaar2c932302006-03-18 21:42:09 +000013985 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013986 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013988 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989}
13990
13991/*
13992 * "histadd()" function
13993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013995f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996{
13997#ifdef FEAT_CMDHIST
13998 int histype;
13999 char_u *str;
14000 char_u buf[NUMBUFLEN];
14001#endif
14002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014003 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004 if (check_restricted() || check_secure())
14005 return;
14006#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014007 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14008 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 if (histype >= 0)
14010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014011 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 if (*str != NUL)
14013 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014014 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014016 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 return;
14018 }
14019 }
14020#endif
14021}
14022
14023/*
14024 * "histdel()" function
14025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014027f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028{
14029#ifdef FEAT_CMDHIST
14030 int n;
14031 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014034 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14035 if (str == NULL)
14036 n = 0;
14037 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014039 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014040 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 else
14045 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014046 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 get_tv_string_buf(&argvars[1], buf));
14048 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049#endif
14050}
14051
14052/*
14053 * "histget()" function
14054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014056f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057{
14058#ifdef FEAT_CMDHIST
14059 int type;
14060 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014061 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014063 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14064 if (str == NULL)
14065 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014067 {
14068 type = get_histtype(str);
14069 if (argvars[1].v_type == VAR_UNKNOWN)
14070 idx = get_history_idx(type);
14071 else
14072 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14073 /* -1 on type error */
14074 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080}
14081
14082/*
14083 * "histnr()" function
14084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014086f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087{
14088 int i;
14089
14090#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 char_u *history = get_tv_string_chk(&argvars[0]);
14092
14093 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 if (i >= HIST_CMD && i < HIST_COUNT)
14095 i = get_history_idx(i);
14096 else
14097#endif
14098 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100}
14101
14102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 * "highlightID(name)" function
14104 */
14105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014106f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014108 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109}
14110
14111/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112 * "highlight_exists()" function
14113 */
14114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014115f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116{
14117 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14118}
14119
14120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 * "hostname()" function
14122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014124f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125{
14126 char_u hostname[256];
14127
14128 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 rettv->v_type = VAR_STRING;
14130 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131}
14132
14133/*
14134 * iconv() function
14135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014137f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138{
14139#ifdef FEAT_MBYTE
14140 char_u buf1[NUMBUFLEN];
14141 char_u buf2[NUMBUFLEN];
14142 char_u *from, *to, *str;
14143 vimconv_T vimconv;
14144#endif
14145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146 rettv->v_type = VAR_STRING;
14147 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148
14149#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150 str = get_tv_string(&argvars[0]);
14151 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14152 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 vimconv.vc_type = CONV_NONE;
14154 convert_setup(&vimconv, from, to);
14155
14156 /* If the encodings are equal, no conversion needed. */
14157 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014158 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161
14162 convert_setup(&vimconv, NULL, NULL);
14163 vim_free(from);
14164 vim_free(to);
14165#endif
14166}
14167
14168/*
14169 * "indent()" function
14170 */
14171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014172f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173{
14174 linenr_T lnum;
14175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014178 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014180 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181}
14182
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014183/*
14184 * "index()" function
14185 */
14186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014187f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014188{
Bram Moolenaar33570922005-01-25 22:26:29 +000014189 list_T *l;
14190 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014191 long idx = 0;
14192 int ic = FALSE;
14193
14194 rettv->vval.v_number = -1;
14195 if (argvars[0].v_type != VAR_LIST)
14196 {
14197 EMSG(_(e_listreq));
14198 return;
14199 }
14200 l = argvars[0].vval.v_list;
14201 if (l != NULL)
14202 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014203 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014204 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014206 int error = FALSE;
14207
Bram Moolenaar758711c2005-02-02 23:11:38 +000014208 /* Start at specified item. Use the cached index that list_find()
14209 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014210 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014211 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014212 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 ic = get_tv_number_chk(&argvars[3], &error);
14214 if (error)
14215 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014216 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014217
Bram Moolenaar758711c2005-02-02 23:11:38 +000014218 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014219 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014220 {
14221 rettv->vval.v_number = idx;
14222 break;
14223 }
14224 }
14225}
14226
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227static int inputsecret_flag = 0;
14228
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014229static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014230
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014232 * This function is used by f_input() and f_inputdialog() functions. The third
14233 * argument to f_input() specifies the type of completion to use at the
14234 * prompt. The third argument to f_inputdialog() specifies the value to return
14235 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 */
14237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014238get_user_input(
14239 typval_T *argvars,
14240 typval_T *rettv,
14241 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 char_u *p = NULL;
14245 int c;
14246 char_u buf[NUMBUFLEN];
14247 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014249 int xp_type = EXPAND_NOTHING;
14250 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014252 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014253 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254
14255#ifdef NO_CONSOLE_INPUT
14256 /* While starting up, there is no place to enter text. */
14257 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259#endif
14260
14261 cmd_silent = FALSE; /* Want to see the prompt. */
14262 if (prompt != NULL)
14263 {
14264 /* Only the part of the message after the last NL is considered as
14265 * prompt for the command line */
14266 p = vim_strrchr(prompt, '\n');
14267 if (p == NULL)
14268 p = prompt;
14269 else
14270 {
14271 ++p;
14272 c = *p;
14273 *p = NUL;
14274 msg_start();
14275 msg_clr_eos();
14276 msg_puts_attr(prompt, echo_attr);
14277 msg_didout = FALSE;
14278 msg_starthere();
14279 *p = c;
14280 }
14281 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283 if (argvars[1].v_type != VAR_UNKNOWN)
14284 {
14285 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14286 if (defstr != NULL)
14287 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014289 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014290 {
14291 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014292 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014293 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014294
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014295 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014296 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014297
Bram Moolenaar4463f292005-09-25 22:20:24 +000014298 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14299 if (xp_name == NULL)
14300 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014301
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014302 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014303
Bram Moolenaar4463f292005-09-25 22:20:24 +000014304 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14305 &xp_arg) == FAIL)
14306 return;
14307 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014308 }
14309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014310 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014311 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014312 int save_ex_normal_busy = ex_normal_busy;
14313 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014314 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014315 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14316 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014317 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014318 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014319 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014320 && argvars[1].v_type != VAR_UNKNOWN
14321 && argvars[2].v_type != VAR_UNKNOWN)
14322 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14323 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014324
14325 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014327 /* since the user typed this, no need to wait for return */
14328 need_wait_return = FALSE;
14329 msg_didout = FALSE;
14330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 cmd_silent = cmd_silent_save;
14332}
14333
14334/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014335 * "input()" function
14336 * Also handles inputsecret() when inputsecret is set.
14337 */
14338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014339f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014340{
14341 get_user_input(argvars, rettv, FALSE);
14342}
14343
14344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 * "inputdialog()" function
14346 */
14347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014348f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349{
14350#if defined(FEAT_GUI_TEXTDIALOG)
14351 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14352 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14353 {
14354 char_u *message;
14355 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014356 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014358 message = get_tv_string_chk(&argvars[0]);
14359 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014360 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014361 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 else
14363 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 if (message != NULL && defstr != NULL
14365 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014366 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 else
14369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 if (message != NULL && defstr != NULL
14371 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014372 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373 rettv->vval.v_string = vim_strsave(
14374 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014378 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379 }
14380 else
14381#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014382 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383}
14384
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014385/*
14386 * "inputlist()" function
14387 */
14388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014389f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014390{
14391 listitem_T *li;
14392 int selected;
14393 int mouse_used;
14394
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014395#ifdef NO_CONSOLE_INPUT
14396 /* While starting up, there is no place to enter text. */
14397 if (no_console_input())
14398 return;
14399#endif
14400 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14401 {
14402 EMSG2(_(e_listarg), "inputlist()");
14403 return;
14404 }
14405
14406 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014407 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014408 lines_left = Rows; /* avoid more prompt */
14409 msg_scroll = TRUE;
14410 msg_clr_eos();
14411
14412 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14413 {
14414 msg_puts(get_tv_string(&li->li_tv));
14415 msg_putchar('\n');
14416 }
14417
14418 /* Ask for choice. */
14419 selected = prompt_for_number(&mouse_used);
14420 if (mouse_used)
14421 selected -= lines_left;
14422
14423 rettv->vval.v_number = selected;
14424}
14425
14426
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14428
14429/*
14430 * "inputrestore()" function
14431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014433f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434{
14435 if (ga_userinput.ga_len > 0)
14436 {
14437 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14439 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014440 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 }
14442 else if (p_verbose > 1)
14443 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014444 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 }
14447}
14448
14449/*
14450 * "inputsave()" function
14451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014453f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014455 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 if (ga_grow(&ga_userinput, 1) == OK)
14457 {
14458 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14459 + ga_userinput.ga_len);
14460 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014461 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 }
14463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465}
14466
14467/*
14468 * "inputsecret()" function
14469 */
14470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014471f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472{
14473 ++cmdline_star;
14474 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 --cmdline_star;
14477 --inputsecret_flag;
14478}
14479
14480/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014481 * "insert()" function
14482 */
14483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014484f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014485{
14486 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014487 listitem_T *item;
14488 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014489 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014490
14491 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014492 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014493 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014494 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014495 {
14496 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 before = get_tv_number_chk(&argvars[2], &error);
14498 if (error)
14499 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014500
Bram Moolenaar758711c2005-02-02 23:11:38 +000014501 if (before == l->lv_len)
14502 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014503 else
14504 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014505 item = list_find(l, before);
14506 if (item == NULL)
14507 {
14508 EMSGN(_(e_listidx), before);
14509 l = NULL;
14510 }
14511 }
14512 if (l != NULL)
14513 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014514 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014515 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014516 }
14517 }
14518}
14519
14520/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014521 * "invert(expr)" function
14522 */
14523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014524f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014525{
14526 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14527}
14528
14529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 * "isdirectory()" function
14531 */
14532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014533f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536}
14537
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014538/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014539 * "islocked()" function
14540 */
14541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014542f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014543{
14544 lval_T lv;
14545 char_u *end;
14546 dictitem_T *di;
14547
14548 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014549 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14550 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014551 if (end != NULL && lv.ll_name != NULL)
14552 {
14553 if (*end != NUL)
14554 EMSG(_(e_trailing));
14555 else
14556 {
14557 if (lv.ll_tv == NULL)
14558 {
14559 if (check_changedtick(lv.ll_name))
14560 rettv->vval.v_number = 1; /* always locked */
14561 else
14562 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014563 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014564 if (di != NULL)
14565 {
14566 /* Consider a variable locked when:
14567 * 1. the variable itself is locked
14568 * 2. the value of the variable is locked.
14569 * 3. the List or Dict value is locked.
14570 */
14571 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14572 || tv_islocked(&di->di_tv));
14573 }
14574 }
14575 }
14576 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014577 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014578 else if (lv.ll_newkey != NULL)
14579 EMSG2(_(e_dictkey), lv.ll_newkey);
14580 else if (lv.ll_list != NULL)
14581 /* List item. */
14582 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14583 else
14584 /* Dictionary item. */
14585 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14586 }
14587 }
14588
14589 clear_lval(&lv);
14590}
14591
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014592#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14593/*
14594 * "isnan()" function
14595 */
14596 static void
14597f_isnan(typval_T *argvars, typval_T *rettv)
14598{
14599 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14600 && isnan(argvars[0].vval.v_float);
14601}
14602#endif
14603
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014604static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014605
14606/*
14607 * Turn a dict into a list:
14608 * "what" == 0: list of keys
14609 * "what" == 1: list of values
14610 * "what" == 2: list of items
14611 */
14612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014613dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014614{
Bram Moolenaar33570922005-01-25 22:26:29 +000014615 list_T *l2;
14616 dictitem_T *di;
14617 hashitem_T *hi;
14618 listitem_T *li;
14619 listitem_T *li2;
14620 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014621 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622
Bram Moolenaar8c711452005-01-14 21:53:12 +000014623 if (argvars[0].v_type != VAR_DICT)
14624 {
14625 EMSG(_(e_dictreq));
14626 return;
14627 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014628 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014629 return;
14630
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014631 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014632 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014634 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014635 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014636 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014637 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014639 --todo;
14640 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014641
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014642 li = listitem_alloc();
14643 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014644 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014645 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014646
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014647 if (what == 0)
14648 {
14649 /* keys() */
14650 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014651 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014652 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14653 }
14654 else if (what == 1)
14655 {
14656 /* values() */
14657 copy_tv(&di->di_tv, &li->li_tv);
14658 }
14659 else
14660 {
14661 /* items() */
14662 l2 = list_alloc();
14663 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014664 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014665 li->li_tv.vval.v_list = l2;
14666 if (l2 == NULL)
14667 break;
14668 ++l2->lv_refcount;
14669
14670 li2 = listitem_alloc();
14671 if (li2 == NULL)
14672 break;
14673 list_append(l2, li2);
14674 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014675 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014676 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14677
14678 li2 = listitem_alloc();
14679 if (li2 == NULL)
14680 break;
14681 list_append(l2, li2);
14682 copy_tv(&di->di_tv, &li2->li_tv);
14683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014684 }
14685 }
14686}
14687
14688/*
14689 * "items(dict)" function
14690 */
14691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014692f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014693{
14694 dict_list(argvars, rettv, 2);
14695}
14696
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014697#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014698/*
14699 * Get the job from the argument.
14700 * Returns NULL if the job is invalid.
14701 */
14702 static job_T *
14703get_job_arg(typval_T *tv)
14704{
14705 job_T *job;
14706
14707 if (tv->v_type != VAR_JOB)
14708 {
14709 EMSG2(_(e_invarg2), get_tv_string(tv));
14710 return NULL;
14711 }
14712 job = tv->vval.v_job;
14713
14714 if (job == NULL)
14715 EMSG(_("E916: not a valid job"));
14716 return job;
14717}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014718
Bram Moolenaar835dc632016-02-07 14:27:38 +010014719/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014720 * "job_getchannel()" function
14721 */
14722 static void
14723f_job_getchannel(typval_T *argvars, typval_T *rettv)
14724{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014725 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014726
Bram Moolenaar65edff82016-02-21 16:40:11 +010014727 if (job != NULL)
14728 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014729 rettv->v_type = VAR_CHANNEL;
14730 rettv->vval.v_channel = job->jv_channel;
14731 if (job->jv_channel != NULL)
14732 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014733 }
14734}
14735
14736/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014737 * "job_info()" function
14738 */
14739 static void
14740f_job_info(typval_T *argvars, typval_T *rettv)
14741{
14742 job_T *job = get_job_arg(&argvars[0]);
14743
14744 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14745 job_info(job, rettv->vval.v_dict);
14746}
14747
14748/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014749 * "job_setoptions()" function
14750 */
14751 static void
14752f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14753{
14754 job_T *job = get_job_arg(&argvars[0]);
14755 jobopt_T opt;
14756
14757 if (job == NULL)
14758 return;
14759 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014760 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014761 return;
14762 job_set_options(job, &opt);
14763}
14764
14765/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014766 * "job_start()" function
14767 */
14768 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014769f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014770{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014771 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014772 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014773}
14774
14775/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014776 * "job_status()" function
14777 */
14778 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014779f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014780{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014781 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014782
Bram Moolenaar65edff82016-02-21 16:40:11 +010014783 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014784 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014785 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014786 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014787 }
14788}
14789
14790/*
14791 * "job_stop()" function
14792 */
14793 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014794f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014795{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014796 job_T *job = get_job_arg(&argvars[0]);
14797
14798 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014799 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014800}
14801#endif
14802
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014804 * "join()" function
14805 */
14806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014807f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014808{
14809 garray_T ga;
14810 char_u *sep;
14811
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014812 if (argvars[0].v_type != VAR_LIST)
14813 {
14814 EMSG(_(e_listreq));
14815 return;
14816 }
14817 if (argvars[0].vval.v_list == NULL)
14818 return;
14819 if (argvars[1].v_type == VAR_UNKNOWN)
14820 sep = (char_u *)" ";
14821 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014822 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014823
14824 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014825
14826 if (sep != NULL)
14827 {
14828 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014829 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014830 ga_append(&ga, NUL);
14831 rettv->vval.v_string = (char_u *)ga.ga_data;
14832 }
14833 else
14834 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014835}
14836
14837/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014838 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014839 */
14840 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014841f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014842{
14843 js_read_T reader;
14844
14845 reader.js_buf = get_tv_string(&argvars[0]);
14846 reader.js_fill = NULL;
14847 reader.js_used = 0;
14848 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14849 EMSG(_(e_invarg));
14850}
14851
14852/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014853 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014854 */
14855 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014856f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014857{
14858 rettv->v_type = VAR_STRING;
14859 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14860}
14861
14862/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014863 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014864 */
14865 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014866f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014867{
14868 js_read_T reader;
14869
14870 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014871 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014872 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014873 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014874 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014875}
14876
14877/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014878 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014879 */
14880 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014881f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014882{
14883 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014884 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014885}
14886
14887/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014888 * "keys()" function
14889 */
14890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014891f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014892{
14893 dict_list(argvars, rettv, 0);
14894}
14895
14896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 * "last_buffer_nr()" function.
14898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014900f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901{
14902 int n = 0;
14903 buf_T *buf;
14904
14905 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14906 if (n < buf->b_fnum)
14907 n = buf->b_fnum;
14908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014910}
14911
14912/*
14913 * "len()" function
14914 */
14915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014916f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014917{
14918 switch (argvars[0].v_type)
14919 {
14920 case VAR_STRING:
14921 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922 rettv->vval.v_number = (varnumber_T)STRLEN(
14923 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014924 break;
14925 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014926 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014927 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014928 case VAR_DICT:
14929 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14930 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014931 case VAR_UNKNOWN:
14932 case VAR_SPECIAL:
14933 case VAR_FLOAT:
14934 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014935 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014936 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014937 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014938 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014939 break;
14940 }
14941}
14942
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014943static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014944
14945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014946libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014947{
14948#ifdef FEAT_LIBCALL
14949 char_u *string_in;
14950 char_u **string_result;
14951 int nr_result;
14952#endif
14953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014954 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014955 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014956 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014957
14958 if (check_restricted() || check_secure())
14959 return;
14960
14961#ifdef FEAT_LIBCALL
14962 /* The first two args must be strings, otherwise its meaningless */
14963 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14964 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014965 string_in = NULL;
14966 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014967 string_in = argvars[2].vval.v_string;
14968 if (type == VAR_NUMBER)
14969 string_result = NULL;
14970 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014972 if (mch_libcall(argvars[0].vval.v_string,
14973 argvars[1].vval.v_string,
14974 string_in,
14975 argvars[2].vval.v_number,
14976 string_result,
14977 &nr_result) == OK
14978 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014980 }
14981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982}
14983
14984/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985 * "libcall()" function
14986 */
14987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014988f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014989{
14990 libcall_common(argvars, rettv, VAR_STRING);
14991}
14992
14993/*
14994 * "libcallnr()" function
14995 */
14996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014997f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014998{
14999 libcall_common(argvars, rettv, VAR_NUMBER);
15000}
15001
15002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003 * "line(string)" function
15004 */
15005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015006f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007{
15008 linenr_T lnum = 0;
15009 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015010 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015012 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 if (fp != NULL)
15014 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016}
15017
15018/*
15019 * "line2byte(lnum)" function
15020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015022f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023{
15024#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015025 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026#else
15027 linenr_T lnum;
15028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015029 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015033 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15034 if (rettv->vval.v_number >= 0)
15035 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036#endif
15037}
15038
15039/*
15040 * "lispindent(lnum)" function
15041 */
15042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015043f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044{
15045#ifdef FEAT_LISP
15046 pos_T pos;
15047 linenr_T lnum;
15048
15049 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15052 {
15053 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015054 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 curwin->w_cursor = pos;
15056 }
15057 else
15058#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015059 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060}
15061
15062/*
15063 * "localtime()" function
15064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015066f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069}
15070
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015071static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072
15073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015074get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075{
15076 char_u *keys;
15077 char_u *which;
15078 char_u buf[NUMBUFLEN];
15079 char_u *keys_buf = NULL;
15080 char_u *rhs;
15081 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015082 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015083 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015084 mapblock_T *mp;
15085 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086
15087 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015088 rettv->v_type = VAR_STRING;
15089 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015091 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092 if (*keys == NUL)
15093 return;
15094
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015095 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015097 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015098 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015099 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015100 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015101 if (argvars[3].v_type != VAR_UNKNOWN)
15102 get_dict = get_tv_number(&argvars[3]);
15103 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105 else
15106 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015107 if (which == NULL)
15108 return;
15109
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 mode = get_map_mode(&which, 0);
15111
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015112 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015113 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015115
15116 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015118 /* Return a string. */
15119 if (rhs != NULL)
15120 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121
Bram Moolenaarbd743252010-10-20 21:23:33 +020015122 }
15123 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15124 {
15125 /* Return a dictionary. */
15126 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15127 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15128 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129
Bram Moolenaarbd743252010-10-20 21:23:33 +020015130 dict_add_nr_str(dict, "lhs", 0L, lhs);
15131 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15132 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15133 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15134 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15135 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15136 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015137 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015138 dict_add_nr_str(dict, "mode", 0L, mapmode);
15139
15140 vim_free(lhs);
15141 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 }
15143}
15144
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015145#ifdef FEAT_FLOAT
15146/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015147 * "log()" function
15148 */
15149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015150f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015151{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015152 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015153
15154 rettv->v_type = VAR_FLOAT;
15155 if (get_float_arg(argvars, &f) == OK)
15156 rettv->vval.v_float = log(f);
15157 else
15158 rettv->vval.v_float = 0.0;
15159}
15160
15161/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015162 * "log10()" function
15163 */
15164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015165f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015166{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015167 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015168
15169 rettv->v_type = VAR_FLOAT;
15170 if (get_float_arg(argvars, &f) == OK)
15171 rettv->vval.v_float = log10(f);
15172 else
15173 rettv->vval.v_float = 0.0;
15174}
15175#endif
15176
Bram Moolenaar1dced572012-04-05 16:54:08 +020015177#ifdef FEAT_LUA
15178/*
15179 * "luaeval()" function
15180 */
15181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015182f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015183{
15184 char_u *str;
15185 char_u buf[NUMBUFLEN];
15186
15187 str = get_tv_string_buf(&argvars[0], buf);
15188 do_luaeval(str, argvars + 1, rettv);
15189}
15190#endif
15191
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015193 * "map()" function
15194 */
15195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015196f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015197{
15198 filter_map(argvars, rettv, TRUE);
15199}
15200
15201/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015202 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 */
15204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015205f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015207 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208}
15209
15210/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212 */
15213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015214f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217}
15218
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015219static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220
15221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015222find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015224 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015225 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015226 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227 char_u *pat;
15228 regmatch_T regmatch;
15229 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015230 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231 char_u *save_cpo;
15232 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015233 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015234 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015235 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015236 list_T *l = NULL;
15237 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015238 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015239 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240
15241 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15242 save_cpo = p_cpo;
15243 p_cpo = (char_u *)"";
15244
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015245 rettv->vval.v_number = -1;
15246 if (type == 3)
15247 {
15248 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015249 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015250 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015251 }
15252 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015254 rettv->v_type = VAR_STRING;
15255 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015258 if (argvars[0].v_type == VAR_LIST)
15259 {
15260 if ((l = argvars[0].vval.v_list) == NULL)
15261 goto theend;
15262 li = l->lv_first;
15263 }
15264 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015265 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015266 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015267 len = (long)STRLEN(str);
15268 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015270 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15271 if (pat == NULL)
15272 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015273
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015274 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015276 int error = FALSE;
15277
15278 start = get_tv_number_chk(&argvars[2], &error);
15279 if (error)
15280 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015281 if (l != NULL)
15282 {
15283 li = list_find(l, start);
15284 if (li == NULL)
15285 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015286 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015287 }
15288 else
15289 {
15290 if (start < 0)
15291 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015292 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015293 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015294 /* When "count" argument is there ignore matches before "start",
15295 * otherwise skip part of the string. Differs when pattern is "^"
15296 * or "\<". */
15297 if (argvars[3].v_type != VAR_UNKNOWN)
15298 startcol = start;
15299 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015300 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015301 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015302 len -= start;
15303 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015304 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015305
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015306 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015307 nth = get_tv_number_chk(&argvars[3], &error);
15308 if (error)
15309 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 }
15311
15312 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15313 if (regmatch.regprog != NULL)
15314 {
15315 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015316
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015317 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015318 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015319 if (l != NULL)
15320 {
15321 if (li == NULL)
15322 {
15323 match = FALSE;
15324 break;
15325 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015326 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015327 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015328 if (str == NULL)
15329 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015330 }
15331
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015332 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015333
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015334 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015335 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015336 if (l == NULL && !match)
15337 break;
15338
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015339 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015340 if (l != NULL)
15341 {
15342 li = li->li_next;
15343 ++idx;
15344 }
15345 else
15346 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015347#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015348 startcol = (colnr_T)(regmatch.startp[0]
15349 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015350#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015351 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015352#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015353 if (startcol > (colnr_T)len
15354 || str + startcol <= regmatch.startp[0])
15355 {
15356 match = FALSE;
15357 break;
15358 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015359 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015360 }
15361
15362 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015366 int i;
15367
15368 /* return list with matched string and submatches */
15369 for (i = 0; i < NSUBEXP; ++i)
15370 {
15371 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015372 {
15373 if (list_append_string(rettv->vval.v_list,
15374 (char_u *)"", 0) == FAIL)
15375 break;
15376 }
15377 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015378 regmatch.startp[i],
15379 (int)(regmatch.endp[i] - regmatch.startp[i]))
15380 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015381 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015382 }
15383 }
15384 else if (type == 2)
15385 {
15386 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015387 if (l != NULL)
15388 copy_tv(&li->li_tv, rettv);
15389 else
15390 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015392 }
15393 else if (l != NULL)
15394 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 else
15396 {
15397 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015398 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 (varnumber_T)(regmatch.startp[0] - str);
15400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015403 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404 }
15405 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015406 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 }
15408
15409theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015410 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 p_cpo = save_cpo;
15412}
15413
15414/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015415 * "match()" function
15416 */
15417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015418f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015419{
15420 find_some_match(argvars, rettv, 1);
15421}
15422
15423/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015424 * "matchadd()" function
15425 */
15426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015427f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015428{
15429#ifdef FEAT_SEARCH_EXTRA
15430 char_u buf[NUMBUFLEN];
15431 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15432 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15433 int prio = 10; /* default priority */
15434 int id = -1;
15435 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015436 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015437
15438 rettv->vval.v_number = -1;
15439
15440 if (grp == NULL || pat == NULL)
15441 return;
15442 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015443 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015444 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015445 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015446 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015447 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015448 if (argvars[4].v_type != VAR_UNKNOWN)
15449 {
15450 if (argvars[4].v_type != VAR_DICT)
15451 {
15452 EMSG(_(e_dictreq));
15453 return;
15454 }
15455 if (dict_find(argvars[4].vval.v_dict,
15456 (char_u *)"conceal", -1) != NULL)
15457 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15458 (char_u *)"conceal", FALSE);
15459 }
15460 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015461 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015462 if (error == TRUE)
15463 return;
15464 if (id >= 1 && id <= 3)
15465 {
15466 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15467 return;
15468 }
15469
Bram Moolenaar6561d522015-07-21 15:48:27 +020015470 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15471 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015472#endif
15473}
15474
15475/*
15476 * "matchaddpos()" function
15477 */
15478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015479f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015480{
15481#ifdef FEAT_SEARCH_EXTRA
15482 char_u buf[NUMBUFLEN];
15483 char_u *group;
15484 int prio = 10;
15485 int id = -1;
15486 int error = FALSE;
15487 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015488 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015489
15490 rettv->vval.v_number = -1;
15491
15492 group = get_tv_string_buf_chk(&argvars[0], buf);
15493 if (group == NULL)
15494 return;
15495
15496 if (argvars[1].v_type != VAR_LIST)
15497 {
15498 EMSG2(_(e_listarg), "matchaddpos()");
15499 return;
15500 }
15501 l = argvars[1].vval.v_list;
15502 if (l == NULL)
15503 return;
15504
15505 if (argvars[2].v_type != VAR_UNKNOWN)
15506 {
15507 prio = get_tv_number_chk(&argvars[2], &error);
15508 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015509 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015510 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015511 if (argvars[4].v_type != VAR_UNKNOWN)
15512 {
15513 if (argvars[4].v_type != VAR_DICT)
15514 {
15515 EMSG(_(e_dictreq));
15516 return;
15517 }
15518 if (dict_find(argvars[4].vval.v_dict,
15519 (char_u *)"conceal", -1) != NULL)
15520 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15521 (char_u *)"conceal", FALSE);
15522 }
15523 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015524 }
15525 if (error == TRUE)
15526 return;
15527
15528 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15529 if (id == 1 || id == 2)
15530 {
15531 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15532 return;
15533 }
15534
Bram Moolenaar6561d522015-07-21 15:48:27 +020015535 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15536 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015537#endif
15538}
15539
15540/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015541 * "matcharg()" function
15542 */
15543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015544f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015545{
15546 if (rettv_list_alloc(rettv) == OK)
15547 {
15548#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015549 int id = get_tv_number(&argvars[0]);
15550 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015551
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015552 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015553 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015554 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15555 {
15556 list_append_string(rettv->vval.v_list,
15557 syn_id2name(m->hlg_id), -1);
15558 list_append_string(rettv->vval.v_list, m->pattern, -1);
15559 }
15560 else
15561 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015562 list_append_string(rettv->vval.v_list, NULL, -1);
15563 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015564 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015565 }
15566#endif
15567 }
15568}
15569
15570/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015571 * "matchdelete()" function
15572 */
15573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015574f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015575{
15576#ifdef FEAT_SEARCH_EXTRA
15577 rettv->vval.v_number = match_delete(curwin,
15578 (int)get_tv_number(&argvars[0]), TRUE);
15579#endif
15580}
15581
15582/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015583 * "matchend()" function
15584 */
15585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015586f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015587{
15588 find_some_match(argvars, rettv, 0);
15589}
15590
15591/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015592 * "matchlist()" function
15593 */
15594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015595f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015596{
15597 find_some_match(argvars, rettv, 3);
15598}
15599
15600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015601 * "matchstr()" function
15602 */
15603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015604f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015605{
15606 find_some_match(argvars, rettv, 2);
15607}
15608
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015609static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015610
15611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015612max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015613{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015614 long n = 0;
15615 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015616 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015617
15618 if (argvars[0].v_type == VAR_LIST)
15619 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015620 list_T *l;
15621 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015622
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015623 l = argvars[0].vval.v_list;
15624 if (l != NULL)
15625 {
15626 li = l->lv_first;
15627 if (li != NULL)
15628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015629 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015630 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015631 {
15632 li = li->li_next;
15633 if (li == NULL)
15634 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015635 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015636 if (domax ? i > n : i < n)
15637 n = i;
15638 }
15639 }
15640 }
15641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015642 else if (argvars[0].v_type == VAR_DICT)
15643 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015644 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015645 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015646 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015647 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015648
15649 d = argvars[0].vval.v_dict;
15650 if (d != NULL)
15651 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015652 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015653 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015654 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015655 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015656 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015657 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015658 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015659 if (first)
15660 {
15661 n = i;
15662 first = FALSE;
15663 }
15664 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015665 n = i;
15666 }
15667 }
15668 }
15669 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015670 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015671 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015672 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015673}
15674
15675/*
15676 * "max()" function
15677 */
15678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015679f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015680{
15681 max_min(argvars, rettv, TRUE);
15682}
15683
15684/*
15685 * "min()" function
15686 */
15687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015688f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015689{
15690 max_min(argvars, rettv, FALSE);
15691}
15692
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015693static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015694
15695/*
15696 * Create the directory in which "dir" is located, and higher levels when
15697 * needed.
15698 */
15699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015700mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015701{
15702 char_u *p;
15703 char_u *updir;
15704 int r = FAIL;
15705
15706 /* Get end of directory name in "dir".
15707 * We're done when it's "/" or "c:/". */
15708 p = gettail_sep(dir);
15709 if (p <= get_past_head(dir))
15710 return OK;
15711
15712 /* If the directory exists we're done. Otherwise: create it.*/
15713 updir = vim_strnsave(dir, (int)(p - dir));
15714 if (updir == NULL)
15715 return FAIL;
15716 if (mch_isdir(updir))
15717 r = OK;
15718 else if (mkdir_recurse(updir, prot) == OK)
15719 r = vim_mkdir_emsg(updir, prot);
15720 vim_free(updir);
15721 return r;
15722}
15723
15724#ifdef vim_mkdir
15725/*
15726 * "mkdir()" function
15727 */
15728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015729f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015730{
15731 char_u *dir;
15732 char_u buf[NUMBUFLEN];
15733 int prot = 0755;
15734
15735 rettv->vval.v_number = FAIL;
15736 if (check_restricted() || check_secure())
15737 return;
15738
15739 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015740 if (*dir == NUL)
15741 rettv->vval.v_number = FAIL;
15742 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015743 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015744 if (*gettail(dir) == NUL)
15745 /* remove trailing slashes */
15746 *gettail_sep(dir) = NUL;
15747
15748 if (argvars[1].v_type != VAR_UNKNOWN)
15749 {
15750 if (argvars[2].v_type != VAR_UNKNOWN)
15751 prot = get_tv_number_chk(&argvars[2], NULL);
15752 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15753 mkdir_recurse(dir, prot);
15754 }
15755 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015756 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015757}
15758#endif
15759
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 * "mode()" function
15762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015764f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015766 char_u buf[3];
15767
15768 buf[1] = NUL;
15769 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 if (VIsual_active)
15772 {
15773 if (VIsual_select)
15774 buf[0] = VIsual_mode + 's' - 'v';
15775 else
15776 buf[0] = VIsual_mode;
15777 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015778 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015779 || State == CONFIRM)
15780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015782 if (State == ASKMORE)
15783 buf[1] = 'm';
15784 else if (State == CONFIRM)
15785 buf[1] = '?';
15786 }
15787 else if (State == EXTERNCMD)
15788 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 else if (State & INSERT)
15790 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015791#ifdef FEAT_VREPLACE
15792 if (State & VREPLACE_FLAG)
15793 {
15794 buf[0] = 'R';
15795 buf[1] = 'v';
15796 }
15797 else
15798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799 if (State & REPLACE_FLAG)
15800 buf[0] = 'R';
15801 else
15802 buf[0] = 'i';
15803 }
15804 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015807 if (exmode_active)
15808 buf[1] = 'v';
15809 }
15810 else if (exmode_active)
15811 {
15812 buf[0] = 'c';
15813 buf[1] = 'e';
15814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015818 if (finish_op)
15819 buf[1] = 'o';
15820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015822 /* Clear out the minor mode when the argument is not a non-zero number or
15823 * non-empty string. */
15824 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015825 buf[1] = NUL;
15826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015827 rettv->vval.v_string = vim_strsave(buf);
15828 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829}
15830
Bram Moolenaar429fa852013-04-15 12:27:36 +020015831#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015832/*
15833 * "mzeval()" function
15834 */
15835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015836f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015837{
15838 char_u *str;
15839 char_u buf[NUMBUFLEN];
15840
15841 str = get_tv_string_buf(&argvars[0], buf);
15842 do_mzeval(str, rettv);
15843}
Bram Moolenaar75676462013-01-30 14:55:42 +010015844
15845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015846mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015847{
15848 typval_T argvars[3];
15849
15850 argvars[0].v_type = VAR_STRING;
15851 argvars[0].vval.v_string = name;
15852 copy_tv(args, &argvars[1]);
15853 argvars[2].v_type = VAR_UNKNOWN;
15854 f_call(argvars, rettv);
15855 clear_tv(&argvars[1]);
15856}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015857#endif
15858
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015860 * "nextnonblank()" function
15861 */
15862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015863f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015864{
15865 linenr_T lnum;
15866
15867 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015869 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015870 {
15871 lnum = 0;
15872 break;
15873 }
15874 if (*skipwhite(ml_get(lnum)) != NUL)
15875 break;
15876 }
15877 rettv->vval.v_number = lnum;
15878}
15879
15880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 * "nr2char()" function
15882 */
15883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015884f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885{
15886 char_u buf[NUMBUFLEN];
15887
15888#ifdef FEAT_MBYTE
15889 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015890 {
15891 int utf8 = 0;
15892
15893 if (argvars[1].v_type != VAR_UNKNOWN)
15894 utf8 = get_tv_number_chk(&argvars[1], NULL);
15895 if (utf8)
15896 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15897 else
15898 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 else
15901#endif
15902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015903 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 buf[1] = NUL;
15905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015906 rettv->v_type = VAR_STRING;
15907 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015908}
15909
15910/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015911 * "or(expr, expr)" function
15912 */
15913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015914f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015915{
15916 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15917 | get_tv_number_chk(&argvars[1], NULL);
15918}
15919
15920/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015921 * "pathshorten()" function
15922 */
15923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015924f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015925{
15926 char_u *p;
15927
15928 rettv->v_type = VAR_STRING;
15929 p = get_tv_string_chk(&argvars[0]);
15930 if (p == NULL)
15931 rettv->vval.v_string = NULL;
15932 else
15933 {
15934 p = vim_strsave(p);
15935 rettv->vval.v_string = p;
15936 if (p != NULL)
15937 shorten_dir(p);
15938 }
15939}
15940
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015941#ifdef FEAT_PERL
15942/*
15943 * "perleval()" function
15944 */
15945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015946f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015947{
15948 char_u *str;
15949 char_u buf[NUMBUFLEN];
15950
15951 str = get_tv_string_buf(&argvars[0], buf);
15952 do_perleval(str, rettv);
15953}
15954#endif
15955
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015956#ifdef FEAT_FLOAT
15957/*
15958 * "pow()" function
15959 */
15960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015961f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015962{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015963 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015964
15965 rettv->v_type = VAR_FLOAT;
15966 if (get_float_arg(argvars, &fx) == OK
15967 && get_float_arg(&argvars[1], &fy) == OK)
15968 rettv->vval.v_float = pow(fx, fy);
15969 else
15970 rettv->vval.v_float = 0.0;
15971}
15972#endif
15973
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015975 * "prevnonblank()" function
15976 */
15977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015978f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015979{
15980 linenr_T lnum;
15981
15982 lnum = get_tv_lnum(argvars);
15983 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15984 lnum = 0;
15985 else
15986 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15987 --lnum;
15988 rettv->vval.v_number = lnum;
15989}
15990
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015991/* This dummy va_list is here because:
15992 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15993 * - locally in the function results in a "used before set" warning
15994 * - using va_start() to initialize it gives "function with fixed args" error */
15995static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015996
Bram Moolenaar8c711452005-01-14 21:53:12 +000015997/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015998 * "printf()" function
15999 */
16000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016001f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016002{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016003 char_u buf[NUMBUFLEN];
16004 int len;
16005 char_u *s;
16006 int saved_did_emsg = did_emsg;
16007 char *fmt;
16008
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016009 rettv->v_type = VAR_STRING;
16010 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016011
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016012 /* Get the required length, allocate the buffer and do it for real. */
16013 did_emsg = FALSE;
16014 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16015 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16016 if (!did_emsg)
16017 {
16018 s = alloc(len + 1);
16019 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016020 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016021 rettv->vval.v_string = s;
16022 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016023 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016024 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016025 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016026}
16027
16028/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016029 * "pumvisible()" function
16030 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016032f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016033{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016034#ifdef FEAT_INS_EXPAND
16035 if (pum_visible())
16036 rettv->vval.v_number = 1;
16037#endif
16038}
16039
Bram Moolenaardb913952012-06-29 12:54:53 +020016040#ifdef FEAT_PYTHON3
16041/*
16042 * "py3eval()" function
16043 */
16044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016045f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016046{
16047 char_u *str;
16048 char_u buf[NUMBUFLEN];
16049
16050 str = get_tv_string_buf(&argvars[0], buf);
16051 do_py3eval(str, rettv);
16052}
16053#endif
16054
16055#ifdef FEAT_PYTHON
16056/*
16057 * "pyeval()" function
16058 */
16059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016060f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016061{
16062 char_u *str;
16063 char_u buf[NUMBUFLEN];
16064
16065 str = get_tv_string_buf(&argvars[0], buf);
16066 do_pyeval(str, rettv);
16067}
16068#endif
16069
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016070/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016071 * "range()" function
16072 */
16073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016074f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016075{
16076 long start;
16077 long end;
16078 long stride = 1;
16079 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016080 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016082 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016083 if (argvars[1].v_type == VAR_UNKNOWN)
16084 {
16085 end = start - 1;
16086 start = 0;
16087 }
16088 else
16089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016090 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016091 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016092 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016093 }
16094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016095 if (error)
16096 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016097 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016098 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016099 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016100 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016101 else
16102 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016103 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016104 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016105 if (list_append_number(rettv->vval.v_list,
16106 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016107 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016108 }
16109}
16110
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016111/*
16112 * "readfile()" function
16113 */
16114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016115f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016116{
16117 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016118 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016119 char_u *fname;
16120 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016121 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16122 int io_size = sizeof(buf);
16123 int readlen; /* size of last fread() */
16124 char_u *prev = NULL; /* previously read bytes, if any */
16125 long prevlen = 0; /* length of data in prev */
16126 long prevsize = 0; /* size of prev buffer */
16127 long maxline = MAXLNUM;
16128 long cnt = 0;
16129 char_u *p; /* position in buf */
16130 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016132 if (argvars[1].v_type != VAR_UNKNOWN)
16133 {
16134 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16135 binary = TRUE;
16136 if (argvars[2].v_type != VAR_UNKNOWN)
16137 maxline = get_tv_number(&argvars[2]);
16138 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016139
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016140 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016141 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016142
16143 /* Always open the file in binary mode, library functions have a mind of
16144 * their own about CR-LF conversion. */
16145 fname = get_tv_string(&argvars[0]);
16146 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16147 {
16148 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16149 return;
16150 }
16151
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016152 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016153 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016154 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016155
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016156 /* This for loop processes what was read, but is also entered at end
16157 * of file so that either:
16158 * - an incomplete line gets written
16159 * - a "binary" file gets an empty line at the end if it ends in a
16160 * newline. */
16161 for (p = buf, start = buf;
16162 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16163 ++p)
16164 {
16165 if (*p == '\n' || readlen <= 0)
16166 {
16167 listitem_T *li;
16168 char_u *s = NULL;
16169 long_u len = p - start;
16170
16171 /* Finished a line. Remove CRs before NL. */
16172 if (readlen > 0 && !binary)
16173 {
16174 while (len > 0 && start[len - 1] == '\r')
16175 --len;
16176 /* removal may cross back to the "prev" string */
16177 if (len == 0)
16178 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16179 --prevlen;
16180 }
16181 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016182 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016183 else
16184 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016185 /* Change "prev" buffer to be the right size. This way
16186 * the bytes are only copied once, and very long lines are
16187 * allocated only once. */
16188 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016189 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016190 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016191 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016192 prev = NULL; /* the list will own the string */
16193 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016194 }
16195 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016196 if (s == NULL)
16197 {
16198 do_outofmem_msg((long_u) prevlen + len + 1);
16199 failed = TRUE;
16200 break;
16201 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016202
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016203 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016204 {
16205 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016206 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016207 break;
16208 }
16209 li->li_tv.v_type = VAR_STRING;
16210 li->li_tv.v_lock = 0;
16211 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016212 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016213
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016214 start = p + 1; /* step over newline */
16215 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016216 break;
16217 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016218 else if (*p == NUL)
16219 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016220#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016221 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16222 * when finding the BF and check the previous two bytes. */
16223 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016224 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016225 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16226 * + 1, these may be in the "prev" string. */
16227 char_u back1 = p >= buf + 1 ? p[-1]
16228 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16229 char_u back2 = p >= buf + 2 ? p[-2]
16230 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16231 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016232
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016233 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016234 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016235 char_u *dest = p - 2;
16236
16237 /* Usually a BOM is at the beginning of a file, and so at
16238 * the beginning of a line; then we can just step over it.
16239 */
16240 if (start == dest)
16241 start = p + 1;
16242 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016243 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016244 /* have to shuffle buf to close gap */
16245 int adjust_prevlen = 0;
16246
16247 if (dest < buf)
16248 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016249 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016250 dest = buf;
16251 }
16252 if (readlen > p - buf + 1)
16253 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16254 readlen -= 3 - adjust_prevlen;
16255 prevlen -= adjust_prevlen;
16256 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016257 }
16258 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016259 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016260#endif
16261 } /* for */
16262
16263 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16264 break;
16265 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016266 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016267 /* There's part of a line in buf, store it in "prev". */
16268 if (p - start + prevlen >= prevsize)
16269 {
16270 /* need bigger "prev" buffer */
16271 char_u *newprev;
16272
16273 /* A common use case is ordinary text files and "prev" gets a
16274 * fragment of a line, so the first allocation is made
16275 * small, to avoid repeatedly 'allocing' large and
16276 * 'reallocing' small. */
16277 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016278 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016279 else
16280 {
16281 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016282 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016283 prevsize = grow50pc > growmin ? grow50pc : growmin;
16284 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016285 newprev = prev == NULL ? alloc(prevsize)
16286 : vim_realloc(prev, prevsize);
16287 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016288 {
16289 do_outofmem_msg((long_u)prevsize);
16290 failed = TRUE;
16291 break;
16292 }
16293 prev = newprev;
16294 }
16295 /* Add the line part to end of "prev". */
16296 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016297 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016298 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016299 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016300
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016301 /*
16302 * For a negative line count use only the lines at the end of the file,
16303 * free the rest.
16304 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016305 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016306 while (cnt > -maxline)
16307 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016308 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016309 --cnt;
16310 }
16311
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016312 if (failed)
16313 {
16314 list_free(rettv->vval.v_list, TRUE);
16315 /* readfile doc says an empty list is returned on error */
16316 rettv->vval.v_list = list_alloc();
16317 }
16318
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016319 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016320 fclose(fd);
16321}
16322
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016323#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016324static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016325
16326/*
16327 * Convert a List to proftime_T.
16328 * Return FAIL when there is something wrong.
16329 */
16330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016331list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016332{
16333 long n1, n2;
16334 int error = FALSE;
16335
16336 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16337 || arg->vval.v_list->lv_len != 2)
16338 return FAIL;
16339 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16340 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16341# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016342 tm->HighPart = n1;
16343 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016344# else
16345 tm->tv_sec = n1;
16346 tm->tv_usec = n2;
16347# endif
16348 return error ? FAIL : OK;
16349}
16350#endif /* FEAT_RELTIME */
16351
16352/*
16353 * "reltime()" function
16354 */
16355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016356f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016357{
16358#ifdef FEAT_RELTIME
16359 proftime_T res;
16360 proftime_T start;
16361
16362 if (argvars[0].v_type == VAR_UNKNOWN)
16363 {
16364 /* No arguments: get current time. */
16365 profile_start(&res);
16366 }
16367 else if (argvars[1].v_type == VAR_UNKNOWN)
16368 {
16369 if (list2proftime(&argvars[0], &res) == FAIL)
16370 return;
16371 profile_end(&res);
16372 }
16373 else
16374 {
16375 /* Two arguments: compute the difference. */
16376 if (list2proftime(&argvars[0], &start) == FAIL
16377 || list2proftime(&argvars[1], &res) == FAIL)
16378 return;
16379 profile_sub(&res, &start);
16380 }
16381
16382 if (rettv_list_alloc(rettv) == OK)
16383 {
16384 long n1, n2;
16385
16386# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016387 n1 = res.HighPart;
16388 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016389# else
16390 n1 = res.tv_sec;
16391 n2 = res.tv_usec;
16392# endif
16393 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16394 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16395 }
16396#endif
16397}
16398
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016399#ifdef FEAT_FLOAT
16400/*
16401 * "reltimefloat()" function
16402 */
16403 static void
16404f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16405{
16406# ifdef FEAT_RELTIME
16407 proftime_T tm;
16408# endif
16409
16410 rettv->v_type = VAR_FLOAT;
16411 rettv->vval.v_float = 0;
16412# ifdef FEAT_RELTIME
16413 if (list2proftime(&argvars[0], &tm) == OK)
16414 rettv->vval.v_float = profile_float(&tm);
16415# endif
16416}
16417#endif
16418
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016419/*
16420 * "reltimestr()" function
16421 */
16422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016423f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016424{
16425#ifdef FEAT_RELTIME
16426 proftime_T tm;
16427#endif
16428
16429 rettv->v_type = VAR_STRING;
16430 rettv->vval.v_string = NULL;
16431#ifdef FEAT_RELTIME
16432 if (list2proftime(&argvars[0], &tm) == OK)
16433 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16434#endif
16435}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016436
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016438static void make_connection(void);
16439static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440
16441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016442make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443{
16444 if (X_DISPLAY == NULL
16445# ifdef FEAT_GUI
16446 && !gui.in_use
16447# endif
16448 )
16449 {
16450 x_force_connect = TRUE;
16451 setup_term_clip();
16452 x_force_connect = FALSE;
16453 }
16454}
16455
16456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016457check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458{
16459 make_connection();
16460 if (X_DISPLAY == NULL)
16461 {
16462 EMSG(_("E240: No connection to Vim server"));
16463 return FAIL;
16464 }
16465 return OK;
16466}
16467#endif
16468
16469#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016470static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016471
16472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016473remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474{
16475 char_u *server_name;
16476 char_u *keys;
16477 char_u *r = NULL;
16478 char_u buf[NUMBUFLEN];
16479# ifdef WIN32
16480 HWND w;
16481# else
16482 Window w;
16483# endif
16484
16485 if (check_restricted() || check_secure())
16486 return;
16487
16488# ifdef FEAT_X11
16489 if (check_connection() == FAIL)
16490 return;
16491# endif
16492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016493 server_name = get_tv_string_chk(&argvars[0]);
16494 if (server_name == NULL)
16495 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016496 keys = get_tv_string_buf(&argvars[1], buf);
16497# ifdef WIN32
16498 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16499# else
16500 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16501 < 0)
16502# endif
16503 {
16504 if (r != NULL)
16505 EMSG(r); /* sending worked but evaluation failed */
16506 else
16507 EMSG2(_("E241: Unable to send to %s"), server_name);
16508 return;
16509 }
16510
16511 rettv->vval.v_string = r;
16512
16513 if (argvars[2].v_type != VAR_UNKNOWN)
16514 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016515 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016516 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016518
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016519 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016520 v.di_tv.v_type = VAR_STRING;
16521 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016522 idvar = get_tv_string_chk(&argvars[2]);
16523 if (idvar != NULL)
16524 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016525 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016526 }
16527}
16528#endif
16529
16530/*
16531 * "remote_expr()" function
16532 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016534f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016535{
16536 rettv->v_type = VAR_STRING;
16537 rettv->vval.v_string = NULL;
16538#ifdef FEAT_CLIENTSERVER
16539 remote_common(argvars, rettv, TRUE);
16540#endif
16541}
16542
16543/*
16544 * "remote_foreground()" function
16545 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016547f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016548{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016549#ifdef FEAT_CLIENTSERVER
16550# ifdef WIN32
16551 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016552 {
16553 char_u *server_name = get_tv_string_chk(&argvars[0]);
16554
16555 if (server_name != NULL)
16556 serverForeground(server_name);
16557 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016558# else
16559 /* Send a foreground() expression to the server. */
16560 argvars[1].v_type = VAR_STRING;
16561 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16562 argvars[2].v_type = VAR_UNKNOWN;
16563 remote_common(argvars, rettv, TRUE);
16564 vim_free(argvars[1].vval.v_string);
16565# endif
16566#endif
16567}
16568
Bram Moolenaar0d660222005-01-07 21:51:51 +000016569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016570f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016571{
16572#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016573 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016574 char_u *s = NULL;
16575# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016576 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016578 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016579
16580 if (check_restricted() || check_secure())
16581 {
16582 rettv->vval.v_number = -1;
16583 return;
16584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016585 serverid = get_tv_string_chk(&argvars[0]);
16586 if (serverid == NULL)
16587 {
16588 rettv->vval.v_number = -1;
16589 return; /* type error; errmsg already given */
16590 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016591# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016592 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016593 if (n == 0)
16594 rettv->vval.v_number = -1;
16595 else
16596 {
16597 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16598 rettv->vval.v_number = (s != NULL);
16599 }
16600# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016601 if (check_connection() == FAIL)
16602 return;
16603
16604 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016605 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016606# endif
16607
16608 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16609 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016610 char_u *retvar;
16611
Bram Moolenaar33570922005-01-25 22:26:29 +000016612 v.di_tv.v_type = VAR_STRING;
16613 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016614 retvar = get_tv_string_chk(&argvars[1]);
16615 if (retvar != NULL)
16616 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016617 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016618 }
16619#else
16620 rettv->vval.v_number = -1;
16621#endif
16622}
16623
Bram Moolenaar0d660222005-01-07 21:51:51 +000016624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016625f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016626{
16627 char_u *r = NULL;
16628
16629#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016630 char_u *serverid = get_tv_string_chk(&argvars[0]);
16631
16632 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016633 {
16634# ifdef WIN32
16635 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016636 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016637
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016638 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016639 if (n != 0)
16640 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16641 if (r == NULL)
16642# else
16643 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016644 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016645# endif
16646 EMSG(_("E277: Unable to read a server reply"));
16647 }
16648#endif
16649 rettv->v_type = VAR_STRING;
16650 rettv->vval.v_string = r;
16651}
16652
16653/*
16654 * "remote_send()" function
16655 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016657f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016658{
16659 rettv->v_type = VAR_STRING;
16660 rettv->vval.v_string = NULL;
16661#ifdef FEAT_CLIENTSERVER
16662 remote_common(argvars, rettv, FALSE);
16663#endif
16664}
16665
16666/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016667 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016668 */
16669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016670f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016671{
Bram Moolenaar33570922005-01-25 22:26:29 +000016672 list_T *l;
16673 listitem_T *item, *item2;
16674 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016675 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016676 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016677 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016678 dict_T *d;
16679 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016680 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016681
Bram Moolenaar8c711452005-01-14 21:53:12 +000016682 if (argvars[0].v_type == VAR_DICT)
16683 {
16684 if (argvars[2].v_type != VAR_UNKNOWN)
16685 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016686 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016687 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016689 key = get_tv_string_chk(&argvars[1]);
16690 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016692 di = dict_find(d, key, -1);
16693 if (di == NULL)
16694 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016695 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16696 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016697 {
16698 *rettv = di->di_tv;
16699 init_tv(&di->di_tv);
16700 dictitem_remove(d, di);
16701 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016703 }
16704 }
16705 else if (argvars[0].v_type != VAR_LIST)
16706 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016707 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016708 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016710 int error = FALSE;
16711
16712 idx = get_tv_number_chk(&argvars[1], &error);
16713 if (error)
16714 ; /* type error: do nothing, errmsg already given */
16715 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016716 EMSGN(_(e_listidx), idx);
16717 else
16718 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016719 if (argvars[2].v_type == VAR_UNKNOWN)
16720 {
16721 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016722 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016723 *rettv = item->li_tv;
16724 vim_free(item);
16725 }
16726 else
16727 {
16728 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016729 end = get_tv_number_chk(&argvars[2], &error);
16730 if (error)
16731 ; /* type error: do nothing */
16732 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016733 EMSGN(_(e_listidx), end);
16734 else
16735 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016736 int cnt = 0;
16737
16738 for (li = item; li != NULL; li = li->li_next)
16739 {
16740 ++cnt;
16741 if (li == item2)
16742 break;
16743 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016744 if (li == NULL) /* didn't find "item2" after "item" */
16745 EMSG(_(e_invrange));
16746 else
16747 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016748 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016749 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016750 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016751 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016752 l->lv_first = item;
16753 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016754 item->li_prev = NULL;
16755 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016756 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016757 }
16758 }
16759 }
16760 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016761 }
16762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763}
16764
16765/*
16766 * "rename({from}, {to})" function
16767 */
16768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016769f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770{
16771 char_u buf[NUMBUFLEN];
16772
16773 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016776 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16777 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778}
16779
16780/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016781 * "repeat()" function
16782 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016784f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016785{
16786 char_u *p;
16787 int n;
16788 int slen;
16789 int len;
16790 char_u *r;
16791 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016792
16793 n = get_tv_number(&argvars[1]);
16794 if (argvars[0].v_type == VAR_LIST)
16795 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016796 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016797 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016798 if (list_extend(rettv->vval.v_list,
16799 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016800 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016801 }
16802 else
16803 {
16804 p = get_tv_string(&argvars[0]);
16805 rettv->v_type = VAR_STRING;
16806 rettv->vval.v_string = NULL;
16807
16808 slen = (int)STRLEN(p);
16809 len = slen * n;
16810 if (len <= 0)
16811 return;
16812
16813 r = alloc(len + 1);
16814 if (r != NULL)
16815 {
16816 for (i = 0; i < n; i++)
16817 mch_memmove(r + i * slen, p, (size_t)slen);
16818 r[len] = NUL;
16819 }
16820
16821 rettv->vval.v_string = r;
16822 }
16823}
16824
16825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 * "resolve()" function
16827 */
16828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016829f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830{
16831 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016832#ifdef HAVE_READLINK
16833 char_u *buf = NULL;
16834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016836 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837#ifdef FEAT_SHORTCUT
16838 {
16839 char_u *v = NULL;
16840
16841 v = mch_resolve_shortcut(p);
16842 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016843 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016845 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 }
16847#else
16848# ifdef HAVE_READLINK
16849 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 char_u *cpy;
16851 int len;
16852 char_u *remain = NULL;
16853 char_u *q;
16854 int is_relative_to_current = FALSE;
16855 int has_trailing_pathsep = FALSE;
16856 int limit = 100;
16857
16858 p = vim_strsave(p);
16859
16860 if (p[0] == '.' && (vim_ispathsep(p[1])
16861 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16862 is_relative_to_current = TRUE;
16863
16864 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016865 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016868 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870
16871 q = getnextcomp(p);
16872 if (*q != NUL)
16873 {
16874 /* Separate the first path component in "p", and keep the
16875 * remainder (beginning with the path separator). */
16876 remain = vim_strsave(q - 1);
16877 q[-1] = NUL;
16878 }
16879
Bram Moolenaard9462e32011-04-11 21:35:11 +020016880 buf = alloc(MAXPATHL + 1);
16881 if (buf == NULL)
16882 goto fail;
16883
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 for (;;)
16885 {
16886 for (;;)
16887 {
16888 len = readlink((char *)p, (char *)buf, MAXPATHL);
16889 if (len <= 0)
16890 break;
16891 buf[len] = NUL;
16892
16893 if (limit-- == 0)
16894 {
16895 vim_free(p);
16896 vim_free(remain);
16897 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016898 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 goto fail;
16900 }
16901
16902 /* Ensure that the result will have a trailing path separator
16903 * if the argument has one. */
16904 if (remain == NULL && has_trailing_pathsep)
16905 add_pathsep(buf);
16906
16907 /* Separate the first path component in the link value and
16908 * concatenate the remainders. */
16909 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16910 if (*q != NUL)
16911 {
16912 if (remain == NULL)
16913 remain = vim_strsave(q - 1);
16914 else
16915 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016916 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 if (cpy != NULL)
16918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 vim_free(remain);
16920 remain = cpy;
16921 }
16922 }
16923 q[-1] = NUL;
16924 }
16925
16926 q = gettail(p);
16927 if (q > p && *q == NUL)
16928 {
16929 /* Ignore trailing path separator. */
16930 q[-1] = NUL;
16931 q = gettail(p);
16932 }
16933 if (q > p && !mch_isFullName(buf))
16934 {
16935 /* symlink is relative to directory of argument */
16936 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16937 if (cpy != NULL)
16938 {
16939 STRCPY(cpy, p);
16940 STRCPY(gettail(cpy), buf);
16941 vim_free(p);
16942 p = cpy;
16943 }
16944 }
16945 else
16946 {
16947 vim_free(p);
16948 p = vim_strsave(buf);
16949 }
16950 }
16951
16952 if (remain == NULL)
16953 break;
16954
16955 /* Append the first path component of "remain" to "p". */
16956 q = getnextcomp(remain + 1);
16957 len = q - remain - (*q != NUL);
16958 cpy = vim_strnsave(p, STRLEN(p) + len);
16959 if (cpy != NULL)
16960 {
16961 STRNCAT(cpy, remain, len);
16962 vim_free(p);
16963 p = cpy;
16964 }
16965 /* Shorten "remain". */
16966 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016967 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 else
16969 {
16970 vim_free(remain);
16971 remain = NULL;
16972 }
16973 }
16974
16975 /* If the result is a relative path name, make it explicitly relative to
16976 * the current directory if and only if the argument had this form. */
16977 if (!vim_ispathsep(*p))
16978 {
16979 if (is_relative_to_current
16980 && *p != NUL
16981 && !(p[0] == '.'
16982 && (p[1] == NUL
16983 || vim_ispathsep(p[1])
16984 || (p[1] == '.'
16985 && (p[2] == NUL
16986 || vim_ispathsep(p[2]))))))
16987 {
16988 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016989 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 if (cpy != NULL)
16991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 vim_free(p);
16993 p = cpy;
16994 }
16995 }
16996 else if (!is_relative_to_current)
16997 {
16998 /* Strip leading "./". */
16999 q = p;
17000 while (q[0] == '.' && vim_ispathsep(q[1]))
17001 q += 2;
17002 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017003 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 }
17005 }
17006
17007 /* Ensure that the result will have no trailing path separator
17008 * if the argument had none. But keep "/" or "//". */
17009 if (!has_trailing_pathsep)
17010 {
17011 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017012 if (after_pathsep(p, q))
17013 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 }
17015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017016 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 }
17018# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017019 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020# endif
17021#endif
17022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017023 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024
17025#ifdef HAVE_READLINK
17026fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017027 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030}
17031
17032/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017033 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 */
17035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017036f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037{
Bram Moolenaar33570922005-01-25 22:26:29 +000017038 list_T *l;
17039 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041 if (argvars[0].v_type != VAR_LIST)
17042 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017043 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017044 && !tv_check_lock(l->lv_lock,
17045 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046 {
17047 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017048 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017049 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050 while (li != NULL)
17051 {
17052 ni = li->li_prev;
17053 list_append(l, li);
17054 li = ni;
17055 }
17056 rettv->vval.v_list = l;
17057 rettv->v_type = VAR_LIST;
17058 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017059 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061}
17062
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017063#define SP_NOMOVE 0x01 /* don't move cursor */
17064#define SP_REPEAT 0x02 /* repeat to find outer pair */
17065#define SP_RETCOUNT 0x04 /* return matchcount */
17066#define SP_SETPCMARK 0x08 /* set previous context mark */
17067#define SP_START 0x10 /* accept match at start position */
17068#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17069#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017070#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017071
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017072static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073
17074/*
17075 * Get flags for a search function.
17076 * Possibly sets "p_ws".
17077 * Returns BACKWARD, FORWARD or zero (for an error).
17078 */
17079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017080get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081{
17082 int dir = FORWARD;
17083 char_u *flags;
17084 char_u nbuf[NUMBUFLEN];
17085 int mask;
17086
17087 if (varp->v_type != VAR_UNKNOWN)
17088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017089 flags = get_tv_string_buf_chk(varp, nbuf);
17090 if (flags == NULL)
17091 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017092 while (*flags != NUL)
17093 {
17094 switch (*flags)
17095 {
17096 case 'b': dir = BACKWARD; break;
17097 case 'w': p_ws = TRUE; break;
17098 case 'W': p_ws = FALSE; break;
17099 default: mask = 0;
17100 if (flagsp != NULL)
17101 switch (*flags)
17102 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017103 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017104 case 'e': mask = SP_END; break;
17105 case 'm': mask = SP_RETCOUNT; break;
17106 case 'n': mask = SP_NOMOVE; break;
17107 case 'p': mask = SP_SUBPAT; break;
17108 case 'r': mask = SP_REPEAT; break;
17109 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017110 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017111 }
17112 if (mask == 0)
17113 {
17114 EMSG2(_(e_invarg2), flags);
17115 dir = 0;
17116 }
17117 else
17118 *flagsp |= mask;
17119 }
17120 if (dir == 0)
17121 break;
17122 ++flags;
17123 }
17124 }
17125 return dir;
17126}
17127
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017129 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017132search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017134 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 char_u *pat;
17136 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017137 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 int save_p_ws = p_ws;
17139 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017140 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017141 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017142 proftime_T tm;
17143#ifdef FEAT_RELTIME
17144 long time_limit = 0;
17145#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017146 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017147 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017149 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017150 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017151 if (dir == 0)
17152 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017153 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017154 if (flags & SP_START)
17155 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017156 if (flags & SP_END)
17157 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017158 if (flags & SP_COLUMN)
17159 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017160
Bram Moolenaar76929292008-01-06 19:07:36 +000017161 /* Optional arguments: line number to stop searching and timeout. */
17162 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017163 {
17164 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17165 if (lnum_stop < 0)
17166 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017167#ifdef FEAT_RELTIME
17168 if (argvars[3].v_type != VAR_UNKNOWN)
17169 {
17170 time_limit = get_tv_number_chk(&argvars[3], NULL);
17171 if (time_limit < 0)
17172 goto theend;
17173 }
17174#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017175 }
17176
Bram Moolenaar76929292008-01-06 19:07:36 +000017177#ifdef FEAT_RELTIME
17178 /* Set the time limit, if there is one. */
17179 profile_setlimit(time_limit, &tm);
17180#endif
17181
Bram Moolenaar231334e2005-07-25 20:46:57 +000017182 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017183 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017184 * Check to make sure only those flags are set.
17185 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17186 * flags cannot be set. Check for that condition also.
17187 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017188 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017189 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017191 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017192 goto theend;
17193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017195 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017196 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017197 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017198 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017200 if (flags & SP_SUBPAT)
17201 retval = subpatnum;
17202 else
17203 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017204 if (flags & SP_SETPCMARK)
17205 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017207 if (match_pos != NULL)
17208 {
17209 /* Store the match cursor position */
17210 match_pos->lnum = pos.lnum;
17211 match_pos->col = pos.col + 1;
17212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 /* "/$" will put the cursor after the end of the line, may need to
17214 * correct that here */
17215 check_cursor();
17216 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017217
17218 /* If 'n' flag is used: restore cursor position. */
17219 if (flags & SP_NOMOVE)
17220 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017221 else
17222 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017223theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017225
17226 return retval;
17227}
17228
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017229#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017230
17231/*
17232 * round() is not in C90, use ceil() or floor() instead.
17233 */
17234 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017235vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017236{
17237 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17238}
17239
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017240/*
17241 * "round({float})" function
17242 */
17243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017244f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017245{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017246 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017247
17248 rettv->v_type = VAR_FLOAT;
17249 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017250 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017251 else
17252 rettv->vval.v_float = 0.0;
17253}
17254#endif
17255
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017256/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017257 * "screenattr()" function
17258 */
17259 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017260f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017261{
17262 int row;
17263 int col;
17264 int c;
17265
17266 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17267 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17268 if (row < 0 || row >= screen_Rows
17269 || col < 0 || col >= screen_Columns)
17270 c = -1;
17271 else
17272 c = ScreenAttrs[LineOffset[row] + col];
17273 rettv->vval.v_number = c;
17274}
17275
17276/*
17277 * "screenchar()" function
17278 */
17279 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017280f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017281{
17282 int row;
17283 int col;
17284 int off;
17285 int c;
17286
17287 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17288 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17289 if (row < 0 || row >= screen_Rows
17290 || col < 0 || col >= screen_Columns)
17291 c = -1;
17292 else
17293 {
17294 off = LineOffset[row] + col;
17295#ifdef FEAT_MBYTE
17296 if (enc_utf8 && ScreenLinesUC[off] != 0)
17297 c = ScreenLinesUC[off];
17298 else
17299#endif
17300 c = ScreenLines[off];
17301 }
17302 rettv->vval.v_number = c;
17303}
17304
17305/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017306 * "screencol()" function
17307 *
17308 * First column is 1 to be consistent with virtcol().
17309 */
17310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017311f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017312{
17313 rettv->vval.v_number = screen_screencol() + 1;
17314}
17315
17316/*
17317 * "screenrow()" function
17318 */
17319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017320f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017321{
17322 rettv->vval.v_number = screen_screenrow() + 1;
17323}
17324
17325/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017326 * "search()" function
17327 */
17328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017329f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017330{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017331 int flags = 0;
17332
17333 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334}
17335
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017337 * "searchdecl()" function
17338 */
17339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017340f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017341{
17342 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017343 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017344 int error = FALSE;
17345 char_u *name;
17346
17347 rettv->vval.v_number = 1; /* default: FAIL */
17348
17349 name = get_tv_string_chk(&argvars[0]);
17350 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017351 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017352 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017353 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17354 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17355 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017356 if (!error && name != NULL)
17357 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017358 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017359}
17360
17361/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017362 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017365searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366{
17367 char_u *spat, *mpat, *epat;
17368 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 int dir;
17371 int flags = 0;
17372 char_u nbuf1[NUMBUFLEN];
17373 char_u nbuf2[NUMBUFLEN];
17374 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017375 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017376 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017377 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017380 spat = get_tv_string_chk(&argvars[0]);
17381 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17382 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17383 if (spat == NULL || mpat == NULL || epat == NULL)
17384 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 /* Handle the optional fourth argument: flags */
17387 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017388 if (dir == 0)
17389 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017390
17391 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017392 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17393 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017394 if ((flags & (SP_END | SP_SUBPAT)) != 0
17395 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017396 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017397 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017398 goto theend;
17399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017401 /* Using 'r' implies 'W', otherwise it doesn't work. */
17402 if (flags & SP_REPEAT)
17403 p_ws = FALSE;
17404
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017405 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017406 if (argvars[3].v_type == VAR_UNKNOWN
17407 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 skip = (char_u *)"";
17409 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017411 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017412 if (argvars[5].v_type != VAR_UNKNOWN)
17413 {
17414 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17415 if (lnum_stop < 0)
17416 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017417#ifdef FEAT_RELTIME
17418 if (argvars[6].v_type != VAR_UNKNOWN)
17419 {
17420 time_limit = get_tv_number_chk(&argvars[6], NULL);
17421 if (time_limit < 0)
17422 goto theend;
17423 }
17424#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017425 }
17426 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017427 if (skip == NULL)
17428 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017430 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017431 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017432
17433theend:
17434 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017435
17436 return retval;
17437}
17438
17439/*
17440 * "searchpair()" function
17441 */
17442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017443f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017444{
17445 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17446}
17447
17448/*
17449 * "searchpairpos()" function
17450 */
17451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017452f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017453{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017454 pos_T match_pos;
17455 int lnum = 0;
17456 int col = 0;
17457
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017458 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017459 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017460
17461 if (searchpair_cmn(argvars, &match_pos) > 0)
17462 {
17463 lnum = match_pos.lnum;
17464 col = match_pos.col;
17465 }
17466
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017467 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17468 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017469}
17470
17471/*
17472 * Search for a start/middle/end thing.
17473 * Used by searchpair(), see its documentation for the details.
17474 * Returns 0 or -1 for no match,
17475 */
17476 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017477do_searchpair(
17478 char_u *spat, /* start pattern */
17479 char_u *mpat, /* middle pattern */
17480 char_u *epat, /* end pattern */
17481 int dir, /* BACKWARD or FORWARD */
17482 char_u *skip, /* skip expression */
17483 int flags, /* SP_SETPCMARK and other SP_ values */
17484 pos_T *match_pos,
17485 linenr_T lnum_stop, /* stop at this line if not zero */
17486 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017487{
17488 char_u *save_cpo;
17489 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17490 long retval = 0;
17491 pos_T pos;
17492 pos_T firstpos;
17493 pos_T foundpos;
17494 pos_T save_cursor;
17495 pos_T save_pos;
17496 int n;
17497 int r;
17498 int nest = 1;
17499 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017500 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017501 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017502
17503 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17504 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017505 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017506
Bram Moolenaar76929292008-01-06 19:07:36 +000017507#ifdef FEAT_RELTIME
17508 /* Set the time limit, if there is one. */
17509 profile_setlimit(time_limit, &tm);
17510#endif
17511
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017512 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17513 * start/middle/end (pat3, for the top pair). */
17514 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17515 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17516 if (pat2 == NULL || pat3 == NULL)
17517 goto theend;
17518 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17519 if (*mpat == NUL)
17520 STRCPY(pat3, pat2);
17521 else
17522 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17523 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017524 if (flags & SP_START)
17525 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017526
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 save_cursor = curwin->w_cursor;
17528 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017529 clearpos(&firstpos);
17530 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 pat = pat3;
17532 for (;;)
17533 {
17534 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017535 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17537 /* didn't find it or found the first match again: FAIL */
17538 break;
17539
17540 if (firstpos.lnum == 0)
17541 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017542 if (equalpos(pos, foundpos))
17543 {
17544 /* Found the same position again. Can happen with a pattern that
17545 * has "\zs" at the end and searching backwards. Advance one
17546 * character and try again. */
17547 if (dir == BACKWARD)
17548 decl(&pos);
17549 else
17550 incl(&pos);
17551 }
17552 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017554 /* clear the start flag to avoid getting stuck here */
17555 options &= ~SEARCH_START;
17556
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 /* If the skip pattern matches, ignore this match. */
17558 if (*skip != NUL)
17559 {
17560 save_pos = curwin->w_cursor;
17561 curwin->w_cursor = pos;
17562 r = eval_to_bool(skip, &err, NULL, FALSE);
17563 curwin->w_cursor = save_pos;
17564 if (err)
17565 {
17566 /* Evaluating {skip} caused an error, break here. */
17567 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017568 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 break;
17570 }
17571 if (r)
17572 continue;
17573 }
17574
17575 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17576 {
17577 /* Found end when searching backwards or start when searching
17578 * forward: nested pair. */
17579 ++nest;
17580 pat = pat2; /* nested, don't search for middle */
17581 }
17582 else
17583 {
17584 /* Found end when searching forward or start when searching
17585 * backward: end of (nested) pair; or found middle in outer pair. */
17586 if (--nest == 1)
17587 pat = pat3; /* outer level, search for middle */
17588 }
17589
17590 if (nest == 0)
17591 {
17592 /* Found the match: return matchcount or line number. */
17593 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017594 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017596 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017597 if (flags & SP_SETPCMARK)
17598 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 curwin->w_cursor = pos;
17600 if (!(flags & SP_REPEAT))
17601 break;
17602 nest = 1; /* search for next unmatched */
17603 }
17604 }
17605
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017606 if (match_pos != NULL)
17607 {
17608 /* Store the match cursor position */
17609 match_pos->lnum = curwin->w_cursor.lnum;
17610 match_pos->col = curwin->w_cursor.col + 1;
17611 }
17612
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017614 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 curwin->w_cursor = save_cursor;
17616
17617theend:
17618 vim_free(pat2);
17619 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017620 if (p_cpo == empty_option)
17621 p_cpo = save_cpo;
17622 else
17623 /* Darn, evaluating the {skip} expression changed the value. */
17624 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017625
17626 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627}
17628
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017629/*
17630 * "searchpos()" function
17631 */
17632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017633f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017634{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017635 pos_T match_pos;
17636 int lnum = 0;
17637 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017638 int n;
17639 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017640
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017641 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017642 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017643
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017644 n = search_cmn(argvars, &match_pos, &flags);
17645 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017646 {
17647 lnum = match_pos.lnum;
17648 col = match_pos.col;
17649 }
17650
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017651 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17652 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017653 if (flags & SP_SUBPAT)
17654 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017655}
17656
Bram Moolenaar0d660222005-01-07 21:51:51 +000017657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017658f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017660#ifdef FEAT_CLIENTSERVER
17661 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017662 char_u *server = get_tv_string_chk(&argvars[0]);
17663 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664
Bram Moolenaar0d660222005-01-07 21:51:51 +000017665 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017666 if (server == NULL || reply == NULL)
17667 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017668 if (check_restricted() || check_secure())
17669 return;
17670# ifdef FEAT_X11
17671 if (check_connection() == FAIL)
17672 return;
17673# endif
17674
17675 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017677 EMSG(_("E258: Unable to send to client"));
17678 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017680 rettv->vval.v_number = 0;
17681#else
17682 rettv->vval.v_number = -1;
17683#endif
17684}
17685
Bram Moolenaar0d660222005-01-07 21:51:51 +000017686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017687f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017688{
17689 char_u *r = NULL;
17690
17691#ifdef FEAT_CLIENTSERVER
17692# ifdef WIN32
17693 r = serverGetVimNames();
17694# else
17695 make_connection();
17696 if (X_DISPLAY != NULL)
17697 r = serverGetVimNames(X_DISPLAY);
17698# endif
17699#endif
17700 rettv->v_type = VAR_STRING;
17701 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702}
17703
17704/*
17705 * "setbufvar()" function
17706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017708f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709{
17710 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017713 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 char_u nbuf[NUMBUFLEN];
17715
17716 if (check_restricted() || check_secure())
17717 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017718 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17719 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017720 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 varp = &argvars[2];
17722
17723 if (buf != NULL && varname != NULL && varp != NULL)
17724 {
17725 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727
17728 if (*varname == '&')
17729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017730 long numval;
17731 char_u *strval;
17732 int error = FALSE;
17733
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017735 numval = get_tv_number_chk(varp, &error);
17736 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017737 if (!error && strval != NULL)
17738 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 }
17740 else
17741 {
17742 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17743 if (bufvarname != NULL)
17744 {
17745 STRCPY(bufvarname, "b:");
17746 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017747 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 vim_free(bufvarname);
17749 }
17750 }
17751
17752 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755}
17756
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017758f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017759{
17760 dict_T *d;
17761 dictitem_T *di;
17762 char_u *csearch;
17763
17764 if (argvars[0].v_type != VAR_DICT)
17765 {
17766 EMSG(_(e_dictreq));
17767 return;
17768 }
17769
17770 if ((d = argvars[0].vval.v_dict) != NULL)
17771 {
17772 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17773 if (csearch != NULL)
17774 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017775#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017776 if (enc_utf8)
17777 {
17778 int pcc[MAX_MCO];
17779 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017780
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017781 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17782 }
17783 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017784#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017785 set_last_csearch(PTR2CHAR(csearch),
17786 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017787 }
17788
17789 di = dict_find(d, (char_u *)"forward", -1);
17790 if (di != NULL)
17791 set_csearch_direction(get_tv_number(&di->di_tv)
17792 ? FORWARD : BACKWARD);
17793
17794 di = dict_find(d, (char_u *)"until", -1);
17795 if (di != NULL)
17796 set_csearch_until(!!get_tv_number(&di->di_tv));
17797 }
17798}
17799
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800/*
17801 * "setcmdpos()" function
17802 */
17803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017804f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017806 int pos = (int)get_tv_number(&argvars[0]) - 1;
17807
17808 if (pos >= 0)
17809 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810}
17811
17812/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017813 * "setfperm({fname}, {mode})" function
17814 */
17815 static void
17816f_setfperm(typval_T *argvars, typval_T *rettv)
17817{
17818 char_u *fname;
17819 char_u modebuf[NUMBUFLEN];
17820 char_u *mode_str;
17821 int i;
17822 int mask;
17823 int mode = 0;
17824
17825 rettv->vval.v_number = 0;
17826 fname = get_tv_string_chk(&argvars[0]);
17827 if (fname == NULL)
17828 return;
17829 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17830 if (mode_str == NULL)
17831 return;
17832 if (STRLEN(mode_str) != 9)
17833 {
17834 EMSG2(_(e_invarg2), mode_str);
17835 return;
17836 }
17837
17838 mask = 1;
17839 for (i = 8; i >= 0; --i)
17840 {
17841 if (mode_str[i] != '-')
17842 mode |= mask;
17843 mask = mask << 1;
17844 }
17845 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17846}
17847
17848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 * "setline()" function
17850 */
17851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017852f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853{
17854 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017855 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017856 list_T *l = NULL;
17857 listitem_T *li = NULL;
17858 long added = 0;
17859 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017861 lnum = get_tv_lnum(&argvars[0]);
17862 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017864 l = argvars[1].vval.v_list;
17865 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017867 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017868 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017869
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017870 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017871 for (;;)
17872 {
17873 if (l != NULL)
17874 {
17875 /* list argument, get next string */
17876 if (li == NULL)
17877 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017878 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017879 li = li->li_next;
17880 }
17881
17882 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017883 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017884 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017885
17886 /* When coming here from Insert mode, sync undo, so that this can be
17887 * undone separately from what was previously inserted. */
17888 if (u_sync_once == 2)
17889 {
17890 u_sync_once = 1; /* notify that u_sync() was called */
17891 u_sync(TRUE);
17892 }
17893
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017894 if (lnum <= curbuf->b_ml.ml_line_count)
17895 {
17896 /* existing line, replace it */
17897 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17898 {
17899 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017900 if (lnum == curwin->w_cursor.lnum)
17901 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017902 rettv->vval.v_number = 0; /* OK */
17903 }
17904 }
17905 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17906 {
17907 /* lnum is one past the last line, append the line */
17908 ++added;
17909 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17910 rettv->vval.v_number = 0; /* OK */
17911 }
17912
17913 if (l == NULL) /* only one string argument */
17914 break;
17915 ++lnum;
17916 }
17917
17918 if (added > 0)
17919 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920}
17921
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017922static 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 +000017923
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017925 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017926 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017928set_qf_ll_list(
17929 win_T *wp UNUSED,
17930 typval_T *list_arg UNUSED,
17931 typval_T *action_arg UNUSED,
17932 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017933{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017934#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017935 char_u *act;
17936 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017937#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017938
Bram Moolenaar2641f772005-03-25 21:58:17 +000017939 rettv->vval.v_number = -1;
17940
17941#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017942 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017943 EMSG(_(e_listreq));
17944 else
17945 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017946 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017947
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017948 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017949 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017950 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017951 if (act == NULL)
17952 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017953 if (*act == 'a' || *act == 'r')
17954 action = *act;
17955 }
17956
Bram Moolenaar81484f42012-12-05 15:16:47 +010017957 if (l != NULL && set_errorlist(wp, l, action,
17958 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017959 rettv->vval.v_number = 0;
17960 }
17961#endif
17962}
17963
17964/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017965 * "setloclist()" function
17966 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017968f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017969{
17970 win_T *win;
17971
17972 rettv->vval.v_number = -1;
17973
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017974 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017975 if (win != NULL)
17976 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17977}
17978
17979/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017980 * "setmatches()" function
17981 */
17982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017983f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017984{
17985#ifdef FEAT_SEARCH_EXTRA
17986 list_T *l;
17987 listitem_T *li;
17988 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017989 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017990
17991 rettv->vval.v_number = -1;
17992 if (argvars[0].v_type != VAR_LIST)
17993 {
17994 EMSG(_(e_listreq));
17995 return;
17996 }
17997 if ((l = argvars[0].vval.v_list) != NULL)
17998 {
17999
18000 /* To some extent make sure that we are dealing with a list from
18001 * "getmatches()". */
18002 li = l->lv_first;
18003 while (li != NULL)
18004 {
18005 if (li->li_tv.v_type != VAR_DICT
18006 || (d = li->li_tv.vval.v_dict) == NULL)
18007 {
18008 EMSG(_(e_invarg));
18009 return;
18010 }
18011 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018012 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18013 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018014 && dict_find(d, (char_u *)"priority", -1) != NULL
18015 && dict_find(d, (char_u *)"id", -1) != NULL))
18016 {
18017 EMSG(_(e_invarg));
18018 return;
18019 }
18020 li = li->li_next;
18021 }
18022
18023 clear_matches(curwin);
18024 li = l->lv_first;
18025 while (li != NULL)
18026 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018027 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018028 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018029 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018030 char_u *group;
18031 int priority;
18032 int id;
18033 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018034
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018035 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018036 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18037 {
18038 if (s == NULL)
18039 {
18040 s = list_alloc();
18041 if (s == NULL)
18042 return;
18043 }
18044
18045 /* match from matchaddpos() */
18046 for (i = 1; i < 9; i++)
18047 {
18048 sprintf((char *)buf, (char *)"pos%d", i);
18049 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18050 {
18051 if (di->di_tv.v_type != VAR_LIST)
18052 return;
18053
18054 list_append_tv(s, &di->di_tv);
18055 s->lv_refcount++;
18056 }
18057 else
18058 break;
18059 }
18060 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018061
18062 group = get_dict_string(d, (char_u *)"group", FALSE);
18063 priority = (int)get_dict_number(d, (char_u *)"priority");
18064 id = (int)get_dict_number(d, (char_u *)"id");
18065 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18066 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18067 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018068 if (i == 0)
18069 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018070 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018071 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018072 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018073 }
18074 else
18075 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018076 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018077 list_unref(s);
18078 s = NULL;
18079 }
18080
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018081 li = li->li_next;
18082 }
18083 rettv->vval.v_number = 0;
18084 }
18085#endif
18086}
18087
18088/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018089 * "setpos()" function
18090 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018092f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018093{
18094 pos_T pos;
18095 int fnum;
18096 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018097 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018098
Bram Moolenaar08250432008-02-13 11:42:46 +000018099 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018100 name = get_tv_string_chk(argvars);
18101 if (name != NULL)
18102 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018103 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018104 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018105 if (--pos.col < 0)
18106 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018107 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018108 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018109 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018110 if (fnum == curbuf->b_fnum)
18111 {
18112 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018113 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018114 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018115 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018116 curwin->w_set_curswant = FALSE;
18117 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018118 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018119 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018120 }
18121 else
18122 EMSG(_(e_invarg));
18123 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018124 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18125 {
18126 /* set mark */
18127 if (setmark_pos(name[1], &pos, fnum) == OK)
18128 rettv->vval.v_number = 0;
18129 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018130 else
18131 EMSG(_(e_invarg));
18132 }
18133 }
18134}
18135
18136/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018137 * "setqflist()" function
18138 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018140f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018141{
18142 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18143}
18144
18145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 * "setreg()" function
18147 */
18148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018149f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150{
18151 int regname;
18152 char_u *strregname;
18153 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018154 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 int append;
18156 char_u yank_type;
18157 long block_len;
18158
18159 block_len = -1;
18160 yank_type = MAUTO;
18161 append = FALSE;
18162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018163 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018164 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018166 if (strregname == NULL)
18167 return; /* type error; errmsg already given */
18168 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169 if (regname == 0 || regname == '@')
18170 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018172 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018174 stropt = get_tv_string_chk(&argvars[2]);
18175 if (stropt == NULL)
18176 return; /* type error */
18177 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 switch (*stropt)
18179 {
18180 case 'a': case 'A': /* append */
18181 append = TRUE;
18182 break;
18183 case 'v': case 'c': /* character-wise selection */
18184 yank_type = MCHAR;
18185 break;
18186 case 'V': case 'l': /* line-wise selection */
18187 yank_type = MLINE;
18188 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 case 'b': case Ctrl_V: /* block-wise selection */
18190 yank_type = MBLOCK;
18191 if (VIM_ISDIGIT(stropt[1]))
18192 {
18193 ++stropt;
18194 block_len = getdigits(&stropt) - 1;
18195 --stropt;
18196 }
18197 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 }
18199 }
18200
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018201 if (argvars[1].v_type == VAR_LIST)
18202 {
18203 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018204 char_u **allocval;
18205 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018206 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018207 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018208 int len = argvars[1].vval.v_list->lv_len;
18209 listitem_T *li;
18210
Bram Moolenaar7d647822014-04-05 21:28:56 +020018211 /* First half: use for pointers to result lines; second half: use for
18212 * pointers to allocated copies. */
18213 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018214 if (lstval == NULL)
18215 return;
18216 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018217 allocval = lstval + len + 2;
18218 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018219
18220 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18221 li = li->li_next)
18222 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018223 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018224 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018225 goto free_lstval;
18226 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018227 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018228 /* Need to make a copy, next get_tv_string_buf_chk() will
18229 * overwrite the string. */
18230 strval = vim_strsave(buf);
18231 if (strval == NULL)
18232 goto free_lstval;
18233 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018234 }
18235 *curval++ = strval;
18236 }
18237 *curval++ = NULL;
18238
18239 write_reg_contents_lst(regname, lstval, -1,
18240 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018241free_lstval:
18242 while (curallocval > allocval)
18243 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018244 vim_free(lstval);
18245 }
18246 else
18247 {
18248 strval = get_tv_string_chk(&argvars[1]);
18249 if (strval == NULL)
18250 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018251 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018254 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255}
18256
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018257/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018258 * "settabvar()" function
18259 */
18260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018261f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018262{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018263#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018264 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018265 tabpage_T *tp;
18266#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018267 char_u *varname, *tabvarname;
18268 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018269
18270 rettv->vval.v_number = 0;
18271
18272 if (check_restricted() || check_secure())
18273 return;
18274
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018275#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018276 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018277#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018278 varname = get_tv_string_chk(&argvars[1]);
18279 varp = &argvars[2];
18280
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018281 if (varname != NULL && varp != NULL
18282#ifdef FEAT_WINDOWS
18283 && tp != NULL
18284#endif
18285 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018286 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018287#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018288 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018289 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018290#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018291
18292 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18293 if (tabvarname != NULL)
18294 {
18295 STRCPY(tabvarname, "t:");
18296 STRCPY(tabvarname + 2, varname);
18297 set_var(tabvarname, varp, TRUE);
18298 vim_free(tabvarname);
18299 }
18300
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018301#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018302 /* Restore current tabpage */
18303 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018304 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018305#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018306 }
18307}
18308
18309/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018310 * "settabwinvar()" function
18311 */
18312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018313f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018314{
18315 setwinvar(argvars, rettv, 1);
18316}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317
18318/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018319 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018322f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018324 setwinvar(argvars, rettv, 0);
18325}
18326
18327/*
18328 * "setwinvar()" and "settabwinvar()" functions
18329 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018330
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018332setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018333{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 win_T *win;
18335#ifdef FEAT_WINDOWS
18336 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018337 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018338 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339#endif
18340 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018341 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018343 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344
18345 if (check_restricted() || check_secure())
18346 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018347
18348#ifdef FEAT_WINDOWS
18349 if (off == 1)
18350 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18351 else
18352 tp = curtab;
18353#endif
18354 win = find_win_by_nr(&argvars[off], tp);
18355 varname = get_tv_string_chk(&argvars[off + 1]);
18356 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357
18358 if (win != NULL && varname != NULL && varp != NULL)
18359 {
18360#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018361 need_switch_win = !(tp == curtab && win == curwin);
18362 if (!need_switch_win
18363 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018366 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018368 long numval;
18369 char_u *strval;
18370 int error = FALSE;
18371
18372 ++varname;
18373 numval = get_tv_number_chk(varp, &error);
18374 strval = get_tv_string_buf_chk(varp, nbuf);
18375 if (!error && strval != NULL)
18376 set_option_value(varname, numval, strval, OPT_LOCAL);
18377 }
18378 else
18379 {
18380 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18381 if (winvarname != NULL)
18382 {
18383 STRCPY(winvarname, "w:");
18384 STRCPY(winvarname + 2, varname);
18385 set_var(winvarname, varp, TRUE);
18386 vim_free(winvarname);
18387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 }
18389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018391 if (need_switch_win)
18392 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393#endif
18394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395}
18396
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018397#ifdef FEAT_CRYPT
18398/*
18399 * "sha256({string})" function
18400 */
18401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018402f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018403{
18404 char_u *p;
18405
18406 p = get_tv_string(&argvars[0]);
18407 rettv->vval.v_string = vim_strsave(
18408 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18409 rettv->v_type = VAR_STRING;
18410}
18411#endif /* FEAT_CRYPT */
18412
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018414 * "shellescape({string})" function
18415 */
18416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018417f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018418{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018419 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018420 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018421 rettv->v_type = VAR_STRING;
18422}
18423
18424/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018425 * shiftwidth() function
18426 */
18427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018428f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018429{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018430 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018431}
18432
18433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018434 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 */
18436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018437f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018439 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440
Bram Moolenaar0d660222005-01-07 21:51:51 +000018441 p = get_tv_string(&argvars[0]);
18442 rettv->vval.v_string = vim_strsave(p);
18443 simplify_filename(rettv->vval.v_string); /* simplify in place */
18444 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445}
18446
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018447#ifdef FEAT_FLOAT
18448/*
18449 * "sin()" function
18450 */
18451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018452f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018453{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018454 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018455
18456 rettv->v_type = VAR_FLOAT;
18457 if (get_float_arg(argvars, &f) == OK)
18458 rettv->vval.v_float = sin(f);
18459 else
18460 rettv->vval.v_float = 0.0;
18461}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018462
18463/*
18464 * "sinh()" function
18465 */
18466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018467f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018468{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018469 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018470
18471 rettv->v_type = VAR_FLOAT;
18472 if (get_float_arg(argvars, &f) == OK)
18473 rettv->vval.v_float = sinh(f);
18474 else
18475 rettv->vval.v_float = 0.0;
18476}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018477#endif
18478
Bram Moolenaar0d660222005-01-07 21:51:51 +000018479static int
18480#ifdef __BORLANDC__
18481 _RTLENTRYF
18482#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018483 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018484static int
18485#ifdef __BORLANDC__
18486 _RTLENTRYF
18487#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018488 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018489
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018490/* struct used in the array that's given to qsort() */
18491typedef struct
18492{
18493 listitem_T *item;
18494 int idx;
18495} sortItem_T;
18496
Bram Moolenaar0b962472016-02-22 22:51:33 +010018497/* struct storing information about current sort */
18498typedef struct
18499{
18500 int item_compare_ic;
18501 int item_compare_numeric;
18502 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018503#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018504 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018505#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018506 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018507 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018508 dict_T *item_compare_selfdict;
18509 int item_compare_func_err;
18510 int item_compare_keep_zero;
18511} sortinfo_T;
18512static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018513static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018514#define ITEM_COMPARE_FAIL 999
18515
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018517 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018519 static int
18520#ifdef __BORLANDC__
18521_RTLENTRYF
18522#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018523item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018525 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018526 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018527 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018528 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018529 int res;
18530 char_u numbuf1[NUMBUFLEN];
18531 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018533 si1 = (sortItem_T *)s1;
18534 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018535 tv1 = &si1->item->li_tv;
18536 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018537
Bram Moolenaar0b962472016-02-22 22:51:33 +010018538 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018539 {
18540 long v1 = get_tv_number(tv1);
18541 long v2 = get_tv_number(tv2);
18542
18543 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18544 }
18545
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018546#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018547 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018548 {
18549 float_T v1 = get_tv_float(tv1);
18550 float_T v2 = get_tv_float(tv2);
18551
18552 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18553 }
18554#endif
18555
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018556 /* tv2string() puts quotes around a string and allocates memory. Don't do
18557 * that for string variables. Use a single quote when comparing with a
18558 * non-string to do what the docs promise. */
18559 if (tv1->v_type == VAR_STRING)
18560 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018561 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018562 p1 = (char_u *)"'";
18563 else
18564 p1 = tv1->vval.v_string;
18565 }
18566 else
18567 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18568 if (tv2->v_type == VAR_STRING)
18569 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018570 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018571 p2 = (char_u *)"'";
18572 else
18573 p2 = tv2->vval.v_string;
18574 }
18575 else
18576 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018577 if (p1 == NULL)
18578 p1 = (char_u *)"";
18579 if (p2 == NULL)
18580 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018581 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018582 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018583 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018584 res = STRICMP(p1, p2);
18585 else
18586 res = STRCMP(p1, p2);
18587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018589 {
18590 double n1, n2;
18591 n1 = strtod((char *)p1, (char **)&p1);
18592 n2 = strtod((char *)p2, (char **)&p2);
18593 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18594 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018595
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018596 /* When the result would be zero, compare the item indexes. Makes the
18597 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018598 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018599 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018600
Bram Moolenaar0d660222005-01-07 21:51:51 +000018601 vim_free(tofree1);
18602 vim_free(tofree2);
18603 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604}
18605
18606 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018607#ifdef __BORLANDC__
18608_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018610item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018611{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018612 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018613 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018614 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018615 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018616 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018617 char_u *func_name;
18618 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018620 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018621 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018622 return 0;
18623
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018624 si1 = (sortItem_T *)s1;
18625 si2 = (sortItem_T *)s2;
18626
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018627 if (partial == NULL)
18628 func_name = sortinfo->item_compare_func;
18629 else
18630 func_name = partial->pt_name;
18631
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018632 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018633 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018634 copy_tv(&si1->item->li_tv, &argv[0]);
18635 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018636
18637 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018638 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018639 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018640 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018641 clear_tv(&argv[0]);
18642 clear_tv(&argv[1]);
18643
18644 if (res == FAIL)
18645 res = ITEM_COMPARE_FAIL;
18646 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018647 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18648 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018649 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018650 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018651
18652 /* When the result would be zero, compare the pointers themselves. Makes
18653 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018654 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018655 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018656
Bram Moolenaar0d660222005-01-07 21:51:51 +000018657 return res;
18658}
18659
18660/*
18661 * "sort({list})" function
18662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018664do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665{
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 list_T *l;
18667 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018668 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018669 sortinfo_T *old_sortinfo;
18670 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018671 long len;
18672 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673
Bram Moolenaar0b962472016-02-22 22:51:33 +010018674 /* Pointer to current info struct used in compare function. Save and
18675 * restore the current one for nested calls. */
18676 old_sortinfo = sortinfo;
18677 sortinfo = &info;
18678
Bram Moolenaar0d660222005-01-07 21:51:51 +000018679 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018680 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 else
18682 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018683 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018684 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018685 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18686 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018687 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018688 rettv->vval.v_list = l;
18689 rettv->v_type = VAR_LIST;
18690 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691
Bram Moolenaar0d660222005-01-07 21:51:51 +000018692 len = list_len(l);
18693 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018694 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695
Bram Moolenaar0b962472016-02-22 22:51:33 +010018696 info.item_compare_ic = FALSE;
18697 info.item_compare_numeric = FALSE;
18698 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018699#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018700 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018701#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018702 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018703 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018704 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018705 if (argvars[1].v_type != VAR_UNKNOWN)
18706 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018707 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018708 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018709 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018710 else if (argvars[1].v_type == VAR_PARTIAL)
18711 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018712 else
18713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018714 int error = FALSE;
18715
18716 i = get_tv_number_chk(&argvars[1], &error);
18717 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018718 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018719 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018720 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018721 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018722 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018723 else if (i != 0)
18724 {
18725 EMSG(_(e_invarg));
18726 goto theend;
18727 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018728 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018729 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018730 if (*info.item_compare_func == NUL)
18731 {
18732 /* empty string means default sort */
18733 info.item_compare_func = NULL;
18734 }
18735 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018736 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018737 info.item_compare_func = NULL;
18738 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018739 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018740 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018741 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018742 info.item_compare_func = NULL;
18743 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018744 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018745#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018746 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018747 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018748 info.item_compare_func = NULL;
18749 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018750 }
18751#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018752 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018753 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018754 info.item_compare_func = NULL;
18755 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018756 }
18757 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018758 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018759
18760 if (argvars[2].v_type != VAR_UNKNOWN)
18761 {
18762 /* optional third argument: {dict} */
18763 if (argvars[2].v_type != VAR_DICT)
18764 {
18765 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018766 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018767 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018768 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018769 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771
Bram Moolenaar0d660222005-01-07 21:51:51 +000018772 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018773 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018774 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018775 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776
Bram Moolenaar327aa022014-03-25 18:24:23 +010018777 i = 0;
18778 if (sort)
18779 {
18780 /* sort(): ptrs will be the list to sort */
18781 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018782 {
18783 ptrs[i].item = li;
18784 ptrs[i].idx = i;
18785 ++i;
18786 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018787
Bram Moolenaar0b962472016-02-22 22:51:33 +010018788 info.item_compare_func_err = FALSE;
18789 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018790 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018791 if ((info.item_compare_func != NULL
18792 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018793 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018794 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018795 EMSG(_("E702: Sort compare function failed"));
18796 else
18797 {
18798 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018799 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018800 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018801 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018802 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018803
Bram Moolenaar0b962472016-02-22 22:51:33 +010018804 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018805 {
18806 /* Clear the List and append the items in sorted order. */
18807 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18808 l->lv_len = 0;
18809 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018810 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018811 }
18812 }
18813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018815 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018816 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018817
18818 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018819 info.item_compare_func_err = FALSE;
18820 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018821 item_compare_func_ptr = info.item_compare_func != NULL
18822 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018823 ? item_compare2 : item_compare;
18824
18825 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18826 li = li->li_next)
18827 {
18828 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18829 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018830 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018831 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018832 {
18833 EMSG(_("E882: Uniq compare function failed"));
18834 break;
18835 }
18836 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018837
Bram Moolenaar0b962472016-02-22 22:51:33 +010018838 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018839 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018840 while (--i >= 0)
18841 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018842 li = ptrs[i].item->li_next;
18843 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018844 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018845 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018846 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018847 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018848 list_fix_watch(l, li);
18849 listitem_free(li);
18850 l->lv_len--;
18851 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018852 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018853 }
18854
18855 vim_free(ptrs);
18856 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018857theend:
18858 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018859}
18860
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018861/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018862 * "sort({list})" function
18863 */
18864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018865f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018866{
18867 do_sort_uniq(argvars, rettv, TRUE);
18868}
18869
18870/*
18871 * "uniq({list})" function
18872 */
18873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018874f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018875{
18876 do_sort_uniq(argvars, rettv, FALSE);
18877}
18878
18879/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018880 * "soundfold({word})" function
18881 */
18882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018883f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018884{
18885 char_u *s;
18886
18887 rettv->v_type = VAR_STRING;
18888 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018889#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018890 rettv->vval.v_string = eval_soundfold(s);
18891#else
18892 rettv->vval.v_string = vim_strsave(s);
18893#endif
18894}
18895
18896/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018897 * "spellbadword()" function
18898 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018900f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018901{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018902 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018903 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018904 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018905
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018906 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018907 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018908
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018909#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018910 if (argvars[0].v_type == VAR_UNKNOWN)
18911 {
18912 /* Find the start and length of the badly spelled word. */
18913 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18914 if (len != 0)
18915 word = ml_get_cursor();
18916 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018917 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018918 {
18919 char_u *str = get_tv_string_chk(&argvars[0]);
18920 int capcol = -1;
18921
18922 if (str != NULL)
18923 {
18924 /* Check the argument for spelling. */
18925 while (*str != NUL)
18926 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018927 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018928 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018929 {
18930 word = str;
18931 break;
18932 }
18933 str += len;
18934 }
18935 }
18936 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018937#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018938
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018939 list_append_string(rettv->vval.v_list, word, len);
18940 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018941 attr == HLF_SPB ? "bad" :
18942 attr == HLF_SPR ? "rare" :
18943 attr == HLF_SPL ? "local" :
18944 attr == HLF_SPC ? "caps" :
18945 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018946}
18947
18948/*
18949 * "spellsuggest()" function
18950 */
18951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018952f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018953{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018954#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018955 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018956 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018957 int maxcount;
18958 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018959 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018960 listitem_T *li;
18961 int need_capital = FALSE;
18962#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018964 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018965 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018966
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018967#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018968 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018969 {
18970 str = get_tv_string(&argvars[0]);
18971 if (argvars[1].v_type != VAR_UNKNOWN)
18972 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018973 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018974 if (maxcount <= 0)
18975 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018976 if (argvars[2].v_type != VAR_UNKNOWN)
18977 {
18978 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18979 if (typeerr)
18980 return;
18981 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018982 }
18983 else
18984 maxcount = 25;
18985
Bram Moolenaar4770d092006-01-12 23:22:24 +000018986 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018987
18988 for (i = 0; i < ga.ga_len; ++i)
18989 {
18990 str = ((char_u **)ga.ga_data)[i];
18991
18992 li = listitem_alloc();
18993 if (li == NULL)
18994 vim_free(str);
18995 else
18996 {
18997 li->li_tv.v_type = VAR_STRING;
18998 li->li_tv.v_lock = 0;
18999 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019000 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019001 }
19002 }
19003 ga_clear(&ga);
19004 }
19005#endif
19006}
19007
Bram Moolenaar0d660222005-01-07 21:51:51 +000019008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019009f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019010{
19011 char_u *str;
19012 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019013 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014 regmatch_T regmatch;
19015 char_u patbuf[NUMBUFLEN];
19016 char_u *save_cpo;
19017 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019018 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019019 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019020 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021
19022 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19023 save_cpo = p_cpo;
19024 p_cpo = (char_u *)"";
19025
19026 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019027 if (argvars[1].v_type != VAR_UNKNOWN)
19028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019029 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19030 if (pat == NULL)
19031 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019032 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019033 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019034 }
19035 if (pat == NULL || *pat == NUL)
19036 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019037
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019038 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019040 if (typeerr)
19041 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042
Bram Moolenaar0d660222005-01-07 21:51:51 +000019043 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19044 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019046 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019047 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019048 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019049 if (*str == NUL)
19050 match = FALSE; /* empty item at the end */
19051 else
19052 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053 if (match)
19054 end = regmatch.startp[0];
19055 else
19056 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019057 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19058 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019059 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019060 if (list_append_string(rettv->vval.v_list, str,
19061 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019062 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019063 }
19064 if (!match)
19065 break;
19066 /* Advance to just after the match. */
19067 if (regmatch.endp[0] > str)
19068 col = 0;
19069 else
19070 {
19071 /* Don't get stuck at the same match. */
19072#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019073 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019074#else
19075 col = 1;
19076#endif
19077 }
19078 str = regmatch.endp[0];
19079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080
Bram Moolenaar473de612013-06-08 18:19:48 +020019081 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083
Bram Moolenaar0d660222005-01-07 21:51:51 +000019084 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085}
19086
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019087#ifdef FEAT_FLOAT
19088/*
19089 * "sqrt()" function
19090 */
19091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019092f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019093{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019094 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019095
19096 rettv->v_type = VAR_FLOAT;
19097 if (get_float_arg(argvars, &f) == OK)
19098 rettv->vval.v_float = sqrt(f);
19099 else
19100 rettv->vval.v_float = 0.0;
19101}
19102
19103/*
19104 * "str2float()" function
19105 */
19106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019107f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019108{
19109 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19110
19111 if (*p == '+')
19112 p = skipwhite(p + 1);
19113 (void)string2float(p, &rettv->vval.v_float);
19114 rettv->v_type = VAR_FLOAT;
19115}
19116#endif
19117
Bram Moolenaar2c932302006-03-18 21:42:09 +000019118/*
19119 * "str2nr()" function
19120 */
19121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019122f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019123{
19124 int base = 10;
19125 char_u *p;
19126 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019127 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019128
19129 if (argvars[1].v_type != VAR_UNKNOWN)
19130 {
19131 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019132 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019133 {
19134 EMSG(_(e_invarg));
19135 return;
19136 }
19137 }
19138
19139 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019140 if (*p == '+')
19141 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019142 switch (base)
19143 {
19144 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19145 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19146 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19147 default: what = 0;
19148 }
19149 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019150 rettv->vval.v_number = n;
19151}
19152
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153#ifdef HAVE_STRFTIME
19154/*
19155 * "strftime({format}[, {time}])" function
19156 */
19157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019158f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159{
19160 char_u result_buf[256];
19161 struct tm *curtime;
19162 time_t seconds;
19163 char_u *p;
19164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019165 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019168 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 seconds = time(NULL);
19170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019171 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 curtime = localtime(&seconds);
19173 /* MSVC returns NULL for an invalid value of seconds. */
19174 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 else
19177 {
19178# ifdef FEAT_MBYTE
19179 vimconv_T conv;
19180 char_u *enc;
19181
19182 conv.vc_type = CONV_NONE;
19183 enc = enc_locale();
19184 convert_setup(&conv, p_enc, enc);
19185 if (conv.vc_type != CONV_NONE)
19186 p = string_convert(&conv, p, NULL);
19187# endif
19188 if (p != NULL)
19189 (void)strftime((char *)result_buf, sizeof(result_buf),
19190 (char *)p, curtime);
19191 else
19192 result_buf[0] = NUL;
19193
19194# ifdef FEAT_MBYTE
19195 if (conv.vc_type != CONV_NONE)
19196 vim_free(p);
19197 convert_setup(&conv, enc, p_enc);
19198 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019199 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200 else
19201# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019202 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203
19204# ifdef FEAT_MBYTE
19205 /* Release conversion descriptors */
19206 convert_setup(&conv, NULL, NULL);
19207 vim_free(enc);
19208# endif
19209 }
19210}
19211#endif
19212
19213/*
19214 * "stridx()" function
19215 */
19216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019217f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218{
19219 char_u buf[NUMBUFLEN];
19220 char_u *needle;
19221 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019222 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019224 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019226 needle = get_tv_string_chk(&argvars[1]);
19227 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019228 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019229 if (needle == NULL || haystack == NULL)
19230 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231
Bram Moolenaar33570922005-01-25 22:26:29 +000019232 if (argvars[2].v_type != VAR_UNKNOWN)
19233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019234 int error = FALSE;
19235
19236 start_idx = get_tv_number_chk(&argvars[2], &error);
19237 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019238 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019239 if (start_idx >= 0)
19240 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019241 }
19242
19243 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19244 if (pos != NULL)
19245 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246}
19247
19248/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019249 * "string()" function
19250 */
19251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019252f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019253{
19254 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019255 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019257 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019258 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19259 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019260 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019261 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019262 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263}
19264
19265/*
19266 * "strlen()" function
19267 */
19268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019269f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019271 rettv->vval.v_number = (varnumber_T)(STRLEN(
19272 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273}
19274
19275/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019276 * "strchars()" function
19277 */
19278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019279f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019280{
19281 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019282 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019283#ifdef FEAT_MBYTE
19284 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019285 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019286#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019287
19288 if (argvars[1].v_type != VAR_UNKNOWN)
19289 skipcc = get_tv_number_chk(&argvars[1], NULL);
19290 if (skipcc < 0 || skipcc > 1)
19291 EMSG(_(e_invarg));
19292 else
19293 {
19294#ifdef FEAT_MBYTE
19295 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19296 while (*s != NUL)
19297 {
19298 func_mb_ptr2char_adv(&s);
19299 ++len;
19300 }
19301 rettv->vval.v_number = len;
19302#else
19303 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19304#endif
19305 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019306}
19307
19308/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019309 * "strdisplaywidth()" function
19310 */
19311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019312f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019313{
19314 char_u *s = get_tv_string(&argvars[0]);
19315 int col = 0;
19316
19317 if (argvars[1].v_type != VAR_UNKNOWN)
19318 col = get_tv_number(&argvars[1]);
19319
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019320 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019321}
19322
19323/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019324 * "strwidth()" function
19325 */
19326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019327f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019328{
19329 char_u *s = get_tv_string(&argvars[0]);
19330
19331 rettv->vval.v_number = (varnumber_T)(
19332#ifdef FEAT_MBYTE
19333 mb_string2cells(s, -1)
19334#else
19335 STRLEN(s)
19336#endif
19337 );
19338}
19339
19340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 * "strpart()" function
19342 */
19343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019344f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345{
19346 char_u *p;
19347 int n;
19348 int len;
19349 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019350 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019352 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 slen = (int)STRLEN(p);
19354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019355 n = get_tv_number_chk(&argvars[1], &error);
19356 if (error)
19357 len = 0;
19358 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019359 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 else
19361 len = slen - n; /* default len: all bytes that are available. */
19362
19363 /*
19364 * Only return the overlap between the specified part and the actual
19365 * string.
19366 */
19367 if (n < 0)
19368 {
19369 len += n;
19370 n = 0;
19371 }
19372 else if (n > slen)
19373 n = slen;
19374 if (len < 0)
19375 len = 0;
19376 else if (n + len > slen)
19377 len = slen - n;
19378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019379 rettv->v_type = VAR_STRING;
19380 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381}
19382
19383/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019384 * "strridx()" function
19385 */
19386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019387f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019388{
19389 char_u buf[NUMBUFLEN];
19390 char_u *needle;
19391 char_u *haystack;
19392 char_u *rest;
19393 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019394 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019396 needle = get_tv_string_chk(&argvars[1]);
19397 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019398
19399 rettv->vval.v_number = -1;
19400 if (needle == NULL || haystack == NULL)
19401 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019402
19403 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019404 if (argvars[2].v_type != VAR_UNKNOWN)
19405 {
19406 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019407 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019408 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019409 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019410 }
19411 else
19412 end_idx = haystack_len;
19413
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019415 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019416 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019417 lastmatch = haystack + end_idx;
19418 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019419 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019420 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019421 for (rest = haystack; *rest != '\0'; ++rest)
19422 {
19423 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019424 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019425 break;
19426 lastmatch = rest;
19427 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019428 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019429
19430 if (lastmatch == NULL)
19431 rettv->vval.v_number = -1;
19432 else
19433 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19434}
19435
19436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 * "strtrans()" function
19438 */
19439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019440f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019442 rettv->v_type = VAR_STRING;
19443 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444}
19445
19446/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019447 * "submatch()" function
19448 */
19449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019450f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019451{
Bram Moolenaar41571762014-04-02 19:00:58 +020019452 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019453 int no;
19454 int retList = 0;
19455
19456 no = (int)get_tv_number_chk(&argvars[0], &error);
19457 if (error)
19458 return;
19459 error = FALSE;
19460 if (argvars[1].v_type != VAR_UNKNOWN)
19461 retList = get_tv_number_chk(&argvars[1], &error);
19462 if (error)
19463 return;
19464
19465 if (retList == 0)
19466 {
19467 rettv->v_type = VAR_STRING;
19468 rettv->vval.v_string = reg_submatch(no);
19469 }
19470 else
19471 {
19472 rettv->v_type = VAR_LIST;
19473 rettv->vval.v_list = reg_submatch_list(no);
19474 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019475}
19476
19477/*
19478 * "substitute()" function
19479 */
19480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019481f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019482{
19483 char_u patbuf[NUMBUFLEN];
19484 char_u subbuf[NUMBUFLEN];
19485 char_u flagsbuf[NUMBUFLEN];
19486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019487 char_u *str = get_tv_string_chk(&argvars[0]);
19488 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19489 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19490 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19491
Bram Moolenaar0d660222005-01-07 21:51:51 +000019492 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019493 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19494 rettv->vval.v_string = NULL;
19495 else
19496 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019497}
19498
19499/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019500 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019503f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504{
19505 int id = 0;
19506#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019507 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 long col;
19509 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019510 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019512 lnum = get_tv_lnum(argvars); /* -1 on type error */
19513 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19514 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019516 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019517 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019518 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519#endif
19520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019521 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522}
19523
19524/*
19525 * "synIDattr(id, what [, mode])" function
19526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019528f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529{
19530 char_u *p = NULL;
19531#ifdef FEAT_SYN_HL
19532 int id;
19533 char_u *what;
19534 char_u *mode;
19535 char_u modebuf[NUMBUFLEN];
19536 int modec;
19537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019538 id = get_tv_number(&argvars[0]);
19539 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019540 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019542 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019544 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 modec = 0; /* replace invalid with current */
19546 }
19547 else
19548 {
19549#ifdef FEAT_GUI
19550 if (gui.in_use)
19551 modec = 'g';
19552 else
19553#endif
19554 if (t_colors > 1)
19555 modec = 'c';
19556 else
19557 modec = 't';
19558 }
19559
19560
19561 switch (TOLOWER_ASC(what[0]))
19562 {
19563 case 'b':
19564 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19565 p = highlight_color(id, what, modec);
19566 else /* bold */
19567 p = highlight_has_attr(id, HL_BOLD, modec);
19568 break;
19569
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019570 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 p = highlight_color(id, what, modec);
19572 break;
19573
19574 case 'i':
19575 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19576 p = highlight_has_attr(id, HL_INVERSE, modec);
19577 else /* italic */
19578 p = highlight_has_attr(id, HL_ITALIC, modec);
19579 break;
19580
19581 case 'n': /* name */
19582 p = get_highlight_name(NULL, id - 1);
19583 break;
19584
19585 case 'r': /* reverse */
19586 p = highlight_has_attr(id, HL_INVERSE, modec);
19587 break;
19588
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019589 case 's':
19590 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19591 p = highlight_color(id, what, modec);
19592 else /* standout */
19593 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 break;
19595
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019596 case 'u':
19597 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19598 /* underline */
19599 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19600 else
19601 /* undercurl */
19602 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603 break;
19604 }
19605
19606 if (p != NULL)
19607 p = vim_strsave(p);
19608#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609 rettv->v_type = VAR_STRING;
19610 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611}
19612
19613/*
19614 * "synIDtrans(id)" function
19615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019617f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618{
19619 int id;
19620
19621#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019622 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623
19624 if (id > 0)
19625 id = syn_get_final_id(id);
19626 else
19627#endif
19628 id = 0;
19629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019630 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631}
19632
19633/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019634 * "synconcealed(lnum, col)" function
19635 */
19636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019637f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019638{
19639#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19640 long lnum;
19641 long col;
19642 int syntax_flags = 0;
19643 int cchar;
19644 int matchid = 0;
19645 char_u str[NUMBUFLEN];
19646#endif
19647
19648 rettv->v_type = VAR_LIST;
19649 rettv->vval.v_list = NULL;
19650
19651#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19652 lnum = get_tv_lnum(argvars); /* -1 on type error */
19653 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19654
19655 vim_memset(str, NUL, sizeof(str));
19656
19657 if (rettv_list_alloc(rettv) != FAIL)
19658 {
19659 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19660 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19661 && curwin->w_p_cole > 0)
19662 {
19663 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19664 syntax_flags = get_syntax_info(&matchid);
19665
19666 /* get the conceal character */
19667 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19668 {
19669 cchar = syn_get_sub_char();
19670 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19671 cchar = lcs_conceal;
19672 if (cchar != NUL)
19673 {
19674# ifdef FEAT_MBYTE
19675 if (has_mbyte)
19676 (*mb_char2bytes)(cchar, str);
19677 else
19678# endif
19679 str[0] = cchar;
19680 }
19681 }
19682 }
19683
19684 list_append_number(rettv->vval.v_list,
19685 (syntax_flags & HL_CONCEAL) != 0);
19686 /* -1 to auto-determine strlen */
19687 list_append_string(rettv->vval.v_list, str, -1);
19688 list_append_number(rettv->vval.v_list, matchid);
19689 }
19690#endif
19691}
19692
19693/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019694 * "synstack(lnum, col)" function
19695 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019697f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019698{
19699#ifdef FEAT_SYN_HL
19700 long lnum;
19701 long col;
19702 int i;
19703 int id;
19704#endif
19705
19706 rettv->v_type = VAR_LIST;
19707 rettv->vval.v_list = NULL;
19708
19709#ifdef FEAT_SYN_HL
19710 lnum = get_tv_lnum(argvars); /* -1 on type error */
19711 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19712
19713 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019714 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019715 && rettv_list_alloc(rettv) != FAIL)
19716 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019717 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019718 for (i = 0; ; ++i)
19719 {
19720 id = syn_get_stack_item(i);
19721 if (id < 0)
19722 break;
19723 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19724 break;
19725 }
19726 }
19727#endif
19728}
19729
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019731get_cmd_output_as_rettv(
19732 typval_T *argvars,
19733 typval_T *rettv,
19734 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019736 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019738 char_u *infile = NULL;
19739 char_u buf[NUMBUFLEN];
19740 int err = FALSE;
19741 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019742 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019743 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019745 rettv->v_type = VAR_STRING;
19746 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019747 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019748 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019749
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019750 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019751 {
19752 /*
19753 * Write the string to a temp file, to be used for input of the shell
19754 * command.
19755 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019756 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019757 {
19758 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019759 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019760 }
19761
19762 fd = mch_fopen((char *)infile, WRITEBIN);
19763 if (fd == NULL)
19764 {
19765 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019766 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019767 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019768 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019769 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019770 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19771 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019772 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019773 else
19774 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019775 size_t len;
19776
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019777 p = get_tv_string_buf_chk(&argvars[1], buf);
19778 if (p == NULL)
19779 {
19780 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019781 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019782 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019783 len = STRLEN(p);
19784 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019785 err = TRUE;
19786 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019787 if (fclose(fd) != 0)
19788 err = TRUE;
19789 if (err)
19790 {
19791 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019792 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019793 }
19794 }
19795
Bram Moolenaar52a72462014-08-29 15:53:52 +020019796 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19797 * echoes typeahead, that messes up the display. */
19798 if (!msg_silent)
19799 flags += SHELL_COOKED;
19800
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019801 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019803 int len;
19804 listitem_T *li;
19805 char_u *s = NULL;
19806 char_u *start;
19807 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019808 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809
Bram Moolenaar52a72462014-08-29 15:53:52 +020019810 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019811 if (res == NULL)
19812 goto errret;
19813
19814 list = list_alloc();
19815 if (list == NULL)
19816 goto errret;
19817
19818 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019820 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019821 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019822 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019823 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019824
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019825 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019826 if (s == NULL)
19827 goto errret;
19828
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019829 for (p = s; start < end; ++p, ++start)
19830 *p = *start == NUL ? NL : *start;
19831 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019832
19833 li = listitem_alloc();
19834 if (li == NULL)
19835 {
19836 vim_free(s);
19837 goto errret;
19838 }
19839 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019840 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019841 li->li_tv.vval.v_string = s;
19842 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019844
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019845 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019846 rettv->v_type = VAR_LIST;
19847 rettv->vval.v_list = list;
19848 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019850 else
19851 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019852 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019853#ifdef USE_CR
19854 /* translate <CR> into <NL> */
19855 if (res != NULL)
19856 {
19857 char_u *s;
19858
19859 for (s = res; *s; ++s)
19860 {
19861 if (*s == CAR)
19862 *s = NL;
19863 }
19864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865#else
19866# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019867 /* translate <CR><NL> into <NL> */
19868 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019870 char_u *s, *d;
19871
19872 d = res;
19873 for (s = res; *s; ++s)
19874 {
19875 if (s[0] == CAR && s[1] == NL)
19876 ++s;
19877 *d++ = *s;
19878 }
19879 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881# endif
19882#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019883 rettv->vval.v_string = res;
19884 res = NULL;
19885 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019886
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019887errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019888 if (infile != NULL)
19889 {
19890 mch_remove(infile);
19891 vim_free(infile);
19892 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019893 if (res != NULL)
19894 vim_free(res);
19895 if (list != NULL)
19896 list_free(list, TRUE);
19897}
19898
19899/*
19900 * "system()" function
19901 */
19902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019903f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019904{
19905 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19906}
19907
19908/*
19909 * "systemlist()" function
19910 */
19911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019912f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019913{
19914 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915}
19916
19917/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019918 * "tabpagebuflist()" function
19919 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019921f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019922{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019923#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019924 tabpage_T *tp;
19925 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019926
19927 if (argvars[0].v_type == VAR_UNKNOWN)
19928 wp = firstwin;
19929 else
19930 {
19931 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19932 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019933 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019934 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019935 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019936 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019937 for (; wp != NULL; wp = wp->w_next)
19938 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019939 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019940 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019941 }
19942#endif
19943}
19944
19945
19946/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019947 * "tabpagenr()" function
19948 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019950f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019951{
19952 int nr = 1;
19953#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019954 char_u *arg;
19955
19956 if (argvars[0].v_type != VAR_UNKNOWN)
19957 {
19958 arg = get_tv_string_chk(&argvars[0]);
19959 nr = 0;
19960 if (arg != NULL)
19961 {
19962 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019963 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019964 else
19965 EMSG2(_(e_invexpr2), arg);
19966 }
19967 }
19968 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019969 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019970#endif
19971 rettv->vval.v_number = nr;
19972}
19973
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019974
19975#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019976static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019977
19978/*
19979 * Common code for tabpagewinnr() and winnr().
19980 */
19981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019982get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019983{
19984 win_T *twin;
19985 int nr = 1;
19986 win_T *wp;
19987 char_u *arg;
19988
19989 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19990 if (argvar->v_type != VAR_UNKNOWN)
19991 {
19992 arg = get_tv_string_chk(argvar);
19993 if (arg == NULL)
19994 nr = 0; /* type error; errmsg already given */
19995 else if (STRCMP(arg, "$") == 0)
19996 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19997 else if (STRCMP(arg, "#") == 0)
19998 {
19999 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20000 if (twin == NULL)
20001 nr = 0;
20002 }
20003 else
20004 {
20005 EMSG2(_(e_invexpr2), arg);
20006 nr = 0;
20007 }
20008 }
20009
20010 if (nr > 0)
20011 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20012 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020013 {
20014 if (wp == NULL)
20015 {
20016 /* didn't find it in this tabpage */
20017 nr = 0;
20018 break;
20019 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020020 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020021 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020022 return nr;
20023}
20024#endif
20025
20026/*
20027 * "tabpagewinnr()" function
20028 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020030f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020031{
20032 int nr = 1;
20033#ifdef FEAT_WINDOWS
20034 tabpage_T *tp;
20035
20036 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20037 if (tp == NULL)
20038 nr = 0;
20039 else
20040 nr = get_winnr(tp, &argvars[1]);
20041#endif
20042 rettv->vval.v_number = nr;
20043}
20044
20045
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020046/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020047 * "tagfiles()" function
20048 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020050f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020051{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020052 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020053 tagname_T tn;
20054 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020055
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020056 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020057 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020058 fname = alloc(MAXPATHL);
20059 if (fname == NULL)
20060 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020061
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020062 for (first = TRUE; ; first = FALSE)
20063 if (get_tagfname(&tn, first, fname) == FAIL
20064 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020065 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020066 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020067 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020068}
20069
20070/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020071 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020072 */
20073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020074f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020075{
20076 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020077
20078 tag_pattern = get_tv_string(&argvars[0]);
20079
20080 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020081 if (*tag_pattern == NUL)
20082 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020084 if (rettv_list_alloc(rettv) == OK)
20085 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020086}
20087
20088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 * "tempname()" function
20090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020092f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093{
20094 static int x = 'A';
20095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020097 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098
20099 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20100 * names. Skip 'I' and 'O', they are used for shell redirection. */
20101 do
20102 {
20103 if (x == 'Z')
20104 x = '0';
20105 else if (x == '9')
20106 x = 'A';
20107 else
20108 {
20109#ifdef EBCDIC
20110 if (x == 'I')
20111 x = 'J';
20112 else if (x == 'R')
20113 x = 'S';
20114 else
20115#endif
20116 ++x;
20117 }
20118 } while (x == 'I' || x == 'O');
20119}
20120
20121/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020122 * "test(list)" function: Just checking the walls...
20123 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020125f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020126{
20127 /* Used for unit testing. Change the code below to your liking. */
20128#if 0
20129 listitem_T *li;
20130 list_T *l;
20131 char_u *bad, *good;
20132
20133 if (argvars[0].v_type != VAR_LIST)
20134 return;
20135 l = argvars[0].vval.v_list;
20136 if (l == NULL)
20137 return;
20138 li = l->lv_first;
20139 if (li == NULL)
20140 return;
20141 bad = get_tv_string(&li->li_tv);
20142 li = li->li_next;
20143 if (li == NULL)
20144 return;
20145 good = get_tv_string(&li->li_tv);
20146 rettv->vval.v_number = test_edit_score(bad, good);
20147#endif
20148}
20149
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020150#ifdef FEAT_FLOAT
20151/*
20152 * "tan()" function
20153 */
20154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020155f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020156{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020157 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020158
20159 rettv->v_type = VAR_FLOAT;
20160 if (get_float_arg(argvars, &f) == OK)
20161 rettv->vval.v_float = tan(f);
20162 else
20163 rettv->vval.v_float = 0.0;
20164}
20165
20166/*
20167 * "tanh()" function
20168 */
20169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020170f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020171{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020172 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020173
20174 rettv->v_type = VAR_FLOAT;
20175 if (get_float_arg(argvars, &f) == OK)
20176 rettv->vval.v_float = tanh(f);
20177 else
20178 rettv->vval.v_float = 0.0;
20179}
20180#endif
20181
Bram Moolenaar975b5272016-03-15 23:10:59 +010020182#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20183/*
20184 * Get a callback from "arg". It can be a Funcref or a function name.
20185 * When "arg" is zero return an empty string.
20186 * Return NULL for an invalid argument.
20187 */
20188 char_u *
20189get_callback(typval_T *arg, partial_T **pp)
20190{
20191 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20192 {
20193 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020194 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020195 return (*pp)->pt_name;
20196 }
20197 *pp = NULL;
20198 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20199 return arg->vval.v_string;
20200 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20201 return (char_u *)"";
20202 EMSG(_("E921: Invalid callback argument"));
20203 return NULL;
20204}
20205#endif
20206
20207#ifdef FEAT_TIMERS
20208/*
20209 * "timer_start(time, callback [, options])" function
20210 */
20211 static void
20212f_timer_start(typval_T *argvars, typval_T *rettv)
20213{
20214 long msec = get_tv_number(&argvars[0]);
20215 timer_T *timer;
20216 int repeat = 0;
20217 char_u *callback;
20218 dict_T *dict;
20219
20220 if (argvars[2].v_type != VAR_UNKNOWN)
20221 {
20222 if (argvars[2].v_type != VAR_DICT
20223 || (dict = argvars[2].vval.v_dict) == NULL)
20224 {
20225 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20226 return;
20227 }
20228 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20229 repeat = get_dict_number(dict, (char_u *)"repeat");
20230 }
20231
20232 timer = create_timer(msec, repeat);
20233 callback = get_callback(&argvars[1], &timer->tr_partial);
20234 if (callback == NULL)
20235 {
20236 stop_timer(timer);
20237 rettv->vval.v_number = -1;
20238 }
20239 else
20240 {
20241 timer->tr_callback = vim_strsave(callback);
20242 rettv->vval.v_number = timer->tr_id;
20243 }
20244}
20245
20246/*
20247 * "timer_stop(timer)" function
20248 */
20249 static void
20250f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20251{
20252 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20253
20254 if (timer != NULL)
20255 stop_timer(timer);
20256}
20257#endif
20258
Bram Moolenaard52d9742005-08-21 22:20:28 +000020259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 * "tolower(string)" function
20261 */
20262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020263f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264{
20265 char_u *p;
20266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020267 p = vim_strsave(get_tv_string(&argvars[0]));
20268 rettv->v_type = VAR_STRING;
20269 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270
20271 if (p != NULL)
20272 while (*p != NUL)
20273 {
20274#ifdef FEAT_MBYTE
20275 int l;
20276
20277 if (enc_utf8)
20278 {
20279 int c, lc;
20280
20281 c = utf_ptr2char(p);
20282 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020283 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 /* TODO: reallocate string when byte count changes. */
20285 if (utf_char2len(lc) == l)
20286 utf_char2bytes(lc, p);
20287 p += l;
20288 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020289 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 p += l; /* skip multi-byte character */
20291 else
20292#endif
20293 {
20294 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20295 ++p;
20296 }
20297 }
20298}
20299
20300/*
20301 * "toupper(string)" function
20302 */
20303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020304f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020306 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020307 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308}
20309
20310/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020311 * "tr(string, fromstr, tostr)" function
20312 */
20313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020314f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020315{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020316 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020317 char_u *fromstr;
20318 char_u *tostr;
20319 char_u *p;
20320#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020321 int inlen;
20322 int fromlen;
20323 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020324 int idx;
20325 char_u *cpstr;
20326 int cplen;
20327 int first = TRUE;
20328#endif
20329 char_u buf[NUMBUFLEN];
20330 char_u buf2[NUMBUFLEN];
20331 garray_T ga;
20332
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020333 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020334 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20335 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020336
20337 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020338 rettv->v_type = VAR_STRING;
20339 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020340 if (fromstr == NULL || tostr == NULL)
20341 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020342 ga_init2(&ga, (int)sizeof(char), 80);
20343
20344#ifdef FEAT_MBYTE
20345 if (!has_mbyte)
20346#endif
20347 /* not multi-byte: fromstr and tostr must be the same length */
20348 if (STRLEN(fromstr) != STRLEN(tostr))
20349 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020350#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020351error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020352#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020353 EMSG2(_(e_invarg2), fromstr);
20354 ga_clear(&ga);
20355 return;
20356 }
20357
20358 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020359 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020360 {
20361#ifdef FEAT_MBYTE
20362 if (has_mbyte)
20363 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020364 inlen = (*mb_ptr2len)(in_str);
20365 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020366 cplen = inlen;
20367 idx = 0;
20368 for (p = fromstr; *p != NUL; p += fromlen)
20369 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020370 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020371 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020372 {
20373 for (p = tostr; *p != NUL; p += tolen)
20374 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020375 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020376 if (idx-- == 0)
20377 {
20378 cplen = tolen;
20379 cpstr = p;
20380 break;
20381 }
20382 }
20383 if (*p == NUL) /* tostr is shorter than fromstr */
20384 goto error;
20385 break;
20386 }
20387 ++idx;
20388 }
20389
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020390 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020391 {
20392 /* Check that fromstr and tostr have the same number of
20393 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020394 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020395 first = FALSE;
20396 for (p = tostr; *p != NUL; p += tolen)
20397 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020398 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020399 --idx;
20400 }
20401 if (idx != 0)
20402 goto error;
20403 }
20404
Bram Moolenaarcde88542015-08-11 19:14:00 +020020405 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020406 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020407 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020408
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020409 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020410 }
20411 else
20412#endif
20413 {
20414 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020415 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020416 if (p != NULL)
20417 ga_append(&ga, tostr[p - fromstr]);
20418 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020419 ga_append(&ga, *in_str);
20420 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020421 }
20422 }
20423
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020424 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020425 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020426 ga_append(&ga, NUL);
20427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020428 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020429}
20430
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020431#ifdef FEAT_FLOAT
20432/*
20433 * "trunc({float})" function
20434 */
20435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020436f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020437{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020438 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020439
20440 rettv->v_type = VAR_FLOAT;
20441 if (get_float_arg(argvars, &f) == OK)
20442 /* trunc() is not in C90, use floor() or ceil() instead. */
20443 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20444 else
20445 rettv->vval.v_float = 0.0;
20446}
20447#endif
20448
Bram Moolenaar8299df92004-07-10 09:47:34 +000020449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 * "type(expr)" function
20451 */
20452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020453f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020455 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020456
20457 switch (argvars[0].v_type)
20458 {
20459 case VAR_NUMBER: n = 0; break;
20460 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020461 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020462 case VAR_FUNC: n = 2; break;
20463 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020464 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020465 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020466 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020467 if (argvars[0].vval.v_number == VVAL_FALSE
20468 || argvars[0].vval.v_number == VVAL_TRUE)
20469 n = 6;
20470 else
20471 n = 7;
20472 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020473 case VAR_JOB: n = 8; break;
20474 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020475 case VAR_UNKNOWN:
20476 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20477 n = -1;
20478 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020479 }
20480 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481}
20482
20483/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020484 * "undofile(name)" function
20485 */
20486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020487f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020488{
20489 rettv->v_type = VAR_STRING;
20490#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020491 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020492 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020493
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020494 if (*fname == NUL)
20495 {
20496 /* If there is no file name there will be no undo file. */
20497 rettv->vval.v_string = NULL;
20498 }
20499 else
20500 {
20501 char_u *ffname = FullName_save(fname, FALSE);
20502
20503 if (ffname != NULL)
20504 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20505 vim_free(ffname);
20506 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020507 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020508#else
20509 rettv->vval.v_string = NULL;
20510#endif
20511}
20512
20513/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020514 * "undotree()" function
20515 */
20516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020517f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020518{
20519 if (rettv_dict_alloc(rettv) == OK)
20520 {
20521 dict_T *dict = rettv->vval.v_dict;
20522 list_T *list;
20523
Bram Moolenaar730cde92010-06-27 05:18:54 +020020524 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020525 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020526 dict_add_nr_str(dict, "save_last",
20527 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020528 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20529 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020530 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020531
20532 list = list_alloc();
20533 if (list != NULL)
20534 {
20535 u_eval_tree(curbuf->b_u_oldhead, list);
20536 dict_add_list(dict, "entries", list);
20537 }
20538 }
20539}
20540
20541/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020542 * "values(dict)" function
20543 */
20544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020545f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020546{
20547 dict_list(argvars, rettv, 1);
20548}
20549
20550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 * "virtcol(string)" function
20552 */
20553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020554f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555{
20556 colnr_T vcol = 0;
20557 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020558 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020560 fp = var2fpos(&argvars[0], FALSE, &fnum);
20561 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20562 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 {
20564 getvvcol(curwin, fp, NULL, NULL, &vcol);
20565 ++vcol;
20566 }
20567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020568 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569}
20570
20571/*
20572 * "visualmode()" function
20573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020575f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 char_u str[2];
20578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020579 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 str[0] = curbuf->b_visual_mode_eval;
20581 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020582 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583
20584 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020585 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587}
20588
20589/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020590 * "wildmenumode()" function
20591 */
20592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020593f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020594{
20595#ifdef FEAT_WILDMENU
20596 if (wild_menu_showing)
20597 rettv->vval.v_number = 1;
20598#endif
20599}
20600
20601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 * "winbufnr(nr)" function
20603 */
20604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020605f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606{
20607 win_T *wp;
20608
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020609 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020613 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614}
20615
20616/*
20617 * "wincol()" function
20618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020620f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621{
20622 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020623 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624}
20625
20626/*
20627 * "winheight(nr)" function
20628 */
20629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020630f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631{
20632 win_T *wp;
20633
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020634 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020636 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020638 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639}
20640
20641/*
20642 * "winline()" function
20643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020645f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646{
20647 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020648 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649}
20650
20651/*
20652 * "winnr()" function
20653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020655f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656{
20657 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020658
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020660 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020662 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663}
20664
20665/*
20666 * "winrestcmd()" function
20667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020669f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670{
20671#ifdef FEAT_WINDOWS
20672 win_T *wp;
20673 int winnr = 1;
20674 garray_T ga;
20675 char_u buf[50];
20676
20677 ga_init2(&ga, (int)sizeof(char), 70);
20678 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20679 {
20680 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20681 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20683 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 ++winnr;
20685 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020686 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020688 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020692 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693}
20694
20695/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020696 * "winrestview()" function
20697 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020699f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020700{
20701 dict_T *dict;
20702
20703 if (argvars[0].v_type != VAR_DICT
20704 || (dict = argvars[0].vval.v_dict) == NULL)
20705 EMSG(_(e_invarg));
20706 else
20707 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020708 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20709 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20710 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20711 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020712#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020713 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20714 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020715#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020716 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20717 {
20718 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20719 curwin->w_set_curswant = FALSE;
20720 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020721
Bram Moolenaar82c25852014-05-28 16:47:16 +020020722 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20723 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020724#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020725 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20726 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020727#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020728 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20729 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20730 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20731 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020732
20733 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020734 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020735# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020736 win_new_width(curwin, W_WIDTH(curwin));
20737# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020738 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020739
Bram Moolenaarb851a962014-10-31 15:45:52 +010020740 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020741 curwin->w_topline = 1;
20742 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20743 curwin->w_topline = curbuf->b_ml.ml_line_count;
20744#ifdef FEAT_DIFF
20745 check_topfill(curwin, TRUE);
20746#endif
20747 }
20748}
20749
20750/*
20751 * "winsaveview()" function
20752 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020754f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020755{
20756 dict_T *dict;
20757
Bram Moolenaara800b422010-06-27 01:15:55 +020020758 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020759 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020760 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020761
20762 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20763 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20764#ifdef FEAT_VIRTUALEDIT
20765 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20766#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020767 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020768 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20769
20770 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20771#ifdef FEAT_DIFF
20772 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20773#endif
20774 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20775 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20776}
20777
20778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 * "winwidth(nr)" function
20780 */
20781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020782f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783{
20784 win_T *wp;
20785
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020786 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020790#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020791 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020793 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794#endif
20795}
20796
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020798 * "wordcount()" function
20799 */
20800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020801f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020802{
20803 if (rettv_dict_alloc(rettv) == FAIL)
20804 return;
20805 cursor_pos_info(rettv->vval.v_dict);
20806}
20807
20808/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020809 * Write list of strings to file
20810 */
20811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020812write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020813{
20814 listitem_T *li;
20815 int c;
20816 int ret = OK;
20817 char_u *s;
20818
20819 for (li = list->lv_first; li != NULL; li = li->li_next)
20820 {
20821 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20822 {
20823 if (*s == '\n')
20824 c = putc(NUL, fd);
20825 else
20826 c = putc(*s, fd);
20827 if (c == EOF)
20828 {
20829 ret = FAIL;
20830 break;
20831 }
20832 }
20833 if (!binary || li->li_next != NULL)
20834 if (putc('\n', fd) == EOF)
20835 {
20836 ret = FAIL;
20837 break;
20838 }
20839 if (ret == FAIL)
20840 {
20841 EMSG(_(e_write));
20842 break;
20843 }
20844 }
20845 return ret;
20846}
20847
20848/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020849 * "writefile()" function
20850 */
20851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020852f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020853{
20854 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020855 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020856 char_u *fname;
20857 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020858 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020859
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020860 if (check_restricted() || check_secure())
20861 return;
20862
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020863 if (argvars[0].v_type != VAR_LIST)
20864 {
20865 EMSG2(_(e_listarg), "writefile()");
20866 return;
20867 }
20868 if (argvars[0].vval.v_list == NULL)
20869 return;
20870
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020871 if (argvars[2].v_type != VAR_UNKNOWN)
20872 {
20873 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20874 binary = TRUE;
20875 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20876 append = TRUE;
20877 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020878
20879 /* Always open the file in binary mode, library functions have a mind of
20880 * their own about CR-LF conversion. */
20881 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020882 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20883 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020884 {
20885 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20886 ret = -1;
20887 }
20888 else
20889 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020890 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20891 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020892 fclose(fd);
20893 }
20894
20895 rettv->vval.v_number = ret;
20896}
20897
20898/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020899 * "xor(expr, expr)" function
20900 */
20901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020902f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020903{
20904 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20905 ^ get_tv_number_chk(&argvars[1], NULL);
20906}
20907
20908
20909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020911 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 */
20913 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020914var2fpos(
20915 typval_T *varp,
20916 int dollar_lnum, /* TRUE when $ is last line */
20917 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020919 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020921 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922
Bram Moolenaara5525202006-03-02 22:52:09 +000020923 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020924 if (varp->v_type == VAR_LIST)
20925 {
20926 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020927 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020928 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020929 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020930
20931 l = varp->vval.v_list;
20932 if (l == NULL)
20933 return NULL;
20934
20935 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020936 pos.lnum = list_find_nr(l, 0L, &error);
20937 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020938 return NULL; /* invalid line number */
20939
20940 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020941 pos.col = list_find_nr(l, 1L, &error);
20942 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020943 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020944 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020945
20946 /* We accept "$" for the column number: last column. */
20947 li = list_find(l, 1L);
20948 if (li != NULL && li->li_tv.v_type == VAR_STRING
20949 && li->li_tv.vval.v_string != NULL
20950 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20951 pos.col = len + 1;
20952
Bram Moolenaara5525202006-03-02 22:52:09 +000020953 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020954 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020955 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020956 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020957
Bram Moolenaara5525202006-03-02 22:52:09 +000020958#ifdef FEAT_VIRTUALEDIT
20959 /* Get the virtual offset. Defaults to zero. */
20960 pos.coladd = list_find_nr(l, 2L, &error);
20961 if (error)
20962 pos.coladd = 0;
20963#endif
20964
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020965 return &pos;
20966 }
20967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020968 name = get_tv_string_chk(varp);
20969 if (name == NULL)
20970 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020971 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020973 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20974 {
20975 if (VIsual_active)
20976 return &VIsual;
20977 return &curwin->w_cursor;
20978 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020979 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020981 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20983 return NULL;
20984 return pp;
20985 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020986
20987#ifdef FEAT_VIRTUALEDIT
20988 pos.coladd = 0;
20989#endif
20990
Bram Moolenaar477933c2007-07-17 14:32:23 +000020991 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020992 {
20993 pos.col = 0;
20994 if (name[1] == '0') /* "w0": first visible line */
20995 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020996 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020997 pos.lnum = curwin->w_topline;
20998 return &pos;
20999 }
21000 else if (name[1] == '$') /* "w$": last visible line */
21001 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021002 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021003 pos.lnum = curwin->w_botline - 1;
21004 return &pos;
21005 }
21006 }
21007 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021009 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 {
21011 pos.lnum = curbuf->b_ml.ml_line_count;
21012 pos.col = 0;
21013 }
21014 else
21015 {
21016 pos.lnum = curwin->w_cursor.lnum;
21017 pos.col = (colnr_T)STRLEN(ml_get_curline());
21018 }
21019 return &pos;
21020 }
21021 return NULL;
21022}
21023
21024/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021025 * Convert list in "arg" into a position and optional file number.
21026 * When "fnump" is NULL there is no file number, only 3 items.
21027 * Note that the column is passed on as-is, the caller may want to decrement
21028 * it to use 1 for the first column.
21029 * Return FAIL when conversion is not possible, doesn't check the position for
21030 * validity.
21031 */
21032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021033list2fpos(
21034 typval_T *arg,
21035 pos_T *posp,
21036 int *fnump,
21037 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021038{
21039 list_T *l = arg->vval.v_list;
21040 long i = 0;
21041 long n;
21042
Bram Moolenaar493c1782014-05-28 14:34:46 +020021043 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21044 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021045 if (arg->v_type != VAR_LIST
21046 || l == NULL
21047 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021048 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021049 return FAIL;
21050
21051 if (fnump != NULL)
21052 {
21053 n = list_find_nr(l, i++, NULL); /* fnum */
21054 if (n < 0)
21055 return FAIL;
21056 if (n == 0)
21057 n = curbuf->b_fnum; /* current buffer */
21058 *fnump = n;
21059 }
21060
21061 n = list_find_nr(l, i++, NULL); /* lnum */
21062 if (n < 0)
21063 return FAIL;
21064 posp->lnum = n;
21065
21066 n = list_find_nr(l, i++, NULL); /* col */
21067 if (n < 0)
21068 return FAIL;
21069 posp->col = n;
21070
21071#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021072 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021073 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021074 posp->coladd = 0;
21075 else
21076 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021077#endif
21078
Bram Moolenaar493c1782014-05-28 14:34:46 +020021079 if (curswantp != NULL)
21080 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21081
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021082 return OK;
21083}
21084
21085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 * Get the length of an environment variable name.
21087 * Advance "arg" to the first character after the name.
21088 * Return 0 for error.
21089 */
21090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021091get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092{
21093 char_u *p;
21094 int len;
21095
21096 for (p = *arg; vim_isIDc(*p); ++p)
21097 ;
21098 if (p == *arg) /* no name found */
21099 return 0;
21100
21101 len = (int)(p - *arg);
21102 *arg = p;
21103 return len;
21104}
21105
21106/*
21107 * Get the length of the name of a function or internal variable.
21108 * "arg" is advanced to the first non-white character after the name.
21109 * Return 0 if something is wrong.
21110 */
21111 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021112get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113{
21114 char_u *p;
21115 int len;
21116
21117 /* Find the end of the name. */
21118 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021119 {
21120 if (*p == ':')
21121 {
21122 /* "s:" is start of "s:var", but "n:" is not and can be used in
21123 * slice "[n:]". Also "xx:" is not a namespace. */
21124 len = (int)(p - *arg);
21125 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21126 || len > 1)
21127 break;
21128 }
21129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 if (p == *arg) /* no name found */
21131 return 0;
21132
21133 len = (int)(p - *arg);
21134 *arg = skipwhite(p);
21135
21136 return len;
21137}
21138
21139/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021140 * Get the length of the name of a variable or function.
21141 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021143 * Return -1 if curly braces expansion failed.
21144 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 * If the name contains 'magic' {}'s, expand them and return the
21146 * expanded name in an allocated string via 'alias' - caller must free.
21147 */
21148 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021149get_name_len(
21150 char_u **arg,
21151 char_u **alias,
21152 int evaluate,
21153 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154{
21155 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 char_u *p;
21157 char_u *expr_start;
21158 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159
21160 *alias = NULL; /* default to no alias */
21161
21162 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21163 && (*arg)[2] == (int)KE_SNR)
21164 {
21165 /* hard coded <SNR>, already translated */
21166 *arg += 3;
21167 return get_id_len(arg) + 3;
21168 }
21169 len = eval_fname_script(*arg);
21170 if (len > 0)
21171 {
21172 /* literal "<SID>", "s:" or "<SNR>" */
21173 *arg += len;
21174 }
21175
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021177 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021179 p = find_name_end(*arg, &expr_start, &expr_end,
21180 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 if (expr_start != NULL)
21182 {
21183 char_u *temp_string;
21184
21185 if (!evaluate)
21186 {
21187 len += (int)(p - *arg);
21188 *arg = skipwhite(p);
21189 return len;
21190 }
21191
21192 /*
21193 * Include any <SID> etc in the expanded string:
21194 * Thus the -len here.
21195 */
21196 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21197 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021198 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 *alias = temp_string;
21200 *arg = skipwhite(p);
21201 return (int)STRLEN(temp_string);
21202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203
21204 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021205 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 EMSG2(_(e_invexpr2), *arg);
21207
21208 return len;
21209}
21210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021211/*
21212 * Find the end of a variable or function name, taking care of magic braces.
21213 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21214 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021215 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021216 * Return a pointer to just after the name. Equal to "arg" if there is no
21217 * valid name.
21218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021220find_name_end(
21221 char_u *arg,
21222 char_u **expr_start,
21223 char_u **expr_end,
21224 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021226 int mb_nest = 0;
21227 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021229 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021231 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021233 *expr_start = NULL;
21234 *expr_end = NULL;
21235 }
21236
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021237 /* Quick check for valid starting character. */
21238 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21239 return arg;
21240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021241 for (p = arg; *p != NUL
21242 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021243 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021244 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021245 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021246 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021247 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021248 if (*p == '\'')
21249 {
21250 /* skip over 'string' to avoid counting [ and ] inside it. */
21251 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21252 ;
21253 if (*p == NUL)
21254 break;
21255 }
21256 else if (*p == '"')
21257 {
21258 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21259 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21260 if (*p == '\\' && p[1] != NUL)
21261 ++p;
21262 if (*p == NUL)
21263 break;
21264 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021265 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21266 {
21267 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021268 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021269 len = (int)(p - arg);
21270 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021271 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021272 break;
21273 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021275 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021277 if (*p == '[')
21278 ++br_nest;
21279 else if (*p == ']')
21280 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021285 if (*p == '{')
21286 {
21287 mb_nest++;
21288 if (expr_start != NULL && *expr_start == NULL)
21289 *expr_start = p;
21290 }
21291 else if (*p == '}')
21292 {
21293 mb_nest--;
21294 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21295 *expr_end = p;
21296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 }
21299
21300 return p;
21301}
21302
21303/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021304 * Expands out the 'magic' {}'s in a variable/function name.
21305 * Note that this can call itself recursively, to deal with
21306 * constructs like foo{bar}{baz}{bam}
21307 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21308 * "in_start" ^
21309 * "expr_start" ^
21310 * "expr_end" ^
21311 * "in_end" ^
21312 *
21313 * Returns a new allocated string, which the caller must free.
21314 * Returns NULL for failure.
21315 */
21316 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021317make_expanded_name(
21318 char_u *in_start,
21319 char_u *expr_start,
21320 char_u *expr_end,
21321 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021322{
21323 char_u c1;
21324 char_u *retval = NULL;
21325 char_u *temp_result;
21326 char_u *nextcmd = NULL;
21327
21328 if (expr_end == NULL || in_end == NULL)
21329 return NULL;
21330 *expr_start = NUL;
21331 *expr_end = NUL;
21332 c1 = *in_end;
21333 *in_end = NUL;
21334
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021335 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021336 if (temp_result != NULL && nextcmd == NULL)
21337 {
21338 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21339 + (in_end - expr_end) + 1));
21340 if (retval != NULL)
21341 {
21342 STRCPY(retval, in_start);
21343 STRCAT(retval, temp_result);
21344 STRCAT(retval, expr_end + 1);
21345 }
21346 }
21347 vim_free(temp_result);
21348
21349 *in_end = c1; /* put char back for error messages */
21350 *expr_start = '{';
21351 *expr_end = '}';
21352
21353 if (retval != NULL)
21354 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021355 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021356 if (expr_start != NULL)
21357 {
21358 /* Further expansion! */
21359 temp_result = make_expanded_name(retval, expr_start,
21360 expr_end, temp_result);
21361 vim_free(retval);
21362 retval = temp_result;
21363 }
21364 }
21365
21366 return retval;
21367}
21368
21369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021371 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 */
21373 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021374eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021376 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21377}
21378
21379/*
21380 * Return TRUE if character "c" can be used as the first character in a
21381 * variable or function name (excluding '{' and '}').
21382 */
21383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021384eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021385{
21386 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387}
21388
21389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 * Set number v: variable to "val".
21391 */
21392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021393set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021395 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396}
21397
21398/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021399 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 */
21401 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021402get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021404 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405}
21406
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021407/*
21408 * Get string v: variable value. Uses a static buffer, can only be used once.
21409 */
21410 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021411get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021412{
21413 return get_tv_string(&vimvars[idx].vv_tv);
21414}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021415
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021417 * Get List v: variable value. Caller must take care of reference count when
21418 * needed.
21419 */
21420 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021421get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021422{
21423 return vimvars[idx].vv_list;
21424}
21425
21426/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021427 * Set v:char to character "c".
21428 */
21429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021430set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021431{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021432 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021433
21434#ifdef FEAT_MBYTE
21435 if (has_mbyte)
21436 buf[(*mb_char2bytes)(c, buf)] = NUL;
21437 else
21438#endif
21439 {
21440 buf[0] = c;
21441 buf[1] = NUL;
21442 }
21443 set_vim_var_string(VV_CHAR, buf, -1);
21444}
21445
21446/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021447 * Set v:count to "count" and v:count1 to "count1".
21448 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 */
21450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021451set_vcount(
21452 long count,
21453 long count1,
21454 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021456 if (set_prevcount)
21457 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021458 vimvars[VV_COUNT].vv_nr = count;
21459 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460}
21461
21462/*
21463 * Set string v: variable to a copy of "val".
21464 */
21465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021466set_vim_var_string(
21467 int idx,
21468 char_u *val,
21469 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470{
Bram Moolenaara542c682016-01-31 16:28:04 +010021471 clear_tv(&vimvars[idx].vv_di.di_tv);
21472 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021474 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021476 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021478 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479}
21480
21481/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021482 * Set List v: variable to "val".
21483 */
21484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021485set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021486{
Bram Moolenaara542c682016-01-31 16:28:04 +010021487 clear_tv(&vimvars[idx].vv_di.di_tv);
21488 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021489 vimvars[idx].vv_list = val;
21490 if (val != NULL)
21491 ++val->lv_refcount;
21492}
21493
21494/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021495 * Set Dictionary v: variable to "val".
21496 */
21497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021498set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021499{
21500 int todo;
21501 hashitem_T *hi;
21502
Bram Moolenaara542c682016-01-31 16:28:04 +010021503 clear_tv(&vimvars[idx].vv_di.di_tv);
21504 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021505 vimvars[idx].vv_dict = val;
21506 if (val != NULL)
21507 {
21508 ++val->dv_refcount;
21509
21510 /* Set readonly */
21511 todo = (int)val->dv_hashtab.ht_used;
21512 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21513 {
21514 if (HASHITEM_EMPTY(hi))
21515 continue;
21516 --todo;
21517 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21518 }
21519 }
21520}
21521
21522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 * Set v:register if needed.
21524 */
21525 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021526set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527{
21528 char_u regname;
21529
21530 if (c == 0 || c == ' ')
21531 regname = '"';
21532 else
21533 regname = c;
21534 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021535 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 set_vim_var_string(VV_REG, &regname, 1);
21537}
21538
21539/*
21540 * Get or set v:exception. If "oldval" == NULL, return the current value.
21541 * Otherwise, restore the value to "oldval" and return NULL.
21542 * Must always be called in pairs to save and restore v:exception! Does not
21543 * take care of memory allocations.
21544 */
21545 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021546v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547{
21548 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021549 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550
Bram Moolenaare9a41262005-01-15 22:18:47 +000021551 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552 return NULL;
21553}
21554
21555/*
21556 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21557 * Otherwise, restore the value to "oldval" and return NULL.
21558 * Must always be called in pairs to save and restore v:throwpoint! Does not
21559 * take care of memory allocations.
21560 */
21561 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021562v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563{
21564 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021565 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566
Bram Moolenaare9a41262005-01-15 22:18:47 +000021567 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 return NULL;
21569}
21570
21571#if defined(FEAT_AUTOCMD) || defined(PROTO)
21572/*
21573 * Set v:cmdarg.
21574 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21575 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21576 * Must always be called in pairs!
21577 */
21578 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021579set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580{
21581 char_u *oldval;
21582 char_u *newval;
21583 unsigned len;
21584
Bram Moolenaare9a41262005-01-15 22:18:47 +000021585 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021586 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021588 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021589 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021590 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 }
21592
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021593 if (eap->force_bin == FORCE_BIN)
21594 len = 6;
21595 else if (eap->force_bin == FORCE_NOBIN)
21596 len = 8;
21597 else
21598 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021599
21600 if (eap->read_edit)
21601 len += 7;
21602
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021603 if (eap->force_ff != 0)
21604 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21605# ifdef FEAT_MBYTE
21606 if (eap->force_enc != 0)
21607 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021608 if (eap->bad_char != 0)
21609 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021610# endif
21611
21612 newval = alloc(len + 1);
21613 if (newval == NULL)
21614 return NULL;
21615
21616 if (eap->force_bin == FORCE_BIN)
21617 sprintf((char *)newval, " ++bin");
21618 else if (eap->force_bin == FORCE_NOBIN)
21619 sprintf((char *)newval, " ++nobin");
21620 else
21621 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021622
21623 if (eap->read_edit)
21624 STRCAT(newval, " ++edit");
21625
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021626 if (eap->force_ff != 0)
21627 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21628 eap->cmd + eap->force_ff);
21629# ifdef FEAT_MBYTE
21630 if (eap->force_enc != 0)
21631 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21632 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021633 if (eap->bad_char == BAD_KEEP)
21634 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21635 else if (eap->bad_char == BAD_DROP)
21636 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21637 else if (eap->bad_char != 0)
21638 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021639# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021640 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021641 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642}
21643#endif
21644
21645/*
21646 * Get the value of internal variable "name".
21647 * Return OK or FAIL.
21648 */
21649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021650get_var_tv(
21651 char_u *name,
21652 int len, /* length of "name" */
21653 typval_T *rettv, /* NULL when only checking existence */
21654 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21655 int verbose, /* may give error message */
21656 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657{
21658 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 typval_T *tv = NULL;
21660 typval_T atv;
21661 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663
21664 /* truncate the name, so that we can use strcmp() */
21665 cc = name[len];
21666 name[len] = NUL;
21667
21668 /*
21669 * Check for "b:changedtick".
21670 */
21671 if (STRCMP(name, "b:changedtick") == 0)
21672 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021673 atv.v_type = VAR_NUMBER;
21674 atv.vval.v_number = curbuf->b_changedtick;
21675 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 }
21677
21678 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 * Check for user-defined variables.
21680 */
21681 else
21682 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021683 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021685 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021686 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021687 if (dip != NULL)
21688 *dip = v;
21689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 }
21691
Bram Moolenaare9a41262005-01-15 22:18:47 +000021692 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021694 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021695 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 ret = FAIL;
21697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021698 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021699 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700
21701 name[len] = cc;
21702
21703 return ret;
21704}
21705
21706/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021707 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21708 * Also handle function call with Funcref variable: func(expr)
21709 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21710 */
21711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021712handle_subscript(
21713 char_u **arg,
21714 typval_T *rettv,
21715 int evaluate, /* do more than finding the end */
21716 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021717{
21718 int ret = OK;
21719 dict_T *selfdict = NULL;
21720 char_u *s;
21721 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021722 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021723
21724 while (ret == OK
21725 && (**arg == '['
21726 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021727 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21728 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021729 && !vim_iswhite(*(*arg - 1)))
21730 {
21731 if (**arg == '(')
21732 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021733 partial_T *pt = NULL;
21734
Bram Moolenaard9fba312005-06-26 22:34:35 +000021735 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021736 if (evaluate)
21737 {
21738 functv = *rettv;
21739 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021740
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021741 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021742 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021743 {
21744 pt = functv.vval.v_partial;
21745 s = pt->pt_name;
21746 }
21747 else
21748 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021749 }
21750 else
21751 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021752 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021753 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021754 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021755
21756 /* Clear the funcref afterwards, so that deleting it while
21757 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021758 if (evaluate)
21759 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021760
21761 /* Stop the expression evaluation when immediately aborting on
21762 * error, or when an interrupt occurred or an exception was thrown
21763 * but not caught. */
21764 if (aborting())
21765 {
21766 if (ret == OK)
21767 clear_tv(rettv);
21768 ret = FAIL;
21769 }
21770 dict_unref(selfdict);
21771 selfdict = NULL;
21772 }
21773 else /* **arg == '[' || **arg == '.' */
21774 {
21775 dict_unref(selfdict);
21776 if (rettv->v_type == VAR_DICT)
21777 {
21778 selfdict = rettv->vval.v_dict;
21779 if (selfdict != NULL)
21780 ++selfdict->dv_refcount;
21781 }
21782 else
21783 selfdict = NULL;
21784 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21785 {
21786 clear_tv(rettv);
21787 ret = FAIL;
21788 }
21789 }
21790 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021791
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021792 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21793 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021794 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021795 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21796 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021797 char_u *tofree = NULL;
21798 ufunc_T *fp;
21799 char_u fname_buf[FLEN_FIXED + 1];
21800 int error;
21801
21802 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021803 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021804 fp = find_func(fname);
21805 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021806
21807 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021808 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021809 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021810 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21811
21812 if (pt != NULL)
21813 {
21814 pt->pt_refcount = 1;
21815 pt->pt_dict = selfdict;
21816 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021817 if (rettv->v_type == VAR_FUNC)
21818 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021819 /* Just a function: Take over the function name and use
21820 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021821 pt->pt_name = rettv->vval.v_string;
21822 }
21823 else
21824 {
21825 partial_T *ret_pt = rettv->vval.v_partial;
21826 int i;
21827
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021828 /* Partial: copy the function name, use selfdict and copy
21829 * args. Can't take over name or args, the partial might
21830 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021831 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021832 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021833 if (ret_pt->pt_argc > 0)
21834 {
21835 pt->pt_argv = (typval_T *)alloc(
21836 sizeof(typval_T) * ret_pt->pt_argc);
21837 if (pt->pt_argv == NULL)
21838 /* out of memory: drop the arguments */
21839 pt->pt_argc = 0;
21840 else
21841 {
21842 pt->pt_argc = ret_pt->pt_argc;
21843 for (i = 0; i < pt->pt_argc; i++)
21844 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21845 }
21846 }
21847 partial_unref(ret_pt);
21848 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021849 rettv->v_type = VAR_PARTIAL;
21850 rettv->vval.v_partial = pt;
21851 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021852 }
21853 }
21854
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021855 dict_unref(selfdict);
21856 return ret;
21857}
21858
21859/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021860 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021861 * value).
21862 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021863 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021864alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021865{
Bram Moolenaar33570922005-01-25 22:26:29 +000021866 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021867}
21868
21869/*
21870 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 * The string "s" must have been allocated, it is consumed.
21872 * Return NULL for out of memory, the variable otherwise.
21873 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021874 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021875alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876{
Bram Moolenaar33570922005-01-25 22:26:29 +000021877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021879 rettv = alloc_tv();
21880 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021882 rettv->v_type = VAR_STRING;
21883 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 }
21885 else
21886 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021887 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888}
21889
21890/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021891 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021894free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895{
21896 if (varp != NULL)
21897 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021898 switch (varp->v_type)
21899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021900 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021901 func_unref(varp->vval.v_string);
21902 /*FALLTHROUGH*/
21903 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021904 vim_free(varp->vval.v_string);
21905 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021906 case VAR_PARTIAL:
21907 partial_unref(varp->vval.v_partial);
21908 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021909 case VAR_LIST:
21910 list_unref(varp->vval.v_list);
21911 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021912 case VAR_DICT:
21913 dict_unref(varp->vval.v_dict);
21914 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021915 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021916#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021917 job_unref(varp->vval.v_job);
21918 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021919#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021920 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021921#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021922 channel_unref(varp->vval.v_channel);
21923 break;
21924#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021925 case VAR_NUMBER:
21926 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021927 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021928 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021929 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 vim_free(varp);
21932 }
21933}
21934
21935/*
21936 * Free the memory for a variable value and set the value to NULL or 0.
21937 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021939clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940{
21941 if (varp != NULL)
21942 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021943 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021945 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021946 func_unref(varp->vval.v_string);
21947 /*FALLTHROUGH*/
21948 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021949 vim_free(varp->vval.v_string);
21950 varp->vval.v_string = NULL;
21951 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021952 case VAR_PARTIAL:
21953 partial_unref(varp->vval.v_partial);
21954 varp->vval.v_partial = NULL;
21955 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021956 case VAR_LIST:
21957 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021958 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021959 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021960 case VAR_DICT:
21961 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021962 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021963 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021964 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021965 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021966 varp->vval.v_number = 0;
21967 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021968 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021969#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021970 varp->vval.v_float = 0.0;
21971 break;
21972#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021973 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021974#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021975 job_unref(varp->vval.v_job);
21976 varp->vval.v_job = NULL;
21977#endif
21978 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021979 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021980#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021981 channel_unref(varp->vval.v_channel);
21982 varp->vval.v_channel = NULL;
21983#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021984 case VAR_UNKNOWN:
21985 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021987 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 }
21989}
21990
21991/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021992 * Set the value of a variable to NULL without freeing items.
21993 */
21994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021995init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021996{
21997 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021998 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021999}
22000
22001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 * Get the number value of a variable.
22003 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022004 * For incompatible types, return 0.
22005 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22006 * caller of incompatible types: it sets *denote to TRUE if "denote"
22007 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022009 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022010get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022012 int error = FALSE;
22013
22014 return get_tv_number_chk(varp, &error); /* return 0L on error */
22015}
22016
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022017 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022018get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022019{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022020 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022022 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022024 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022025 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022026 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022027#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022028 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022029 break;
22030#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022031 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022032 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022033 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022034 break;
22035 case VAR_STRING:
22036 if (varp->vval.v_string != NULL)
22037 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022038 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022039 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022040 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022041 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022042 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022043 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022044 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022045 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022046 case VAR_SPECIAL:
22047 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22048 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022049 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022050#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022051 EMSG(_("E910: Using a Job as a Number"));
22052 break;
22053#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022054 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022055#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022056 EMSG(_("E913: Using a Channel as a Number"));
22057 break;
22058#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022059 case VAR_UNKNOWN:
22060 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022061 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022063 if (denote == NULL) /* useful for values that must be unsigned */
22064 n = -1;
22065 else
22066 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022067 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068}
22069
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022070#ifdef FEAT_FLOAT
22071 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022072get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022073{
22074 switch (varp->v_type)
22075 {
22076 case VAR_NUMBER:
22077 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022078 case VAR_FLOAT:
22079 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022080 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022081 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022082 EMSG(_("E891: Using a Funcref as a Float"));
22083 break;
22084 case VAR_STRING:
22085 EMSG(_("E892: Using a String as a Float"));
22086 break;
22087 case VAR_LIST:
22088 EMSG(_("E893: Using a List as a Float"));
22089 break;
22090 case VAR_DICT:
22091 EMSG(_("E894: Using a Dictionary as a Float"));
22092 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022093 case VAR_SPECIAL:
22094 EMSG(_("E907: Using a special value as a Float"));
22095 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022096 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022097# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022098 EMSG(_("E911: Using a Job as a Float"));
22099 break;
22100# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022101 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022102# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022103 EMSG(_("E914: Using a Channel as a Float"));
22104 break;
22105# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022106 case VAR_UNKNOWN:
22107 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022108 break;
22109 }
22110 return 0;
22111}
22112#endif
22113
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022115 * Get the lnum from the first argument.
22116 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022117 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 */
22119 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022120get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121{
Bram Moolenaar33570922005-01-25 22:26:29 +000022122 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 linenr_T lnum;
22124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022125 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126 if (lnum == 0) /* no valid number, try using line() */
22127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022128 rettv.v_type = VAR_NUMBER;
22129 f_line(argvars, &rettv);
22130 lnum = rettv.vval.v_number;
22131 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 }
22133 return lnum;
22134}
22135
22136/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022137 * Get the lnum from the first argument.
22138 * Also accepts "$", then "buf" is used.
22139 * Returns 0 on error.
22140 */
22141 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022142get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022143{
22144 if (argvars[0].v_type == VAR_STRING
22145 && argvars[0].vval.v_string != NULL
22146 && argvars[0].vval.v_string[0] == '$'
22147 && buf != NULL)
22148 return buf->b_ml.ml_line_count;
22149 return get_tv_number_chk(&argvars[0], NULL);
22150}
22151
22152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 * Get the string value of a variable.
22154 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022155 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22156 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 * If the String variable has never been set, return an empty string.
22158 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022159 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22160 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022162 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022163get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164{
22165 static char_u mybuf[NUMBUFLEN];
22166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022167 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168}
22169
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022170 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022171get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022173 char_u *res = get_tv_string_buf_chk(varp, buf);
22174
22175 return res != NULL ? res : (char_u *)"";
22176}
22177
Bram Moolenaar7d647822014-04-05 21:28:56 +020022178/*
22179 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22180 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022181 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022182get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022183{
22184 static char_u mybuf[NUMBUFLEN];
22185
22186 return get_tv_string_buf_chk(varp, mybuf);
22187}
22188
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022189 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022190get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022191{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022192 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022194 case VAR_NUMBER:
22195 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22196 return buf;
22197 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022198 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022199 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022200 break;
22201 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022202 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022203 break;
22204 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022205 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022206 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022207 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022208#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022209 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022210 break;
22211#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022212 case VAR_STRING:
22213 if (varp->vval.v_string != NULL)
22214 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022215 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022216 case VAR_SPECIAL:
22217 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22218 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022219 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022220#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022221 {
22222 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022223 char *status;
22224
22225 if (job == NULL)
22226 return (char_u *)"no process";
22227 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022228 : job->jv_status == JOB_ENDED ? "dead"
22229 : "run";
22230# ifdef UNIX
22231 vim_snprintf((char *)buf, NUMBUFLEN,
22232 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022233# elif defined(WIN32)
22234 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022235 "process %ld %s",
22236 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022237 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022238# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022239 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022240 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22241# endif
22242 return buf;
22243 }
22244#endif
22245 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022246 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022247#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022248 {
22249 channel_T *channel = varp->vval.v_channel;
22250 char *status = channel_status(channel);
22251
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022252 if (channel == NULL)
22253 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22254 else
22255 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022256 "channel %d %s", channel->ch_id, status);
22257 return buf;
22258 }
22259#endif
22260 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022261 case VAR_UNKNOWN:
22262 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022263 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022265 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266}
22267
22268/*
22269 * Find variable "name" in the list of variables.
22270 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022271 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022272 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022273 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022275 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022276find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022279 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280
Bram Moolenaara7043832005-01-21 11:56:39 +000022281 ht = find_var_ht(name, &varname);
22282 if (htp != NULL)
22283 *htp = ht;
22284 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022286 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287}
22288
22289/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022290 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022291 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022293 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022294find_var_in_ht(
22295 hashtab_T *ht,
22296 int htname,
22297 char_u *varname,
22298 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022299{
Bram Moolenaar33570922005-01-25 22:26:29 +000022300 hashitem_T *hi;
22301
22302 if (*varname == NUL)
22303 {
22304 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022305 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022306 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022307 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 case 'g': return &globvars_var;
22309 case 'v': return &vimvars_var;
22310 case 'b': return &curbuf->b_bufvar;
22311 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022312#ifdef FEAT_WINDOWS
22313 case 't': return &curtab->tp_winvar;
22314#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022315 case 'l': return current_funccal == NULL
22316 ? NULL : &current_funccal->l_vars_var;
22317 case 'a': return current_funccal == NULL
22318 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022319 }
22320 return NULL;
22321 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022322
22323 hi = hash_find(ht, varname);
22324 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022325 {
22326 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022327 * worked find the variable again. Don't auto-load a script if it was
22328 * loaded already, otherwise it would be loaded every time when
22329 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022330 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022331 {
22332 /* Note: script_autoload() may make "hi" invalid. It must either
22333 * be obtained again or not used. */
22334 if (!script_autoload(varname, FALSE) || aborting())
22335 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022336 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022337 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022338 if (HASHITEM_EMPTY(hi))
22339 return NULL;
22340 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022341 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022342}
22343
22344/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022345 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022346 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022347 * Set "varname" to the start of name without ':'.
22348 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022349 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022350find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022352 hashitem_T *hi;
22353
Bram Moolenaar73627d02015-08-11 15:46:09 +020022354 if (name[0] == NUL)
22355 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 if (name[1] != ':')
22357 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022358 /* The name must not start with a colon or #. */
22359 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360 return NULL;
22361 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022362
22363 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022364 hi = hash_find(&compat_hashtab, name);
22365 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022366 return &compat_hashtab;
22367
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022369 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022370 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371 }
22372 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022373 if (*name == 'g') /* global variable */
22374 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022375 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22376 */
22377 if (vim_strchr(name + 2, ':') != NULL
22378 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022379 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022381 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022383 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022384#ifdef FEAT_WINDOWS
22385 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022386 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022387#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022388 if (*name == 'v') /* v: variable */
22389 return &vimvarht;
22390 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022391 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022392 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022393 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 if (*name == 's' /* script variable */
22395 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22396 return &SCRIPT_VARS(current_SID);
22397 return NULL;
22398}
22399
22400/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022401 * Get function call environment based on bactrace debug level
22402 */
22403 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022404get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022405{
22406 int i;
22407 funccall_T *funccal;
22408 funccall_T *temp_funccal;
22409
22410 funccal = current_funccal;
22411 if (debug_backtrace_level > 0)
22412 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022413 for (i = 0; i < debug_backtrace_level; i++)
22414 {
22415 temp_funccal = funccal->caller;
22416 if (temp_funccal)
22417 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022418 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022419 /* backtrace level overflow. reset to max */
22420 debug_backtrace_level = i;
22421 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022422 }
22423 return funccal;
22424}
22425
22426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022428 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 * Returns NULL when it doesn't exist.
22430 */
22431 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022432get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433{
Bram Moolenaar33570922005-01-25 22:26:29 +000022434 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022436 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 if (v == NULL)
22438 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022439 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440}
22441
22442/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022443 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 * sourcing this script and when executing functions defined in the script.
22445 */
22446 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022447new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448{
Bram Moolenaara7043832005-01-21 11:56:39 +000022449 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 hashtab_T *ht;
22451 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022452
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22454 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022455 /* Re-allocating ga_data means that an ht_array pointing to
22456 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022457 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022458 for (i = 1; i <= ga_scripts.ga_len; ++i)
22459 {
22460 ht = &SCRIPT_VARS(i);
22461 if (ht->ht_mask == HT_INIT_SIZE - 1)
22462 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022463 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022464 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022465 }
22466
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 while (ga_scripts.ga_len < id)
22468 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022469 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022470 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022471 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 }
22474 }
22475}
22476
22477/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022478 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22479 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480 */
22481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022482init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483{
Bram Moolenaar33570922005-01-25 22:26:29 +000022484 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022485 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022486 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022487 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022488 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022489 dict_var->di_tv.vval.v_dict = dict;
22490 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022491 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22493 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494}
22495
22496/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022497 * Unreference a dictionary initialized by init_var_dict().
22498 */
22499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022500unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022501{
22502 /* Now the dict needs to be freed if no one else is using it, go back to
22503 * normal reference counting. */
22504 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22505 dict_unref(dict);
22506}
22507
22508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022510 * Frees all allocated variables and the value they contain.
22511 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 */
22513 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022514vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022515{
22516 vars_clear_ext(ht, TRUE);
22517}
22518
22519/*
22520 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22521 */
22522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022523vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524{
Bram Moolenaara7043832005-01-21 11:56:39 +000022525 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 hashitem_T *hi;
22527 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528
Bram Moolenaar33570922005-01-25 22:26:29 +000022529 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022530 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022531 for (hi = ht->ht_array; todo > 0; ++hi)
22532 {
22533 if (!HASHITEM_EMPTY(hi))
22534 {
22535 --todo;
22536
Bram Moolenaar33570922005-01-25 22:26:29 +000022537 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022538 * ht_array might change then. hash_clear() takes care of it
22539 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022540 v = HI2DI(hi);
22541 if (free_val)
22542 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022543 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022544 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022545 }
22546 }
22547 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022548 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549}
22550
Bram Moolenaara7043832005-01-21 11:56:39 +000022551/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022552 * Delete a variable from hashtab "ht" at item "hi".
22553 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022556delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557{
Bram Moolenaar33570922005-01-25 22:26:29 +000022558 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022559
22560 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022561 clear_tv(&di->di_tv);
22562 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563}
22564
22565/*
22566 * List the value of one internal variable.
22567 */
22568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022569list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022571 char_u *tofree;
22572 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022573 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022574
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022575 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022577 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022578 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579}
22580
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022582list_one_var_a(
22583 char_u *prefix,
22584 char_u *name,
22585 int type,
22586 char_u *string,
22587 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588{
Bram Moolenaar31859182007-08-14 20:41:13 +000022589 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22590 msg_start();
22591 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 if (name != NULL) /* "a:" vars don't have a name stored */
22593 msg_puts(name);
22594 msg_putchar(' ');
22595 msg_advance(22);
22596 if (type == VAR_NUMBER)
22597 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022598 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022599 msg_putchar('*');
22600 else if (type == VAR_LIST)
22601 {
22602 msg_putchar('[');
22603 if (*string == '[')
22604 ++string;
22605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022606 else if (type == VAR_DICT)
22607 {
22608 msg_putchar('{');
22609 if (*string == '{')
22610 ++string;
22611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 else
22613 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022614
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022616
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022617 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022618 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022619 if (*first)
22620 {
22621 msg_clr_eos();
22622 *first = FALSE;
22623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624}
22625
22626/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022627 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 * If the variable already exists, the value is updated.
22629 * Otherwise the variable is created.
22630 */
22631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022632set_var(
22633 char_u *name,
22634 typval_T *tv,
22635 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636{
Bram Moolenaar33570922005-01-25 22:26:29 +000022637 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022639 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022641 ht = find_var_ht(name, &varname);
22642 if (ht == NULL || *varname == NUL)
22643 {
22644 EMSG2(_(e_illvar), name);
22645 return;
22646 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022647 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022648
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022649 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22650 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022651 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022652
Bram Moolenaar33570922005-01-25 22:26:29 +000022653 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022656 if (var_check_ro(v->di_flags, name, FALSE)
22657 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022658 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022659
22660 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022661 * Handle setting internal v: variables separately where needed to
22662 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022663 */
22664 if (ht == &vimvarht)
22665 {
22666 if (v->di_tv.v_type == VAR_STRING)
22667 {
22668 vim_free(v->di_tv.vval.v_string);
22669 if (copy || tv->v_type != VAR_STRING)
22670 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22671 else
22672 {
22673 /* Take over the string to avoid an extra alloc/free. */
22674 v->di_tv.vval.v_string = tv->vval.v_string;
22675 tv->vval.v_string = NULL;
22676 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022677 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022678 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022679 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022680 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022681 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022682 if (STRCMP(varname, "searchforward") == 0)
22683 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022684#ifdef FEAT_SEARCH_EXTRA
22685 else if (STRCMP(varname, "hlsearch") == 0)
22686 {
22687 no_hlsearch = !v->di_tv.vval.v_number;
22688 redraw_all_later(SOME_VALID);
22689 }
22690#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022691 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022692 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022693 else if (v->di_tv.v_type != tv->v_type)
22694 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 }
22696
22697 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 }
22699 else /* add a new variable */
22700 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022701 /* Can't add "v:" variable. */
22702 if (ht == &vimvarht)
22703 {
22704 EMSG2(_(e_illvar), name);
22705 return;
22706 }
22707
Bram Moolenaar92124a32005-06-17 22:03:40 +000022708 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022709 if (!valid_varname(varname))
22710 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022711
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022712 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22713 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022714 if (v == NULL)
22715 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022716 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022719 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 return;
22721 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022722 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022724
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022725 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022726 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022727 else
22728 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022729 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022730 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022731 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733}
22734
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022735/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022736 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022737 * Also give an error message.
22738 */
22739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022740var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022741{
22742 if (flags & DI_FLAGS_RO)
22743 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022744 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022745 return TRUE;
22746 }
22747 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22748 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022749 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 return TRUE;
22751 }
22752 return FALSE;
22753}
22754
22755/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022756 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22757 * Also give an error message.
22758 */
22759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022760var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022761{
22762 if (flags & DI_FLAGS_FIX)
22763 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022764 EMSG2(_("E795: Cannot delete variable %s"),
22765 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022766 return TRUE;
22767 }
22768 return FALSE;
22769}
22770
22771/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022772 * Check if a funcref is assigned to a valid variable name.
22773 * Return TRUE and give an error if not.
22774 */
22775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022776var_check_func_name(
22777 char_u *name, /* points to start of variable name */
22778 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022779{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022780 /* Allow for w: b: s: and t:. */
22781 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022782 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22783 ? name[2] : name[0]))
22784 {
22785 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22786 name);
22787 return TRUE;
22788 }
22789 /* Don't allow hiding a function. When "v" is not NULL we might be
22790 * assigning another function to the same var, the type is checked
22791 * below. */
22792 if (new_var && function_exists(name))
22793 {
22794 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22795 name);
22796 return TRUE;
22797 }
22798 return FALSE;
22799}
22800
22801/*
22802 * Check if a variable name is valid.
22803 * Return FALSE and give an error if not.
22804 */
22805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022806valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022807{
22808 char_u *p;
22809
22810 for (p = varname; *p != NUL; ++p)
22811 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22812 && *p != AUTOLOAD_CHAR)
22813 {
22814 EMSG2(_(e_illvar), varname);
22815 return FALSE;
22816 }
22817 return TRUE;
22818}
22819
22820/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022821 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022822 * Also give an error message, using "name" or _("name") when use_gettext is
22823 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022824 */
22825 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022826tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022827{
22828 if (lock & VAR_LOCKED)
22829 {
22830 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022831 name == NULL ? (char_u *)_("Unknown")
22832 : use_gettext ? (char_u *)_(name)
22833 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022834 return TRUE;
22835 }
22836 if (lock & VAR_FIXED)
22837 {
22838 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022839 name == NULL ? (char_u *)_("Unknown")
22840 : use_gettext ? (char_u *)_(name)
22841 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022842 return TRUE;
22843 }
22844 return FALSE;
22845}
22846
22847/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022848 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022849 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022850 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022851 * It is OK for "from" and "to" to point to the same item. This is used to
22852 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022853 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022854 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022855copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022857 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022858 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022859 switch (from->v_type)
22860 {
22861 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022862 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022863 to->vval.v_number = from->vval.v_number;
22864 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022865 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022866#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022867 to->vval.v_float = from->vval.v_float;
22868 break;
22869#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022870 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022871#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022872 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022873 if (to->vval.v_job != NULL)
22874 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022875 break;
22876#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022877 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022878#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022879 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022880 if (to->vval.v_channel != NULL)
22881 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022882 break;
22883#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022884 case VAR_STRING:
22885 case VAR_FUNC:
22886 if (from->vval.v_string == NULL)
22887 to->vval.v_string = NULL;
22888 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022889 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022890 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022891 if (from->v_type == VAR_FUNC)
22892 func_ref(to->vval.v_string);
22893 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022894 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022895 case VAR_PARTIAL:
22896 if (from->vval.v_partial == NULL)
22897 to->vval.v_partial = NULL;
22898 else
22899 {
22900 to->vval.v_partial = from->vval.v_partial;
22901 ++to->vval.v_partial->pt_refcount;
22902 }
22903 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022904 case VAR_LIST:
22905 if (from->vval.v_list == NULL)
22906 to->vval.v_list = NULL;
22907 else
22908 {
22909 to->vval.v_list = from->vval.v_list;
22910 ++to->vval.v_list->lv_refcount;
22911 }
22912 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022913 case VAR_DICT:
22914 if (from->vval.v_dict == NULL)
22915 to->vval.v_dict = NULL;
22916 else
22917 {
22918 to->vval.v_dict = from->vval.v_dict;
22919 ++to->vval.v_dict->dv_refcount;
22920 }
22921 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022922 case VAR_UNKNOWN:
22923 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022924 break;
22925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926}
22927
22928/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022929 * Make a copy of an item.
22930 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022931 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22932 * reference to an already copied list/dict can be used.
22933 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022934 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022935 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022936item_copy(
22937 typval_T *from,
22938 typval_T *to,
22939 int deep,
22940 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022941{
22942 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022943 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022944
Bram Moolenaar33570922005-01-25 22:26:29 +000022945 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022946 {
22947 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022948 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022949 }
22950 ++recurse;
22951
22952 switch (from->v_type)
22953 {
22954 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022955 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022956 case VAR_STRING:
22957 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022958 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022959 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022960 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022961 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022962 copy_tv(from, to);
22963 break;
22964 case VAR_LIST:
22965 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022966 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022967 if (from->vval.v_list == NULL)
22968 to->vval.v_list = NULL;
22969 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22970 {
22971 /* use the copy made earlier */
22972 to->vval.v_list = from->vval.v_list->lv_copylist;
22973 ++to->vval.v_list->lv_refcount;
22974 }
22975 else
22976 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22977 if (to->vval.v_list == NULL)
22978 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022979 break;
22980 case VAR_DICT:
22981 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022982 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022983 if (from->vval.v_dict == NULL)
22984 to->vval.v_dict = NULL;
22985 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22986 {
22987 /* use the copy made earlier */
22988 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22989 ++to->vval.v_dict->dv_refcount;
22990 }
22991 else
22992 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22993 if (to->vval.v_dict == NULL)
22994 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022995 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022996 case VAR_UNKNOWN:
22997 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022998 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022999 }
23000 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023001 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023002}
23003
23004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 * ":echo expr1 ..." print each argument separated with a space, add a
23006 * newline at the end.
23007 * ":echon expr1 ..." print each argument plain.
23008 */
23009 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023010ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011{
23012 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023013 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023014 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015 char_u *p;
23016 int needclr = TRUE;
23017 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023018 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019
23020 if (eap->skip)
23021 ++emsg_skip;
23022 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23023 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023024 /* If eval1() causes an error message the text from the command may
23025 * still need to be cleared. E.g., "echo 22,44". */
23026 need_clr_eos = needclr;
23027
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023029 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 {
23031 /*
23032 * Report the invalid expression unless the expression evaluation
23033 * has been cancelled due to an aborting error, an interrupt, or an
23034 * exception.
23035 */
23036 if (!aborting())
23037 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023038 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039 break;
23040 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023041 need_clr_eos = FALSE;
23042
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 if (!eap->skip)
23044 {
23045 if (atstart)
23046 {
23047 atstart = FALSE;
23048 /* Call msg_start() after eval1(), evaluating the expression
23049 * may cause a message to appear. */
23050 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023051 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023052 /* Mark the saved text as finishing the line, so that what
23053 * follows is displayed on a new line when scrolling back
23054 * at the more prompt. */
23055 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 }
23059 else if (eap->cmdidx == CMD_echo)
23060 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023061 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023062 if (p != NULL)
23063 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023065 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023067 if (*p != TAB && needclr)
23068 {
23069 /* remove any text still there from the command */
23070 msg_clr_eos();
23071 needclr = FALSE;
23072 }
23073 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 }
23075 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023076 {
23077#ifdef FEAT_MBYTE
23078 if (has_mbyte)
23079 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023080 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023081
23082 (void)msg_outtrans_len_attr(p, i, echo_attr);
23083 p += i - 1;
23084 }
23085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023087 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023090 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023092 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 arg = skipwhite(arg);
23094 }
23095 eap->nextcmd = check_nextcmd(arg);
23096
23097 if (eap->skip)
23098 --emsg_skip;
23099 else
23100 {
23101 /* remove text that may still be there from the command */
23102 if (needclr)
23103 msg_clr_eos();
23104 if (eap->cmdidx == CMD_echo)
23105 msg_end();
23106 }
23107}
23108
23109/*
23110 * ":echohl {name}".
23111 */
23112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023113ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114{
23115 int id;
23116
23117 id = syn_name2id(eap->arg);
23118 if (id == 0)
23119 echo_attr = 0;
23120 else
23121 echo_attr = syn_id2attr(id);
23122}
23123
23124/*
23125 * ":execute expr1 ..." execute the result of an expression.
23126 * ":echomsg expr1 ..." Print a message
23127 * ":echoerr expr1 ..." Print an error
23128 * Each gets spaces around each argument and a newline at the end for
23129 * echo commands
23130 */
23131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023132ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133{
23134 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023135 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 int ret = OK;
23137 char_u *p;
23138 garray_T ga;
23139 int len;
23140 int save_did_emsg;
23141
23142 ga_init2(&ga, 1, 80);
23143
23144 if (eap->skip)
23145 ++emsg_skip;
23146 while (*arg != NUL && *arg != '|' && *arg != '\n')
23147 {
23148 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023149 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 {
23151 /*
23152 * Report the invalid expression unless the expression evaluation
23153 * has been cancelled due to an aborting error, an interrupt, or an
23154 * exception.
23155 */
23156 if (!aborting())
23157 EMSG2(_(e_invexpr2), p);
23158 ret = FAIL;
23159 break;
23160 }
23161
23162 if (!eap->skip)
23163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023164 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 len = (int)STRLEN(p);
23166 if (ga_grow(&ga, len + 2) == FAIL)
23167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023168 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 ret = FAIL;
23170 break;
23171 }
23172 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 ga.ga_len += len;
23176 }
23177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023178 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 arg = skipwhite(arg);
23180 }
23181
23182 if (ret != FAIL && ga.ga_data != NULL)
23183 {
23184 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023187 out_flush();
23188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 else if (eap->cmdidx == CMD_echoerr)
23190 {
23191 /* We don't want to abort following commands, restore did_emsg. */
23192 save_did_emsg = did_emsg;
23193 EMSG((char_u *)ga.ga_data);
23194 if (!force_abort)
23195 did_emsg = save_did_emsg;
23196 }
23197 else if (eap->cmdidx == CMD_execute)
23198 do_cmdline((char_u *)ga.ga_data,
23199 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23200 }
23201
23202 ga_clear(&ga);
23203
23204 if (eap->skip)
23205 --emsg_skip;
23206
23207 eap->nextcmd = check_nextcmd(arg);
23208}
23209
23210/*
23211 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23212 * "arg" points to the "&" or '+' when called, to "option" when returning.
23213 * Returns NULL when no option name found. Otherwise pointer to the char
23214 * after the option name.
23215 */
23216 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023217find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218{
23219 char_u *p = *arg;
23220
23221 ++p;
23222 if (*p == 'g' && p[1] == ':')
23223 {
23224 *opt_flags = OPT_GLOBAL;
23225 p += 2;
23226 }
23227 else if (*p == 'l' && p[1] == ':')
23228 {
23229 *opt_flags = OPT_LOCAL;
23230 p += 2;
23231 }
23232 else
23233 *opt_flags = 0;
23234
23235 if (!ASCII_ISALPHA(*p))
23236 return NULL;
23237 *arg = p;
23238
23239 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23240 p += 4; /* termcap option */
23241 else
23242 while (ASCII_ISALPHA(*p))
23243 ++p;
23244 return p;
23245}
23246
23247/*
23248 * ":function"
23249 */
23250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023251ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252{
23253 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023254 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 int j;
23256 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023258 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259 char_u *name = NULL;
23260 char_u *p;
23261 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023262 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 garray_T newargs;
23264 garray_T newlines;
23265 int varargs = FALSE;
23266 int mustend = FALSE;
23267 int flags = 0;
23268 ufunc_T *fp;
23269 int indent;
23270 int nesting;
23271 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023272 dictitem_T *v;
23273 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023274 static int func_nr = 0; /* number for nameless function */
23275 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023276 hashtab_T *ht;
23277 int todo;
23278 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023279 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280
23281 /*
23282 * ":function" without argument: list functions.
23283 */
23284 if (ends_excmd(*eap->arg))
23285 {
23286 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023287 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023288 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023289 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023290 {
23291 if (!HASHITEM_EMPTY(hi))
23292 {
23293 --todo;
23294 fp = HI2UF(hi);
23295 if (!isdigit(*fp->uf_name))
23296 list_func_head(fp, FALSE);
23297 }
23298 }
23299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300 eap->nextcmd = check_nextcmd(eap->arg);
23301 return;
23302 }
23303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023304 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023305 * ":function /pat": list functions matching pattern.
23306 */
23307 if (*eap->arg == '/')
23308 {
23309 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23310 if (!eap->skip)
23311 {
23312 regmatch_T regmatch;
23313
23314 c = *p;
23315 *p = NUL;
23316 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23317 *p = c;
23318 if (regmatch.regprog != NULL)
23319 {
23320 regmatch.rm_ic = p_ic;
23321
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023322 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023323 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23324 {
23325 if (!HASHITEM_EMPTY(hi))
23326 {
23327 --todo;
23328 fp = HI2UF(hi);
23329 if (!isdigit(*fp->uf_name)
23330 && vim_regexec(&regmatch, fp->uf_name, 0))
23331 list_func_head(fp, FALSE);
23332 }
23333 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023334 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023335 }
23336 }
23337 if (*p == '/')
23338 ++p;
23339 eap->nextcmd = check_nextcmd(p);
23340 return;
23341 }
23342
23343 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023344 * Get the function name. There are these situations:
23345 * func normal function name
23346 * "name" == func, "fudi.fd_dict" == NULL
23347 * dict.func new dictionary entry
23348 * "name" == NULL, "fudi.fd_dict" set,
23349 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23350 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023351 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023352 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23353 * dict.func existing dict entry that's not a Funcref
23354 * "name" == NULL, "fudi.fd_dict" set,
23355 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023356 * s:func script-local function name
23357 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023360 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023361 paren = (vim_strchr(p, '(') != NULL);
23362 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 {
23364 /*
23365 * Return on an invalid expression in braces, unless the expression
23366 * evaluation has been cancelled due to an aborting error, an
23367 * interrupt, or an exception.
23368 */
23369 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023371 if (!eap->skip && fudi.fd_newkey != NULL)
23372 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023373 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 else
23377 eap->skip = TRUE;
23378 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023379
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 /* An error in a function call during evaluation of an expression in magic
23381 * braces should not cause the function not to be defined. */
23382 saved_did_emsg = did_emsg;
23383 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384
23385 /*
23386 * ":function func" with only function name: list function.
23387 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023388 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389 {
23390 if (!ends_excmd(*skipwhite(p)))
23391 {
23392 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023393 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 }
23395 eap->nextcmd = check_nextcmd(p);
23396 if (eap->nextcmd != NULL)
23397 *p = NUL;
23398 if (!eap->skip && !got_int)
23399 {
23400 fp = find_func(name);
23401 if (fp != NULL)
23402 {
23403 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023404 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023406 if (FUNCLINE(fp, j) == NULL)
23407 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 msg_putchar('\n');
23409 msg_outnum((long)(j + 1));
23410 if (j < 9)
23411 msg_putchar(' ');
23412 if (j < 99)
23413 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023414 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415 out_flush(); /* show a line at a time */
23416 ui_breakcheck();
23417 }
23418 if (!got_int)
23419 {
23420 msg_putchar('\n');
23421 msg_puts((char_u *)" endfunction");
23422 }
23423 }
23424 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023425 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023427 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 }
23429
23430 /*
23431 * ":function name(arg1, arg2)" Define function.
23432 */
23433 p = skipwhite(p);
23434 if (*p != '(')
23435 {
23436 if (!eap->skip)
23437 {
23438 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023439 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440 }
23441 /* attempt to continue by skipping some text */
23442 if (vim_strchr(p, '(') != NULL)
23443 p = vim_strchr(p, '(');
23444 }
23445 p = skipwhite(p + 1);
23446
23447 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23448 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23449
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023450 if (!eap->skip)
23451 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023452 /* Check the name of the function. Unless it's a dictionary function
23453 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023454 if (name != NULL)
23455 arg = name;
23456 else
23457 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023458 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023459 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23460 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023461 {
23462 if (*arg == K_SPECIAL)
23463 j = 3;
23464 else
23465 j = 0;
23466 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23467 : eval_isnamec(arg[j])))
23468 ++j;
23469 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023470 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023471 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023472 /* Disallow using the g: dict. */
23473 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23474 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023475 }
23476
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 /*
23478 * Isolate the arguments: "arg1, arg2, ...)"
23479 */
23480 while (*p != ')')
23481 {
23482 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23483 {
23484 varargs = TRUE;
23485 p += 3;
23486 mustend = TRUE;
23487 }
23488 else
23489 {
23490 arg = p;
23491 while (ASCII_ISALNUM(*p) || *p == '_')
23492 ++p;
23493 if (arg == p || isdigit(*arg)
23494 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23495 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23496 {
23497 if (!eap->skip)
23498 EMSG2(_("E125: Illegal argument: %s"), arg);
23499 break;
23500 }
23501 if (ga_grow(&newargs, 1) == FAIL)
23502 goto erret;
23503 c = *p;
23504 *p = NUL;
23505 arg = vim_strsave(arg);
23506 if (arg == NULL)
23507 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023508
23509 /* Check for duplicate argument name. */
23510 for (i = 0; i < newargs.ga_len; ++i)
23511 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23512 {
23513 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023514 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023515 goto erret;
23516 }
23517
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23519 *p = c;
23520 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 if (*p == ',')
23522 ++p;
23523 else
23524 mustend = TRUE;
23525 }
23526 p = skipwhite(p);
23527 if (mustend && *p != ')')
23528 {
23529 if (!eap->skip)
23530 EMSG2(_(e_invarg2), eap->arg);
23531 break;
23532 }
23533 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023534 if (*p != ')')
23535 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 ++p; /* skip the ')' */
23537
Bram Moolenaare9a41262005-01-15 22:18:47 +000023538 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 for (;;)
23540 {
23541 p = skipwhite(p);
23542 if (STRNCMP(p, "range", 5) == 0)
23543 {
23544 flags |= FC_RANGE;
23545 p += 5;
23546 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023547 else if (STRNCMP(p, "dict", 4) == 0)
23548 {
23549 flags |= FC_DICT;
23550 p += 4;
23551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 else if (STRNCMP(p, "abort", 5) == 0)
23553 {
23554 flags |= FC_ABORT;
23555 p += 5;
23556 }
23557 else
23558 break;
23559 }
23560
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023561 /* When there is a line break use what follows for the function body.
23562 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23563 if (*p == '\n')
23564 line_arg = p + 1;
23565 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 EMSG(_(e_trailing));
23567
23568 /*
23569 * Read the body of the function, until ":endfunction" is found.
23570 */
23571 if (KeyTyped)
23572 {
23573 /* Check if the function already exists, don't let the user type the
23574 * whole function before telling him it doesn't work! For a script we
23575 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023576 if (!eap->skip && !eap->forceit)
23577 {
23578 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23579 EMSG(_(e_funcdict));
23580 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023581 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023584 if (!eap->skip && did_emsg)
23585 goto erret;
23586
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 msg_putchar('\n'); /* don't overwrite the function name */
23588 cmdline_row = msg_row;
23589 }
23590
23591 indent = 2;
23592 nesting = 0;
23593 for (;;)
23594 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023595 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023596 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023597 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023598 saved_wait_return = FALSE;
23599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023601 sourcing_lnum_off = sourcing_lnum;
23602
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023603 if (line_arg != NULL)
23604 {
23605 /* Use eap->arg, split up in parts by line breaks. */
23606 theline = line_arg;
23607 p = vim_strchr(theline, '\n');
23608 if (p == NULL)
23609 line_arg += STRLEN(line_arg);
23610 else
23611 {
23612 *p = NUL;
23613 line_arg = p + 1;
23614 }
23615 }
23616 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 theline = getcmdline(':', 0L, indent);
23618 else
23619 theline = eap->getline(':', eap->cookie, indent);
23620 if (KeyTyped)
23621 lines_left = Rows - 1;
23622 if (theline == NULL)
23623 {
23624 EMSG(_("E126: Missing :endfunction"));
23625 goto erret;
23626 }
23627
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023628 /* Detect line continuation: sourcing_lnum increased more than one. */
23629 if (sourcing_lnum > sourcing_lnum_off + 1)
23630 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23631 else
23632 sourcing_lnum_off = 0;
23633
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 if (skip_until != NULL)
23635 {
23636 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23637 * don't check for ":endfunc". */
23638 if (STRCMP(theline, skip_until) == 0)
23639 {
23640 vim_free(skip_until);
23641 skip_until = NULL;
23642 }
23643 }
23644 else
23645 {
23646 /* skip ':' and blanks*/
23647 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23648 ;
23649
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023650 /* Check for "endfunction". */
23651 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023653 if (line_arg == NULL)
23654 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 break;
23656 }
23657
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023658 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 * at "end". */
23660 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23661 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023662 else if (STRNCMP(p, "if", 2) == 0
23663 || STRNCMP(p, "wh", 2) == 0
23664 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 || STRNCMP(p, "try", 3) == 0)
23666 indent += 2;
23667
23668 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023669 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023671 if (*p == '!')
23672 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023674 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023675 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023677 ++nesting;
23678 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 }
23680 }
23681
23682 /* Check for ":append" or ":insert". */
23683 p = skip_range(p, NULL);
23684 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23685 || (p[0] == 'i'
23686 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23687 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23688 skip_until = vim_strsave((char_u *)".");
23689
23690 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23691 arg = skipwhite(skiptowhite(p));
23692 if (arg[0] == '<' && arg[1] =='<'
23693 && ((p[0] == 'p' && p[1] == 'y'
23694 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23695 || (p[0] == 'p' && p[1] == 'e'
23696 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23697 || (p[0] == 't' && p[1] == 'c'
23698 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023699 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23700 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23702 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023703 || (p[0] == 'm' && p[1] == 'z'
23704 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 ))
23706 {
23707 /* ":python <<" continues until a dot, like ":append" */
23708 p = skipwhite(arg + 2);
23709 if (*p == NUL)
23710 skip_until = vim_strsave((char_u *)".");
23711 else
23712 skip_until = vim_strsave(p);
23713 }
23714 }
23715
23716 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023717 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023718 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023719 if (line_arg == NULL)
23720 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023721 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023722 }
23723
23724 /* Copy the line to newly allocated memory. get_one_sourceline()
23725 * allocates 250 bytes per line, this saves 80% on average. The cost
23726 * is an extra alloc/free. */
23727 p = vim_strsave(theline);
23728 if (p != NULL)
23729 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023730 if (line_arg == NULL)
23731 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023732 theline = p;
23733 }
23734
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023735 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23736
23737 /* Add NULL lines for continuation lines, so that the line count is
23738 * equal to the index in the growarray. */
23739 while (sourcing_lnum_off-- > 0)
23740 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023741
23742 /* Check for end of eap->arg. */
23743 if (line_arg != NULL && *line_arg == NUL)
23744 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 }
23746
23747 /* Don't define the function when skipping commands or when an error was
23748 * detected. */
23749 if (eap->skip || did_emsg)
23750 goto erret;
23751
23752 /*
23753 * If there are no errors, add the function
23754 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023755 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023756 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023757 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023758 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023759 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023760 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023761 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023762 goto erret;
23763 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023764
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023765 fp = find_func(name);
23766 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023768 if (!eap->forceit)
23769 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023770 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023771 goto erret;
23772 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023773 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023774 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023775 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023776 name);
23777 goto erret;
23778 }
23779 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023780 ga_clear_strings(&(fp->uf_args));
23781 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023782 vim_free(name);
23783 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 }
23786 else
23787 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023788 char numbuf[20];
23789
23790 fp = NULL;
23791 if (fudi.fd_newkey == NULL && !eap->forceit)
23792 {
23793 EMSG(_(e_funcdict));
23794 goto erret;
23795 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023796 if (fudi.fd_di == NULL)
23797 {
23798 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023799 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023800 goto erret;
23801 }
23802 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023803 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023804 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023805
23806 /* Give the function a sequential number. Can only be used with a
23807 * Funcref! */
23808 vim_free(name);
23809 sprintf(numbuf, "%d", ++func_nr);
23810 name = vim_strsave((char_u *)numbuf);
23811 if (name == NULL)
23812 goto erret;
23813 }
23814
23815 if (fp == NULL)
23816 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023817 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023818 {
23819 int slen, plen;
23820 char_u *scriptname;
23821
23822 /* Check that the autoload name matches the script name. */
23823 j = FAIL;
23824 if (sourcing_name != NULL)
23825 {
23826 scriptname = autoload_name(name);
23827 if (scriptname != NULL)
23828 {
23829 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023830 plen = (int)STRLEN(p);
23831 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023832 if (slen > plen && fnamecmp(p,
23833 sourcing_name + slen - plen) == 0)
23834 j = OK;
23835 vim_free(scriptname);
23836 }
23837 }
23838 if (j == FAIL)
23839 {
23840 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23841 goto erret;
23842 }
23843 }
23844
23845 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 if (fp == NULL)
23847 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023848
23849 if (fudi.fd_dict != NULL)
23850 {
23851 if (fudi.fd_di == NULL)
23852 {
23853 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023854 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023855 if (fudi.fd_di == NULL)
23856 {
23857 vim_free(fp);
23858 goto erret;
23859 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023860 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23861 {
23862 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023863 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023864 goto erret;
23865 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023866 }
23867 else
23868 /* overwrite existing dict entry */
23869 clear_tv(&fudi.fd_di->di_tv);
23870 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023871 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023872 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023873 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023874
23875 /* behave like "dict" was used */
23876 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023877 }
23878
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023880 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023881 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23882 {
23883 vim_free(fp);
23884 goto erret;
23885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023887 fp->uf_args = newargs;
23888 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023889#ifdef FEAT_PROFILE
23890 fp->uf_tml_count = NULL;
23891 fp->uf_tml_total = NULL;
23892 fp->uf_tml_self = NULL;
23893 fp->uf_profiling = FALSE;
23894 if (prof_def_func())
23895 func_do_profile(fp);
23896#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023897 fp->uf_varargs = varargs;
23898 fp->uf_flags = flags;
23899 fp->uf_calls = 0;
23900 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023901 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902
23903erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 ga_clear_strings(&newargs);
23905 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023906ret_free:
23907 vim_free(skip_until);
23908 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023911 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912}
23913
23914/*
23915 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023916 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023918 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023919 * TFN_INT: internal function name OK
23920 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023921 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 * Advances "pp" to just after the function name (if no error).
23923 */
23924 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023925trans_function_name(
23926 char_u **pp,
23927 int skip, /* only find the end, don't evaluate */
23928 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023929 funcdict_T *fdp, /* return: info about dictionary used */
23930 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023932 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 char_u *start;
23934 char_u *end;
23935 int lead;
23936 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023938 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023939
23940 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023941 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023943
23944 /* Check for hard coded <SNR>: already translated function ID (from a user
23945 * command). */
23946 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23947 && (*pp)[2] == (int)KE_SNR)
23948 {
23949 *pp += 3;
23950 len = get_id_len(pp) + 3;
23951 return vim_strnsave(start, len);
23952 }
23953
23954 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23955 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023957 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023959
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023960 /* Note that TFN_ flags use the same values as GLV_ flags. */
23961 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023962 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023963 if (end == start)
23964 {
23965 if (!skip)
23966 EMSG(_("E129: Function name required"));
23967 goto theend;
23968 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023969 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023970 {
23971 /*
23972 * Report an invalid expression in braces, unless the expression
23973 * evaluation has been cancelled due to an aborting error, an
23974 * interrupt, or an exception.
23975 */
23976 if (!aborting())
23977 {
23978 if (end != NULL)
23979 EMSG2(_(e_invarg2), start);
23980 }
23981 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023982 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023983 goto theend;
23984 }
23985
23986 if (lv.ll_tv != NULL)
23987 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023988 if (fdp != NULL)
23989 {
23990 fdp->fd_dict = lv.ll_dict;
23991 fdp->fd_newkey = lv.ll_newkey;
23992 lv.ll_newkey = NULL;
23993 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023995 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23996 {
23997 name = vim_strsave(lv.ll_tv->vval.v_string);
23998 *pp = end;
23999 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024000 else if (lv.ll_tv->v_type == VAR_PARTIAL
24001 && lv.ll_tv->vval.v_partial != NULL)
24002 {
24003 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24004 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024005 if (partial != NULL)
24006 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024008 else
24009 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024010 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24011 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012 EMSG(_(e_funcref));
24013 else
24014 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024015 name = NULL;
24016 }
24017 goto theend;
24018 }
24019
24020 if (lv.ll_name == NULL)
24021 {
24022 /* Error found, but continue after the function name. */
24023 *pp = end;
24024 goto theend;
24025 }
24026
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024027 /* Check if the name is a Funcref. If so, use the value. */
24028 if (lv.ll_exp_name != NULL)
24029 {
24030 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024031 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024032 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024033 if (name == lv.ll_exp_name)
24034 name = NULL;
24035 }
24036 else
24037 {
24038 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024039 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024040 if (name == *pp)
24041 name = NULL;
24042 }
24043 if (name != NULL)
24044 {
24045 name = vim_strsave(name);
24046 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024047 if (STRNCMP(name, "<SNR>", 5) == 0)
24048 {
24049 /* Change "<SNR>" to the byte sequence. */
24050 name[0] = K_SPECIAL;
24051 name[1] = KS_EXTRA;
24052 name[2] = (int)KE_SNR;
24053 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24054 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024055 goto theend;
24056 }
24057
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024058 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024059 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024060 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024061 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24062 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24063 {
24064 /* When there was "s:" already or the name expanded to get a
24065 * leading "s:" then remove it. */
24066 lv.ll_name += 2;
24067 len -= 2;
24068 lead = 2;
24069 }
24070 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024071 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024072 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024073 /* skip over "s:" and "g:" */
24074 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024075 lv.ll_name += 2;
24076 len = (int)(end - lv.ll_name);
24077 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024078
24079 /*
24080 * Copy the function name to allocated memory.
24081 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24082 * Accept <SNR>123_name() outside a script.
24083 */
24084 if (skip)
24085 lead = 0; /* do nothing */
24086 else if (lead > 0)
24087 {
24088 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024089 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24090 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024091 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024092 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024093 if (current_SID <= 0)
24094 {
24095 EMSG(_(e_usingsid));
24096 goto theend;
24097 }
24098 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24099 lead += (int)STRLEN(sid_buf);
24100 }
24101 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024102 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024103 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024104 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024105 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024106 goto theend;
24107 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024108 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024109 {
24110 char_u *cp = vim_strchr(lv.ll_name, ':');
24111
24112 if (cp != NULL && cp < end)
24113 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024114 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024115 goto theend;
24116 }
24117 }
24118
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024119 name = alloc((unsigned)(len + lead + 1));
24120 if (name != NULL)
24121 {
24122 if (lead > 0)
24123 {
24124 name[0] = K_SPECIAL;
24125 name[1] = KS_EXTRA;
24126 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024127 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024128 STRCPY(name + 3, sid_buf);
24129 }
24130 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024131 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024132 }
24133 *pp = end;
24134
24135theend:
24136 clear_lval(&lv);
24137 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138}
24139
24140/*
24141 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24142 * Return 2 if "p" starts with "s:".
24143 * Return 0 otherwise.
24144 */
24145 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024146eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024148 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24149 * the standard library function. */
24150 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24151 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024152 return 5;
24153 if (p[0] == 's' && p[1] == ':')
24154 return 2;
24155 return 0;
24156}
24157
24158/*
24159 * Return TRUE if "p" starts with "<SID>" or "s:".
24160 * Only works if eval_fname_script() returned non-zero for "p"!
24161 */
24162 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024163eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164{
24165 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24166}
24167
24168/*
24169 * List the head of the function: "name(arg1, arg2)".
24170 */
24171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024172list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173{
24174 int j;
24175
24176 msg_start();
24177 if (indent)
24178 MSG_PUTS(" ");
24179 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024180 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 {
24182 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024183 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 }
24185 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024186 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024188 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 {
24190 if (j)
24191 MSG_PUTS(", ");
24192 msg_puts(FUNCARG(fp, j));
24193 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024194 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 {
24196 if (j)
24197 MSG_PUTS(", ");
24198 MSG_PUTS("...");
24199 }
24200 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024201 if (fp->uf_flags & FC_ABORT)
24202 MSG_PUTS(" abort");
24203 if (fp->uf_flags & FC_RANGE)
24204 MSG_PUTS(" range");
24205 if (fp->uf_flags & FC_DICT)
24206 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024207 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024208 if (p_verbose > 0)
24209 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210}
24211
24212/*
24213 * Find a function by name, return pointer to it in ufuncs.
24214 * Return NULL for unknown function.
24215 */
24216 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024217find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024219 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024221 hi = hash_find(&func_hashtab, name);
24222 if (!HASHITEM_EMPTY(hi))
24223 return HI2UF(hi);
24224 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225}
24226
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024227#if defined(EXITFREE) || defined(PROTO)
24228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024229free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024230{
24231 hashitem_T *hi;
24232
24233 /* Need to start all over every time, because func_free() may change the
24234 * hash table. */
24235 while (func_hashtab.ht_used > 0)
24236 for (hi = func_hashtab.ht_array; ; ++hi)
24237 if (!HASHITEM_EMPTY(hi))
24238 {
24239 func_free(HI2UF(hi));
24240 break;
24241 }
24242}
24243#endif
24244
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024246translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024247{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024248 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024249 return find_internal_func(name) >= 0;
24250 return find_func(name) != NULL;
24251}
24252
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024253/*
24254 * Return TRUE if a function "name" exists.
24255 */
24256 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024257function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024258{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024259 char_u *nm = name;
24260 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024261 int n = FALSE;
24262
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024263 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024264 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024265 nm = skipwhite(nm);
24266
24267 /* Only accept "funcname", "funcname ", "funcname (..." and
24268 * "funcname(...", not "funcname!...". */
24269 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024270 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024271 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024272 return n;
24273}
24274
Bram Moolenaara1544c02013-05-30 12:35:52 +020024275 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024276get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024277{
24278 char_u *nm = name;
24279 char_u *p;
24280
Bram Moolenaar65639032016-03-16 21:40:30 +010024281 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024282
24283 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024284 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024285 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024286
Bram Moolenaara1544c02013-05-30 12:35:52 +020024287 vim_free(p);
24288 return NULL;
24289}
24290
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024291/*
24292 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024293 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24294 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024295 */
24296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024297builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024298{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024299 char_u *p;
24300
24301 if (!ASCII_ISLOWER(name[0]))
24302 return FALSE;
24303 p = vim_strchr(name, AUTOLOAD_CHAR);
24304 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024305}
24306
Bram Moolenaar05159a02005-02-26 23:04:13 +000024307#if defined(FEAT_PROFILE) || defined(PROTO)
24308/*
24309 * Start profiling function "fp".
24310 */
24311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024312func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024313{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024314 int len = fp->uf_lines.ga_len;
24315
24316 if (len == 0)
24317 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024318 fp->uf_tm_count = 0;
24319 profile_zero(&fp->uf_tm_self);
24320 profile_zero(&fp->uf_tm_total);
24321 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024322 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024323 if (fp->uf_tml_total == NULL)
24324 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024325 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024326 if (fp->uf_tml_self == NULL)
24327 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024328 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024329 fp->uf_tml_idx = -1;
24330 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24331 || fp->uf_tml_self == NULL)
24332 return; /* out of memory */
24333
24334 fp->uf_profiling = TRUE;
24335}
24336
24337/*
24338 * Dump the profiling results for all functions in file "fd".
24339 */
24340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024341func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024342{
24343 hashitem_T *hi;
24344 int todo;
24345 ufunc_T *fp;
24346 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024347 ufunc_T **sorttab;
24348 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024349
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024350 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024351 if (todo == 0)
24352 return; /* nothing to dump */
24353
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024354 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024355
Bram Moolenaar05159a02005-02-26 23:04:13 +000024356 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24357 {
24358 if (!HASHITEM_EMPTY(hi))
24359 {
24360 --todo;
24361 fp = HI2UF(hi);
24362 if (fp->uf_profiling)
24363 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024364 if (sorttab != NULL)
24365 sorttab[st_len++] = fp;
24366
Bram Moolenaar05159a02005-02-26 23:04:13 +000024367 if (fp->uf_name[0] == K_SPECIAL)
24368 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24369 else
24370 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24371 if (fp->uf_tm_count == 1)
24372 fprintf(fd, "Called 1 time\n");
24373 else
24374 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24375 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24376 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24377 fprintf(fd, "\n");
24378 fprintf(fd, "count total (s) self (s)\n");
24379
24380 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24381 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024382 if (FUNCLINE(fp, i) == NULL)
24383 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024384 prof_func_line(fd, fp->uf_tml_count[i],
24385 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024386 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24387 }
24388 fprintf(fd, "\n");
24389 }
24390 }
24391 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024392
24393 if (sorttab != NULL && st_len > 0)
24394 {
24395 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24396 prof_total_cmp);
24397 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24398 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24399 prof_self_cmp);
24400 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24401 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024402
24403 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024404}
Bram Moolenaar73830342005-02-28 22:48:19 +000024405
24406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024407prof_sort_list(
24408 FILE *fd,
24409 ufunc_T **sorttab,
24410 int st_len,
24411 char *title,
24412 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024413{
24414 int i;
24415 ufunc_T *fp;
24416
24417 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24418 fprintf(fd, "count total (s) self (s) function\n");
24419 for (i = 0; i < 20 && i < st_len; ++i)
24420 {
24421 fp = sorttab[i];
24422 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24423 prefer_self);
24424 if (fp->uf_name[0] == K_SPECIAL)
24425 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24426 else
24427 fprintf(fd, " %s()\n", fp->uf_name);
24428 }
24429 fprintf(fd, "\n");
24430}
24431
24432/*
24433 * Print the count and times for one function or function line.
24434 */
24435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024436prof_func_line(
24437 FILE *fd,
24438 int count,
24439 proftime_T *total,
24440 proftime_T *self,
24441 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024442{
24443 if (count > 0)
24444 {
24445 fprintf(fd, "%5d ", count);
24446 if (prefer_self && profile_equal(total, self))
24447 fprintf(fd, " ");
24448 else
24449 fprintf(fd, "%s ", profile_msg(total));
24450 if (!prefer_self && profile_equal(total, self))
24451 fprintf(fd, " ");
24452 else
24453 fprintf(fd, "%s ", profile_msg(self));
24454 }
24455 else
24456 fprintf(fd, " ");
24457}
24458
24459/*
24460 * Compare function for total time sorting.
24461 */
24462 static int
24463#ifdef __BORLANDC__
24464_RTLENTRYF
24465#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024466prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024467{
24468 ufunc_T *p1, *p2;
24469
24470 p1 = *(ufunc_T **)s1;
24471 p2 = *(ufunc_T **)s2;
24472 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24473}
24474
24475/*
24476 * Compare function for self time sorting.
24477 */
24478 static int
24479#ifdef __BORLANDC__
24480_RTLENTRYF
24481#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024482prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024483{
24484 ufunc_T *p1, *p2;
24485
24486 p1 = *(ufunc_T **)s1;
24487 p2 = *(ufunc_T **)s2;
24488 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24489}
24490
Bram Moolenaar05159a02005-02-26 23:04:13 +000024491#endif
24492
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024493/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024494 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024495 * Return TRUE if a package was loaded.
24496 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024497 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024498script_autoload(
24499 char_u *name,
24500 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024501{
24502 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024503 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024504 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024505 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024506
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024507 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024508 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024509 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024510 return FALSE;
24511
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024512 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024513
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024514 /* Find the name in the list of previously loaded package names. Skip
24515 * "autoload/", it's always the same. */
24516 for (i = 0; i < ga_loaded.ga_len; ++i)
24517 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24518 break;
24519 if (!reload && i < ga_loaded.ga_len)
24520 ret = FALSE; /* was loaded already */
24521 else
24522 {
24523 /* Remember the name if it wasn't loaded already. */
24524 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24525 {
24526 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24527 tofree = NULL;
24528 }
24529
24530 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024531 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024532 ret = TRUE;
24533 }
24534
24535 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024536 return ret;
24537}
24538
24539/*
24540 * Return the autoload script name for a function or variable name.
24541 * Returns NULL when out of memory.
24542 */
24543 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024544autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024545{
24546 char_u *p;
24547 char_u *scriptname;
24548
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024549 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024550 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24551 if (scriptname == NULL)
24552 return FALSE;
24553 STRCPY(scriptname, "autoload/");
24554 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024555 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024556 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024557 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024558 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024559 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024560}
24561
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24563
24564/*
24565 * Function given to ExpandGeneric() to obtain the list of user defined
24566 * function names.
24567 */
24568 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024569get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024571 static long_u done;
24572 static hashitem_T *hi;
24573 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574
24575 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024577 done = 0;
24578 hi = func_hashtab.ht_array;
24579 }
24580 if (done < func_hashtab.ht_used)
24581 {
24582 if (done++ > 0)
24583 ++hi;
24584 while (HASHITEM_EMPTY(hi))
24585 ++hi;
24586 fp = HI2UF(hi);
24587
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024588 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024589 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024590
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024591 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24592 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593
24594 cat_func_name(IObuff, fp);
24595 if (xp->xp_context != EXPAND_USER_FUNC)
24596 {
24597 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024598 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 STRCAT(IObuff, ")");
24600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 return IObuff;
24602 }
24603 return NULL;
24604}
24605
24606#endif /* FEAT_CMDL_COMPL */
24607
24608/*
24609 * Copy the function name of "fp" to buffer "buf".
24610 * "buf" must be able to hold the function name plus three bytes.
24611 * Takes care of script-local function names.
24612 */
24613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024614cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024616 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 {
24618 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024619 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 }
24621 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024622 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623}
24624
24625/*
24626 * ":delfunction {name}"
24627 */
24628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024629ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024631 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 char_u *p;
24633 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024634 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635
24636 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024637 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024638 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024640 {
24641 if (fudi.fd_dict != NULL && !eap->skip)
24642 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 if (!ends_excmd(*skipwhite(p)))
24646 {
24647 vim_free(name);
24648 EMSG(_(e_trailing));
24649 return;
24650 }
24651 eap->nextcmd = check_nextcmd(p);
24652 if (eap->nextcmd != NULL)
24653 *p = NUL;
24654
24655 if (!eap->skip)
24656 fp = find_func(name);
24657 vim_free(name);
24658
24659 if (!eap->skip)
24660 {
24661 if (fp == NULL)
24662 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024663 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 return;
24665 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024666 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 {
24668 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24669 return;
24670 }
24671
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024672 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024674 /* Delete the dict item that refers to the function, it will
24675 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024676 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024678 else
24679 func_free(fp);
24680 }
24681}
24682
24683/*
24684 * Free a function and remove it from the list of functions.
24685 */
24686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024687func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024688{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024689 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024690
24691 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024692 ga_clear_strings(&(fp->uf_args));
24693 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024694#ifdef FEAT_PROFILE
24695 vim_free(fp->uf_tml_count);
24696 vim_free(fp->uf_tml_total);
24697 vim_free(fp->uf_tml_self);
24698#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024699
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024700 /* remove the function from the function hashtable */
24701 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24702 if (HASHITEM_EMPTY(hi))
24703 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024704 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024705 hash_remove(&func_hashtab, hi);
24706
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024707 vim_free(fp);
24708}
24709
24710/*
24711 * Unreference a Function: decrement the reference count and free it when it
24712 * becomes zero. Only for numbered functions.
24713 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024714 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024715func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024716{
24717 ufunc_T *fp;
24718
24719 if (name != NULL && isdigit(*name))
24720 {
24721 fp = find_func(name);
24722 if (fp == NULL)
24723 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024724 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024725 {
24726 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024727 * when "uf_calls" becomes zero. */
24728 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024729 func_free(fp);
24730 }
24731 }
24732}
24733
24734/*
24735 * Count a reference to a Function.
24736 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024738func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024739{
24740 ufunc_T *fp;
24741
24742 if (name != NULL && isdigit(*name))
24743 {
24744 fp = find_func(name);
24745 if (fp == NULL)
24746 EMSG2(_(e_intern2), "func_ref()");
24747 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024748 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024749 }
24750}
24751
24752/*
24753 * Call a user function.
24754 */
24755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024756call_user_func(
24757 ufunc_T *fp, /* pointer to function */
24758 int argcount, /* nr of args */
24759 typval_T *argvars, /* arguments */
24760 typval_T *rettv, /* return value */
24761 linenr_T firstline, /* first line of range */
24762 linenr_T lastline, /* last line of range */
24763 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764{
Bram Moolenaar33570922005-01-25 22:26:29 +000024765 char_u *save_sourcing_name;
24766 linenr_T save_sourcing_lnum;
24767 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024768 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024769 int save_did_emsg;
24770 static int depth = 0;
24771 dictitem_T *v;
24772 int fixvar_idx = 0; /* index in fixvar[] */
24773 int i;
24774 int ai;
24775 char_u numbuf[NUMBUFLEN];
24776 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024777 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024778#ifdef FEAT_PROFILE
24779 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024780 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782
24783 /* If depth of calling is getting too high, don't execute the function */
24784 if (depth >= p_mfd)
24785 {
24786 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024787 rettv->v_type = VAR_NUMBER;
24788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 return;
24790 }
24791 ++depth;
24792
24793 line_breakcheck(); /* check for CTRL-C hit */
24794
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024795 fc = (funccall_T *)alloc(sizeof(funccall_T));
24796 fc->caller = current_funccal;
24797 current_funccal = fc;
24798 fc->func = fp;
24799 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024800 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024801 fc->linenr = 0;
24802 fc->returned = FALSE;
24803 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024805 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24806 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807
Bram Moolenaar33570922005-01-25 22:26:29 +000024808 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024809 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024810 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24811 * each argument variable and saves a lot of time.
24812 */
24813 /*
24814 * Init l: variables.
24815 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024816 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024817 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024818 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024819 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24820 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024821 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024822 name = v->di_key;
24823 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024824 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024825 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024826 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024827 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024828 v->di_tv.vval.v_dict = selfdict;
24829 ++selfdict->dv_refcount;
24830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024831
Bram Moolenaar33570922005-01-25 22:26:29 +000024832 /*
24833 * Init a: variables.
24834 * Set a:0 to "argcount".
24835 * Set a:000 to a list with room for the "..." arguments.
24836 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024837 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024838 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024839 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024840 /* Use "name" to avoid a warning from some compiler that checks the
24841 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024842 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024843 name = v->di_key;
24844 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024845 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024846 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024847 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024848 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024849 v->di_tv.vval.v_list = &fc->l_varlist;
24850 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24851 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24852 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024853
24854 /*
24855 * Set a:firstline to "firstline" and a:lastline to "lastline".
24856 * Set a:name to named arguments.
24857 * Set a:N to the "..." arguments.
24858 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024859 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024860 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024861 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024862 (varnumber_T)lastline);
24863 for (i = 0; i < argcount; ++i)
24864 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024865 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024866 if (ai < 0)
24867 /* named argument a:name */
24868 name = FUNCARG(fp, i);
24869 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024870 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024871 /* "..." argument a:1, a:2, etc. */
24872 sprintf((char *)numbuf, "%d", ai + 1);
24873 name = numbuf;
24874 }
24875 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24876 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024877 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24879 }
24880 else
24881 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024882 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24883 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024884 if (v == NULL)
24885 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024886 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024887 }
24888 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024889 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024890
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024891 /* Note: the values are copied directly to avoid alloc/free.
24892 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024893 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024894 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024895
24896 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24897 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024898 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24899 fc->l_listitems[ai].li_tv = argvars[i];
24900 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024901 }
24902 }
24903
Bram Moolenaar071d4272004-06-13 20:20:40 +000024904 /* Don't redraw while executing the function. */
24905 ++RedrawingDisabled;
24906 save_sourcing_name = sourcing_name;
24907 save_sourcing_lnum = sourcing_lnum;
24908 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024909 /* need space for function name + ("function " + 3) or "[number]" */
24910 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24911 + STRLEN(fp->uf_name) + 20;
24912 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 if (sourcing_name != NULL)
24914 {
24915 if (save_sourcing_name != NULL
24916 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024917 sprintf((char *)sourcing_name, "%s[%d]..",
24918 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919 else
24920 STRCPY(sourcing_name, "function ");
24921 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24922
24923 if (p_verbose >= 12)
24924 {
24925 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024926 verbose_enter_scroll();
24927
Bram Moolenaar555b2802005-05-19 21:08:39 +000024928 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 if (p_verbose >= 14)
24930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024932 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024933 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024934 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935
24936 msg_puts((char_u *)"(");
24937 for (i = 0; i < argcount; ++i)
24938 {
24939 if (i > 0)
24940 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024941 if (argvars[i].v_type == VAR_NUMBER)
24942 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943 else
24944 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024945 /* Do not want errors such as E724 here. */
24946 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024947 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024948 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024949 if (s != NULL)
24950 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024951 if (vim_strsize(s) > MSG_BUF_CLEN)
24952 {
24953 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24954 s = buf;
24955 }
24956 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024957 vim_free(tofree);
24958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959 }
24960 }
24961 msg_puts((char_u *)")");
24962 }
24963 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024964
24965 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966 --no_wait_return;
24967 }
24968 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024969#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024970 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024971 {
24972 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24973 func_do_profile(fp);
24974 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024975 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024976 {
24977 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024978 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024979 profile_zero(&fp->uf_tm_children);
24980 }
24981 script_prof_save(&wait_start);
24982 }
24983#endif
24984
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024986 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 save_did_emsg = did_emsg;
24988 did_emsg = FALSE;
24989
24990 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024991 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24993
24994 --RedrawingDisabled;
24995
24996 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024997 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024999 clear_tv(rettv);
25000 rettv->v_type = VAR_NUMBER;
25001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025002 }
25003
Bram Moolenaar05159a02005-02-26 23:04:13 +000025004#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025005 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025006 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025007 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025008 profile_end(&call_start);
25009 profile_sub_wait(&wait_start, &call_start);
25010 profile_add(&fp->uf_tm_total, &call_start);
25011 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025012 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025013 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025014 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25015 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025016 }
25017 }
25018#endif
25019
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020 /* when being verbose, mention the return value */
25021 if (p_verbose >= 12)
25022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025024 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025027 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025028 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025029 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025030 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025033 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025034 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025035 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025036 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025037
Bram Moolenaar555b2802005-05-19 21:08:39 +000025038 /* The value may be very long. Skip the middle part, so that we
25039 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025040 * truncate it at the end. Don't want errors such as E724 here. */
25041 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025042 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025043 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025044 if (s != NULL)
25045 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025046 if (vim_strsize(s) > MSG_BUF_CLEN)
25047 {
25048 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25049 s = buf;
25050 }
25051 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025052 vim_free(tofree);
25053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 }
25055 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025056
25057 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058 --no_wait_return;
25059 }
25060
25061 vim_free(sourcing_name);
25062 sourcing_name = save_sourcing_name;
25063 sourcing_lnum = save_sourcing_lnum;
25064 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025065#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025066 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025067 script_prof_restore(&wait_start);
25068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069
25070 if (p_verbose >= 12 && sourcing_name != NULL)
25071 {
25072 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025073 verbose_enter_scroll();
25074
Bram Moolenaar555b2802005-05-19 21:08:39 +000025075 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025077
25078 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079 --no_wait_return;
25080 }
25081
25082 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025083 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025085
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025086 /* If the a:000 list and the l: and a: dicts are not referenced we can
25087 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025088 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25089 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25090 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25091 {
25092 free_funccal(fc, FALSE);
25093 }
25094 else
25095 {
25096 hashitem_T *hi;
25097 listitem_T *li;
25098 int todo;
25099
25100 /* "fc" is still in use. This can happen when returning "a:000" or
25101 * assigning "l:" to a global variable.
25102 * Link "fc" in the list for garbage collection later. */
25103 fc->caller = previous_funccal;
25104 previous_funccal = fc;
25105
25106 /* Make a copy of the a: variables, since we didn't do that above. */
25107 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25108 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25109 {
25110 if (!HASHITEM_EMPTY(hi))
25111 {
25112 --todo;
25113 v = HI2DI(hi);
25114 copy_tv(&v->di_tv, &v->di_tv);
25115 }
25116 }
25117
25118 /* Make a copy of the a:000 items, since we didn't do that above. */
25119 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25120 copy_tv(&li->li_tv, &li->li_tv);
25121 }
25122}
25123
25124/*
25125 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025126 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025127 */
25128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025129can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025130{
25131 return (fc->l_varlist.lv_copyID != copyID
25132 && fc->l_vars.dv_copyID != copyID
25133 && fc->l_avars.dv_copyID != copyID);
25134}
25135
25136/*
25137 * Free "fc" and what it contains.
25138 */
25139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025140free_funccal(
25141 funccall_T *fc,
25142 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025143{
25144 listitem_T *li;
25145
25146 /* The a: variables typevals may not have been allocated, only free the
25147 * allocated variables. */
25148 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25149
25150 /* free all l: variables */
25151 vars_clear(&fc->l_vars.dv_hashtab);
25152
25153 /* Free the a:000 variables if they were allocated. */
25154 if (free_val)
25155 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25156 clear_tv(&li->li_tv);
25157
25158 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159}
25160
25161/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025162 * Add a number variable "name" to dict "dp" with value "nr".
25163 */
25164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025165add_nr_var(
25166 dict_T *dp,
25167 dictitem_T *v,
25168 char *name,
25169 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025170{
25171 STRCPY(v->di_key, name);
25172 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25173 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25174 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025175 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025176 v->di_tv.vval.v_number = nr;
25177}
25178
25179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 * ":return [expr]"
25181 */
25182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025183ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184{
25185 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025186 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025187 int returning = FALSE;
25188
25189 if (current_funccal == NULL)
25190 {
25191 EMSG(_("E133: :return not inside a function"));
25192 return;
25193 }
25194
25195 if (eap->skip)
25196 ++emsg_skip;
25197
25198 eap->nextcmd = NULL;
25199 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025200 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 {
25202 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025203 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025205 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 }
25207 /* It's safer to return also on error. */
25208 else if (!eap->skip)
25209 {
25210 /*
25211 * Return unless the expression evaluation has been cancelled due to an
25212 * aborting error, an interrupt, or an exception.
25213 */
25214 if (!aborting())
25215 returning = do_return(eap, FALSE, TRUE, NULL);
25216 }
25217
25218 /* When skipping or the return gets pending, advance to the next command
25219 * in this line (!returning). Otherwise, ignore the rest of the line.
25220 * Following lines will be ignored by get_func_line(). */
25221 if (returning)
25222 eap->nextcmd = NULL;
25223 else if (eap->nextcmd == NULL) /* no argument */
25224 eap->nextcmd = check_nextcmd(arg);
25225
25226 if (eap->skip)
25227 --emsg_skip;
25228}
25229
25230/*
25231 * Return from a function. Possibly makes the return pending. Also called
25232 * for a pending return at the ":endtry" or after returning from an extra
25233 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025234 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025235 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 * FALSE when the return gets pending.
25237 */
25238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025239do_return(
25240 exarg_T *eap,
25241 int reanimate,
25242 int is_cmd,
25243 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244{
25245 int idx;
25246 struct condstack *cstack = eap->cstack;
25247
25248 if (reanimate)
25249 /* Undo the return. */
25250 current_funccal->returned = FALSE;
25251
25252 /*
25253 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25254 * not in its finally clause (which then is to be executed next) is found.
25255 * In this case, make the ":return" pending for execution at the ":endtry".
25256 * Otherwise, return normally.
25257 */
25258 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25259 if (idx >= 0)
25260 {
25261 cstack->cs_pending[idx] = CSTP_RETURN;
25262
25263 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025264 /* A pending return again gets pending. "rettv" points to an
25265 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025267 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 else
25269 {
25270 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025271 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025273 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025275 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 {
25277 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025278 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025279 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 else
25281 EMSG(_(e_outofmem));
25282 }
25283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025284 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285
25286 if (reanimate)
25287 {
25288 /* The pending return value could be overwritten by a ":return"
25289 * without argument in a finally clause; reset the default
25290 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025291 current_funccal->rettv->v_type = VAR_NUMBER;
25292 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 }
25294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025295 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 }
25297 else
25298 {
25299 current_funccal->returned = TRUE;
25300
25301 /* If the return is carried out now, store the return value. For
25302 * a return immediately after reanimation, the value is already
25303 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025304 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025306 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025307 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025309 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310 }
25311 }
25312
25313 return idx < 0;
25314}
25315
25316/*
25317 * Free the variable with a pending return value.
25318 */
25319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025320discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321{
Bram Moolenaar33570922005-01-25 22:26:29 +000025322 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323}
25324
25325/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025326 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327 * is an allocated string. Used by report_pending() for verbose messages.
25328 */
25329 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025330get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025332 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025333 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025334 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025336 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025337 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025338 if (s == NULL)
25339 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025340
25341 STRCPY(IObuff, ":return ");
25342 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25343 if (STRLEN(s) + 8 >= IOSIZE)
25344 STRCPY(IObuff + IOSIZE - 4, "...");
25345 vim_free(tofree);
25346 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025347}
25348
25349/*
25350 * Get next function line.
25351 * Called by do_cmdline() to get the next line.
25352 * Returns allocated string, or NULL for end of function.
25353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025355get_func_line(
25356 int c UNUSED,
25357 void *cookie,
25358 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359{
Bram Moolenaar33570922005-01-25 22:26:29 +000025360 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025361 ufunc_T *fp = fcp->func;
25362 char_u *retval;
25363 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364
25365 /* If breakpoints have been added/deleted need to check for it. */
25366 if (fcp->dbg_tick != debug_tick)
25367 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025368 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 sourcing_lnum);
25370 fcp->dbg_tick = debug_tick;
25371 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025372#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025373 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025374 func_line_end(cookie);
25375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376
Bram Moolenaar05159a02005-02-26 23:04:13 +000025377 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025378 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25379 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 retval = NULL;
25381 else
25382 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025383 /* Skip NULL lines (continuation lines). */
25384 while (fcp->linenr < gap->ga_len
25385 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25386 ++fcp->linenr;
25387 if (fcp->linenr >= gap->ga_len)
25388 retval = NULL;
25389 else
25390 {
25391 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25392 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025393#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025394 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025395 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025396#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398 }
25399
25400 /* Did we encounter a breakpoint? */
25401 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25402 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025403 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025405 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025406 sourcing_lnum);
25407 fcp->dbg_tick = debug_tick;
25408 }
25409
25410 return retval;
25411}
25412
Bram Moolenaar05159a02005-02-26 23:04:13 +000025413#if defined(FEAT_PROFILE) || defined(PROTO)
25414/*
25415 * Called when starting to read a function line.
25416 * "sourcing_lnum" must be correct!
25417 * When skipping lines it may not actually be executed, but we won't find out
25418 * until later and we need to store the time now.
25419 */
25420 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025421func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025422{
25423 funccall_T *fcp = (funccall_T *)cookie;
25424 ufunc_T *fp = fcp->func;
25425
25426 if (fp->uf_profiling && sourcing_lnum >= 1
25427 && sourcing_lnum <= fp->uf_lines.ga_len)
25428 {
25429 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025430 /* Skip continuation lines. */
25431 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25432 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025433 fp->uf_tml_execed = FALSE;
25434 profile_start(&fp->uf_tml_start);
25435 profile_zero(&fp->uf_tml_children);
25436 profile_get_wait(&fp->uf_tml_wait);
25437 }
25438}
25439
25440/*
25441 * Called when actually executing a function line.
25442 */
25443 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025444func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025445{
25446 funccall_T *fcp = (funccall_T *)cookie;
25447 ufunc_T *fp = fcp->func;
25448
25449 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25450 fp->uf_tml_execed = TRUE;
25451}
25452
25453/*
25454 * Called when done with a function line.
25455 */
25456 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025457func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025458{
25459 funccall_T *fcp = (funccall_T *)cookie;
25460 ufunc_T *fp = fcp->func;
25461
25462 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25463 {
25464 if (fp->uf_tml_execed)
25465 {
25466 ++fp->uf_tml_count[fp->uf_tml_idx];
25467 profile_end(&fp->uf_tml_start);
25468 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025469 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025470 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25471 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025472 }
25473 fp->uf_tml_idx = -1;
25474 }
25475}
25476#endif
25477
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478/*
25479 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025480 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 */
25482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025483func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484{
Bram Moolenaar33570922005-01-25 22:26:29 +000025485 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486
25487 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25488 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025489 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 || fcp->returned);
25491}
25492
25493/*
25494 * return TRUE if cookie indicates a function which "abort"s on errors.
25495 */
25496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025497func_has_abort(
25498 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025500 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501}
25502
25503#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25504typedef enum
25505{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025506 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25507 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25508 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509} var_flavour_T;
25510
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025511static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512
25513 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025514var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515{
25516 char_u *p = varname;
25517
25518 if (ASCII_ISUPPER(*p))
25519 {
25520 while (*(++p))
25521 if (ASCII_ISLOWER(*p))
25522 return VAR_FLAVOUR_SESSION;
25523 return VAR_FLAVOUR_VIMINFO;
25524 }
25525 else
25526 return VAR_FLAVOUR_DEFAULT;
25527}
25528#endif
25529
25530#if defined(FEAT_VIMINFO) || defined(PROTO)
25531/*
25532 * Restore global vars that start with a capital from the viminfo file
25533 */
25534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025535read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536{
25537 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025538 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025539 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025540 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025541
25542 if (!writing && (find_viminfo_parameter('!') != NULL))
25543 {
25544 tab = vim_strchr(virp->vir_line + 1, '\t');
25545 if (tab != NULL)
25546 {
25547 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025548 switch (*tab)
25549 {
25550 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025551#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025552 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025553#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025554 case 'D': type = VAR_DICT; break;
25555 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025556 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558
25559 tab = vim_strchr(tab, '\t');
25560 if (tab != NULL)
25561 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025562 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025563 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025564 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025566#ifdef FEAT_FLOAT
25567 else if (type == VAR_FLOAT)
25568 (void)string2float(tab + 1, &tv.vval.v_float);
25569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025571 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025572 if (type == VAR_DICT || type == VAR_LIST)
25573 {
25574 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25575
25576 if (etv == NULL)
25577 /* Failed to parse back the dict or list, use it as a
25578 * string. */
25579 tv.v_type = VAR_STRING;
25580 else
25581 {
25582 vim_free(tv.vval.v_string);
25583 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025584 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025585 }
25586 }
25587
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025588 /* when in a function use global variables */
25589 save_funccal = current_funccal;
25590 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025591 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025592 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025593
25594 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025595 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025596 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25597 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598 }
25599 }
25600 }
25601
25602 return viminfo_readline(virp);
25603}
25604
25605/*
25606 * Write global vars that start with a capital to the viminfo file
25607 */
25608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025609write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025610{
Bram Moolenaar33570922005-01-25 22:26:29 +000025611 hashitem_T *hi;
25612 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025613 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025614 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025615 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025616 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025617 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618
25619 if (find_viminfo_parameter('!') == NULL)
25620 return;
25621
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025622 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025623
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025624 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025625 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025626 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025627 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025629 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025630 this_var = HI2DI(hi);
25631 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025632 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025633 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025634 {
25635 case VAR_STRING: s = "STR"; break;
25636 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025637 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025638 case VAR_DICT: s = "DIC"; break;
25639 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025640 case VAR_SPECIAL: s = "XPL"; break;
25641
25642 case VAR_UNKNOWN:
25643 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025644 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025645 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025646 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025647 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025648 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025649 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025650 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025651 if (p != NULL)
25652 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025653 vim_free(tofree);
25654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 }
25656 }
25657}
25658#endif
25659
25660#if defined(FEAT_SESSION) || defined(PROTO)
25661 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025662store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663{
Bram Moolenaar33570922005-01-25 22:26:29 +000025664 hashitem_T *hi;
25665 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025666 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 char_u *p, *t;
25668
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025669 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025670 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025672 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025673 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025674 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025675 this_var = HI2DI(hi);
25676 if ((this_var->di_tv.v_type == VAR_NUMBER
25677 || this_var->di_tv.v_type == VAR_STRING)
25678 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025679 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025680 /* Escape special characters with a backslash. Turn a LF and
25681 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025682 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025683 (char_u *)"\\\"\n\r");
25684 if (p == NULL) /* out of memory */
25685 break;
25686 for (t = p; *t != NUL; ++t)
25687 if (*t == '\n')
25688 *t = 'n';
25689 else if (*t == '\r')
25690 *t = 'r';
25691 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025692 this_var->di_key,
25693 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25694 : ' ',
25695 p,
25696 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25697 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025698 || put_eol(fd) == FAIL)
25699 {
25700 vim_free(p);
25701 return FAIL;
25702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 vim_free(p);
25704 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025705#ifdef FEAT_FLOAT
25706 else if (this_var->di_tv.v_type == VAR_FLOAT
25707 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25708 {
25709 float_T f = this_var->di_tv.vval.v_float;
25710 int sign = ' ';
25711
25712 if (f < 0)
25713 {
25714 f = -f;
25715 sign = '-';
25716 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025717 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025718 this_var->di_key, sign, f) < 0)
25719 || put_eol(fd) == FAIL)
25720 return FAIL;
25721 }
25722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025723 }
25724 }
25725 return OK;
25726}
25727#endif
25728
Bram Moolenaar661b1822005-07-28 22:36:45 +000025729/*
25730 * Display script name where an item was last set.
25731 * Should only be invoked when 'verbose' is non-zero.
25732 */
25733 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025734last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025735{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025736 char_u *p;
25737
Bram Moolenaar661b1822005-07-28 22:36:45 +000025738 if (scriptID != 0)
25739 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025740 p = home_replace_save(NULL, get_scriptname(scriptID));
25741 if (p != NULL)
25742 {
25743 verbose_enter();
25744 MSG_PUTS(_("\n\tLast set from "));
25745 MSG_PUTS(p);
25746 vim_free(p);
25747 verbose_leave();
25748 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025749 }
25750}
25751
Bram Moolenaard812df62008-11-09 12:46:09 +000025752/*
25753 * List v:oldfiles in a nice way.
25754 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025756ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025757{
25758 list_T *l = vimvars[VV_OLDFILES].vv_list;
25759 listitem_T *li;
25760 int nr = 0;
25761
25762 if (l == NULL)
25763 msg((char_u *)_("No old files"));
25764 else
25765 {
25766 msg_start();
25767 msg_scroll = TRUE;
25768 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25769 {
25770 msg_outnum((long)++nr);
25771 MSG_PUTS(": ");
25772 msg_outtrans(get_tv_string(&li->li_tv));
25773 msg_putchar('\n');
25774 out_flush(); /* output one line at a time */
25775 ui_breakcheck();
25776 }
25777 /* Assume "got_int" was set to truncate the listing. */
25778 got_int = FALSE;
25779
25780#ifdef FEAT_BROWSE_CMD
25781 if (cmdmod.browse)
25782 {
25783 quit_more = FALSE;
25784 nr = prompt_for_number(FALSE);
25785 msg_starthere();
25786 if (nr > 0)
25787 {
25788 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25789 (long)nr);
25790
25791 if (p != NULL)
25792 {
25793 p = expand_env_save(p);
25794 eap->arg = p;
25795 eap->cmdidx = CMD_edit;
25796 cmdmod.browse = FALSE;
25797 do_exedit(eap, NULL);
25798 vim_free(p);
25799 }
25800 }
25801 }
25802#endif
25803 }
25804}
25805
Bram Moolenaar53744302015-07-17 17:38:22 +020025806/* reset v:option_new, v:option_old and v:option_type */
25807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025808reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025809{
25810 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25811 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25812 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25813}
25814
25815
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816#endif /* FEAT_EVAL */
25817
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025819#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820
25821#ifdef WIN3264
25822/*
25823 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25824 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025825static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25826static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25827static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025828
25829/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025830 * Get the short path (8.3) for the filename in "fnamep".
25831 * Only works for a valid file name.
25832 * When the path gets longer "fnamep" is changed and the allocated buffer
25833 * is put in "bufp".
25834 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25835 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 */
25837 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025838get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025840 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025841 char_u *newbuf;
25842
25843 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025844 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025845 if (l > len - 1)
25846 {
25847 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025848 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025849 newbuf = vim_strnsave(*fnamep, l);
25850 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025851 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852
25853 vim_free(*bufp);
25854 *fnamep = *bufp = newbuf;
25855
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025856 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025857 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 }
25859
25860 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025861 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862}
25863
25864/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025865 * Get the short path (8.3) for the filename in "fname". The converted
25866 * path is returned in "bufp".
25867 *
25868 * Some of the directories specified in "fname" may not exist. This function
25869 * will shorten the existing directories at the beginning of the path and then
25870 * append the remaining non-existing path.
25871 *
25872 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025873 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025874 * bufp - Pointer to an allocated buffer for the filename.
25875 * fnamelen - Length of the filename pointed to by fname
25876 *
25877 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878 */
25879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025880shortpath_for_invalid_fname(
25881 char_u **fname,
25882 char_u **bufp,
25883 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025885 char_u *short_fname, *save_fname, *pbuf_unused;
25886 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025887 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025888 int old_len, len;
25889 int new_len, sfx_len;
25890 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891
25892 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025893 old_len = *fnamelen;
25894 save_fname = vim_strnsave(*fname, old_len);
25895 pbuf_unused = NULL;
25896 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025897
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025898 endp = save_fname + old_len - 1; /* Find the end of the copy */
25899 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025901 /*
25902 * Try shortening the supplied path till it succeeds by removing one
25903 * directory at a time from the tail of the path.
25904 */
25905 len = 0;
25906 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025907 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025908 /* go back one path-separator */
25909 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25910 --endp;
25911 if (endp <= save_fname)
25912 break; /* processed the complete path */
25913
25914 /*
25915 * Replace the path separator with a NUL and try to shorten the
25916 * resulting path.
25917 */
25918 ch = *endp;
25919 *endp = 0;
25920 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025921 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025922 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25923 {
25924 retval = FAIL;
25925 goto theend;
25926 }
25927 *endp = ch; /* preserve the string */
25928
25929 if (len > 0)
25930 break; /* successfully shortened the path */
25931
25932 /* failed to shorten the path. Skip the path separator */
25933 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934 }
25935
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025936 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025938 /*
25939 * Succeeded in shortening the path. Now concatenate the shortened
25940 * path with the remaining path at the tail.
25941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025943 /* Compute the length of the new path. */
25944 sfx_len = (int)(save_endp - endp) + 1;
25945 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025947 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025949 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025951 /* There is not enough space in the currently allocated string,
25952 * copy it to a buffer big enough. */
25953 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025955 {
25956 retval = FAIL;
25957 goto theend;
25958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 }
25960 else
25961 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025962 /* Transfer short_fname to the main buffer (it's big enough),
25963 * unless get_short_pathname() did its work in-place. */
25964 *fname = *bufp = save_fname;
25965 if (short_fname != save_fname)
25966 vim_strncpy(save_fname, short_fname, len);
25967 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025969
25970 /* concat the not-shortened part of the path */
25971 vim_strncpy(*fname + len, endp, sfx_len);
25972 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025974
25975theend:
25976 vim_free(pbuf_unused);
25977 vim_free(save_fname);
25978
25979 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980}
25981
25982/*
25983 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025984 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025985 */
25986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025987shortpath_for_partial(
25988 char_u **fnamep,
25989 char_u **bufp,
25990 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991{
25992 int sepcount, len, tflen;
25993 char_u *p;
25994 char_u *pbuf, *tfname;
25995 int hasTilde;
25996
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025997 /* Count up the path separators from the RHS.. so we know which part
25998 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026000 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026001 if (vim_ispathsep(*p))
26002 ++sepcount;
26003
26004 /* Need full path first (use expand_env() to remove a "~/") */
26005 hasTilde = (**fnamep == '~');
26006 if (hasTilde)
26007 pbuf = tfname = expand_env_save(*fnamep);
26008 else
26009 pbuf = tfname = FullName_save(*fnamep, FALSE);
26010
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026011 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026013 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26014 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015
26016 if (len == 0)
26017 {
26018 /* Don't have a valid filename, so shorten the rest of the
26019 * path if we can. This CAN give us invalid 8.3 filenames, but
26020 * there's not a lot of point in guessing what it might be.
26021 */
26022 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026023 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26024 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025 }
26026
26027 /* Count the paths backward to find the beginning of the desired string. */
26028 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026029 {
26030#ifdef FEAT_MBYTE
26031 if (has_mbyte)
26032 p -= mb_head_off(tfname, p);
26033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026034 if (vim_ispathsep(*p))
26035 {
26036 if (sepcount == 0 || (hasTilde && sepcount == 1))
26037 break;
26038 else
26039 sepcount --;
26040 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026042 if (hasTilde)
26043 {
26044 --p;
26045 if (p >= tfname)
26046 *p = '~';
26047 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026048 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049 }
26050 else
26051 ++p;
26052
26053 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26054 vim_free(*bufp);
26055 *fnamelen = (int)STRLEN(p);
26056 *bufp = pbuf;
26057 *fnamep = p;
26058
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026059 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060}
26061#endif /* WIN3264 */
26062
26063/*
26064 * Adjust a filename, according to a string of modifiers.
26065 * *fnamep must be NUL terminated when called. When returning, the length is
26066 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026067 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068 * When there is an error, *fnamep is set to NULL.
26069 */
26070 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026071modify_fname(
26072 char_u *src, /* string with modifiers */
26073 int *usedlen, /* characters after src that are used */
26074 char_u **fnamep, /* file name so far */
26075 char_u **bufp, /* buffer for allocated file name or NULL */
26076 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077{
26078 int valid = 0;
26079 char_u *tail;
26080 char_u *s, *p, *pbuf;
26081 char_u dirname[MAXPATHL];
26082 int c;
26083 int has_fullname = 0;
26084#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026085 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026086 int has_shortname = 0;
26087#endif
26088
26089repeat:
26090 /* ":p" - full path/file_name */
26091 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26092 {
26093 has_fullname = 1;
26094
26095 valid |= VALID_PATH;
26096 *usedlen += 2;
26097
26098 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26099 if ((*fnamep)[0] == '~'
26100#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26101 && ((*fnamep)[1] == '/'
26102# ifdef BACKSLASH_IN_FILENAME
26103 || (*fnamep)[1] == '\\'
26104# endif
26105 || (*fnamep)[1] == NUL)
26106
26107#endif
26108 )
26109 {
26110 *fnamep = expand_env_save(*fnamep);
26111 vim_free(*bufp); /* free any allocated file name */
26112 *bufp = *fnamep;
26113 if (*fnamep == NULL)
26114 return -1;
26115 }
26116
26117 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026118 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026119 {
26120 if (vim_ispathsep(*p)
26121 && p[1] == '.'
26122 && (p[2] == NUL
26123 || vim_ispathsep(p[2])
26124 || (p[2] == '.'
26125 && (p[3] == NUL || vim_ispathsep(p[3])))))
26126 break;
26127 }
26128
26129 /* FullName_save() is slow, don't use it when not needed. */
26130 if (*p != NUL || !vim_isAbsName(*fnamep))
26131 {
26132 *fnamep = FullName_save(*fnamep, *p != NUL);
26133 vim_free(*bufp); /* free any allocated file name */
26134 *bufp = *fnamep;
26135 if (*fnamep == NULL)
26136 return -1;
26137 }
26138
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026139#ifdef WIN3264
26140# if _WIN32_WINNT >= 0x0500
26141 if (vim_strchr(*fnamep, '~') != NULL)
26142 {
26143 /* Expand 8.3 filename to full path. Needed to make sure the same
26144 * file does not have two different names.
26145 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26146 p = alloc(_MAX_PATH + 1);
26147 if (p != NULL)
26148 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026149 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026150 {
26151 vim_free(*bufp);
26152 *bufp = *fnamep = p;
26153 }
26154 else
26155 vim_free(p);
26156 }
26157 }
26158# endif
26159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 /* Append a path separator to a directory. */
26161 if (mch_isdir(*fnamep))
26162 {
26163 /* Make room for one or two extra characters. */
26164 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26165 vim_free(*bufp); /* free any allocated file name */
26166 *bufp = *fnamep;
26167 if (*fnamep == NULL)
26168 return -1;
26169 add_pathsep(*fnamep);
26170 }
26171 }
26172
26173 /* ":." - path relative to the current directory */
26174 /* ":~" - path relative to the home directory */
26175 /* ":8" - shortname path - postponed till after */
26176 while (src[*usedlen] == ':'
26177 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26178 {
26179 *usedlen += 2;
26180 if (c == '8')
26181 {
26182#ifdef WIN3264
26183 has_shortname = 1; /* Postpone this. */
26184#endif
26185 continue;
26186 }
26187 pbuf = NULL;
26188 /* Need full path first (use expand_env() to remove a "~/") */
26189 if (!has_fullname)
26190 {
26191 if (c == '.' && **fnamep == '~')
26192 p = pbuf = expand_env_save(*fnamep);
26193 else
26194 p = pbuf = FullName_save(*fnamep, FALSE);
26195 }
26196 else
26197 p = *fnamep;
26198
26199 has_fullname = 0;
26200
26201 if (p != NULL)
26202 {
26203 if (c == '.')
26204 {
26205 mch_dirname(dirname, MAXPATHL);
26206 s = shorten_fname(p, dirname);
26207 if (s != NULL)
26208 {
26209 *fnamep = s;
26210 if (pbuf != NULL)
26211 {
26212 vim_free(*bufp); /* free any allocated file name */
26213 *bufp = pbuf;
26214 pbuf = NULL;
26215 }
26216 }
26217 }
26218 else
26219 {
26220 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26221 /* Only replace it when it starts with '~' */
26222 if (*dirname == '~')
26223 {
26224 s = vim_strsave(dirname);
26225 if (s != NULL)
26226 {
26227 *fnamep = s;
26228 vim_free(*bufp);
26229 *bufp = s;
26230 }
26231 }
26232 }
26233 vim_free(pbuf);
26234 }
26235 }
26236
26237 tail = gettail(*fnamep);
26238 *fnamelen = (int)STRLEN(*fnamep);
26239
26240 /* ":h" - head, remove "/file_name", can be repeated */
26241 /* Don't remove the first "/" or "c:\" */
26242 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26243 {
26244 valid |= VALID_HEAD;
26245 *usedlen += 2;
26246 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026247 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026248 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249 *fnamelen = (int)(tail - *fnamep);
26250#ifdef VMS
26251 if (*fnamelen > 0)
26252 *fnamelen += 1; /* the path separator is part of the path */
26253#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026254 if (*fnamelen == 0)
26255 {
26256 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26257 p = vim_strsave((char_u *)".");
26258 if (p == NULL)
26259 return -1;
26260 vim_free(*bufp);
26261 *bufp = *fnamep = tail = p;
26262 *fnamelen = 1;
26263 }
26264 else
26265 {
26266 while (tail > s && !after_pathsep(s, tail))
26267 mb_ptr_back(*fnamep, tail);
26268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269 }
26270
26271 /* ":8" - shortname */
26272 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26273 {
26274 *usedlen += 2;
26275#ifdef WIN3264
26276 has_shortname = 1;
26277#endif
26278 }
26279
26280#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026281 /*
26282 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026283 */
26284 if (has_shortname)
26285 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026286 /* Copy the string if it is shortened by :h and when it wasn't copied
26287 * yet, because we are going to change it in place. Avoids changing
26288 * the buffer name for "%:8". */
26289 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026290 {
26291 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026292 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293 return -1;
26294 vim_free(*bufp);
26295 *bufp = *fnamep = p;
26296 }
26297
26298 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026299 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026300 if (!has_fullname && !vim_isAbsName(*fnamep))
26301 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026302 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026303 return -1;
26304 }
26305 else
26306 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026307 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308
Bram Moolenaardc935552011-08-17 15:23:23 +020026309 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026311 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026312 return -1;
26313
26314 if (l == 0)
26315 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026316 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026318 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026319 return -1;
26320 }
26321 *fnamelen = l;
26322 }
26323 }
26324#endif /* WIN3264 */
26325
26326 /* ":t" - tail, just the basename */
26327 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26328 {
26329 *usedlen += 2;
26330 *fnamelen -= (int)(tail - *fnamep);
26331 *fnamep = tail;
26332 }
26333
26334 /* ":e" - extension, can be repeated */
26335 /* ":r" - root, without extension, can be repeated */
26336 while (src[*usedlen] == ':'
26337 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26338 {
26339 /* find a '.' in the tail:
26340 * - for second :e: before the current fname
26341 * - otherwise: The last '.'
26342 */
26343 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26344 s = *fnamep - 2;
26345 else
26346 s = *fnamep + *fnamelen - 1;
26347 for ( ; s > tail; --s)
26348 if (s[0] == '.')
26349 break;
26350 if (src[*usedlen + 1] == 'e') /* :e */
26351 {
26352 if (s > tail)
26353 {
26354 *fnamelen += (int)(*fnamep - (s + 1));
26355 *fnamep = s + 1;
26356#ifdef VMS
26357 /* cut version from the extension */
26358 s = *fnamep + *fnamelen - 1;
26359 for ( ; s > *fnamep; --s)
26360 if (s[0] == ';')
26361 break;
26362 if (s > *fnamep)
26363 *fnamelen = s - *fnamep;
26364#endif
26365 }
26366 else if (*fnamep <= tail)
26367 *fnamelen = 0;
26368 }
26369 else /* :r */
26370 {
26371 if (s > tail) /* remove one extension */
26372 *fnamelen = (int)(s - *fnamep);
26373 }
26374 *usedlen += 2;
26375 }
26376
26377 /* ":s?pat?foo?" - substitute */
26378 /* ":gs?pat?foo?" - global substitute */
26379 if (src[*usedlen] == ':'
26380 && (src[*usedlen + 1] == 's'
26381 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26382 {
26383 char_u *str;
26384 char_u *pat;
26385 char_u *sub;
26386 int sep;
26387 char_u *flags;
26388 int didit = FALSE;
26389
26390 flags = (char_u *)"";
26391 s = src + *usedlen + 2;
26392 if (src[*usedlen + 1] == 'g')
26393 {
26394 flags = (char_u *)"g";
26395 ++s;
26396 }
26397
26398 sep = *s++;
26399 if (sep)
26400 {
26401 /* find end of pattern */
26402 p = vim_strchr(s, sep);
26403 if (p != NULL)
26404 {
26405 pat = vim_strnsave(s, (int)(p - s));
26406 if (pat != NULL)
26407 {
26408 s = p + 1;
26409 /* find end of substitution */
26410 p = vim_strchr(s, sep);
26411 if (p != NULL)
26412 {
26413 sub = vim_strnsave(s, (int)(p - s));
26414 str = vim_strnsave(*fnamep, *fnamelen);
26415 if (sub != NULL && str != NULL)
26416 {
26417 *usedlen = (int)(p + 1 - src);
26418 s = do_string_sub(str, pat, sub, flags);
26419 if (s != NULL)
26420 {
26421 *fnamep = s;
26422 *fnamelen = (int)STRLEN(s);
26423 vim_free(*bufp);
26424 *bufp = s;
26425 didit = TRUE;
26426 }
26427 }
26428 vim_free(sub);
26429 vim_free(str);
26430 }
26431 vim_free(pat);
26432 }
26433 }
26434 /* after using ":s", repeat all the modifiers */
26435 if (didit)
26436 goto repeat;
26437 }
26438 }
26439
Bram Moolenaar26df0922014-02-23 23:39:13 +010026440 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26441 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026442 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026443 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026444 if (c != NUL)
26445 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026446 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026447 if (c != NUL)
26448 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026449 if (p == NULL)
26450 return -1;
26451 vim_free(*bufp);
26452 *bufp = *fnamep = p;
26453 *fnamelen = (int)STRLEN(p);
26454 *usedlen += 2;
26455 }
26456
Bram Moolenaar071d4272004-06-13 20:20:40 +000026457 return valid;
26458}
26459
26460/*
26461 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26462 * "flags" can be "g" to do a global substitute.
26463 * Returns an allocated string, NULL for error.
26464 */
26465 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026466do_string_sub(
26467 char_u *str,
26468 char_u *pat,
26469 char_u *sub,
26470 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026471{
26472 int sublen;
26473 regmatch_T regmatch;
26474 int i;
26475 int do_all;
26476 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026477 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026478 garray_T ga;
26479 char_u *ret;
26480 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026481 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026482
26483 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26484 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026485 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026486
26487 ga_init2(&ga, 1, 200);
26488
26489 do_all = (flags[0] == 'g');
26490
26491 regmatch.rm_ic = p_ic;
26492 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26493 if (regmatch.regprog != NULL)
26494 {
26495 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026496 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026497 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26498 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026499 /* Skip empty match except for first match. */
26500 if (regmatch.startp[0] == regmatch.endp[0])
26501 {
26502 if (zero_width == regmatch.startp[0])
26503 {
26504 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026505 i = MB_PTR2LEN(tail);
26506 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26507 (size_t)i);
26508 ga.ga_len += i;
26509 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026510 continue;
26511 }
26512 zero_width = regmatch.startp[0];
26513 }
26514
Bram Moolenaar071d4272004-06-13 20:20:40 +000026515 /*
26516 * Get some space for a temporary buffer to do the substitution
26517 * into. It will contain:
26518 * - The text up to where the match is.
26519 * - The substituted text.
26520 * - The text after the match.
26521 */
26522 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026523 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26525 {
26526 ga_clear(&ga);
26527 break;
26528 }
26529
26530 /* copy the text up to where the match is */
26531 i = (int)(regmatch.startp[0] - tail);
26532 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26533 /* add the substituted text */
26534 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26535 + ga.ga_len + i, TRUE, TRUE, FALSE);
26536 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026537 tail = regmatch.endp[0];
26538 if (*tail == NUL)
26539 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026540 if (!do_all)
26541 break;
26542 }
26543
26544 if (ga.ga_data != NULL)
26545 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26546
Bram Moolenaar473de612013-06-08 18:19:48 +020026547 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026548 }
26549
26550 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26551 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026552 if (p_cpo == empty_option)
26553 p_cpo = save_cpo;
26554 else
26555 /* Darn, evaluating {sub} expression changed the value. */
26556 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026557
26558 return ret;
26559}
26560
26561#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */