blob: 86580450fca5fe5f4f1e3b82f86d67686a5d80a2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000113
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100114#define NAMESPACE_CHAR (char_u *)"abglstvw"
115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000128 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 */
130static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131#define COPYID_INC 2
132#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133
Bram Moolenaar8502c702014-06-17 12:51:16 +0200134/* Abort conversion to string after a recursion error. */
135static int did_echo_string_emsg = FALSE;
136
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100212/* List heads for garbage collection. Although there can be a reference loop
213 * from partial to dict to partial, we don't need to keep track of the partial,
214 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100300 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
302} vimvars[VV_LEN] =
303{
304 /*
305 * The order here must match the VV_ defines in vim.h!
306 * Initializing a union does not work, leave tv.vval empty to get zero's.
307 */
308 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("count1", VAR_NUMBER), VV_RO},
310 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
311 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
312 {VV_NAME("warningmsg", VAR_STRING), 0},
313 {VV_NAME("statusmsg", VAR_STRING), 0},
314 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
316 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
318 {VV_NAME("termresponse", VAR_STRING), VV_RO},
319 {VV_NAME("fname", VAR_STRING), VV_RO},
320 {VV_NAME("lang", VAR_STRING), VV_RO},
321 {VV_NAME("lc_time", VAR_STRING), VV_RO},
322 {VV_NAME("ctype", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
325 {VV_NAME("fname_in", VAR_STRING), VV_RO},
326 {VV_NAME("fname_out", VAR_STRING), VV_RO},
327 {VV_NAME("fname_new", VAR_STRING), VV_RO},
328 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
329 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
330 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
333 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("progname", VAR_STRING), VV_RO},
335 {VV_NAME("servername", VAR_STRING), VV_RO},
336 {VV_NAME("dying", VAR_NUMBER), VV_RO},
337 {VV_NAME("exception", VAR_STRING), VV_RO},
338 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
339 {VV_NAME("register", VAR_STRING), VV_RO},
340 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
341 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000342 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
343 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000344 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000345 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
346 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000347 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000352 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000353 {VV_NAME("swapname", VAR_STRING), VV_RO},
354 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000355 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200356 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000357 {VV_NAME("mouse_win", VAR_NUMBER), 0},
358 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
359 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000360 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100362 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000363 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200364 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200365 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200366 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200367 {VV_NAME("option_new", VAR_STRING), VV_RO},
368 {VV_NAME("option_old", VAR_STRING), VV_RO},
369 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100370 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371 {VV_NAME("false", VAR_SPECIAL), VV_RO},
372 {VV_NAME("true", VAR_SPECIAL), VV_RO},
373 {VV_NAME("null", VAR_SPECIAL), VV_RO},
374 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100375 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200376 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200434static void list_free_contents(list_T *l);
435static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100436static long list_len(list_T *l);
437static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
438static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
439static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
440static long list_find_nr(list_T *l, long idx, int *errorp);
441static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100442static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
443static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
444static list_T *list_copy(list_T *orig, int deep, int copyID);
445static char_u *list2string(typval_T *tv, int copyID);
446static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
447static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
448static int free_unref_items(int copyID);
449static dictitem_T *dictitem_copy(dictitem_T *org);
450static void dictitem_remove(dict_T *dict, dictitem_T *item);
451static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
452static long dict_len(dict_T *d);
453static char_u *dict2string(typval_T *tv, int copyID);
454static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
455static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
457static char_u *string_quote(char_u *str, int function);
458static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
459static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100460static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
461static 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 +0100462static void emsg_funcname(char *ermsg, char_u *name);
463static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200465static void dict_free_contents(dict_T *d);
466static void dict_free_dict(dict_T *d);
467
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100469static void f_abs(typval_T *argvars, typval_T *rettv);
470static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100472static void f_add(typval_T *argvars, typval_T *rettv);
473static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
474static void f_and(typval_T *argvars, typval_T *rettv);
475static void f_append(typval_T *argvars, typval_T *rettv);
476static void f_argc(typval_T *argvars, typval_T *rettv);
477static void f_argidx(typval_T *argvars, typval_T *rettv);
478static void f_arglistid(typval_T *argvars, typval_T *rettv);
479static void f_argv(typval_T *argvars, typval_T *rettv);
480static void f_assert_equal(typval_T *argvars, typval_T *rettv);
481static void f_assert_exception(typval_T *argvars, typval_T *rettv);
482static void f_assert_fails(typval_T *argvars, typval_T *rettv);
483static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200484static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200485static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
486static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100487static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_asin(typval_T *argvars, typval_T *rettv);
490static void f_atan(typval_T *argvars, typval_T *rettv);
491static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_browse(typval_T *argvars, typval_T *rettv);
494static void f_browsedir(typval_T *argvars, typval_T *rettv);
495static void f_bufexists(typval_T *argvars, typval_T *rettv);
496static void f_buflisted(typval_T *argvars, typval_T *rettv);
497static void f_bufloaded(typval_T *argvars, typval_T *rettv);
498static void f_bufname(typval_T *argvars, typval_T *rettv);
499static void f_bufnr(typval_T *argvars, typval_T *rettv);
500static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
501static void f_byte2line(typval_T *argvars, typval_T *rettv);
502static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
503static void f_byteidx(typval_T *argvars, typval_T *rettv);
504static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
505static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000506#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100507static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100509#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100511static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
512static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100513static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100514static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100515static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100516static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100517static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
518static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100519static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100520static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100521static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
522static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100523static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100524static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100525#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_changenr(typval_T *argvars, typval_T *rettv);
527static void f_char2nr(typval_T *argvars, typval_T *rettv);
528static void f_cindent(typval_T *argvars, typval_T *rettv);
529static void f_clearmatches(typval_T *argvars, typval_T *rettv);
530static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000531#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_complete(typval_T *argvars, typval_T *rettv);
533static void f_complete_add(typval_T *argvars, typval_T *rettv);
534static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000535#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100536static void f_confirm(typval_T *argvars, typval_T *rettv);
537static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_cos(typval_T *argvars, typval_T *rettv);
540static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000541#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_count(typval_T *argvars, typval_T *rettv);
543static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
544static void f_cursor(typval_T *argsvars, typval_T *rettv);
545static void f_deepcopy(typval_T *argvars, typval_T *rettv);
546static void f_delete(typval_T *argvars, typval_T *rettv);
547static void f_did_filetype(typval_T *argvars, typval_T *rettv);
548static void f_diff_filler(typval_T *argvars, typval_T *rettv);
549static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100550static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_empty(typval_T *argvars, typval_T *rettv);
552static void f_escape(typval_T *argvars, typval_T *rettv);
553static void f_eval(typval_T *argvars, typval_T *rettv);
554static void f_eventhandler(typval_T *argvars, typval_T *rettv);
555static void f_executable(typval_T *argvars, typval_T *rettv);
556static void f_exepath(typval_T *argvars, typval_T *rettv);
557static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_expand(typval_T *argvars, typval_T *rettv);
562static void f_extend(typval_T *argvars, typval_T *rettv);
563static void f_feedkeys(typval_T *argvars, typval_T *rettv);
564static void f_filereadable(typval_T *argvars, typval_T *rettv);
565static void f_filewritable(typval_T *argvars, typval_T *rettv);
566static void f_filter(typval_T *argvars, typval_T *rettv);
567static void f_finddir(typval_T *argvars, typval_T *rettv);
568static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000569#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100570static void f_float2nr(typval_T *argvars, typval_T *rettv);
571static void f_floor(typval_T *argvars, typval_T *rettv);
572static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000573#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100574static void f_fnameescape(typval_T *argvars, typval_T *rettv);
575static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
576static void f_foldclosed(typval_T *argvars, typval_T *rettv);
577static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
578static void f_foldlevel(typval_T *argvars, typval_T *rettv);
579static void f_foldtext(typval_T *argvars, typval_T *rettv);
580static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
581static void f_foreground(typval_T *argvars, typval_T *rettv);
582static void f_function(typval_T *argvars, typval_T *rettv);
583static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200584static void f_garbagecollect_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100585static void f_get(typval_T *argvars, typval_T *rettv);
586static void f_getbufline(typval_T *argvars, typval_T *rettv);
587static void f_getbufvar(typval_T *argvars, typval_T *rettv);
588static void f_getchar(typval_T *argvars, typval_T *rettv);
589static void f_getcharmod(typval_T *argvars, typval_T *rettv);
590static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
591static void f_getcmdline(typval_T *argvars, typval_T *rettv);
592static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
593static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
594static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
595static void f_getcwd(typval_T *argvars, typval_T *rettv);
596static void f_getfontname(typval_T *argvars, typval_T *rettv);
597static void f_getfperm(typval_T *argvars, typval_T *rettv);
598static void f_getfsize(typval_T *argvars, typval_T *rettv);
599static void f_getftime(typval_T *argvars, typval_T *rettv);
600static void f_getftype(typval_T *argvars, typval_T *rettv);
601static void f_getline(typval_T *argvars, typval_T *rettv);
602static void f_getmatches(typval_T *argvars, typval_T *rettv);
603static void f_getpid(typval_T *argvars, typval_T *rettv);
604static void f_getcurpos(typval_T *argvars, typval_T *rettv);
605static void f_getpos(typval_T *argvars, typval_T *rettv);
606static void f_getqflist(typval_T *argvars, typval_T *rettv);
607static void f_getreg(typval_T *argvars, typval_T *rettv);
608static void f_getregtype(typval_T *argvars, typval_T *rettv);
609static void f_gettabvar(typval_T *argvars, typval_T *rettv);
610static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
611static void f_getwinposx(typval_T *argvars, typval_T *rettv);
612static void f_getwinposy(typval_T *argvars, typval_T *rettv);
613static void f_getwinvar(typval_T *argvars, typval_T *rettv);
614static void f_glob(typval_T *argvars, typval_T *rettv);
615static void f_globpath(typval_T *argvars, typval_T *rettv);
616static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
617static void f_has(typval_T *argvars, typval_T *rettv);
618static void f_has_key(typval_T *argvars, typval_T *rettv);
619static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
620static void f_hasmapto(typval_T *argvars, typval_T *rettv);
621static void f_histadd(typval_T *argvars, typval_T *rettv);
622static void f_histdel(typval_T *argvars, typval_T *rettv);
623static void f_histget(typval_T *argvars, typval_T *rettv);
624static void f_histnr(typval_T *argvars, typval_T *rettv);
625static void f_hlID(typval_T *argvars, typval_T *rettv);
626static void f_hlexists(typval_T *argvars, typval_T *rettv);
627static void f_hostname(typval_T *argvars, typval_T *rettv);
628static void f_iconv(typval_T *argvars, typval_T *rettv);
629static void f_indent(typval_T *argvars, typval_T *rettv);
630static void f_index(typval_T *argvars, typval_T *rettv);
631static void f_input(typval_T *argvars, typval_T *rettv);
632static void f_inputdialog(typval_T *argvars, typval_T *rettv);
633static void f_inputlist(typval_T *argvars, typval_T *rettv);
634static void f_inputrestore(typval_T *argvars, typval_T *rettv);
635static void f_inputsave(typval_T *argvars, typval_T *rettv);
636static void f_inputsecret(typval_T *argvars, typval_T *rettv);
637static void f_insert(typval_T *argvars, typval_T *rettv);
638static void f_invert(typval_T *argvars, typval_T *rettv);
639static void f_isdirectory(typval_T *argvars, typval_T *rettv);
640static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100641#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
642static void f_isnan(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100645#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100646static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100647static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100648static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100649static void f_job_start(typval_T *argvars, typval_T *rettv);
650static void f_job_stop(typval_T *argvars, typval_T *rettv);
651static void f_job_status(typval_T *argvars, typval_T *rettv);
652#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100653static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100654static void f_js_decode(typval_T *argvars, typval_T *rettv);
655static void f_js_encode(typval_T *argvars, typval_T *rettv);
656static void f_json_decode(typval_T *argvars, typval_T *rettv);
657static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_keys(typval_T *argvars, typval_T *rettv);
659static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
660static void f_len(typval_T *argvars, typval_T *rettv);
661static void f_libcall(typval_T *argvars, typval_T *rettv);
662static void f_libcallnr(typval_T *argvars, typval_T *rettv);
663static void f_line(typval_T *argvars, typval_T *rettv);
664static void f_line2byte(typval_T *argvars, typval_T *rettv);
665static void f_lispindent(typval_T *argvars, typval_T *rettv);
666static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100668static void f_log(typval_T *argvars, typval_T *rettv);
669static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200671#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200673#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_map(typval_T *argvars, typval_T *rettv);
675static void f_maparg(typval_T *argvars, typval_T *rettv);
676static void f_mapcheck(typval_T *argvars, typval_T *rettv);
677static void f_match(typval_T *argvars, typval_T *rettv);
678static void f_matchadd(typval_T *argvars, typval_T *rettv);
679static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
680static void f_matcharg(typval_T *argvars, typval_T *rettv);
681static void f_matchdelete(typval_T *argvars, typval_T *rettv);
682static void f_matchend(typval_T *argvars, typval_T *rettv);
683static void f_matchlist(typval_T *argvars, typval_T *rettv);
684static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200685static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_max(typval_T *argvars, typval_T *rettv);
687static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000688#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100692#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
696static void f_nr2char(typval_T *argvars, typval_T *rettv);
697static void f_or(typval_T *argvars, typval_T *rettv);
698static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100699#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100701#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
706static void f_printf(typval_T *argvars, typval_T *rettv);
707static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200708#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200710#endif
711#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200713#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_range(typval_T *argvars, typval_T *rettv);
715static void f_readfile(typval_T *argvars, typval_T *rettv);
716static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100717#ifdef FEAT_FLOAT
718static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_reltimestr(typval_T *argvars, typval_T *rettv);
721static void f_remote_expr(typval_T *argvars, typval_T *rettv);
722static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
723static void f_remote_peek(typval_T *argvars, typval_T *rettv);
724static void f_remote_read(typval_T *argvars, typval_T *rettv);
725static void f_remote_send(typval_T *argvars, typval_T *rettv);
726static void f_remove(typval_T *argvars, typval_T *rettv);
727static void f_rename(typval_T *argvars, typval_T *rettv);
728static void f_repeat(typval_T *argvars, typval_T *rettv);
729static void f_resolve(typval_T *argvars, typval_T *rettv);
730static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100732static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_screenattr(typval_T *argvars, typval_T *rettv);
735static void f_screenchar(typval_T *argvars, typval_T *rettv);
736static void f_screencol(typval_T *argvars, typval_T *rettv);
737static void f_screenrow(typval_T *argvars, typval_T *rettv);
738static void f_search(typval_T *argvars, typval_T *rettv);
739static void f_searchdecl(typval_T *argvars, typval_T *rettv);
740static void f_searchpair(typval_T *argvars, typval_T *rettv);
741static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
742static void f_searchpos(typval_T *argvars, typval_T *rettv);
743static void f_server2client(typval_T *argvars, typval_T *rettv);
744static void f_serverlist(typval_T *argvars, typval_T *rettv);
745static void f_setbufvar(typval_T *argvars, typval_T *rettv);
746static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
747static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100748static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_setline(typval_T *argvars, typval_T *rettv);
750static void f_setloclist(typval_T *argvars, typval_T *rettv);
751static void f_setmatches(typval_T *argvars, typval_T *rettv);
752static void f_setpos(typval_T *argvars, typval_T *rettv);
753static void f_setqflist(typval_T *argvars, typval_T *rettv);
754static void f_setreg(typval_T *argvars, typval_T *rettv);
755static void f_settabvar(typval_T *argvars, typval_T *rettv);
756static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
757static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100758#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100760#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_shellescape(typval_T *argvars, typval_T *rettv);
762static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
763static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_sin(typval_T *argvars, typval_T *rettv);
766static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_sort(typval_T *argvars, typval_T *rettv);
769static void f_soundfold(typval_T *argvars, typval_T *rettv);
770static void f_spellbadword(typval_T *argvars, typval_T *rettv);
771static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
772static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000773#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100774static void f_sqrt(typval_T *argvars, typval_T *rettv);
775static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000776#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_str2nr(typval_T *argvars, typval_T *rettv);
778static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000779#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_stridx(typval_T *argvars, typval_T *rettv);
783static void f_string(typval_T *argvars, typval_T *rettv);
784static void f_strlen(typval_T *argvars, typval_T *rettv);
785static void f_strpart(typval_T *argvars, typval_T *rettv);
786static void f_strridx(typval_T *argvars, typval_T *rettv);
787static void f_strtrans(typval_T *argvars, typval_T *rettv);
788static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
789static void f_strwidth(typval_T *argvars, typval_T *rettv);
790static void f_submatch(typval_T *argvars, typval_T *rettv);
791static void f_substitute(typval_T *argvars, typval_T *rettv);
792static void f_synID(typval_T *argvars, typval_T *rettv);
793static void f_synIDattr(typval_T *argvars, typval_T *rettv);
794static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
795static void f_synstack(typval_T *argvars, typval_T *rettv);
796static void f_synconcealed(typval_T *argvars, typval_T *rettv);
797static void f_system(typval_T *argvars, typval_T *rettv);
798static void f_systemlist(typval_T *argvars, typval_T *rettv);
799static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
800static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
801static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
802static void f_taglist(typval_T *argvars, typval_T *rettv);
803static void f_tagfiles(typval_T *argvars, typval_T *rettv);
804static void f_tempname(typval_T *argvars, typval_T *rettv);
805static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200806#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100807static void f_tan(typval_T *argvars, typval_T *rettv);
808static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200809#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100810#ifdef FEAT_TIMERS
811static void f_timer_start(typval_T *argvars, typval_T *rettv);
812static void f_timer_stop(typval_T *argvars, typval_T *rettv);
813#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100814static void f_tolower(typval_T *argvars, typval_T *rettv);
815static void f_toupper(typval_T *argvars, typval_T *rettv);
816static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000817#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100818static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000819#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static void f_type(typval_T *argvars, typval_T *rettv);
821static void f_undofile(typval_T *argvars, typval_T *rettv);
822static void f_undotree(typval_T *argvars, typval_T *rettv);
823static void f_uniq(typval_T *argvars, typval_T *rettv);
824static void f_values(typval_T *argvars, typval_T *rettv);
825static void f_virtcol(typval_T *argvars, typval_T *rettv);
826static void f_visualmode(typval_T *argvars, typval_T *rettv);
827static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100828static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100829static void f_win_getid(typval_T *argvars, typval_T *rettv);
830static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
831static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
832static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_winbufnr(typval_T *argvars, typval_T *rettv);
834static void f_wincol(typval_T *argvars, typval_T *rettv);
835static void f_winheight(typval_T *argvars, typval_T *rettv);
836static void f_winline(typval_T *argvars, typval_T *rettv);
837static void f_winnr(typval_T *argvars, typval_T *rettv);
838static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
839static void f_winrestview(typval_T *argvars, typval_T *rettv);
840static void f_winsaveview(typval_T *argvars, typval_T *rettv);
841static void f_winwidth(typval_T *argvars, typval_T *rettv);
842static void f_writefile(typval_T *argvars, typval_T *rettv);
843static void f_wordcount(typval_T *argvars, typval_T *rettv);
844static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000845
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
847static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
848static int get_env_len(char_u **arg);
849static int get_id_len(char_u **arg);
850static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
851static 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 +0000852#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
853#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
854 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100855static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
856static int eval_isnamec(int c);
857static int eval_isnamec1(int c);
858static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
859static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100860static typval_T *alloc_string_tv(char_u *string);
861static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100862#ifdef FEAT_FLOAT
863static float_T get_tv_float(typval_T *varp);
864#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100865static linenr_T get_tv_lnum(typval_T *argvars);
866static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100867static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
868static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
869static hashtab_T *find_var_ht(char_u *name, char_u **varname);
870static funccall_T *get_funccal(void);
871static void vars_clear_ext(hashtab_T *ht, int free_val);
872static void delete_var(hashtab_T *ht, hashitem_T *hi);
873static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
874static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
875static void set_var(char_u *name, typval_T *varp, int copy);
876static int var_check_ro(int flags, char_u *name, int use_gettext);
877static int var_check_fixed(int flags, char_u *name, int use_gettext);
878static int var_check_func_name(char_u *name, int new_var);
879static int valid_varname(char_u *varname);
880static int tv_check_lock(int lock, char_u *name, int use_gettext);
881static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
882static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100883static 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 +0100884static int eval_fname_script(char_u *p);
885static int eval_fname_sid(char_u *p);
886static void list_func_head(ufunc_T *fp, int indent);
887static ufunc_T *find_func(char_u *name);
888static int function_exists(char_u *name);
889static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100891static void func_do_profile(ufunc_T *fp);
892static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
893static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000894static int
895# ifdef __BORLANDC__
896 _RTLENTRYF
897# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100898 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000899static int
900# ifdef __BORLANDC__
901 _RTLENTRYF
902# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100903 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100905static int script_autoload(char_u *name, int reload);
906static char_u *autoload_name(char_u *name);
907static void cat_func_name(char_u *buf, ufunc_T *fp);
908static void func_free(ufunc_T *fp);
909static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
910static int can_free_funccal(funccall_T *fc, int copyID) ;
911static void free_funccal(funccall_T *fc, int free_val);
912static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
913static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
914static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
915static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
916static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
917static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
918static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
919static int write_list(FILE *fd, list_T *list, int binary);
920static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000921
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200922
923#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100924static int compare_func_name(const void *s1, const void *s2);
925static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200926#endif
927
Bram Moolenaar33570922005-01-25 22:26:29 +0000928/*
929 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000930 */
931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100932eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000933{
Bram Moolenaar33570922005-01-25 22:26:29 +0000934 int i;
935 struct vimvar *p;
936
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200937 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
938 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200939 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000940 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000941 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000942
943 for (i = 0; i < VV_LEN; ++i)
944 {
945 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200946 if (STRLEN(p->vv_name) > 16)
947 {
948 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
949 getout(1);
950 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000951 STRCPY(p->vv_di.di_key, p->vv_name);
952 if (p->vv_flags & VV_RO)
953 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
954 else if (p->vv_flags & VV_RO_SBX)
955 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
956 else
957 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000958
959 /* add to v: scope dict, unless the value is not always available */
960 if (p->vv_type != VAR_UNKNOWN)
961 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000963 /* add to compat scope dict */
964 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100966 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
967
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000968 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100969 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200970 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100971 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100972
973 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
974 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
975 set_vim_var_nr(VV_NONE, VVAL_NONE);
976 set_vim_var_nr(VV_NULL, VVAL_NULL);
977
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200978 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200979
980#ifdef EBCDIC
981 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100982 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200983 */
984 sortFunctions();
985#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000986}
987
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000988#if defined(EXITFREE) || defined(PROTO)
989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100990eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000991{
992 int i;
993 struct vimvar *p;
994
995 for (i = 0; i < VV_LEN; ++i)
996 {
997 p = &vimvars[i];
998 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000999 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001000 vim_free(p->vv_str);
1001 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001002 }
1003 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1004 {
1005 list_unref(p->vv_list);
1006 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001007 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001008 }
1009 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001010 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001011 hash_clear(&compat_hashtab);
1012
Bram Moolenaard9fba312005-06-26 22:34:35 +00001013 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001014# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001015 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001016# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001017
1018 /* global variables */
1019 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001020
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001021 /* autoloaded script names */
1022 ga_clear_strings(&ga_loaded);
1023
Bram Moolenaarcca74132013-09-25 21:00:28 +02001024 /* Script-local variables. First clear all the variables and in a second
1025 * loop free the scriptvar_T, because a variable in one script might hold
1026 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001027 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001028 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001029 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001030 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001031 ga_clear(&ga_scripts);
1032
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001033 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001034 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001035
1036 /* functions */
1037 free_all_functions();
1038 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001039}
1040#endif
1041
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 * Return the name of the executed function.
1044 */
1045 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001046func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001048 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050
1051/*
1052 * Return the address holding the next breakpoint line for a funccall cookie.
1053 */
1054 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001055func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaar33570922005-01-25 22:26:29 +00001057 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058}
1059
1060/*
1061 * Return the address holding the debug tick for a funccall cookie.
1062 */
1063 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001064func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
Bram Moolenaar33570922005-01-25 22:26:29 +00001066 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067}
1068
1069/*
1070 * Return the nesting level for a funccall cookie.
1071 */
1072 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001073func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076}
1077
1078/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001079funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001081/* pointer to list of previously used funccal, still around because some
1082 * item in it is still being used. */
1083funccall_T *previous_funccal = NULL;
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085/*
1086 * Return TRUE when a function was ended by a ":return" command.
1087 */
1088 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001089current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 return current_funccal->returned;
1092}
1093
1094
1095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 * Set an internal variable to a string value. Creates the variable if it does
1097 * not already exist.
1098 */
1099 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001100set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001102 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001103 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
1105 val = vim_strsave(value);
1106 if (val != NULL)
1107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001108 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001109 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001111 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001112 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 }
1114 }
1115}
1116
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119static char_u *redir_endp = NULL;
1120static char_u *redir_varname = NULL;
1121
1122/*
1123 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001124 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 * Returns OK if successfully completed the setup. FAIL otherwise.
1126 */
1127 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001128var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129{
1130 int save_emsg;
1131 int err;
1132 typval_T tv;
1133
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001135 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 {
1137 EMSG(_(e_invarg));
1138 return FAIL;
1139 }
1140
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 redir_varname = vim_strsave(name);
1143 if (redir_varname == NULL)
1144 return FAIL;
1145
1146 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1147 if (redir_lval == NULL)
1148 {
1149 var_redir_stop();
1150 return FAIL;
1151 }
1152
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153 /* The output is stored in growarray "redir_ga" until redirection ends. */
1154 ga_init2(&redir_ga, (int)sizeof(char), 500);
1155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001157 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001158 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1160 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001161 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 if (redir_endp != NULL && *redir_endp != NUL)
1163 /* Trailing characters are present after the variable name */
1164 EMSG(_(e_trailing));
1165 else
1166 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 var_redir_stop();
1169 return FAIL;
1170 }
1171
1172 /* check if we can write to the variable: set it to or append an empty
1173 * string */
1174 save_emsg = did_emsg;
1175 did_emsg = FALSE;
1176 tv.v_type = VAR_STRING;
1177 tv.vval.v_string = (char_u *)"";
1178 if (append)
1179 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1180 else
1181 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001182 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001184 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 if (err)
1186 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001187 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188 var_redir_stop();
1189 return FAIL;
1190 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191
1192 return OK;
1193}
1194
1195/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 * Append "value[value_len]" to the variable set by var_redir_start().
1197 * The actual appending is postponed until redirection ends, because the value
1198 * appended may in fact be the string we write to, changing it may cause freed
1199 * memory to be used:
1200 * :redir => foo
1201 * :let foo
1202 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 */
1204 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001205var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001207 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208
1209 if (redir_lval == NULL)
1210 return;
1211
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001212 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001213 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001215 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001217 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001218 {
1219 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001220 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001221 }
1222 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001223 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001224}
1225
1226/*
1227 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001228 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229 */
1230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001231var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001232{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001233 typval_T tv;
1234
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001235 if (redir_lval != NULL)
1236 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001237 /* If there was no error: assign the text to the variable. */
1238 if (redir_endp != NULL)
1239 {
1240 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1241 tv.v_type = VAR_STRING;
1242 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001243 /* Call get_lval() again, if it's inside a Dict or List it may
1244 * have changed. */
1245 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001246 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001247 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1248 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1249 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001250 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001251
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001252 /* free the collected output */
1253 vim_free(redir_ga.ga_data);
1254 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001255
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001256 vim_free(redir_lval);
1257 redir_lval = NULL;
1258 }
1259 vim_free(redir_varname);
1260 redir_varname = NULL;
1261}
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263# if defined(FEAT_MBYTE) || defined(PROTO)
1264 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001265eval_charconvert(
1266 char_u *enc_from,
1267 char_u *enc_to,
1268 char_u *fname_from,
1269 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270{
1271 int err = FALSE;
1272
1273 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1274 set_vim_var_string(VV_CC_TO, enc_to, -1);
1275 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1276 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1277 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1278 err = TRUE;
1279 set_vim_var_string(VV_CC_FROM, NULL, -1);
1280 set_vim_var_string(VV_CC_TO, NULL, -1);
1281 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1282 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1283
1284 if (err)
1285 return FAIL;
1286 return OK;
1287}
1288# endif
1289
1290# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1291 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 int err = FALSE;
1295
1296 set_vim_var_string(VV_FNAME_IN, fname, -1);
1297 set_vim_var_string(VV_CMDARG, args, -1);
1298 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1299 err = TRUE;
1300 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1301 set_vim_var_string(VV_CMDARG, NULL, -1);
1302
1303 if (err)
1304 {
1305 mch_remove(fname);
1306 return FAIL;
1307 }
1308 return OK;
1309}
1310# endif
1311
1312# if defined(FEAT_DIFF) || defined(PROTO)
1313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001314eval_diff(
1315 char_u *origfile,
1316 char_u *newfile,
1317 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318{
1319 int err = FALSE;
1320
1321 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1322 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1323 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1324 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1325 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1326 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1327 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1328}
1329
1330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001331eval_patch(
1332 char_u *origfile,
1333 char_u *difffile,
1334 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 int err;
1337
1338 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1339 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1340 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1341 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1342 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1343 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1344 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1345}
1346# endif
1347
1348/*
1349 * Top level evaluation function, returning a boolean.
1350 * Sets "error" to TRUE if there was an error.
1351 * Return TRUE or FALSE.
1352 */
1353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001354eval_to_bool(
1355 char_u *arg,
1356 int *error,
1357 char_u **nextcmd,
1358 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
Bram Moolenaar33570922005-01-25 22:26:29 +00001360 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 int retval = FALSE;
1362
1363 if (skip)
1364 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001365 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 else
1368 {
1369 *error = FALSE;
1370 if (!skip)
1371 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001372 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375 }
1376 if (skip)
1377 --emsg_skip;
1378
1379 return retval;
1380}
1381
1382/*
1383 * Top level evaluation function, returning a string. If "skip" is TRUE,
1384 * only parsing to "nextcmd" is done, without reporting errors. Return
1385 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1386 */
1387 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001388eval_to_string_skip(
1389 char_u *arg,
1390 char_u **nextcmd,
1391 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
Bram Moolenaar33570922005-01-25 22:26:29 +00001393 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 char_u *retval;
1395
1396 if (skip)
1397 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001398 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 retval = NULL;
1400 else
1401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 retval = vim_strsave(get_tv_string(&tv));
1403 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405 if (skip)
1406 --emsg_skip;
1407
1408 return retval;
1409}
1410
1411/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001412 * Skip over an expression at "*pp".
1413 * Return FAIL for an error, OK otherwise.
1414 */
1415 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001416skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001419
1420 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001421 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001422}
1423
1424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001426 * When "convert" is TRUE convert a List into a sequence of lines and convert
1427 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 * Return pointer to allocated memory, or NULL for failure.
1429 */
1430 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001431eval_to_string(
1432 char_u *arg,
1433 char_u **nextcmd,
1434 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
Bram Moolenaar33570922005-01-25 22:26:29 +00001436 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001438 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001439#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001440 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001443 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 retval = NULL;
1445 else
1446 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001447 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001448 {
1449 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001450 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001451 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001452 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001453 if (tv.vval.v_list->lv_len > 0)
1454 ga_append(&ga, NL);
1455 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001456 ga_append(&ga, NUL);
1457 retval = (char_u *)ga.ga_data;
1458 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001459#ifdef FEAT_FLOAT
1460 else if (convert && tv.v_type == VAR_FLOAT)
1461 {
1462 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1463 retval = vim_strsave(numbuf);
1464 }
1465#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001466 else
1467 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001468 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470
1471 return retval;
1472}
1473
1474/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001475 * Call eval_to_string() without using current local variables and using
1476 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 */
1478 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001479eval_to_string_safe(
1480 char_u *arg,
1481 char_u **nextcmd,
1482 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 char_u *retval;
1485 void *save_funccalp;
1486
1487 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001488 if (use_sandbox)
1489 ++sandbox;
1490 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001491 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001492 if (use_sandbox)
1493 --sandbox;
1494 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 restore_funccal(save_funccalp);
1496 return retval;
1497}
1498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499/*
1500 * Top level evaluation function, returning a number.
1501 * Evaluates "expr" silently.
1502 * Returns -1 for an error.
1503 */
1504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001505eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
Bram Moolenaar33570922005-01-25 22:26:29 +00001507 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001509 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
1511 ++emsg_off;
1512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001513 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 retval = -1;
1515 else
1516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001517 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
1520 --emsg_off;
1521
1522 return retval;
1523}
1524
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525/*
1526 * Prepare v: variable "idx" to be used.
1527 * Save the current typeval in "save_tv".
1528 * When not used yet add the variable to the v: hashtable.
1529 */
1530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001531prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001532{
1533 *save_tv = vimvars[idx].vv_tv;
1534 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1535 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1536}
1537
1538/*
1539 * Restore v: variable "idx" to typeval "save_tv".
1540 * When no longer defined, remove the variable from the v: hashtable.
1541 */
1542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001544{
1545 hashitem_T *hi;
1546
Bram Moolenaara40058a2005-07-11 22:42:07 +00001547 vimvars[idx].vv_tv = *save_tv;
1548 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1549 {
1550 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1551 if (HASHITEM_EMPTY(hi))
1552 EMSG2(_(e_intern2), "restore_vimvar()");
1553 else
1554 hash_remove(&vimvarht, hi);
1555 }
1556}
1557
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001558#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001559/*
1560 * Evaluate an expression to a list with suggestions.
1561 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001562 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001563 */
1564 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001565eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001566{
1567 typval_T save_val;
1568 typval_T rettv;
1569 list_T *list = NULL;
1570 char_u *p = skipwhite(expr);
1571
1572 /* Set "v:val" to the bad word. */
1573 prepare_vimvar(VV_VAL, &save_val);
1574 vimvars[VV_VAL].vv_type = VAR_STRING;
1575 vimvars[VV_VAL].vv_str = badword;
1576 if (p_verbose == 0)
1577 ++emsg_off;
1578
1579 if (eval1(&p, &rettv, TRUE) == OK)
1580 {
1581 if (rettv.v_type != VAR_LIST)
1582 clear_tv(&rettv);
1583 else
1584 list = rettv.vval.v_list;
1585 }
1586
1587 if (p_verbose == 0)
1588 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001589 restore_vimvar(VV_VAL, &save_val);
1590
1591 return list;
1592}
1593
1594/*
1595 * "list" is supposed to contain two items: a word and a number. Return the
1596 * word in "pp" and the number as the return value.
1597 * Return -1 if anything isn't right.
1598 * Used to get the good word and score from the eval_spell_expr() result.
1599 */
1600 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001602{
1603 listitem_T *li;
1604
1605 li = list->lv_first;
1606 if (li == NULL)
1607 return -1;
1608 *pp = get_tv_string(&li->li_tv);
1609
1610 li = li->li_next;
1611 if (li == NULL)
1612 return -1;
1613 return get_tv_number(&li->li_tv);
1614}
1615#endif
1616
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001617/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001618 * Top level evaluation function.
1619 * Returns an allocated typval_T with the result.
1620 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001621 */
1622 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001624{
1625 typval_T *tv;
1626
1627 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001628 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001629 {
1630 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001631 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001632 }
1633
1634 return tv;
1635}
1636
1637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001639 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 * Uses argv[argc] for the function arguments. Only Number and String
1641 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001645call_vim_function(
1646 char_u *func,
1647 int argc,
1648 char_u **argv,
1649 int safe, /* use the sandbox */
1650 int str_arg_only, /* all arguments are strings */
1651 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
Bram Moolenaar33570922005-01-25 22:26:29 +00001653 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 long n;
1655 int len;
1656 int i;
1657 int doesrange;
1658 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001661 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665 for (i = 0; i < argc; i++)
1666 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001667 /* Pass a NULL or empty argument as an empty string */
1668 if (argv[i] == NULL || *argv[i] == NUL)
1669 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001670 argvars[i].v_type = VAR_STRING;
1671 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001672 continue;
1673 }
1674
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001675 if (str_arg_only)
1676 len = 0;
1677 else
1678 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001679 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (len != 0 && len == (int)STRLEN(argv[i]))
1681 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001682 argvars[i].v_type = VAR_NUMBER;
1683 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 }
1685 else
1686 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001687 argvars[i].v_type = VAR_STRING;
1688 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
1690 }
1691
1692 if (safe)
1693 {
1694 save_funccalp = save_funccal();
1695 ++sandbox;
1696 }
1697
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1699 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001701 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 if (safe)
1703 {
1704 --sandbox;
1705 restore_funccal(save_funccalp);
1706 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 vim_free(argvars);
1708
1709 if (ret == FAIL)
1710 clear_tv(rettv);
1711
1712 return ret;
1713}
1714
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001715/*
1716 * Call vimL function "func" and return the result as a number.
1717 * Returns -1 when calling the function fails.
1718 * Uses argv[argc] for the function arguments.
1719 */
1720 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001721call_func_retnr(
1722 char_u *func,
1723 int argc,
1724 char_u **argv,
1725 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001726{
1727 typval_T rettv;
1728 long retval;
1729
1730 /* All arguments are passed as strings, no conversion to number. */
1731 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1732 return -1;
1733
1734 retval = get_tv_number_chk(&rettv, NULL);
1735 clear_tv(&rettv);
1736 return retval;
1737}
1738
1739#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1740 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1741
Bram Moolenaar4f688582007-07-24 12:34:30 +00001742# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001744 * Call vimL function "func" and return the result as a string.
1745 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001746 * Uses argv[argc] for the function arguments.
1747 */
1748 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001749call_func_retstr(
1750 char_u *func,
1751 int argc,
1752 char_u **argv,
1753 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754{
1755 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001756 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001758 /* All arguments are passed as strings, no conversion to number. */
1759 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760 return NULL;
1761
1762 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 return retval;
1765}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001766# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001768/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001769 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001771 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772 */
1773 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774call_func_retlist(
1775 char_u *func,
1776 int argc,
1777 char_u **argv,
1778 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001779{
1780 typval_T rettv;
1781
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001782 /* All arguments are passed as strings, no conversion to number. */
1783 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001784 return NULL;
1785
1786 if (rettv.v_type != VAR_LIST)
1787 {
1788 clear_tv(&rettv);
1789 return NULL;
1790 }
1791
1792 return rettv.vval.v_list;
1793}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#endif
1795
1796/*
1797 * Save the current function call pointer, and set it to NULL.
1798 * Used when executing autocommands and for ":source".
1799 */
1800 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001801save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001803 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 current_funccal = NULL;
1806 return (void *)fc;
1807}
1808
1809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001810restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001812 funccall_T *fc = (funccall_T *)vfc;
1813
1814 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815}
1816
Bram Moolenaar05159a02005-02-26 23:04:13 +00001817#if defined(FEAT_PROFILE) || defined(PROTO)
1818/*
1819 * Prepare profiling for entering a child or something else that is not
1820 * counted for the script/function itself.
1821 * Should always be called in pair with prof_child_exit().
1822 */
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824prof_child_enter(
1825 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001826{
1827 funccall_T *fc = current_funccal;
1828
1829 if (fc != NULL && fc->func->uf_profiling)
1830 profile_start(&fc->prof_child);
1831 script_prof_save(tm);
1832}
1833
1834/*
1835 * Take care of time spent in a child.
1836 * Should always be called after prof_child_enter().
1837 */
1838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001839prof_child_exit(
1840 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001841{
1842 funccall_T *fc = current_funccal;
1843
1844 if (fc != NULL && fc->func->uf_profiling)
1845 {
1846 profile_end(&fc->prof_child);
1847 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1848 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1849 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1850 }
1851 script_prof_restore(tm);
1852}
1853#endif
1854
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856#ifdef FEAT_FOLDING
1857/*
1858 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1859 * it in "*cp". Doesn't give error messages.
1860 */
1861 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001862eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
Bram Moolenaar33570922005-01-25 22:26:29 +00001864 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 int retval;
1866 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001867 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1868 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
1870 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001871 if (use_sandbox)
1872 ++sandbox;
1873 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 retval = 0;
1877 else
1878 {
1879 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 if (tv.v_type == VAR_NUMBER)
1881 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001882 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 retval = 0;
1884 else
1885 {
1886 /* If the result is a string, check if there is a non-digit before
1887 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (!VIM_ISDIGIT(*s) && *s != '-')
1890 *cp = *s++;
1891 retval = atol((char *)s);
1892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
1895 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001896 if (use_sandbox)
1897 --sandbox;
1898 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 return retval;
1901}
1902#endif
1903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 * ":let" list all variable values
1906 * ":let var1 var2" list variable values
1907 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 * ":let var += expr" assignment command.
1909 * ":let var -= expr" assignment command.
1910 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 */
1913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001914ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915{
1916 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 int var_count = 0;
1921 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001923 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
Bram Moolenaardb552d602006-03-23 22:59:57 +00001926 argend = skip_var_list(arg, &var_count, &semicolon);
1927 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001929 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1930 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001931 expr = skipwhite(argend);
1932 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1933 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001935 /*
1936 * ":let" without "=": list variables
1937 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 if (*arg == '[')
1939 EMSG(_(e_invarg));
1940 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001942 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001946 list_glob_vars(&first);
1947 list_buf_vars(&first);
1948 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001949#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001950 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001951#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001952 list_script_vars(&first);
1953 list_func_vars(&first);
1954 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 eap->nextcmd = check_nextcmd(arg);
1957 }
1958 else
1959 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 op[0] = '=';
1961 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001962 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001964 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1965 op[0] = *expr; /* +=, -= or .= */
1966 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001968 else
1969 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 if (eap->skip)
1972 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (eap->skip)
1975 {
1976 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001977 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 --emsg_skip;
1979 }
1980 else if (i != FAIL)
1981 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 }
1987}
1988
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989/*
1990 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1991 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 * When "nextchars" is not NULL it points to a string with characters that
1993 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1994 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 * Returns OK or FAIL;
1996 */
1997 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001998ex_let_vars(
1999 char_u *arg_start,
2000 typval_T *tv,
2001 int copy, /* copy values from "tv", don't move */
2002 int semicolon, /* from skip_var_list() */
2003 int var_count, /* from skip_var_list() */
2004 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005{
2006 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002007 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002009 listitem_T *item;
2010 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011
2012 if (*arg != '[')
2013 {
2014 /*
2015 * ":let var = expr" or ":for var in list"
2016 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002017 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 return FAIL;
2019 return OK;
2020 }
2021
2022 /*
2023 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2024 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002025 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 {
2027 EMSG(_(e_listreq));
2028 return FAIL;
2029 }
2030
2031 i = list_len(l);
2032 if (semicolon == 0 && var_count < i)
2033 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002034 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035 return FAIL;
2036 }
2037 if (var_count - semicolon > i)
2038 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002039 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 return FAIL;
2041 }
2042
2043 item = l->lv_first;
2044 while (*arg != ']')
2045 {
2046 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 item = item->li_next;
2049 if (arg == NULL)
2050 return FAIL;
2051
2052 arg = skipwhite(arg);
2053 if (*arg == ';')
2054 {
2055 /* Put the rest of the list (may be empty) in the var after ';'.
2056 * Create a new list for this. */
2057 l = list_alloc();
2058 if (l == NULL)
2059 return FAIL;
2060 while (item != NULL)
2061 {
2062 list_append_tv(l, &item->li_tv);
2063 item = item->li_next;
2064 }
2065
2066 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002067 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002068 ltv.vval.v_list = l;
2069 l->lv_refcount = 1;
2070
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002071 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2072 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073 clear_tv(&ltv);
2074 if (arg == NULL)
2075 return FAIL;
2076 break;
2077 }
2078 else if (*arg != ',' && *arg != ']')
2079 {
2080 EMSG2(_(e_intern2), "ex_let_vars()");
2081 return FAIL;
2082 }
2083 }
2084
2085 return OK;
2086}
2087
2088/*
2089 * Skip over assignable variable "var" or list of variables "[var, var]".
2090 * Used for ":let varvar = expr" and ":for varvar in expr".
2091 * For "[var, var]" increment "*var_count" for each variable.
2092 * for "[var, var; var]" set "semicolon".
2093 * Return NULL for an error.
2094 */
2095 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002096skip_var_list(
2097 char_u *arg,
2098 int *var_count,
2099 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002100{
2101 char_u *p, *s;
2102
2103 if (*arg == '[')
2104 {
2105 /* "[var, var]": find the matching ']'. */
2106 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002107 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002108 {
2109 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2110 s = skip_var_one(p);
2111 if (s == p)
2112 {
2113 EMSG2(_(e_invarg2), p);
2114 return NULL;
2115 }
2116 ++*var_count;
2117
2118 p = skipwhite(s);
2119 if (*p == ']')
2120 break;
2121 else if (*p == ';')
2122 {
2123 if (*semicolon == 1)
2124 {
2125 EMSG(_("Double ; in list of variables"));
2126 return NULL;
2127 }
2128 *semicolon = 1;
2129 }
2130 else if (*p != ',')
2131 {
2132 EMSG2(_(e_invarg2), p);
2133 return NULL;
2134 }
2135 }
2136 return p + 1;
2137 }
2138 else
2139 return skip_var_one(arg);
2140}
2141
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002143 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002144 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002147skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002148{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002149 if (*arg == '@' && arg[1] != NUL)
2150 return arg + 2;
2151 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2152 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002153}
2154
Bram Moolenaara7043832005-01-21 11:56:39 +00002155/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002156 * List variables for hashtab "ht" with prefix "prefix".
2157 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002158 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002160list_hashtable_vars(
2161 hashtab_T *ht,
2162 char_u *prefix,
2163 int empty,
2164 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002165{
Bram Moolenaar33570922005-01-25 22:26:29 +00002166 hashitem_T *hi;
2167 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002168 int todo;
2169
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002170 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002171 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2172 {
2173 if (!HASHITEM_EMPTY(hi))
2174 {
2175 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002176 di = HI2DI(hi);
2177 if (empty || di->di_tv.v_type != VAR_STRING
2178 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002180 }
2181 }
2182}
2183
2184/*
2185 * List global variables.
2186 */
2187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002189{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002191}
2192
2193/*
2194 * List buffer variables.
2195 */
2196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002197list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002198{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 char_u numbuf[NUMBUFLEN];
2200
Bram Moolenaar429fa852013-04-15 12:27:36 +02002201 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203
2204 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2206 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002207}
2208
2209/*
2210 * List window variables.
2211 */
2212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002213list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002214{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002215 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002217}
2218
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219#ifdef FEAT_WINDOWS
2220/*
2221 * List tab page variables.
2222 */
2223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002224list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002225{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002226 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002227 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002228}
2229#endif
2230
Bram Moolenaara7043832005-01-21 11:56:39 +00002231/*
2232 * List Vim variables.
2233 */
2234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002235list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002237 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238}
2239
2240/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002241 * List script-local variables, if there is a script.
2242 */
2243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002244list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002245{
2246 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002247 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2248 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002249}
2250
2251/*
2252 * List function variables, if there is a function.
2253 */
2254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002255list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002256{
2257 if (current_funccal != NULL)
2258 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002259 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002260}
2261
2262/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 * List variables in "arg".
2264 */
2265 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002266list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267{
2268 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 char_u *name_start;
2272 char_u *arg_subsc;
2273 char_u *tofree;
2274 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275
2276 while (!ends_excmd(*arg) && !got_int)
2277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002280 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2282 {
2283 emsg_severe = TRUE;
2284 EMSG(_(e_trailing));
2285 break;
2286 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290 /* get_name_len() takes care of expanding curly braces */
2291 name_start = name = arg;
2292 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2293 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 /* This is mainly to keep test 49 working: when expanding
2296 * curly braces fails overrule the exception error message. */
2297 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 emsg_severe = TRUE;
2300 EMSG2(_(e_invarg2), arg);
2301 break;
2302 }
2303 error = TRUE;
2304 }
2305 else
2306 {
2307 if (tofree != NULL)
2308 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002309 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 else
2312 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 /* handle d.key, l[idx], f(expr) */
2314 arg_subsc = arg;
2315 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002316 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002322 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002323 case 'g': list_glob_vars(first); break;
2324 case 'b': list_buf_vars(first); break;
2325 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002326#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002327 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002328#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002329 case 'v': list_vim_vars(first); break;
2330 case 's': list_script_vars(first); break;
2331 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332 default:
2333 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002335 }
2336 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337 {
2338 char_u numbuf[NUMBUFLEN];
2339 char_u *tf;
2340 int c;
2341 char_u *s;
2342
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002343 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002344 c = *arg;
2345 *arg = NUL;
2346 list_one_var_a((char_u *)"",
2347 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002348 tv.v_type,
2349 s == NULL ? (char_u *)"" : s,
2350 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002351 *arg = c;
2352 vim_free(tf);
2353 }
2354 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002355 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 }
2357 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002358
2359 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002361
2362 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 }
2364
2365 return arg;
2366}
2367
2368/*
2369 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2370 * Returns a pointer to the char just after the var name.
2371 * Returns NULL if there is an error.
2372 */
2373 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002374ex_let_one(
2375 char_u *arg, /* points to variable name */
2376 typval_T *tv, /* value to assign to variable */
2377 int copy, /* copy value from "tv" */
2378 char_u *endchars, /* valid chars after variable name or NULL */
2379 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380{
2381 int c1;
2382 char_u *name;
2383 char_u *p;
2384 char_u *arg_end = NULL;
2385 int len;
2386 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388
2389 /*
2390 * ":let $VAR = expr": Set environment variable.
2391 */
2392 if (*arg == '$')
2393 {
2394 /* Find the end of the name. */
2395 ++arg;
2396 name = arg;
2397 len = get_env_len(&arg);
2398 if (len == 0)
2399 EMSG2(_(e_invarg2), name - 1);
2400 else
2401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 if (op != NULL && (*op == '+' || *op == '-'))
2403 EMSG2(_(e_letwrong), op);
2404 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002405 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002407 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 {
2409 c1 = name[len];
2410 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002411 p = get_tv_string_chk(tv);
2412 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 {
2414 int mustfree = FALSE;
2415 char_u *s = vim_getenv(name, &mustfree);
2416
2417 if (s != NULL)
2418 {
2419 p = tofree = concat_str(s, p);
2420 if (mustfree)
2421 vim_free(s);
2422 }
2423 }
2424 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002427 if (STRICMP(name, "HOME") == 0)
2428 init_homedir();
2429 else if (didset_vim && STRICMP(name, "VIM") == 0)
2430 didset_vim = FALSE;
2431 else if (didset_vimruntime
2432 && STRICMP(name, "VIMRUNTIME") == 0)
2433 didset_vimruntime = FALSE;
2434 arg_end = arg;
2435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 }
2439 }
2440 }
2441
2442 /*
2443 * ":let &option = expr": Set option value.
2444 * ":let &l:option = expr": Set local option value.
2445 * ":let &g:option = expr": Set global option value.
2446 */
2447 else if (*arg == '&')
2448 {
2449 /* Find the end of the name. */
2450 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 if (p == NULL || (endchars != NULL
2452 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 EMSG(_(e_letunexp));
2454 else
2455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 long n;
2457 int opt_type;
2458 long numval;
2459 char_u *stringval = NULL;
2460 char_u *s;
2461
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 c1 = *p;
2463 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464
2465 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 s = get_tv_string_chk(tv); /* != NULL if number or string */
2467 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 {
2469 opt_type = get_option_value(arg, &numval,
2470 &stringval, opt_flags);
2471 if ((opt_type == 1 && *op == '.')
2472 || (opt_type == 0 && *op != '.'))
2473 EMSG2(_(e_letwrong), op);
2474 else
2475 {
2476 if (opt_type == 1) /* number */
2477 {
2478 if (*op == '+')
2479 n = numval + n;
2480 else
2481 n = numval - n;
2482 }
2483 else if (opt_type == 0 && stringval != NULL) /* string */
2484 {
2485 s = concat_str(stringval, s);
2486 vim_free(stringval);
2487 stringval = s;
2488 }
2489 }
2490 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002491 if (s != NULL)
2492 {
2493 set_option_value(arg, n, s, opt_flags);
2494 arg_end = p;
2495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
2499 }
2500
2501 /*
2502 * ":let @r = expr": Set register contents.
2503 */
2504 else if (*arg == '@')
2505 {
2506 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 if (op != NULL && (*op == '+' || *op == '-'))
2508 EMSG2(_(e_letwrong), op);
2509 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002510 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 EMSG(_(e_letunexp));
2512 else
2513 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002514 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 char_u *s;
2516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002517 p = get_tv_string_chk(tv);
2518 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002520 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002521 if (s != NULL)
2522 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002523 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002524 vim_free(s);
2525 }
2526 }
2527 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002528 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530 arg_end = arg + 1;
2531 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002532 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 }
2534 }
2535
2536 /*
2537 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002540 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002542 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002544 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2548 EMSG(_(e_letunexp));
2549 else
2550 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002551 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 arg_end = p;
2553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 }
2557
2558 else
2559 EMSG2(_(e_invarg2), arg);
2560
2561 return arg_end;
2562}
2563
2564/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2566 */
2567 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002568check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569{
2570 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2571 {
2572 EMSG2(_(e_readonlyvar), arg);
2573 return TRUE;
2574 }
2575 return FALSE;
2576}
2577
2578/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 * Get an lval: variable, Dict item or List item that can be assigned a value
2580 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2581 * "name.key", "name.key[expr]" etc.
2582 * Indexing only works if "name" is an existing List or Dictionary.
2583 * "name" points to the start of the name.
2584 * If "rettv" is not NULL it points to the value to be assigned.
2585 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2586 * wrong; must end in space or cmd separator.
2587 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002588 * flags:
2589 * GLV_QUIET: do not give error messages
2590 * GLV_NO_AUTOLOAD: do not use script autoloading
2591 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002593 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 */
2596 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002597get_lval(
2598 char_u *name,
2599 typval_T *rettv,
2600 lval_T *lp,
2601 int unlet,
2602 int skip,
2603 int flags, /* GLV_ values */
2604 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 char_u *expr_start, *expr_end;
2608 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002609 dictitem_T *v;
2610 typval_T var1;
2611 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002613 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002616 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621
2622 if (skip)
2623 {
2624 /* When skipping just find the end of the name. */
2625 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002626 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 }
2628
2629 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002630 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (expr_start != NULL)
2632 {
2633 /* Don't expand the name when we already know there is an error. */
2634 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2635 && *p != '[' && *p != '.')
2636 {
2637 EMSG(_(e_trailing));
2638 return NULL;
2639 }
2640
2641 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2642 if (lp->ll_exp_name == NULL)
2643 {
2644 /* Report an invalid expression in braces, unless the
2645 * expression evaluation has been cancelled due to an
2646 * aborting error, an interrupt, or an exception. */
2647 if (!aborting() && !quiet)
2648 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002649 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 EMSG2(_(e_invarg2), name);
2651 return NULL;
2652 }
2653 }
2654 lp->ll_name = lp->ll_exp_name;
2655 }
2656 else
2657 lp->ll_name = name;
2658
2659 /* Without [idx] or .key we are done. */
2660 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2661 return p;
2662
2663 cc = *p;
2664 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002665 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (v == NULL && !quiet)
2667 EMSG2(_(e_undefvar), lp->ll_name);
2668 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 if (v == NULL)
2670 return NULL;
2671
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 /*
2673 * Loop until no more [idx] or .key is following.
2674 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002675 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2679 && !(lp->ll_tv->v_type == VAR_DICT
2680 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_("E689: Can only index a List or Dictionary"));
2684 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 if (!quiet)
2689 EMSG(_("E708: [:] must come last"));
2690 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 len = -1;
2694 if (*p == '.')
2695 {
2696 key = p + 1;
2697 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2698 ;
2699 if (len == 0)
2700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_(e_emptykey));
2703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705 p = key + len;
2706 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 else
2708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (*p == ':')
2712 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 else
2714 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 empty1 = FALSE;
2716 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002718 if (get_tv_string_chk(&var1) == NULL)
2719 {
2720 /* not a number or string */
2721 clear_tv(&var1);
2722 return NULL;
2723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
2725
2726 /* Optionally get the second index [ :expr]. */
2727 if (*p == ':')
2728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002732 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 if (!empty1)
2734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 if (rettv != NULL && (rettv->v_type != VAR_LIST
2738 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (!quiet)
2741 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 if (!empty1)
2743 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746 p = skipwhite(p + 1);
2747 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 else
2750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (!empty1)
2755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002758 if (get_tv_string_chk(&var2) == NULL)
2759 {
2760 /* not a number or string */
2761 if (!empty1)
2762 clear_tv(&var1);
2763 clear_tv(&var2);
2764 return NULL;
2765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002768 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002771
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002773 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 if (!quiet)
2775 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (!empty1)
2777 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 }
2782
2783 /* Skip to past ']'. */
2784 ++p;
2785 }
2786
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 {
2789 if (len == -1)
2790 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002792 key = get_tv_string_chk(&var1); /* is number or string */
2793 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 }
2798 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002799 lp->ll_list = NULL;
2800 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002801 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002802
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002803 /* When assigning to a scope dictionary check that a function and
2804 * variable name is valid (only variable name unless it is l: or
2805 * g: dictionary). Disallow overwriting a builtin function. */
2806 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002807 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002808 int prevval;
2809 int wrong;
2810
2811 if (len != -1)
2812 {
2813 prevval = key[len];
2814 key[len] = NUL;
2815 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002816 else
2817 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002818 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2819 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002821 || !valid_varname(key);
2822 if (len != -1)
2823 key[len] = prevval;
2824 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002825 return NULL;
2826 }
2827
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002830 /* Can't add "v:" variable. */
2831 if (lp->ll_dict == &vimvardict)
2832 {
2833 EMSG2(_(e_illvar), name);
2834 return NULL;
2835 }
2836
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002837 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002841 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 if (len == -1)
2843 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 }
2846 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 if (len == -1)
2851 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 p = NULL;
2854 break;
2855 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002856 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002857 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002858 return NULL;
2859
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 if (len == -1)
2861 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 }
2864 else
2865 {
2866 /*
2867 * Get the number and item for the only or first index of the List.
2868 */
2869 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 else
2872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002873 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 clear_tv(&var1);
2875 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_list = lp->ll_tv->vval.v_list;
2878 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2879 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002881 if (lp->ll_n1 < 0)
2882 {
2883 lp->ll_n1 = 0;
2884 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2885 }
2886 }
2887 if (lp->ll_li == NULL)
2888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002891 if (!quiet)
2892 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 }
2895
2896 /*
2897 * May need to find the item or absolute index for the second
2898 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 * When no index given: "lp->ll_empty2" is TRUE.
2900 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002904 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002910 {
2911 if (!quiet)
2912 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002914 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 }
2917
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2919 if (lp->ll_n1 < 0)
2920 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2921 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002922 {
2923 if (!quiet)
2924 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002926 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002927 }
2928
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002931 }
2932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 return p;
2934}
2935
2936/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 */
2939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002940clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941{
2942 vim_free(lp->ll_exp_name);
2943 vim_free(lp->ll_newkey);
2944}
2945
2946/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002947 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 */
2951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002952set_var_lval(
2953 lval_T *lp,
2954 char_u *endp,
2955 typval_T *rettv,
2956 int copy,
2957 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958{
2959 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002960 listitem_T *ri;
2961 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962
2963 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002966 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 cc = *endp;
2968 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002969 if (op != NULL && *op != '=')
2970 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002971 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002972
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002973 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002974 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002975 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002976 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002978 if ((di == NULL
2979 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2980 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2981 FALSE)))
2982 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 set_var(lp->ll_name, &tv, FALSE);
2984 clear_tv(&tv);
2985 }
2986 }
2987 else
2988 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002990 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002992 else if (tv_check_lock(lp->ll_newkey == NULL
2993 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002994 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002995 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 else if (lp->ll_range)
2997 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002998 listitem_T *ll_li = lp->ll_li;
2999 int ll_n1 = lp->ll_n1;
3000
3001 /*
3002 * Check whether any of the list items is locked
3003 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003004 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003005 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003006 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003007 return;
3008 ri = ri->li_next;
3009 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3010 break;
3011 ll_li = ll_li->li_next;
3012 ++ll_n1;
3013 }
3014
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 /*
3016 * Assign the List values to the list items.
3017 */
3018 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003019 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 if (op != NULL && *op != '=')
3021 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3022 else
3023 {
3024 clear_tv(&lp->ll_li->li_tv);
3025 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3026 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 ri = ri->li_next;
3028 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3029 break;
3030 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003031 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003033 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003034 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 ri = NULL;
3036 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003037 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003038 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 lp->ll_li = lp->ll_li->li_next;
3040 ++lp->ll_n1;
3041 }
3042 if (ri != NULL)
3043 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 else if (lp->ll_empty2
3045 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 : lp->ll_n1 != lp->ll_n2)
3047 EMSG(_("E711: List value has not enough items"));
3048 }
3049 else
3050 {
3051 /*
3052 * Assign to a List or Dictionary item.
3053 */
3054 if (lp->ll_newkey != NULL)
3055 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003056 if (op != NULL && *op != '=')
3057 {
3058 EMSG2(_(e_letwrong), op);
3059 return;
3060 }
3061
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003063 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 if (di == NULL)
3065 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003066 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3067 {
3068 vim_free(di);
3069 return;
3070 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003071 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 else if (op != NULL && *op != '=')
3074 {
3075 tv_op(lp->ll_tv, rettv, op);
3076 return;
3077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003079 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003080
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003081 /*
3082 * Assign the value to the variable or list item.
3083 */
3084 if (copy)
3085 copy_tv(rettv, lp->ll_tv);
3086 else
3087 {
3088 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003089 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003090 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 }
3092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003093}
3094
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003095/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3097 * Returns OK or FAIL.
3098 */
3099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003100tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003101{
3102 long n;
3103 char_u numbuf[NUMBUFLEN];
3104 char_u *s;
3105
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003106 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3107 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3108 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 {
3110 switch (tv1->v_type)
3111 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003112 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 case VAR_DICT:
3114 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003115 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003116 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003117 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003118 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 break;
3120
3121 case VAR_LIST:
3122 if (*op != '+' || tv2->v_type != VAR_LIST)
3123 break;
3124 /* List += List */
3125 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3126 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3127 return OK;
3128
3129 case VAR_NUMBER:
3130 case VAR_STRING:
3131 if (tv2->v_type == VAR_LIST)
3132 break;
3133 if (*op == '+' || *op == '-')
3134 {
3135 /* nr += nr or nr -= nr*/
3136 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003137#ifdef FEAT_FLOAT
3138 if (tv2->v_type == VAR_FLOAT)
3139 {
3140 float_T f = n;
3141
3142 if (*op == '+')
3143 f += tv2->vval.v_float;
3144 else
3145 f -= tv2->vval.v_float;
3146 clear_tv(tv1);
3147 tv1->v_type = VAR_FLOAT;
3148 tv1->vval.v_float = f;
3149 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003150 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151#endif
3152 {
3153 if (*op == '+')
3154 n += get_tv_number(tv2);
3155 else
3156 n -= get_tv_number(tv2);
3157 clear_tv(tv1);
3158 tv1->v_type = VAR_NUMBER;
3159 tv1->vval.v_number = n;
3160 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003161 }
3162 else
3163 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164 if (tv2->v_type == VAR_FLOAT)
3165 break;
3166
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003167 /* str .= str */
3168 s = get_tv_string(tv1);
3169 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3170 clear_tv(tv1);
3171 tv1->v_type = VAR_STRING;
3172 tv1->vval.v_string = s;
3173 }
3174 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003176 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003177#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178 {
3179 float_T f;
3180
3181 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3182 && tv2->v_type != VAR_NUMBER
3183 && tv2->v_type != VAR_STRING))
3184 break;
3185 if (tv2->v_type == VAR_FLOAT)
3186 f = tv2->vval.v_float;
3187 else
3188 f = get_tv_number(tv2);
3189 if (*op == '+')
3190 tv1->vval.v_float += f;
3191 else
3192 tv1->vval.v_float -= f;
3193 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003195 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003196 }
3197 }
3198
3199 EMSG2(_(e_letwrong), op);
3200 return FAIL;
3201}
3202
3203/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 * Add a watcher to a list.
3205 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003207list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208{
3209 lw->lw_next = l->lv_watch;
3210 l->lv_watch = lw;
3211}
3212
3213/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003214 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 * No warning when it isn't found...
3216 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221
3222 lwp = &l->lv_watch;
3223 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3224 {
3225 if (lw == lwrem)
3226 {
3227 *lwp = lw->lw_next;
3228 break;
3229 }
3230 lwp = &lw->lw_next;
3231 }
3232}
3233
3234/*
3235 * Just before removing an item from a list: advance watchers to the next
3236 * item.
3237 */
3238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003239list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240{
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242
3243 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3244 if (lw->lw_item == item)
3245 lw->lw_item = item->li_next;
3246}
3247
3248/*
3249 * Evaluate the expression used in a ":for var in expr" command.
3250 * "arg" points to "var".
3251 * Set "*errp" to TRUE for an error, FALSE otherwise;
3252 * Return a pointer that holds the info. Null when there is an error.
3253 */
3254 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003255eval_for_line(
3256 char_u *arg,
3257 int *errp,
3258 char_u **nextcmdp,
3259 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260{
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003263 typval_T tv;
3264 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265
3266 *errp = TRUE; /* default: there is an error */
3267
Bram Moolenaar33570922005-01-25 22:26:29 +00003268 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 if (fi == NULL)
3270 return NULL;
3271
3272 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3273 if (expr == NULL)
3274 return fi;
3275
3276 expr = skipwhite(expr);
3277 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3278 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003279 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 return fi;
3281 }
3282
3283 if (skip)
3284 ++emsg_skip;
3285 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3286 {
3287 *errp = FALSE;
3288 if (!skip)
3289 {
3290 l = tv.vval.v_list;
3291 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003292 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003294 clear_tv(&tv);
3295 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 else
3297 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003298 /* No need to increment the refcount, it's already set for the
3299 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 fi->fi_list = l;
3301 list_add_watch(l, &fi->fi_lw);
3302 fi->fi_lw.lw_item = l->lv_first;
3303 }
3304 }
3305 }
3306 if (skip)
3307 --emsg_skip;
3308
3309 return fi;
3310}
3311
3312/*
3313 * Use the first item in a ":for" list. Advance to the next.
3314 * Assign the values to the variable (list). "arg" points to the first one.
3315 * Return TRUE when a valid item was found, FALSE when at end of list or
3316 * something wrong.
3317 */
3318 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003319next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003321 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324
3325 item = fi->fi_lw.lw_item;
3326 if (item == NULL)
3327 result = FALSE;
3328 else
3329 {
3330 fi->fi_lw.lw_item = item->li_next;
3331 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3332 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3333 }
3334 return result;
3335}
3336
3337/*
3338 * Free the structure used to store info used by ":for".
3339 */
3340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003341free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342{
Bram Moolenaar33570922005-01-25 22:26:29 +00003343 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003345 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003346 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003348 list_unref(fi->fi_list);
3349 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 vim_free(fi);
3351}
3352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3354
3355 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356set_context_for_expression(
3357 expand_T *xp,
3358 char_u *arg,
3359 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 int got_eq = FALSE;
3362 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 if (cmdidx == CMD_let)
3366 {
3367 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003368 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003369 {
3370 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003371 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003372 {
3373 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003374 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375 if (vim_iswhite(*p))
3376 break;
3377 }
3378 return;
3379 }
3380 }
3381 else
3382 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3383 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 while ((xp->xp_pattern = vim_strpbrk(arg,
3385 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3386 {
3387 c = *xp->xp_pattern;
3388 if (c == '&')
3389 {
3390 c = xp->xp_pattern[1];
3391 if (c == '&')
3392 {
3393 ++xp->xp_pattern;
3394 xp->xp_context = cmdidx != CMD_let || got_eq
3395 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3396 }
3397 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003400 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3401 xp->xp_pattern += 2;
3402
3403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
3405 else if (c == '$')
3406 {
3407 /* environment variable */
3408 xp->xp_context = EXPAND_ENV_VARS;
3409 }
3410 else if (c == '=')
3411 {
3412 got_eq = TRUE;
3413 xp->xp_context = EXPAND_EXPRESSION;
3414 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003415 else if (c == '#'
3416 && xp->xp_context == EXPAND_EXPRESSION)
3417 {
3418 /* Autoload function/variable contains '#'. */
3419 break;
3420 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003421 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 && xp->xp_context == EXPAND_FUNCTIONS
3423 && vim_strchr(xp->xp_pattern, '(') == NULL)
3424 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003425 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 break;
3427 }
3428 else if (cmdidx != CMD_let || got_eq)
3429 {
3430 if (c == '"') /* string */
3431 {
3432 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3433 if (c == '\\' && xp->xp_pattern[1] != NUL)
3434 ++xp->xp_pattern;
3435 xp->xp_context = EXPAND_NOTHING;
3436 }
3437 else if (c == '\'') /* literal string */
3438 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003439 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3441 /* skip */ ;
3442 xp->xp_context = EXPAND_NOTHING;
3443 }
3444 else if (c == '|')
3445 {
3446 if (xp->xp_pattern[1] == '|')
3447 {
3448 ++xp->xp_pattern;
3449 xp->xp_context = EXPAND_EXPRESSION;
3450 }
3451 else
3452 xp->xp_context = EXPAND_COMMANDS;
3453 }
3454 else
3455 xp->xp_context = EXPAND_EXPRESSION;
3456 }
3457 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003458 /* Doesn't look like something valid, expand as an expression
3459 * anyway. */
3460 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 arg = xp->xp_pattern;
3462 if (*arg != NUL)
3463 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3464 /* skip */ ;
3465 }
3466 xp->xp_pattern = arg;
3467}
3468
3469#endif /* FEAT_CMDL_COMPL */
3470
3471/*
3472 * ":1,25call func(arg1, arg2)" function call.
3473 */
3474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003475ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 char_u *arg = eap->arg;
3478 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003480 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003482 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 linenr_T lnum;
3484 int doesrange;
3485 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003486 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003487 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003489 if (eap->skip)
3490 {
3491 /* trans_function_name() doesn't work well when skipping, use eval0()
3492 * instead to skip to any following command, e.g. for:
3493 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003494 ++emsg_skip;
3495 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3496 clear_tv(&rettv);
3497 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003498 return;
3499 }
3500
Bram Moolenaar65639032016-03-16 21:40:30 +01003501 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003502 if (fudi.fd_newkey != NULL)
3503 {
3504 /* Still need to give an error message for missing key. */
3505 EMSG2(_(e_dictkey), fudi.fd_newkey);
3506 vim_free(fudi.fd_newkey);
3507 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003508 if (tofree == NULL)
3509 return;
3510
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003511 /* Increase refcount on dictionary, it could get deleted when evaluating
3512 * the arguments. */
3513 if (fudi.fd_dict != NULL)
3514 ++fudi.fd_dict->dv_refcount;
3515
Bram Moolenaar65639032016-03-16 21:40:30 +01003516 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3517 * contents. For VAR_PARTIAL get its partial, unless we already have one
3518 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003519 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003520 name = deref_func_name(tofree, &len,
3521 partial != NULL ? NULL : &partial, FALSE);
3522
Bram Moolenaar532c7802005-01-27 14:44:31 +00003523 /* Skip white space to allow ":call func ()". Not good, but required for
3524 * backward compatibility. */
3525 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003526 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528 if (*startarg != '(')
3529 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003530 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 goto end;
3532 }
3533
3534 /*
3535 * When skipping, evaluate the function once, to find the end of the
3536 * arguments.
3537 * When the function takes a range, this is discovered after the first
3538 * call, and the loop is broken.
3539 */
3540 if (eap->skip)
3541 {
3542 ++emsg_skip;
3543 lnum = eap->line2; /* do it once, also with an invalid range */
3544 }
3545 else
3546 lnum = eap->line1;
3547 for ( ; lnum <= eap->line2; ++lnum)
3548 {
3549 if (!eap->skip && eap->addr_count > 0)
3550 {
3551 curwin->w_cursor.lnum = lnum;
3552 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003553#ifdef FEAT_VIRTUALEDIT
3554 curwin->w_cursor.coladd = 0;
3555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003558 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003559 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003560 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
3562 failed = TRUE;
3563 break;
3564 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003565
3566 /* Handle a function returning a Funcref, Dictionary or List. */
3567 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3568 {
3569 failed = TRUE;
3570 break;
3571 }
3572
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003573 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (doesrange || eap->skip)
3575 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003578 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003579 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003580 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (aborting())
3582 break;
3583 }
3584 if (eap->skip)
3585 --emsg_skip;
3586
3587 if (!failed)
3588 {
3589 /* Check for trailing illegal characters and a following command. */
3590 if (!ends_excmd(*arg))
3591 {
3592 emsg_severe = TRUE;
3593 EMSG(_(e_trailing));
3594 }
3595 else
3596 eap->nextcmd = check_nextcmd(arg);
3597 }
3598
3599end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003600 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003601 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602}
3603
3604/*
3605 * ":unlet[!] var1 ... " command.
3606 */
3607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003608ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610 ex_unletlock(eap, eap->arg, 0);
3611}
3612
3613/*
3614 * ":lockvar" and ":unlockvar" commands
3615 */
3616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003618{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003620 int deep = 2;
3621
3622 if (eap->forceit)
3623 deep = -1;
3624 else if (vim_isdigit(*arg))
3625 {
3626 deep = getdigits(&arg);
3627 arg = skipwhite(arg);
3628 }
3629
3630 ex_unletlock(eap, arg, deep);
3631}
3632
3633/*
3634 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3635 */
3636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003637ex_unletlock(
3638 exarg_T *eap,
3639 char_u *argstart,
3640 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641{
3642 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 do
3648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003650 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003651 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (lv.ll_name == NULL)
3653 error = TRUE; /* error but continue parsing */
3654 if (name_end == NULL || (!vim_iswhite(*name_end)
3655 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 if (name_end != NULL)
3658 {
3659 emsg_severe = TRUE;
3660 EMSG(_(e_trailing));
3661 }
3662 if (!(eap->skip || error))
3663 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 break;
3665 }
3666
3667 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 {
3669 if (eap->cmdidx == CMD_unlet)
3670 {
3671 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3672 error = TRUE;
3673 }
3674 else
3675 {
3676 if (do_lock_var(&lv, name_end, deep,
3677 eap->cmdidx == CMD_lockvar) == FAIL)
3678 error = TRUE;
3679 }
3680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 if (!eap->skip)
3683 clear_lval(&lv);
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 arg = skipwhite(name_end);
3686 } while (!ends_excmd(*arg));
3687
3688 eap->nextcmd = check_nextcmd(arg);
3689}
3690
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003692do_unlet_var(
3693 lval_T *lp,
3694 char_u *name_end,
3695 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003696{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 int ret = OK;
3698 int cc;
3699
3700 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003702 cc = *name_end;
3703 *name_end = NUL;
3704
3705 /* Normal name or expanded name. */
3706 if (check_changedtick(lp->ll_name))
3707 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003708 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003711 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003712 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003713 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003714 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003715 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003716 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 else if (lp->ll_range)
3718 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003719 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003720 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003721 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003722
3723 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3724 {
3725 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003726 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003727 return FAIL;
3728 ll_li = li;
3729 ++ll_n1;
3730 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003731
3732 /* Delete a range of List items. */
3733 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3734 {
3735 li = lp->ll_li->li_next;
3736 listitem_remove(lp->ll_list, lp->ll_li);
3737 lp->ll_li = li;
3738 ++lp->ll_n1;
3739 }
3740 }
3741 else
3742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003743 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003744 /* unlet a List item. */
3745 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003746 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003747 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003748 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003749 }
3750
3751 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003752}
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754/*
3755 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 */
3758 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003759do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
Bram Moolenaar33570922005-01-25 22:26:29 +00003761 hashtab_T *ht;
3762 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003763 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003764 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003765 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 ht = find_var_ht(name, &varname);
3768 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003770 if (ht == &globvarht)
3771 d = &globvardict;
3772 else if (current_funccal != NULL
3773 && ht == &current_funccal->l_vars.dv_hashtab)
3774 d = &current_funccal->l_vars;
3775 else if (ht == &compat_hashtab)
3776 d = &vimvardict;
3777 else
3778 {
3779 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3780 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3781 }
3782 if (d == NULL)
3783 {
3784 EMSG2(_(e_intern2), "do_unlet()");
3785 return FAIL;
3786 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003787 hi = hash_find(ht, varname);
3788 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003789 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003790 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003791 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003792 || var_check_ro(di->di_flags, name, FALSE)
3793 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003794 return FAIL;
3795
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003796 delete_var(ht, hi);
3797 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 if (forceit)
3801 return OK;
3802 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return FAIL;
3804}
3805
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003806/*
3807 * Lock or unlock variable indicated by "lp".
3808 * "deep" is the levels to go (-1 for unlimited);
3809 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3810 */
3811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003812do_lock_var(
3813 lval_T *lp,
3814 char_u *name_end,
3815 int deep,
3816 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003817{
3818 int ret = OK;
3819 int cc;
3820 dictitem_T *di;
3821
3822 if (deep == 0) /* nothing to do */
3823 return OK;
3824
3825 if (lp->ll_tv == NULL)
3826 {
3827 cc = *name_end;
3828 *name_end = NUL;
3829
3830 /* Normal name or expanded name. */
3831 if (check_changedtick(lp->ll_name))
3832 ret = FAIL;
3833 else
3834 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003835 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836 if (di == NULL)
3837 ret = FAIL;
3838 else
3839 {
3840 if (lock)
3841 di->di_flags |= DI_FLAGS_LOCK;
3842 else
3843 di->di_flags &= ~DI_FLAGS_LOCK;
3844 item_lock(&di->di_tv, deep, lock);
3845 }
3846 }
3847 *name_end = cc;
3848 }
3849 else if (lp->ll_range)
3850 {
3851 listitem_T *li = lp->ll_li;
3852
3853 /* (un)lock a range of List items. */
3854 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3855 {
3856 item_lock(&li->li_tv, deep, lock);
3857 li = li->li_next;
3858 ++lp->ll_n1;
3859 }
3860 }
3861 else if (lp->ll_list != NULL)
3862 /* (un)lock a List item. */
3863 item_lock(&lp->ll_li->li_tv, deep, lock);
3864 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003865 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003866 item_lock(&lp->ll_di->di_tv, deep, lock);
3867
3868 return ret;
3869}
3870
3871/*
3872 * Lock or unlock an item. "deep" is nr of levels to go.
3873 */
3874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003875item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003876{
3877 static int recurse = 0;
3878 list_T *l;
3879 listitem_T *li;
3880 dict_T *d;
3881 hashitem_T *hi;
3882 int todo;
3883
3884 if (recurse >= DICT_MAXNEST)
3885 {
3886 EMSG(_("E743: variable nested too deep for (un)lock"));
3887 return;
3888 }
3889 if (deep == 0)
3890 return;
3891 ++recurse;
3892
3893 /* lock/unlock the item itself */
3894 if (lock)
3895 tv->v_lock |= VAR_LOCKED;
3896 else
3897 tv->v_lock &= ~VAR_LOCKED;
3898
3899 switch (tv->v_type)
3900 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003901 case VAR_UNKNOWN:
3902 case VAR_NUMBER:
3903 case VAR_STRING:
3904 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003905 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003906 case VAR_FLOAT:
3907 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003908 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003909 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003910 break;
3911
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003912 case VAR_LIST:
3913 if ((l = tv->vval.v_list) != NULL)
3914 {
3915 if (lock)
3916 l->lv_lock |= VAR_LOCKED;
3917 else
3918 l->lv_lock &= ~VAR_LOCKED;
3919 if (deep < 0 || deep > 1)
3920 /* recursive: lock/unlock the items the List contains */
3921 for (li = l->lv_first; li != NULL; li = li->li_next)
3922 item_lock(&li->li_tv, deep - 1, lock);
3923 }
3924 break;
3925 case VAR_DICT:
3926 if ((d = tv->vval.v_dict) != NULL)
3927 {
3928 if (lock)
3929 d->dv_lock |= VAR_LOCKED;
3930 else
3931 d->dv_lock &= ~VAR_LOCKED;
3932 if (deep < 0 || deep > 1)
3933 {
3934 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003935 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003936 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3937 {
3938 if (!HASHITEM_EMPTY(hi))
3939 {
3940 --todo;
3941 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3942 }
3943 }
3944 }
3945 }
3946 }
3947 --recurse;
3948}
3949
Bram Moolenaara40058a2005-07-11 22:42:07 +00003950/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003951 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3952 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003953 */
3954 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003955tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003956{
3957 return (tv->v_lock & VAR_LOCKED)
3958 || (tv->v_type == VAR_LIST
3959 && tv->vval.v_list != NULL
3960 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3961 || (tv->v_type == VAR_DICT
3962 && tv->vval.v_dict != NULL
3963 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3964}
3965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3967/*
3968 * Delete all "menutrans_" variables.
3969 */
3970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003971del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003974 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
Bram Moolenaar33570922005-01-25 22:26:29 +00003976 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003977 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003979 {
3980 if (!HASHITEM_EMPTY(hi))
3981 {
3982 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003983 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3984 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003985 }
3986 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988}
3989#endif
3990
3991#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3992
3993/*
3994 * Local string buffer for the next two functions to store a variable name
3995 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3996 * get_user_var_name().
3997 */
3998
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003999static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001static char_u *varnamebuf = NULL;
4002static int varnamebuflen = 0;
4003
4004/*
4005 * Function to concatenate a prefix and a variable name.
4006 */
4007 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004008cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
4010 int len;
4011
4012 len = (int)STRLEN(name) + 3;
4013 if (len > varnamebuflen)
4014 {
4015 vim_free(varnamebuf);
4016 len += 10; /* some additional space */
4017 varnamebuf = alloc(len);
4018 if (varnamebuf == NULL)
4019 {
4020 varnamebuflen = 0;
4021 return NULL;
4022 }
4023 varnamebuflen = len;
4024 }
4025 *varnamebuf = prefix;
4026 varnamebuf[1] = ':';
4027 STRCPY(varnamebuf + 2, name);
4028 return varnamebuf;
4029}
4030
4031/*
4032 * Function given to ExpandGeneric() to obtain the list of user defined
4033 * (global/buffer/window/built-in) variable names.
4034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004036get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004038 static long_u gdone;
4039 static long_u bdone;
4040 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004041#ifdef FEAT_WINDOWS
4042 static long_u tdone;
4043#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004044 static int vidx;
4045 static hashitem_T *hi;
4046 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004049 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004050 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004051#ifdef FEAT_WINDOWS
4052 tdone = 0;
4053#endif
4054 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004055
4056 /* Global variables */
4057 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004059 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004061 else
4062 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004063 while (HASHITEM_EMPTY(hi))
4064 ++hi;
4065 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4066 return cat_prefix_varname('g', hi->hi_key);
4067 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004069
4070 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004071 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004074 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004076 else
4077 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004078 while (HASHITEM_EMPTY(hi))
4079 ++hi;
4080 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004084 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 return (char_u *)"b:changedtick";
4086 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004087
4088 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004089 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004092 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004094 else
4095 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 while (HASHITEM_EMPTY(hi))
4097 ++hi;
4098 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004100
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004101#ifdef FEAT_WINDOWS
4102 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004103 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004104 if (tdone < ht->ht_used)
4105 {
4106 if (tdone++ == 0)
4107 hi = ht->ht_array;
4108 else
4109 ++hi;
4110 while (HASHITEM_EMPTY(hi))
4111 ++hi;
4112 return cat_prefix_varname('t', hi->hi_key);
4113 }
4114#endif
4115
Bram Moolenaar33570922005-01-25 22:26:29 +00004116 /* v: variables */
4117 if (vidx < VV_LEN)
4118 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120 vim_free(varnamebuf);
4121 varnamebuf = NULL;
4122 varnamebuflen = 0;
4123 return NULL;
4124}
4125
4126#endif /* FEAT_CMDL_COMPL */
4127
4128/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004129 * Return TRUE if "pat" matches "text".
4130 * Does not use 'cpo' and always uses 'magic'.
4131 */
4132 static int
4133pattern_match(char_u *pat, char_u *text, int ic)
4134{
4135 int matches = FALSE;
4136 char_u *save_cpo;
4137 regmatch_T regmatch;
4138
4139 /* avoid 'l' flag in 'cpoptions' */
4140 save_cpo = p_cpo;
4141 p_cpo = (char_u *)"";
4142 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4143 if (regmatch.regprog != NULL)
4144 {
4145 regmatch.rm_ic = ic;
4146 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4147 vim_regfree(regmatch.regprog);
4148 }
4149 p_cpo = save_cpo;
4150 return matches;
4151}
4152
4153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 * types for expressions.
4155 */
4156typedef enum
4157{
4158 TYPE_UNKNOWN = 0
4159 , TYPE_EQUAL /* == */
4160 , TYPE_NEQUAL /* != */
4161 , TYPE_GREATER /* > */
4162 , TYPE_GEQUAL /* >= */
4163 , TYPE_SMALLER /* < */
4164 , TYPE_SEQUAL /* <= */
4165 , TYPE_MATCH /* =~ */
4166 , TYPE_NOMATCH /* !~ */
4167} exptype_T;
4168
4169/*
4170 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4173 */
4174
4175/*
4176 * Handle zero level expression.
4177 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004179 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 * Return OK or FAIL.
4181 */
4182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004183eval0(
4184 char_u *arg,
4185 typval_T *rettv,
4186 char_u **nextcmd,
4187 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
4189 int ret;
4190 char_u *p;
4191
4192 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 if (ret == FAIL || !ends_excmd(*p))
4195 {
4196 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 /*
4199 * Report the invalid expression unless the expression evaluation has
4200 * been cancelled due to an aborting error, an interrupt, or an
4201 * exception.
4202 */
4203 if (!aborting())
4204 EMSG2(_(e_invexpr2), arg);
4205 ret = FAIL;
4206 }
4207 if (nextcmd != NULL)
4208 *nextcmd = check_nextcmd(p);
4209
4210 return ret;
4211}
4212
4213/*
4214 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004215 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 *
4217 * "arg" must point to the first non-white of the expression.
4218 * "arg" is advanced to the next non-white after the recognized expression.
4219 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004220 * Note: "rettv.v_lock" is not set.
4221 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 * Return OK or FAIL.
4223 */
4224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004225eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
4227 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004228 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230 /*
4231 * Get the first variable.
4232 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 return FAIL;
4235
4236 if ((*arg)[0] == '?')
4237 {
4238 result = FALSE;
4239 if (evaluate)
4240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 int error = FALSE;
4242
4243 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249
4250 /*
4251 * Get the second variable.
4252 */
4253 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return FAIL;
4256
4257 /*
4258 * Check for the ":".
4259 */
4260 if ((*arg)[0] != ':')
4261 {
4262 EMSG(_("E109: Missing ':' after '?'"));
4263 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 return FAIL;
4266 }
4267
4268 /*
4269 * Get the third variable.
4270 */
4271 *arg = skipwhite(*arg + 1);
4272 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4273 {
4274 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 return FAIL;
4277 }
4278 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281
4282 return OK;
4283}
4284
4285/*
4286 * Handle first level expression:
4287 * expr2 || expr2 || expr2 logical OR
4288 *
4289 * "arg" must point to the first non-white of the expression.
4290 * "arg" is advanced to the next non-white after the recognized expression.
4291 *
4292 * Return OK or FAIL.
4293 */
4294 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004295eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296{
Bram Moolenaar33570922005-01-25 22:26:29 +00004297 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 long result;
4299 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
4302 /*
4303 * Get the first variable.
4304 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 return FAIL;
4307
4308 /*
4309 * Repeat until there is no following "||".
4310 */
4311 first = TRUE;
4312 result = FALSE;
4313 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4314 {
4315 if (evaluate && first)
4316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 if (error)
4321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 first = FALSE;
4323 }
4324
4325 /*
4326 * Get the second variable.
4327 */
4328 *arg = skipwhite(*arg + 2);
4329 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4330 return FAIL;
4331
4332 /*
4333 * Compute the result.
4334 */
4335 if (evaluate && !result)
4336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (error)
4341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343 if (evaluate)
4344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 rettv->v_type = VAR_NUMBER;
4346 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 }
4349
4350 return OK;
4351}
4352
4353/*
4354 * Handle second level expression:
4355 * expr3 && expr3 && expr3 logical AND
4356 *
4357 * "arg" must point to the first non-white of the expression.
4358 * "arg" is advanced to the next non-white after the recognized expression.
4359 *
4360 * Return OK or FAIL.
4361 */
4362 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004363eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
Bram Moolenaar33570922005-01-25 22:26:29 +00004365 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 long result;
4367 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004368 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
4370 /*
4371 * Get the first variable.
4372 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004373 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 return FAIL;
4375
4376 /*
4377 * Repeat until there is no following "&&".
4378 */
4379 first = TRUE;
4380 result = TRUE;
4381 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4382 {
4383 if (evaluate && first)
4384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004385 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004388 if (error)
4389 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 first = FALSE;
4391 }
4392
4393 /*
4394 * Get the second variable.
4395 */
4396 *arg = skipwhite(*arg + 2);
4397 if (eval4(arg, &var2, evaluate && result) == FAIL)
4398 return FAIL;
4399
4400 /*
4401 * Compute the result.
4402 */
4403 if (evaluate && result)
4404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004405 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004408 if (error)
4409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 if (evaluate)
4412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004413 rettv->v_type = VAR_NUMBER;
4414 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 }
4417
4418 return OK;
4419}
4420
4421/*
4422 * Handle third level expression:
4423 * var1 == var2
4424 * var1 =~ var2
4425 * var1 != var2
4426 * var1 !~ var2
4427 * var1 > var2
4428 * var1 >= var2
4429 * var1 < var2
4430 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004431 * var1 is var2
4432 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 *
4434 * "arg" must point to the first non-white of the expression.
4435 * "arg" is advanced to the next non-white after the recognized expression.
4436 *
4437 * Return OK or FAIL.
4438 */
4439 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004440eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
Bram Moolenaar33570922005-01-25 22:26:29 +00004442 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 char_u *p;
4444 int i;
4445 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004446 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 int len = 2;
4448 long n1, n2;
4449 char_u *s1, *s2;
4450 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
4453 /*
4454 * Get the first variable.
4455 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004456 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 return FAIL;
4458
4459 p = *arg;
4460 switch (p[0])
4461 {
4462 case '=': if (p[1] == '=')
4463 type = TYPE_EQUAL;
4464 else if (p[1] == '~')
4465 type = TYPE_MATCH;
4466 break;
4467 case '!': if (p[1] == '=')
4468 type = TYPE_NEQUAL;
4469 else if (p[1] == '~')
4470 type = TYPE_NOMATCH;
4471 break;
4472 case '>': if (p[1] != '=')
4473 {
4474 type = TYPE_GREATER;
4475 len = 1;
4476 }
4477 else
4478 type = TYPE_GEQUAL;
4479 break;
4480 case '<': if (p[1] != '=')
4481 {
4482 type = TYPE_SMALLER;
4483 len = 1;
4484 }
4485 else
4486 type = TYPE_SEQUAL;
4487 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 case 'i': if (p[1] == 's')
4489 {
4490 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4491 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004492 i = p[len];
4493 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004494 {
4495 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4496 type_is = TRUE;
4497 }
4498 }
4499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501
4502 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004503 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 */
4505 if (type != TYPE_UNKNOWN)
4506 {
4507 /* extra question mark appended: ignore case */
4508 if (p[len] == '?')
4509 {
4510 ic = TRUE;
4511 ++len;
4512 }
4513 /* extra '#' appended: match case */
4514 else if (p[len] == '#')
4515 {
4516 ic = FALSE;
4517 ++len;
4518 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004519 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 else
4521 ic = p_ic;
4522
4523 /*
4524 * Get the second variable.
4525 */
4526 *arg = skipwhite(p + len);
4527 if (eval5(arg, &var2, evaluate) == FAIL)
4528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004529 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 return FAIL;
4531 }
4532
4533 if (evaluate)
4534 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004535 if (type_is && rettv->v_type != var2.v_type)
4536 {
4537 /* For "is" a different type always means FALSE, for "notis"
4538 * it means TRUE. */
4539 n1 = (type == TYPE_NEQUAL);
4540 }
4541 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4542 {
4543 if (type_is)
4544 {
4545 n1 = (rettv->v_type == var2.v_type
4546 && rettv->vval.v_list == var2.vval.v_list);
4547 if (type == TYPE_NEQUAL)
4548 n1 = !n1;
4549 }
4550 else if (rettv->v_type != var2.v_type
4551 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4552 {
4553 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004554 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004556 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004557 clear_tv(rettv);
4558 clear_tv(&var2);
4559 return FAIL;
4560 }
4561 else
4562 {
4563 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004564 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4565 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004566 if (type == TYPE_NEQUAL)
4567 n1 = !n1;
4568 }
4569 }
4570
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004571 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4572 {
4573 if (type_is)
4574 {
4575 n1 = (rettv->v_type == var2.v_type
4576 && rettv->vval.v_dict == var2.vval.v_dict);
4577 if (type == TYPE_NEQUAL)
4578 n1 = !n1;
4579 }
4580 else if (rettv->v_type != var2.v_type
4581 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4582 {
4583 if (rettv->v_type != var2.v_type)
4584 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4585 else
4586 EMSG(_("E736: Invalid operation for Dictionary"));
4587 clear_tv(rettv);
4588 clear_tv(&var2);
4589 return FAIL;
4590 }
4591 else
4592 {
4593 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004594 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4595 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004596 if (type == TYPE_NEQUAL)
4597 n1 = !n1;
4598 }
4599 }
4600
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004601 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4602 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004603 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004604 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004605 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004606 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004607 clear_tv(rettv);
4608 clear_tv(&var2);
4609 return FAIL;
4610 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004611 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004612 if (type == TYPE_NEQUAL)
4613 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004614 }
4615
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004616#ifdef FEAT_FLOAT
4617 /*
4618 * If one of the two variables is a float, compare as a float.
4619 * When using "=~" or "!~", always compare as string.
4620 */
4621 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4622 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4623 {
4624 float_T f1, f2;
4625
4626 if (rettv->v_type == VAR_FLOAT)
4627 f1 = rettv->vval.v_float;
4628 else
4629 f1 = get_tv_number(rettv);
4630 if (var2.v_type == VAR_FLOAT)
4631 f2 = var2.vval.v_float;
4632 else
4633 f2 = get_tv_number(&var2);
4634 n1 = FALSE;
4635 switch (type)
4636 {
4637 case TYPE_EQUAL: n1 = (f1 == f2); break;
4638 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4639 case TYPE_GREATER: n1 = (f1 > f2); break;
4640 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4641 case TYPE_SMALLER: n1 = (f1 < f2); break;
4642 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4643 case TYPE_UNKNOWN:
4644 case TYPE_MATCH:
4645 case TYPE_NOMATCH: break; /* avoid gcc warning */
4646 }
4647 }
4648#endif
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 /*
4651 * If one of the two variables is a number, compare as a number.
4652 * When using "=~" or "!~", always compare as string.
4653 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004654 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 n1 = get_tv_number(rettv);
4658 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 switch (type)
4660 {
4661 case TYPE_EQUAL: n1 = (n1 == n2); break;
4662 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4663 case TYPE_GREATER: n1 = (n1 > n2); break;
4664 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4665 case TYPE_SMALLER: n1 = (n1 < n2); break;
4666 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4667 case TYPE_UNKNOWN:
4668 case TYPE_MATCH:
4669 case TYPE_NOMATCH: break; /* avoid gcc warning */
4670 }
4671 }
4672 else
4673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 s1 = get_tv_string_buf(rettv, buf1);
4675 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4677 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4678 else
4679 i = 0;
4680 n1 = FALSE;
4681 switch (type)
4682 {
4683 case TYPE_EQUAL: n1 = (i == 0); break;
4684 case TYPE_NEQUAL: n1 = (i != 0); break;
4685 case TYPE_GREATER: n1 = (i > 0); break;
4686 case TYPE_GEQUAL: n1 = (i >= 0); break;
4687 case TYPE_SMALLER: n1 = (i < 0); break;
4688 case TYPE_SEQUAL: n1 = (i <= 0); break;
4689
4690 case TYPE_MATCH:
4691 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004692 n1 = pattern_match(s2, s1, ic);
4693 if (type == TYPE_NOMATCH)
4694 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 break;
4696
4697 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4698 }
4699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 clear_tv(rettv);
4701 clear_tv(&var2);
4702 rettv->v_type = VAR_NUMBER;
4703 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 }
4706
4707 return OK;
4708}
4709
4710/*
4711 * Handle fourth level expression:
4712 * + number addition
4713 * - number subtraction
4714 * . string concatenation
4715 *
4716 * "arg" must point to the first non-white of the expression.
4717 * "arg" is advanced to the next non-white after the recognized expression.
4718 *
4719 * Return OK or FAIL.
4720 */
4721 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004722eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
Bram Moolenaar33570922005-01-25 22:26:29 +00004724 typval_T var2;
4725 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 int op;
4727 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728#ifdef FEAT_FLOAT
4729 float_T f1 = 0, f2 = 0;
4730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 char_u *s1, *s2;
4732 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4733 char_u *p;
4734
4735 /*
4736 * Get the first variable.
4737 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004738 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 return FAIL;
4740
4741 /*
4742 * Repeat computing, until no '+', '-' or '.' is following.
4743 */
4744 for (;;)
4745 {
4746 op = **arg;
4747 if (op != '+' && op != '-' && op != '.')
4748 break;
4749
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004750 if ((op != '+' || rettv->v_type != VAR_LIST)
4751#ifdef FEAT_FLOAT
4752 && (op == '.' || rettv->v_type != VAR_FLOAT)
4753#endif
4754 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 {
4756 /* For "list + ...", an illegal use of the first operand as
4757 * a number cannot be determined before evaluating the 2nd
4758 * operand: if this is also a list, all is ok.
4759 * For "something . ...", "something - ..." or "non-list + ...",
4760 * we know that the first operand needs to be a string or number
4761 * without evaluating the 2nd operand. So check before to avoid
4762 * side effects after an error. */
4763 if (evaluate && get_tv_string_chk(rettv) == NULL)
4764 {
4765 clear_tv(rettv);
4766 return FAIL;
4767 }
4768 }
4769
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 /*
4771 * Get the second variable.
4772 */
4773 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004774 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004776 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 return FAIL;
4778 }
4779
4780 if (evaluate)
4781 {
4782 /*
4783 * Compute the result.
4784 */
4785 if (op == '.')
4786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4788 s2 = get_tv_string_buf_chk(&var2, buf2);
4789 if (s2 == NULL) /* type error ? */
4790 {
4791 clear_tv(rettv);
4792 clear_tv(&var2);
4793 return FAIL;
4794 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004795 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004796 clear_tv(rettv);
4797 rettv->v_type = VAR_STRING;
4798 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004800 else if (op == '+' && rettv->v_type == VAR_LIST
4801 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004802 {
4803 /* concatenate Lists */
4804 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4805 &var3) == FAIL)
4806 {
4807 clear_tv(rettv);
4808 clear_tv(&var2);
4809 return FAIL;
4810 }
4811 clear_tv(rettv);
4812 *rettv = var3;
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 else
4815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 int error = FALSE;
4817
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004818#ifdef FEAT_FLOAT
4819 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004821 f1 = rettv->vval.v_float;
4822 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004823 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824 else
4825#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827 n1 = get_tv_number_chk(rettv, &error);
4828 if (error)
4829 {
4830 /* This can only happen for "list + non-list". For
4831 * "non-list + ..." or "something - ...", we returned
4832 * before evaluating the 2nd operand. */
4833 clear_tv(rettv);
4834 return FAIL;
4835 }
4836#ifdef FEAT_FLOAT
4837 if (var2.v_type == VAR_FLOAT)
4838 f1 = n1;
4839#endif
4840 }
4841#ifdef FEAT_FLOAT
4842 if (var2.v_type == VAR_FLOAT)
4843 {
4844 f2 = var2.vval.v_float;
4845 n2 = 0;
4846 }
4847 else
4848#endif
4849 {
4850 n2 = get_tv_number_chk(&var2, &error);
4851 if (error)
4852 {
4853 clear_tv(rettv);
4854 clear_tv(&var2);
4855 return FAIL;
4856 }
4857#ifdef FEAT_FLOAT
4858 if (rettv->v_type == VAR_FLOAT)
4859 f2 = n2;
4860#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863
4864#ifdef FEAT_FLOAT
4865 /* If there is a float on either side the result is a float. */
4866 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4867 {
4868 if (op == '+')
4869 f1 = f1 + f2;
4870 else
4871 f1 = f1 - f2;
4872 rettv->v_type = VAR_FLOAT;
4873 rettv->vval.v_float = f1;
4874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876#endif
4877 {
4878 if (op == '+')
4879 n1 = n1 + n2;
4880 else
4881 n1 = n1 - n2;
4882 rettv->v_type = VAR_NUMBER;
4883 rettv->vval.v_number = n1;
4884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 }
4889 return OK;
4890}
4891
4892/*
4893 * Handle fifth level expression:
4894 * * number multiplication
4895 * / number division
4896 * % number modulo
4897 *
4898 * "arg" must point to the first non-white of the expression.
4899 * "arg" is advanced to the next non-white after the recognized expression.
4900 *
4901 * Return OK or FAIL.
4902 */
4903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004904eval6(
4905 char_u **arg,
4906 typval_T *rettv,
4907 int evaluate,
4908 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
Bram Moolenaar33570922005-01-25 22:26:29 +00004910 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 int op;
4912 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913#ifdef FEAT_FLOAT
4914 int use_float = FALSE;
4915 float_T f1 = 0, f2;
4916#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004917 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919 /*
4920 * Get the first variable.
4921 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004922 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
4924
4925 /*
4926 * Repeat computing, until no '*', '/' or '%' is following.
4927 */
4928 for (;;)
4929 {
4930 op = **arg;
4931 if (op != '*' && op != '/' && op != '%')
4932 break;
4933
4934 if (evaluate)
4935 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004936#ifdef FEAT_FLOAT
4937 if (rettv->v_type == VAR_FLOAT)
4938 {
4939 f1 = rettv->vval.v_float;
4940 use_float = TRUE;
4941 n1 = 0;
4942 }
4943 else
4944#endif
4945 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004946 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004947 if (error)
4948 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950 else
4951 n1 = 0;
4952
4953 /*
4954 * Get the second variable.
4955 */
4956 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004957 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 return FAIL;
4959
4960 if (evaluate)
4961 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962#ifdef FEAT_FLOAT
4963 if (var2.v_type == VAR_FLOAT)
4964 {
4965 if (!use_float)
4966 {
4967 f1 = n1;
4968 use_float = TRUE;
4969 }
4970 f2 = var2.vval.v_float;
4971 n2 = 0;
4972 }
4973 else
4974#endif
4975 {
4976 n2 = get_tv_number_chk(&var2, &error);
4977 clear_tv(&var2);
4978 if (error)
4979 return FAIL;
4980#ifdef FEAT_FLOAT
4981 if (use_float)
4982 f2 = n2;
4983#endif
4984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
4986 /*
4987 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990#ifdef FEAT_FLOAT
4991 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 if (op == '*')
4994 f1 = f1 * f2;
4995 else if (op == '/')
4996 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004997# ifdef VMS
4998 /* VMS crashes on divide by zero, work around it */
4999 if (f2 == 0.0)
5000 {
5001 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005002 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005003 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005004 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005005 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005006 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005007 }
5008 else
5009 f1 = f1 / f2;
5010# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 /* We rely on the floating point library to handle divide
5012 * by zero to result in "inf" and not a crash. */
5013 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005014# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005018 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019 return FAIL;
5020 }
5021 rettv->v_type = VAR_FLOAT;
5022 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005027 if (op == '*')
5028 n1 = n1 * n2;
5029 else if (op == '/')
5030 {
5031 if (n2 == 0) /* give an error message? */
5032 {
5033 if (n1 == 0)
5034 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5035 else if (n1 < 0)
5036 n1 = -0x7fffffffL;
5037 else
5038 n1 = 0x7fffffffL;
5039 }
5040 else
5041 n1 = n1 / n2;
5042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 {
5045 if (n2 == 0) /* give an error message? */
5046 n1 = 0;
5047 else
5048 n1 = n1 % n2;
5049 }
5050 rettv->v_type = VAR_NUMBER;
5051 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054 }
5055
5056 return OK;
5057}
5058
5059/*
5060 * Handle sixth level expression:
5061 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005062 * "string" string constant
5063 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 * &option-name option value
5065 * @r register contents
5066 * identifier variable value
5067 * function() function call
5068 * $VAR environment variable
5069 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005070 * [expr, expr] List
5071 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 *
5073 * Also handle:
5074 * ! in front logical NOT
5075 * - in front unary minus
5076 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 * trailing [] subscript in String or List
5078 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 *
5080 * "arg" must point to the first non-white of the expression.
5081 * "arg" is advanced to the next non-white after the recognized expression.
5082 *
5083 * Return OK or FAIL.
5084 */
5085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005086eval7(
5087 char_u **arg,
5088 typval_T *rettv,
5089 int evaluate,
5090 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 long n;
5093 int len;
5094 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 char_u *start_leader, *end_leader;
5096 int ret = OK;
5097 char_u *alias;
5098
5099 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005101 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005103 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
5105 /*
5106 * Skip '!' and '-' characters. They are handled later.
5107 */
5108 start_leader = *arg;
5109 while (**arg == '!' || **arg == '-' || **arg == '+')
5110 *arg = skipwhite(*arg + 1);
5111 end_leader = *arg;
5112
5113 switch (**arg)
5114 {
5115 /*
5116 * Number constant.
5117 */
5118 case '0':
5119 case '1':
5120 case '2':
5121 case '3':
5122 case '4':
5123 case '5':
5124 case '6':
5125 case '7':
5126 case '8':
5127 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005128 {
5129#ifdef FEAT_FLOAT
5130 char_u *p = skipdigits(*arg + 1);
5131 int get_float = FALSE;
5132
5133 /* We accept a float when the format matches
5134 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005135 * strict to avoid backwards compatibility problems.
5136 * Don't look for a float after the "." operator, so that
5137 * ":let vers = 1.2.3" doesn't fail. */
5138 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005140 get_float = TRUE;
5141 p = skipdigits(p + 2);
5142 if (*p == 'e' || *p == 'E')
5143 {
5144 ++p;
5145 if (*p == '-' || *p == '+')
5146 ++p;
5147 if (!vim_isdigit(*p))
5148 get_float = FALSE;
5149 else
5150 p = skipdigits(p + 1);
5151 }
5152 if (ASCII_ISALPHA(*p) || *p == '.')
5153 get_float = FALSE;
5154 }
5155 if (get_float)
5156 {
5157 float_T f;
5158
5159 *arg += string2float(*arg, &f);
5160 if (evaluate)
5161 {
5162 rettv->v_type = VAR_FLOAT;
5163 rettv->vval.v_float = f;
5164 }
5165 }
5166 else
5167#endif
5168 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005169 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005170 *arg += len;
5171 if (evaluate)
5172 {
5173 rettv->v_type = VAR_NUMBER;
5174 rettv->vval.v_number = n;
5175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180 /*
5181 * String constant: "string".
5182 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 break;
5185
5186 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190 break;
5191
5192 /*
5193 * List: [expr, expr]
5194 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 break;
5197
5198 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 * Dictionary: {key: val, key: val}
5200 */
5201 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5202 break;
5203
5204 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005205 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005207 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 break;
5209
5210 /*
5211 * Environment variable: $VAR.
5212 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215
5216 /*
5217 * Register contents: @r.
5218 */
5219 case '@': ++*arg;
5220 if (evaluate)
5221 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005222 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005223 rettv->vval.v_string = get_reg_contents(**arg,
5224 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226 if (**arg != NUL)
5227 ++*arg;
5228 break;
5229
5230 /*
5231 * nested expression: (expression).
5232 */
5233 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005234 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (**arg == ')')
5236 ++*arg;
5237 else if (ret == OK)
5238 {
5239 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005240 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 ret = FAIL;
5242 }
5243 break;
5244
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 break;
5247 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248
5249 if (ret == NOTDONE)
5250 {
5251 /*
5252 * Must be a variable or function name.
5253 * Can also be a curly-braces kind of name: {expr}.
5254 */
5255 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005256 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 if (alias != NULL)
5258 s = alias;
5259
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 ret = FAIL;
5262 else
5263 {
5264 if (**arg == '(') /* recursive! */
5265 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005266 partial_T *partial;
5267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 /* If "s" is the name of a variable of type VAR_FUNC
5269 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005270 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271
5272 /* Invoke the function. */
5273 ret = get_func_tv(s, len, rettv, arg,
5274 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005275 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005276
5277 /* If evaluate is FALSE rettv->v_type was not set in
5278 * get_func_tv, but it's needed in handle_subscript() to parse
5279 * what follows. So set it here. */
5280 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5281 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005282 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005283 rettv->v_type = VAR_FUNC;
5284 }
5285
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 /* Stop the expression evaluation when immediately
5287 * aborting on error, or when an interrupt occurred or
5288 * an exception was thrown but not caught. */
5289 if (aborting())
5290 {
5291 if (ret == OK)
5292 clear_tv(rettv);
5293 ret = FAIL;
5294 }
5295 }
5296 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005297 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005298 else
5299 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005301 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 }
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 *arg = skipwhite(*arg);
5305
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005306 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5307 * expr(expr). */
5308 if (ret == OK)
5309 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310
5311 /*
5312 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5313 */
5314 if (ret == OK && evaluate && end_leader > start_leader)
5315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005316 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005317 int val = 0;
5318#ifdef FEAT_FLOAT
5319 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321 if (rettv->v_type == VAR_FLOAT)
5322 f = rettv->vval.v_float;
5323 else
5324#endif
5325 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 clear_tv(rettv);
5329 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 else
5332 {
5333 while (end_leader > start_leader)
5334 {
5335 --end_leader;
5336 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005337 {
5338#ifdef FEAT_FLOAT
5339 if (rettv->v_type == VAR_FLOAT)
5340 f = !f;
5341 else
5342#endif
5343 val = !val;
5344 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005345 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005346 {
5347#ifdef FEAT_FLOAT
5348 if (rettv->v_type == VAR_FLOAT)
5349 f = -f;
5350 else
5351#endif
5352 val = -val;
5353 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005354 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005355#ifdef FEAT_FLOAT
5356 if (rettv->v_type == VAR_FLOAT)
5357 {
5358 clear_tv(rettv);
5359 rettv->vval.v_float = f;
5360 }
5361 else
5362#endif
5363 {
5364 clear_tv(rettv);
5365 rettv->v_type = VAR_NUMBER;
5366 rettv->vval.v_number = val;
5367 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370
5371 return ret;
5372}
5373
5374/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005375 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5376 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5378 */
5379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005380eval_index(
5381 char_u **arg,
5382 typval_T *rettv,
5383 int evaluate,
5384 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385{
5386 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 long len = -1;
5390 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393
Bram Moolenaara03f2332016-02-06 18:09:59 +01005394 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005396 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005397 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005398 if (verbose)
5399 EMSG(_("E695: Cannot index a Funcref"));
5400 return FAIL;
5401 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005402#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005403 if (verbose)
5404 EMSG(_(e_float_as_string));
5405 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005406#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005407 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005408 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005409 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005410 if (verbose)
5411 EMSG(_("E909: Cannot index a special variable"));
5412 return FAIL;
5413 case VAR_UNKNOWN:
5414 if (evaluate)
5415 return FAIL;
5416 /* FALLTHROUGH */
5417
5418 case VAR_STRING:
5419 case VAR_NUMBER:
5420 case VAR_LIST:
5421 case VAR_DICT:
5422 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005423 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005425 init_tv(&var1);
5426 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429 /*
5430 * dict.name
5431 */
5432 key = *arg + 1;
5433 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5434 ;
5435 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 }
5439 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 /*
5442 * something[idx]
5443 *
5444 * Get the (first) variable from inside the [].
5445 */
5446 *arg = skipwhite(*arg + 1);
5447 if (**arg == ':')
5448 empty1 = TRUE;
5449 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5450 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005451 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5452 {
5453 /* not a number or string */
5454 clear_tv(&var1);
5455 return FAIL;
5456 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457
5458 /*
5459 * Get the second variable from inside the [:].
5460 */
5461 if (**arg == ':')
5462 {
5463 range = TRUE;
5464 *arg = skipwhite(*arg + 1);
5465 if (**arg == ']')
5466 empty2 = TRUE;
5467 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005469 if (!empty1)
5470 clear_tv(&var1);
5471 return FAIL;
5472 }
5473 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5474 {
5475 /* not a number or string */
5476 if (!empty1)
5477 clear_tv(&var1);
5478 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005479 return FAIL;
5480 }
5481 }
5482
5483 /* Check for the ']'. */
5484 if (**arg != ']')
5485 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005486 if (verbose)
5487 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 clear_tv(&var1);
5489 if (range)
5490 clear_tv(&var2);
5491 return FAIL;
5492 }
5493 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 }
5495
5496 if (evaluate)
5497 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 n1 = 0;
5499 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005501 n1 = get_tv_number(&var1);
5502 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 }
5504 if (range)
5505 {
5506 if (empty2)
5507 n2 = -1;
5508 else
5509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510 n2 = get_tv_number(&var2);
5511 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 }
5513 }
5514
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005515 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005517 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005518 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005519 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005520 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005521 case VAR_SPECIAL:
5522 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005523 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005524 break; /* not evaluating, skipping over subscript */
5525
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 case VAR_NUMBER:
5527 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 len = (long)STRLEN(s);
5530 if (range)
5531 {
5532 /* The resulting variable is a substring. If the indexes
5533 * are out of range the result is empty. */
5534 if (n1 < 0)
5535 {
5536 n1 = len + n1;
5537 if (n1 < 0)
5538 n1 = 0;
5539 }
5540 if (n2 < 0)
5541 n2 = len + n2;
5542 else if (n2 >= len)
5543 n2 = len;
5544 if (n1 >= len || n2 < 0 || n1 > n2)
5545 s = NULL;
5546 else
5547 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5548 }
5549 else
5550 {
5551 /* The resulting variable is a string of a single
5552 * character. If the index is too big or negative the
5553 * result is empty. */
5554 if (n1 >= len || n1 < 0)
5555 s = NULL;
5556 else
5557 s = vim_strnsave(s + n1, 1);
5558 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 clear_tv(rettv);
5560 rettv->v_type = VAR_STRING;
5561 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 break;
5563
5564 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 if (n1 < 0)
5567 n1 = len + n1;
5568 if (!empty1 && (n1 < 0 || n1 >= len))
5569 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005570 /* For a range we allow invalid values and return an empty
5571 * list. A list index out of range is an error. */
5572 if (!range)
5573 {
5574 if (verbose)
5575 EMSGN(_(e_listidx), n1);
5576 return FAIL;
5577 }
5578 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 }
5580 if (range)
5581 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005582 list_T *l;
5583 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584
5585 if (n2 < 0)
5586 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005587 else if (n2 >= len)
5588 n2 = len - 1;
5589 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005590 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 l = list_alloc();
5592 if (l == NULL)
5593 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 n1 <= n2; ++n1)
5596 {
5597 if (list_append_tv(l, &item->li_tv) == FAIL)
5598 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005599 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 return FAIL;
5601 }
5602 item = item->li_next;
5603 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 clear_tv(rettv);
5605 rettv->v_type = VAR_LIST;
5606 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005607 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 }
5609 else
5610 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005611 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 clear_tv(rettv);
5613 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 }
5615 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616
5617 case VAR_DICT:
5618 if (range)
5619 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005620 if (verbose)
5621 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 if (len == -1)
5623 clear_tv(&var1);
5624 return FAIL;
5625 }
5626 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005628
5629 if (len == -1)
5630 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005631 key = get_tv_string_chk(&var1);
5632 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 clear_tv(&var1);
5635 return FAIL;
5636 }
5637 }
5638
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005639 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005641 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005642 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 if (len == -1)
5644 clear_tv(&var1);
5645 if (item == NULL)
5646 return FAIL;
5647
5648 copy_tv(&item->di_tv, &var1);
5649 clear_tv(rettv);
5650 *rettv = var1;
5651 }
5652 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653 }
5654 }
5655
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 return OK;
5657}
5658
5659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 * Get an option value.
5661 * "arg" points to the '&' or '+' before the option name.
5662 * "arg" is advanced to character after the option name.
5663 * Return OK or FAIL.
5664 */
5665 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005666get_option_tv(
5667 char_u **arg,
5668 typval_T *rettv, /* when NULL, only check if option exists */
5669 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671 char_u *option_end;
5672 long numval;
5673 char_u *stringval;
5674 int opt_type;
5675 int c;
5676 int working = (**arg == '+'); /* has("+option") */
5677 int ret = OK;
5678 int opt_flags;
5679
5680 /*
5681 * Isolate the option name and find its value.
5682 */
5683 option_end = find_option_end(arg, &opt_flags);
5684 if (option_end == NULL)
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 EMSG2(_("E112: Option name missing: %s"), *arg);
5688 return FAIL;
5689 }
5690
5691 if (!evaluate)
5692 {
5693 *arg = option_end;
5694 return OK;
5695 }
5696
5697 c = *option_end;
5698 *option_end = NUL;
5699 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
5702 if (opt_type == -3) /* invalid name */
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 EMSG2(_("E113: Unknown option: %s"), *arg);
5706 ret = FAIL;
5707 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005708 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 {
5710 if (opt_type == -2) /* hidden string option */
5711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005712 rettv->v_type = VAR_STRING;
5713 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
5715 else if (opt_type == -1) /* hidden number option */
5716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005717 rettv->v_type = VAR_NUMBER;
5718 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720 else if (opt_type == 1) /* number option */
5721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005722 rettv->v_type = VAR_NUMBER;
5723 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725 else /* string option */
5726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005727 rettv->v_type = VAR_STRING;
5728 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730 }
5731 else if (working && (opt_type == -2 || opt_type == -1))
5732 ret = FAIL;
5733
5734 *option_end = c; /* put back for error messages */
5735 *arg = option_end;
5736
5737 return ret;
5738}
5739
5740/*
5741 * Allocate a variable for a string constant.
5742 * Return OK or FAIL.
5743 */
5744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005745get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
5747 char_u *p;
5748 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 int extra = 0;
5750
5751 /*
5752 * Find the end of the string, skipping backslashed characters.
5753 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
5756 if (*p == '\\' && p[1] != NUL)
5757 {
5758 ++p;
5759 /* A "\<x>" form occupies at least 4 characters, and produces up
5760 * to 6 characters: reserve space for 2 extra */
5761 if (*p == '<')
5762 extra += 2;
5763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765
5766 if (*p != '"')
5767 {
5768 EMSG2(_("E114: Missing quote: %s"), *arg);
5769 return FAIL;
5770 }
5771
5772 /* If only parsing, set *arg and return here */
5773 if (!evaluate)
5774 {
5775 *arg = p + 1;
5776 return OK;
5777 }
5778
5779 /*
5780 * Copy the string into allocated memory, handling backslashed
5781 * characters.
5782 */
5783 name = alloc((unsigned)(p - *arg + extra));
5784 if (name == NULL)
5785 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 rettv->v_type = VAR_STRING;
5787 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 {
5791 if (*p == '\\')
5792 {
5793 switch (*++p)
5794 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 case 'b': *name++ = BS; ++p; break;
5796 case 'e': *name++ = ESC; ++p; break;
5797 case 'f': *name++ = FF; ++p; break;
5798 case 'n': *name++ = NL; ++p; break;
5799 case 'r': *name++ = CAR; ++p; break;
5800 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801
5802 case 'X': /* hex: "\x1", "\x12" */
5803 case 'x':
5804 case 'u': /* Unicode: "\u0023" */
5805 case 'U':
5806 if (vim_isxdigit(p[1]))
5807 {
5808 int n, nr;
5809 int c = toupper(*p);
5810
5811 if (c == 'X')
5812 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005813 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005815 else
5816 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 nr = 0;
5818 while (--n >= 0 && vim_isxdigit(p[1]))
5819 {
5820 ++p;
5821 nr = (nr << 4) + hex2nr(*p);
5822 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824#ifdef FEAT_MBYTE
5825 /* For "\u" store the number according to
5826 * 'encoding'. */
5827 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 else
5830#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 break;
5834
5835 /* octal: "\1", "\12", "\123" */
5836 case '0':
5837 case '1':
5838 case '2':
5839 case '3':
5840 case '4':
5841 case '5':
5842 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 case '7': *name = *p++ - '0';
5844 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 *name = (*name << 3) + *p++ - '0';
5847 if (*p >= '0' && *p <= '7')
5848 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852
5853 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 if (extra != 0)
5856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 break;
5859 }
5860 /* FALLTHROUGH */
5861
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 break;
5864 }
5865 }
5866 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 *arg = p + 1;
5872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 return OK;
5874}
5875
5876/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 * Return OK or FAIL.
5879 */
5880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005881get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
5883 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 int reduce = 0;
5886
5887 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 */
5890 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 break;
5896 ++reduce;
5897 ++p;
5898 }
5899 }
5900
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 return FAIL;
5905 }
5906
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 if (!evaluate)
5909 {
5910 *arg = p + 1;
5911 return OK;
5912 }
5913
5914 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005916 */
5917 str = alloc((unsigned)((p - *arg) - reduce));
5918 if (str == NULL)
5919 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 rettv->v_type = VAR_STRING;
5921 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 break;
5929 ++p;
5930 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005932 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 *arg = p + 1;
5935
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 return OK;
5937}
5938
Bram Moolenaarddecc252016-04-06 22:59:37 +02005939 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005940partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005941{
5942 int i;
5943
5944 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005945 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005946 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005947 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005948 func_unref(pt->pt_name);
5949 vim_free(pt->pt_name);
5950 vim_free(pt);
5951}
5952
5953/*
5954 * Unreference a closure: decrement the reference count and free it when it
5955 * becomes zero.
5956 */
5957 void
5958partial_unref(partial_T *pt)
5959{
5960 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005961 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005962}
5963
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005964/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 * Allocate a variable for a List and fill it from "*arg".
5966 * Return OK or FAIL.
5967 */
5968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970{
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 list_T *l = NULL;
5972 typval_T tv;
5973 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974
5975 if (evaluate)
5976 {
5977 l = list_alloc();
5978 if (l == NULL)
5979 return FAIL;
5980 }
5981
5982 *arg = skipwhite(*arg + 1);
5983 while (**arg != ']' && **arg != NUL)
5984 {
5985 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5986 goto failret;
5987 if (evaluate)
5988 {
5989 item = listitem_alloc();
5990 if (item != NULL)
5991 {
5992 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005993 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 list_append(l, item);
5995 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005996 else
5997 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 }
5999
6000 if (**arg == ']')
6001 break;
6002 if (**arg != ',')
6003 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006004 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 goto failret;
6006 }
6007 *arg = skipwhite(*arg + 1);
6008 }
6009
6010 if (**arg != ']')
6011 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006012 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013failret:
6014 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006015 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 return FAIL;
6017 }
6018
6019 *arg = skipwhite(*arg + 1);
6020 if (evaluate)
6021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006022 rettv->v_type = VAR_LIST;
6023 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 ++l->lv_refcount;
6025 }
6026
6027 return OK;
6028}
6029
6030/*
6031 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006032 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006034 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006035list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 list_T *l;
6038
6039 l = (list_T *)alloc_clear(sizeof(list_T));
6040 if (l != NULL)
6041 {
6042 /* Prepend the list to the list of lists for garbage collection. */
6043 if (first_list != NULL)
6044 first_list->lv_used_prev = l;
6045 l->lv_used_prev = NULL;
6046 l->lv_used_next = first_list;
6047 first_list = l;
6048 }
6049 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050}
6051
6052/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006053 * Allocate an empty list for a return value.
6054 * Returns OK or FAIL.
6055 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006056 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006057rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006058{
6059 list_T *l = list_alloc();
6060
6061 if (l == NULL)
6062 return FAIL;
6063
6064 rettv->vval.v_list = l;
6065 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006066 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006067 ++l->lv_refcount;
6068 return OK;
6069}
6070
6071/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 * Unreference a list: decrement the reference count and free it when it
6073 * becomes zero.
6074 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006078 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006079 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080}
6081
6082/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006083 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 * Ignores the reference count.
6085 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006086 static void
6087list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088{
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006091 for (item = l->lv_first; item != NULL; item = l->lv_first)
6092 {
6093 /* Remove the item before deleting it. */
6094 l->lv_first = item->li_next;
6095 clear_tv(&item->li_tv);
6096 vim_free(item);
6097 }
6098}
6099
6100 static void
6101list_free_list(list_T *l)
6102{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006103 /* Remove the list from the list of lists for garbage collection. */
6104 if (l->lv_used_prev == NULL)
6105 first_list = l->lv_used_next;
6106 else
6107 l->lv_used_prev->lv_used_next = l->lv_used_next;
6108 if (l->lv_used_next != NULL)
6109 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6110
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111 vim_free(l);
6112}
6113
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006114 void
6115list_free(list_T *l)
6116{
6117 if (!in_free_unref_items)
6118 {
6119 list_free_contents(l);
6120 list_free_list(l);
6121 }
6122}
6123
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006124/*
6125 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006126 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006127 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006128 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006129listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006132}
6133
6134/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006135 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006140 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141 vim_free(item);
6142}
6143
6144/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006145 * Remove a list item from a List and free it. Also clears the value.
6146 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006148listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006149{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006150 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006151 listitem_free(item);
6152}
6153
6154/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 * Get the number of items in a list.
6156 */
6157 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006158list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 if (l == NULL)
6161 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006162 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163}
6164
6165/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 * Return TRUE when two lists have exactly the same values.
6167 */
6168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006169list_equal(
6170 list_T *l1,
6171 list_T *l2,
6172 int ic, /* ignore case for strings */
6173 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174{
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006177 if (l1 == NULL || l2 == NULL)
6178 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006179 if (l1 == l2)
6180 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006181 if (list_len(l1) != list_len(l2))
6182 return FALSE;
6183
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 for (item1 = l1->lv_first, item2 = l2->lv_first;
6185 item1 != NULL && item2 != NULL;
6186 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006188 return FALSE;
6189 return item1 == NULL && item2 == NULL;
6190}
6191
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006192/*
6193 * Return the dictitem that an entry in a hashtable points to.
6194 */
6195 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006196dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006197{
6198 return HI2DI(hi);
6199}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006200
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006201/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006202 * Return TRUE when two dictionaries have exactly the same key/values.
6203 */
6204 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006205dict_equal(
6206 dict_T *d1,
6207 dict_T *d2,
6208 int ic, /* ignore case for strings */
6209 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006210{
Bram Moolenaar33570922005-01-25 22:26:29 +00006211 hashitem_T *hi;
6212 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006213 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006214
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006215 if (d1 == NULL || d2 == NULL)
6216 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006217 if (d1 == d2)
6218 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006219 if (dict_len(d1) != dict_len(d2))
6220 return FALSE;
6221
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006222 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006223 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006224 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006225 if (!HASHITEM_EMPTY(hi))
6226 {
6227 item2 = dict_find(d2, hi->hi_key, -1);
6228 if (item2 == NULL)
6229 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006230 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006231 return FALSE;
6232 --todo;
6233 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006234 }
6235 return TRUE;
6236}
6237
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006238static int tv_equal_recurse_limit;
6239
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006240/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006241 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006243 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006244 */
6245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006246tv_equal(
6247 typval_T *tv1,
6248 typval_T *tv2,
6249 int ic, /* ignore case */
6250 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006251{
6252 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006253 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006254 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006255 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006256
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006257 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6258 if ((tv1->v_type == VAR_FUNC
6259 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6260 && (tv2->v_type == VAR_FUNC
6261 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6262 {
6263 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6264 : tv1->vval.v_partial->pt_name;
6265 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6266 : tv2->vval.v_partial->pt_name;
6267 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6268 }
6269
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006270 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006271 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006272
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006273 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006274 * recursiveness to a limit. We guess they are equal then.
6275 * A fixed limit has the problem of still taking an awful long time.
6276 * Reduce the limit every time running into it. That should work fine for
6277 * deeply linked structures that are not recursively linked and catch
6278 * recursiveness quickly. */
6279 if (!recursive)
6280 tv_equal_recurse_limit = 1000;
6281 if (recursive_cnt >= tv_equal_recurse_limit)
6282 {
6283 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006284 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006285 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006286
6287 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006288 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006289 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006290 ++recursive_cnt;
6291 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6292 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006293 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006294
6295 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006296 ++recursive_cnt;
6297 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6298 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006299 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006300
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006301 case VAR_NUMBER:
6302 return tv1->vval.v_number == tv2->vval.v_number;
6303
6304 case VAR_STRING:
6305 s1 = get_tv_string_buf(tv1, buf1);
6306 s2 = get_tv_string_buf(tv2, buf2);
6307 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006308
6309 case VAR_SPECIAL:
6310 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006311
6312 case VAR_FLOAT:
6313#ifdef FEAT_FLOAT
6314 return tv1->vval.v_float == tv2->vval.v_float;
6315#endif
6316 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006317#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006318 return tv1->vval.v_job == tv2->vval.v_job;
6319#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006320 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006321#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006322 return tv1->vval.v_channel == tv2->vval.v_channel;
6323#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006324 case VAR_FUNC:
6325 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006326 case VAR_UNKNOWN:
6327 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006329
Bram Moolenaara03f2332016-02-06 18:09:59 +01006330 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6331 * does not equal anything, not even itself. */
6332 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006333}
6334
6335/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336 * Locate item with index "n" in list "l" and return it.
6337 * A negative index is counted from the end; -1 is the last item.
6338 * Returns NULL when "n" is out of range.
6339 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006340 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006341list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342{
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344 long idx;
6345
6346 if (l == NULL)
6347 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006348
6349 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006351 n = l->lv_len + n;
6352
6353 /* Check for index out of range. */
6354 if (n < 0 || n >= l->lv_len)
6355 return NULL;
6356
6357 /* When there is a cached index may start search from there. */
6358 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006360 if (n < l->lv_idx / 2)
6361 {
6362 /* closest to the start of the list */
6363 item = l->lv_first;
6364 idx = 0;
6365 }
6366 else if (n > (l->lv_idx + l->lv_len) / 2)
6367 {
6368 /* closest to the end of the list */
6369 item = l->lv_last;
6370 idx = l->lv_len - 1;
6371 }
6372 else
6373 {
6374 /* closest to the cached index */
6375 item = l->lv_idx_item;
6376 idx = l->lv_idx;
6377 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 }
6379 else
6380 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006381 if (n < l->lv_len / 2)
6382 {
6383 /* closest to the start of the list */
6384 item = l->lv_first;
6385 idx = 0;
6386 }
6387 else
6388 {
6389 /* closest to the end of the list */
6390 item = l->lv_last;
6391 idx = l->lv_len - 1;
6392 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006394
6395 while (n > idx)
6396 {
6397 /* search forward */
6398 item = item->li_next;
6399 ++idx;
6400 }
6401 while (n < idx)
6402 {
6403 /* search backward */
6404 item = item->li_prev;
6405 --idx;
6406 }
6407
6408 /* cache the used index */
6409 l->lv_idx = idx;
6410 l->lv_idx_item = item;
6411
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 return item;
6413}
6414
6415/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006416 * Get list item "l[idx]" as a number.
6417 */
6418 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006419list_find_nr(
6420 list_T *l,
6421 long idx,
6422 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006423{
6424 listitem_T *li;
6425
6426 li = list_find(l, idx);
6427 if (li == NULL)
6428 {
6429 if (errorp != NULL)
6430 *errorp = TRUE;
6431 return -1L;
6432 }
6433 return get_tv_number_chk(&li->li_tv, errorp);
6434}
6435
6436/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006437 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6438 */
6439 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006441{
6442 listitem_T *li;
6443
6444 li = list_find(l, idx - 1);
6445 if (li == NULL)
6446 {
6447 EMSGN(_(e_listidx), idx);
6448 return NULL;
6449 }
6450 return get_tv_string(&li->li_tv);
6451}
6452
6453/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006454 * Locate "item" list "l" and return its index.
6455 * Returns -1 when "item" is not in the list.
6456 */
6457 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006458list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006459{
6460 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006462
6463 if (l == NULL)
6464 return -1;
6465 idx = 0;
6466 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6467 ++idx;
6468 if (li == NULL)
6469 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006470 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006471}
6472
6473/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474 * Append item "item" to the end of list "l".
6475 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006476 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006477list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478{
6479 if (l->lv_last == NULL)
6480 {
6481 /* empty list */
6482 l->lv_first = item;
6483 l->lv_last = item;
6484 item->li_prev = NULL;
6485 }
6486 else
6487 {
6488 l->lv_last->li_next = item;
6489 item->li_prev = l->lv_last;
6490 l->lv_last = item;
6491 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006492 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 item->li_next = NULL;
6494}
6495
6496/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006500 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006501list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006503 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504
Bram Moolenaar05159a02005-02-26 23:04:13 +00006505 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006507 copy_tv(tv, &li->li_tv);
6508 list_append(l, li);
6509 return OK;
6510}
6511
6512/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006513 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006514 * Return FAIL when out of memory.
6515 */
6516 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006517list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006518{
6519 listitem_T *li = listitem_alloc();
6520
6521 if (li == NULL)
6522 return FAIL;
6523 li->li_tv.v_type = VAR_DICT;
6524 li->li_tv.v_lock = 0;
6525 li->li_tv.vval.v_dict = dict;
6526 list_append(list, li);
6527 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 return OK;
6529}
6530
6531/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006532 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006533 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006534 * Returns FAIL when out of memory.
6535 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006536 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006537list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006538{
6539 listitem_T *li = listitem_alloc();
6540
6541 if (li == NULL)
6542 return FAIL;
6543 list_append(l, li);
6544 li->li_tv.v_type = VAR_STRING;
6545 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006546 if (str == NULL)
6547 li->li_tv.vval.v_string = NULL;
6548 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006549 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006550 return FAIL;
6551 return OK;
6552}
6553
6554/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006555 * Append "n" to list "l".
6556 * Returns FAIL when out of memory.
6557 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006558 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006560{
6561 listitem_T *li;
6562
6563 li = listitem_alloc();
6564 if (li == NULL)
6565 return FAIL;
6566 li->li_tv.v_type = VAR_NUMBER;
6567 li->li_tv.v_lock = 0;
6568 li->li_tv.vval.v_number = n;
6569 list_append(l, li);
6570 return OK;
6571}
6572
6573/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006575 * If "item" is NULL append at the end.
6576 * Return FAIL when out of memory.
6577 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006579list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580{
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582
6583 if (ni == NULL)
6584 return FAIL;
6585 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006586 list_insert(l, ni, item);
6587 return OK;
6588}
6589
6590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006591list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006592{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 if (item == NULL)
6594 /* Append new item at end of list. */
6595 list_append(l, ni);
6596 else
6597 {
6598 /* Insert new item before existing item. */
6599 ni->li_prev = item->li_prev;
6600 ni->li_next = item;
6601 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006602 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006603 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006604 ++l->lv_idx;
6605 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006607 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006608 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006609 l->lv_idx_item = NULL;
6610 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006611 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006612 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006614}
6615
6616/*
6617 * Extend "l1" with "l2".
6618 * If "bef" is NULL append at the end, otherwise insert before this item.
6619 * Returns FAIL when out of memory.
6620 */
6621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006622list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006623{
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006625 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006627 /* We also quit the loop when we have inserted the original item count of
6628 * the list, avoid a hang when we extend a list with itself. */
6629 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006630 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6631 return FAIL;
6632 return OK;
6633}
6634
6635/*
6636 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6637 * Return FAIL when out of memory.
6638 */
6639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006640list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641{
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006643
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006644 if (l1 == NULL || l2 == NULL)
6645 return FAIL;
6646
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006648 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006649 if (l == NULL)
6650 return FAIL;
6651 tv->v_type = VAR_LIST;
6652 tv->vval.v_list = l;
6653
6654 /* append all items from the second list */
6655 return list_extend(l, l2, NULL);
6656}
6657
6658/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006659 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006661 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 * Returns NULL when out of memory.
6663 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006664 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666{
Bram Moolenaar33570922005-01-25 22:26:29 +00006667 list_T *copy;
6668 listitem_T *item;
6669 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670
6671 if (orig == NULL)
6672 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673
6674 copy = list_alloc();
6675 if (copy != NULL)
6676 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 if (copyID != 0)
6678 {
6679 /* Do this before adding the items, because one of the items may
6680 * refer back to this list. */
6681 orig->lv_copyID = copyID;
6682 orig->lv_copylist = copy;
6683 }
6684 for (item = orig->lv_first; item != NULL && !got_int;
6685 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686 {
6687 ni = listitem_alloc();
6688 if (ni == NULL)
6689 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006690 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006691 {
6692 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6693 {
6694 vim_free(ni);
6695 break;
6696 }
6697 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006698 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006699 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 list_append(copy, ni);
6701 }
6702 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006703 if (item != NULL)
6704 {
6705 list_unref(copy);
6706 copy = NULL;
6707 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006708 }
6709
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006710 return copy;
6711}
6712
6713/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006714 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006715 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006716 * This used to be called list_remove, but that conflicts with a Sun header
6717 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006718 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006720vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006721{
Bram Moolenaar33570922005-01-25 22:26:29 +00006722 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006723
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006724 /* notify watchers */
6725 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006726 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006727 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006728 list_fix_watch(l, ip);
6729 if (ip == item2)
6730 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006731 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732
6733 if (item2->li_next == NULL)
6734 l->lv_last = item->li_prev;
6735 else
6736 item2->li_next->li_prev = item->li_prev;
6737 if (item->li_prev == NULL)
6738 l->lv_first = item2->li_next;
6739 else
6740 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006741 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006742}
6743
6744/*
6745 * Return an allocated string with the string representation of a list.
6746 * May return NULL.
6747 */
6748 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006749list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750{
6751 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006752
6753 if (tv->vval.v_list == NULL)
6754 return NULL;
6755 ga_init2(&ga, (int)sizeof(char), 80);
6756 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006757 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006758 {
6759 vim_free(ga.ga_data);
6760 return NULL;
6761 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762 ga_append(&ga, ']');
6763 ga_append(&ga, NUL);
6764 return (char_u *)ga.ga_data;
6765}
6766
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006767typedef struct join_S {
6768 char_u *s;
6769 char_u *tofree;
6770} join_T;
6771
6772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006773list_join_inner(
6774 garray_T *gap, /* to store the result in */
6775 list_T *l,
6776 char_u *sep,
6777 int echo_style,
6778 int copyID,
6779 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006780{
6781 int i;
6782 join_T *p;
6783 int len;
6784 int sumlen = 0;
6785 int first = TRUE;
6786 char_u *tofree;
6787 char_u numbuf[NUMBUFLEN];
6788 listitem_T *item;
6789 char_u *s;
6790
6791 /* Stringify each item in the list. */
6792 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6793 {
6794 if (echo_style)
6795 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6796 else
6797 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6798 if (s == NULL)
6799 return FAIL;
6800
6801 len = (int)STRLEN(s);
6802 sumlen += len;
6803
Bram Moolenaarcde88542015-08-11 19:14:00 +02006804 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006805 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6806 if (tofree != NULL || s != numbuf)
6807 {
6808 p->s = s;
6809 p->tofree = tofree;
6810 }
6811 else
6812 {
6813 p->s = vim_strnsave(s, len);
6814 p->tofree = p->s;
6815 }
6816
6817 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006818 if (did_echo_string_emsg) /* recursion error, bail out */
6819 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006820 }
6821
6822 /* Allocate result buffer with its total size, avoid re-allocation and
6823 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6824 if (join_gap->ga_len >= 2)
6825 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6826 if (ga_grow(gap, sumlen + 2) == FAIL)
6827 return FAIL;
6828
6829 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6830 {
6831 if (first)
6832 first = FALSE;
6833 else
6834 ga_concat(gap, sep);
6835 p = ((join_T *)join_gap->ga_data) + i;
6836
6837 if (p->s != NULL)
6838 ga_concat(gap, p->s);
6839 line_breakcheck();
6840 }
6841
6842 return OK;
6843}
6844
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006846 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006847 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006848 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006849 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006851list_join(
6852 garray_T *gap,
6853 list_T *l,
6854 char_u *sep,
6855 int echo_style,
6856 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006857{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006858 garray_T join_ga;
6859 int retval;
6860 join_T *p;
6861 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006862
Bram Moolenaard39a7512015-04-16 22:51:22 +02006863 if (l->lv_len < 1)
6864 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006865 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6866 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6867
6868 /* Dispose each item in join_ga. */
6869 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006870 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006871 p = (join_T *)join_ga.ga_data;
6872 for (i = 0; i < join_ga.ga_len; ++i)
6873 {
6874 vim_free(p->tofree);
6875 ++p;
6876 }
6877 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006878 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006879
6880 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006881}
6882
6883/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006884 * Return the next (unique) copy ID.
6885 * Used for serializing nested structures.
6886 */
6887 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006888get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006889{
6890 current_copyID += COPYID_INC;
6891 return current_copyID;
6892}
6893
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006894/* Used by get_func_tv() */
6895static garray_T funcargs = GA_EMPTY;
6896
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006897/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 * Garbage collection for lists and dictionaries.
6899 *
6900 * We use reference counts to be able to free most items right away when they
6901 * are no longer used. But for composite items it's possible that it becomes
6902 * unused while the reference count is > 0: When there is a recursive
6903 * reference. Example:
6904 * :let l = [1, 2, 3]
6905 * :let d = {9: l}
6906 * :let l[1] = d
6907 *
6908 * Since this is quite unusual we handle this with garbage collection: every
6909 * once in a while find out which lists and dicts are not referenced from any
6910 * variable.
6911 *
6912 * Here is a good reference text about garbage collection (refers to Python
6913 * but it applies to all reference-counting mechanisms):
6914 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006915 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006916
6917/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918 * Do garbage collection for lists and dicts.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006919 * When "testing" is TRUE this is called from garbagecollect_for_testing().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006921 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006923garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006924{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006925 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 buf_T *buf;
6928 win_T *wp;
6929 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006930 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006931 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006932 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006933#ifdef FEAT_WINDOWS
6934 tabpage_T *tp;
6935#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006937 if (!testing)
6938 {
6939 /* Only do this once. */
6940 want_garbage_collect = FALSE;
6941 may_garbage_collect = FALSE;
6942 garbage_collect_at_exit = FALSE;
6943 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006944
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006945 /* We advance by two because we add one for items referenced through
6946 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006947 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949 /*
6950 * 1. Go through all accessible variables and mark all lists and dicts
6951 * with copyID.
6952 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006953
6954 /* Don't free variables in the previous_funccal list unless they are only
6955 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006956 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6958 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6960 NULL);
6961 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6962 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 }
6964
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965 /* script-local variables */
6966 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006967 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968
6969 /* buffer-local variables */
6970 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006971 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6972 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973
6974 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006975 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006976 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6977 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006978#ifdef FEAT_AUTOCMD
6979 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6981 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006982#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006984#ifdef FEAT_WINDOWS
6985 /* tabpage-local variables */
6986 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006987 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6988 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006989#endif
6990
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006992 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993
6994 /* function-local variables */
6995 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6996 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006997 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6998 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 }
7000
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007001 /* function call arguments, if v:testing is set. */
7002 for (i = 0; i < funcargs.ga_len; ++i)
7003 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7004 copyID, NULL, NULL);
7005
Bram Moolenaard812df62008-11-09 12:46:09 +00007006 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007008
Bram Moolenaar1dced572012-04-05 16:54:08 +02007009#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007010 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007011#endif
7012
Bram Moolenaardb913952012-06-29 12:54:53 +02007013#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007014 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007015#endif
7016
7017#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007019#endif
7020
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007021#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007022 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007023#endif
7024
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007026 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 /*
7028 * 2. Free lists and dictionaries that are not referenced.
7029 */
7030 did_free = free_unref_items(copyID);
7031
7032 /*
7033 * 3. Check if any funccal can be freed now.
7034 */
7035 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007036 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 if (can_free_funccal(*pfc, copyID))
7038 {
7039 fc = *pfc;
7040 *pfc = fc->caller;
7041 free_funccal(fc, TRUE);
7042 did_free = TRUE;
7043 did_free_funccal = TRUE;
7044 }
7045 else
7046 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007047 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 if (did_free_funccal)
7049 /* When a funccal was freed some more items might be garbage
7050 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007051 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007052 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 else if (p_verbose > 0)
7054 {
7055 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7056 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007057
7058 return did_free;
7059}
7060
7061/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007062 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007063 */
7064 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007065free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007066{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007067 dict_T *dd, *dd_next;
7068 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007069 int did_free = FALSE;
7070
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007071 /* Let all "free" functions know that we are here. This means no
7072 * dictionaries, lists, channels or jobs are to be freed, because we will
7073 * do that here. */
7074 in_free_unref_items = TRUE;
7075
7076 /*
7077 * PASS 1: free the contents of the items. We don't free the items
7078 * themselves yet, so that it is possible to decrement refcount counters
7079 */
7080
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007082 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007084 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007085 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007087 /* Free the Dictionary and ordinary items it contains, but don't
7088 * recurse into Lists and Dictionaries, they will be in the list
7089 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007090 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007091 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093
7094 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007095 * Go through the list of lists and free items without the copyID.
7096 * But don't free a list that has a watcher (used in a for loop), these
7097 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007098 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007099 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7100 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7101 && ll->lv_watch == NULL)
7102 {
7103 /* Free the List and ordinary items it contains, but don't recurse
7104 * into Lists and Dictionaries, they will be in the list of dicts
7105 * or list of lists. */
7106 list_free_contents(ll);
7107 did_free = TRUE;
7108 }
7109
7110#ifdef FEAT_JOB_CHANNEL
7111 /* Go through the list of jobs and free items without the copyID. This
7112 * must happen before doing channels, because jobs refer to channels, but
7113 * the reference from the channel to the job isn't tracked. */
7114 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7115
7116 /* Go through the list of channels and free items without the copyID. */
7117 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7118#endif
7119
7120 /*
7121 * PASS 2: free the items themselves.
7122 */
7123 for (dd = first_dict; dd != NULL; dd = dd_next)
7124 {
7125 dd_next = dd->dv_used_next;
7126 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7127 dict_free_dict(dd);
7128 }
7129
7130 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007131 {
7132 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007133 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7134 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007135 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007136 /* Free the List and ordinary items it contains, but don't recurse
7137 * into Lists and Dictionaries, they will be in the list of dicts
7138 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007139 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007140 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007141 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007142
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007143#ifdef FEAT_JOB_CHANNEL
7144 /* Go through the list of jobs and free items without the copyID. This
7145 * must happen before doing channels, because jobs refer to channels, but
7146 * the reference from the channel to the job isn't tracked. */
7147 free_unused_jobs(copyID, COPYID_MASK);
7148
7149 /* Go through the list of channels and free items without the copyID. */
7150 free_unused_channels(copyID, COPYID_MASK);
7151#endif
7152
7153 in_free_unref_items = FALSE;
7154
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007155 return did_free;
7156}
7157
7158/*
7159 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007160 * "list_stack" is used to add lists to be marked. Can be NULL.
7161 *
7162 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007163 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007166{
7167 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007169 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007170 hashtab_T *cur_ht;
7171 ht_stack_T *ht_stack = NULL;
7172 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007173
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007174 cur_ht = ht;
7175 for (;;)
7176 {
7177 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007178 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007179 /* Mark each item in the hashtab. If the item contains a hashtab
7180 * it is added to ht_stack, if it contains a list it is added to
7181 * list_stack. */
7182 todo = (int)cur_ht->ht_used;
7183 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7184 if (!HASHITEM_EMPTY(hi))
7185 {
7186 --todo;
7187 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7188 &ht_stack, list_stack);
7189 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007190 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007191
7192 if (ht_stack == NULL)
7193 break;
7194
7195 /* take an item from the stack */
7196 cur_ht = ht_stack->ht;
7197 tempitem = ht_stack;
7198 ht_stack = ht_stack->prev;
7199 free(tempitem);
7200 }
7201
7202 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007203}
7204
7205/*
7206 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007207 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7208 *
7209 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007211 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007212set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007213{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007214 listitem_T *li;
7215 int abort = FALSE;
7216 list_T *cur_l;
7217 list_stack_T *list_stack = NULL;
7218 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007219
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007220 cur_l = l;
7221 for (;;)
7222 {
7223 if (!abort)
7224 /* Mark each item in the list. If the item contains a hashtab
7225 * it is added to ht_stack, if it contains a list it is added to
7226 * list_stack. */
7227 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7228 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7229 ht_stack, &list_stack);
7230 if (list_stack == NULL)
7231 break;
7232
7233 /* take an item from the stack */
7234 cur_l = list_stack->list;
7235 tempitem = list_stack;
7236 list_stack = list_stack->prev;
7237 free(tempitem);
7238 }
7239
7240 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241}
7242
7243/*
7244 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007245 * "list_stack" is used to add lists to be marked. Can be NULL.
7246 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7247 *
7248 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007249 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007251set_ref_in_item(
7252 typval_T *tv,
7253 int copyID,
7254 ht_stack_T **ht_stack,
7255 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007256{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007257 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007258
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007259 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007260 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007261 dict_T *dd = tv->vval.v_dict;
7262
Bram Moolenaara03f2332016-02-06 18:09:59 +01007263 if (dd != NULL && dd->dv_copyID != copyID)
7264 {
7265 /* Didn't see this dict yet. */
7266 dd->dv_copyID = copyID;
7267 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007268 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007269 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7270 }
7271 else
7272 {
7273 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7274 if (newitem == NULL)
7275 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007276 else
7277 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007278 newitem->ht = &dd->dv_hashtab;
7279 newitem->prev = *ht_stack;
7280 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007281 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007282 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007283 }
7284 }
7285 else if (tv->v_type == VAR_LIST)
7286 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007287 list_T *ll = tv->vval.v_list;
7288
Bram Moolenaara03f2332016-02-06 18:09:59 +01007289 if (ll != NULL && ll->lv_copyID != copyID)
7290 {
7291 /* Didn't see this list yet. */
7292 ll->lv_copyID = copyID;
7293 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007294 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007295 abort = set_ref_in_list(ll, copyID, ht_stack);
7296 }
7297 else
7298 {
7299 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007300 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007301 if (newitem == NULL)
7302 abort = TRUE;
7303 else
7304 {
7305 newitem->list = ll;
7306 newitem->prev = *list_stack;
7307 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007308 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007309 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007310 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007311 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007312 else if (tv->v_type == VAR_PARTIAL)
7313 {
7314 partial_T *pt = tv->vval.v_partial;
7315 int i;
7316
7317 /* A partial does not have a copyID, because it cannot contain itself.
7318 */
7319 if (pt != NULL)
7320 {
7321 if (pt->pt_dict != NULL)
7322 {
7323 typval_T dtv;
7324
7325 dtv.v_type = VAR_DICT;
7326 dtv.vval.v_dict = pt->pt_dict;
7327 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7328 }
7329
7330 for (i = 0; i < pt->pt_argc; ++i)
7331 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7332 ht_stack, list_stack);
7333 }
7334 }
7335#ifdef FEAT_JOB_CHANNEL
7336 else if (tv->v_type == VAR_JOB)
7337 {
7338 job_T *job = tv->vval.v_job;
7339 typval_T dtv;
7340
7341 if (job != NULL && job->jv_copyID != copyID)
7342 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007343 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007344 if (job->jv_channel != NULL)
7345 {
7346 dtv.v_type = VAR_CHANNEL;
7347 dtv.vval.v_channel = job->jv_channel;
7348 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7349 }
7350 if (job->jv_exit_partial != NULL)
7351 {
7352 dtv.v_type = VAR_PARTIAL;
7353 dtv.vval.v_partial = job->jv_exit_partial;
7354 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7355 }
7356 }
7357 }
7358 else if (tv->v_type == VAR_CHANNEL)
7359 {
7360 channel_T *ch =tv->vval.v_channel;
7361 int part;
7362 typval_T dtv;
7363 jsonq_T *jq;
7364 cbq_T *cq;
7365
7366 if (ch != NULL && ch->ch_copyID != copyID)
7367 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007368 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007369 for (part = PART_SOCK; part <= PART_IN; ++part)
7370 {
7371 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7372 jq = jq->jq_next)
7373 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7374 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7375 cq = cq->cq_next)
7376 if (cq->cq_partial != NULL)
7377 {
7378 dtv.v_type = VAR_PARTIAL;
7379 dtv.vval.v_partial = cq->cq_partial;
7380 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7381 }
7382 if (ch->ch_part[part].ch_partial != NULL)
7383 {
7384 dtv.v_type = VAR_PARTIAL;
7385 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7386 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7387 }
7388 }
7389 if (ch->ch_partial != NULL)
7390 {
7391 dtv.v_type = VAR_PARTIAL;
7392 dtv.vval.v_partial = ch->ch_partial;
7393 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7394 }
7395 if (ch->ch_close_partial != NULL)
7396 {
7397 dtv.v_type = VAR_PARTIAL;
7398 dtv.vval.v_partial = ch->ch_close_partial;
7399 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7400 }
7401 }
7402 }
7403#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007404 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007405}
7406
7407/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 * Allocate an empty header for a dictionary.
7409 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007410 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412{
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007416 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007417 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007418 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007419 if (first_dict != NULL)
7420 first_dict->dv_used_prev = d;
7421 d->dv_used_next = first_dict;
7422 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007423 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007424
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007426 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007427 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007428 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007429 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007430 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432}
7433
7434/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007435 * Allocate an empty dict for a return value.
7436 * Returns OK or FAIL.
7437 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007439rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007440{
7441 dict_T *d = dict_alloc();
7442
7443 if (d == NULL)
7444 return FAIL;
7445
7446 rettv->vval.v_dict = d;
7447 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007448 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007449 ++d->dv_refcount;
7450 return OK;
7451}
7452
7453
7454/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007455 * Unreference a Dictionary: decrement the reference count and free it when it
7456 * becomes zero.
7457 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007458 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007459dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007461 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007462 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463}
7464
7465/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007466 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 * Ignores the reference count.
7468 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007469 static void
7470dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007472 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007473 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007474 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007476 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007477 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007478 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007479 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007481 if (!HASHITEM_EMPTY(hi))
7482 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007483 /* Remove the item before deleting it, just in case there is
7484 * something recursive causing trouble. */
7485 di = HI2DI(hi);
7486 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007487 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007488 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007489 --todo;
7490 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007493}
7494
7495 static void
7496dict_free_dict(dict_T *d)
7497{
7498 /* Remove the dict from the list of dicts for garbage collection. */
7499 if (d->dv_used_prev == NULL)
7500 first_dict = d->dv_used_next;
7501 else
7502 d->dv_used_prev->dv_used_next = d->dv_used_next;
7503 if (d->dv_used_next != NULL)
7504 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 vim_free(d);
7506}
7507
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007508 void
7509dict_free(dict_T *d)
7510{
7511 if (!in_free_unref_items)
7512 {
7513 dict_free_contents(d);
7514 dict_free_dict(d);
7515 }
7516}
7517
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518/*
7519 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 * The "key" is copied to the new item.
7521 * Note that the value of the item "di_tv" still needs to be initialized!
7522 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007524 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007525dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526{
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007528
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007529 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007533 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007534 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536}
7537
7538/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007539 * Make a copy of a Dictionary item.
7540 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007542dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007543{
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007545
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007546 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7547 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007548 if (di != NULL)
7549 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007551 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 copy_tv(&org->di_tv, &di->di_tv);
7553 }
7554 return di;
7555}
7556
7557/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007558 * Remove item "item" from Dictionary "dict" and free it.
7559 */
7560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007561dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562{
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007564
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 if (HASHITEM_EMPTY(hi))
7567 EMSG2(_(e_intern2), "dictitem_remove()");
7568 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007570 dictitem_free(item);
7571}
7572
7573/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 * Free a dict item. Also clears the value.
7575 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007577dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007580 if (item->di_flags & DI_FLAGS_ALLOC)
7581 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582}
7583
7584/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007585 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7586 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007587 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007588 * Returns NULL when out of memory.
7589 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007591dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007592{
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 dict_T *copy;
7594 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007595 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007597
7598 if (orig == NULL)
7599 return NULL;
7600
7601 copy = dict_alloc();
7602 if (copy != NULL)
7603 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007604 if (copyID != 0)
7605 {
7606 orig->dv_copyID = copyID;
7607 orig->dv_copydict = copy;
7608 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007609 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007610 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007612 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007613 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 --todo;
7615
7616 di = dictitem_alloc(hi->hi_key);
7617 if (di == NULL)
7618 break;
7619 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007620 {
7621 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7622 copyID) == FAIL)
7623 {
7624 vim_free(di);
7625 break;
7626 }
7627 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 else
7629 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7630 if (dict_add(copy, di) == FAIL)
7631 {
7632 dictitem_free(di);
7633 break;
7634 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007639 if (todo > 0)
7640 {
7641 dict_unref(copy);
7642 copy = NULL;
7643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644 }
7645
7646 return copy;
7647}
7648
7649/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007651 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655{
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657}
7658
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007660 * Add a number or string entry to dictionary "d".
7661 * When "str" is NULL use number "nr", otherwise use "str".
7662 * Returns FAIL when out of memory and when key already exists.
7663 */
7664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665dict_add_nr_str(
7666 dict_T *d,
7667 char *key,
7668 long nr,
7669 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007670{
7671 dictitem_T *item;
7672
7673 item = dictitem_alloc((char_u *)key);
7674 if (item == NULL)
7675 return FAIL;
7676 item->di_tv.v_lock = 0;
7677 if (str == NULL)
7678 {
7679 item->di_tv.v_type = VAR_NUMBER;
7680 item->di_tv.vval.v_number = nr;
7681 }
7682 else
7683 {
7684 item->di_tv.v_type = VAR_STRING;
7685 item->di_tv.vval.v_string = vim_strsave(str);
7686 }
7687 if (dict_add(d, item) == FAIL)
7688 {
7689 dictitem_free(item);
7690 return FAIL;
7691 }
7692 return OK;
7693}
7694
7695/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007696 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007697 * Returns FAIL when out of memory and when key already exists.
7698 */
7699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007700dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007701{
7702 dictitem_T *item;
7703
7704 item = dictitem_alloc((char_u *)key);
7705 if (item == NULL)
7706 return FAIL;
7707 item->di_tv.v_lock = 0;
7708 item->di_tv.v_type = VAR_LIST;
7709 item->di_tv.vval.v_list = list;
7710 if (dict_add(d, item) == FAIL)
7711 {
7712 dictitem_free(item);
7713 return FAIL;
7714 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007715 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007716 return OK;
7717}
7718
7719/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007720 * Get the number of items in a Dictionary.
7721 */
7722 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007723dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007724{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007725 if (d == NULL)
7726 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007727 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007728}
7729
7730/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007731 * Find item "key[len]" in Dictionary "d".
7732 * If "len" is negative use strlen(key).
7733 * Returns NULL when not found.
7734 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007735 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007738#define AKEYLEN 200
7739 char_u buf[AKEYLEN];
7740 char_u *akey;
7741 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007744 if (len < 0)
7745 akey = key;
7746 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007747 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007748 tofree = akey = vim_strnsave(key, len);
7749 if (akey == NULL)
7750 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007751 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007752 else
7753 {
7754 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007755 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007756 akey = buf;
7757 }
7758
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007760 vim_free(tofree);
7761 if (HASHITEM_EMPTY(hi))
7762 return NULL;
7763 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764}
7765
7766/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007767 * Get a string item from a dictionary.
7768 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007769 * Returns NULL if the entry doesn't exist or out of memory.
7770 */
7771 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007772get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007773{
7774 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007775 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007776
7777 di = dict_find(d, key, -1);
7778 if (di == NULL)
7779 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007780 s = get_tv_string(&di->di_tv);
7781 if (save && s != NULL)
7782 s = vim_strsave(s);
7783 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007784}
7785
7786/*
7787 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007788 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007789 */
7790 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007791get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007792{
7793 dictitem_T *di;
7794
7795 di = dict_find(d, key, -1);
7796 if (di == NULL)
7797 return 0;
7798 return get_tv_number(&di->di_tv);
7799}
7800
7801/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007802 * Return an allocated string with the string representation of a Dictionary.
7803 * May return NULL.
7804 */
7805 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007806dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007807{
7808 garray_T ga;
7809 int first = TRUE;
7810 char_u *tofree;
7811 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007813 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007814 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007815 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007816
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007817 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007818 return NULL;
7819 ga_init2(&ga, (int)sizeof(char), 80);
7820 ga_append(&ga, '{');
7821
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007822 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007823 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007824 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007825 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007826 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007827 --todo;
7828
7829 if (first)
7830 first = FALSE;
7831 else
7832 ga_concat(&ga, (char_u *)", ");
7833
7834 tofree = string_quote(hi->hi_key, FALSE);
7835 if (tofree != NULL)
7836 {
7837 ga_concat(&ga, tofree);
7838 vim_free(tofree);
7839 }
7840 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007841 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007842 if (s != NULL)
7843 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007844 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007845 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007846 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007847 line_breakcheck();
7848
Bram Moolenaar8c711452005-01-14 21:53:12 +00007849 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007850 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007851 if (todo > 0)
7852 {
7853 vim_free(ga.ga_data);
7854 return NULL;
7855 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856
7857 ga_append(&ga, '}');
7858 ga_append(&ga, NUL);
7859 return (char_u *)ga.ga_data;
7860}
7861
7862/*
7863 * Allocate a variable for a Dictionary and fill it from "*arg".
7864 * Return OK or FAIL. Returns NOTDONE for {expr}.
7865 */
7866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007867get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007868{
Bram Moolenaar33570922005-01-25 22:26:29 +00007869 dict_T *d = NULL;
7870 typval_T tvkey;
7871 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007872 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007874 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007875 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007876
7877 /*
7878 * First check if it's not a curly-braces thing: {expr}.
7879 * Must do this without evaluating, otherwise a function may be called
7880 * twice. Unfortunately this means we need to call eval1() twice for the
7881 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007883 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007884 if (*start != '}')
7885 {
7886 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7887 return FAIL;
7888 if (*start == '}')
7889 return NOTDONE;
7890 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007891
7892 if (evaluate)
7893 {
7894 d = dict_alloc();
7895 if (d == NULL)
7896 return FAIL;
7897 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007898 tvkey.v_type = VAR_UNKNOWN;
7899 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007900
7901 *arg = skipwhite(*arg + 1);
7902 while (**arg != '}' && **arg != NUL)
7903 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007904 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007905 goto failret;
7906 if (**arg != ':')
7907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007908 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007909 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007910 goto failret;
7911 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007912 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007914 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007915 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007916 {
7917 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007918 clear_tv(&tvkey);
7919 goto failret;
7920 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007921 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007922
7923 *arg = skipwhite(*arg + 1);
7924 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7925 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007926 if (evaluate)
7927 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007928 goto failret;
7929 }
7930 if (evaluate)
7931 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007932 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 if (item != NULL)
7934 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007935 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007936 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007937 clear_tv(&tv);
7938 goto failret;
7939 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007940 item = dictitem_alloc(key);
7941 clear_tv(&tvkey);
7942 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007945 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007946 if (dict_add(d, item) == FAIL)
7947 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 }
7949 }
7950
7951 if (**arg == '}')
7952 break;
7953 if (**arg != ',')
7954 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007955 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 goto failret;
7957 }
7958 *arg = skipwhite(*arg + 1);
7959 }
7960
7961 if (**arg != '}')
7962 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007963 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007964failret:
7965 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007966 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 return FAIL;
7968 }
7969
7970 *arg = skipwhite(*arg + 1);
7971 if (evaluate)
7972 {
7973 rettv->v_type = VAR_DICT;
7974 rettv->vval.v_dict = d;
7975 ++d->dv_refcount;
7976 }
7977
7978 return OK;
7979}
7980
Bram Moolenaar17a13432016-01-24 14:22:10 +01007981 static char *
7982get_var_special_name(int nr)
7983{
7984 switch (nr)
7985 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007986 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007987 case VVAL_TRUE: return "v:true";
7988 case VVAL_NONE: return "v:none";
7989 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007990 }
7991 EMSG2(_(e_intern2), "get_var_special_name()");
7992 return "42";
7993}
7994
Bram Moolenaar8c711452005-01-14 21:53:12 +00007995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007996 * Return a string with the string representation of a variable.
7997 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007998 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008000 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008001 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008002 */
8003 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008004echo_string(
8005 typval_T *tv,
8006 char_u **tofree,
8007 char_u *numbuf,
8008 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008009{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 static int recurse = 0;
8011 char_u *r = NULL;
8012
Bram Moolenaar33570922005-01-25 22:26:29 +00008013 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008015 if (!did_echo_string_emsg)
8016 {
8017 /* Only give this message once for a recursive call to avoid
8018 * flooding the user with errors. And stop iterating over lists
8019 * and dicts. */
8020 did_echo_string_emsg = TRUE;
8021 EMSG(_("E724: variable nested too deep for displaying"));
8022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008023 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008024 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008025 }
8026 ++recurse;
8027
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008028 switch (tv->v_type)
8029 {
8030 case VAR_FUNC:
8031 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008032 r = tv->vval.v_string;
8033 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008034
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008035 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008036 {
8037 partial_T *pt = tv->vval.v_partial;
8038 char_u *fname = string_quote(pt == NULL ? NULL
8039 : pt->pt_name, FALSE);
8040 garray_T ga;
8041 int i;
8042 char_u *tf;
8043
8044 ga_init2(&ga, 1, 100);
8045 ga_concat(&ga, (char_u *)"function(");
8046 if (fname != NULL)
8047 {
8048 ga_concat(&ga, fname);
8049 vim_free(fname);
8050 }
8051 if (pt != NULL && pt->pt_argc > 0)
8052 {
8053 ga_concat(&ga, (char_u *)", [");
8054 for (i = 0; i < pt->pt_argc; ++i)
8055 {
8056 if (i > 0)
8057 ga_concat(&ga, (char_u *)", ");
8058 ga_concat(&ga,
8059 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8060 vim_free(tf);
8061 }
8062 ga_concat(&ga, (char_u *)"]");
8063 }
8064 if (pt != NULL && pt->pt_dict != NULL)
8065 {
8066 typval_T dtv;
8067
8068 ga_concat(&ga, (char_u *)", ");
8069 dtv.v_type = VAR_DICT;
8070 dtv.vval.v_dict = pt->pt_dict;
8071 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8072 vim_free(tf);
8073 }
8074 ga_concat(&ga, (char_u *)")");
8075
8076 *tofree = ga.ga_data;
8077 r = *tofree;
8078 break;
8079 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008080
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008081 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008082 if (tv->vval.v_list == NULL)
8083 {
8084 *tofree = NULL;
8085 r = NULL;
8086 }
8087 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8088 {
8089 *tofree = NULL;
8090 r = (char_u *)"[...]";
8091 }
8092 else
8093 {
8094 tv->vval.v_list->lv_copyID = copyID;
8095 *tofree = list2string(tv, copyID);
8096 r = *tofree;
8097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008098 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008099
Bram Moolenaar8c711452005-01-14 21:53:12 +00008100 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008101 if (tv->vval.v_dict == NULL)
8102 {
8103 *tofree = NULL;
8104 r = NULL;
8105 }
8106 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8107 {
8108 *tofree = NULL;
8109 r = (char_u *)"{...}";
8110 }
8111 else
8112 {
8113 tv->vval.v_dict->dv_copyID = copyID;
8114 *tofree = dict2string(tv, copyID);
8115 r = *tofree;
8116 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008117 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008118
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008119 case VAR_STRING:
8120 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008121 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008122 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008123 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008124 *tofree = NULL;
8125 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008127
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008128 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008129#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008130 *tofree = NULL;
8131 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8132 r = numbuf;
8133 break;
8134#endif
8135
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008136 case VAR_SPECIAL:
8137 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008138 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008139 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008140 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008141
Bram Moolenaar8502c702014-06-17 12:51:16 +02008142 if (--recurse == 0)
8143 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008144 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008145}
8146
8147/*
8148 * Return a string with the string representation of a variable.
8149 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8150 * "numbuf" is used for a number.
8151 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008152 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008153 */
8154 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008155tv2string(
8156 typval_T *tv,
8157 char_u **tofree,
8158 char_u *numbuf,
8159 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008160{
8161 switch (tv->v_type)
8162 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008163 case VAR_FUNC:
8164 *tofree = string_quote(tv->vval.v_string, TRUE);
8165 return *tofree;
8166 case VAR_STRING:
8167 *tofree = string_quote(tv->vval.v_string, FALSE);
8168 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008169 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008170#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008171 *tofree = NULL;
8172 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8173 return numbuf;
8174#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008175 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008176 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008177 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008178 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008179 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008180 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008181 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008182 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008184 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008185 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186}
8187
8188/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008189 * Return string "str" in ' quotes, doubling ' characters.
8190 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008191 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008192 */
8193 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008194string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008195{
Bram Moolenaar33570922005-01-25 22:26:29 +00008196 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008197 char_u *p, *r, *s;
8198
Bram Moolenaar33570922005-01-25 22:26:29 +00008199 len = (function ? 13 : 3);
8200 if (str != NULL)
8201 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008202 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 for (p = str; *p != NUL; mb_ptr_adv(p))
8204 if (*p == '\'')
8205 ++len;
8206 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008207 s = r = alloc(len);
8208 if (r != NULL)
8209 {
8210 if (function)
8211 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008212 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008213 r += 10;
8214 }
8215 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008216 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008217 if (str != NULL)
8218 for (p = str; *p != NUL; )
8219 {
8220 if (*p == '\'')
8221 *r++ = '\'';
8222 MB_COPY_CHAR(p, r);
8223 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008224 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008225 if (function)
8226 *r++ = ')';
8227 *r++ = NUL;
8228 }
8229 return s;
8230}
8231
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008232#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008233/*
8234 * Convert the string "text" to a floating point number.
8235 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8236 * this always uses a decimal point.
8237 * Returns the length of the text that was consumed.
8238 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008240string2float(
8241 char_u *text,
8242 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008243{
8244 char *s = (char *)text;
8245 float_T f;
8246
8247 f = strtod(s, &s);
8248 *value = f;
8249 return (int)((char_u *)s - text);
8250}
8251#endif
8252
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 * Get the value of an environment variable.
8255 * "arg" is pointing to the '$'. It is advanced to after the name.
8256 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008257 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 */
8259 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008260get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262 char_u *string = NULL;
8263 int len;
8264 int cc;
8265 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008266 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267
8268 ++*arg;
8269 name = *arg;
8270 len = get_env_len(arg);
8271 if (evaluate)
8272 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008273 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008274 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008275
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008276 cc = name[len];
8277 name[len] = NUL;
8278 /* first try vim_getenv(), fast for normal environment vars */
8279 string = vim_getenv(name, &mustfree);
8280 if (string != NULL && *string != NUL)
8281 {
8282 if (!mustfree)
8283 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008285 else
8286 {
8287 if (mustfree)
8288 vim_free(string);
8289
8290 /* next try expanding things like $VIM and ${HOME} */
8291 string = expand_env_save(name - 1);
8292 if (string != NULL && *string == '$')
8293 {
8294 vim_free(string);
8295 string = NULL;
8296 }
8297 }
8298 name[len] = cc;
8299
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008300 rettv->v_type = VAR_STRING;
8301 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 }
8303
8304 return OK;
8305}
8306
8307/*
8308 * Array with names and number of arguments of all internal functions
8309 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8310 */
8311static struct fst
8312{
8313 char *f_name; /* function name */
8314 char f_min_argc; /* minimal number of arguments */
8315 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008316 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008317 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318} functions[] =
8319{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008320#ifdef FEAT_FLOAT
8321 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008322 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008323#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008324 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008325 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008326 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {"append", 2, 2, f_append},
8328 {"argc", 0, 0, f_argc},
8329 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008330 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008331 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008332#ifdef FEAT_FLOAT
8333 {"asin", 1, 1, f_asin}, /* WJMc */
8334#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008335 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008336 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008337 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008338 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008339 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008340 {"assert_notequal", 2, 3, f_assert_notequal},
8341 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008342 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008343#ifdef FEAT_FLOAT
8344 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008345 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008348 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"bufexists", 1, 1, f_bufexists},
8350 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8351 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8352 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8353 {"buflisted", 1, 1, f_buflisted},
8354 {"bufloaded", 1, 1, f_bufloaded},
8355 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008356 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"bufwinnr", 1, 1, f_bufwinnr},
8358 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008359 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008360 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008361 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008362#ifdef FEAT_FLOAT
8363 {"ceil", 1, 1, f_ceil},
8364#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008365#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008366 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008367 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8368 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008369 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008370 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008371 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008372 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008373 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008374 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008375 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008376 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008377 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8378 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008379 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008380 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008381#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008382 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008383 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008385 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008387#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008388 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008389 {"complete_add", 1, 1, f_complete_add},
8390 {"complete_check", 0, 0, f_complete_check},
8391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008393 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008394#ifdef FEAT_FLOAT
8395 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008396 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008397#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008398 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008400 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008401 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008402 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008404 {"diff_filler", 1, 1, f_diff_filler},
8405 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008406 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008407 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008409 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {"eventhandler", 0, 0, f_eventhandler},
8411 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008412 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008414#ifdef FEAT_FLOAT
8415 {"exp", 1, 1, f_exp},
8416#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008417 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008418 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008419 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8421 {"filereadable", 1, 1, f_filereadable},
8422 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008423 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008424 {"finddir", 1, 3, f_finddir},
8425 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008426#ifdef FEAT_FLOAT
8427 {"float2nr", 1, 1, f_float2nr},
8428 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008429 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008430#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008431 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"fnamemodify", 2, 2, f_fnamemodify},
8433 {"foldclosed", 1, 1, f_foldclosed},
8434 {"foldclosedend", 1, 1, f_foldclosedend},
8435 {"foldlevel", 1, 1, f_foldlevel},
8436 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008437 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008439 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008440 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008441 {"garbagecollect_for_testing", 0, 0, f_garbagecollect_for_testing},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008442 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008443 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008444 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"getchar", 0, 1, f_getchar},
8446 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008447 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {"getcmdline", 0, 0, f_getcmdline},
8449 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008450 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008451 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008452 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008453 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008454 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008455 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {"getfsize", 1, 1, f_getfsize},
8457 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008458 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008459 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008460 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008461 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008462 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008463 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008464 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008465 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008467 {"gettabvar", 2, 3, f_gettabvar},
8468 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {"getwinposx", 0, 0, f_getwinposx},
8470 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008471 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008472 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008473 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008474 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008476 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008477 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008478 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8480 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8481 {"histadd", 2, 2, f_histadd},
8482 {"histdel", 1, 2, f_histdel},
8483 {"histget", 1, 2, f_histget},
8484 {"histnr", 1, 1, f_histnr},
8485 {"hlID", 1, 1, f_hlID},
8486 {"hlexists", 1, 1, f_hlexists},
8487 {"hostname", 0, 0, f_hostname},
8488 {"iconv", 3, 3, f_iconv},
8489 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008490 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008491 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008493 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {"inputrestore", 0, 0, f_inputrestore},
8495 {"inputsave", 0, 0, f_inputsave},
8496 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008497 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008498 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008500 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008501#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8502 {"isnan", 1, 1, f_isnan},
8503#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008504 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008505#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008506 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008507 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008508 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008509 {"job_start", 1, 2, f_job_start},
8510 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008511 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008512#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008513 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008514 {"js_decode", 1, 1, f_js_decode},
8515 {"js_encode", 1, 1, f_js_encode},
8516 {"json_decode", 1, 1, f_json_decode},
8517 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008518 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {"libcall", 3, 3, f_libcall},
8522 {"libcallnr", 3, 3, f_libcallnr},
8523 {"line", 1, 1, f_line},
8524 {"line2byte", 1, 1, f_line2byte},
8525 {"lispindent", 1, 1, f_lispindent},
8526 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008527#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008528 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008529 {"log10", 1, 1, f_log10},
8530#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008531#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008532 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008533#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008534 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008535 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008536 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008537 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008538 {"matchadd", 2, 5, f_matchadd},
8539 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008540 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008541 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008542 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008543 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008544 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008545 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008546 {"max", 1, 1, f_max},
8547 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008548#ifdef vim_mkdir
8549 {"mkdir", 1, 3, f_mkdir},
8550#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008551 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008552#ifdef FEAT_MZSCHEME
8553 {"mzeval", 1, 1, f_mzeval},
8554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008556 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008557 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008558 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008559#ifdef FEAT_PERL
8560 {"perleval", 1, 1, f_perleval},
8561#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008562#ifdef FEAT_FLOAT
8563 {"pow", 2, 2, f_pow},
8564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008566 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008567 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008568#ifdef FEAT_PYTHON3
8569 {"py3eval", 1, 1, f_py3eval},
8570#endif
8571#ifdef FEAT_PYTHON
8572 {"pyeval", 1, 1, f_pyeval},
8573#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008574 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008575 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008576 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008577#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008578 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008579#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008580 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 {"remote_expr", 2, 3, f_remote_expr},
8582 {"remote_foreground", 1, 1, f_remote_foreground},
8583 {"remote_peek", 1, 2, f_remote_peek},
8584 {"remote_read", 1, 1, f_remote_read},
8585 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008586 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008588 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008590 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008591#ifdef FEAT_FLOAT
8592 {"round", 1, 1, f_round},
8593#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008594 {"screenattr", 2, 2, f_screenattr},
8595 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008596 {"screencol", 0, 0, f_screencol},
8597 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008598 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008599 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008600 {"searchpair", 3, 7, f_searchpair},
8601 {"searchpairpos", 3, 7, f_searchpairpos},
8602 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 {"server2client", 2, 2, f_server2client},
8604 {"serverlist", 0, 0, f_serverlist},
8605 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008606 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008608 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008610 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008611 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008612 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008613 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008615 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008616 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008618#ifdef FEAT_CRYPT
8619 {"sha256", 1, 1, f_sha256},
8620#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008621 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008622 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008624#ifdef FEAT_FLOAT
8625 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008626 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008627#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008628 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008629 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008630 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008631 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008632 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008633#ifdef FEAT_FLOAT
8634 {"sqrt", 1, 1, f_sqrt},
8635 {"str2float", 1, 1, f_str2float},
8636#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008637 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008638 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008639 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#ifdef HAVE_STRFTIME
8641 {"strftime", 1, 2, f_strftime},
8642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008643 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {"strlen", 1, 1, f_strlen},
8646 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008647 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008649 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008650 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 {"substitute", 4, 4, f_substitute},
8652 {"synID", 3, 3, f_synID},
8653 {"synIDattr", 2, 3, f_synIDattr},
8654 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008655 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008656 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008657 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008658 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008659 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008660 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008661 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008662 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008663 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008664#ifdef FEAT_FLOAT
8665 {"tan", 1, 1, f_tan},
8666 {"tanh", 1, 1, f_tanh},
8667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008669 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008670#ifdef FEAT_TIMERS
8671 {"timer_start", 2, 3, f_timer_start},
8672 {"timer_stop", 1, 1, f_timer_stop},
8673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {"tolower", 1, 1, f_tolower},
8675 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008676 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008677#ifdef FEAT_FLOAT
8678 {"trunc", 1, 1, f_trunc},
8679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008681 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008682 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008683 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008684 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {"virtcol", 1, 1, f_virtcol},
8686 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008687 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008688 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008689 {"win_getid", 0, 2, f_win_getid},
8690 {"win_gotoid", 1, 1, f_win_gotoid},
8691 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8692 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {"winbufnr", 1, 1, f_winbufnr},
8694 {"wincol", 0, 0, f_wincol},
8695 {"winheight", 1, 1, f_winheight},
8696 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008697 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008699 {"winrestview", 1, 1, f_winrestview},
8700 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008702 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008703 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008704 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705};
8706
8707#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8708
8709/*
8710 * Function given to ExpandGeneric() to obtain the list of internal
8711 * or user defined function names.
8712 */
8713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008714get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 static int intidx = -1;
8717 char_u *name;
8718
8719 if (idx == 0)
8720 intidx = -1;
8721 if (intidx < 0)
8722 {
8723 name = get_user_func_name(xp, idx);
8724 if (name != NULL)
8725 return name;
8726 }
8727 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8728 {
8729 STRCPY(IObuff, functions[intidx].f_name);
8730 STRCAT(IObuff, "(");
8731 if (functions[intidx].f_max_argc == 0)
8732 STRCAT(IObuff, ")");
8733 return IObuff;
8734 }
8735
8736 return NULL;
8737}
8738
8739/*
8740 * Function given to ExpandGeneric() to obtain the list of internal or
8741 * user defined variable or function names.
8742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008744get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
8746 static int intidx = -1;
8747 char_u *name;
8748
8749 if (idx == 0)
8750 intidx = -1;
8751 if (intidx < 0)
8752 {
8753 name = get_function_name(xp, idx);
8754 if (name != NULL)
8755 return name;
8756 }
8757 return get_user_var_name(xp, ++intidx);
8758}
8759
8760#endif /* FEAT_CMDL_COMPL */
8761
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008762#if defined(EBCDIC) || defined(PROTO)
8763/*
8764 * Compare struct fst by function name.
8765 */
8766 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008767compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008768{
8769 struct fst *p1 = (struct fst *)s1;
8770 struct fst *p2 = (struct fst *)s2;
8771
8772 return STRCMP(p1->f_name, p2->f_name);
8773}
8774
8775/*
8776 * Sort the function table by function name.
8777 * The sorting of the table above is ASCII dependant.
8778 * On machines using EBCDIC we have to sort it.
8779 */
8780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008781sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008782{
8783 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8784
8785 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8786}
8787#endif
8788
8789
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790/*
8791 * Find internal function in table above.
8792 * Return index, or -1 if not found
8793 */
8794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008795find_internal_func(
8796 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
8798 int first = 0;
8799 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8800 int cmp;
8801 int x;
8802
8803 /*
8804 * Find the function name in the table. Binary search.
8805 */
8806 while (first <= last)
8807 {
8808 x = first + ((unsigned)(last - first) >> 1);
8809 cmp = STRCMP(name, functions[x].f_name);
8810 if (cmp < 0)
8811 last = x - 1;
8812 else if (cmp > 0)
8813 first = x + 1;
8814 else
8815 return x;
8816 }
8817 return -1;
8818}
8819
8820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008821 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8822 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008823 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8824 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008825 */
8826 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008827deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008828{
Bram Moolenaar33570922005-01-25 22:26:29 +00008829 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008830 int cc;
8831
Bram Moolenaar65639032016-03-16 21:40:30 +01008832 if (partialp != NULL)
8833 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008834
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008835 cc = name[*lenp];
8836 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008837 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008838 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008840 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008841 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008842 {
8843 *lenp = 0;
8844 return (char_u *)""; /* just in case */
8845 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008846 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008848 }
8849
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008850 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8851 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008852 partial_T *pt = v->di_tv.vval.v_partial;
8853
8854 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008855 {
8856 *lenp = 0;
8857 return (char_u *)""; /* just in case */
8858 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008859 if (partialp != NULL)
8860 *partialp = pt;
8861 *lenp = (int)STRLEN(pt->pt_name);
8862 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008863 }
8864
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008865 return name;
8866}
8867
8868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 * Allocate a variable for the result of a function.
8870 * Return OK or FAIL.
8871 */
8872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008873get_func_tv(
8874 char_u *name, /* name of the function */
8875 int len, /* length of "name" */
8876 typval_T *rettv,
8877 char_u **arg, /* argument, pointing to the '(' */
8878 linenr_T firstline, /* first line of range */
8879 linenr_T lastline, /* last line of range */
8880 int *doesrange, /* return: function handled range */
8881 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008882 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008883 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
8885 char_u *argp;
8886 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008887 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 int argcount = 0; /* number of arguments found */
8889
8890 /*
8891 * Get the arguments.
8892 */
8893 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008894 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 {
8896 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8897 if (*argp == ')' || *argp == ',' || *argp == NUL)
8898 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8900 {
8901 ret = FAIL;
8902 break;
8903 }
8904 ++argcount;
8905 if (*argp != ',')
8906 break;
8907 }
8908 if (*argp == ')')
8909 ++argp;
8910 else
8911 ret = FAIL;
8912
8913 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008914 {
8915 int i = 0;
8916
8917 if (get_vim_var_nr(VV_TESTING))
8918 {
8919 /* Prepare for calling garbagecollect_for_testing(), need to know
8920 * what variables are used on the call stack. */
8921 if (funcargs.ga_itemsize == 0)
8922 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8923 for (i = 0; i < argcount; ++i)
8924 if (ga_grow(&funcargs, 1) == OK)
8925 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8926 &argvars[i];
8927 }
8928
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008929 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008930 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008931
8932 funcargs.ga_len -= i;
8933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 {
8936 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008937 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008938 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008939 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941
8942 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
8945 *arg = skipwhite(argp);
8946 return ret;
8947}
8948
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008949#define ERROR_UNKNOWN 0
8950#define ERROR_TOOMANY 1
8951#define ERROR_TOOFEW 2
8952#define ERROR_SCRIPT 3
8953#define ERROR_DICT 4
8954#define ERROR_NONE 5
8955#define ERROR_OTHER 6
8956#define FLEN_FIXED 40
8957
8958/*
8959 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8960 * Change <SNR>123_name() to K_SNR 123_name().
8961 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8962 * (slow).
8963 */
8964 static char_u *
8965fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8966{
8967 int llen;
8968 char_u *fname;
8969 int i;
8970
8971 llen = eval_fname_script(name);
8972 if (llen > 0)
8973 {
8974 fname_buf[0] = K_SPECIAL;
8975 fname_buf[1] = KS_EXTRA;
8976 fname_buf[2] = (int)KE_SNR;
8977 i = 3;
8978 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8979 {
8980 if (current_SID <= 0)
8981 *error = ERROR_SCRIPT;
8982 else
8983 {
8984 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8985 i = (int)STRLEN(fname_buf);
8986 }
8987 }
8988 if (i + STRLEN(name + llen) < FLEN_FIXED)
8989 {
8990 STRCPY(fname_buf + i, name + llen);
8991 fname = fname_buf;
8992 }
8993 else
8994 {
8995 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8996 if (fname == NULL)
8997 *error = ERROR_OTHER;
8998 else
8999 {
9000 *tofree = fname;
9001 mch_memmove(fname, fname_buf, (size_t)i);
9002 STRCPY(fname + i, name + llen);
9003 }
9004 }
9005 }
9006 else
9007 fname = name;
9008 return fname;
9009}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010
9011/*
9012 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009013 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009014 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009016 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009017call_func(
9018 char_u *funcname, /* name of the function */
9019 int len, /* length of "name" */
9020 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009021 int argcount_in, /* number of "argvars" */
9022 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009023 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009024 linenr_T firstline, /* first line of range */
9025 linenr_T lastline, /* last line of range */
9026 int *doesrange, /* return: function handled range */
9027 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009028 partial_T *partial, /* optional, can be NULL */
9029 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
9031 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 int error = ERROR_NONE;
9033 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009036 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009038 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009039 int argcount = argcount_in;
9040 typval_T *argvars = argvars_in;
9041 dict_T *selfdict = selfdict_in;
9042 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9043 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009044
9045 /* Make a copy of the name, if it comes from a funcref variable it could
9046 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009047 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009048 if (name == NULL)
9049 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009051 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052
9053 *doesrange = FALSE;
9054
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009055 if (partial != NULL)
9056 {
9057 if (partial->pt_dict != NULL)
9058 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009059 /* When the function has a partial with a dict and there is a dict
9060 * argument, use the dict argument. That is backwards compatible.
9061 */
9062 if (selfdict_in == NULL)
9063 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009064 }
9065 if (error == ERROR_NONE && partial->pt_argc > 0)
9066 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009067 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9068 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9069 for (i = 0; i < argcount_in; ++i)
9070 argv[i + argv_clear] = argvars_in[i];
9071 argvars = argv;
9072 argcount = partial->pt_argc + argcount_in;
9073 }
9074 }
9075
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076
9077 /* execute the function if no errors detected and executing */
9078 if (evaluate && error == ERROR_NONE)
9079 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009080 char_u *rfname = fname;
9081
9082 /* Ignore "g:" before a function name. */
9083 if (fname[0] == 'g' && fname[1] == ':')
9084 rfname = fname + 2;
9085
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009086 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9087 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 error = ERROR_UNKNOWN;
9089
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009090 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 {
9092 /*
9093 * User defined function.
9094 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009095 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009096
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009098 /* Trigger FuncUndefined event, may load the function. */
9099 if (fp == NULL
9100 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009101 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009102 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009104 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009105 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 }
9107#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009108 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009109 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009110 {
9111 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009112 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009113 }
9114
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 if (fp != NULL)
9116 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009117 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009119 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009121 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009123 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009124 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 else
9126 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009127 int did_save_redo = FALSE;
9128
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 /*
9130 * Call the user function.
9131 * Save and restore search patterns, script variables and
9132 * redo buffer.
9133 */
9134 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009135#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009136 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009137#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009138 {
9139 saveRedobuff();
9140 did_save_redo = TRUE;
9141 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009142 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009144 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009145 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9146 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9147 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009148 /* Function was unreferenced while being used, free it
9149 * now. */
9150 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009151 if (did_save_redo)
9152 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 restore_search_patterns();
9154 error = ERROR_NONE;
9155 }
9156 }
9157 }
9158 else
9159 {
9160 /*
9161 * Find the function name in the table, call its implementation.
9162 */
9163 i = find_internal_func(fname);
9164 if (i >= 0)
9165 {
9166 if (argcount < functions[i].f_min_argc)
9167 error = ERROR_TOOFEW;
9168 else if (argcount > functions[i].f_max_argc)
9169 error = ERROR_TOOMANY;
9170 else
9171 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009172 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 error = ERROR_NONE;
9175 }
9176 }
9177 }
9178 /*
9179 * The function call (or "FuncUndefined" autocommand sequence) might
9180 * have been aborted by an error, an interrupt, or an explicitly thrown
9181 * exception that has not been caught so far. This situation can be
9182 * tested for by calling aborting(). For an error in an internal
9183 * function or for the "E132" error in call_user_func(), however, the
9184 * throw point at which the "force_abort" flag (temporarily reset by
9185 * emsg()) is normally updated has not been reached yet. We need to
9186 * update that flag first to make aborting() reliable.
9187 */
9188 update_force_abort();
9189 }
9190 if (error == ERROR_NONE)
9191 ret = OK;
9192
9193 /*
9194 * Report an error unless the argument evaluation or function call has been
9195 * cancelled due to an aborting error, an interrupt, or an exception.
9196 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009197 if (!aborting())
9198 {
9199 switch (error)
9200 {
9201 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009202 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009203 break;
9204 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009205 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009206 break;
9207 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009208 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009209 name);
9210 break;
9211 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009212 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009213 name);
9214 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009215 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009216 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009217 name);
9218 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009219 }
9220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009222 while (argv_clear > 0)
9223 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009224 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009225 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226
9227 return ret;
9228}
9229
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009230/*
9231 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009232 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009233 */
9234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009235emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009236{
9237 char_u *p;
9238
9239 if (*name == K_SPECIAL)
9240 p = concat_str((char_u *)"<SNR>", name + 3);
9241 else
9242 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009243 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009244 if (p != name)
9245 vim_free(p);
9246}
9247
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009248/*
9249 * Return TRUE for a non-zero Number and a non-empty String.
9250 */
9251 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009252non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009253{
9254 return ((argvars[0].v_type == VAR_NUMBER
9255 && argvars[0].vval.v_number != 0)
9256 || (argvars[0].v_type == VAR_STRING
9257 && argvars[0].vval.v_string != NULL
9258 && *argvars[0].vval.v_string != NUL));
9259}
9260
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261/*********************************************
9262 * Implementation of the built-in functions
9263 */
9264
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009265#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009266static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009267
9268/*
9269 * Get the float value of "argvars[0]" into "f".
9270 * Returns FAIL when the argument is not a Number or Float.
9271 */
9272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009273get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009274{
9275 if (argvars[0].v_type == VAR_FLOAT)
9276 {
9277 *f = argvars[0].vval.v_float;
9278 return OK;
9279 }
9280 if (argvars[0].v_type == VAR_NUMBER)
9281 {
9282 *f = (float_T)argvars[0].vval.v_number;
9283 return OK;
9284 }
9285 EMSG(_("E808: Number or Float required"));
9286 return FAIL;
9287}
9288
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009289/*
9290 * "abs(expr)" function
9291 */
9292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009293f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009294{
9295 if (argvars[0].v_type == VAR_FLOAT)
9296 {
9297 rettv->v_type = VAR_FLOAT;
9298 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9299 }
9300 else
9301 {
9302 varnumber_T n;
9303 int error = FALSE;
9304
9305 n = get_tv_number_chk(&argvars[0], &error);
9306 if (error)
9307 rettv->vval.v_number = -1;
9308 else if (n > 0)
9309 rettv->vval.v_number = n;
9310 else
9311 rettv->vval.v_number = -n;
9312 }
9313}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009314
9315/*
9316 * "acos()" function
9317 */
9318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009319f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009320{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009321 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009322
9323 rettv->v_type = VAR_FLOAT;
9324 if (get_float_arg(argvars, &f) == OK)
9325 rettv->vval.v_float = acos(f);
9326 else
9327 rettv->vval.v_float = 0.0;
9328}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009329#endif
9330
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009332 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 */
9334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009335f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336{
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009340 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009342 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009343 && !tv_check_lock(l->lv_lock,
9344 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009345 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009347 }
9348 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009349 EMSG(_(e_listreq));
9350}
9351
9352/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009353 * "alloc_fail(id, countdown, repeat)" function
9354 */
9355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009356f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009357{
9358 if (argvars[0].v_type != VAR_NUMBER
9359 || argvars[0].vval.v_number <= 0
9360 || argvars[1].v_type != VAR_NUMBER
9361 || argvars[1].vval.v_number < 0
9362 || argvars[2].v_type != VAR_NUMBER)
9363 EMSG(_(e_invarg));
9364 else
9365 {
9366 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009367 if (alloc_fail_id >= aid_last)
9368 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009369 alloc_fail_countdown = argvars[1].vval.v_number;
9370 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009371 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009372 }
9373}
9374
9375/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009376 * "and(expr, expr)" function
9377 */
9378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009379f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009380{
9381 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9382 & get_tv_number_chk(&argvars[1], NULL);
9383}
9384
9385/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009386 * "append(lnum, string/list)" function
9387 */
9388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009389f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009390{
9391 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009392 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009393 list_T *l = NULL;
9394 listitem_T *li = NULL;
9395 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009396 long added = 0;
9397
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009398 /* When coming here from Insert mode, sync undo, so that this can be
9399 * undone separately from what was previously inserted. */
9400 if (u_sync_once == 2)
9401 {
9402 u_sync_once = 1; /* notify that u_sync() was called */
9403 u_sync(TRUE);
9404 }
9405
Bram Moolenaar0d660222005-01-07 21:51:51 +00009406 lnum = get_tv_lnum(argvars);
9407 if (lnum >= 0
9408 && lnum <= curbuf->b_ml.ml_line_count
9409 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009410 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009411 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009412 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009413 l = argvars[1].vval.v_list;
9414 if (l == NULL)
9415 return;
9416 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009417 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009418 for (;;)
9419 {
9420 if (l == NULL)
9421 tv = &argvars[1]; /* append a string */
9422 else if (li == NULL)
9423 break; /* end of list */
9424 else
9425 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009426 line = get_tv_string_chk(tv);
9427 if (line == NULL) /* type error */
9428 {
9429 rettv->vval.v_number = 1; /* Failed */
9430 break;
9431 }
9432 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009433 ++added;
9434 if (l == NULL)
9435 break;
9436 li = li->li_next;
9437 }
9438
9439 appended_lines_mark(lnum, added);
9440 if (curwin->w_cursor.lnum > lnum)
9441 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009443 else
9444 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445}
9446
9447/*
9448 * "argc()" function
9449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009451f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454}
9455
9456/*
9457 * "argidx()" function
9458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009460f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463}
9464
9465/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009466 * "arglistid()" function
9467 */
9468 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009469f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009470{
9471 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009472
9473 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009474 wp = find_tabwin(&argvars[0], &argvars[1]);
9475 if (wp != NULL)
9476 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009477}
9478
9479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 * "argv(nr)" function
9481 */
9482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009483f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485 int idx;
9486
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009487 if (argvars[0].v_type != VAR_UNKNOWN)
9488 {
9489 idx = get_tv_number_chk(&argvars[0], NULL);
9490 if (idx >= 0 && idx < ARGCOUNT)
9491 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9492 else
9493 rettv->vval.v_string = NULL;
9494 rettv->v_type = VAR_STRING;
9495 }
9496 else if (rettv_list_alloc(rettv) == OK)
9497 for (idx = 0; idx < ARGCOUNT; ++idx)
9498 list_append_string(rettv->vval.v_list,
9499 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500}
9501
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009502typedef enum
9503{
9504 ASSERT_EQUAL,
9505 ASSERT_NOTEQUAL,
9506 ASSERT_MATCH,
9507 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009508 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009509} assert_type_T;
9510
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009511static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009512static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009513static void assert_error(garray_T *gap);
9514static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009515
9516/*
9517 * Prepare "gap" for an assert error and add the sourcing position.
9518 */
9519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009520prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009521{
9522 char buf[NUMBUFLEN];
9523
9524 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009525 if (sourcing_name != NULL)
9526 {
9527 ga_concat(gap, sourcing_name);
9528 if (sourcing_lnum > 0)
9529 ga_concat(gap, (char_u *)" ");
9530 }
9531 if (sourcing_lnum > 0)
9532 {
9533 sprintf(buf, "line %ld", (long)sourcing_lnum);
9534 ga_concat(gap, (char_u *)buf);
9535 }
9536 if (sourcing_name != NULL || sourcing_lnum > 0)
9537 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009538}
9539
9540/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009541 * Append "str" to "gap", escaping unprintable characters.
9542 * Changes NL to \n, CR to \r, etc.
9543 */
9544 static void
9545ga_concat_esc(garray_T *gap, char_u *str)
9546{
9547 char_u *p;
9548 char_u buf[NUMBUFLEN];
9549
Bram Moolenaarf1551962016-03-15 12:55:58 +01009550 if (str == NULL)
9551 {
9552 ga_concat(gap, (char_u *)"NULL");
9553 return;
9554 }
9555
Bram Moolenaar23689172016-02-15 22:37:37 +01009556 for (p = str; *p != NUL; ++p)
9557 switch (*p)
9558 {
9559 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9560 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9561 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9562 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9563 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9564 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9565 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9566 default:
9567 if (*p < ' ')
9568 {
9569 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9570 ga_concat(gap, buf);
9571 }
9572 else
9573 ga_append(gap, *p);
9574 break;
9575 }
9576}
9577
9578/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009579 * Fill "gap" with information about an assert error.
9580 */
9581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009582fill_assert_error(
9583 garray_T *gap,
9584 typval_T *opt_msg_tv,
9585 char_u *exp_str,
9586 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009587 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009588 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009589{
9590 char_u numbuf[NUMBUFLEN];
9591 char_u *tofree;
9592
9593 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9594 {
9595 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9596 vim_free(tofree);
9597 }
9598 else
9599 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009600 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009601 ga_concat(gap, (char_u *)"Pattern ");
9602 else
9603 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009604 if (exp_str == NULL)
9605 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009606 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009607 vim_free(tofree);
9608 }
9609 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009610 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009611 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009612 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009613 else if (atype == ASSERT_NOTMATCH)
9614 ga_concat(gap, (char_u *)" does match ");
9615 else if (atype == ASSERT_NOTEQUAL)
9616 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009617 else
9618 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009619 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009620 vim_free(tofree);
9621 }
9622}
Bram Moolenaar43345542015-11-29 17:35:35 +01009623
9624/*
9625 * Add an assert error to v:errors.
9626 */
9627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009628assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009629{
9630 struct vimvar *vp = &vimvars[VV_ERRORS];
9631
9632 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9633 /* Make sure v:errors is a list. */
9634 set_vim_var_list(VV_ERRORS, list_alloc());
9635 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9636}
9637
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009638 static void
9639assert_equal_common(typval_T *argvars, assert_type_T atype)
9640{
9641 garray_T ga;
9642
9643 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9644 != (atype == ASSERT_EQUAL))
9645 {
9646 prepare_assert_error(&ga);
9647 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9648 atype);
9649 assert_error(&ga);
9650 ga_clear(&ga);
9651 }
9652}
9653
Bram Moolenaar43345542015-11-29 17:35:35 +01009654/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009655 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009656 */
9657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009658f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009659{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009660 assert_equal_common(argvars, ASSERT_EQUAL);
9661}
Bram Moolenaar43345542015-11-29 17:35:35 +01009662
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009663/*
9664 * "assert_notequal(expected, actual[, msg])" function
9665 */
9666 static void
9667f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9668{
9669 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009670}
9671
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009672/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009673 * "assert_exception(string[, msg])" function
9674 */
9675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009676f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009677{
9678 garray_T ga;
9679 char *error;
9680
9681 error = (char *)get_tv_string_chk(&argvars[0]);
9682 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9683 {
9684 prepare_assert_error(&ga);
9685 ga_concat(&ga, (char_u *)"v:exception is not set");
9686 assert_error(&ga);
9687 ga_clear(&ga);
9688 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009689 else if (error != NULL
9690 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009691 {
9692 prepare_assert_error(&ga);
9693 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009694 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009695 assert_error(&ga);
9696 ga_clear(&ga);
9697 }
9698}
9699
9700/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009701 * "assert_fails(cmd [, error])" function
9702 */
9703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009704f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009705{
9706 char_u *cmd = get_tv_string_chk(&argvars[0]);
9707 garray_T ga;
9708
9709 called_emsg = FALSE;
9710 suppress_errthrow = TRUE;
9711 emsg_silent = TRUE;
9712 do_cmdline_cmd(cmd);
9713 if (!called_emsg)
9714 {
9715 prepare_assert_error(&ga);
9716 ga_concat(&ga, (char_u *)"command did not fail: ");
9717 ga_concat(&ga, cmd);
9718 assert_error(&ga);
9719 ga_clear(&ga);
9720 }
9721 else if (argvars[1].v_type != VAR_UNKNOWN)
9722 {
9723 char_u buf[NUMBUFLEN];
9724 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9725
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009726 if (error == NULL
9727 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009728 {
9729 prepare_assert_error(&ga);
9730 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009731 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009732 assert_error(&ga);
9733 ga_clear(&ga);
9734 }
9735 }
9736
9737 called_emsg = FALSE;
9738 suppress_errthrow = FALSE;
9739 emsg_silent = FALSE;
9740 emsg_on_display = FALSE;
9741 set_vim_var_string(VV_ERRMSG, NULL, 0);
9742}
9743
9744/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009745 * Common for assert_true() and assert_false().
9746 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009749{
9750 int error = FALSE;
9751 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009752
Bram Moolenaar37127922016-02-06 20:29:28 +01009753 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009754 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009755 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009756 if (argvars[0].v_type != VAR_NUMBER
9757 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9758 || error)
9759 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009760 prepare_assert_error(&ga);
9761 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009762 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009763 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009764 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009765 ga_clear(&ga);
9766 }
9767}
9768
9769/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009770 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009771 */
9772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009773f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009774{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009775 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009776}
9777
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009778 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009779assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009780{
9781 garray_T ga;
9782 char_u buf1[NUMBUFLEN];
9783 char_u buf2[NUMBUFLEN];
9784 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9785 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9786
Bram Moolenaar72188e92016-03-28 22:48:29 +02009787 if (pat == NULL || text == NULL)
9788 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009789 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009790 {
9791 prepare_assert_error(&ga);
9792 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009793 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009794 assert_error(&ga);
9795 ga_clear(&ga);
9796 }
9797}
9798
9799/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009800 * "assert_match(pattern, actual[, msg])" function
9801 */
9802 static void
9803f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9804{
9805 assert_match_common(argvars, ASSERT_MATCH);
9806}
9807
9808/*
9809 * "assert_notmatch(pattern, actual[, msg])" function
9810 */
9811 static void
9812f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9813{
9814 assert_match_common(argvars, ASSERT_NOTMATCH);
9815}
9816
9817/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009818 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009819 */
9820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009821f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009822{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009823 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009824}
9825
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009826#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009827/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009828 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009829 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009831f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009832{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009833 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009834
9835 rettv->v_type = VAR_FLOAT;
9836 if (get_float_arg(argvars, &f) == OK)
9837 rettv->vval.v_float = asin(f);
9838 else
9839 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009840}
9841
9842/*
9843 * "atan()" function
9844 */
9845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009846f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009847{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009848 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009849
9850 rettv->v_type = VAR_FLOAT;
9851 if (get_float_arg(argvars, &f) == OK)
9852 rettv->vval.v_float = atan(f);
9853 else
9854 rettv->vval.v_float = 0.0;
9855}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009856
9857/*
9858 * "atan2()" function
9859 */
9860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009861f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009862{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009863 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009864
9865 rettv->v_type = VAR_FLOAT;
9866 if (get_float_arg(argvars, &fx) == OK
9867 && get_float_arg(&argvars[1], &fy) == OK)
9868 rettv->vval.v_float = atan2(fx, fy);
9869 else
9870 rettv->vval.v_float = 0.0;
9871}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009872#endif
9873
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874/*
9875 * "browse(save, title, initdir, default)" function
9876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009878f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880#ifdef FEAT_BROWSE
9881 int save;
9882 char_u *title;
9883 char_u *initdir;
9884 char_u *defname;
9885 char_u buf[NUMBUFLEN];
9886 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009887 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009889 save = get_tv_number_chk(&argvars[0], &error);
9890 title = get_tv_string_chk(&argvars[1]);
9891 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9892 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 if (error || title == NULL || initdir == NULL || defname == NULL)
9895 rettv->vval.v_string = NULL;
9896 else
9897 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009898 do_browse(save ? BROWSE_SAVE : 0,
9899 title, defname, NULL, initdir, NULL, curbuf);
9900#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009902#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009904}
9905
9906/*
9907 * "browsedir(title, initdir)" function
9908 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009910f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009911{
9912#ifdef FEAT_BROWSE
9913 char_u *title;
9914 char_u *initdir;
9915 char_u buf[NUMBUFLEN];
9916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009917 title = get_tv_string_chk(&argvars[0]);
9918 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009920 if (title == NULL || initdir == NULL)
9921 rettv->vval.v_string = NULL;
9922 else
9923 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009924 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009928 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929}
9930
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009931static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009932
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933/*
9934 * Find a buffer by number or exact name.
9935 */
9936 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009937find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938{
9939 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009941 if (avar->v_type == VAR_NUMBER)
9942 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009943 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009945 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009946 if (buf == NULL)
9947 {
9948 /* No full path name match, try a match with a URL or a "nofile"
9949 * buffer, these don't use the full path. */
9950 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9951 if (buf->b_fname != NULL
9952 && (path_with_url(buf->b_fname)
9953#ifdef FEAT_QUICKFIX
9954 || bt_nofile(buf)
9955#endif
9956 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009957 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009958 break;
9959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 }
9961 return buf;
9962}
9963
9964/*
9965 * "bufexists(expr)" function
9966 */
9967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009968f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009970 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971}
9972
9973/*
9974 * "buflisted(expr)" function
9975 */
9976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009977f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
9979 buf_T *buf;
9980
9981 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983}
9984
9985/*
9986 * "bufloaded(expr)" function
9987 */
9988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009989f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990{
9991 buf_T *buf;
9992
9993 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
9996
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009997 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009998buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 int save_magic;
10001 char_u *save_cpo;
10002 buf_T *buf;
10003
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10005 save_magic = p_magic;
10006 p_magic = TRUE;
10007 save_cpo = p_cpo;
10008 p_cpo = (char_u *)"";
10009
10010 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010011 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012
10013 p_magic = save_magic;
10014 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010015 return buf;
10016}
10017
10018/*
10019 * Get buffer by number or pattern.
10020 */
10021 static buf_T *
10022get_buf_tv(typval_T *tv, int curtab_only)
10023{
10024 char_u *name = tv->vval.v_string;
10025 buf_T *buf;
10026
10027 if (tv->v_type == VAR_NUMBER)
10028 return buflist_findnr((int)tv->vval.v_number);
10029 if (tv->v_type != VAR_STRING)
10030 return NULL;
10031 if (name == NULL || *name == NUL)
10032 return curbuf;
10033 if (name[0] == '$' && name[1] == NUL)
10034 return lastbuf;
10035
10036 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037
10038 /* If not found, try expanding the name, like done for bufexists(). */
10039 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
10042 return buf;
10043}
10044
10045/*
10046 * "bufname(expr)" function
10047 */
10048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010049f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050{
10051 buf_T *buf;
10052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010053 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010055 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 --emsg_off;
10062}
10063
10064/*
10065 * "bufnr(expr)" function
10066 */
10067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010068f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069{
10070 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010071 int error = FALSE;
10072 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010076 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010077 --emsg_off;
10078
10079 /* If the buffer isn't found and the second argument is not zero create a
10080 * new buffer. */
10081 if (buf == NULL
10082 && argvars[1].v_type != VAR_UNKNOWN
10083 && get_tv_number_chk(&argvars[1], &error) != 0
10084 && !error
10085 && (name = get_tv_string_chk(&argvars[0])) != NULL
10086 && !error)
10087 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10088
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093}
10094
10095/*
10096 * "bufwinnr(nr)" function
10097 */
10098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010099f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101#ifdef FEAT_WINDOWS
10102 win_T *wp;
10103 int winnr = 0;
10104#endif
10105 buf_T *buf;
10106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010109 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110#ifdef FEAT_WINDOWS
10111 for (wp = firstwin; wp; wp = wp->w_next)
10112 {
10113 ++winnr;
10114 if (wp->w_buffer == buf)
10115 break;
10116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#endif
10121 --emsg_off;
10122}
10123
10124/*
10125 * "byte2line(byte)" function
10126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010128f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129{
10130#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#else
10133 long boff = 0;
10134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 (linenr_T)0, &boff);
10141#endif
10142}
10143
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010145byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010146{
10147#ifdef FEAT_MBYTE
10148 char_u *t;
10149#endif
10150 char_u *str;
10151 long idx;
10152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 str = get_tv_string_chk(&argvars[0]);
10154 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010156 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010157 return;
10158
10159#ifdef FEAT_MBYTE
10160 t = str;
10161 for ( ; idx > 0; idx--)
10162 {
10163 if (*t == NUL) /* EOL reached */
10164 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010165 if (enc_utf8 && comp)
10166 t += utf_ptr2len(t);
10167 else
10168 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010169 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010170 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010171#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010172 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010173 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010174#endif
10175}
10176
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010177/*
10178 * "byteidx()" function
10179 */
10180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010181f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010182{
10183 byteidx(argvars, rettv, FALSE);
10184}
10185
10186/*
10187 * "byteidxcomp()" function
10188 */
10189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010190f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010191{
10192 byteidx(argvars, rettv, TRUE);
10193}
10194
Bram Moolenaardb913952012-06-29 12:54:53 +020010195 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010196func_call(
10197 char_u *name,
10198 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010199 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010200 dict_T *selfdict,
10201 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010202{
10203 listitem_T *item;
10204 typval_T argv[MAX_FUNC_ARGS + 1];
10205 int argc = 0;
10206 int dummy;
10207 int r = 0;
10208
10209 for (item = args->vval.v_list->lv_first; item != NULL;
10210 item = item->li_next)
10211 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010212 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010213 {
10214 EMSG(_("E699: Too many arguments"));
10215 break;
10216 }
10217 /* Make a copy of each argument. This is needed to be able to set
10218 * v_lock to VAR_FIXED in the copy without changing the original list.
10219 */
10220 copy_tv(&item->li_tv, &argv[argc++]);
10221 }
10222
10223 if (item == NULL)
10224 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10225 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010226 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010227
10228 /* Free the arguments. */
10229 while (argc > 0)
10230 clear_tv(&argv[--argc]);
10231
10232 return r;
10233}
10234
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010235/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010236 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010237 */
10238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010239f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010240{
10241 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010242 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010244
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010245 if (argvars[1].v_type != VAR_LIST)
10246 {
10247 EMSG(_(e_listreq));
10248 return;
10249 }
10250 if (argvars[1].vval.v_list == NULL)
10251 return;
10252
10253 if (argvars[0].v_type == VAR_FUNC)
10254 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010255 else if (argvars[0].v_type == VAR_PARTIAL)
10256 {
10257 partial = argvars[0].vval.v_partial;
10258 func = partial->pt_name;
10259 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010260 else
10261 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 if (*func == NUL)
10263 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010264
Bram Moolenaare9a41262005-01-15 22:18:47 +000010265 if (argvars[2].v_type != VAR_UNKNOWN)
10266 {
10267 if (argvars[2].v_type != VAR_DICT)
10268 {
10269 EMSG(_(e_dictreq));
10270 return;
10271 }
10272 selfdict = argvars[2].vval.v_dict;
10273 }
10274
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010275 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276}
10277
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010278#ifdef FEAT_FLOAT
10279/*
10280 * "ceil({float})" function
10281 */
10282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010283f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010284{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010285 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010286
10287 rettv->v_type = VAR_FLOAT;
10288 if (get_float_arg(argvars, &f) == OK)
10289 rettv->vval.v_float = ceil(f);
10290 else
10291 rettv->vval.v_float = 0.0;
10292}
10293#endif
10294
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010295#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010296/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010297 * "ch_close()" function
10298 */
10299 static void
10300f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10301{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010302 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010303
10304 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010305 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010306 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010307 channel_clear(channel);
10308 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010309}
10310
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010311/*
10312 * "ch_getbufnr()" function
10313 */
10314 static void
10315f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10316{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010317 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010318
10319 rettv->vval.v_number = -1;
10320 if (channel != NULL)
10321 {
10322 char_u *what = get_tv_string(&argvars[1]);
10323 int part;
10324
10325 if (STRCMP(what, "err") == 0)
10326 part = PART_ERR;
10327 else if (STRCMP(what, "out") == 0)
10328 part = PART_OUT;
10329 else if (STRCMP(what, "in") == 0)
10330 part = PART_IN;
10331 else
10332 part = PART_SOCK;
10333 if (channel->ch_part[part].ch_buffer != NULL)
10334 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10335 }
10336}
10337
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010338/*
10339 * "ch_getjob()" function
10340 */
10341 static void
10342f_ch_getjob(typval_T *argvars, typval_T *rettv)
10343{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010344 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010345
10346 if (channel != NULL)
10347 {
10348 rettv->v_type = VAR_JOB;
10349 rettv->vval.v_job = channel->ch_job;
10350 if (channel->ch_job != NULL)
10351 ++channel->ch_job->jv_refcount;
10352 }
10353}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010354
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010355/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010356 * "ch_info()" function
10357 */
10358 static void
10359f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10360{
10361 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10362
10363 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10364 channel_info(channel, rettv->vval.v_dict);
10365}
10366
10367/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010368 * "ch_log()" function
10369 */
10370 static void
10371f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10372{
10373 char_u *msg = get_tv_string(&argvars[0]);
10374 channel_T *channel = NULL;
10375
10376 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010377 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010378
10379 ch_log(channel, (char *)msg);
10380}
10381
10382/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010383 * "ch_logfile()" function
10384 */
10385 static void
10386f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10387{
10388 char_u *fname;
10389 char_u *opt = (char_u *)"";
10390 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010391
10392 fname = get_tv_string(&argvars[0]);
10393 if (argvars[1].v_type == VAR_STRING)
10394 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010395 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010396}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010397
10398/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010399 * "ch_open()" function
10400 */
10401 static void
10402f_ch_open(typval_T *argvars, typval_T *rettv)
10403{
Bram Moolenaar77073442016-02-13 23:23:53 +010010404 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010405 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010406}
10407
10408/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010409 * "ch_read()" function
10410 */
10411 static void
10412f_ch_read(typval_T *argvars, typval_T *rettv)
10413{
10414 common_channel_read(argvars, rettv, FALSE);
10415}
10416
10417/*
10418 * "ch_readraw()" function
10419 */
10420 static void
10421f_ch_readraw(typval_T *argvars, typval_T *rettv)
10422{
10423 common_channel_read(argvars, rettv, TRUE);
10424}
10425
10426/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010427 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010428 */
10429 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010430f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10431{
10432 ch_expr_common(argvars, rettv, TRUE);
10433}
10434
10435/*
10436 * "ch_sendexpr()" function
10437 */
10438 static void
10439f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10440{
10441 ch_expr_common(argvars, rettv, FALSE);
10442}
10443
10444/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010445 * "ch_evalraw()" function
10446 */
10447 static void
10448f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10449{
10450 ch_raw_common(argvars, rettv, TRUE);
10451}
10452
10453/*
10454 * "ch_sendraw()" function
10455 */
10456 static void
10457f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10458{
10459 ch_raw_common(argvars, rettv, FALSE);
10460}
10461
10462/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010463 * "ch_setoptions()" function
10464 */
10465 static void
10466f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10467{
10468 channel_T *channel;
10469 jobopt_T opt;
10470
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010471 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010472 if (channel == NULL)
10473 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010474 clear_job_options(&opt);
10475 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010476 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10477 channel_set_options(channel, &opt);
10478 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010479}
10480
10481/*
10482 * "ch_status()" function
10483 */
10484 static void
10485f_ch_status(typval_T *argvars, typval_T *rettv)
10486{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010487 channel_T *channel;
10488
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010489 /* return an empty string by default */
10490 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010491 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010492
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010493 channel = get_channel_arg(&argvars[0], FALSE);
10494 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010495}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010496#endif
10497
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010498/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010499 * "changenr()" function
10500 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010502f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010503{
10504 rettv->vval.v_number = curbuf->b_u_seq_cur;
10505}
10506
10507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 * "char2nr(string)" function
10509 */
10510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010511f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512{
10513#ifdef FEAT_MBYTE
10514 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010515 {
10516 int utf8 = 0;
10517
10518 if (argvars[1].v_type != VAR_UNKNOWN)
10519 utf8 = get_tv_number_chk(&argvars[1], NULL);
10520
10521 if (utf8)
10522 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10523 else
10524 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 else
10527#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529}
10530
10531/*
10532 * "cindent(lnum)" function
10533 */
10534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010535f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536{
10537#ifdef FEAT_CINDENT
10538 pos_T pos;
10539 linenr_T lnum;
10540
10541 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10544 {
10545 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 curwin->w_cursor = pos;
10548 }
10549 else
10550#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552}
10553
10554/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010555 * "clearmatches()" function
10556 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010558f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010559{
10560#ifdef FEAT_SEARCH_EXTRA
10561 clear_matches(curwin);
10562#endif
10563}
10564
10565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 * "col(string)" function
10567 */
10568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010569f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570{
10571 colnr_T col = 0;
10572 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010573 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010575 fp = var2fpos(&argvars[0], FALSE, &fnum);
10576 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 {
10578 if (fp->col == MAXCOL)
10579 {
10580 /* '> can be MAXCOL, get the length of the line then */
10581 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010582 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 else
10584 col = MAXCOL;
10585 }
10586 else
10587 {
10588 col = fp->col + 1;
10589#ifdef FEAT_VIRTUALEDIT
10590 /* col(".") when the cursor is on the NUL at the end of the line
10591 * because of "coladd" can be seen as an extra column. */
10592 if (virtual_active() && fp == &curwin->w_cursor)
10593 {
10594 char_u *p = ml_get_cursor();
10595
10596 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10597 curwin->w_virtcol - curwin->w_cursor.coladd))
10598 {
10599# ifdef FEAT_MBYTE
10600 int l;
10601
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010602 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 col += l;
10604# else
10605 if (*p != NUL && p[1] == NUL)
10606 ++col;
10607# endif
10608 }
10609 }
10610#endif
10611 }
10612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614}
10615
Bram Moolenaar572cb562005-08-05 21:35:02 +000010616#if defined(FEAT_INS_EXPAND)
10617/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010618 * "complete()" function
10619 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010621f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010622{
10623 int startcol;
10624
10625 if ((State & INSERT) == 0)
10626 {
10627 EMSG(_("E785: complete() can only be used in Insert mode"));
10628 return;
10629 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010630
10631 /* Check for undo allowed here, because if something was already inserted
10632 * the line was already saved for undo and this check isn't done. */
10633 if (!undo_allowed())
10634 return;
10635
Bram Moolenaarade00832006-03-10 21:46:58 +000010636 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10637 {
10638 EMSG(_(e_invarg));
10639 return;
10640 }
10641
10642 startcol = get_tv_number_chk(&argvars[0], NULL);
10643 if (startcol <= 0)
10644 return;
10645
10646 set_completion(startcol - 1, argvars[1].vval.v_list);
10647}
10648
10649/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010650 * "complete_add()" function
10651 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010653f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010654{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010655 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010656}
10657
10658/*
10659 * "complete_check()" function
10660 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010662f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010663{
10664 int saved = RedrawingDisabled;
10665
10666 RedrawingDisabled = 0;
10667 ins_compl_check_keys(0);
10668 rettv->vval.v_number = compl_interrupted;
10669 RedrawingDisabled = saved;
10670}
10671#endif
10672
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673/*
10674 * "confirm(message, buttons[, default [, type]])" function
10675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010677f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
10679#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10680 char_u *message;
10681 char_u *buttons = NULL;
10682 char_u buf[NUMBUFLEN];
10683 char_u buf2[NUMBUFLEN];
10684 int def = 1;
10685 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010686 char_u *typestr;
10687 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 message = get_tv_string_chk(&argvars[0]);
10690 if (message == NULL)
10691 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010692 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010694 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10695 if (buttons == NULL)
10696 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010697 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010700 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010702 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10703 if (typestr == NULL)
10704 error = TRUE;
10705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 switch (TOUPPER_ASC(*typestr))
10708 {
10709 case 'E': type = VIM_ERROR; break;
10710 case 'Q': type = VIM_QUESTION; break;
10711 case 'I': type = VIM_INFO; break;
10712 case 'W': type = VIM_WARNING; break;
10713 case 'G': type = VIM_GENERIC; break;
10714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 }
10716 }
10717 }
10718 }
10719
10720 if (buttons == NULL || *buttons == NUL)
10721 buttons = (char_u *)_("&Ok");
10722
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010723 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010724 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010725 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726#endif
10727}
10728
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010729/*
10730 * "copy()" function
10731 */
10732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010733f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010734{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010735 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010736}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010738#ifdef FEAT_FLOAT
10739/*
10740 * "cos()" function
10741 */
10742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010743f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010744{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010745 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010746
10747 rettv->v_type = VAR_FLOAT;
10748 if (get_float_arg(argvars, &f) == OK)
10749 rettv->vval.v_float = cos(f);
10750 else
10751 rettv->vval.v_float = 0.0;
10752}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010753
10754/*
10755 * "cosh()" function
10756 */
10757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010758f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010759{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010760 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010761
10762 rettv->v_type = VAR_FLOAT;
10763 if (get_float_arg(argvars, &f) == OK)
10764 rettv->vval.v_float = cosh(f);
10765 else
10766 rettv->vval.v_float = 0.0;
10767}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010768#endif
10769
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010771 * "count()" function
10772 */
10773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010774f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010775{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010776 long n = 0;
10777 int ic = FALSE;
10778
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010780 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010781 listitem_T *li;
10782 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010783 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010784
Bram Moolenaare9a41262005-01-15 22:18:47 +000010785 if ((l = argvars[0].vval.v_list) != NULL)
10786 {
10787 li = l->lv_first;
10788 if (argvars[2].v_type != VAR_UNKNOWN)
10789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010790 int error = FALSE;
10791
10792 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010793 if (argvars[3].v_type != VAR_UNKNOWN)
10794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 idx = get_tv_number_chk(&argvars[3], &error);
10796 if (!error)
10797 {
10798 li = list_find(l, idx);
10799 if (li == NULL)
10800 EMSGN(_(e_listidx), idx);
10801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010802 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 if (error)
10804 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010805 }
10806
10807 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010808 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010809 ++n;
10810 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010812 else if (argvars[0].v_type == VAR_DICT)
10813 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010814 int todo;
10815 dict_T *d;
10816 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010817
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010818 if ((d = argvars[0].vval.v_dict) != NULL)
10819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010820 int error = FALSE;
10821
Bram Moolenaare9a41262005-01-15 22:18:47 +000010822 if (argvars[2].v_type != VAR_UNKNOWN)
10823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010824 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010825 if (argvars[3].v_type != VAR_UNKNOWN)
10826 EMSG(_(e_invarg));
10827 }
10828
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010829 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010830 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010831 {
10832 if (!HASHITEM_EMPTY(hi))
10833 {
10834 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010835 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010836 ++n;
10837 }
10838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010839 }
10840 }
10841 else
10842 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010843 rettv->vval.v_number = n;
10844}
10845
10846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10848 *
10849 * Checks the existence of a cscope connection.
10850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010852f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853{
10854#ifdef FEAT_CSCOPE
10855 int num = 0;
10856 char_u *dbpath = NULL;
10857 char_u *prepend = NULL;
10858 char_u buf[NUMBUFLEN];
10859
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010860 if (argvars[0].v_type != VAR_UNKNOWN
10861 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863 num = (int)get_tv_number(&argvars[0]);
10864 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010865 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 }
10868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870#endif
10871}
10872
10873/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010874 * "cursor(lnum, col)" function, or
10875 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010877 * Moves the cursor to the specified line and column.
10878 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010881f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882{
10883 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010884#ifdef FEAT_VIRTUALEDIT
10885 long coladd = 0;
10886#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010887 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010889 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010890 if (argvars[1].v_type == VAR_UNKNOWN)
10891 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010892 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010893 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010894
Bram Moolenaar493c1782014-05-28 14:34:46 +020010895 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010896 {
10897 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010898 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010899 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010900 line = pos.lnum;
10901 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010902#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010903 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010904#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010905 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010906 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010907 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010908 set_curswant = FALSE;
10909 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010910 }
10911 else
10912 {
10913 line = get_tv_lnum(argvars);
10914 col = get_tv_number_chk(&argvars[1], NULL);
10915#ifdef FEAT_VIRTUALEDIT
10916 if (argvars[2].v_type != VAR_UNKNOWN)
10917 coladd = get_tv_number_chk(&argvars[2], NULL);
10918#endif
10919 }
10920 if (line < 0 || col < 0
10921#ifdef FEAT_VIRTUALEDIT
10922 || coladd < 0
10923#endif
10924 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010925 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 if (line > 0)
10927 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 if (col > 0)
10929 curwin->w_cursor.col = col - 1;
10930#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010931 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932#endif
10933
10934 /* Make sure the cursor is in a valid position. */
10935 check_cursor();
10936#ifdef FEAT_MBYTE
10937 /* Correct cursor for multi-byte character. */
10938 if (has_mbyte)
10939 mb_adjust_cursor();
10940#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010941
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010942 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010943 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944}
10945
10946/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010947 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 */
10949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010950f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010952 int noref = 0;
10953
10954 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010956 if (noref < 0 || noref > 1)
10957 EMSG(_(e_invarg));
10958 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010959 {
10960 current_copyID += COPYID_INC;
10961 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963}
10964
10965/*
10966 * "delete()" function
10967 */
10968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010969f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010971 char_u nbuf[NUMBUFLEN];
10972 char_u *name;
10973 char_u *flags;
10974
10975 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010977 return;
10978
10979 name = get_tv_string(&argvars[0]);
10980 if (name == NULL || *name == NUL)
10981 {
10982 EMSG(_(e_invarg));
10983 return;
10984 }
10985
10986 if (argvars[1].v_type != VAR_UNKNOWN)
10987 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010989 flags = (char_u *)"";
10990
10991 if (*flags == NUL)
10992 /* delete a file */
10993 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10994 else if (STRCMP(flags, "d") == 0)
10995 /* delete an empty directory */
10996 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10997 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010998 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010999 rettv->vval.v_number = delete_recursive(name);
11000 else
11001 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002}
11003
11004/*
11005 * "did_filetype()" function
11006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011008f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009{
11010#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012#endif
11013}
11014
11015/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011016 * "diff_filler()" function
11017 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011019f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011020{
11021#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011023#endif
11024}
11025
11026/*
11027 * "diff_hlID()" function
11028 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011030f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011031{
11032#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011034 static linenr_T prev_lnum = 0;
11035 static int changedtick = 0;
11036 static int fnum = 0;
11037 static int change_start = 0;
11038 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011039 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011040 int filler_lines;
11041 int col;
11042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011043 if (lnum < 0) /* ignore type error in {lnum} arg */
11044 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011045 if (lnum != prev_lnum
11046 || changedtick != curbuf->b_changedtick
11047 || fnum != curbuf->b_fnum)
11048 {
11049 /* New line, buffer, change: need to get the values. */
11050 filler_lines = diff_check(curwin, lnum);
11051 if (filler_lines < 0)
11052 {
11053 if (filler_lines == -1)
11054 {
11055 change_start = MAXCOL;
11056 change_end = -1;
11057 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11058 hlID = HLF_ADD; /* added line */
11059 else
11060 hlID = HLF_CHD; /* changed line */
11061 }
11062 else
11063 hlID = HLF_ADD; /* added line */
11064 }
11065 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011066 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011067 prev_lnum = lnum;
11068 changedtick = curbuf->b_changedtick;
11069 fnum = curbuf->b_fnum;
11070 }
11071
11072 if (hlID == HLF_CHD || hlID == HLF_TXD)
11073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011075 if (col >= change_start && col <= change_end)
11076 hlID = HLF_TXD; /* changed text */
11077 else
11078 hlID = HLF_CHD; /* changed line */
11079 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011080 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011081#endif
11082}
11083
11084/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011085 * "disable_char_avail_for_testing({expr})" function
11086 */
11087 static void
11088f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11089{
11090 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11091}
11092
11093/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011094 * "empty({expr})" function
11095 */
11096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011097f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011098{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011099 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011100
11101 switch (argvars[0].v_type)
11102 {
11103 case VAR_STRING:
11104 case VAR_FUNC:
11105 n = argvars[0].vval.v_string == NULL
11106 || *argvars[0].vval.v_string == NUL;
11107 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011108 case VAR_PARTIAL:
11109 n = FALSE;
11110 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011111 case VAR_NUMBER:
11112 n = argvars[0].vval.v_number == 0;
11113 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011114 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011115#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011116 n = argvars[0].vval.v_float == 0.0;
11117 break;
11118#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011119 case VAR_LIST:
11120 n = argvars[0].vval.v_list == NULL
11121 || argvars[0].vval.v_list->lv_first == NULL;
11122 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011123 case VAR_DICT:
11124 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011125 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011127 case VAR_SPECIAL:
11128 n = argvars[0].vval.v_number != VVAL_TRUE;
11129 break;
11130
Bram Moolenaar835dc632016-02-07 14:27:38 +010011131 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011132#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011133 n = argvars[0].vval.v_job == NULL
11134 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11135 break;
11136#endif
11137 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011138#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011139 n = argvars[0].vval.v_channel == NULL
11140 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011141 break;
11142#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011143 case VAR_UNKNOWN:
11144 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11145 n = TRUE;
11146 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011147 }
11148
11149 rettv->vval.v_number = n;
11150}
11151
11152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 * "escape({string}, {chars})" function
11154 */
11155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011156f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157{
11158 char_u buf[NUMBUFLEN];
11159
Bram Moolenaar758711c2005-02-02 23:11:38 +000011160 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11161 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163}
11164
11165/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011166 * "eval()" function
11167 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011169f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011170{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011171 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011173 s = get_tv_string_chk(&argvars[0]);
11174 if (s != NULL)
11175 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011176
Bram Moolenaar615b9972015-01-14 17:15:05 +010011177 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11179 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011180 if (p != NULL && !aborting())
11181 EMSG2(_(e_invexpr2), p);
11182 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011184 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011186 else if (*s != NUL)
11187 EMSG(_(e_trailing));
11188}
11189
11190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 * "eventhandler()" function
11192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011194f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197}
11198
11199/*
11200 * "executable()" function
11201 */
11202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011203f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011205 char_u *name = get_tv_string(&argvars[0]);
11206
11207 /* Check in $PATH and also check directly if there is a directory name. */
11208 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11209 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011210}
11211
11212/*
11213 * "exepath()" function
11214 */
11215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011216f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011217{
11218 char_u *p = NULL;
11219
Bram Moolenaarb5971142015-03-21 17:32:19 +010011220 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011221 rettv->v_type = VAR_STRING;
11222 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223}
11224
11225/*
11226 * "exists()" function
11227 */
11228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011229f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230{
11231 char_u *p;
11232 char_u *name;
11233 int n = FALSE;
11234 int len = 0;
11235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 if (*p == '$') /* environment variable */
11238 {
11239 /* first try "normal" environment variables (fast) */
11240 if (mch_getenv(p + 1) != NULL)
11241 n = TRUE;
11242 else
11243 {
11244 /* try expanding things like $VIM and ${HOME} */
11245 p = expand_env_save(p);
11246 if (p != NULL && *p != '$')
11247 n = TRUE;
11248 vim_free(p);
11249 }
11250 }
11251 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011253 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011254 if (*skipwhite(p) != NUL)
11255 n = FALSE; /* trailing garbage */
11256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 else if (*p == '*') /* internal or user defined function */
11258 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011259 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 }
11261 else if (*p == ':')
11262 {
11263 n = cmd_exists(p + 1);
11264 }
11265 else if (*p == '#')
11266 {
11267#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011268 if (p[1] == '#')
11269 n = autocmd_supported(p + 2);
11270 else
11271 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272#endif
11273 }
11274 else /* internal variable */
11275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011276 char_u *tofree;
11277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011279 /* get_name_len() takes care of expanding curly braces */
11280 name = p;
11281 len = get_name_len(&p, &tofree, TRUE, FALSE);
11282 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011284 if (tofree != NULL)
11285 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011286 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011287 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011289 /* handle d.key, l[idx], f(expr) */
11290 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11291 if (n)
11292 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 }
11294 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011295 if (*p != NUL)
11296 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011298 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 }
11300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302}
11303
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011304#ifdef FEAT_FLOAT
11305/*
11306 * "exp()" function
11307 */
11308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011309f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011310{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011311 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011312
11313 rettv->v_type = VAR_FLOAT;
11314 if (get_float_arg(argvars, &f) == OK)
11315 rettv->vval.v_float = exp(f);
11316 else
11317 rettv->vval.v_float = 0.0;
11318}
11319#endif
11320
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321/*
11322 * "expand()" function
11323 */
11324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011325f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326{
11327 char_u *s;
11328 int len;
11329 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011330 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011333 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011336 if (argvars[1].v_type != VAR_UNKNOWN
11337 && argvars[2].v_type != VAR_UNKNOWN
11338 && get_tv_number_chk(&argvars[2], &error)
11339 && !error)
11340 {
11341 rettv->v_type = VAR_LIST;
11342 rettv->vval.v_list = NULL;
11343 }
11344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 if (*s == '%' || *s == '#' || *s == '<')
11347 {
11348 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011349 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011351 if (rettv->v_type == VAR_LIST)
11352 {
11353 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11354 list_append_string(rettv->vval.v_list, result, -1);
11355 else
11356 vim_free(result);
11357 }
11358 else
11359 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 }
11361 else
11362 {
11363 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011364 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 if (argvars[1].v_type != VAR_UNKNOWN
11366 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011367 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 if (!error)
11369 {
11370 ExpandInit(&xpc);
11371 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011372 if (p_wic)
11373 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011374 if (rettv->v_type == VAR_STRING)
11375 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11376 options, WILD_ALL);
11377 else if (rettv_list_alloc(rettv) != FAIL)
11378 {
11379 int i;
11380
11381 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11382 for (i = 0; i < xpc.xp_numfiles; i++)
11383 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11384 ExpandCleanup(&xpc);
11385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 }
11387 else
11388 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 }
11390}
11391
11392/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011393 * Go over all entries in "d2" and add them to "d1".
11394 * When "action" is "error" then a duplicate key is an error.
11395 * When "action" is "force" then a duplicate key is overwritten.
11396 * Otherwise duplicate keys are ignored ("action" is "keep").
11397 */
11398 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011399dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011400{
11401 dictitem_T *di1;
11402 hashitem_T *hi2;
11403 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011404 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011405
11406 todo = (int)d2->dv_hashtab.ht_used;
11407 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11408 {
11409 if (!HASHITEM_EMPTY(hi2))
11410 {
11411 --todo;
11412 di1 = dict_find(d1, hi2->hi_key, -1);
11413 if (d1->dv_scope != 0)
11414 {
11415 /* Disallow replacing a builtin function in l: and g:.
11416 * Check the key to be valid when adding to any
11417 * scope. */
11418 if (d1->dv_scope == VAR_DEF_SCOPE
11419 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11420 && var_check_func_name(hi2->hi_key,
11421 di1 == NULL))
11422 break;
11423 if (!valid_varname(hi2->hi_key))
11424 break;
11425 }
11426 if (di1 == NULL)
11427 {
11428 di1 = dictitem_copy(HI2DI(hi2));
11429 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11430 dictitem_free(di1);
11431 }
11432 else if (*action == 'e')
11433 {
11434 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11435 break;
11436 }
11437 else if (*action == 'f' && HI2DI(hi2) != di1)
11438 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011439 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11440 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011441 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011442 clear_tv(&di1->di_tv);
11443 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11444 }
11445 }
11446 }
11447}
11448
11449/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011450 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011451 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011452 */
11453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011454f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011455{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011456 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011457
Bram Moolenaare9a41262005-01-15 22:18:47 +000011458 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011459 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011460 list_T *l1, *l2;
11461 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011462 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011464
Bram Moolenaare9a41262005-01-15 22:18:47 +000011465 l1 = argvars[0].vval.v_list;
11466 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011467 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011468 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469 {
11470 if (argvars[2].v_type != VAR_UNKNOWN)
11471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011472 before = get_tv_number_chk(&argvars[2], &error);
11473 if (error)
11474 return; /* type error; errmsg already given */
11475
Bram Moolenaar758711c2005-02-02 23:11:38 +000011476 if (before == l1->lv_len)
11477 item = NULL;
11478 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011479 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011480 item = list_find(l1, before);
11481 if (item == NULL)
11482 {
11483 EMSGN(_(e_listidx), before);
11484 return;
11485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011486 }
11487 }
11488 else
11489 item = NULL;
11490 list_extend(l1, l2, item);
11491
Bram Moolenaare9a41262005-01-15 22:18:47 +000011492 copy_tv(&argvars[0], rettv);
11493 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011494 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011495 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11496 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011497 dict_T *d1, *d2;
11498 char_u *action;
11499 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011500
11501 d1 = argvars[0].vval.v_dict;
11502 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011503 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011504 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011505 {
11506 /* Check the third argument. */
11507 if (argvars[2].v_type != VAR_UNKNOWN)
11508 {
11509 static char *(av[]) = {"keep", "force", "error"};
11510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011511 action = get_tv_string_chk(&argvars[2]);
11512 if (action == NULL)
11513 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011514 for (i = 0; i < 3; ++i)
11515 if (STRCMP(action, av[i]) == 0)
11516 break;
11517 if (i == 3)
11518 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011519 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 return;
11521 }
11522 }
11523 else
11524 action = (char_u *)"force";
11525
Bram Moolenaara9922d62013-05-30 13:01:18 +020011526 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011527
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528 copy_tv(&argvars[0], rettv);
11529 }
11530 }
11531 else
11532 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011533}
11534
11535/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011536 * "feedkeys()" function
11537 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011539f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011540{
11541 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011542 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011543 char_u *keys, *flags;
11544 char_u nbuf[NUMBUFLEN];
11545 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011546 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011547 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011548
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011549 /* This is not allowed in the sandbox. If the commands would still be
11550 * executed in the sandbox it would be OK, but it probably happens later,
11551 * when "sandbox" is no longer set. */
11552 if (check_secure())
11553 return;
11554
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011555 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011556
11557 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011558 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011559 flags = get_tv_string_buf(&argvars[1], nbuf);
11560 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011561 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011562 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011563 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011564 case 'n': remap = FALSE; break;
11565 case 'm': remap = TRUE; break;
11566 case 't': typed = TRUE; break;
11567 case 'i': insert = TRUE; break;
11568 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011569 }
11570 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011571 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011572
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011573 if (*keys != NUL || execute)
11574 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011575 /* Need to escape K_SPECIAL and CSI before putting the string in the
11576 * typeahead buffer. */
11577 keys_esc = vim_strsave_escape_csi(keys);
11578 if (keys_esc != NULL)
11579 {
11580 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011581 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011582 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011583 if (vgetc_busy)
11584 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011585 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011586 {
11587 int save_msg_scroll = msg_scroll;
11588
11589 /* Avoid a 1 second delay when the keys start Insert mode. */
11590 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011591
11592 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011593 exec_normal(TRUE);
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011594 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011595 msg_scroll |= save_msg_scroll;
11596 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011597 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011598 }
11599}
11600
11601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 * "filereadable()" function
11603 */
11604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011605f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011607 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 char_u *p;
11609 int n;
11610
Bram Moolenaarc236c162008-07-13 17:41:49 +000011611#ifndef O_NONBLOCK
11612# define O_NONBLOCK 0
11613#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011615 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11616 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 {
11618 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011619 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 }
11621 else
11622 n = FALSE;
11623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625}
11626
11627/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011628 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 * rights to write into.
11630 */
11631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011632f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011634 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635}
11636
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011637 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011638findfilendir(
11639 typval_T *argvars UNUSED,
11640 typval_T *rettv,
11641 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011642{
11643#ifdef FEAT_SEARCHPATH
11644 char_u *fname;
11645 char_u *fresult = NULL;
11646 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11647 char_u *p;
11648 char_u pathbuf[NUMBUFLEN];
11649 int count = 1;
11650 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011651 int error = FALSE;
11652#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011653
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011654 rettv->vval.v_string = NULL;
11655 rettv->v_type = VAR_STRING;
11656
11657#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011659
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011660 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011662 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11663 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011664 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011665 else
11666 {
11667 if (*p != NUL)
11668 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011671 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011673 }
11674
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011675 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11676 error = TRUE;
11677
11678 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 do
11681 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011682 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011683 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 fresult = find_file_in_path_option(first ? fname : NULL,
11685 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011686 0, first, path,
11687 find_what,
11688 curbuf->b_ffname,
11689 find_what == FINDFILE_DIR
11690 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011692
11693 if (fresult != NULL && rettv->v_type == VAR_LIST)
11694 list_append_string(rettv->vval.v_list, fresult, -1);
11695
11696 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011697 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011698
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011699 if (rettv->v_type == VAR_STRING)
11700 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011701#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011702}
11703
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011704static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11705static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011706
11707/*
11708 * Implementation of map() and filter().
11709 */
11710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011711filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011712{
11713 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011714 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011715 listitem_T *li, *nli;
11716 list_T *l = NULL;
11717 dictitem_T *di;
11718 hashtab_T *ht;
11719 hashitem_T *hi;
11720 dict_T *d = NULL;
11721 typval_T save_val;
11722 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011723 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011724 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011725 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011726 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011727 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011728 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011729 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011730
Bram Moolenaare9a41262005-01-15 22:18:47 +000011731 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011732 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011733 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011734 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011735 return;
11736 }
11737 else if (argvars[0].v_type == VAR_DICT)
11738 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011739 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011740 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011741 return;
11742 }
11743 else
11744 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011745 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 return;
11747 }
11748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011749 expr = get_tv_string_buf_chk(&argvars[1], buf);
11750 /* On type errors, the preceding call has already displayed an error
11751 * message. Avoid a misleading error message for an empty string that
11752 * was not passed as argument. */
11753 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011755 prepare_vimvar(VV_VAL, &save_val);
11756 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011757
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011758 /* We reset "did_emsg" to be able to detect whether an error
11759 * occurred during evaluation of the expression. */
11760 save_did_emsg = did_emsg;
11761 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011762
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011763 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011766 vimvars[VV_KEY].vv_type = VAR_STRING;
11767
11768 ht = &d->dv_hashtab;
11769 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011770 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 if (!HASHITEM_EMPTY(hi))
11774 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011775 int r;
11776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011777 --todo;
11778 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011779 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011780 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11781 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 break;
11783 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011784 r = filter_map_one(&di->di_tv, expr, map, &rem);
11785 clear_tv(&vimvars[VV_KEY].vv_tv);
11786 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 break;
11788 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011789 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011790 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11791 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011792 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011793 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 }
11796 }
11797 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 }
11799 else
11800 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011801 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 for (li = l->lv_first; li != NULL; li = nli)
11804 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011805 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011806 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011807 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011808 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011809 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011810 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011811 break;
11812 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011814 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011815 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011816 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011817
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011818 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011820
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011821 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011823
11824 copy_tv(&argvars[0], rettv);
11825}
11826
11827 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011828filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011829{
Bram Moolenaar33570922005-01-25 22:26:29 +000011830 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011831 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011832 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011833
Bram Moolenaar33570922005-01-25 22:26:29 +000011834 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011835 s = expr;
11836 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011837 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011838 if (*s != NUL) /* check for trailing chars after expr */
11839 {
11840 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011841 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011842 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 }
11844 if (map)
11845 {
11846 /* map(): replace the list item value */
11847 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011848 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011849 *tv = rettv;
11850 }
11851 else
11852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011853 int error = FALSE;
11854
Bram Moolenaare9a41262005-01-15 22:18:47 +000011855 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011857 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011858 /* On type error, nothing has been removed; return FAIL to stop the
11859 * loop. The error message was given by get_tv_number_chk(). */
11860 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011861 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011862 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011863 retval = OK;
11864theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011865 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011866 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011867}
11868
11869/*
11870 * "filter()" function
11871 */
11872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011873f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011874{
11875 filter_map(argvars, rettv, FALSE);
11876}
11877
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011878/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011879 * "finddir({fname}[, {path}[, {count}]])" function
11880 */
11881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011882f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011883{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011884 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011885}
11886
11887/*
11888 * "findfile({fname}[, {path}[, {count}]])" function
11889 */
11890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011891f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011893 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011894}
11895
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011896#ifdef FEAT_FLOAT
11897/*
11898 * "float2nr({float})" function
11899 */
11900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011901f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011902{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011903 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011904
11905 if (get_float_arg(argvars, &f) == OK)
11906 {
11907 if (f < -0x7fffffff)
11908 rettv->vval.v_number = -0x7fffffff;
11909 else if (f > 0x7fffffff)
11910 rettv->vval.v_number = 0x7fffffff;
11911 else
11912 rettv->vval.v_number = (varnumber_T)f;
11913 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011914}
11915
11916/*
11917 * "floor({float})" function
11918 */
11919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011920f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011921{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011922 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011923
11924 rettv->v_type = VAR_FLOAT;
11925 if (get_float_arg(argvars, &f) == OK)
11926 rettv->vval.v_float = floor(f);
11927 else
11928 rettv->vval.v_float = 0.0;
11929}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011930
11931/*
11932 * "fmod()" function
11933 */
11934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011935f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011936{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011937 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011938
11939 rettv->v_type = VAR_FLOAT;
11940 if (get_float_arg(argvars, &fx) == OK
11941 && get_float_arg(&argvars[1], &fy) == OK)
11942 rettv->vval.v_float = fmod(fx, fy);
11943 else
11944 rettv->vval.v_float = 0.0;
11945}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011946#endif
11947
Bram Moolenaar0d660222005-01-07 21:51:51 +000011948/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011949 * "fnameescape({string})" function
11950 */
11951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011952f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011953{
11954 rettv->vval.v_string = vim_strsave_fnameescape(
11955 get_tv_string(&argvars[0]), FALSE);
11956 rettv->v_type = VAR_STRING;
11957}
11958
11959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 * "fnamemodify({fname}, {mods})" function
11961 */
11962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011963f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964{
11965 char_u *fname;
11966 char_u *mods;
11967 int usedlen = 0;
11968 int len;
11969 char_u *fbuf = NULL;
11970 char_u buf[NUMBUFLEN];
11971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011972 fname = get_tv_string_chk(&argvars[0]);
11973 mods = get_tv_string_buf_chk(&argvars[1], buf);
11974 if (fname == NULL || mods == NULL)
11975 fname = NULL;
11976 else
11977 {
11978 len = (int)STRLEN(fname);
11979 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 vim_free(fbuf);
11988}
11989
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011990static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991
11992/*
11993 * "foldclosed()" function
11994 */
11995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011996foldclosed_both(
11997 typval_T *argvars UNUSED,
11998 typval_T *rettv,
11999 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
12001#ifdef FEAT_FOLDING
12002 linenr_T lnum;
12003 linenr_T first, last;
12004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12007 {
12008 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12009 {
12010 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 return;
12015 }
12016 }
12017#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019}
12020
12021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012022 * "foldclosed()" function
12023 */
12024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012025f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012026{
12027 foldclosed_both(argvars, rettv, FALSE);
12028}
12029
12030/*
12031 * "foldclosedend()" function
12032 */
12033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012034f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012035{
12036 foldclosed_both(argvars, rettv, TRUE);
12037}
12038
12039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 * "foldlevel()" function
12041 */
12042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012043f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044{
12045#ifdef FEAT_FOLDING
12046 linenr_T lnum;
12047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052}
12053
12054/*
12055 * "foldtext()" function
12056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012058f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059{
12060#ifdef FEAT_FOLDING
12061 linenr_T lnum;
12062 char_u *s;
12063 char_u *r;
12064 int len;
12065 char *txt;
12066#endif
12067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068 rettv->v_type = VAR_STRING;
12069 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012071 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12072 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12073 <= curbuf->b_ml.ml_line_count
12074 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075 {
12076 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012077 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12078 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 {
12080 if (!linewhite(lnum))
12081 break;
12082 ++lnum;
12083 }
12084
12085 /* Find interesting text in this line. */
12086 s = skipwhite(ml_get(lnum));
12087 /* skip C comment-start */
12088 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012091 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012092 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012093 {
12094 s = skipwhite(ml_get(lnum + 1));
12095 if (*s == '*')
12096 s = skipwhite(s + 1);
12097 }
12098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099 txt = _("+-%s%3ld lines: ");
12100 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012101 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 + 20 /* for %3ld */
12103 + STRLEN(s))); /* concatenated */
12104 if (r != NULL)
12105 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012106 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12107 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12108 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 len = (int)STRLEN(r);
12110 STRCAT(r, s);
12111 /* remove 'foldmarker' and 'commentstring' */
12112 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 }
12115 }
12116#endif
12117}
12118
12119/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012120 * "foldtextresult(lnum)" function
12121 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012123f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012124{
12125#ifdef FEAT_FOLDING
12126 linenr_T lnum;
12127 char_u *text;
12128 char_u buf[51];
12129 foldinfo_T foldinfo;
12130 int fold_count;
12131#endif
12132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 rettv->v_type = VAR_STRING;
12134 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012135#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012137 /* treat illegal types and illegal string values for {lnum} the same */
12138 if (lnum < 0)
12139 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012140 fold_count = foldedCount(curwin, lnum, &foldinfo);
12141 if (fold_count > 0)
12142 {
12143 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12144 &foldinfo, buf);
12145 if (text == buf)
12146 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012148 }
12149#endif
12150}
12151
12152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 * "foreground()" function
12154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012156f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158#ifdef FEAT_GUI
12159 if (gui.in_use)
12160 gui_mch_set_foreground();
12161#else
12162# ifdef WIN32
12163 win32_set_foreground();
12164# endif
12165#endif
12166}
12167
12168/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012169 * "function()" function
12170 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012172f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012173{
12174 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012175 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012176 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012177 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012178
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012179 if (argvars[0].v_type == VAR_FUNC)
12180 {
12181 /* function(MyFunc, [arg], dict) */
12182 s = argvars[0].vval.v_string;
12183 }
12184 else if (argvars[0].v_type == VAR_PARTIAL
12185 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012186 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012187 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012188 arg_pt = argvars[0].vval.v_partial;
12189 s = arg_pt->pt_name;
12190 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012191 else
12192 {
12193 /* function('MyFunc', [arg], dict) */
12194 s = get_tv_string(&argvars[0]);
12195 use_string = TRUE;
12196 }
12197
12198 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012199 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012200 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012201 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12202 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012203 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012204 else
12205 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012206 int dict_idx = 0;
12207 int arg_idx = 0;
12208 list_T *list = NULL;
12209
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012210 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012211 {
12212 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012213 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012214
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012215 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12216 * also be called from another script. Using trans_function_name()
12217 * would also work, but some plugins depend on the name being
12218 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012219 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012220 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12221 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012222 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012223 STRCPY(name, sid_buf);
12224 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012225 }
12226 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012227 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012228 name = vim_strsave(s);
12229
12230 if (argvars[1].v_type != VAR_UNKNOWN)
12231 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012232 if (argvars[2].v_type != VAR_UNKNOWN)
12233 {
12234 /* function(name, [args], dict) */
12235 arg_idx = 1;
12236 dict_idx = 2;
12237 }
12238 else if (argvars[1].v_type == VAR_DICT)
12239 /* function(name, dict) */
12240 dict_idx = 1;
12241 else
12242 /* function(name, [args]) */
12243 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012244 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012245 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012246 if (argvars[dict_idx].v_type != VAR_DICT)
12247 {
12248 EMSG(_("E922: expected a dict"));
12249 vim_free(name);
12250 return;
12251 }
12252 if (argvars[dict_idx].vval.v_dict == NULL)
12253 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012254 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012255 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012256 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012257 if (argvars[arg_idx].v_type != VAR_LIST)
12258 {
12259 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12260 vim_free(name);
12261 return;
12262 }
12263 list = argvars[arg_idx].vval.v_list;
12264 if (list == NULL || list->lv_len == 0)
12265 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012266 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012267 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012268 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012269 {
12270 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012271
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012272 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012273 if (pt == NULL)
12274 vim_free(name);
12275 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012276 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012277 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012278 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012279 listitem_T *li;
12280 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012281 int arg_len = 0;
12282 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012283
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012284 if (arg_pt != NULL)
12285 arg_len = arg_pt->pt_argc;
12286 if (list != NULL)
12287 lv_len = list->lv_len;
12288 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012289 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012290 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012291 if (pt->pt_argv == NULL)
12292 {
12293 vim_free(pt);
12294 vim_free(name);
12295 return;
12296 }
12297 else
12298 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012299 for (i = 0; i < arg_len; i++)
12300 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12301 if (lv_len > 0)
12302 for (li = list->lv_first; li != NULL;
12303 li = li->li_next)
12304 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012305 }
12306 }
12307
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012308 /* For "function(dict.func, [], dict)" and "func" is a partial
12309 * use "dict". That is backwards compatible. */
12310 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012311 {
12312 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12313 ++pt->pt_dict->dv_refcount;
12314 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012315 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012316 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012317 pt->pt_dict = arg_pt->pt_dict;
12318 if (pt->pt_dict != NULL)
12319 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012320 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012321
12322 pt->pt_refcount = 1;
12323 pt->pt_name = name;
12324 func_ref(pt->pt_name);
12325 }
12326 rettv->v_type = VAR_PARTIAL;
12327 rettv->vval.v_partial = pt;
12328 }
12329 else
12330 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012331 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012332 rettv->v_type = VAR_FUNC;
12333 rettv->vval.v_string = name;
12334 func_ref(name);
12335 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012336 }
12337}
12338
12339/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012340 * "garbagecollect()" function
12341 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012343f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012344{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012345 /* This is postponed until we are back at the toplevel, because we may be
12346 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12347 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012348
12349 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12350 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012351}
12352
12353/*
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020012354 * "garbagecollect_for_testing()" function
12355 */
12356 static void
12357f_garbagecollect_for_testing(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12358{
12359 /* This is dangerous, any Lists and Dicts used internally may be freed
12360 * while still in use. */
12361 garbage_collect(TRUE);
12362}
12363
12364/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012365 * "get()" function
12366 */
12367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012368f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012369{
Bram Moolenaar33570922005-01-25 22:26:29 +000012370 listitem_T *li;
12371 list_T *l;
12372 dictitem_T *di;
12373 dict_T *d;
12374 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012375
Bram Moolenaare9a41262005-01-15 22:18:47 +000012376 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012377 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012378 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012380 int error = FALSE;
12381
12382 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12383 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012384 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012385 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012387 else if (argvars[0].v_type == VAR_DICT)
12388 {
12389 if ((d = argvars[0].vval.v_dict) != NULL)
12390 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012391 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012392 if (di != NULL)
12393 tv = &di->di_tv;
12394 }
12395 }
12396 else
12397 EMSG2(_(e_listdictarg), "get()");
12398
12399 if (tv == NULL)
12400 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012401 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012402 copy_tv(&argvars[2], rettv);
12403 }
12404 else
12405 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012406}
12407
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012408static 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 +000012409
12410/*
12411 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012412 * Return a range (from start to end) of lines in rettv from the specified
12413 * buffer.
12414 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012415 */
12416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012417get_buffer_lines(
12418 buf_T *buf,
12419 linenr_T start,
12420 linenr_T end,
12421 int retlist,
12422 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012423{
12424 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012425
Bram Moolenaar959a1432013-12-14 12:17:38 +010012426 rettv->v_type = VAR_STRING;
12427 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012428 if (retlist && rettv_list_alloc(rettv) == FAIL)
12429 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012430
12431 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12432 return;
12433
12434 if (!retlist)
12435 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012436 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12437 p = ml_get_buf(buf, start, FALSE);
12438 else
12439 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012440 rettv->vval.v_string = vim_strsave(p);
12441 }
12442 else
12443 {
12444 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012445 return;
12446
12447 if (start < 1)
12448 start = 1;
12449 if (end > buf->b_ml.ml_line_count)
12450 end = buf->b_ml.ml_line_count;
12451 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012452 if (list_append_string(rettv->vval.v_list,
12453 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012454 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012455 }
12456}
12457
12458/*
12459 * "getbufline()" function
12460 */
12461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012462f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012463{
12464 linenr_T lnum;
12465 linenr_T end;
12466 buf_T *buf;
12467
12468 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12469 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012470 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012471 --emsg_off;
12472
Bram Moolenaar661b1822005-07-28 22:36:45 +000012473 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012474 if (argvars[2].v_type == VAR_UNKNOWN)
12475 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012476 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012477 end = get_tv_lnum_buf(&argvars[2], buf);
12478
Bram Moolenaar342337a2005-07-21 21:11:17 +000012479 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012480}
12481
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482/*
12483 * "getbufvar()" function
12484 */
12485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012486f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487{
12488 buf_T *buf;
12489 buf_T *save_curbuf;
12490 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012491 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012492 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012494 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12495 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012496 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012497 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012498
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012499 rettv->v_type = VAR_STRING;
12500 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501
12502 if (buf != NULL && varname != NULL)
12503 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012504 /* set curbuf to be our buf, temporarily */
12505 save_curbuf = curbuf;
12506 curbuf = buf;
12507
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012509 {
12510 if (get_option_tv(&varname, rettv, TRUE) == OK)
12511 done = TRUE;
12512 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012513 else if (STRCMP(varname, "changedtick") == 0)
12514 {
12515 rettv->v_type = VAR_NUMBER;
12516 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012517 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012518 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012519 else
12520 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012521 /* Look up the variable. */
12522 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12523 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12524 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012525 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012526 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012527 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012528 done = TRUE;
12529 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012530 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012531
12532 /* restore previous notion of curbuf */
12533 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012534 }
12535
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012536 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12537 /* use the default value */
12538 copy_tv(&argvars[2], rettv);
12539
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 --emsg_off;
12541}
12542
12543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544 * "getchar()" function
12545 */
12546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012547f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548{
12549 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012550 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012552 /* Position the cursor. Needed after a message that ends in a space. */
12553 windgoto(msg_row, msg_col);
12554
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 ++no_mapping;
12556 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012557 for (;;)
12558 {
12559 if (argvars[0].v_type == VAR_UNKNOWN)
12560 /* getchar(): blocking wait. */
12561 n = safe_vgetc();
12562 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12563 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012564 n = vpeekc_any();
12565 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012566 /* illegal argument or getchar(0) and no char avail: return zero */
12567 n = 0;
12568 else
12569 /* getchar(0) and char avail: return char */
12570 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012571
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012572 if (n == K_IGNORE)
12573 continue;
12574 break;
12575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576 --no_mapping;
12577 --allow_keys;
12578
Bram Moolenaar219b8702006-11-01 14:32:36 +000012579 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12580 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12581 vimvars[VV_MOUSE_COL].vv_nr = 0;
12582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012583 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 if (IS_SPECIAL(n) || mod_mask != 0)
12585 {
12586 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12587 int i = 0;
12588
12589 /* Turn a special key into three bytes, plus modifier. */
12590 if (mod_mask != 0)
12591 {
12592 temp[i++] = K_SPECIAL;
12593 temp[i++] = KS_MODIFIER;
12594 temp[i++] = mod_mask;
12595 }
12596 if (IS_SPECIAL(n))
12597 {
12598 temp[i++] = K_SPECIAL;
12599 temp[i++] = K_SECOND(n);
12600 temp[i++] = K_THIRD(n);
12601 }
12602#ifdef FEAT_MBYTE
12603 else if (has_mbyte)
12604 i += (*mb_char2bytes)(n, temp + i);
12605#endif
12606 else
12607 temp[i++] = n;
12608 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609 rettv->v_type = VAR_STRING;
12610 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012611
12612#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012613 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012614 {
12615 int row = mouse_row;
12616 int col = mouse_col;
12617 win_T *win;
12618 linenr_T lnum;
12619# ifdef FEAT_WINDOWS
12620 win_T *wp;
12621# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012622 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012623
12624 if (row >= 0 && col >= 0)
12625 {
12626 /* Find the window at the mouse coordinates and compute the
12627 * text position. */
12628 win = mouse_find_win(&row, &col);
12629 (void)mouse_comp_pos(win, &row, &col, &lnum);
12630# ifdef FEAT_WINDOWS
12631 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012632 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012633# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012634 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012635 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12636 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12637 }
12638 }
12639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 }
12641}
12642
12643/*
12644 * "getcharmod()" function
12645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012647f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650}
12651
12652/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012653 * "getcharsearch()" function
12654 */
12655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012656f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012657{
12658 if (rettv_dict_alloc(rettv) != FAIL)
12659 {
12660 dict_T *dict = rettv->vval.v_dict;
12661
12662 dict_add_nr_str(dict, "char", 0L, last_csearch());
12663 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12664 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12665 }
12666}
12667
12668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 * "getcmdline()" function
12670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012672f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674 rettv->v_type = VAR_STRING;
12675 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676}
12677
12678/*
12679 * "getcmdpos()" function
12680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012682f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685}
12686
12687/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012688 * "getcmdtype()" function
12689 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012691f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012692{
12693 rettv->v_type = VAR_STRING;
12694 rettv->vval.v_string = alloc(2);
12695 if (rettv->vval.v_string != NULL)
12696 {
12697 rettv->vval.v_string[0] = get_cmdline_type();
12698 rettv->vval.v_string[1] = NUL;
12699 }
12700}
12701
12702/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012703 * "getcmdwintype()" function
12704 */
12705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012706f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012707{
12708 rettv->v_type = VAR_STRING;
12709 rettv->vval.v_string = NULL;
12710#ifdef FEAT_CMDWIN
12711 rettv->vval.v_string = alloc(2);
12712 if (rettv->vval.v_string != NULL)
12713 {
12714 rettv->vval.v_string[0] = cmdwin_type;
12715 rettv->vval.v_string[1] = NUL;
12716 }
12717#endif
12718}
12719
12720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721 * "getcwd()" function
12722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012724f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012726 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012727 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012730 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012731
12732 wp = find_tabwin(&argvars[0], &argvars[1]);
12733 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012735 if (wp->w_localdir != NULL)
12736 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12737 else if(globaldir != NULL)
12738 rettv->vval.v_string = vim_strsave(globaldir);
12739 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012740 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012741 cwd = alloc(MAXPATHL);
12742 if (cwd != NULL)
12743 {
12744 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12745 rettv->vval.v_string = vim_strsave(cwd);
12746 vim_free(cwd);
12747 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012748 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012749#ifdef BACKSLASH_IN_FILENAME
12750 if (rettv->vval.v_string != NULL)
12751 slash_adjust(rettv->vval.v_string);
12752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 }
12754}
12755
12756/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012757 * "getfontname()" function
12758 */
12759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012760f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 rettv->v_type = VAR_STRING;
12763 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012764#ifdef FEAT_GUI
12765 if (gui.in_use)
12766 {
12767 GuiFont font;
12768 char_u *name = NULL;
12769
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012770 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012771 {
12772 /* Get the "Normal" font. Either the name saved by
12773 * hl_set_font_name() or from the font ID. */
12774 font = gui.norm_font;
12775 name = hl_get_font_name();
12776 }
12777 else
12778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012780 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12781 return;
12782 font = gui_mch_get_font(name, FALSE);
12783 if (font == NOFONT)
12784 return; /* Invalid font name, return empty string. */
12785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012787 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012788 gui_mch_free_font(font);
12789 }
12790#endif
12791}
12792
12793/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012794 * "getfperm({fname})" function
12795 */
12796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012797f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012798{
12799 char_u *fname;
12800 struct stat st;
12801 char_u *perm = NULL;
12802 char_u flags[] = "rwx";
12803 int i;
12804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012808 if (mch_stat((char *)fname, &st) >= 0)
12809 {
12810 perm = vim_strsave((char_u *)"---------");
12811 if (perm != NULL)
12812 {
12813 for (i = 0; i < 9; i++)
12814 {
12815 if (st.st_mode & (1 << (8 - i)))
12816 perm[i] = flags[i % 3];
12817 }
12818 }
12819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012821}
12822
12823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 * "getfsize({fname})" function
12825 */
12826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012827f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828{
12829 char_u *fname;
12830 struct stat st;
12831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835
12836 if (mch_stat((char *)fname, &st) >= 0)
12837 {
12838 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012843
12844 /* non-perfect check for overflow */
12845 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12846 rettv->vval.v_number = -2;
12847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 }
12849 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851}
12852
12853/*
12854 * "getftime({fname})" function
12855 */
12856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012857f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858{
12859 char_u *fname;
12860 struct stat st;
12861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863
12864 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868}
12869
12870/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012871 * "getftype({fname})" function
12872 */
12873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012874f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012875{
12876 char_u *fname;
12877 struct stat st;
12878 char_u *type = NULL;
12879 char *t;
12880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012884 if (mch_lstat((char *)fname, &st) >= 0)
12885 {
12886#ifdef S_ISREG
12887 if (S_ISREG(st.st_mode))
12888 t = "file";
12889 else if (S_ISDIR(st.st_mode))
12890 t = "dir";
12891# ifdef S_ISLNK
12892 else if (S_ISLNK(st.st_mode))
12893 t = "link";
12894# endif
12895# ifdef S_ISBLK
12896 else if (S_ISBLK(st.st_mode))
12897 t = "bdev";
12898# endif
12899# ifdef S_ISCHR
12900 else if (S_ISCHR(st.st_mode))
12901 t = "cdev";
12902# endif
12903# ifdef S_ISFIFO
12904 else if (S_ISFIFO(st.st_mode))
12905 t = "fifo";
12906# endif
12907# ifdef S_ISSOCK
12908 else if (S_ISSOCK(st.st_mode))
12909 t = "fifo";
12910# endif
12911 else
12912 t = "other";
12913#else
12914# ifdef S_IFMT
12915 switch (st.st_mode & S_IFMT)
12916 {
12917 case S_IFREG: t = "file"; break;
12918 case S_IFDIR: t = "dir"; break;
12919# ifdef S_IFLNK
12920 case S_IFLNK: t = "link"; break;
12921# endif
12922# ifdef S_IFBLK
12923 case S_IFBLK: t = "bdev"; break;
12924# endif
12925# ifdef S_IFCHR
12926 case S_IFCHR: t = "cdev"; break;
12927# endif
12928# ifdef S_IFIFO
12929 case S_IFIFO: t = "fifo"; break;
12930# endif
12931# ifdef S_IFSOCK
12932 case S_IFSOCK: t = "socket"; break;
12933# endif
12934 default: t = "other";
12935 }
12936# else
12937 if (mch_isdir(fname))
12938 t = "dir";
12939 else
12940 t = "file";
12941# endif
12942#endif
12943 type = vim_strsave((char_u *)t);
12944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012946}
12947
12948/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012949 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012950 */
12951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012952f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012953{
12954 linenr_T lnum;
12955 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012956 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012957
12958 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012959 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012960 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012961 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012962 retlist = FALSE;
12963 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012964 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012965 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012966 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012967 retlist = TRUE;
12968 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012969
Bram Moolenaar342337a2005-07-21 21:11:17 +000012970 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012971}
12972
12973/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012974 * "getmatches()" function
12975 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012977f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012978{
12979#ifdef FEAT_SEARCH_EXTRA
12980 dict_T *dict;
12981 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012982 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012983
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012984 if (rettv_list_alloc(rettv) == OK)
12985 {
12986 while (cur != NULL)
12987 {
12988 dict = dict_alloc();
12989 if (dict == NULL)
12990 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012991 if (cur->match.regprog == NULL)
12992 {
12993 /* match added with matchaddpos() */
12994 for (i = 0; i < MAXPOSMATCH; ++i)
12995 {
12996 llpos_T *llpos;
12997 char buf[6];
12998 list_T *l;
12999
13000 llpos = &cur->pos.pos[i];
13001 if (llpos->lnum == 0)
13002 break;
13003 l = list_alloc();
13004 if (l == NULL)
13005 break;
13006 list_append_number(l, (varnumber_T)llpos->lnum);
13007 if (llpos->col > 0)
13008 {
13009 list_append_number(l, (varnumber_T)llpos->col);
13010 list_append_number(l, (varnumber_T)llpos->len);
13011 }
13012 sprintf(buf, "pos%d", i + 1);
13013 dict_add_list(dict, buf, l);
13014 }
13015 }
13016 else
13017 {
13018 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13019 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013020 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013021 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13022 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013023# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013024 if (cur->conceal_char)
13025 {
13026 char_u buf[MB_MAXBYTES + 1];
13027
13028 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13029 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13030 }
13031# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013032 list_append_dict(rettv->vval.v_list, dict);
13033 cur = cur->next;
13034 }
13035 }
13036#endif
13037}
13038
13039/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013040 * "getpid()" function
13041 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013043f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013044{
13045 rettv->vval.v_number = mch_get_pid();
13046}
13047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013048static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013049
13050/*
13051 * "getcurpos()" function
13052 */
13053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013054f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013055{
13056 getpos_both(argvars, rettv, TRUE);
13057}
13058
Bram Moolenaar18081e32008-02-20 19:11:07 +000013059/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013060 * "getpos(string)" function
13061 */
13062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013063f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013064{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013065 getpos_both(argvars, rettv, FALSE);
13066}
13067
13068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013069getpos_both(
13070 typval_T *argvars,
13071 typval_T *rettv,
13072 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013073{
Bram Moolenaara5525202006-03-02 22:52:09 +000013074 pos_T *fp;
13075 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013076 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013077
13078 if (rettv_list_alloc(rettv) == OK)
13079 {
13080 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013081 if (getcurpos)
13082 fp = &curwin->w_cursor;
13083 else
13084 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013085 if (fnum != -1)
13086 list_append_number(l, (varnumber_T)fnum);
13087 else
13088 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013089 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13090 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013091 list_append_number(l, (fp != NULL)
13092 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013093 : (varnumber_T)0);
13094 list_append_number(l,
13095#ifdef FEAT_VIRTUALEDIT
13096 (fp != NULL) ? (varnumber_T)fp->coladd :
13097#endif
13098 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013099 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013100 {
13101 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013102 list_append_number(l, curwin->w_curswant == MAXCOL ?
13103 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013104 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013105 }
13106 else
13107 rettv->vval.v_number = FALSE;
13108}
13109
13110/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013111 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013112 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013114f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013115{
13116#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013117 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013118#endif
13119
Bram Moolenaar2641f772005-03-25 21:58:17 +000013120#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013121 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013122 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013123 wp = NULL;
13124 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13125 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013126 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013127 if (wp == NULL)
13128 return;
13129 }
13130
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013131 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013132 }
13133#endif
13134}
13135
13136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 * "getreg()" function
13138 */
13139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013140f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141{
13142 char_u *strregname;
13143 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013144 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013145 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013148 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013150 strregname = get_tv_string_chk(&argvars[0]);
13151 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013152 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013154 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013155 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13156 return_list = get_tv_number_chk(&argvars[2], &error);
13157 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013160 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013161
13162 if (error)
13163 return;
13164
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165 regname = (strregname == NULL ? '"' : *strregname);
13166 if (regname == 0)
13167 regname = '"';
13168
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013169 if (return_list)
13170 {
13171 rettv->v_type = VAR_LIST;
13172 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13173 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013174 if (rettv->vval.v_list != NULL)
13175 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013176 }
13177 else
13178 {
13179 rettv->v_type = VAR_STRING;
13180 rettv->vval.v_string = get_reg_contents(regname,
13181 arg2 ? GREG_EXPR_SRC : 0);
13182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183}
13184
13185/*
13186 * "getregtype()" function
13187 */
13188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013189f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190{
13191 char_u *strregname;
13192 int regname;
13193 char_u buf[NUMBUFLEN + 2];
13194 long reglen = 0;
13195
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013196 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 {
13198 strregname = get_tv_string_chk(&argvars[0]);
13199 if (strregname == NULL) /* type error; errmsg already given */
13200 {
13201 rettv->v_type = VAR_STRING;
13202 rettv->vval.v_string = NULL;
13203 return;
13204 }
13205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 else
13207 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013208 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209
13210 regname = (strregname == NULL ? '"' : *strregname);
13211 if (regname == 0)
13212 regname = '"';
13213
13214 buf[0] = NUL;
13215 buf[1] = NUL;
13216 switch (get_reg_type(regname, &reglen))
13217 {
13218 case MLINE: buf[0] = 'V'; break;
13219 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 case MBLOCK:
13221 buf[0] = Ctrl_V;
13222 sprintf((char *)buf + 1, "%ld", reglen + 1);
13223 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225 rettv->v_type = VAR_STRING;
13226 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227}
13228
13229/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013230 * "gettabvar()" function
13231 */
13232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013233f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013234{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013235 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013236 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013237 dictitem_T *v;
13238 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013239 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013240
13241 rettv->v_type = VAR_STRING;
13242 rettv->vval.v_string = NULL;
13243
13244 varname = get_tv_string_chk(&argvars[1]);
13245 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13246 if (tp != NULL && varname != NULL)
13247 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013248 /* Set tp to be our tabpage, temporarily. Also set the window to the
13249 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013250 if (switch_win(&oldcurwin, &oldtabpage,
13251 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013252 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013253 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013254 /* look up the variable */
13255 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13256 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13257 if (v != NULL)
13258 {
13259 copy_tv(&v->di_tv, rettv);
13260 done = TRUE;
13261 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013262 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013263
13264 /* restore previous notion of curwin */
13265 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013266 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013267
13268 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013269 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013270}
13271
13272/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013273 * "gettabwinvar()" function
13274 */
13275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013276f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013277{
13278 getwinvar(argvars, rettv, 1);
13279}
13280
13281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 * "getwinposx()" function
13283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013285f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013287 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288#ifdef FEAT_GUI
13289 if (gui.in_use)
13290 {
13291 int x, y;
13292
13293 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013294 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 }
13296#endif
13297}
13298
13299/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013300 * "win_findbuf()" function
13301 */
13302 static void
13303f_win_findbuf(typval_T *argvars, typval_T *rettv)
13304{
13305 if (rettv_list_alloc(rettv) != FAIL)
13306 win_findbuf(argvars, rettv->vval.v_list);
13307}
13308
13309/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013310 * "win_getid()" function
13311 */
13312 static void
13313f_win_getid(typval_T *argvars, typval_T *rettv)
13314{
13315 rettv->vval.v_number = win_getid(argvars);
13316}
13317
13318/*
13319 * "win_gotoid()" function
13320 */
13321 static void
13322f_win_gotoid(typval_T *argvars, typval_T *rettv)
13323{
13324 rettv->vval.v_number = win_gotoid(argvars);
13325}
13326
13327/*
13328 * "win_id2tabwin()" function
13329 */
13330 static void
13331f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13332{
13333 if (rettv_list_alloc(rettv) != FAIL)
13334 win_id2tabwin(argvars, rettv->vval.v_list);
13335}
13336
13337/*
13338 * "win_id2win()" function
13339 */
13340 static void
13341f_win_id2win(typval_T *argvars, typval_T *rettv)
13342{
13343 rettv->vval.v_number = win_id2win(argvars);
13344}
13345
13346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 * "getwinposy()" function
13348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013350f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353#ifdef FEAT_GUI
13354 if (gui.in_use)
13355 {
13356 int x, y;
13357
13358 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 }
13361#endif
13362}
13363
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013364/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013365 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013366 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013367 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013368find_win_by_nr(
13369 typval_T *vp,
13370 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013371{
13372#ifdef FEAT_WINDOWS
13373 win_T *wp;
13374#endif
13375 int nr;
13376
13377 nr = get_tv_number_chk(vp, NULL);
13378
13379#ifdef FEAT_WINDOWS
13380 if (nr < 0)
13381 return NULL;
13382 if (nr == 0)
13383 return curwin;
13384
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013385 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13386 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013387 if (--nr <= 0)
13388 break;
13389 return wp;
13390#else
13391 if (nr == 0 || nr == 1)
13392 return curwin;
13393 return NULL;
13394#endif
13395}
13396
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013398 * Find window specified by "wvp" in tabpage "tvp".
13399 */
13400 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013401find_tabwin(
13402 typval_T *wvp, /* VAR_UNKNOWN for current window */
13403 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013404{
13405 win_T *wp = NULL;
13406 tabpage_T *tp = NULL;
13407 long n;
13408
13409 if (wvp->v_type != VAR_UNKNOWN)
13410 {
13411 if (tvp->v_type != VAR_UNKNOWN)
13412 {
13413 n = get_tv_number(tvp);
13414 if (n >= 0)
13415 tp = find_tabpage(n);
13416 }
13417 else
13418 tp = curtab;
13419
13420 if (tp != NULL)
13421 wp = find_win_by_nr(wvp, tp);
13422 }
13423 else
13424 wp = curwin;
13425
13426 return wp;
13427}
13428
13429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 * "getwinvar()" function
13431 */
13432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013433f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013435 getwinvar(argvars, rettv, 0);
13436}
13437
13438/*
13439 * getwinvar() and gettabwinvar()
13440 */
13441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013442getwinvar(
13443 typval_T *argvars,
13444 typval_T *rettv,
13445 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013446{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013447 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013449 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013450 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013451 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013452#ifdef FEAT_WINDOWS
13453 win_T *oldcurwin;
13454 tabpage_T *oldtabpage;
13455 int need_switch_win;
13456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013458#ifdef FEAT_WINDOWS
13459 if (off == 1)
13460 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13461 else
13462 tp = curtab;
13463#endif
13464 win = find_win_by_nr(&argvars[off], tp);
13465 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013468 rettv->v_type = VAR_STRING;
13469 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470
13471 if (win != NULL && varname != NULL)
13472 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013473#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013474 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013475 * otherwise the window is not valid. Only do this when needed,
13476 * autocommands get blocked. */
13477 need_switch_win = !(tp == curtab && win == curwin);
13478 if (!need_switch_win
13479 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13480#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013481 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013482 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013483 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013484 if (get_option_tv(&varname, rettv, 1) == OK)
13485 done = TRUE;
13486 }
13487 else
13488 {
13489 /* Look up the variable. */
13490 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13491 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13492 varname, FALSE);
13493 if (v != NULL)
13494 {
13495 copy_tv(&v->di_tv, rettv);
13496 done = TRUE;
13497 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013500
Bram Moolenaarba117c22015-09-29 16:53:22 +020013501#ifdef FEAT_WINDOWS
13502 if (need_switch_win)
13503 /* restore previous notion of curwin */
13504 restore_win(oldcurwin, oldtabpage, TRUE);
13505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 }
13507
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013508 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13509 /* use the default return value */
13510 copy_tv(&argvars[off + 2], rettv);
13511
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 --emsg_off;
13513}
13514
13515/*
13516 * "glob()" function
13517 */
13518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013519f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013521 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013523 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013525 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013526 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013528 if (argvars[1].v_type != VAR_UNKNOWN)
13529 {
13530 if (get_tv_number_chk(&argvars[1], &error))
13531 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013532 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013533 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013534 if (get_tv_number_chk(&argvars[2], &error))
13535 {
13536 rettv->v_type = VAR_LIST;
13537 rettv->vval.v_list = NULL;
13538 }
13539 if (argvars[3].v_type != VAR_UNKNOWN
13540 && get_tv_number_chk(&argvars[3], &error))
13541 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013542 }
13543 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013544 if (!error)
13545 {
13546 ExpandInit(&xpc);
13547 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013548 if (p_wic)
13549 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013550 if (rettv->v_type == VAR_STRING)
13551 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013552 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013553 else if (rettv_list_alloc(rettv) != FAIL)
13554 {
13555 int i;
13556
13557 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13558 NULL, options, WILD_ALL_KEEP);
13559 for (i = 0; i < xpc.xp_numfiles; i++)
13560 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13561
13562 ExpandCleanup(&xpc);
13563 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013564 }
13565 else
13566 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567}
13568
13569/*
13570 * "globpath()" function
13571 */
13572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013573f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013575 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013577 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013578 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013579 garray_T ga;
13580 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013582 /* When the optional second argument is non-zero, don't remove matches
13583 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013585 if (argvars[2].v_type != VAR_UNKNOWN)
13586 {
13587 if (get_tv_number_chk(&argvars[2], &error))
13588 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013589 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013590 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013591 if (get_tv_number_chk(&argvars[3], &error))
13592 {
13593 rettv->v_type = VAR_LIST;
13594 rettv->vval.v_list = NULL;
13595 }
13596 if (argvars[4].v_type != VAR_UNKNOWN
13597 && get_tv_number_chk(&argvars[4], &error))
13598 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013599 }
13600 }
13601 if (file != NULL && !error)
13602 {
13603 ga_init2(&ga, (int)sizeof(char_u *), 10);
13604 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13605 if (rettv->v_type == VAR_STRING)
13606 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13607 else if (rettv_list_alloc(rettv) != FAIL)
13608 for (i = 0; i < ga.ga_len; ++i)
13609 list_append_string(rettv->vval.v_list,
13610 ((char_u **)(ga.ga_data))[i], -1);
13611 ga_clear_strings(&ga);
13612 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013614 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615}
13616
13617/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013618 * "glob2regpat()" function
13619 */
13620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013621f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013622{
13623 char_u *pat = get_tv_string_chk(&argvars[0]);
13624
13625 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013626 rettv->vval.v_string = (pat == NULL)
13627 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013628}
13629
13630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 * "has()" function
13632 */
13633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013634f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635{
13636 int i;
13637 char_u *name;
13638 int n = FALSE;
13639 static char *(has_list[]) =
13640 {
13641#ifdef AMIGA
13642 "amiga",
13643# ifdef FEAT_ARP
13644 "arp",
13645# endif
13646#endif
13647#ifdef __BEOS__
13648 "beos",
13649#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013650#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 "mac",
13652#endif
13653#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013654 "macunix", /* built with 'darwin' enabled */
13655#endif
13656#if defined(__APPLE__) && __APPLE__ == 1
13657 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659#ifdef __QNX__
13660 "qnx",
13661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662#ifdef UNIX
13663 "unix",
13664#endif
13665#ifdef VMS
13666 "vms",
13667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668#ifdef WIN32
13669 "win32",
13670#endif
13671#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13672 "win32unix",
13673#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013674#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 "win64",
13676#endif
13677#ifdef EBCDIC
13678 "ebcdic",
13679#endif
13680#ifndef CASE_INSENSITIVE_FILENAME
13681 "fname_case",
13682#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013683#ifdef HAVE_ACL
13684 "acl",
13685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686#ifdef FEAT_ARABIC
13687 "arabic",
13688#endif
13689#ifdef FEAT_AUTOCMD
13690 "autocmd",
13691#endif
13692#ifdef FEAT_BEVAL
13693 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013694# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13695 "balloon_multiline",
13696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697#endif
13698#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13699 "builtin_terms",
13700# ifdef ALL_BUILTIN_TCAPS
13701 "all_builtin_terms",
13702# endif
13703#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013704#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13705 || defined(FEAT_GUI_W32) \
13706 || defined(FEAT_GUI_MOTIF))
13707 "browsefilter",
13708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709#ifdef FEAT_BYTEOFF
13710 "byte_offset",
13711#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013712#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013713 "channel",
13714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715#ifdef FEAT_CINDENT
13716 "cindent",
13717#endif
13718#ifdef FEAT_CLIENTSERVER
13719 "clientserver",
13720#endif
13721#ifdef FEAT_CLIPBOARD
13722 "clipboard",
13723#endif
13724#ifdef FEAT_CMDL_COMPL
13725 "cmdline_compl",
13726#endif
13727#ifdef FEAT_CMDHIST
13728 "cmdline_hist",
13729#endif
13730#ifdef FEAT_COMMENTS
13731 "comments",
13732#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013733#ifdef FEAT_CONCEAL
13734 "conceal",
13735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736#ifdef FEAT_CRYPT
13737 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013738 "crypt-blowfish",
13739 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740#endif
13741#ifdef FEAT_CSCOPE
13742 "cscope",
13743#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013744#ifdef FEAT_CURSORBIND
13745 "cursorbind",
13746#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013747#ifdef CURSOR_SHAPE
13748 "cursorshape",
13749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750#ifdef DEBUG
13751 "debug",
13752#endif
13753#ifdef FEAT_CON_DIALOG
13754 "dialog_con",
13755#endif
13756#ifdef FEAT_GUI_DIALOG
13757 "dialog_gui",
13758#endif
13759#ifdef FEAT_DIFF
13760 "diff",
13761#endif
13762#ifdef FEAT_DIGRAPHS
13763 "digraphs",
13764#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013765#ifdef FEAT_DIRECTX
13766 "directx",
13767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768#ifdef FEAT_DND
13769 "dnd",
13770#endif
13771#ifdef FEAT_EMACS_TAGS
13772 "emacs_tags",
13773#endif
13774 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013775 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776#ifdef FEAT_SEARCH_EXTRA
13777 "extra_search",
13778#endif
13779#ifdef FEAT_FKMAP
13780 "farsi",
13781#endif
13782#ifdef FEAT_SEARCHPATH
13783 "file_in_path",
13784#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013785#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013786 "filterpipe",
13787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788#ifdef FEAT_FIND_ID
13789 "find_in_path",
13790#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013791#ifdef FEAT_FLOAT
13792 "float",
13793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794#ifdef FEAT_FOLDING
13795 "folding",
13796#endif
13797#ifdef FEAT_FOOTER
13798 "footer",
13799#endif
13800#if !defined(USE_SYSTEM) && defined(UNIX)
13801 "fork",
13802#endif
13803#ifdef FEAT_GETTEXT
13804 "gettext",
13805#endif
13806#ifdef FEAT_GUI
13807 "gui",
13808#endif
13809#ifdef FEAT_GUI_ATHENA
13810# ifdef FEAT_GUI_NEXTAW
13811 "gui_neXtaw",
13812# else
13813 "gui_athena",
13814# endif
13815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816#ifdef FEAT_GUI_GTK
13817 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013818# ifdef USE_GTK3
13819 "gui_gtk3",
13820# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013824#ifdef FEAT_GUI_GNOME
13825 "gui_gnome",
13826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827#ifdef FEAT_GUI_MAC
13828 "gui_mac",
13829#endif
13830#ifdef FEAT_GUI_MOTIF
13831 "gui_motif",
13832#endif
13833#ifdef FEAT_GUI_PHOTON
13834 "gui_photon",
13835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836#ifdef FEAT_GUI_W32
13837 "gui_win32",
13838#endif
13839#ifdef FEAT_HANGULIN
13840 "hangul_input",
13841#endif
13842#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13843 "iconv",
13844#endif
13845#ifdef FEAT_INS_EXPAND
13846 "insert_expand",
13847#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013848#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013849 "job",
13850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851#ifdef FEAT_JUMPLIST
13852 "jumplist",
13853#endif
13854#ifdef FEAT_KEYMAP
13855 "keymap",
13856#endif
13857#ifdef FEAT_LANGMAP
13858 "langmap",
13859#endif
13860#ifdef FEAT_LIBCALL
13861 "libcall",
13862#endif
13863#ifdef FEAT_LINEBREAK
13864 "linebreak",
13865#endif
13866#ifdef FEAT_LISP
13867 "lispindent",
13868#endif
13869#ifdef FEAT_LISTCMDS
13870 "listcmds",
13871#endif
13872#ifdef FEAT_LOCALMAP
13873 "localmap",
13874#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013875#ifdef FEAT_LUA
13876# ifndef DYNAMIC_LUA
13877 "lua",
13878# endif
13879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880#ifdef FEAT_MENU
13881 "menu",
13882#endif
13883#ifdef FEAT_SESSION
13884 "mksession",
13885#endif
13886#ifdef FEAT_MODIFY_FNAME
13887 "modify_fname",
13888#endif
13889#ifdef FEAT_MOUSE
13890 "mouse",
13891#endif
13892#ifdef FEAT_MOUSESHAPE
13893 "mouseshape",
13894#endif
13895#if defined(UNIX) || defined(VMS)
13896# ifdef FEAT_MOUSE_DEC
13897 "mouse_dec",
13898# endif
13899# ifdef FEAT_MOUSE_GPM
13900 "mouse_gpm",
13901# endif
13902# ifdef FEAT_MOUSE_JSB
13903 "mouse_jsbterm",
13904# endif
13905# ifdef FEAT_MOUSE_NET
13906 "mouse_netterm",
13907# endif
13908# ifdef FEAT_MOUSE_PTERM
13909 "mouse_pterm",
13910# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013911# ifdef FEAT_MOUSE_SGR
13912 "mouse_sgr",
13913# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013914# ifdef FEAT_SYSMOUSE
13915 "mouse_sysmouse",
13916# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013917# ifdef FEAT_MOUSE_URXVT
13918 "mouse_urxvt",
13919# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920# ifdef FEAT_MOUSE_XTERM
13921 "mouse_xterm",
13922# endif
13923#endif
13924#ifdef FEAT_MBYTE
13925 "multi_byte",
13926#endif
13927#ifdef FEAT_MBYTE_IME
13928 "multi_byte_ime",
13929#endif
13930#ifdef FEAT_MULTI_LANG
13931 "multi_lang",
13932#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013933#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013934#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013935 "mzscheme",
13936#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938#ifdef FEAT_OLE
13939 "ole",
13940#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013941 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942#ifdef FEAT_PATH_EXTRA
13943 "path_extra",
13944#endif
13945#ifdef FEAT_PERL
13946#ifndef DYNAMIC_PERL
13947 "perl",
13948#endif
13949#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013950#ifdef FEAT_PERSISTENT_UNDO
13951 "persistent_undo",
13952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953#ifdef FEAT_PYTHON
13954#ifndef DYNAMIC_PYTHON
13955 "python",
13956#endif
13957#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013958#ifdef FEAT_PYTHON3
13959#ifndef DYNAMIC_PYTHON3
13960 "python3",
13961#endif
13962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963#ifdef FEAT_POSTSCRIPT
13964 "postscript",
13965#endif
13966#ifdef FEAT_PRINTER
13967 "printer",
13968#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013969#ifdef FEAT_PROFILE
13970 "profile",
13971#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013972#ifdef FEAT_RELTIME
13973 "reltime",
13974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975#ifdef FEAT_QUICKFIX
13976 "quickfix",
13977#endif
13978#ifdef FEAT_RIGHTLEFT
13979 "rightleft",
13980#endif
13981#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13982 "ruby",
13983#endif
13984#ifdef FEAT_SCROLLBIND
13985 "scrollbind",
13986#endif
13987#ifdef FEAT_CMDL_INFO
13988 "showcmd",
13989 "cmdline_info",
13990#endif
13991#ifdef FEAT_SIGNS
13992 "signs",
13993#endif
13994#ifdef FEAT_SMARTINDENT
13995 "smartindent",
13996#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013997#ifdef STARTUPTIME
13998 "startuptime",
13999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000#ifdef FEAT_STL_OPT
14001 "statusline",
14002#endif
14003#ifdef FEAT_SUN_WORKSHOP
14004 "sun_workshop",
14005#endif
14006#ifdef FEAT_NETBEANS_INTG
14007 "netbeans_intg",
14008#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014009#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014010 "spell",
14011#endif
14012#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 "syntax",
14014#endif
14015#if defined(USE_SYSTEM) || !defined(UNIX)
14016 "system",
14017#endif
14018#ifdef FEAT_TAG_BINS
14019 "tag_binary",
14020#endif
14021#ifdef FEAT_TAG_OLDSTATIC
14022 "tag_old_static",
14023#endif
14024#ifdef FEAT_TAG_ANYWHITE
14025 "tag_any_white",
14026#endif
14027#ifdef FEAT_TCL
14028# ifndef DYNAMIC_TCL
14029 "tcl",
14030# endif
14031#endif
14032#ifdef TERMINFO
14033 "terminfo",
14034#endif
14035#ifdef FEAT_TERMRESPONSE
14036 "termresponse",
14037#endif
14038#ifdef FEAT_TEXTOBJ
14039 "textobjects",
14040#endif
14041#ifdef HAVE_TGETENT
14042 "tgetent",
14043#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014044#ifdef FEAT_TIMERS
14045 "timers",
14046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047#ifdef FEAT_TITLE
14048 "title",
14049#endif
14050#ifdef FEAT_TOOLBAR
14051 "toolbar",
14052#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014053#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14054 "unnamedplus",
14055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056#ifdef FEAT_USR_CMDS
14057 "user-commands", /* was accidentally included in 5.4 */
14058 "user_commands",
14059#endif
14060#ifdef FEAT_VIMINFO
14061 "viminfo",
14062#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014063#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 "vertsplit",
14065#endif
14066#ifdef FEAT_VIRTUALEDIT
14067 "virtualedit",
14068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070#ifdef FEAT_VISUALEXTRA
14071 "visualextra",
14072#endif
14073#ifdef FEAT_VREPLACE
14074 "vreplace",
14075#endif
14076#ifdef FEAT_WILDIGN
14077 "wildignore",
14078#endif
14079#ifdef FEAT_WILDMENU
14080 "wildmenu",
14081#endif
14082#ifdef FEAT_WINDOWS
14083 "windows",
14084#endif
14085#ifdef FEAT_WAK
14086 "winaltkeys",
14087#endif
14088#ifdef FEAT_WRITEBACKUP
14089 "writebackup",
14090#endif
14091#ifdef FEAT_XIM
14092 "xim",
14093#endif
14094#ifdef FEAT_XFONTSET
14095 "xfontset",
14096#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014097#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014098 "xpm",
14099 "xpm_w32", /* for backward compatibility */
14100#else
14101# if defined(HAVE_XPM)
14102 "xpm",
14103# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105#ifdef USE_XSMP
14106 "xsmp",
14107#endif
14108#ifdef USE_XSMP_INTERACT
14109 "xsmp_interact",
14110#endif
14111#ifdef FEAT_XCLIPBOARD
14112 "xterm_clipboard",
14113#endif
14114#ifdef FEAT_XTERM_SAVE
14115 "xterm_save",
14116#endif
14117#if defined(UNIX) && defined(FEAT_X11)
14118 "X11",
14119#endif
14120 NULL
14121 };
14122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 for (i = 0; has_list[i] != NULL; ++i)
14125 if (STRICMP(name, has_list[i]) == 0)
14126 {
14127 n = TRUE;
14128 break;
14129 }
14130
14131 if (n == FALSE)
14132 {
14133 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014134 {
14135 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014136 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014137 && vim_isdigit(name[6])
14138 && vim_isdigit(name[8])
14139 && vim_isdigit(name[10]))
14140 {
14141 int major = atoi((char *)name + 6);
14142 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014143
14144 /* Expect "patch-9.9.01234". */
14145 n = (major < VIM_VERSION_MAJOR
14146 || (major == VIM_VERSION_MAJOR
14147 && (minor < VIM_VERSION_MINOR
14148 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014149 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014150 }
14151 else
14152 n = has_patch(atoi((char *)name + 5));
14153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 else if (STRICMP(name, "vim_starting") == 0)
14155 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014156#ifdef FEAT_MBYTE
14157 else if (STRICMP(name, "multi_byte_encoding") == 0)
14158 n = has_mbyte;
14159#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014160#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14161 else if (STRICMP(name, "balloon_multiline") == 0)
14162 n = multiline_balloon_available();
14163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164#ifdef DYNAMIC_TCL
14165 else if (STRICMP(name, "tcl") == 0)
14166 n = tcl_enabled(FALSE);
14167#endif
14168#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14169 else if (STRICMP(name, "iconv") == 0)
14170 n = iconv_enabled(FALSE);
14171#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014172#ifdef DYNAMIC_LUA
14173 else if (STRICMP(name, "lua") == 0)
14174 n = lua_enabled(FALSE);
14175#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014176#ifdef DYNAMIC_MZSCHEME
14177 else if (STRICMP(name, "mzscheme") == 0)
14178 n = mzscheme_enabled(FALSE);
14179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180#ifdef DYNAMIC_RUBY
14181 else if (STRICMP(name, "ruby") == 0)
14182 n = ruby_enabled(FALSE);
14183#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014184#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185#ifdef DYNAMIC_PYTHON
14186 else if (STRICMP(name, "python") == 0)
14187 n = python_enabled(FALSE);
14188#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014189#endif
14190#ifdef FEAT_PYTHON3
14191#ifdef DYNAMIC_PYTHON3
14192 else if (STRICMP(name, "python3") == 0)
14193 n = python3_enabled(FALSE);
14194#endif
14195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196#ifdef DYNAMIC_PERL
14197 else if (STRICMP(name, "perl") == 0)
14198 n = perl_enabled(FALSE);
14199#endif
14200#ifdef FEAT_GUI
14201 else if (STRICMP(name, "gui_running") == 0)
14202 n = (gui.in_use || gui.starting);
14203# ifdef FEAT_GUI_W32
14204 else if (STRICMP(name, "gui_win32s") == 0)
14205 n = gui_is_win32s();
14206# endif
14207# ifdef FEAT_BROWSE
14208 else if (STRICMP(name, "browse") == 0)
14209 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14210# endif
14211#endif
14212#ifdef FEAT_SYN_HL
14213 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014214 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215#endif
14216#if defined(WIN3264)
14217 else if (STRICMP(name, "win95") == 0)
14218 n = mch_windows95();
14219#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014220#ifdef FEAT_NETBEANS_INTG
14221 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014222 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224 }
14225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227}
14228
14229/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014230 * "has_key()" function
14231 */
14232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014233f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014234{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014235 if (argvars[0].v_type != VAR_DICT)
14236 {
14237 EMSG(_(e_dictreq));
14238 return;
14239 }
14240 if (argvars[0].vval.v_dict == NULL)
14241 return;
14242
14243 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014244 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014245}
14246
14247/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014248 * "haslocaldir()" function
14249 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014251f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014252{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014253 win_T *wp = NULL;
14254
14255 wp = find_tabwin(&argvars[0], &argvars[1]);
14256 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014257}
14258
14259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 * "hasmapto()" function
14261 */
14262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014263f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264{
14265 char_u *name;
14266 char_u *mode;
14267 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014268 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014271 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 mode = (char_u *)"nvo";
14273 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014276 if (argvars[2].v_type != VAR_UNKNOWN)
14277 abbr = get_tv_number(&argvars[2]);
14278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279
Bram Moolenaar2c932302006-03-18 21:42:09 +000014280 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014281 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284}
14285
14286/*
14287 * "histadd()" function
14288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014290f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291{
14292#ifdef FEAT_CMDHIST
14293 int histype;
14294 char_u *str;
14295 char_u buf[NUMBUFLEN];
14296#endif
14297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014298 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299 if (check_restricted() || check_secure())
14300 return;
14301#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14303 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 if (histype >= 0)
14305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014306 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 if (*str != NUL)
14308 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014309 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 return;
14313 }
14314 }
14315#endif
14316}
14317
14318/*
14319 * "histdel()" function
14320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014322f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323{
14324#ifdef FEAT_CMDHIST
14325 int n;
14326 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014327 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014329 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14330 if (str == NULL)
14331 n = 0;
14332 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014334 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014335 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014337 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014338 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 else
14340 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014341 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014342 get_tv_string_buf(&argvars[1], buf));
14343 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344#endif
14345}
14346
14347/*
14348 * "histget()" function
14349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014351f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352{
14353#ifdef FEAT_CMDHIST
14354 int type;
14355 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014356 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014358 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14359 if (str == NULL)
14360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014362 {
14363 type = get_histtype(str);
14364 if (argvars[1].v_type == VAR_UNKNOWN)
14365 idx = get_history_idx(type);
14366 else
14367 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14368 /* -1 on type error */
14369 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375}
14376
14377/*
14378 * "histnr()" function
14379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014381f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382{
14383 int i;
14384
14385#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 char_u *history = get_tv_string_chk(&argvars[0]);
14387
14388 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 if (i >= HIST_CMD && i < HIST_COUNT)
14390 i = get_history_idx(i);
14391 else
14392#endif
14393 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014394 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395}
14396
14397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 * "highlightID(name)" function
14399 */
14400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014401f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404}
14405
14406/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014407 * "highlight_exists()" function
14408 */
14409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014410f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014411{
14412 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14413}
14414
14415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 * "hostname()" function
14417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014419f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420{
14421 char_u hostname[256];
14422
14423 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014424 rettv->v_type = VAR_STRING;
14425 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426}
14427
14428/*
14429 * iconv() function
14430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014432f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433{
14434#ifdef FEAT_MBYTE
14435 char_u buf1[NUMBUFLEN];
14436 char_u buf2[NUMBUFLEN];
14437 char_u *from, *to, *str;
14438 vimconv_T vimconv;
14439#endif
14440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014441 rettv->v_type = VAR_STRING;
14442 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443
14444#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 str = get_tv_string(&argvars[0]);
14446 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14447 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 vimconv.vc_type = CONV_NONE;
14449 convert_setup(&vimconv, from, to);
14450
14451 /* If the encodings are equal, no conversion needed. */
14452 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456
14457 convert_setup(&vimconv, NULL, NULL);
14458 vim_free(from);
14459 vim_free(to);
14460#endif
14461}
14462
14463/*
14464 * "indent()" function
14465 */
14466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014467f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468{
14469 linenr_T lnum;
14470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476}
14477
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014478/*
14479 * "index()" function
14480 */
14481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014482f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014483{
Bram Moolenaar33570922005-01-25 22:26:29 +000014484 list_T *l;
14485 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014486 long idx = 0;
14487 int ic = FALSE;
14488
14489 rettv->vval.v_number = -1;
14490 if (argvars[0].v_type != VAR_LIST)
14491 {
14492 EMSG(_(e_listreq));
14493 return;
14494 }
14495 l = argvars[0].vval.v_list;
14496 if (l != NULL)
14497 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014498 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014499 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014501 int error = FALSE;
14502
Bram Moolenaar758711c2005-02-02 23:11:38 +000014503 /* Start at specified item. Use the cached index that list_find()
14504 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014506 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014507 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014508 ic = get_tv_number_chk(&argvars[3], &error);
14509 if (error)
14510 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014511 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014512
Bram Moolenaar758711c2005-02-02 23:11:38 +000014513 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014514 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014515 {
14516 rettv->vval.v_number = idx;
14517 break;
14518 }
14519 }
14520}
14521
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522static int inputsecret_flag = 0;
14523
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014524static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014525
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014527 * This function is used by f_input() and f_inputdialog() functions. The third
14528 * argument to f_input() specifies the type of completion to use at the
14529 * prompt. The third argument to f_inputdialog() specifies the value to return
14530 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 */
14532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014533get_user_input(
14534 typval_T *argvars,
14535 typval_T *rettv,
14536 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014538 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 char_u *p = NULL;
14540 int c;
14541 char_u buf[NUMBUFLEN];
14542 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014543 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014544 int xp_type = EXPAND_NOTHING;
14545 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014548 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549
14550#ifdef NO_CONSOLE_INPUT
14551 /* While starting up, there is no place to enter text. */
14552 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554#endif
14555
14556 cmd_silent = FALSE; /* Want to see the prompt. */
14557 if (prompt != NULL)
14558 {
14559 /* Only the part of the message after the last NL is considered as
14560 * prompt for the command line */
14561 p = vim_strrchr(prompt, '\n');
14562 if (p == NULL)
14563 p = prompt;
14564 else
14565 {
14566 ++p;
14567 c = *p;
14568 *p = NUL;
14569 msg_start();
14570 msg_clr_eos();
14571 msg_puts_attr(prompt, echo_attr);
14572 msg_didout = FALSE;
14573 msg_starthere();
14574 *p = c;
14575 }
14576 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 if (argvars[1].v_type != VAR_UNKNOWN)
14579 {
14580 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14581 if (defstr != NULL)
14582 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014584 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014585 {
14586 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014587 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014588 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014589
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014590 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014591 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014592
Bram Moolenaar4463f292005-09-25 22:20:24 +000014593 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14594 if (xp_name == NULL)
14595 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014596
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014597 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014598
Bram Moolenaar4463f292005-09-25 22:20:24 +000014599 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14600 &xp_arg) == FAIL)
14601 return;
14602 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014603 }
14604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014605 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014606 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014607 int save_ex_normal_busy = ex_normal_busy;
14608 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014609 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014610 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14611 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014612 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014613 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014614 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014615 && argvars[1].v_type != VAR_UNKNOWN
14616 && argvars[2].v_type != VAR_UNKNOWN)
14617 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14618 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014619
14620 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014622 /* since the user typed this, no need to wait for return */
14623 need_wait_return = FALSE;
14624 msg_didout = FALSE;
14625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 cmd_silent = cmd_silent_save;
14627}
14628
14629/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014630 * "input()" function
14631 * Also handles inputsecret() when inputsecret is set.
14632 */
14633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014634f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014635{
14636 get_user_input(argvars, rettv, FALSE);
14637}
14638
14639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 * "inputdialog()" function
14641 */
14642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014643f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644{
14645#if defined(FEAT_GUI_TEXTDIALOG)
14646 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14647 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14648 {
14649 char_u *message;
14650 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014651 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014653 message = get_tv_string_chk(&argvars[0]);
14654 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014655 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014656 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657 else
14658 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014659 if (message != NULL && defstr != NULL
14660 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014661 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014662 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663 else
14664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014665 if (message != NULL && defstr != NULL
14666 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014667 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014668 rettv->vval.v_string = vim_strsave(
14669 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014671 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 }
14675 else
14676#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014677 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678}
14679
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014680/*
14681 * "inputlist()" function
14682 */
14683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014684f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014685{
14686 listitem_T *li;
14687 int selected;
14688 int mouse_used;
14689
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014690#ifdef NO_CONSOLE_INPUT
14691 /* While starting up, there is no place to enter text. */
14692 if (no_console_input())
14693 return;
14694#endif
14695 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14696 {
14697 EMSG2(_(e_listarg), "inputlist()");
14698 return;
14699 }
14700
14701 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014702 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014703 lines_left = Rows; /* avoid more prompt */
14704 msg_scroll = TRUE;
14705 msg_clr_eos();
14706
14707 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14708 {
14709 msg_puts(get_tv_string(&li->li_tv));
14710 msg_putchar('\n');
14711 }
14712
14713 /* Ask for choice. */
14714 selected = prompt_for_number(&mouse_used);
14715 if (mouse_used)
14716 selected -= lines_left;
14717
14718 rettv->vval.v_number = selected;
14719}
14720
14721
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14723
14724/*
14725 * "inputrestore()" function
14726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014728f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729{
14730 if (ga_userinput.ga_len > 0)
14731 {
14732 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14734 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014735 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 }
14737 else if (p_verbose > 1)
14738 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014739 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014740 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 }
14742}
14743
14744/*
14745 * "inputsave()" function
14746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014748f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014750 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 if (ga_grow(&ga_userinput, 1) == OK)
14752 {
14753 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14754 + ga_userinput.ga_len);
14755 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014756 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 }
14758 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014759 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760}
14761
14762/*
14763 * "inputsecret()" function
14764 */
14765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014766f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767{
14768 ++cmdline_star;
14769 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014770 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 --cmdline_star;
14772 --inputsecret_flag;
14773}
14774
14775/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014776 * "insert()" function
14777 */
14778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014779f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014780{
14781 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014782 listitem_T *item;
14783 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014785
14786 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014787 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014788 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014789 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014790 {
14791 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014792 before = get_tv_number_chk(&argvars[2], &error);
14793 if (error)
14794 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014795
Bram Moolenaar758711c2005-02-02 23:11:38 +000014796 if (before == l->lv_len)
14797 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014798 else
14799 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014800 item = list_find(l, before);
14801 if (item == NULL)
14802 {
14803 EMSGN(_(e_listidx), before);
14804 l = NULL;
14805 }
14806 }
14807 if (l != NULL)
14808 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014809 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014810 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014811 }
14812 }
14813}
14814
14815/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014816 * "invert(expr)" function
14817 */
14818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014819f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014820{
14821 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14822}
14823
14824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 * "isdirectory()" function
14826 */
14827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014828f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014830 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831}
14832
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014833/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014834 * "islocked()" function
14835 */
14836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014837f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014838{
14839 lval_T lv;
14840 char_u *end;
14841 dictitem_T *di;
14842
14843 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014844 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14845 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014846 if (end != NULL && lv.ll_name != NULL)
14847 {
14848 if (*end != NUL)
14849 EMSG(_(e_trailing));
14850 else
14851 {
14852 if (lv.ll_tv == NULL)
14853 {
14854 if (check_changedtick(lv.ll_name))
14855 rettv->vval.v_number = 1; /* always locked */
14856 else
14857 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014858 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014859 if (di != NULL)
14860 {
14861 /* Consider a variable locked when:
14862 * 1. the variable itself is locked
14863 * 2. the value of the variable is locked.
14864 * 3. the List or Dict value is locked.
14865 */
14866 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14867 || tv_islocked(&di->di_tv));
14868 }
14869 }
14870 }
14871 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014872 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014873 else if (lv.ll_newkey != NULL)
14874 EMSG2(_(e_dictkey), lv.ll_newkey);
14875 else if (lv.ll_list != NULL)
14876 /* List item. */
14877 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14878 else
14879 /* Dictionary item. */
14880 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14881 }
14882 }
14883
14884 clear_lval(&lv);
14885}
14886
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014887#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14888/*
14889 * "isnan()" function
14890 */
14891 static void
14892f_isnan(typval_T *argvars, typval_T *rettv)
14893{
14894 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14895 && isnan(argvars[0].vval.v_float);
14896}
14897#endif
14898
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014899static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014900
14901/*
14902 * Turn a dict into a list:
14903 * "what" == 0: list of keys
14904 * "what" == 1: list of values
14905 * "what" == 2: list of items
14906 */
14907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014908dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014909{
Bram Moolenaar33570922005-01-25 22:26:29 +000014910 list_T *l2;
14911 dictitem_T *di;
14912 hashitem_T *hi;
14913 listitem_T *li;
14914 listitem_T *li2;
14915 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014916 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014917
Bram Moolenaar8c711452005-01-14 21:53:12 +000014918 if (argvars[0].v_type != VAR_DICT)
14919 {
14920 EMSG(_(e_dictreq));
14921 return;
14922 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014923 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014924 return;
14925
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014926 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014927 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014928
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014929 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014930 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014931 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014932 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014933 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014934 --todo;
14935 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014936
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014937 li = listitem_alloc();
14938 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014939 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014940 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014941
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014942 if (what == 0)
14943 {
14944 /* keys() */
14945 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014946 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014947 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14948 }
14949 else if (what == 1)
14950 {
14951 /* values() */
14952 copy_tv(&di->di_tv, &li->li_tv);
14953 }
14954 else
14955 {
14956 /* items() */
14957 l2 = list_alloc();
14958 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014959 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014960 li->li_tv.vval.v_list = l2;
14961 if (l2 == NULL)
14962 break;
14963 ++l2->lv_refcount;
14964
14965 li2 = listitem_alloc();
14966 if (li2 == NULL)
14967 break;
14968 list_append(l2, li2);
14969 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014970 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014971 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14972
14973 li2 = listitem_alloc();
14974 if (li2 == NULL)
14975 break;
14976 list_append(l2, li2);
14977 copy_tv(&di->di_tv, &li2->li_tv);
14978 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014979 }
14980 }
14981}
14982
14983/*
14984 * "items(dict)" function
14985 */
14986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014987f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014988{
14989 dict_list(argvars, rettv, 2);
14990}
14991
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014992#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014993/*
14994 * Get the job from the argument.
14995 * Returns NULL if the job is invalid.
14996 */
14997 static job_T *
14998get_job_arg(typval_T *tv)
14999{
15000 job_T *job;
15001
15002 if (tv->v_type != VAR_JOB)
15003 {
15004 EMSG2(_(e_invarg2), get_tv_string(tv));
15005 return NULL;
15006 }
15007 job = tv->vval.v_job;
15008
15009 if (job == NULL)
15010 EMSG(_("E916: not a valid job"));
15011 return job;
15012}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015013
Bram Moolenaar835dc632016-02-07 14:27:38 +010015014/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015015 * "job_getchannel()" function
15016 */
15017 static void
15018f_job_getchannel(typval_T *argvars, typval_T *rettv)
15019{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015020 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015021
Bram Moolenaar65edff82016-02-21 16:40:11 +010015022 if (job != NULL)
15023 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015024 rettv->v_type = VAR_CHANNEL;
15025 rettv->vval.v_channel = job->jv_channel;
15026 if (job->jv_channel != NULL)
15027 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015028 }
15029}
15030
15031/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015032 * "job_info()" function
15033 */
15034 static void
15035f_job_info(typval_T *argvars, typval_T *rettv)
15036{
15037 job_T *job = get_job_arg(&argvars[0]);
15038
15039 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15040 job_info(job, rettv->vval.v_dict);
15041}
15042
15043/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015044 * "job_setoptions()" function
15045 */
15046 static void
15047f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15048{
15049 job_T *job = get_job_arg(&argvars[0]);
15050 jobopt_T opt;
15051
15052 if (job == NULL)
15053 return;
15054 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015055 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15056 job_set_options(job, &opt);
15057 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015058}
15059
15060/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015061 * "job_start()" function
15062 */
15063 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015064f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015065{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015066 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015067 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015068}
15069
15070/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015071 * "job_status()" function
15072 */
15073 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015074f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015075{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015076 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015077
Bram Moolenaar65edff82016-02-21 16:40:11 +010015078 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015079 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015080 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015081 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015082 }
15083}
15084
15085/*
15086 * "job_stop()" function
15087 */
15088 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015089f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015090{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015091 job_T *job = get_job_arg(&argvars[0]);
15092
15093 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015094 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015095}
15096#endif
15097
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015099 * "join()" function
15100 */
15101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015102f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015103{
15104 garray_T ga;
15105 char_u *sep;
15106
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015107 if (argvars[0].v_type != VAR_LIST)
15108 {
15109 EMSG(_(e_listreq));
15110 return;
15111 }
15112 if (argvars[0].vval.v_list == NULL)
15113 return;
15114 if (argvars[1].v_type == VAR_UNKNOWN)
15115 sep = (char_u *)" ";
15116 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015117 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015118
15119 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015120
15121 if (sep != NULL)
15122 {
15123 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015124 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015125 ga_append(&ga, NUL);
15126 rettv->vval.v_string = (char_u *)ga.ga_data;
15127 }
15128 else
15129 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015130}
15131
15132/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015133 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015134 */
15135 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015136f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015137{
15138 js_read_T reader;
15139
15140 reader.js_buf = get_tv_string(&argvars[0]);
15141 reader.js_fill = NULL;
15142 reader.js_used = 0;
15143 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15144 EMSG(_(e_invarg));
15145}
15146
15147/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015148 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015149 */
15150 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015151f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015152{
15153 rettv->v_type = VAR_STRING;
15154 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15155}
15156
15157/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015158 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015159 */
15160 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015161f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015162{
15163 js_read_T reader;
15164
15165 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015166 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015167 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015168 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015169 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015170}
15171
15172/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015173 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015174 */
15175 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015176f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015177{
15178 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015179 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015180}
15181
15182/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015183 * "keys()" function
15184 */
15185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015186f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015187{
15188 dict_list(argvars, rettv, 0);
15189}
15190
15191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 * "last_buffer_nr()" function.
15193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015195f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196{
15197 int n = 0;
15198 buf_T *buf;
15199
15200 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15201 if (n < buf->b_fnum)
15202 n = buf->b_fnum;
15203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015205}
15206
15207/*
15208 * "len()" function
15209 */
15210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015211f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015212{
15213 switch (argvars[0].v_type)
15214 {
15215 case VAR_STRING:
15216 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015217 rettv->vval.v_number = (varnumber_T)STRLEN(
15218 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015219 break;
15220 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015221 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015222 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015223 case VAR_DICT:
15224 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15225 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015226 case VAR_UNKNOWN:
15227 case VAR_SPECIAL:
15228 case VAR_FLOAT:
15229 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015230 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015231 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015232 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015233 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015234 break;
15235 }
15236}
15237
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015238static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015239
15240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015241libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015242{
15243#ifdef FEAT_LIBCALL
15244 char_u *string_in;
15245 char_u **string_result;
15246 int nr_result;
15247#endif
15248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015249 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015250 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015251 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015252
15253 if (check_restricted() || check_secure())
15254 return;
15255
15256#ifdef FEAT_LIBCALL
15257 /* The first two args must be strings, otherwise its meaningless */
15258 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15259 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015260 string_in = NULL;
15261 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015262 string_in = argvars[2].vval.v_string;
15263 if (type == VAR_NUMBER)
15264 string_result = NULL;
15265 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015266 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015267 if (mch_libcall(argvars[0].vval.v_string,
15268 argvars[1].vval.v_string,
15269 string_in,
15270 argvars[2].vval.v_number,
15271 string_result,
15272 &nr_result) == OK
15273 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015274 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015275 }
15276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277}
15278
15279/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280 * "libcall()" function
15281 */
15282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015283f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284{
15285 libcall_common(argvars, rettv, VAR_STRING);
15286}
15287
15288/*
15289 * "libcallnr()" function
15290 */
15291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015292f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015293{
15294 libcall_common(argvars, rettv, VAR_NUMBER);
15295}
15296
15297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 * "line(string)" function
15299 */
15300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015301f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302{
15303 linenr_T lnum = 0;
15304 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015305 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015307 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 if (fp != NULL)
15309 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015310 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311}
15312
15313/*
15314 * "line2byte(lnum)" function
15315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015317f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318{
15319#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015320 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321#else
15322 linenr_T lnum;
15323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015324 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015326 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015328 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15329 if (rettv->vval.v_number >= 0)
15330 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331#endif
15332}
15333
15334/*
15335 * "lispindent(lnum)" function
15336 */
15337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339{
15340#ifdef FEAT_LISP
15341 pos_T pos;
15342 linenr_T lnum;
15343
15344 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015345 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15347 {
15348 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 curwin->w_cursor = pos;
15351 }
15352 else
15353#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015354 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355}
15356
15357/*
15358 * "localtime()" function
15359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015361f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364}
15365
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015366static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367
15368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015369get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370{
15371 char_u *keys;
15372 char_u *which;
15373 char_u buf[NUMBUFLEN];
15374 char_u *keys_buf = NULL;
15375 char_u *rhs;
15376 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015377 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015378 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015379 mapblock_T *mp;
15380 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381
15382 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383 rettv->v_type = VAR_STRING;
15384 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 if (*keys == NUL)
15388 return;
15389
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015390 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015392 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015393 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015394 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015395 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015396 if (argvars[3].v_type != VAR_UNKNOWN)
15397 get_dict = get_tv_number(&argvars[3]);
15398 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 else
15401 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015402 if (which == NULL)
15403 return;
15404
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 mode = get_map_mode(&which, 0);
15406
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015407 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015408 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015410
15411 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015413 /* Return a string. */
15414 if (rhs != NULL)
15415 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416
Bram Moolenaarbd743252010-10-20 21:23:33 +020015417 }
15418 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15419 {
15420 /* Return a dictionary. */
15421 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15422 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15423 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424
Bram Moolenaarbd743252010-10-20 21:23:33 +020015425 dict_add_nr_str(dict, "lhs", 0L, lhs);
15426 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15427 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15428 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15429 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15430 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15431 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015432 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015433 dict_add_nr_str(dict, "mode", 0L, mapmode);
15434
15435 vim_free(lhs);
15436 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 }
15438}
15439
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015440#ifdef FEAT_FLOAT
15441/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015442 * "log()" function
15443 */
15444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015445f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015446{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015447 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015448
15449 rettv->v_type = VAR_FLOAT;
15450 if (get_float_arg(argvars, &f) == OK)
15451 rettv->vval.v_float = log(f);
15452 else
15453 rettv->vval.v_float = 0.0;
15454}
15455
15456/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015457 * "log10()" function
15458 */
15459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015460f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015461{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015462 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015463
15464 rettv->v_type = VAR_FLOAT;
15465 if (get_float_arg(argvars, &f) == OK)
15466 rettv->vval.v_float = log10(f);
15467 else
15468 rettv->vval.v_float = 0.0;
15469}
15470#endif
15471
Bram Moolenaar1dced572012-04-05 16:54:08 +020015472#ifdef FEAT_LUA
15473/*
15474 * "luaeval()" function
15475 */
15476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015477f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015478{
15479 char_u *str;
15480 char_u buf[NUMBUFLEN];
15481
15482 str = get_tv_string_buf(&argvars[0], buf);
15483 do_luaeval(str, argvars + 1, rettv);
15484}
15485#endif
15486
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015488 * "map()" function
15489 */
15490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015491f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015492{
15493 filter_map(argvars, rettv, TRUE);
15494}
15495
15496/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015497 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 */
15499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015500f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015502 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503}
15504
15505/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015506 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 */
15508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015509f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015511 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512}
15513
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015514static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515
15516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015517find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015519 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015520 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015521 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522 char_u *pat;
15523 regmatch_T regmatch;
15524 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015525 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 char_u *save_cpo;
15527 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015528 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015529 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015530 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015531 list_T *l = NULL;
15532 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015533 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015534 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535
15536 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15537 save_cpo = p_cpo;
15538 p_cpo = (char_u *)"";
15539
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015540 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015541 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015542 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015543 /* type 3: return empty list when there are no matches.
15544 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015545 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015546 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015547 if (type == 4
15548 && (list_append_string(rettv->vval.v_list,
15549 (char_u *)"", 0) == FAIL
15550 || list_append_number(rettv->vval.v_list,
15551 (varnumber_T)-1) == FAIL
15552 || list_append_number(rettv->vval.v_list,
15553 (varnumber_T)-1) == FAIL
15554 || list_append_number(rettv->vval.v_list,
15555 (varnumber_T)-1) == FAIL))
15556 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015557 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015558 rettv->vval.v_list = NULL;
15559 goto theend;
15560 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015561 }
15562 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015564 rettv->v_type = VAR_STRING;
15565 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015568 if (argvars[0].v_type == VAR_LIST)
15569 {
15570 if ((l = argvars[0].vval.v_list) == NULL)
15571 goto theend;
15572 li = l->lv_first;
15573 }
15574 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015575 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015576 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015577 len = (long)STRLEN(str);
15578 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015580 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15581 if (pat == NULL)
15582 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015583
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015584 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015586 int error = FALSE;
15587
15588 start = get_tv_number_chk(&argvars[2], &error);
15589 if (error)
15590 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015591 if (l != NULL)
15592 {
15593 li = list_find(l, start);
15594 if (li == NULL)
15595 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015596 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015597 }
15598 else
15599 {
15600 if (start < 0)
15601 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015602 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015603 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015604 /* When "count" argument is there ignore matches before "start",
15605 * otherwise skip part of the string. Differs when pattern is "^"
15606 * or "\<". */
15607 if (argvars[3].v_type != VAR_UNKNOWN)
15608 startcol = start;
15609 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015610 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015611 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015612 len -= start;
15613 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015614 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015615
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015616 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015617 nth = get_tv_number_chk(&argvars[3], &error);
15618 if (error)
15619 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 }
15621
15622 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15623 if (regmatch.regprog != NULL)
15624 {
15625 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015627 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015628 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015629 if (l != NULL)
15630 {
15631 if (li == NULL)
15632 {
15633 match = FALSE;
15634 break;
15635 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015636 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015637 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015638 if (str == NULL)
15639 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015640 }
15641
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015642 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015643
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015644 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015645 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015646 if (l == NULL && !match)
15647 break;
15648
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015649 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015650 if (l != NULL)
15651 {
15652 li = li->li_next;
15653 ++idx;
15654 }
15655 else
15656 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015657#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015658 startcol = (colnr_T)(regmatch.startp[0]
15659 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015660#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015661 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015662#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015663 if (startcol > (colnr_T)len
15664 || str + startcol <= regmatch.startp[0])
15665 {
15666 match = FALSE;
15667 break;
15668 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015669 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015670 }
15671
15672 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015674 if (type == 4)
15675 {
15676 listitem_T *li1 = rettv->vval.v_list->lv_first;
15677 listitem_T *li2 = li1->li_next;
15678 listitem_T *li3 = li2->li_next;
15679 listitem_T *li4 = li3->li_next;
15680
15681 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15682 (int)(regmatch.endp[0] - regmatch.startp[0]));
15683 li3->li_tv.vval.v_number =
15684 (varnumber_T)(regmatch.startp[0] - expr);
15685 li4->li_tv.vval.v_number =
15686 (varnumber_T)(regmatch.endp[0] - expr);
15687 if (l != NULL)
15688 li2->li_tv.vval.v_number = (varnumber_T)idx;
15689 }
15690 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015691 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015692 int i;
15693
15694 /* return list with matched string and submatches */
15695 for (i = 0; i < NSUBEXP; ++i)
15696 {
15697 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015698 {
15699 if (list_append_string(rettv->vval.v_list,
15700 (char_u *)"", 0) == FAIL)
15701 break;
15702 }
15703 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015704 regmatch.startp[i],
15705 (int)(regmatch.endp[i] - regmatch.startp[i]))
15706 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015707 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015708 }
15709 }
15710 else if (type == 2)
15711 {
15712 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015713 if (l != NULL)
15714 copy_tv(&li->li_tv, rettv);
15715 else
15716 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015718 }
15719 else if (l != NULL)
15720 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 else
15722 {
15723 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015724 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 (varnumber_T)(regmatch.startp[0] - str);
15726 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015729 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 }
15731 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015732 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 }
15734
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015735 if (type == 4 && l == NULL)
15736 /* matchstrpos() without a list: drop the second item. */
15737 listitem_remove(rettv->vval.v_list,
15738 rettv->vval.v_list->lv_first->li_next);
15739
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015741 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 p_cpo = save_cpo;
15743}
15744
15745/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746 * "match()" function
15747 */
15748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015749f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015750{
15751 find_some_match(argvars, rettv, 1);
15752}
15753
15754/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015755 * "matchadd()" function
15756 */
15757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015758f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015759{
15760#ifdef FEAT_SEARCH_EXTRA
15761 char_u buf[NUMBUFLEN];
15762 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15763 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15764 int prio = 10; /* default priority */
15765 int id = -1;
15766 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015767 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015768
15769 rettv->vval.v_number = -1;
15770
15771 if (grp == NULL || pat == NULL)
15772 return;
15773 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015774 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015775 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015776 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015777 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015778 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015779 if (argvars[4].v_type != VAR_UNKNOWN)
15780 {
15781 if (argvars[4].v_type != VAR_DICT)
15782 {
15783 EMSG(_(e_dictreq));
15784 return;
15785 }
15786 if (dict_find(argvars[4].vval.v_dict,
15787 (char_u *)"conceal", -1) != NULL)
15788 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15789 (char_u *)"conceal", FALSE);
15790 }
15791 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015792 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015793 if (error == TRUE)
15794 return;
15795 if (id >= 1 && id <= 3)
15796 {
15797 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15798 return;
15799 }
15800
Bram Moolenaar6561d522015-07-21 15:48:27 +020015801 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15802 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015803#endif
15804}
15805
15806/*
15807 * "matchaddpos()" function
15808 */
15809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015810f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015811{
15812#ifdef FEAT_SEARCH_EXTRA
15813 char_u buf[NUMBUFLEN];
15814 char_u *group;
15815 int prio = 10;
15816 int id = -1;
15817 int error = FALSE;
15818 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015819 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015820
15821 rettv->vval.v_number = -1;
15822
15823 group = get_tv_string_buf_chk(&argvars[0], buf);
15824 if (group == NULL)
15825 return;
15826
15827 if (argvars[1].v_type != VAR_LIST)
15828 {
15829 EMSG2(_(e_listarg), "matchaddpos()");
15830 return;
15831 }
15832 l = argvars[1].vval.v_list;
15833 if (l == NULL)
15834 return;
15835
15836 if (argvars[2].v_type != VAR_UNKNOWN)
15837 {
15838 prio = get_tv_number_chk(&argvars[2], &error);
15839 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015840 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015841 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015842 if (argvars[4].v_type != VAR_UNKNOWN)
15843 {
15844 if (argvars[4].v_type != VAR_DICT)
15845 {
15846 EMSG(_(e_dictreq));
15847 return;
15848 }
15849 if (dict_find(argvars[4].vval.v_dict,
15850 (char_u *)"conceal", -1) != NULL)
15851 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15852 (char_u *)"conceal", FALSE);
15853 }
15854 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015855 }
15856 if (error == TRUE)
15857 return;
15858
15859 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15860 if (id == 1 || id == 2)
15861 {
15862 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15863 return;
15864 }
15865
Bram Moolenaar6561d522015-07-21 15:48:27 +020015866 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15867 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015868#endif
15869}
15870
15871/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015872 * "matcharg()" function
15873 */
15874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015875f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015876{
15877 if (rettv_list_alloc(rettv) == OK)
15878 {
15879#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015880 int id = get_tv_number(&argvars[0]);
15881 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015882
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015883 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015884 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015885 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15886 {
15887 list_append_string(rettv->vval.v_list,
15888 syn_id2name(m->hlg_id), -1);
15889 list_append_string(rettv->vval.v_list, m->pattern, -1);
15890 }
15891 else
15892 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015893 list_append_string(rettv->vval.v_list, NULL, -1);
15894 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015895 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015896 }
15897#endif
15898 }
15899}
15900
15901/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015902 * "matchdelete()" function
15903 */
15904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015905f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015906{
15907#ifdef FEAT_SEARCH_EXTRA
15908 rettv->vval.v_number = match_delete(curwin,
15909 (int)get_tv_number(&argvars[0]), TRUE);
15910#endif
15911}
15912
15913/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015914 * "matchend()" function
15915 */
15916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015917f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015918{
15919 find_some_match(argvars, rettv, 0);
15920}
15921
15922/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015923 * "matchlist()" function
15924 */
15925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015926f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015927{
15928 find_some_match(argvars, rettv, 3);
15929}
15930
15931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015932 * "matchstr()" function
15933 */
15934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015935f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015936{
15937 find_some_match(argvars, rettv, 2);
15938}
15939
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015940/*
15941 * "matchstrpos()" function
15942 */
15943 static void
15944f_matchstrpos(typval_T *argvars, typval_T *rettv)
15945{
15946 find_some_match(argvars, rettv, 4);
15947}
15948
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015949static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015950
15951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015952max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015953{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015954 long n = 0;
15955 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015956 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015957
15958 if (argvars[0].v_type == VAR_LIST)
15959 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015960 list_T *l;
15961 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015962
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015963 l = argvars[0].vval.v_list;
15964 if (l != NULL)
15965 {
15966 li = l->lv_first;
15967 if (li != NULL)
15968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015969 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015970 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015971 {
15972 li = li->li_next;
15973 if (li == NULL)
15974 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015975 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015976 if (domax ? i > n : i < n)
15977 n = i;
15978 }
15979 }
15980 }
15981 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015982 else if (argvars[0].v_type == VAR_DICT)
15983 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015984 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015985 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015986 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015987 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015988
15989 d = argvars[0].vval.v_dict;
15990 if (d != NULL)
15991 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015992 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015993 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015994 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015995 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015996 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015997 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015998 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015999 if (first)
16000 {
16001 n = i;
16002 first = FALSE;
16003 }
16004 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016005 n = i;
16006 }
16007 }
16008 }
16009 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016010 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016011 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016012 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016013}
16014
16015/*
16016 * "max()" function
16017 */
16018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016019f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016020{
16021 max_min(argvars, rettv, TRUE);
16022}
16023
16024/*
16025 * "min()" function
16026 */
16027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016028f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016029{
16030 max_min(argvars, rettv, FALSE);
16031}
16032
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016033static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016034
16035/*
16036 * Create the directory in which "dir" is located, and higher levels when
16037 * needed.
16038 */
16039 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016040mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016041{
16042 char_u *p;
16043 char_u *updir;
16044 int r = FAIL;
16045
16046 /* Get end of directory name in "dir".
16047 * We're done when it's "/" or "c:/". */
16048 p = gettail_sep(dir);
16049 if (p <= get_past_head(dir))
16050 return OK;
16051
16052 /* If the directory exists we're done. Otherwise: create it.*/
16053 updir = vim_strnsave(dir, (int)(p - dir));
16054 if (updir == NULL)
16055 return FAIL;
16056 if (mch_isdir(updir))
16057 r = OK;
16058 else if (mkdir_recurse(updir, prot) == OK)
16059 r = vim_mkdir_emsg(updir, prot);
16060 vim_free(updir);
16061 return r;
16062}
16063
16064#ifdef vim_mkdir
16065/*
16066 * "mkdir()" function
16067 */
16068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016069f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016070{
16071 char_u *dir;
16072 char_u buf[NUMBUFLEN];
16073 int prot = 0755;
16074
16075 rettv->vval.v_number = FAIL;
16076 if (check_restricted() || check_secure())
16077 return;
16078
16079 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016080 if (*dir == NUL)
16081 rettv->vval.v_number = FAIL;
16082 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016083 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016084 if (*gettail(dir) == NUL)
16085 /* remove trailing slashes */
16086 *gettail_sep(dir) = NUL;
16087
16088 if (argvars[1].v_type != VAR_UNKNOWN)
16089 {
16090 if (argvars[2].v_type != VAR_UNKNOWN)
16091 prot = get_tv_number_chk(&argvars[2], NULL);
16092 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16093 mkdir_recurse(dir, prot);
16094 }
16095 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016096 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016097}
16098#endif
16099
Bram Moolenaar0d660222005-01-07 21:51:51 +000016100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 * "mode()" function
16102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016104f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016106 char_u buf[3];
16107
16108 buf[1] = NUL;
16109 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 if (VIsual_active)
16112 {
16113 if (VIsual_select)
16114 buf[0] = VIsual_mode + 's' - 'v';
16115 else
16116 buf[0] = VIsual_mode;
16117 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016118 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016119 || State == CONFIRM)
16120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016122 if (State == ASKMORE)
16123 buf[1] = 'm';
16124 else if (State == CONFIRM)
16125 buf[1] = '?';
16126 }
16127 else if (State == EXTERNCMD)
16128 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129 else if (State & INSERT)
16130 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016131#ifdef FEAT_VREPLACE
16132 if (State & VREPLACE_FLAG)
16133 {
16134 buf[0] = 'R';
16135 buf[1] = 'v';
16136 }
16137 else
16138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 if (State & REPLACE_FLAG)
16140 buf[0] = 'R';
16141 else
16142 buf[0] = 'i';
16143 }
16144 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016147 if (exmode_active)
16148 buf[1] = 'v';
16149 }
16150 else if (exmode_active)
16151 {
16152 buf[0] = 'c';
16153 buf[1] = 'e';
16154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016158 if (finish_op)
16159 buf[1] = 'o';
16160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016162 /* Clear out the minor mode when the argument is not a non-zero number or
16163 * non-empty string. */
16164 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016165 buf[1] = NUL;
16166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016167 rettv->vval.v_string = vim_strsave(buf);
16168 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169}
16170
Bram Moolenaar429fa852013-04-15 12:27:36 +020016171#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016172/*
16173 * "mzeval()" function
16174 */
16175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016176f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016177{
16178 char_u *str;
16179 char_u buf[NUMBUFLEN];
16180
16181 str = get_tv_string_buf(&argvars[0], buf);
16182 do_mzeval(str, rettv);
16183}
Bram Moolenaar75676462013-01-30 14:55:42 +010016184
16185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016186mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016187{
16188 typval_T argvars[3];
16189
16190 argvars[0].v_type = VAR_STRING;
16191 argvars[0].vval.v_string = name;
16192 copy_tv(args, &argvars[1]);
16193 argvars[2].v_type = VAR_UNKNOWN;
16194 f_call(argvars, rettv);
16195 clear_tv(&argvars[1]);
16196}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016197#endif
16198
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200 * "nextnonblank()" function
16201 */
16202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016203f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016204{
16205 linenr_T lnum;
16206
16207 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016209 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016210 {
16211 lnum = 0;
16212 break;
16213 }
16214 if (*skipwhite(ml_get(lnum)) != NUL)
16215 break;
16216 }
16217 rettv->vval.v_number = lnum;
16218}
16219
16220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 * "nr2char()" function
16222 */
16223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016224f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225{
16226 char_u buf[NUMBUFLEN];
16227
16228#ifdef FEAT_MBYTE
16229 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016230 {
16231 int utf8 = 0;
16232
16233 if (argvars[1].v_type != VAR_UNKNOWN)
16234 utf8 = get_tv_number_chk(&argvars[1], NULL);
16235 if (utf8)
16236 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16237 else
16238 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 else
16241#endif
16242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244 buf[1] = NUL;
16245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246 rettv->v_type = VAR_STRING;
16247 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016248}
16249
16250/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016251 * "or(expr, expr)" function
16252 */
16253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016254f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016255{
16256 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16257 | get_tv_number_chk(&argvars[1], NULL);
16258}
16259
16260/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016261 * "pathshorten()" function
16262 */
16263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016264f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016265{
16266 char_u *p;
16267
16268 rettv->v_type = VAR_STRING;
16269 p = get_tv_string_chk(&argvars[0]);
16270 if (p == NULL)
16271 rettv->vval.v_string = NULL;
16272 else
16273 {
16274 p = vim_strsave(p);
16275 rettv->vval.v_string = p;
16276 if (p != NULL)
16277 shorten_dir(p);
16278 }
16279}
16280
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016281#ifdef FEAT_PERL
16282/*
16283 * "perleval()" function
16284 */
16285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016286f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016287{
16288 char_u *str;
16289 char_u buf[NUMBUFLEN];
16290
16291 str = get_tv_string_buf(&argvars[0], buf);
16292 do_perleval(str, rettv);
16293}
16294#endif
16295
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016296#ifdef FEAT_FLOAT
16297/*
16298 * "pow()" function
16299 */
16300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016301f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016302{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016303 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016304
16305 rettv->v_type = VAR_FLOAT;
16306 if (get_float_arg(argvars, &fx) == OK
16307 && get_float_arg(&argvars[1], &fy) == OK)
16308 rettv->vval.v_float = pow(fx, fy);
16309 else
16310 rettv->vval.v_float = 0.0;
16311}
16312#endif
16313
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016314/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016315 * "prevnonblank()" function
16316 */
16317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016318f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016319{
16320 linenr_T lnum;
16321
16322 lnum = get_tv_lnum(argvars);
16323 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16324 lnum = 0;
16325 else
16326 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16327 --lnum;
16328 rettv->vval.v_number = lnum;
16329}
16330
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016331/* This dummy va_list is here because:
16332 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16333 * - locally in the function results in a "used before set" warning
16334 * - using va_start() to initialize it gives "function with fixed args" error */
16335static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016336
Bram Moolenaar8c711452005-01-14 21:53:12 +000016337/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016338 * "printf()" function
16339 */
16340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016341f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016342{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016343 char_u buf[NUMBUFLEN];
16344 int len;
16345 char_u *s;
16346 int saved_did_emsg = did_emsg;
16347 char *fmt;
16348
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016349 rettv->v_type = VAR_STRING;
16350 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016351
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016352 /* Get the required length, allocate the buffer and do it for real. */
16353 did_emsg = FALSE;
16354 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16355 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16356 if (!did_emsg)
16357 {
16358 s = alloc(len + 1);
16359 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016360 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016361 rettv->vval.v_string = s;
16362 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016363 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016364 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016365 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016366}
16367
16368/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016369 * "pumvisible()" function
16370 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016372f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016373{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016374#ifdef FEAT_INS_EXPAND
16375 if (pum_visible())
16376 rettv->vval.v_number = 1;
16377#endif
16378}
16379
Bram Moolenaardb913952012-06-29 12:54:53 +020016380#ifdef FEAT_PYTHON3
16381/*
16382 * "py3eval()" function
16383 */
16384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016385f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016386{
16387 char_u *str;
16388 char_u buf[NUMBUFLEN];
16389
16390 str = get_tv_string_buf(&argvars[0], buf);
16391 do_py3eval(str, rettv);
16392}
16393#endif
16394
16395#ifdef FEAT_PYTHON
16396/*
16397 * "pyeval()" function
16398 */
16399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016400f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016401{
16402 char_u *str;
16403 char_u buf[NUMBUFLEN];
16404
16405 str = get_tv_string_buf(&argvars[0], buf);
16406 do_pyeval(str, rettv);
16407}
16408#endif
16409
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016410/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016411 * "range()" function
16412 */
16413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016414f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016415{
16416 long start;
16417 long end;
16418 long stride = 1;
16419 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016420 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016422 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016423 if (argvars[1].v_type == VAR_UNKNOWN)
16424 {
16425 end = start - 1;
16426 start = 0;
16427 }
16428 else
16429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016430 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016431 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016432 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016433 }
16434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016435 if (error)
16436 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016437 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016438 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016439 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016440 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016441 else
16442 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016443 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016444 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016445 if (list_append_number(rettv->vval.v_list,
16446 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016447 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016448 }
16449}
16450
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016451/*
16452 * "readfile()" function
16453 */
16454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016455f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016456{
16457 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016458 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016459 char_u *fname;
16460 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016461 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16462 int io_size = sizeof(buf);
16463 int readlen; /* size of last fread() */
16464 char_u *prev = NULL; /* previously read bytes, if any */
16465 long prevlen = 0; /* length of data in prev */
16466 long prevsize = 0; /* size of prev buffer */
16467 long maxline = MAXLNUM;
16468 long cnt = 0;
16469 char_u *p; /* position in buf */
16470 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016471
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016472 if (argvars[1].v_type != VAR_UNKNOWN)
16473 {
16474 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16475 binary = TRUE;
16476 if (argvars[2].v_type != VAR_UNKNOWN)
16477 maxline = get_tv_number(&argvars[2]);
16478 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016479
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016480 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016481 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016482
16483 /* Always open the file in binary mode, library functions have a mind of
16484 * their own about CR-LF conversion. */
16485 fname = get_tv_string(&argvars[0]);
16486 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16487 {
16488 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16489 return;
16490 }
16491
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016492 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016493 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016494 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016495
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016496 /* This for loop processes what was read, but is also entered at end
16497 * of file so that either:
16498 * - an incomplete line gets written
16499 * - a "binary" file gets an empty line at the end if it ends in a
16500 * newline. */
16501 for (p = buf, start = buf;
16502 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16503 ++p)
16504 {
16505 if (*p == '\n' || readlen <= 0)
16506 {
16507 listitem_T *li;
16508 char_u *s = NULL;
16509 long_u len = p - start;
16510
16511 /* Finished a line. Remove CRs before NL. */
16512 if (readlen > 0 && !binary)
16513 {
16514 while (len > 0 && start[len - 1] == '\r')
16515 --len;
16516 /* removal may cross back to the "prev" string */
16517 if (len == 0)
16518 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16519 --prevlen;
16520 }
16521 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016522 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016523 else
16524 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016525 /* Change "prev" buffer to be the right size. This way
16526 * the bytes are only copied once, and very long lines are
16527 * allocated only once. */
16528 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016529 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016530 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016531 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016532 prev = NULL; /* the list will own the string */
16533 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016534 }
16535 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016536 if (s == NULL)
16537 {
16538 do_outofmem_msg((long_u) prevlen + len + 1);
16539 failed = TRUE;
16540 break;
16541 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016542
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016543 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016544 {
16545 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016546 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016547 break;
16548 }
16549 li->li_tv.v_type = VAR_STRING;
16550 li->li_tv.v_lock = 0;
16551 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016552 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016553
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016554 start = p + 1; /* step over newline */
16555 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556 break;
16557 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016558 else if (*p == NUL)
16559 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016560#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016561 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16562 * when finding the BF and check the previous two bytes. */
16563 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016564 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016565 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16566 * + 1, these may be in the "prev" string. */
16567 char_u back1 = p >= buf + 1 ? p[-1]
16568 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16569 char_u back2 = p >= buf + 2 ? p[-2]
16570 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16571 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016572
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016573 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016574 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016575 char_u *dest = p - 2;
16576
16577 /* Usually a BOM is at the beginning of a file, and so at
16578 * the beginning of a line; then we can just step over it.
16579 */
16580 if (start == dest)
16581 start = p + 1;
16582 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016583 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016584 /* have to shuffle buf to close gap */
16585 int adjust_prevlen = 0;
16586
16587 if (dest < buf)
16588 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016589 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016590 dest = buf;
16591 }
16592 if (readlen > p - buf + 1)
16593 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16594 readlen -= 3 - adjust_prevlen;
16595 prevlen -= adjust_prevlen;
16596 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016597 }
16598 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016599 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016600#endif
16601 } /* for */
16602
16603 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16604 break;
16605 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016606 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016607 /* There's part of a line in buf, store it in "prev". */
16608 if (p - start + prevlen >= prevsize)
16609 {
16610 /* need bigger "prev" buffer */
16611 char_u *newprev;
16612
16613 /* A common use case is ordinary text files and "prev" gets a
16614 * fragment of a line, so the first allocation is made
16615 * small, to avoid repeatedly 'allocing' large and
16616 * 'reallocing' small. */
16617 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016618 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016619 else
16620 {
16621 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016622 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016623 prevsize = grow50pc > growmin ? grow50pc : growmin;
16624 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016625 newprev = prev == NULL ? alloc(prevsize)
16626 : vim_realloc(prev, prevsize);
16627 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016628 {
16629 do_outofmem_msg((long_u)prevsize);
16630 failed = TRUE;
16631 break;
16632 }
16633 prev = newprev;
16634 }
16635 /* Add the line part to end of "prev". */
16636 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016637 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016638 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016639 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016640
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016641 /*
16642 * For a negative line count use only the lines at the end of the file,
16643 * free the rest.
16644 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016645 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016646 while (cnt > -maxline)
16647 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016648 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016649 --cnt;
16650 }
16651
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016652 if (failed)
16653 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016654 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016655 /* readfile doc says an empty list is returned on error */
16656 rettv->vval.v_list = list_alloc();
16657 }
16658
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016659 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016660 fclose(fd);
16661}
16662
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016663#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016664static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016665
16666/*
16667 * Convert a List to proftime_T.
16668 * Return FAIL when there is something wrong.
16669 */
16670 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016671list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016672{
16673 long n1, n2;
16674 int error = FALSE;
16675
16676 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16677 || arg->vval.v_list->lv_len != 2)
16678 return FAIL;
16679 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16680 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16681# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016682 tm->HighPart = n1;
16683 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016684# else
16685 tm->tv_sec = n1;
16686 tm->tv_usec = n2;
16687# endif
16688 return error ? FAIL : OK;
16689}
16690#endif /* FEAT_RELTIME */
16691
16692/*
16693 * "reltime()" function
16694 */
16695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016696f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016697{
16698#ifdef FEAT_RELTIME
16699 proftime_T res;
16700 proftime_T start;
16701
16702 if (argvars[0].v_type == VAR_UNKNOWN)
16703 {
16704 /* No arguments: get current time. */
16705 profile_start(&res);
16706 }
16707 else if (argvars[1].v_type == VAR_UNKNOWN)
16708 {
16709 if (list2proftime(&argvars[0], &res) == FAIL)
16710 return;
16711 profile_end(&res);
16712 }
16713 else
16714 {
16715 /* Two arguments: compute the difference. */
16716 if (list2proftime(&argvars[0], &start) == FAIL
16717 || list2proftime(&argvars[1], &res) == FAIL)
16718 return;
16719 profile_sub(&res, &start);
16720 }
16721
16722 if (rettv_list_alloc(rettv) == OK)
16723 {
16724 long n1, n2;
16725
16726# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016727 n1 = res.HighPart;
16728 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016729# else
16730 n1 = res.tv_sec;
16731 n2 = res.tv_usec;
16732# endif
16733 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16734 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16735 }
16736#endif
16737}
16738
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016739#ifdef FEAT_FLOAT
16740/*
16741 * "reltimefloat()" function
16742 */
16743 static void
16744f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16745{
16746# ifdef FEAT_RELTIME
16747 proftime_T tm;
16748# endif
16749
16750 rettv->v_type = VAR_FLOAT;
16751 rettv->vval.v_float = 0;
16752# ifdef FEAT_RELTIME
16753 if (list2proftime(&argvars[0], &tm) == OK)
16754 rettv->vval.v_float = profile_float(&tm);
16755# endif
16756}
16757#endif
16758
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016759/*
16760 * "reltimestr()" function
16761 */
16762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016763f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016764{
16765#ifdef FEAT_RELTIME
16766 proftime_T tm;
16767#endif
16768
16769 rettv->v_type = VAR_STRING;
16770 rettv->vval.v_string = NULL;
16771#ifdef FEAT_RELTIME
16772 if (list2proftime(&argvars[0], &tm) == OK)
16773 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16774#endif
16775}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016776
Bram Moolenaar0d660222005-01-07 21:51:51 +000016777#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016778static void make_connection(void);
16779static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016780
16781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016782make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783{
16784 if (X_DISPLAY == NULL
16785# ifdef FEAT_GUI
16786 && !gui.in_use
16787# endif
16788 )
16789 {
16790 x_force_connect = TRUE;
16791 setup_term_clip();
16792 x_force_connect = FALSE;
16793 }
16794}
16795
16796 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016797check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016798{
16799 make_connection();
16800 if (X_DISPLAY == NULL)
16801 {
16802 EMSG(_("E240: No connection to Vim server"));
16803 return FAIL;
16804 }
16805 return OK;
16806}
16807#endif
16808
16809#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016810static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016811
16812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016813remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016814{
16815 char_u *server_name;
16816 char_u *keys;
16817 char_u *r = NULL;
16818 char_u buf[NUMBUFLEN];
16819# ifdef WIN32
16820 HWND w;
16821# else
16822 Window w;
16823# endif
16824
16825 if (check_restricted() || check_secure())
16826 return;
16827
16828# ifdef FEAT_X11
16829 if (check_connection() == FAIL)
16830 return;
16831# endif
16832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016833 server_name = get_tv_string_chk(&argvars[0]);
16834 if (server_name == NULL)
16835 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016836 keys = get_tv_string_buf(&argvars[1], buf);
16837# ifdef WIN32
16838 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16839# else
16840 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16841 < 0)
16842# endif
16843 {
16844 if (r != NULL)
16845 EMSG(r); /* sending worked but evaluation failed */
16846 else
16847 EMSG2(_("E241: Unable to send to %s"), server_name);
16848 return;
16849 }
16850
16851 rettv->vval.v_string = r;
16852
16853 if (argvars[2].v_type != VAR_UNKNOWN)
16854 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016855 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016856 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016857 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016858
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016859 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 v.di_tv.v_type = VAR_STRING;
16861 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016862 idvar = get_tv_string_chk(&argvars[2]);
16863 if (idvar != NULL)
16864 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016865 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016866 }
16867}
16868#endif
16869
16870/*
16871 * "remote_expr()" function
16872 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016874f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875{
16876 rettv->v_type = VAR_STRING;
16877 rettv->vval.v_string = NULL;
16878#ifdef FEAT_CLIENTSERVER
16879 remote_common(argvars, rettv, TRUE);
16880#endif
16881}
16882
16883/*
16884 * "remote_foreground()" function
16885 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016887f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016888{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889#ifdef FEAT_CLIENTSERVER
16890# ifdef WIN32
16891 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016892 {
16893 char_u *server_name = get_tv_string_chk(&argvars[0]);
16894
16895 if (server_name != NULL)
16896 serverForeground(server_name);
16897 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898# else
16899 /* Send a foreground() expression to the server. */
16900 argvars[1].v_type = VAR_STRING;
16901 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16902 argvars[2].v_type = VAR_UNKNOWN;
16903 remote_common(argvars, rettv, TRUE);
16904 vim_free(argvars[1].vval.v_string);
16905# endif
16906#endif
16907}
16908
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016910f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911{
16912#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914 char_u *s = NULL;
16915# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016916 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016918 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016919
16920 if (check_restricted() || check_secure())
16921 {
16922 rettv->vval.v_number = -1;
16923 return;
16924 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016925 serverid = get_tv_string_chk(&argvars[0]);
16926 if (serverid == NULL)
16927 {
16928 rettv->vval.v_number = -1;
16929 return; /* type error; errmsg already given */
16930 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016932 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 if (n == 0)
16934 rettv->vval.v_number = -1;
16935 else
16936 {
16937 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16938 rettv->vval.v_number = (s != NULL);
16939 }
16940# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941 if (check_connection() == FAIL)
16942 return;
16943
16944 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016945 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946# endif
16947
16948 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 char_u *retvar;
16951
Bram Moolenaar33570922005-01-25 22:26:29 +000016952 v.di_tv.v_type = VAR_STRING;
16953 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016954 retvar = get_tv_string_chk(&argvars[1]);
16955 if (retvar != NULL)
16956 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016957 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 }
16959#else
16960 rettv->vval.v_number = -1;
16961#endif
16962}
16963
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016965f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966{
16967 char_u *r = NULL;
16968
16969#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016970 char_u *serverid = get_tv_string_chk(&argvars[0]);
16971
16972 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 {
16974# ifdef WIN32
16975 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016976 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016978 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 if (n != 0)
16980 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16981 if (r == NULL)
16982# else
16983 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985# endif
16986 EMSG(_("E277: Unable to read a server reply"));
16987 }
16988#endif
16989 rettv->v_type = VAR_STRING;
16990 rettv->vval.v_string = r;
16991}
16992
16993/*
16994 * "remote_send()" function
16995 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016997f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998{
16999 rettv->v_type = VAR_STRING;
17000 rettv->vval.v_string = NULL;
17001#ifdef FEAT_CLIENTSERVER
17002 remote_common(argvars, rettv, FALSE);
17003#endif
17004}
17005
17006/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017007 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017008 */
17009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017010f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017011{
Bram Moolenaar33570922005-01-25 22:26:29 +000017012 list_T *l;
17013 listitem_T *item, *item2;
17014 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017015 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017016 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017017 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017018 dict_T *d;
17019 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017020 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017021
Bram Moolenaar8c711452005-01-14 21:53:12 +000017022 if (argvars[0].v_type == VAR_DICT)
17023 {
17024 if (argvars[2].v_type != VAR_UNKNOWN)
17025 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017026 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017027 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 key = get_tv_string_chk(&argvars[1]);
17030 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017032 di = dict_find(d, key, -1);
17033 if (di == NULL)
17034 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017035 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17036 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017037 {
17038 *rettv = di->di_tv;
17039 init_tv(&di->di_tv);
17040 dictitem_remove(d, di);
17041 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017043 }
17044 }
17045 else if (argvars[0].v_type != VAR_LIST)
17046 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017047 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017048 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017050 int error = FALSE;
17051
17052 idx = get_tv_number_chk(&argvars[1], &error);
17053 if (error)
17054 ; /* type error: do nothing, errmsg already given */
17055 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017056 EMSGN(_(e_listidx), idx);
17057 else
17058 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017059 if (argvars[2].v_type == VAR_UNKNOWN)
17060 {
17061 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017062 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017063 *rettv = item->li_tv;
17064 vim_free(item);
17065 }
17066 else
17067 {
17068 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017069 end = get_tv_number_chk(&argvars[2], &error);
17070 if (error)
17071 ; /* type error: do nothing */
17072 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017073 EMSGN(_(e_listidx), end);
17074 else
17075 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017076 int cnt = 0;
17077
17078 for (li = item; li != NULL; li = li->li_next)
17079 {
17080 ++cnt;
17081 if (li == item2)
17082 break;
17083 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017084 if (li == NULL) /* didn't find "item2" after "item" */
17085 EMSG(_(e_invrange));
17086 else
17087 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017088 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017089 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017090 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017091 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017092 l->lv_first = item;
17093 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017094 item->li_prev = NULL;
17095 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017096 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017097 }
17098 }
17099 }
17100 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017101 }
17102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103}
17104
17105/*
17106 * "rename({from}, {to})" function
17107 */
17108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017109f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110{
17111 char_u buf[NUMBUFLEN];
17112
17113 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17117 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118}
17119
17120/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017121 * "repeat()" function
17122 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017124f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017125{
17126 char_u *p;
17127 int n;
17128 int slen;
17129 int len;
17130 char_u *r;
17131 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017132
17133 n = get_tv_number(&argvars[1]);
17134 if (argvars[0].v_type == VAR_LIST)
17135 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017136 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017137 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017138 if (list_extend(rettv->vval.v_list,
17139 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017140 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017141 }
17142 else
17143 {
17144 p = get_tv_string(&argvars[0]);
17145 rettv->v_type = VAR_STRING;
17146 rettv->vval.v_string = NULL;
17147
17148 slen = (int)STRLEN(p);
17149 len = slen * n;
17150 if (len <= 0)
17151 return;
17152
17153 r = alloc(len + 1);
17154 if (r != NULL)
17155 {
17156 for (i = 0; i < n; i++)
17157 mch_memmove(r + i * slen, p, (size_t)slen);
17158 r[len] = NUL;
17159 }
17160
17161 rettv->vval.v_string = r;
17162 }
17163}
17164
17165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 * "resolve()" function
17167 */
17168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017169f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170{
17171 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017172#ifdef HAVE_READLINK
17173 char_u *buf = NULL;
17174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177#ifdef FEAT_SHORTCUT
17178 {
17179 char_u *v = NULL;
17180
17181 v = mch_resolve_shortcut(p);
17182 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017183 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 }
17187#else
17188# ifdef HAVE_READLINK
17189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 char_u *cpy;
17191 int len;
17192 char_u *remain = NULL;
17193 char_u *q;
17194 int is_relative_to_current = FALSE;
17195 int has_trailing_pathsep = FALSE;
17196 int limit = 100;
17197
17198 p = vim_strsave(p);
17199
17200 if (p[0] == '.' && (vim_ispathsep(p[1])
17201 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17202 is_relative_to_current = TRUE;
17203
17204 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017205 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017208 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210
17211 q = getnextcomp(p);
17212 if (*q != NUL)
17213 {
17214 /* Separate the first path component in "p", and keep the
17215 * remainder (beginning with the path separator). */
17216 remain = vim_strsave(q - 1);
17217 q[-1] = NUL;
17218 }
17219
Bram Moolenaard9462e32011-04-11 21:35:11 +020017220 buf = alloc(MAXPATHL + 1);
17221 if (buf == NULL)
17222 goto fail;
17223
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 for (;;)
17225 {
17226 for (;;)
17227 {
17228 len = readlink((char *)p, (char *)buf, MAXPATHL);
17229 if (len <= 0)
17230 break;
17231 buf[len] = NUL;
17232
17233 if (limit-- == 0)
17234 {
17235 vim_free(p);
17236 vim_free(remain);
17237 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017238 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 goto fail;
17240 }
17241
17242 /* Ensure that the result will have a trailing path separator
17243 * if the argument has one. */
17244 if (remain == NULL && has_trailing_pathsep)
17245 add_pathsep(buf);
17246
17247 /* Separate the first path component in the link value and
17248 * concatenate the remainders. */
17249 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17250 if (*q != NUL)
17251 {
17252 if (remain == NULL)
17253 remain = vim_strsave(q - 1);
17254 else
17255 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017256 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 if (cpy != NULL)
17258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 vim_free(remain);
17260 remain = cpy;
17261 }
17262 }
17263 q[-1] = NUL;
17264 }
17265
17266 q = gettail(p);
17267 if (q > p && *q == NUL)
17268 {
17269 /* Ignore trailing path separator. */
17270 q[-1] = NUL;
17271 q = gettail(p);
17272 }
17273 if (q > p && !mch_isFullName(buf))
17274 {
17275 /* symlink is relative to directory of argument */
17276 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17277 if (cpy != NULL)
17278 {
17279 STRCPY(cpy, p);
17280 STRCPY(gettail(cpy), buf);
17281 vim_free(p);
17282 p = cpy;
17283 }
17284 }
17285 else
17286 {
17287 vim_free(p);
17288 p = vim_strsave(buf);
17289 }
17290 }
17291
17292 if (remain == NULL)
17293 break;
17294
17295 /* Append the first path component of "remain" to "p". */
17296 q = getnextcomp(remain + 1);
17297 len = q - remain - (*q != NUL);
17298 cpy = vim_strnsave(p, STRLEN(p) + len);
17299 if (cpy != NULL)
17300 {
17301 STRNCAT(cpy, remain, len);
17302 vim_free(p);
17303 p = cpy;
17304 }
17305 /* Shorten "remain". */
17306 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017307 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 else
17309 {
17310 vim_free(remain);
17311 remain = NULL;
17312 }
17313 }
17314
17315 /* If the result is a relative path name, make it explicitly relative to
17316 * the current directory if and only if the argument had this form. */
17317 if (!vim_ispathsep(*p))
17318 {
17319 if (is_relative_to_current
17320 && *p != NUL
17321 && !(p[0] == '.'
17322 && (p[1] == NUL
17323 || vim_ispathsep(p[1])
17324 || (p[1] == '.'
17325 && (p[2] == NUL
17326 || vim_ispathsep(p[2]))))))
17327 {
17328 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017329 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 if (cpy != NULL)
17331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 vim_free(p);
17333 p = cpy;
17334 }
17335 }
17336 else if (!is_relative_to_current)
17337 {
17338 /* Strip leading "./". */
17339 q = p;
17340 while (q[0] == '.' && vim_ispathsep(q[1]))
17341 q += 2;
17342 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017343 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 }
17345 }
17346
17347 /* Ensure that the result will have no trailing path separator
17348 * if the argument had none. But keep "/" or "//". */
17349 if (!has_trailing_pathsep)
17350 {
17351 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017352 if (after_pathsep(p, q))
17353 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 }
17355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017356 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 }
17358# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360# endif
17361#endif
17362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364
17365#ifdef HAVE_READLINK
17366fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017367 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370}
17371
17372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017373 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 */
17375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017376f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377{
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 list_T *l;
17379 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380
Bram Moolenaar0d660222005-01-07 21:51:51 +000017381 if (argvars[0].v_type != VAR_LIST)
17382 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017383 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017384 && !tv_check_lock(l->lv_lock,
17385 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017386 {
17387 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017388 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017389 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017390 while (li != NULL)
17391 {
17392 ni = li->li_prev;
17393 list_append(l, li);
17394 li = ni;
17395 }
17396 rettv->vval.v_list = l;
17397 rettv->v_type = VAR_LIST;
17398 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017399 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401}
17402
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017403#define SP_NOMOVE 0x01 /* don't move cursor */
17404#define SP_REPEAT 0x02 /* repeat to find outer pair */
17405#define SP_RETCOUNT 0x04 /* return matchcount */
17406#define SP_SETPCMARK 0x08 /* set previous context mark */
17407#define SP_START 0x10 /* accept match at start position */
17408#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17409#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017410#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017411
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017412static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017413
17414/*
17415 * Get flags for a search function.
17416 * Possibly sets "p_ws".
17417 * Returns BACKWARD, FORWARD or zero (for an error).
17418 */
17419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017420get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017421{
17422 int dir = FORWARD;
17423 char_u *flags;
17424 char_u nbuf[NUMBUFLEN];
17425 int mask;
17426
17427 if (varp->v_type != VAR_UNKNOWN)
17428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017429 flags = get_tv_string_buf_chk(varp, nbuf);
17430 if (flags == NULL)
17431 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017432 while (*flags != NUL)
17433 {
17434 switch (*flags)
17435 {
17436 case 'b': dir = BACKWARD; break;
17437 case 'w': p_ws = TRUE; break;
17438 case 'W': p_ws = FALSE; break;
17439 default: mask = 0;
17440 if (flagsp != NULL)
17441 switch (*flags)
17442 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017443 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017444 case 'e': mask = SP_END; break;
17445 case 'm': mask = SP_RETCOUNT; break;
17446 case 'n': mask = SP_NOMOVE; break;
17447 case 'p': mask = SP_SUBPAT; break;
17448 case 'r': mask = SP_REPEAT; break;
17449 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017450 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017451 }
17452 if (mask == 0)
17453 {
17454 EMSG2(_(e_invarg2), flags);
17455 dir = 0;
17456 }
17457 else
17458 *flagsp |= mask;
17459 }
17460 if (dir == 0)
17461 break;
17462 ++flags;
17463 }
17464 }
17465 return dir;
17466}
17467
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017469 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017472search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017474 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 char_u *pat;
17476 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017477 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 int save_p_ws = p_ws;
17479 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017480 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017481 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017482 proftime_T tm;
17483#ifdef FEAT_RELTIME
17484 long time_limit = 0;
17485#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017486 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017487 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017489 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017490 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017491 if (dir == 0)
17492 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017493 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017494 if (flags & SP_START)
17495 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017496 if (flags & SP_END)
17497 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017498 if (flags & SP_COLUMN)
17499 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017500
Bram Moolenaar76929292008-01-06 19:07:36 +000017501 /* Optional arguments: line number to stop searching and timeout. */
17502 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017503 {
17504 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17505 if (lnum_stop < 0)
17506 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017507#ifdef FEAT_RELTIME
17508 if (argvars[3].v_type != VAR_UNKNOWN)
17509 {
17510 time_limit = get_tv_number_chk(&argvars[3], NULL);
17511 if (time_limit < 0)
17512 goto theend;
17513 }
17514#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017515 }
17516
Bram Moolenaar76929292008-01-06 19:07:36 +000017517#ifdef FEAT_RELTIME
17518 /* Set the time limit, if there is one. */
17519 profile_setlimit(time_limit, &tm);
17520#endif
17521
Bram Moolenaar231334e2005-07-25 20:46:57 +000017522 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017523 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017524 * Check to make sure only those flags are set.
17525 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17526 * flags cannot be set. Check for that condition also.
17527 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017528 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017529 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017531 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017532 goto theend;
17533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017535 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017536 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017537 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017538 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017540 if (flags & SP_SUBPAT)
17541 retval = subpatnum;
17542 else
17543 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017544 if (flags & SP_SETPCMARK)
17545 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017547 if (match_pos != NULL)
17548 {
17549 /* Store the match cursor position */
17550 match_pos->lnum = pos.lnum;
17551 match_pos->col = pos.col + 1;
17552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 /* "/$" will put the cursor after the end of the line, may need to
17554 * correct that here */
17555 check_cursor();
17556 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017557
17558 /* If 'n' flag is used: restore cursor position. */
17559 if (flags & SP_NOMOVE)
17560 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017561 else
17562 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017563theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017565
17566 return retval;
17567}
17568
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017569#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017570
17571/*
17572 * round() is not in C90, use ceil() or floor() instead.
17573 */
17574 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017575vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017576{
17577 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17578}
17579
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017580/*
17581 * "round({float})" function
17582 */
17583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017584f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017585{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017586 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017587
17588 rettv->v_type = VAR_FLOAT;
17589 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017590 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017591 else
17592 rettv->vval.v_float = 0.0;
17593}
17594#endif
17595
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017596/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017597 * "screenattr()" function
17598 */
17599 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017600f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017601{
17602 int row;
17603 int col;
17604 int c;
17605
17606 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17607 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17608 if (row < 0 || row >= screen_Rows
17609 || col < 0 || col >= screen_Columns)
17610 c = -1;
17611 else
17612 c = ScreenAttrs[LineOffset[row] + col];
17613 rettv->vval.v_number = c;
17614}
17615
17616/*
17617 * "screenchar()" function
17618 */
17619 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017620f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017621{
17622 int row;
17623 int col;
17624 int off;
17625 int c;
17626
17627 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17628 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17629 if (row < 0 || row >= screen_Rows
17630 || col < 0 || col >= screen_Columns)
17631 c = -1;
17632 else
17633 {
17634 off = LineOffset[row] + col;
17635#ifdef FEAT_MBYTE
17636 if (enc_utf8 && ScreenLinesUC[off] != 0)
17637 c = ScreenLinesUC[off];
17638 else
17639#endif
17640 c = ScreenLines[off];
17641 }
17642 rettv->vval.v_number = c;
17643}
17644
17645/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017646 * "screencol()" function
17647 *
17648 * First column is 1 to be consistent with virtcol().
17649 */
17650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017651f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017652{
17653 rettv->vval.v_number = screen_screencol() + 1;
17654}
17655
17656/*
17657 * "screenrow()" function
17658 */
17659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017660f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017661{
17662 rettv->vval.v_number = screen_screenrow() + 1;
17663}
17664
17665/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017666 * "search()" function
17667 */
17668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017669f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017670{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017671 int flags = 0;
17672
17673 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674}
17675
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017677 * "searchdecl()" function
17678 */
17679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017680f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017681{
17682 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017683 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017684 int error = FALSE;
17685 char_u *name;
17686
17687 rettv->vval.v_number = 1; /* default: FAIL */
17688
17689 name = get_tv_string_chk(&argvars[0]);
17690 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017691 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017692 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017693 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17694 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17695 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017696 if (!error && name != NULL)
17697 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017698 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017699}
17700
17701/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017702 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017705searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706{
17707 char_u *spat, *mpat, *epat;
17708 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 int dir;
17711 int flags = 0;
17712 char_u nbuf1[NUMBUFLEN];
17713 char_u nbuf2[NUMBUFLEN];
17714 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017715 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017716 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017717 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017720 spat = get_tv_string_chk(&argvars[0]);
17721 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17722 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17723 if (spat == NULL || mpat == NULL || epat == NULL)
17724 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 /* Handle the optional fourth argument: flags */
17727 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017728 if (dir == 0)
17729 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017730
17731 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017732 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17733 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017734 if ((flags & (SP_END | SP_SUBPAT)) != 0
17735 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017736 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017737 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017738 goto theend;
17739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017741 /* Using 'r' implies 'W', otherwise it doesn't work. */
17742 if (flags & SP_REPEAT)
17743 p_ws = FALSE;
17744
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017745 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017746 if (argvars[3].v_type == VAR_UNKNOWN
17747 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 skip = (char_u *)"";
17749 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017751 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017752 if (argvars[5].v_type != VAR_UNKNOWN)
17753 {
17754 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17755 if (lnum_stop < 0)
17756 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017757#ifdef FEAT_RELTIME
17758 if (argvars[6].v_type != VAR_UNKNOWN)
17759 {
17760 time_limit = get_tv_number_chk(&argvars[6], NULL);
17761 if (time_limit < 0)
17762 goto theend;
17763 }
17764#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017765 }
17766 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017767 if (skip == NULL)
17768 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017770 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017771 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017772
17773theend:
17774 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017775
17776 return retval;
17777}
17778
17779/*
17780 * "searchpair()" function
17781 */
17782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017783f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017784{
17785 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17786}
17787
17788/*
17789 * "searchpairpos()" function
17790 */
17791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017792f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017793{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017794 pos_T match_pos;
17795 int lnum = 0;
17796 int col = 0;
17797
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017798 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017799 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017800
17801 if (searchpair_cmn(argvars, &match_pos) > 0)
17802 {
17803 lnum = match_pos.lnum;
17804 col = match_pos.col;
17805 }
17806
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017807 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17808 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017809}
17810
17811/*
17812 * Search for a start/middle/end thing.
17813 * Used by searchpair(), see its documentation for the details.
17814 * Returns 0 or -1 for no match,
17815 */
17816 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017817do_searchpair(
17818 char_u *spat, /* start pattern */
17819 char_u *mpat, /* middle pattern */
17820 char_u *epat, /* end pattern */
17821 int dir, /* BACKWARD or FORWARD */
17822 char_u *skip, /* skip expression */
17823 int flags, /* SP_SETPCMARK and other SP_ values */
17824 pos_T *match_pos,
17825 linenr_T lnum_stop, /* stop at this line if not zero */
17826 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017827{
17828 char_u *save_cpo;
17829 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17830 long retval = 0;
17831 pos_T pos;
17832 pos_T firstpos;
17833 pos_T foundpos;
17834 pos_T save_cursor;
17835 pos_T save_pos;
17836 int n;
17837 int r;
17838 int nest = 1;
17839 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017840 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017841 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017842
17843 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17844 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017845 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017846
Bram Moolenaar76929292008-01-06 19:07:36 +000017847#ifdef FEAT_RELTIME
17848 /* Set the time limit, if there is one. */
17849 profile_setlimit(time_limit, &tm);
17850#endif
17851
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017852 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17853 * start/middle/end (pat3, for the top pair). */
17854 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17855 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17856 if (pat2 == NULL || pat3 == NULL)
17857 goto theend;
17858 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17859 if (*mpat == NUL)
17860 STRCPY(pat3, pat2);
17861 else
17862 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17863 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017864 if (flags & SP_START)
17865 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017866
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 save_cursor = curwin->w_cursor;
17868 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017869 clearpos(&firstpos);
17870 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 pat = pat3;
17872 for (;;)
17873 {
17874 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017875 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17877 /* didn't find it or found the first match again: FAIL */
17878 break;
17879
17880 if (firstpos.lnum == 0)
17881 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017882 if (equalpos(pos, foundpos))
17883 {
17884 /* Found the same position again. Can happen with a pattern that
17885 * has "\zs" at the end and searching backwards. Advance one
17886 * character and try again. */
17887 if (dir == BACKWARD)
17888 decl(&pos);
17889 else
17890 incl(&pos);
17891 }
17892 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017894 /* clear the start flag to avoid getting stuck here */
17895 options &= ~SEARCH_START;
17896
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 /* If the skip pattern matches, ignore this match. */
17898 if (*skip != NUL)
17899 {
17900 save_pos = curwin->w_cursor;
17901 curwin->w_cursor = pos;
17902 r = eval_to_bool(skip, &err, NULL, FALSE);
17903 curwin->w_cursor = save_pos;
17904 if (err)
17905 {
17906 /* Evaluating {skip} caused an error, break here. */
17907 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017908 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 break;
17910 }
17911 if (r)
17912 continue;
17913 }
17914
17915 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17916 {
17917 /* Found end when searching backwards or start when searching
17918 * forward: nested pair. */
17919 ++nest;
17920 pat = pat2; /* nested, don't search for middle */
17921 }
17922 else
17923 {
17924 /* Found end when searching forward or start when searching
17925 * backward: end of (nested) pair; or found middle in outer pair. */
17926 if (--nest == 1)
17927 pat = pat3; /* outer level, search for middle */
17928 }
17929
17930 if (nest == 0)
17931 {
17932 /* Found the match: return matchcount or line number. */
17933 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017934 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017936 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017937 if (flags & SP_SETPCMARK)
17938 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 curwin->w_cursor = pos;
17940 if (!(flags & SP_REPEAT))
17941 break;
17942 nest = 1; /* search for next unmatched */
17943 }
17944 }
17945
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017946 if (match_pos != NULL)
17947 {
17948 /* Store the match cursor position */
17949 match_pos->lnum = curwin->w_cursor.lnum;
17950 match_pos->col = curwin->w_cursor.col + 1;
17951 }
17952
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017954 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 curwin->w_cursor = save_cursor;
17956
17957theend:
17958 vim_free(pat2);
17959 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017960 if (p_cpo == empty_option)
17961 p_cpo = save_cpo;
17962 else
17963 /* Darn, evaluating the {skip} expression changed the value. */
17964 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017965
17966 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967}
17968
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017969/*
17970 * "searchpos()" function
17971 */
17972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017973f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017974{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017975 pos_T match_pos;
17976 int lnum = 0;
17977 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017978 int n;
17979 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017980
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017981 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017982 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017984 n = search_cmn(argvars, &match_pos, &flags);
17985 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017986 {
17987 lnum = match_pos.lnum;
17988 col = match_pos.col;
17989 }
17990
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017991 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17992 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017993 if (flags & SP_SUBPAT)
17994 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017995}
17996
Bram Moolenaar0d660222005-01-07 21:51:51 +000017997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017998f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018000#ifdef FEAT_CLIENTSERVER
18001 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018002 char_u *server = get_tv_string_chk(&argvars[0]);
18003 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004
Bram Moolenaar0d660222005-01-07 21:51:51 +000018005 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018006 if (server == NULL || reply == NULL)
18007 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018008 if (check_restricted() || check_secure())
18009 return;
18010# ifdef FEAT_X11
18011 if (check_connection() == FAIL)
18012 return;
18013# endif
18014
18015 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017 EMSG(_("E258: Unable to send to client"));
18018 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018020 rettv->vval.v_number = 0;
18021#else
18022 rettv->vval.v_number = -1;
18023#endif
18024}
18025
Bram Moolenaar0d660222005-01-07 21:51:51 +000018026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018027f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018028{
18029 char_u *r = NULL;
18030
18031#ifdef FEAT_CLIENTSERVER
18032# ifdef WIN32
18033 r = serverGetVimNames();
18034# else
18035 make_connection();
18036 if (X_DISPLAY != NULL)
18037 r = serverGetVimNames(X_DISPLAY);
18038# endif
18039#endif
18040 rettv->v_type = VAR_STRING;
18041 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042}
18043
18044/*
18045 * "setbufvar()" function
18046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018048f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049{
18050 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018053 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 char_u nbuf[NUMBUFLEN];
18055
18056 if (check_restricted() || check_secure())
18057 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018058 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18059 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018060 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 varp = &argvars[2];
18062
18063 if (buf != NULL && varname != NULL && varp != NULL)
18064 {
18065 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067
18068 if (*varname == '&')
18069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018070 long numval;
18071 char_u *strval;
18072 int error = FALSE;
18073
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018075 numval = get_tv_number_chk(varp, &error);
18076 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018077 if (!error && strval != NULL)
18078 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 }
18080 else
18081 {
18082 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18083 if (bufvarname != NULL)
18084 {
18085 STRCPY(bufvarname, "b:");
18086 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018087 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 vim_free(bufvarname);
18089 }
18090 }
18091
18092 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095}
18096
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018098f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018099{
18100 dict_T *d;
18101 dictitem_T *di;
18102 char_u *csearch;
18103
18104 if (argvars[0].v_type != VAR_DICT)
18105 {
18106 EMSG(_(e_dictreq));
18107 return;
18108 }
18109
18110 if ((d = argvars[0].vval.v_dict) != NULL)
18111 {
18112 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18113 if (csearch != NULL)
18114 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018115#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018116 if (enc_utf8)
18117 {
18118 int pcc[MAX_MCO];
18119 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018120
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018121 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18122 }
18123 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018124#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018125 set_last_csearch(PTR2CHAR(csearch),
18126 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018127 }
18128
18129 di = dict_find(d, (char_u *)"forward", -1);
18130 if (di != NULL)
18131 set_csearch_direction(get_tv_number(&di->di_tv)
18132 ? FORWARD : BACKWARD);
18133
18134 di = dict_find(d, (char_u *)"until", -1);
18135 if (di != NULL)
18136 set_csearch_until(!!get_tv_number(&di->di_tv));
18137 }
18138}
18139
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140/*
18141 * "setcmdpos()" function
18142 */
18143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018144f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018146 int pos = (int)get_tv_number(&argvars[0]) - 1;
18147
18148 if (pos >= 0)
18149 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150}
18151
18152/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018153 * "setfperm({fname}, {mode})" function
18154 */
18155 static void
18156f_setfperm(typval_T *argvars, typval_T *rettv)
18157{
18158 char_u *fname;
18159 char_u modebuf[NUMBUFLEN];
18160 char_u *mode_str;
18161 int i;
18162 int mask;
18163 int mode = 0;
18164
18165 rettv->vval.v_number = 0;
18166 fname = get_tv_string_chk(&argvars[0]);
18167 if (fname == NULL)
18168 return;
18169 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18170 if (mode_str == NULL)
18171 return;
18172 if (STRLEN(mode_str) != 9)
18173 {
18174 EMSG2(_(e_invarg2), mode_str);
18175 return;
18176 }
18177
18178 mask = 1;
18179 for (i = 8; i >= 0; --i)
18180 {
18181 if (mode_str[i] != '-')
18182 mode |= mask;
18183 mask = mask << 1;
18184 }
18185 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18186}
18187
18188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 * "setline()" function
18190 */
18191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018192f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193{
18194 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018195 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018196 list_T *l = NULL;
18197 listitem_T *li = NULL;
18198 long added = 0;
18199 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018201 lnum = get_tv_lnum(&argvars[0]);
18202 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018204 l = argvars[1].vval.v_list;
18205 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018207 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018208 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018209
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018210 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018211 for (;;)
18212 {
18213 if (l != NULL)
18214 {
18215 /* list argument, get next string */
18216 if (li == NULL)
18217 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018218 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018219 li = li->li_next;
18220 }
18221
18222 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018223 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018224 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018225
18226 /* When coming here from Insert mode, sync undo, so that this can be
18227 * undone separately from what was previously inserted. */
18228 if (u_sync_once == 2)
18229 {
18230 u_sync_once = 1; /* notify that u_sync() was called */
18231 u_sync(TRUE);
18232 }
18233
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018234 if (lnum <= curbuf->b_ml.ml_line_count)
18235 {
18236 /* existing line, replace it */
18237 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18238 {
18239 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018240 if (lnum == curwin->w_cursor.lnum)
18241 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018242 rettv->vval.v_number = 0; /* OK */
18243 }
18244 }
18245 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18246 {
18247 /* lnum is one past the last line, append the line */
18248 ++added;
18249 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18250 rettv->vval.v_number = 0; /* OK */
18251 }
18252
18253 if (l == NULL) /* only one string argument */
18254 break;
18255 ++lnum;
18256 }
18257
18258 if (added > 0)
18259 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260}
18261
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018262static 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 +000018263
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018265 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018266 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018268set_qf_ll_list(
18269 win_T *wp UNUSED,
18270 typval_T *list_arg UNUSED,
18271 typval_T *action_arg UNUSED,
18272 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018273{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018274#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018275 char_u *act;
18276 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018277#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018278
Bram Moolenaar2641f772005-03-25 21:58:17 +000018279 rettv->vval.v_number = -1;
18280
18281#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018282 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018283 EMSG(_(e_listreq));
18284 else
18285 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018286 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018287
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018288 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018289 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018290 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018291 if (act == NULL)
18292 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018293 if (*act == 'a' || *act == 'r')
18294 action = *act;
18295 }
18296
Bram Moolenaar81484f42012-12-05 15:16:47 +010018297 if (l != NULL && set_errorlist(wp, l, action,
18298 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018299 rettv->vval.v_number = 0;
18300 }
18301#endif
18302}
18303
18304/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018305 * "setloclist()" function
18306 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018308f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018309{
18310 win_T *win;
18311
18312 rettv->vval.v_number = -1;
18313
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018314 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018315 if (win != NULL)
18316 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18317}
18318
18319/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018320 * "setmatches()" function
18321 */
18322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018323f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018324{
18325#ifdef FEAT_SEARCH_EXTRA
18326 list_T *l;
18327 listitem_T *li;
18328 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018329 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018330
18331 rettv->vval.v_number = -1;
18332 if (argvars[0].v_type != VAR_LIST)
18333 {
18334 EMSG(_(e_listreq));
18335 return;
18336 }
18337 if ((l = argvars[0].vval.v_list) != NULL)
18338 {
18339
18340 /* To some extent make sure that we are dealing with a list from
18341 * "getmatches()". */
18342 li = l->lv_first;
18343 while (li != NULL)
18344 {
18345 if (li->li_tv.v_type != VAR_DICT
18346 || (d = li->li_tv.vval.v_dict) == NULL)
18347 {
18348 EMSG(_(e_invarg));
18349 return;
18350 }
18351 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018352 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18353 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018354 && dict_find(d, (char_u *)"priority", -1) != NULL
18355 && dict_find(d, (char_u *)"id", -1) != NULL))
18356 {
18357 EMSG(_(e_invarg));
18358 return;
18359 }
18360 li = li->li_next;
18361 }
18362
18363 clear_matches(curwin);
18364 li = l->lv_first;
18365 while (li != NULL)
18366 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018367 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018368 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018369 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018370 char_u *group;
18371 int priority;
18372 int id;
18373 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018374
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018375 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018376 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18377 {
18378 if (s == NULL)
18379 {
18380 s = list_alloc();
18381 if (s == NULL)
18382 return;
18383 }
18384
18385 /* match from matchaddpos() */
18386 for (i = 1; i < 9; i++)
18387 {
18388 sprintf((char *)buf, (char *)"pos%d", i);
18389 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18390 {
18391 if (di->di_tv.v_type != VAR_LIST)
18392 return;
18393
18394 list_append_tv(s, &di->di_tv);
18395 s->lv_refcount++;
18396 }
18397 else
18398 break;
18399 }
18400 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018401
18402 group = get_dict_string(d, (char_u *)"group", FALSE);
18403 priority = (int)get_dict_number(d, (char_u *)"priority");
18404 id = (int)get_dict_number(d, (char_u *)"id");
18405 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18406 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18407 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018408 if (i == 0)
18409 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018410 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018411 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018412 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018413 }
18414 else
18415 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018416 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018417 list_unref(s);
18418 s = NULL;
18419 }
18420
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018421 li = li->li_next;
18422 }
18423 rettv->vval.v_number = 0;
18424 }
18425#endif
18426}
18427
18428/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018429 * "setpos()" function
18430 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018432f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018433{
18434 pos_T pos;
18435 int fnum;
18436 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018437 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018438
Bram Moolenaar08250432008-02-13 11:42:46 +000018439 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018440 name = get_tv_string_chk(argvars);
18441 if (name != NULL)
18442 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018443 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018444 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018445 if (--pos.col < 0)
18446 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018447 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018448 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018449 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018450 if (fnum == curbuf->b_fnum)
18451 {
18452 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018453 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018454 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018455 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018456 curwin->w_set_curswant = FALSE;
18457 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018458 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018459 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018460 }
18461 else
18462 EMSG(_(e_invarg));
18463 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018464 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18465 {
18466 /* set mark */
18467 if (setmark_pos(name[1], &pos, fnum) == OK)
18468 rettv->vval.v_number = 0;
18469 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018470 else
18471 EMSG(_(e_invarg));
18472 }
18473 }
18474}
18475
18476/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018477 * "setqflist()" function
18478 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018480f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018481{
18482 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18483}
18484
18485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 * "setreg()" function
18487 */
18488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018489f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490{
18491 int regname;
18492 char_u *strregname;
18493 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018494 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 int append;
18496 char_u yank_type;
18497 long block_len;
18498
18499 block_len = -1;
18500 yank_type = MAUTO;
18501 append = FALSE;
18502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018503 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018504 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018506 if (strregname == NULL)
18507 return; /* type error; errmsg already given */
18508 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 if (regname == 0 || regname == '@')
18510 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018512 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018514 stropt = get_tv_string_chk(&argvars[2]);
18515 if (stropt == NULL)
18516 return; /* type error */
18517 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 switch (*stropt)
18519 {
18520 case 'a': case 'A': /* append */
18521 append = TRUE;
18522 break;
18523 case 'v': case 'c': /* character-wise selection */
18524 yank_type = MCHAR;
18525 break;
18526 case 'V': case 'l': /* line-wise selection */
18527 yank_type = MLINE;
18528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 case 'b': case Ctrl_V: /* block-wise selection */
18530 yank_type = MBLOCK;
18531 if (VIM_ISDIGIT(stropt[1]))
18532 {
18533 ++stropt;
18534 block_len = getdigits(&stropt) - 1;
18535 --stropt;
18536 }
18537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 }
18539 }
18540
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018541 if (argvars[1].v_type == VAR_LIST)
18542 {
18543 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018544 char_u **allocval;
18545 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018546 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018547 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018548 int len = argvars[1].vval.v_list->lv_len;
18549 listitem_T *li;
18550
Bram Moolenaar7d647822014-04-05 21:28:56 +020018551 /* First half: use for pointers to result lines; second half: use for
18552 * pointers to allocated copies. */
18553 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018554 if (lstval == NULL)
18555 return;
18556 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018557 allocval = lstval + len + 2;
18558 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018559
18560 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18561 li = li->li_next)
18562 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018563 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018564 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018565 goto free_lstval;
18566 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018567 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018568 /* Need to make a copy, next get_tv_string_buf_chk() will
18569 * overwrite the string. */
18570 strval = vim_strsave(buf);
18571 if (strval == NULL)
18572 goto free_lstval;
18573 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018574 }
18575 *curval++ = strval;
18576 }
18577 *curval++ = NULL;
18578
18579 write_reg_contents_lst(regname, lstval, -1,
18580 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018581free_lstval:
18582 while (curallocval > allocval)
18583 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018584 vim_free(lstval);
18585 }
18586 else
18587 {
18588 strval = get_tv_string_chk(&argvars[1]);
18589 if (strval == NULL)
18590 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018591 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595}
18596
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018597/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018598 * "settabvar()" function
18599 */
18600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018601f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018602{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018603#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018604 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018605 tabpage_T *tp;
18606#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018607 char_u *varname, *tabvarname;
18608 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018609
18610 rettv->vval.v_number = 0;
18611
18612 if (check_restricted() || check_secure())
18613 return;
18614
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018615#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018616 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018617#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018618 varname = get_tv_string_chk(&argvars[1]);
18619 varp = &argvars[2];
18620
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018621 if (varname != NULL && varp != NULL
18622#ifdef FEAT_WINDOWS
18623 && tp != NULL
18624#endif
18625 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018626 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018627#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018628 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018629 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018630#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018631
18632 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18633 if (tabvarname != NULL)
18634 {
18635 STRCPY(tabvarname, "t:");
18636 STRCPY(tabvarname + 2, varname);
18637 set_var(tabvarname, varp, TRUE);
18638 vim_free(tabvarname);
18639 }
18640
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018641#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018642 /* Restore current tabpage */
18643 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018644 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018645#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018646 }
18647}
18648
18649/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018650 * "settabwinvar()" function
18651 */
18652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018653f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018654{
18655 setwinvar(argvars, rettv, 1);
18656}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657
18658/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018659 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018662f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018664 setwinvar(argvars, rettv, 0);
18665}
18666
18667/*
18668 * "setwinvar()" and "settabwinvar()" functions
18669 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018670
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018672setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018673{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 win_T *win;
18675#ifdef FEAT_WINDOWS
18676 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018677 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018678 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679#endif
18680 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018681 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018683 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684
18685 if (check_restricted() || check_secure())
18686 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018687
18688#ifdef FEAT_WINDOWS
18689 if (off == 1)
18690 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18691 else
18692 tp = curtab;
18693#endif
18694 win = find_win_by_nr(&argvars[off], tp);
18695 varname = get_tv_string_chk(&argvars[off + 1]);
18696 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697
18698 if (win != NULL && varname != NULL && varp != NULL)
18699 {
18700#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018701 need_switch_win = !(tp == curtab && win == curwin);
18702 if (!need_switch_win
18703 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018706 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018708 long numval;
18709 char_u *strval;
18710 int error = FALSE;
18711
18712 ++varname;
18713 numval = get_tv_number_chk(varp, &error);
18714 strval = get_tv_string_buf_chk(varp, nbuf);
18715 if (!error && strval != NULL)
18716 set_option_value(varname, numval, strval, OPT_LOCAL);
18717 }
18718 else
18719 {
18720 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18721 if (winvarname != NULL)
18722 {
18723 STRCPY(winvarname, "w:");
18724 STRCPY(winvarname + 2, varname);
18725 set_var(winvarname, varp, TRUE);
18726 vim_free(winvarname);
18727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 }
18729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018731 if (need_switch_win)
18732 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733#endif
18734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735}
18736
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018737#ifdef FEAT_CRYPT
18738/*
18739 * "sha256({string})" function
18740 */
18741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018742f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018743{
18744 char_u *p;
18745
18746 p = get_tv_string(&argvars[0]);
18747 rettv->vval.v_string = vim_strsave(
18748 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18749 rettv->v_type = VAR_STRING;
18750}
18751#endif /* FEAT_CRYPT */
18752
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018754 * "shellescape({string})" function
18755 */
18756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018757f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018758{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018759 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018760 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018761 rettv->v_type = VAR_STRING;
18762}
18763
18764/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018765 * shiftwidth() function
18766 */
18767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018768f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018769{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018770 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018771}
18772
18773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018774 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 */
18776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018777f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018779 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780
Bram Moolenaar0d660222005-01-07 21:51:51 +000018781 p = get_tv_string(&argvars[0]);
18782 rettv->vval.v_string = vim_strsave(p);
18783 simplify_filename(rettv->vval.v_string); /* simplify in place */
18784 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785}
18786
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018787#ifdef FEAT_FLOAT
18788/*
18789 * "sin()" function
18790 */
18791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018792f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018793{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018794 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018795
18796 rettv->v_type = VAR_FLOAT;
18797 if (get_float_arg(argvars, &f) == OK)
18798 rettv->vval.v_float = sin(f);
18799 else
18800 rettv->vval.v_float = 0.0;
18801}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018802
18803/*
18804 * "sinh()" function
18805 */
18806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018807f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018808{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018809 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018810
18811 rettv->v_type = VAR_FLOAT;
18812 if (get_float_arg(argvars, &f) == OK)
18813 rettv->vval.v_float = sinh(f);
18814 else
18815 rettv->vval.v_float = 0.0;
18816}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018817#endif
18818
Bram Moolenaar0d660222005-01-07 21:51:51 +000018819static int
18820#ifdef __BORLANDC__
18821 _RTLENTRYF
18822#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018823 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018824static int
18825#ifdef __BORLANDC__
18826 _RTLENTRYF
18827#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018828 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018829
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018830/* struct used in the array that's given to qsort() */
18831typedef struct
18832{
18833 listitem_T *item;
18834 int idx;
18835} sortItem_T;
18836
Bram Moolenaar0b962472016-02-22 22:51:33 +010018837/* struct storing information about current sort */
18838typedef struct
18839{
18840 int item_compare_ic;
18841 int item_compare_numeric;
18842 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018843#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018844 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018845#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018846 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018847 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018848 dict_T *item_compare_selfdict;
18849 int item_compare_func_err;
18850 int item_compare_keep_zero;
18851} sortinfo_T;
18852static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018853static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018854#define ITEM_COMPARE_FAIL 999
18855
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018857 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018859 static int
18860#ifdef __BORLANDC__
18861_RTLENTRYF
18862#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018863item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018865 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018866 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018867 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018868 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018869 int res;
18870 char_u numbuf1[NUMBUFLEN];
18871 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018873 si1 = (sortItem_T *)s1;
18874 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018875 tv1 = &si1->item->li_tv;
18876 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018877
Bram Moolenaar0b962472016-02-22 22:51:33 +010018878 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018879 {
18880 long v1 = get_tv_number(tv1);
18881 long v2 = get_tv_number(tv2);
18882
18883 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18884 }
18885
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018886#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018887 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018888 {
18889 float_T v1 = get_tv_float(tv1);
18890 float_T v2 = get_tv_float(tv2);
18891
18892 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18893 }
18894#endif
18895
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018896 /* tv2string() puts quotes around a string and allocates memory. Don't do
18897 * that for string variables. Use a single quote when comparing with a
18898 * non-string to do what the docs promise. */
18899 if (tv1->v_type == VAR_STRING)
18900 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018901 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018902 p1 = (char_u *)"'";
18903 else
18904 p1 = tv1->vval.v_string;
18905 }
18906 else
18907 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18908 if (tv2->v_type == VAR_STRING)
18909 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018910 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018911 p2 = (char_u *)"'";
18912 else
18913 p2 = tv2->vval.v_string;
18914 }
18915 else
18916 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018917 if (p1 == NULL)
18918 p1 = (char_u *)"";
18919 if (p2 == NULL)
18920 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018921 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018922 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018923 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018924 res = STRICMP(p1, p2);
18925 else
18926 res = STRCMP(p1, p2);
18927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018929 {
18930 double n1, n2;
18931 n1 = strtod((char *)p1, (char **)&p1);
18932 n2 = strtod((char *)p2, (char **)&p2);
18933 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18934 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018935
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018936 /* When the result would be zero, compare the item indexes. Makes the
18937 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018938 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018939 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018940
Bram Moolenaar0d660222005-01-07 21:51:51 +000018941 vim_free(tofree1);
18942 vim_free(tofree2);
18943 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944}
18945
18946 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018947#ifdef __BORLANDC__
18948_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018950item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018951{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018952 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018953 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018954 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018955 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018957 char_u *func_name;
18958 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018960 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018961 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018962 return 0;
18963
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018964 si1 = (sortItem_T *)s1;
18965 si2 = (sortItem_T *)s2;
18966
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018967 if (partial == NULL)
18968 func_name = sortinfo->item_compare_func;
18969 else
18970 func_name = partial->pt_name;
18971
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018972 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018973 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018974 copy_tv(&si1->item->li_tv, &argv[0]);
18975 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018976
18977 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018978 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018979 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018980 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018981 clear_tv(&argv[0]);
18982 clear_tv(&argv[1]);
18983
18984 if (res == FAIL)
18985 res = ITEM_COMPARE_FAIL;
18986 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018987 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18988 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018989 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018990 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018991
18992 /* When the result would be zero, compare the pointers themselves. Makes
18993 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018994 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018995 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018996
Bram Moolenaar0d660222005-01-07 21:51:51 +000018997 return res;
18998}
18999
19000/*
19001 * "sort({list})" function
19002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019004do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005{
Bram Moolenaar33570922005-01-25 22:26:29 +000019006 list_T *l;
19007 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019008 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019009 sortinfo_T *old_sortinfo;
19010 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019011 long len;
19012 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013
Bram Moolenaar0b962472016-02-22 22:51:33 +010019014 /* Pointer to current info struct used in compare function. Save and
19015 * restore the current one for nested calls. */
19016 old_sortinfo = sortinfo;
19017 sortinfo = &info;
19018
Bram Moolenaar0d660222005-01-07 21:51:51 +000019019 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019020 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021 else
19022 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019023 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019024 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019025 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19026 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019027 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019028 rettv->vval.v_list = l;
19029 rettv->v_type = VAR_LIST;
19030 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031
Bram Moolenaar0d660222005-01-07 21:51:51 +000019032 len = list_len(l);
19033 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019034 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035
Bram Moolenaar0b962472016-02-22 22:51:33 +010019036 info.item_compare_ic = FALSE;
19037 info.item_compare_numeric = FALSE;
19038 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019039#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019040 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019041#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019042 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019043 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019044 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045 if (argvars[1].v_type != VAR_UNKNOWN)
19046 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019047 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019048 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019049 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019050 else if (argvars[1].v_type == VAR_PARTIAL)
19051 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019052 else
19053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019054 int error = FALSE;
19055
19056 i = get_tv_number_chk(&argvars[1], &error);
19057 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019058 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019059 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019060 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019061 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019062 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019063 else if (i != 0)
19064 {
19065 EMSG(_(e_invarg));
19066 goto theend;
19067 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019068 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019069 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019070 if (*info.item_compare_func == NUL)
19071 {
19072 /* empty string means default sort */
19073 info.item_compare_func = NULL;
19074 }
19075 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019076 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019077 info.item_compare_func = NULL;
19078 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019079 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019080 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019081 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019082 info.item_compare_func = NULL;
19083 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019084 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019085#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019086 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019087 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019088 info.item_compare_func = NULL;
19089 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019090 }
19091#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019092 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019093 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 info.item_compare_func = NULL;
19095 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019096 }
19097 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019098 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019099
19100 if (argvars[2].v_type != VAR_UNKNOWN)
19101 {
19102 /* optional third argument: {dict} */
19103 if (argvars[2].v_type != VAR_DICT)
19104 {
19105 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019106 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019107 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019108 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019109 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111
Bram Moolenaar0d660222005-01-07 21:51:51 +000019112 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019113 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019114 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019115 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116
Bram Moolenaar327aa022014-03-25 18:24:23 +010019117 i = 0;
19118 if (sort)
19119 {
19120 /* sort(): ptrs will be the list to sort */
19121 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019122 {
19123 ptrs[i].item = li;
19124 ptrs[i].idx = i;
19125 ++i;
19126 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019127
Bram Moolenaar0b962472016-02-22 22:51:33 +010019128 info.item_compare_func_err = FALSE;
19129 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019130 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019131 if ((info.item_compare_func != NULL
19132 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019133 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019134 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019135 EMSG(_("E702: Sort compare function failed"));
19136 else
19137 {
19138 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019139 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019140 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019141 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019142 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019143
Bram Moolenaar0b962472016-02-22 22:51:33 +010019144 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019145 {
19146 /* Clear the List and append the items in sorted order. */
19147 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19148 l->lv_len = 0;
19149 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019150 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019151 }
19152 }
19153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019155 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019156 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019157
19158 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019159 info.item_compare_func_err = FALSE;
19160 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019161 item_compare_func_ptr = info.item_compare_func != NULL
19162 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019163 ? item_compare2 : item_compare;
19164
19165 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19166 li = li->li_next)
19167 {
19168 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19169 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019170 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019171 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019172 {
19173 EMSG(_("E882: Uniq compare function failed"));
19174 break;
19175 }
19176 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019177
Bram Moolenaar0b962472016-02-22 22:51:33 +010019178 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019179 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019180 while (--i >= 0)
19181 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019182 li = ptrs[i].item->li_next;
19183 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019184 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019185 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019186 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019187 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019188 list_fix_watch(l, li);
19189 listitem_free(li);
19190 l->lv_len--;
19191 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019193 }
19194
19195 vim_free(ptrs);
19196 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019197theend:
19198 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019199}
19200
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019201/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019202 * "sort({list})" function
19203 */
19204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019205f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019206{
19207 do_sort_uniq(argvars, rettv, TRUE);
19208}
19209
19210/*
19211 * "uniq({list})" function
19212 */
19213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019214f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019215{
19216 do_sort_uniq(argvars, rettv, FALSE);
19217}
19218
19219/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019220 * "soundfold({word})" function
19221 */
19222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019223f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019224{
19225 char_u *s;
19226
19227 rettv->v_type = VAR_STRING;
19228 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019229#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019230 rettv->vval.v_string = eval_soundfold(s);
19231#else
19232 rettv->vval.v_string = vim_strsave(s);
19233#endif
19234}
19235
19236/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019237 * "spellbadword()" function
19238 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019240f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019241{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019242 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019243 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019244 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019245
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019246 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019247 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019248
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019249#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019250 if (argvars[0].v_type == VAR_UNKNOWN)
19251 {
19252 /* Find the start and length of the badly spelled word. */
19253 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19254 if (len != 0)
19255 word = ml_get_cursor();
19256 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019257 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019258 {
19259 char_u *str = get_tv_string_chk(&argvars[0]);
19260 int capcol = -1;
19261
19262 if (str != NULL)
19263 {
19264 /* Check the argument for spelling. */
19265 while (*str != NUL)
19266 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019267 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019268 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019269 {
19270 word = str;
19271 break;
19272 }
19273 str += len;
19274 }
19275 }
19276 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019277#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019278
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019279 list_append_string(rettv->vval.v_list, word, len);
19280 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019281 attr == HLF_SPB ? "bad" :
19282 attr == HLF_SPR ? "rare" :
19283 attr == HLF_SPL ? "local" :
19284 attr == HLF_SPC ? "caps" :
19285 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019286}
19287
19288/*
19289 * "spellsuggest()" function
19290 */
19291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019292f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019293{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019294#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019295 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019296 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019297 int maxcount;
19298 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019299 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019300 listitem_T *li;
19301 int need_capital = FALSE;
19302#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019304 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019305 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019306
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019307#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019308 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019309 {
19310 str = get_tv_string(&argvars[0]);
19311 if (argvars[1].v_type != VAR_UNKNOWN)
19312 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019313 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019314 if (maxcount <= 0)
19315 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019316 if (argvars[2].v_type != VAR_UNKNOWN)
19317 {
19318 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19319 if (typeerr)
19320 return;
19321 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019322 }
19323 else
19324 maxcount = 25;
19325
Bram Moolenaar4770d092006-01-12 23:22:24 +000019326 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019327
19328 for (i = 0; i < ga.ga_len; ++i)
19329 {
19330 str = ((char_u **)ga.ga_data)[i];
19331
19332 li = listitem_alloc();
19333 if (li == NULL)
19334 vim_free(str);
19335 else
19336 {
19337 li->li_tv.v_type = VAR_STRING;
19338 li->li_tv.v_lock = 0;
19339 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019340 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019341 }
19342 }
19343 ga_clear(&ga);
19344 }
19345#endif
19346}
19347
Bram Moolenaar0d660222005-01-07 21:51:51 +000019348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019349f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019350{
19351 char_u *str;
19352 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019353 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019354 regmatch_T regmatch;
19355 char_u patbuf[NUMBUFLEN];
19356 char_u *save_cpo;
19357 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019358 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019359 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019360 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019361
19362 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19363 save_cpo = p_cpo;
19364 p_cpo = (char_u *)"";
19365
19366 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019367 if (argvars[1].v_type != VAR_UNKNOWN)
19368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019369 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19370 if (pat == NULL)
19371 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019372 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019373 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019374 }
19375 if (pat == NULL || *pat == NUL)
19376 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019377
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019378 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019380 if (typeerr)
19381 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382
Bram Moolenaar0d660222005-01-07 21:51:51 +000019383 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19384 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019386 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019387 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019388 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019389 if (*str == NUL)
19390 match = FALSE; /* empty item at the end */
19391 else
19392 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019393 if (match)
19394 end = regmatch.startp[0];
19395 else
19396 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019397 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19398 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019400 if (list_append_string(rettv->vval.v_list, str,
19401 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019402 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019403 }
19404 if (!match)
19405 break;
19406 /* Advance to just after the match. */
19407 if (regmatch.endp[0] > str)
19408 col = 0;
19409 else
19410 {
19411 /* Don't get stuck at the same match. */
19412#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019413 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414#else
19415 col = 1;
19416#endif
19417 }
19418 str = regmatch.endp[0];
19419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420
Bram Moolenaar473de612013-06-08 18:19:48 +020019421 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423
Bram Moolenaar0d660222005-01-07 21:51:51 +000019424 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425}
19426
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019427#ifdef FEAT_FLOAT
19428/*
19429 * "sqrt()" function
19430 */
19431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019432f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019433{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019434 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019435
19436 rettv->v_type = VAR_FLOAT;
19437 if (get_float_arg(argvars, &f) == OK)
19438 rettv->vval.v_float = sqrt(f);
19439 else
19440 rettv->vval.v_float = 0.0;
19441}
19442
19443/*
19444 * "str2float()" function
19445 */
19446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019447f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019448{
19449 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19450
19451 if (*p == '+')
19452 p = skipwhite(p + 1);
19453 (void)string2float(p, &rettv->vval.v_float);
19454 rettv->v_type = VAR_FLOAT;
19455}
19456#endif
19457
Bram Moolenaar2c932302006-03-18 21:42:09 +000019458/*
19459 * "str2nr()" function
19460 */
19461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019462f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019463{
19464 int base = 10;
19465 char_u *p;
19466 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019467 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019468
19469 if (argvars[1].v_type != VAR_UNKNOWN)
19470 {
19471 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019472 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019473 {
19474 EMSG(_(e_invarg));
19475 return;
19476 }
19477 }
19478
19479 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019480 if (*p == '+')
19481 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019482 switch (base)
19483 {
19484 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19485 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19486 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19487 default: what = 0;
19488 }
19489 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019490 rettv->vval.v_number = n;
19491}
19492
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493#ifdef HAVE_STRFTIME
19494/*
19495 * "strftime({format}[, {time}])" function
19496 */
19497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019498f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499{
19500 char_u result_buf[256];
19501 struct tm *curtime;
19502 time_t seconds;
19503 char_u *p;
19504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019505 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019508 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 seconds = time(NULL);
19510 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019511 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 curtime = localtime(&seconds);
19513 /* MSVC returns NULL for an invalid value of seconds. */
19514 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019515 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 else
19517 {
19518# ifdef FEAT_MBYTE
19519 vimconv_T conv;
19520 char_u *enc;
19521
19522 conv.vc_type = CONV_NONE;
19523 enc = enc_locale();
19524 convert_setup(&conv, p_enc, enc);
19525 if (conv.vc_type != CONV_NONE)
19526 p = string_convert(&conv, p, NULL);
19527# endif
19528 if (p != NULL)
19529 (void)strftime((char *)result_buf, sizeof(result_buf),
19530 (char *)p, curtime);
19531 else
19532 result_buf[0] = NUL;
19533
19534# ifdef FEAT_MBYTE
19535 if (conv.vc_type != CONV_NONE)
19536 vim_free(p);
19537 convert_setup(&conv, enc, p_enc);
19538 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 else
19541# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019542 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543
19544# ifdef FEAT_MBYTE
19545 /* Release conversion descriptors */
19546 convert_setup(&conv, NULL, NULL);
19547 vim_free(enc);
19548# endif
19549 }
19550}
19551#endif
19552
19553/*
19554 * "stridx()" function
19555 */
19556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019557f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558{
19559 char_u buf[NUMBUFLEN];
19560 char_u *needle;
19561 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019562 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019564 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019566 needle = get_tv_string_chk(&argvars[1]);
19567 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019568 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019569 if (needle == NULL || haystack == NULL)
19570 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571
Bram Moolenaar33570922005-01-25 22:26:29 +000019572 if (argvars[2].v_type != VAR_UNKNOWN)
19573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019574 int error = FALSE;
19575
19576 start_idx = get_tv_number_chk(&argvars[2], &error);
19577 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019578 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019579 if (start_idx >= 0)
19580 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019581 }
19582
19583 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19584 if (pos != NULL)
19585 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586}
19587
19588/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019589 * "string()" function
19590 */
19591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019592f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019593{
19594 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019595 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019598 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19599 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019600 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019601 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019602 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603}
19604
19605/*
19606 * "strlen()" function
19607 */
19608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019609f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019611 rettv->vval.v_number = (varnumber_T)(STRLEN(
19612 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613}
19614
19615/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019616 * "strchars()" function
19617 */
19618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019619f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019620{
19621 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019622 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019623#ifdef FEAT_MBYTE
19624 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019625 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019626#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019627
19628 if (argvars[1].v_type != VAR_UNKNOWN)
19629 skipcc = get_tv_number_chk(&argvars[1], NULL);
19630 if (skipcc < 0 || skipcc > 1)
19631 EMSG(_(e_invarg));
19632 else
19633 {
19634#ifdef FEAT_MBYTE
19635 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19636 while (*s != NUL)
19637 {
19638 func_mb_ptr2char_adv(&s);
19639 ++len;
19640 }
19641 rettv->vval.v_number = len;
19642#else
19643 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19644#endif
19645 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019646}
19647
19648/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019649 * "strdisplaywidth()" function
19650 */
19651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019652f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019653{
19654 char_u *s = get_tv_string(&argvars[0]);
19655 int col = 0;
19656
19657 if (argvars[1].v_type != VAR_UNKNOWN)
19658 col = get_tv_number(&argvars[1]);
19659
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019660 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019661}
19662
19663/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019664 * "strwidth()" function
19665 */
19666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019667f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019668{
19669 char_u *s = get_tv_string(&argvars[0]);
19670
19671 rettv->vval.v_number = (varnumber_T)(
19672#ifdef FEAT_MBYTE
19673 mb_string2cells(s, -1)
19674#else
19675 STRLEN(s)
19676#endif
19677 );
19678}
19679
19680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 * "strpart()" function
19682 */
19683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019684f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685{
19686 char_u *p;
19687 int n;
19688 int len;
19689 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019690 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 slen = (int)STRLEN(p);
19694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019695 n = get_tv_number_chk(&argvars[1], &error);
19696 if (error)
19697 len = 0;
19698 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019699 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 else
19701 len = slen - n; /* default len: all bytes that are available. */
19702
19703 /*
19704 * Only return the overlap between the specified part and the actual
19705 * string.
19706 */
19707 if (n < 0)
19708 {
19709 len += n;
19710 n = 0;
19711 }
19712 else if (n > slen)
19713 n = slen;
19714 if (len < 0)
19715 len = 0;
19716 else if (n + len > slen)
19717 len = slen - n;
19718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 rettv->v_type = VAR_STRING;
19720 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721}
19722
19723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019724 * "strridx()" function
19725 */
19726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019727f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019728{
19729 char_u buf[NUMBUFLEN];
19730 char_u *needle;
19731 char_u *haystack;
19732 char_u *rest;
19733 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019734 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019736 needle = get_tv_string_chk(&argvars[1]);
19737 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019738
19739 rettv->vval.v_number = -1;
19740 if (needle == NULL || haystack == NULL)
19741 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019742
19743 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019744 if (argvars[2].v_type != VAR_UNKNOWN)
19745 {
19746 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019747 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019748 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019749 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019750 }
19751 else
19752 end_idx = haystack_len;
19753
Bram Moolenaar0d660222005-01-07 21:51:51 +000019754 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019755 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019756 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019757 lastmatch = haystack + end_idx;
19758 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019759 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019760 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019761 for (rest = haystack; *rest != '\0'; ++rest)
19762 {
19763 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019764 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019765 break;
19766 lastmatch = rest;
19767 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019768 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019769
19770 if (lastmatch == NULL)
19771 rettv->vval.v_number = -1;
19772 else
19773 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19774}
19775
19776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 * "strtrans()" function
19778 */
19779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019780f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 rettv->v_type = VAR_STRING;
19783 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784}
19785
19786/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019787 * "submatch()" function
19788 */
19789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019790f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019791{
Bram Moolenaar41571762014-04-02 19:00:58 +020019792 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019793 int no;
19794 int retList = 0;
19795
19796 no = (int)get_tv_number_chk(&argvars[0], &error);
19797 if (error)
19798 return;
19799 error = FALSE;
19800 if (argvars[1].v_type != VAR_UNKNOWN)
19801 retList = get_tv_number_chk(&argvars[1], &error);
19802 if (error)
19803 return;
19804
19805 if (retList == 0)
19806 {
19807 rettv->v_type = VAR_STRING;
19808 rettv->vval.v_string = reg_submatch(no);
19809 }
19810 else
19811 {
19812 rettv->v_type = VAR_LIST;
19813 rettv->vval.v_list = reg_submatch_list(no);
19814 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019815}
19816
19817/*
19818 * "substitute()" function
19819 */
19820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019821f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019822{
19823 char_u patbuf[NUMBUFLEN];
19824 char_u subbuf[NUMBUFLEN];
19825 char_u flagsbuf[NUMBUFLEN];
19826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019827 char_u *str = get_tv_string_chk(&argvars[0]);
19828 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19829 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19830 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19831
Bram Moolenaar0d660222005-01-07 21:51:51 +000019832 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019833 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19834 rettv->vval.v_string = NULL;
19835 else
19836 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019837}
19838
19839/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019840 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019843f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844{
19845 int id = 0;
19846#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019847 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 long col;
19849 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019850 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019852 lnum = get_tv_lnum(argvars); /* -1 on type error */
19853 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19854 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019856 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019857 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019858 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859#endif
19860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019861 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862}
19863
19864/*
19865 * "synIDattr(id, what [, mode])" function
19866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019868f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869{
19870 char_u *p = NULL;
19871#ifdef FEAT_SYN_HL
19872 int id;
19873 char_u *what;
19874 char_u *mode;
19875 char_u modebuf[NUMBUFLEN];
19876 int modec;
19877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019878 id = get_tv_number(&argvars[0]);
19879 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019880 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019882 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019884 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 modec = 0; /* replace invalid with current */
19886 }
19887 else
19888 {
19889#ifdef FEAT_GUI
19890 if (gui.in_use)
19891 modec = 'g';
19892 else
19893#endif
19894 if (t_colors > 1)
19895 modec = 'c';
19896 else
19897 modec = 't';
19898 }
19899
19900
19901 switch (TOLOWER_ASC(what[0]))
19902 {
19903 case 'b':
19904 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19905 p = highlight_color(id, what, modec);
19906 else /* bold */
19907 p = highlight_has_attr(id, HL_BOLD, modec);
19908 break;
19909
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019910 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 p = highlight_color(id, what, modec);
19912 break;
19913
19914 case 'i':
19915 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19916 p = highlight_has_attr(id, HL_INVERSE, modec);
19917 else /* italic */
19918 p = highlight_has_attr(id, HL_ITALIC, modec);
19919 break;
19920
19921 case 'n': /* name */
19922 p = get_highlight_name(NULL, id - 1);
19923 break;
19924
19925 case 'r': /* reverse */
19926 p = highlight_has_attr(id, HL_INVERSE, modec);
19927 break;
19928
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019929 case 's':
19930 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19931 p = highlight_color(id, what, modec);
19932 else /* standout */
19933 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 break;
19935
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019936 case 'u':
19937 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19938 /* underline */
19939 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19940 else
19941 /* undercurl */
19942 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 break;
19944 }
19945
19946 if (p != NULL)
19947 p = vim_strsave(p);
19948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949 rettv->v_type = VAR_STRING;
19950 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951}
19952
19953/*
19954 * "synIDtrans(id)" function
19955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019957f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958{
19959 int id;
19960
19961#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019962 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963
19964 if (id > 0)
19965 id = syn_get_final_id(id);
19966 else
19967#endif
19968 id = 0;
19969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019970 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971}
19972
19973/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019974 * "synconcealed(lnum, col)" function
19975 */
19976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019977f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019978{
19979#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19980 long lnum;
19981 long col;
19982 int syntax_flags = 0;
19983 int cchar;
19984 int matchid = 0;
19985 char_u str[NUMBUFLEN];
19986#endif
19987
19988 rettv->v_type = VAR_LIST;
19989 rettv->vval.v_list = NULL;
19990
19991#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19992 lnum = get_tv_lnum(argvars); /* -1 on type error */
19993 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19994
19995 vim_memset(str, NUL, sizeof(str));
19996
19997 if (rettv_list_alloc(rettv) != FAIL)
19998 {
19999 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20000 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20001 && curwin->w_p_cole > 0)
20002 {
20003 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20004 syntax_flags = get_syntax_info(&matchid);
20005
20006 /* get the conceal character */
20007 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20008 {
20009 cchar = syn_get_sub_char();
20010 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20011 cchar = lcs_conceal;
20012 if (cchar != NUL)
20013 {
20014# ifdef FEAT_MBYTE
20015 if (has_mbyte)
20016 (*mb_char2bytes)(cchar, str);
20017 else
20018# endif
20019 str[0] = cchar;
20020 }
20021 }
20022 }
20023
20024 list_append_number(rettv->vval.v_list,
20025 (syntax_flags & HL_CONCEAL) != 0);
20026 /* -1 to auto-determine strlen */
20027 list_append_string(rettv->vval.v_list, str, -1);
20028 list_append_number(rettv->vval.v_list, matchid);
20029 }
20030#endif
20031}
20032
20033/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020034 * "synstack(lnum, col)" function
20035 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020037f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020038{
20039#ifdef FEAT_SYN_HL
20040 long lnum;
20041 long col;
20042 int i;
20043 int id;
20044#endif
20045
20046 rettv->v_type = VAR_LIST;
20047 rettv->vval.v_list = NULL;
20048
20049#ifdef FEAT_SYN_HL
20050 lnum = get_tv_lnum(argvars); /* -1 on type error */
20051 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20052
20053 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020054 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020055 && rettv_list_alloc(rettv) != FAIL)
20056 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020057 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020058 for (i = 0; ; ++i)
20059 {
20060 id = syn_get_stack_item(i);
20061 if (id < 0)
20062 break;
20063 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20064 break;
20065 }
20066 }
20067#endif
20068}
20069
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020071get_cmd_output_as_rettv(
20072 typval_T *argvars,
20073 typval_T *rettv,
20074 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020076 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020078 char_u *infile = NULL;
20079 char_u buf[NUMBUFLEN];
20080 int err = FALSE;
20081 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020082 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020083 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020085 rettv->v_type = VAR_STRING;
20086 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020087 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020088 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020089
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020090 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020091 {
20092 /*
20093 * Write the string to a temp file, to be used for input of the shell
20094 * command.
20095 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020096 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020097 {
20098 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020099 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020100 }
20101
20102 fd = mch_fopen((char *)infile, WRITEBIN);
20103 if (fd == NULL)
20104 {
20105 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020106 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020107 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020108 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020109 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020110 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20111 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020112 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020113 else
20114 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020115 size_t len;
20116
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020117 p = get_tv_string_buf_chk(&argvars[1], buf);
20118 if (p == NULL)
20119 {
20120 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020121 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020122 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020123 len = STRLEN(p);
20124 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020125 err = TRUE;
20126 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020127 if (fclose(fd) != 0)
20128 err = TRUE;
20129 if (err)
20130 {
20131 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020132 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020133 }
20134 }
20135
Bram Moolenaar52a72462014-08-29 15:53:52 +020020136 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20137 * echoes typeahead, that messes up the display. */
20138 if (!msg_silent)
20139 flags += SHELL_COOKED;
20140
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020141 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020143 int len;
20144 listitem_T *li;
20145 char_u *s = NULL;
20146 char_u *start;
20147 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020148 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149
Bram Moolenaar52a72462014-08-29 15:53:52 +020020150 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020151 if (res == NULL)
20152 goto errret;
20153
20154 list = list_alloc();
20155 if (list == NULL)
20156 goto errret;
20157
20158 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020160 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020161 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020162 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020163 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020164
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020165 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020166 if (s == NULL)
20167 goto errret;
20168
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020169 for (p = s; start < end; ++p, ++start)
20170 *p = *start == NUL ? NL : *start;
20171 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020172
20173 li = listitem_alloc();
20174 if (li == NULL)
20175 {
20176 vim_free(s);
20177 goto errret;
20178 }
20179 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020180 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020181 li->li_tv.vval.v_string = s;
20182 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020184
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020185 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020186 rettv->v_type = VAR_LIST;
20187 rettv->vval.v_list = list;
20188 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020190 else
20191 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020192 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020193#ifdef USE_CR
20194 /* translate <CR> into <NL> */
20195 if (res != NULL)
20196 {
20197 char_u *s;
20198
20199 for (s = res; *s; ++s)
20200 {
20201 if (*s == CAR)
20202 *s = NL;
20203 }
20204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205#else
20206# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020207 /* translate <CR><NL> into <NL> */
20208 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020210 char_u *s, *d;
20211
20212 d = res;
20213 for (s = res; *s; ++s)
20214 {
20215 if (s[0] == CAR && s[1] == NL)
20216 ++s;
20217 *d++ = *s;
20218 }
20219 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221# endif
20222#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020223 rettv->vval.v_string = res;
20224 res = NULL;
20225 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020226
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020227errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020228 if (infile != NULL)
20229 {
20230 mch_remove(infile);
20231 vim_free(infile);
20232 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020233 if (res != NULL)
20234 vim_free(res);
20235 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020236 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020237}
20238
20239/*
20240 * "system()" function
20241 */
20242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020243f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020244{
20245 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20246}
20247
20248/*
20249 * "systemlist()" function
20250 */
20251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020252f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020253{
20254 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255}
20256
20257/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020258 * "tabpagebuflist()" function
20259 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020261f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020262{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020263#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020264 tabpage_T *tp;
20265 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020266
20267 if (argvars[0].v_type == VAR_UNKNOWN)
20268 wp = firstwin;
20269 else
20270 {
20271 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20272 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020273 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020274 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020275 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020276 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020277 for (; wp != NULL; wp = wp->w_next)
20278 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020279 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020280 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020281 }
20282#endif
20283}
20284
20285
20286/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020287 * "tabpagenr()" function
20288 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020290f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020291{
20292 int nr = 1;
20293#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020294 char_u *arg;
20295
20296 if (argvars[0].v_type != VAR_UNKNOWN)
20297 {
20298 arg = get_tv_string_chk(&argvars[0]);
20299 nr = 0;
20300 if (arg != NULL)
20301 {
20302 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020303 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020304 else
20305 EMSG2(_(e_invexpr2), arg);
20306 }
20307 }
20308 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020309 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020310#endif
20311 rettv->vval.v_number = nr;
20312}
20313
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020314
20315#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020316static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020317
20318/*
20319 * Common code for tabpagewinnr() and winnr().
20320 */
20321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020322get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020323{
20324 win_T *twin;
20325 int nr = 1;
20326 win_T *wp;
20327 char_u *arg;
20328
20329 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20330 if (argvar->v_type != VAR_UNKNOWN)
20331 {
20332 arg = get_tv_string_chk(argvar);
20333 if (arg == NULL)
20334 nr = 0; /* type error; errmsg already given */
20335 else if (STRCMP(arg, "$") == 0)
20336 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20337 else if (STRCMP(arg, "#") == 0)
20338 {
20339 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20340 if (twin == NULL)
20341 nr = 0;
20342 }
20343 else
20344 {
20345 EMSG2(_(e_invexpr2), arg);
20346 nr = 0;
20347 }
20348 }
20349
20350 if (nr > 0)
20351 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20352 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020353 {
20354 if (wp == NULL)
20355 {
20356 /* didn't find it in this tabpage */
20357 nr = 0;
20358 break;
20359 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020360 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020361 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020362 return nr;
20363}
20364#endif
20365
20366/*
20367 * "tabpagewinnr()" function
20368 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020370f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020371{
20372 int nr = 1;
20373#ifdef FEAT_WINDOWS
20374 tabpage_T *tp;
20375
20376 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20377 if (tp == NULL)
20378 nr = 0;
20379 else
20380 nr = get_winnr(tp, &argvars[1]);
20381#endif
20382 rettv->vval.v_number = nr;
20383}
20384
20385
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020386/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020387 * "tagfiles()" function
20388 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020390f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020391{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020392 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020393 tagname_T tn;
20394 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020395
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020396 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020397 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020398 fname = alloc(MAXPATHL);
20399 if (fname == NULL)
20400 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020401
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020402 for (first = TRUE; ; first = FALSE)
20403 if (get_tagfname(&tn, first, fname) == FAIL
20404 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020405 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020406 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020407 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020408}
20409
20410/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020411 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020412 */
20413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020414f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020415{
20416 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020417
20418 tag_pattern = get_tv_string(&argvars[0]);
20419
20420 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020421 if (*tag_pattern == NUL)
20422 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020423
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020424 if (rettv_list_alloc(rettv) == OK)
20425 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020426}
20427
20428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 * "tempname()" function
20430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020432f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433{
20434 static int x = 'A';
20435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020436 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020437 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438
20439 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20440 * names. Skip 'I' and 'O', they are used for shell redirection. */
20441 do
20442 {
20443 if (x == 'Z')
20444 x = '0';
20445 else if (x == '9')
20446 x = 'A';
20447 else
20448 {
20449#ifdef EBCDIC
20450 if (x == 'I')
20451 x = 'J';
20452 else if (x == 'R')
20453 x = 'S';
20454 else
20455#endif
20456 ++x;
20457 }
20458 } while (x == 'I' || x == 'O');
20459}
20460
20461/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020462 * "test(list)" function: Just checking the walls...
20463 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020465f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020466{
20467 /* Used for unit testing. Change the code below to your liking. */
20468#if 0
20469 listitem_T *li;
20470 list_T *l;
20471 char_u *bad, *good;
20472
20473 if (argvars[0].v_type != VAR_LIST)
20474 return;
20475 l = argvars[0].vval.v_list;
20476 if (l == NULL)
20477 return;
20478 li = l->lv_first;
20479 if (li == NULL)
20480 return;
20481 bad = get_tv_string(&li->li_tv);
20482 li = li->li_next;
20483 if (li == NULL)
20484 return;
20485 good = get_tv_string(&li->li_tv);
20486 rettv->vval.v_number = test_edit_score(bad, good);
20487#endif
20488}
20489
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020490#ifdef FEAT_FLOAT
20491/*
20492 * "tan()" function
20493 */
20494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020495f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020496{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020497 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020498
20499 rettv->v_type = VAR_FLOAT;
20500 if (get_float_arg(argvars, &f) == OK)
20501 rettv->vval.v_float = tan(f);
20502 else
20503 rettv->vval.v_float = 0.0;
20504}
20505
20506/*
20507 * "tanh()" function
20508 */
20509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020510f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020511{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020512 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020513
20514 rettv->v_type = VAR_FLOAT;
20515 if (get_float_arg(argvars, &f) == OK)
20516 rettv->vval.v_float = tanh(f);
20517 else
20518 rettv->vval.v_float = 0.0;
20519}
20520#endif
20521
Bram Moolenaar975b5272016-03-15 23:10:59 +010020522#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20523/*
20524 * Get a callback from "arg". It can be a Funcref or a function name.
20525 * When "arg" is zero return an empty string.
20526 * Return NULL for an invalid argument.
20527 */
20528 char_u *
20529get_callback(typval_T *arg, partial_T **pp)
20530{
20531 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20532 {
20533 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020534 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020535 return (*pp)->pt_name;
20536 }
20537 *pp = NULL;
20538 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20539 return arg->vval.v_string;
20540 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20541 return (char_u *)"";
20542 EMSG(_("E921: Invalid callback argument"));
20543 return NULL;
20544}
20545#endif
20546
20547#ifdef FEAT_TIMERS
20548/*
20549 * "timer_start(time, callback [, options])" function
20550 */
20551 static void
20552f_timer_start(typval_T *argvars, typval_T *rettv)
20553{
20554 long msec = get_tv_number(&argvars[0]);
20555 timer_T *timer;
20556 int repeat = 0;
20557 char_u *callback;
20558 dict_T *dict;
20559
20560 if (argvars[2].v_type != VAR_UNKNOWN)
20561 {
20562 if (argvars[2].v_type != VAR_DICT
20563 || (dict = argvars[2].vval.v_dict) == NULL)
20564 {
20565 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20566 return;
20567 }
20568 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20569 repeat = get_dict_number(dict, (char_u *)"repeat");
20570 }
20571
20572 timer = create_timer(msec, repeat);
20573 callback = get_callback(&argvars[1], &timer->tr_partial);
20574 if (callback == NULL)
20575 {
20576 stop_timer(timer);
20577 rettv->vval.v_number = -1;
20578 }
20579 else
20580 {
20581 timer->tr_callback = vim_strsave(callback);
20582 rettv->vval.v_number = timer->tr_id;
20583 }
20584}
20585
20586/*
20587 * "timer_stop(timer)" function
20588 */
20589 static void
20590f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20591{
20592 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20593
20594 if (timer != NULL)
20595 stop_timer(timer);
20596}
20597#endif
20598
Bram Moolenaard52d9742005-08-21 22:20:28 +000020599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 * "tolower(string)" function
20601 */
20602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020603f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604{
20605 char_u *p;
20606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020607 p = vim_strsave(get_tv_string(&argvars[0]));
20608 rettv->v_type = VAR_STRING;
20609 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610
20611 if (p != NULL)
20612 while (*p != NUL)
20613 {
20614#ifdef FEAT_MBYTE
20615 int l;
20616
20617 if (enc_utf8)
20618 {
20619 int c, lc;
20620
20621 c = utf_ptr2char(p);
20622 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020623 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 /* TODO: reallocate string when byte count changes. */
20625 if (utf_char2len(lc) == l)
20626 utf_char2bytes(lc, p);
20627 p += l;
20628 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020629 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 p += l; /* skip multi-byte character */
20631 else
20632#endif
20633 {
20634 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20635 ++p;
20636 }
20637 }
20638}
20639
20640/*
20641 * "toupper(string)" function
20642 */
20643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020644f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020646 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020647 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648}
20649
20650/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020651 * "tr(string, fromstr, tostr)" function
20652 */
20653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020654f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020655{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020656 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020657 char_u *fromstr;
20658 char_u *tostr;
20659 char_u *p;
20660#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020661 int inlen;
20662 int fromlen;
20663 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020664 int idx;
20665 char_u *cpstr;
20666 int cplen;
20667 int first = TRUE;
20668#endif
20669 char_u buf[NUMBUFLEN];
20670 char_u buf2[NUMBUFLEN];
20671 garray_T ga;
20672
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020673 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020674 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20675 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020676
20677 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020678 rettv->v_type = VAR_STRING;
20679 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020680 if (fromstr == NULL || tostr == NULL)
20681 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020682 ga_init2(&ga, (int)sizeof(char), 80);
20683
20684#ifdef FEAT_MBYTE
20685 if (!has_mbyte)
20686#endif
20687 /* not multi-byte: fromstr and tostr must be the same length */
20688 if (STRLEN(fromstr) != STRLEN(tostr))
20689 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020690#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020691error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020692#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020693 EMSG2(_(e_invarg2), fromstr);
20694 ga_clear(&ga);
20695 return;
20696 }
20697
20698 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020699 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020700 {
20701#ifdef FEAT_MBYTE
20702 if (has_mbyte)
20703 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020704 inlen = (*mb_ptr2len)(in_str);
20705 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020706 cplen = inlen;
20707 idx = 0;
20708 for (p = fromstr; *p != NUL; p += fromlen)
20709 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020710 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020711 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020712 {
20713 for (p = tostr; *p != NUL; p += tolen)
20714 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020715 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020716 if (idx-- == 0)
20717 {
20718 cplen = tolen;
20719 cpstr = p;
20720 break;
20721 }
20722 }
20723 if (*p == NUL) /* tostr is shorter than fromstr */
20724 goto error;
20725 break;
20726 }
20727 ++idx;
20728 }
20729
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020730 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020731 {
20732 /* Check that fromstr and tostr have the same number of
20733 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020734 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020735 first = FALSE;
20736 for (p = tostr; *p != NUL; p += tolen)
20737 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020738 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020739 --idx;
20740 }
20741 if (idx != 0)
20742 goto error;
20743 }
20744
Bram Moolenaarcde88542015-08-11 19:14:00 +020020745 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020746 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020747 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020748
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020749 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020750 }
20751 else
20752#endif
20753 {
20754 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020755 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020756 if (p != NULL)
20757 ga_append(&ga, tostr[p - fromstr]);
20758 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020759 ga_append(&ga, *in_str);
20760 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020761 }
20762 }
20763
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020764 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020765 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020766 ga_append(&ga, NUL);
20767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020768 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020769}
20770
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020771#ifdef FEAT_FLOAT
20772/*
20773 * "trunc({float})" function
20774 */
20775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020776f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020777{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020778 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020779
20780 rettv->v_type = VAR_FLOAT;
20781 if (get_float_arg(argvars, &f) == OK)
20782 /* trunc() is not in C90, use floor() or ceil() instead. */
20783 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20784 else
20785 rettv->vval.v_float = 0.0;
20786}
20787#endif
20788
Bram Moolenaar8299df92004-07-10 09:47:34 +000020789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 * "type(expr)" function
20791 */
20792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020793f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020795 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020796
20797 switch (argvars[0].v_type)
20798 {
20799 case VAR_NUMBER: n = 0; break;
20800 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020801 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020802 case VAR_FUNC: n = 2; break;
20803 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020804 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020805 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020806 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020807 if (argvars[0].vval.v_number == VVAL_FALSE
20808 || argvars[0].vval.v_number == VVAL_TRUE)
20809 n = 6;
20810 else
20811 n = 7;
20812 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020813 case VAR_JOB: n = 8; break;
20814 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020815 case VAR_UNKNOWN:
20816 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20817 n = -1;
20818 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020819 }
20820 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821}
20822
20823/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020824 * "undofile(name)" function
20825 */
20826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020827f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020828{
20829 rettv->v_type = VAR_STRING;
20830#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020831 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020832 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020833
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020834 if (*fname == NUL)
20835 {
20836 /* If there is no file name there will be no undo file. */
20837 rettv->vval.v_string = NULL;
20838 }
20839 else
20840 {
20841 char_u *ffname = FullName_save(fname, FALSE);
20842
20843 if (ffname != NULL)
20844 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20845 vim_free(ffname);
20846 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020847 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020848#else
20849 rettv->vval.v_string = NULL;
20850#endif
20851}
20852
20853/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020854 * "undotree()" function
20855 */
20856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020857f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020858{
20859 if (rettv_dict_alloc(rettv) == OK)
20860 {
20861 dict_T *dict = rettv->vval.v_dict;
20862 list_T *list;
20863
Bram Moolenaar730cde92010-06-27 05:18:54 +020020864 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020865 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020866 dict_add_nr_str(dict, "save_last",
20867 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020868 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20869 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020870 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020871
20872 list = list_alloc();
20873 if (list != NULL)
20874 {
20875 u_eval_tree(curbuf->b_u_oldhead, list);
20876 dict_add_list(dict, "entries", list);
20877 }
20878 }
20879}
20880
20881/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020882 * "values(dict)" function
20883 */
20884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020885f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020886{
20887 dict_list(argvars, rettv, 1);
20888}
20889
20890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 * "virtcol(string)" function
20892 */
20893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020894f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895{
20896 colnr_T vcol = 0;
20897 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020898 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020900 fp = var2fpos(&argvars[0], FALSE, &fnum);
20901 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20902 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 {
20904 getvvcol(curwin, fp, NULL, NULL, &vcol);
20905 ++vcol;
20906 }
20907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020908 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909}
20910
20911/*
20912 * "visualmode()" function
20913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020915f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 char_u str[2];
20918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020919 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 str[0] = curbuf->b_visual_mode_eval;
20921 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020922 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923
20924 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020925 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927}
20928
20929/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020930 * "wildmenumode()" function
20931 */
20932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020933f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020934{
20935#ifdef FEAT_WILDMENU
20936 if (wild_menu_showing)
20937 rettv->vval.v_number = 1;
20938#endif
20939}
20940
20941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 * "winbufnr(nr)" function
20943 */
20944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020945f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946{
20947 win_T *wp;
20948
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020949 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020951 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020953 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954}
20955
20956/*
20957 * "wincol()" function
20958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020960f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961{
20962 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020963 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964}
20965
20966/*
20967 * "winheight(nr)" function
20968 */
20969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020970f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971{
20972 win_T *wp;
20973
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020974 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020978 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979}
20980
20981/*
20982 * "winline()" function
20983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020985f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986{
20987 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020988 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989}
20990
20991/*
20992 * "winnr()" function
20993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020995f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996{
20997 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020998
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021000 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021002 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003}
21004
21005/*
21006 * "winrestcmd()" function
21007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021009f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010{
21011#ifdef FEAT_WINDOWS
21012 win_T *wp;
21013 int winnr = 1;
21014 garray_T ga;
21015 char_u buf[50];
21016
21017 ga_init2(&ga, (int)sizeof(char), 70);
21018 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21019 {
21020 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21021 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21023 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024 ++winnr;
21025 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021026 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021028 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033}
21034
21035/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021036 * "winrestview()" function
21037 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021039f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021040{
21041 dict_T *dict;
21042
21043 if (argvars[0].v_type != VAR_DICT
21044 || (dict = argvars[0].vval.v_dict) == NULL)
21045 EMSG(_(e_invarg));
21046 else
21047 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021048 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21049 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21050 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21051 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021052#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021053 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21054 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021055#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021056 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21057 {
21058 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21059 curwin->w_set_curswant = FALSE;
21060 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021061
Bram Moolenaar82c25852014-05-28 16:47:16 +020021062 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21063 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021064#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021065 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21066 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021067#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021068 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21069 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21070 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21071 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021072
21073 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021074 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021075# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021076 win_new_width(curwin, W_WIDTH(curwin));
21077# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021078 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021079
Bram Moolenaarb851a962014-10-31 15:45:52 +010021080 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021081 curwin->w_topline = 1;
21082 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21083 curwin->w_topline = curbuf->b_ml.ml_line_count;
21084#ifdef FEAT_DIFF
21085 check_topfill(curwin, TRUE);
21086#endif
21087 }
21088}
21089
21090/*
21091 * "winsaveview()" function
21092 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021094f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021095{
21096 dict_T *dict;
21097
Bram Moolenaara800b422010-06-27 01:15:55 +020021098 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021099 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021100 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021101
21102 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21103 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21104#ifdef FEAT_VIRTUALEDIT
21105 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21106#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021107 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021108 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21109
21110 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21111#ifdef FEAT_DIFF
21112 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21113#endif
21114 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21115 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21116}
21117
21118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 * "winwidth(nr)" function
21120 */
21121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021122f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123{
21124 win_T *wp;
21125
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021126 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021128 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021130#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021131 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021133 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134#endif
21135}
21136
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021138 * "wordcount()" function
21139 */
21140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021141f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021142{
21143 if (rettv_dict_alloc(rettv) == FAIL)
21144 return;
21145 cursor_pos_info(rettv->vval.v_dict);
21146}
21147
21148/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021149 * Write list of strings to file
21150 */
21151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021152write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021153{
21154 listitem_T *li;
21155 int c;
21156 int ret = OK;
21157 char_u *s;
21158
21159 for (li = list->lv_first; li != NULL; li = li->li_next)
21160 {
21161 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21162 {
21163 if (*s == '\n')
21164 c = putc(NUL, fd);
21165 else
21166 c = putc(*s, fd);
21167 if (c == EOF)
21168 {
21169 ret = FAIL;
21170 break;
21171 }
21172 }
21173 if (!binary || li->li_next != NULL)
21174 if (putc('\n', fd) == EOF)
21175 {
21176 ret = FAIL;
21177 break;
21178 }
21179 if (ret == FAIL)
21180 {
21181 EMSG(_(e_write));
21182 break;
21183 }
21184 }
21185 return ret;
21186}
21187
21188/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021189 * "writefile()" function
21190 */
21191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021192f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021193{
21194 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021195 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021196 char_u *fname;
21197 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021198 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021199
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021200 if (check_restricted() || check_secure())
21201 return;
21202
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021203 if (argvars[0].v_type != VAR_LIST)
21204 {
21205 EMSG2(_(e_listarg), "writefile()");
21206 return;
21207 }
21208 if (argvars[0].vval.v_list == NULL)
21209 return;
21210
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021211 if (argvars[2].v_type != VAR_UNKNOWN)
21212 {
21213 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21214 binary = TRUE;
21215 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21216 append = TRUE;
21217 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021218
21219 /* Always open the file in binary mode, library functions have a mind of
21220 * their own about CR-LF conversion. */
21221 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021222 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21223 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021224 {
21225 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21226 ret = -1;
21227 }
21228 else
21229 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021230 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21231 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021232 fclose(fd);
21233 }
21234
21235 rettv->vval.v_number = ret;
21236}
21237
21238/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021239 * "xor(expr, expr)" function
21240 */
21241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021242f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021243{
21244 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21245 ^ get_tv_number_chk(&argvars[1], NULL);
21246}
21247
21248
21249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021251 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 */
21253 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021254var2fpos(
21255 typval_T *varp,
21256 int dollar_lnum, /* TRUE when $ is last line */
21257 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021259 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021261 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262
Bram Moolenaara5525202006-03-02 22:52:09 +000021263 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021264 if (varp->v_type == VAR_LIST)
21265 {
21266 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021267 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021268 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021269 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021270
21271 l = varp->vval.v_list;
21272 if (l == NULL)
21273 return NULL;
21274
21275 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021276 pos.lnum = list_find_nr(l, 0L, &error);
21277 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021278 return NULL; /* invalid line number */
21279
21280 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021281 pos.col = list_find_nr(l, 1L, &error);
21282 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021283 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021284 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021285
21286 /* We accept "$" for the column number: last column. */
21287 li = list_find(l, 1L);
21288 if (li != NULL && li->li_tv.v_type == VAR_STRING
21289 && li->li_tv.vval.v_string != NULL
21290 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21291 pos.col = len + 1;
21292
Bram Moolenaara5525202006-03-02 22:52:09 +000021293 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021294 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021295 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021296 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021297
Bram Moolenaara5525202006-03-02 22:52:09 +000021298#ifdef FEAT_VIRTUALEDIT
21299 /* Get the virtual offset. Defaults to zero. */
21300 pos.coladd = list_find_nr(l, 2L, &error);
21301 if (error)
21302 pos.coladd = 0;
21303#endif
21304
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021305 return &pos;
21306 }
21307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021308 name = get_tv_string_chk(varp);
21309 if (name == NULL)
21310 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021311 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021313 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21314 {
21315 if (VIsual_active)
21316 return &VIsual;
21317 return &curwin->w_cursor;
21318 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021319 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021321 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21323 return NULL;
21324 return pp;
21325 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021326
21327#ifdef FEAT_VIRTUALEDIT
21328 pos.coladd = 0;
21329#endif
21330
Bram Moolenaar477933c2007-07-17 14:32:23 +000021331 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021332 {
21333 pos.col = 0;
21334 if (name[1] == '0') /* "w0": first visible line */
21335 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021336 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021337 pos.lnum = curwin->w_topline;
21338 return &pos;
21339 }
21340 else if (name[1] == '$') /* "w$": last visible line */
21341 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021342 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021343 pos.lnum = curwin->w_botline - 1;
21344 return &pos;
21345 }
21346 }
21347 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021349 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 {
21351 pos.lnum = curbuf->b_ml.ml_line_count;
21352 pos.col = 0;
21353 }
21354 else
21355 {
21356 pos.lnum = curwin->w_cursor.lnum;
21357 pos.col = (colnr_T)STRLEN(ml_get_curline());
21358 }
21359 return &pos;
21360 }
21361 return NULL;
21362}
21363
21364/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021365 * Convert list in "arg" into a position and optional file number.
21366 * When "fnump" is NULL there is no file number, only 3 items.
21367 * Note that the column is passed on as-is, the caller may want to decrement
21368 * it to use 1 for the first column.
21369 * Return FAIL when conversion is not possible, doesn't check the position for
21370 * validity.
21371 */
21372 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021373list2fpos(
21374 typval_T *arg,
21375 pos_T *posp,
21376 int *fnump,
21377 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021378{
21379 list_T *l = arg->vval.v_list;
21380 long i = 0;
21381 long n;
21382
Bram Moolenaar493c1782014-05-28 14:34:46 +020021383 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21384 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021385 if (arg->v_type != VAR_LIST
21386 || l == NULL
21387 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021388 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021389 return FAIL;
21390
21391 if (fnump != NULL)
21392 {
21393 n = list_find_nr(l, i++, NULL); /* fnum */
21394 if (n < 0)
21395 return FAIL;
21396 if (n == 0)
21397 n = curbuf->b_fnum; /* current buffer */
21398 *fnump = n;
21399 }
21400
21401 n = list_find_nr(l, i++, NULL); /* lnum */
21402 if (n < 0)
21403 return FAIL;
21404 posp->lnum = n;
21405
21406 n = list_find_nr(l, i++, NULL); /* col */
21407 if (n < 0)
21408 return FAIL;
21409 posp->col = n;
21410
21411#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021412 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021413 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021414 posp->coladd = 0;
21415 else
21416 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021417#endif
21418
Bram Moolenaar493c1782014-05-28 14:34:46 +020021419 if (curswantp != NULL)
21420 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21421
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021422 return OK;
21423}
21424
21425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 * Get the length of an environment variable name.
21427 * Advance "arg" to the first character after the name.
21428 * Return 0 for error.
21429 */
21430 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021431get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432{
21433 char_u *p;
21434 int len;
21435
21436 for (p = *arg; vim_isIDc(*p); ++p)
21437 ;
21438 if (p == *arg) /* no name found */
21439 return 0;
21440
21441 len = (int)(p - *arg);
21442 *arg = p;
21443 return len;
21444}
21445
21446/*
21447 * Get the length of the name of a function or internal variable.
21448 * "arg" is advanced to the first non-white character after the name.
21449 * Return 0 if something is wrong.
21450 */
21451 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021452get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453{
21454 char_u *p;
21455 int len;
21456
21457 /* Find the end of the name. */
21458 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021459 {
21460 if (*p == ':')
21461 {
21462 /* "s:" is start of "s:var", but "n:" is not and can be used in
21463 * slice "[n:]". Also "xx:" is not a namespace. */
21464 len = (int)(p - *arg);
21465 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21466 || len > 1)
21467 break;
21468 }
21469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470 if (p == *arg) /* no name found */
21471 return 0;
21472
21473 len = (int)(p - *arg);
21474 *arg = skipwhite(p);
21475
21476 return len;
21477}
21478
21479/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021480 * Get the length of the name of a variable or function.
21481 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021483 * Return -1 if curly braces expansion failed.
21484 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 * If the name contains 'magic' {}'s, expand them and return the
21486 * expanded name in an allocated string via 'alias' - caller must free.
21487 */
21488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021489get_name_len(
21490 char_u **arg,
21491 char_u **alias,
21492 int evaluate,
21493 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494{
21495 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 char_u *p;
21497 char_u *expr_start;
21498 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499
21500 *alias = NULL; /* default to no alias */
21501
21502 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21503 && (*arg)[2] == (int)KE_SNR)
21504 {
21505 /* hard coded <SNR>, already translated */
21506 *arg += 3;
21507 return get_id_len(arg) + 3;
21508 }
21509 len = eval_fname_script(*arg);
21510 if (len > 0)
21511 {
21512 /* literal "<SID>", "s:" or "<SNR>" */
21513 *arg += len;
21514 }
21515
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021517 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021519 p = find_name_end(*arg, &expr_start, &expr_end,
21520 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521 if (expr_start != NULL)
21522 {
21523 char_u *temp_string;
21524
21525 if (!evaluate)
21526 {
21527 len += (int)(p - *arg);
21528 *arg = skipwhite(p);
21529 return len;
21530 }
21531
21532 /*
21533 * Include any <SID> etc in the expanded string:
21534 * Thus the -len here.
21535 */
21536 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21537 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021538 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 *alias = temp_string;
21540 *arg = skipwhite(p);
21541 return (int)STRLEN(temp_string);
21542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543
21544 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021545 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 EMSG2(_(e_invexpr2), *arg);
21547
21548 return len;
21549}
21550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021551/*
21552 * Find the end of a variable or function name, taking care of magic braces.
21553 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21554 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021555 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021556 * Return a pointer to just after the name. Equal to "arg" if there is no
21557 * valid name.
21558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021560find_name_end(
21561 char_u *arg,
21562 char_u **expr_start,
21563 char_u **expr_end,
21564 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021566 int mb_nest = 0;
21567 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021569 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021571 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021573 *expr_start = NULL;
21574 *expr_end = NULL;
21575 }
21576
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021577 /* Quick check for valid starting character. */
21578 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21579 return arg;
21580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581 for (p = arg; *p != NUL
21582 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021583 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021584 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021585 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021586 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021587 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021588 if (*p == '\'')
21589 {
21590 /* skip over 'string' to avoid counting [ and ] inside it. */
21591 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21592 ;
21593 if (*p == NUL)
21594 break;
21595 }
21596 else if (*p == '"')
21597 {
21598 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21599 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21600 if (*p == '\\' && p[1] != NUL)
21601 ++p;
21602 if (*p == NUL)
21603 break;
21604 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021605 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21606 {
21607 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021608 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021609 len = (int)(p - arg);
21610 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021611 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021612 break;
21613 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021615 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021617 if (*p == '[')
21618 ++br_nest;
21619 else if (*p == ']')
21620 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021623 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625 if (*p == '{')
21626 {
21627 mb_nest++;
21628 if (expr_start != NULL && *expr_start == NULL)
21629 *expr_start = p;
21630 }
21631 else if (*p == '}')
21632 {
21633 mb_nest--;
21634 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21635 *expr_end = p;
21636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 }
21639
21640 return p;
21641}
21642
21643/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021644 * Expands out the 'magic' {}'s in a variable/function name.
21645 * Note that this can call itself recursively, to deal with
21646 * constructs like foo{bar}{baz}{bam}
21647 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21648 * "in_start" ^
21649 * "expr_start" ^
21650 * "expr_end" ^
21651 * "in_end" ^
21652 *
21653 * Returns a new allocated string, which the caller must free.
21654 * Returns NULL for failure.
21655 */
21656 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021657make_expanded_name(
21658 char_u *in_start,
21659 char_u *expr_start,
21660 char_u *expr_end,
21661 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021662{
21663 char_u c1;
21664 char_u *retval = NULL;
21665 char_u *temp_result;
21666 char_u *nextcmd = NULL;
21667
21668 if (expr_end == NULL || in_end == NULL)
21669 return NULL;
21670 *expr_start = NUL;
21671 *expr_end = NUL;
21672 c1 = *in_end;
21673 *in_end = NUL;
21674
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021675 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021676 if (temp_result != NULL && nextcmd == NULL)
21677 {
21678 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21679 + (in_end - expr_end) + 1));
21680 if (retval != NULL)
21681 {
21682 STRCPY(retval, in_start);
21683 STRCAT(retval, temp_result);
21684 STRCAT(retval, expr_end + 1);
21685 }
21686 }
21687 vim_free(temp_result);
21688
21689 *in_end = c1; /* put char back for error messages */
21690 *expr_start = '{';
21691 *expr_end = '}';
21692
21693 if (retval != NULL)
21694 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021695 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021696 if (expr_start != NULL)
21697 {
21698 /* Further expansion! */
21699 temp_result = make_expanded_name(retval, expr_start,
21700 expr_end, temp_result);
21701 vim_free(retval);
21702 retval = temp_result;
21703 }
21704 }
21705
21706 return retval;
21707}
21708
21709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021711 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712 */
21713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021714eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021716 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21717}
21718
21719/*
21720 * Return TRUE if character "c" can be used as the first character in a
21721 * variable or function name (excluding '{' and '}').
21722 */
21723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021724eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021725{
21726 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727}
21728
21729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 * Set number v: variable to "val".
21731 */
21732 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021733set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021735 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736}
21737
21738/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021739 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 */
21741 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021742get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021744 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745}
21746
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021747/*
21748 * Get string v: variable value. Uses a static buffer, can only be used once.
21749 */
21750 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021751get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021752{
21753 return get_tv_string(&vimvars[idx].vv_tv);
21754}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021755
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021757 * Get List v: variable value. Caller must take care of reference count when
21758 * needed.
21759 */
21760 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021761get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021762{
21763 return vimvars[idx].vv_list;
21764}
21765
21766/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021767 * Set v:char to character "c".
21768 */
21769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021770set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021771{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021772 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021773
21774#ifdef FEAT_MBYTE
21775 if (has_mbyte)
21776 buf[(*mb_char2bytes)(c, buf)] = NUL;
21777 else
21778#endif
21779 {
21780 buf[0] = c;
21781 buf[1] = NUL;
21782 }
21783 set_vim_var_string(VV_CHAR, buf, -1);
21784}
21785
21786/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021787 * Set v:count to "count" and v:count1 to "count1".
21788 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 */
21790 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021791set_vcount(
21792 long count,
21793 long count1,
21794 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021796 if (set_prevcount)
21797 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021798 vimvars[VV_COUNT].vv_nr = count;
21799 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800}
21801
21802/*
21803 * Set string v: variable to a copy of "val".
21804 */
21805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021806set_vim_var_string(
21807 int idx,
21808 char_u *val,
21809 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810{
Bram Moolenaara542c682016-01-31 16:28:04 +010021811 clear_tv(&vimvars[idx].vv_di.di_tv);
21812 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021814 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021816 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021818 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819}
21820
21821/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021822 * Set List v: variable to "val".
21823 */
21824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021825set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021826{
Bram Moolenaara542c682016-01-31 16:28:04 +010021827 clear_tv(&vimvars[idx].vv_di.di_tv);
21828 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021829 vimvars[idx].vv_list = val;
21830 if (val != NULL)
21831 ++val->lv_refcount;
21832}
21833
21834/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021835 * Set Dictionary v: variable to "val".
21836 */
21837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021838set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021839{
21840 int todo;
21841 hashitem_T *hi;
21842
Bram Moolenaara542c682016-01-31 16:28:04 +010021843 clear_tv(&vimvars[idx].vv_di.di_tv);
21844 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021845 vimvars[idx].vv_dict = val;
21846 if (val != NULL)
21847 {
21848 ++val->dv_refcount;
21849
21850 /* Set readonly */
21851 todo = (int)val->dv_hashtab.ht_used;
21852 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21853 {
21854 if (HASHITEM_EMPTY(hi))
21855 continue;
21856 --todo;
21857 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21858 }
21859 }
21860}
21861
21862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 * Set v:register if needed.
21864 */
21865 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021866set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867{
21868 char_u regname;
21869
21870 if (c == 0 || c == ' ')
21871 regname = '"';
21872 else
21873 regname = c;
21874 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021875 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 set_vim_var_string(VV_REG, &regname, 1);
21877}
21878
21879/*
21880 * Get or set v:exception. If "oldval" == NULL, return the current value.
21881 * Otherwise, restore the value to "oldval" and return NULL.
21882 * Must always be called in pairs to save and restore v:exception! Does not
21883 * take care of memory allocations.
21884 */
21885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021886v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887{
21888 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021889 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890
Bram Moolenaare9a41262005-01-15 22:18:47 +000021891 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 return NULL;
21893}
21894
21895/*
21896 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21897 * Otherwise, restore the value to "oldval" and return NULL.
21898 * Must always be called in pairs to save and restore v:throwpoint! Does not
21899 * take care of memory allocations.
21900 */
21901 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021902v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903{
21904 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021905 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906
Bram Moolenaare9a41262005-01-15 22:18:47 +000021907 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 return NULL;
21909}
21910
21911#if defined(FEAT_AUTOCMD) || defined(PROTO)
21912/*
21913 * Set v:cmdarg.
21914 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21915 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21916 * Must always be called in pairs!
21917 */
21918 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021919set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920{
21921 char_u *oldval;
21922 char_u *newval;
21923 unsigned len;
21924
Bram Moolenaare9a41262005-01-15 22:18:47 +000021925 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021926 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021928 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021929 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021930 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 }
21932
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021933 if (eap->force_bin == FORCE_BIN)
21934 len = 6;
21935 else if (eap->force_bin == FORCE_NOBIN)
21936 len = 8;
21937 else
21938 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021939
21940 if (eap->read_edit)
21941 len += 7;
21942
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021943 if (eap->force_ff != 0)
21944 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21945# ifdef FEAT_MBYTE
21946 if (eap->force_enc != 0)
21947 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021948 if (eap->bad_char != 0)
21949 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021950# endif
21951
21952 newval = alloc(len + 1);
21953 if (newval == NULL)
21954 return NULL;
21955
21956 if (eap->force_bin == FORCE_BIN)
21957 sprintf((char *)newval, " ++bin");
21958 else if (eap->force_bin == FORCE_NOBIN)
21959 sprintf((char *)newval, " ++nobin");
21960 else
21961 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021962
21963 if (eap->read_edit)
21964 STRCAT(newval, " ++edit");
21965
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021966 if (eap->force_ff != 0)
21967 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21968 eap->cmd + eap->force_ff);
21969# ifdef FEAT_MBYTE
21970 if (eap->force_enc != 0)
21971 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21972 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021973 if (eap->bad_char == BAD_KEEP)
21974 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21975 else if (eap->bad_char == BAD_DROP)
21976 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21977 else if (eap->bad_char != 0)
21978 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021979# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021980 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021981 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982}
21983#endif
21984
21985/*
21986 * Get the value of internal variable "name".
21987 * Return OK or FAIL.
21988 */
21989 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021990get_var_tv(
21991 char_u *name,
21992 int len, /* length of "name" */
21993 typval_T *rettv, /* NULL when only checking existence */
21994 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21995 int verbose, /* may give error message */
21996 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997{
21998 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021999 typval_T *tv = NULL;
22000 typval_T atv;
22001 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003
22004 /* truncate the name, so that we can use strcmp() */
22005 cc = name[len];
22006 name[len] = NUL;
22007
22008 /*
22009 * Check for "b:changedtick".
22010 */
22011 if (STRCMP(name, "b:changedtick") == 0)
22012 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022013 atv.v_type = VAR_NUMBER;
22014 atv.vval.v_number = curbuf->b_changedtick;
22015 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 }
22017
22018 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 * Check for user-defined variables.
22020 */
22021 else
22022 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022023 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022025 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022026 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022027 if (dip != NULL)
22028 *dip = v;
22029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 }
22031
Bram Moolenaare9a41262005-01-15 22:18:47 +000022032 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022034 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022035 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 ret = FAIL;
22037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022038 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022039 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040
22041 name[len] = cc;
22042
22043 return ret;
22044}
22045
22046/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022047 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22048 * Also handle function call with Funcref variable: func(expr)
22049 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22050 */
22051 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022052handle_subscript(
22053 char_u **arg,
22054 typval_T *rettv,
22055 int evaluate, /* do more than finding the end */
22056 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022057{
22058 int ret = OK;
22059 dict_T *selfdict = NULL;
22060 char_u *s;
22061 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022062 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022063
22064 while (ret == OK
22065 && (**arg == '['
22066 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022067 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22068 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022069 && !vim_iswhite(*(*arg - 1)))
22070 {
22071 if (**arg == '(')
22072 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022073 partial_T *pt = NULL;
22074
Bram Moolenaard9fba312005-06-26 22:34:35 +000022075 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022076 if (evaluate)
22077 {
22078 functv = *rettv;
22079 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022080
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022081 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022082 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022083 {
22084 pt = functv.vval.v_partial;
22085 s = pt->pt_name;
22086 }
22087 else
22088 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022089 }
22090 else
22091 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022092 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022093 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022094 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022095
22096 /* Clear the funcref afterwards, so that deleting it while
22097 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022098 if (evaluate)
22099 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022100
22101 /* Stop the expression evaluation when immediately aborting on
22102 * error, or when an interrupt occurred or an exception was thrown
22103 * but not caught. */
22104 if (aborting())
22105 {
22106 if (ret == OK)
22107 clear_tv(rettv);
22108 ret = FAIL;
22109 }
22110 dict_unref(selfdict);
22111 selfdict = NULL;
22112 }
22113 else /* **arg == '[' || **arg == '.' */
22114 {
22115 dict_unref(selfdict);
22116 if (rettv->v_type == VAR_DICT)
22117 {
22118 selfdict = rettv->vval.v_dict;
22119 if (selfdict != NULL)
22120 ++selfdict->dv_refcount;
22121 }
22122 else
22123 selfdict = NULL;
22124 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22125 {
22126 clear_tv(rettv);
22127 ret = FAIL;
22128 }
22129 }
22130 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022131
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022132 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
22133 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022134 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022135 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22136 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022137 char_u *tofree = NULL;
22138 ufunc_T *fp;
22139 char_u fname_buf[FLEN_FIXED + 1];
22140 int error;
22141
22142 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022143 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022144 fp = find_func(fname);
22145 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022146
22147 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010022148 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022149 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022150 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22151
22152 if (pt != NULL)
22153 {
22154 pt->pt_refcount = 1;
22155 pt->pt_dict = selfdict;
22156 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022157 if (rettv->v_type == VAR_FUNC)
22158 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022159 /* Just a function: Take over the function name and use
22160 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022161 pt->pt_name = rettv->vval.v_string;
22162 }
22163 else
22164 {
22165 partial_T *ret_pt = rettv->vval.v_partial;
22166 int i;
22167
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022168 /* Partial: copy the function name, use selfdict and copy
22169 * args. Can't take over name or args, the partial might
22170 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022171 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022172 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022173 if (ret_pt->pt_argc > 0)
22174 {
22175 pt->pt_argv = (typval_T *)alloc(
22176 sizeof(typval_T) * ret_pt->pt_argc);
22177 if (pt->pt_argv == NULL)
22178 /* out of memory: drop the arguments */
22179 pt->pt_argc = 0;
22180 else
22181 {
22182 pt->pt_argc = ret_pt->pt_argc;
22183 for (i = 0; i < pt->pt_argc; i++)
22184 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22185 }
22186 }
22187 partial_unref(ret_pt);
22188 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022189 rettv->v_type = VAR_PARTIAL;
22190 rettv->vval.v_partial = pt;
22191 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022192 }
22193 }
22194
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022195 dict_unref(selfdict);
22196 return ret;
22197}
22198
22199/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022200 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022201 * value).
22202 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022203 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022204alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022205{
Bram Moolenaar33570922005-01-25 22:26:29 +000022206 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022207}
22208
22209/*
22210 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 * The string "s" must have been allocated, it is consumed.
22212 * Return NULL for out of memory, the variable otherwise.
22213 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022214 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022215alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216{
Bram Moolenaar33570922005-01-25 22:26:29 +000022217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022219 rettv = alloc_tv();
22220 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022222 rettv->v_type = VAR_STRING;
22223 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 }
22225 else
22226 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022227 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228}
22229
22230/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022231 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022234free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235{
22236 if (varp != NULL)
22237 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022238 switch (varp->v_type)
22239 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022240 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022241 func_unref(varp->vval.v_string);
22242 /*FALLTHROUGH*/
22243 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022244 vim_free(varp->vval.v_string);
22245 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022246 case VAR_PARTIAL:
22247 partial_unref(varp->vval.v_partial);
22248 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022249 case VAR_LIST:
22250 list_unref(varp->vval.v_list);
22251 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022252 case VAR_DICT:
22253 dict_unref(varp->vval.v_dict);
22254 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022255 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022256#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022257 job_unref(varp->vval.v_job);
22258 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022259#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022260 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022261#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022262 channel_unref(varp->vval.v_channel);
22263 break;
22264#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022265 case VAR_NUMBER:
22266 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022267 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022268 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022269 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 vim_free(varp);
22272 }
22273}
22274
22275/*
22276 * Free the memory for a variable value and set the value to NULL or 0.
22277 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022279clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280{
22281 if (varp != NULL)
22282 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022283 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022285 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022286 func_unref(varp->vval.v_string);
22287 /*FALLTHROUGH*/
22288 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022289 vim_free(varp->vval.v_string);
22290 varp->vval.v_string = NULL;
22291 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022292 case VAR_PARTIAL:
22293 partial_unref(varp->vval.v_partial);
22294 varp->vval.v_partial = NULL;
22295 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022296 case VAR_LIST:
22297 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022298 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022299 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022300 case VAR_DICT:
22301 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022302 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022303 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022304 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022305 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022306 varp->vval.v_number = 0;
22307 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022308 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022309#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022310 varp->vval.v_float = 0.0;
22311 break;
22312#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022313 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022314#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022315 job_unref(varp->vval.v_job);
22316 varp->vval.v_job = NULL;
22317#endif
22318 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022319 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022320#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022321 channel_unref(varp->vval.v_channel);
22322 varp->vval.v_channel = NULL;
22323#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022324 case VAR_UNKNOWN:
22325 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022327 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 }
22329}
22330
22331/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022332 * Set the value of a variable to NULL without freeing items.
22333 */
22334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022335init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022336{
22337 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022338 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022339}
22340
22341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 * Get the number value of a variable.
22343 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022344 * For incompatible types, return 0.
22345 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22346 * caller of incompatible types: it sets *denote to TRUE if "denote"
22347 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022349 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022350get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022352 int error = FALSE;
22353
22354 return get_tv_number_chk(varp, &error); /* return 0L on error */
22355}
22356
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022357 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022358get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022359{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022360 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022362 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022364 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022365 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022366 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022367#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022368 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022369 break;
22370#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022371 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022372 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022373 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022374 break;
22375 case VAR_STRING:
22376 if (varp->vval.v_string != NULL)
22377 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022378 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022379 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022380 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022381 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022382 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022383 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022384 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022385 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022386 case VAR_SPECIAL:
22387 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22388 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022389 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022390#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022391 EMSG(_("E910: Using a Job as a Number"));
22392 break;
22393#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022394 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022395#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022396 EMSG(_("E913: Using a Channel as a Number"));
22397 break;
22398#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022399 case VAR_UNKNOWN:
22400 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022401 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022403 if (denote == NULL) /* useful for values that must be unsigned */
22404 n = -1;
22405 else
22406 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022407 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408}
22409
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022410#ifdef FEAT_FLOAT
22411 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022412get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022413{
22414 switch (varp->v_type)
22415 {
22416 case VAR_NUMBER:
22417 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022418 case VAR_FLOAT:
22419 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022420 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022421 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022422 EMSG(_("E891: Using a Funcref as a Float"));
22423 break;
22424 case VAR_STRING:
22425 EMSG(_("E892: Using a String as a Float"));
22426 break;
22427 case VAR_LIST:
22428 EMSG(_("E893: Using a List as a Float"));
22429 break;
22430 case VAR_DICT:
22431 EMSG(_("E894: Using a Dictionary as a Float"));
22432 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022433 case VAR_SPECIAL:
22434 EMSG(_("E907: Using a special value as a Float"));
22435 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022436 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022437# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022438 EMSG(_("E911: Using a Job as a Float"));
22439 break;
22440# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022441 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022442# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022443 EMSG(_("E914: Using a Channel as a Float"));
22444 break;
22445# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022446 case VAR_UNKNOWN:
22447 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022448 break;
22449 }
22450 return 0;
22451}
22452#endif
22453
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022455 * Get the lnum from the first argument.
22456 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022457 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 */
22459 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022460get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461{
Bram Moolenaar33570922005-01-25 22:26:29 +000022462 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 linenr_T lnum;
22464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022465 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 if (lnum == 0) /* no valid number, try using line() */
22467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022468 rettv.v_type = VAR_NUMBER;
22469 f_line(argvars, &rettv);
22470 lnum = rettv.vval.v_number;
22471 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 }
22473 return lnum;
22474}
22475
22476/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022477 * Get the lnum from the first argument.
22478 * Also accepts "$", then "buf" is used.
22479 * Returns 0 on error.
22480 */
22481 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022482get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022483{
22484 if (argvars[0].v_type == VAR_STRING
22485 && argvars[0].vval.v_string != NULL
22486 && argvars[0].vval.v_string[0] == '$'
22487 && buf != NULL)
22488 return buf->b_ml.ml_line_count;
22489 return get_tv_number_chk(&argvars[0], NULL);
22490}
22491
22492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 * Get the string value of a variable.
22494 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022495 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22496 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 * If the String variable has never been set, return an empty string.
22498 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022499 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22500 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022502 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022503get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504{
22505 static char_u mybuf[NUMBUFLEN];
22506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022507 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508}
22509
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022510 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022511get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022513 char_u *res = get_tv_string_buf_chk(varp, buf);
22514
22515 return res != NULL ? res : (char_u *)"";
22516}
22517
Bram Moolenaar7d647822014-04-05 21:28:56 +020022518/*
22519 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22520 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022521 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022522get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022523{
22524 static char_u mybuf[NUMBUFLEN];
22525
22526 return get_tv_string_buf_chk(varp, mybuf);
22527}
22528
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022530get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022531{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022532 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022534 case VAR_NUMBER:
22535 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22536 return buf;
22537 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022538 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022539 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022540 break;
22541 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022542 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022543 break;
22544 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022545 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022546 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022547 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022548#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022549 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022550 break;
22551#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022552 case VAR_STRING:
22553 if (varp->vval.v_string != NULL)
22554 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022555 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022556 case VAR_SPECIAL:
22557 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22558 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022559 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022560#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022561 {
22562 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022563 char *status;
22564
22565 if (job == NULL)
22566 return (char_u *)"no process";
22567 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022568 : job->jv_status == JOB_ENDED ? "dead"
22569 : "run";
22570# ifdef UNIX
22571 vim_snprintf((char *)buf, NUMBUFLEN,
22572 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022573# elif defined(WIN32)
22574 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022575 "process %ld %s",
22576 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022577 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022578# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022579 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022580 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22581# endif
22582 return buf;
22583 }
22584#endif
22585 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022586 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022587#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022588 {
22589 channel_T *channel = varp->vval.v_channel;
22590 char *status = channel_status(channel);
22591
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022592 if (channel == NULL)
22593 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22594 else
22595 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022596 "channel %d %s", channel->ch_id, status);
22597 return buf;
22598 }
22599#endif
22600 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022601 case VAR_UNKNOWN:
22602 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022603 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022605 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606}
22607
22608/*
22609 * Find variable "name" in the list of variables.
22610 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022611 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022612 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022613 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022615 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022616find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022619 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620
Bram Moolenaara7043832005-01-21 11:56:39 +000022621 ht = find_var_ht(name, &varname);
22622 if (htp != NULL)
22623 *htp = ht;
22624 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022626 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627}
22628
22629/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022630 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022631 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022633 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022634find_var_in_ht(
22635 hashtab_T *ht,
22636 int htname,
22637 char_u *varname,
22638 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022639{
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 hashitem_T *hi;
22641
22642 if (*varname == NUL)
22643 {
22644 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022645 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022646 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022647 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022648 case 'g': return &globvars_var;
22649 case 'v': return &vimvars_var;
22650 case 'b': return &curbuf->b_bufvar;
22651 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022652#ifdef FEAT_WINDOWS
22653 case 't': return &curtab->tp_winvar;
22654#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022655 case 'l': return current_funccal == NULL
22656 ? NULL : &current_funccal->l_vars_var;
22657 case 'a': return current_funccal == NULL
22658 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022659 }
22660 return NULL;
22661 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022662
22663 hi = hash_find(ht, varname);
22664 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022665 {
22666 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022667 * worked find the variable again. Don't auto-load a script if it was
22668 * loaded already, otherwise it would be loaded every time when
22669 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022670 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022671 {
22672 /* Note: script_autoload() may make "hi" invalid. It must either
22673 * be obtained again or not used. */
22674 if (!script_autoload(varname, FALSE) || aborting())
22675 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022676 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022677 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022678 if (HASHITEM_EMPTY(hi))
22679 return NULL;
22680 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022681 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022682}
22683
22684/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022685 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022686 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022687 * Set "varname" to the start of name without ':'.
22688 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022689 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022690find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022692 hashitem_T *hi;
22693
Bram Moolenaar73627d02015-08-11 15:46:09 +020022694 if (name[0] == NUL)
22695 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 if (name[1] != ':')
22697 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022698 /* The name must not start with a colon or #. */
22699 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 return NULL;
22701 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022702
22703 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022704 hi = hash_find(&compat_hashtab, name);
22705 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022706 return &compat_hashtab;
22707
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022709 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022710 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 }
22712 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022713 if (*name == 'g') /* global variable */
22714 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022715 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22716 */
22717 if (vim_strchr(name + 2, ':') != NULL
22718 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022719 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022721 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022723 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022724#ifdef FEAT_WINDOWS
22725 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022726 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022727#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022728 if (*name == 'v') /* v: variable */
22729 return &vimvarht;
22730 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022731 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022732 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022733 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 if (*name == 's' /* script variable */
22735 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22736 return &SCRIPT_VARS(current_SID);
22737 return NULL;
22738}
22739
22740/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022741 * Get function call environment based on bactrace debug level
22742 */
22743 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022744get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022745{
22746 int i;
22747 funccall_T *funccal;
22748 funccall_T *temp_funccal;
22749
22750 funccal = current_funccal;
22751 if (debug_backtrace_level > 0)
22752 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022753 for (i = 0; i < debug_backtrace_level; i++)
22754 {
22755 temp_funccal = funccal->caller;
22756 if (temp_funccal)
22757 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022758 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022759 /* backtrace level overflow. reset to max */
22760 debug_backtrace_level = i;
22761 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022762 }
22763 return funccal;
22764}
22765
22766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022768 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 * Returns NULL when it doesn't exist.
22770 */
22771 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022772get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773{
Bram Moolenaar33570922005-01-25 22:26:29 +000022774 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022776 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 if (v == NULL)
22778 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022779 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780}
22781
22782/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022783 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784 * sourcing this script and when executing functions defined in the script.
22785 */
22786 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022787new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788{
Bram Moolenaara7043832005-01-21 11:56:39 +000022789 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022790 hashtab_T *ht;
22791 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022792
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22794 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022795 /* Re-allocating ga_data means that an ht_array pointing to
22796 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022797 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022798 for (i = 1; i <= ga_scripts.ga_len; ++i)
22799 {
22800 ht = &SCRIPT_VARS(i);
22801 if (ht->ht_mask == HT_INIT_SIZE - 1)
22802 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022803 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022804 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022805 }
22806
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 while (ga_scripts.ga_len < id)
22808 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022809 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022810 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022811 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813 }
22814 }
22815}
22816
22817/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022818 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22819 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 */
22821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022822init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823{
Bram Moolenaar33570922005-01-25 22:26:29 +000022824 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022825 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022826 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022827 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022828 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022829 dict_var->di_tv.vval.v_dict = dict;
22830 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022831 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022832 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22833 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834}
22835
22836/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022837 * Unreference a dictionary initialized by init_var_dict().
22838 */
22839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022840unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022841{
22842 /* Now the dict needs to be freed if no one else is using it, go back to
22843 * normal reference counting. */
22844 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22845 dict_unref(dict);
22846}
22847
22848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022850 * Frees all allocated variables and the value they contain.
22851 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 */
22853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022854vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022855{
22856 vars_clear_ext(ht, TRUE);
22857}
22858
22859/*
22860 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22861 */
22862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022863vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864{
Bram Moolenaara7043832005-01-21 11:56:39 +000022865 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022866 hashitem_T *hi;
22867 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868
Bram Moolenaar33570922005-01-25 22:26:29 +000022869 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022870 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022871 for (hi = ht->ht_array; todo > 0; ++hi)
22872 {
22873 if (!HASHITEM_EMPTY(hi))
22874 {
22875 --todo;
22876
Bram Moolenaar33570922005-01-25 22:26:29 +000022877 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022878 * ht_array might change then. hash_clear() takes care of it
22879 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022880 v = HI2DI(hi);
22881 if (free_val)
22882 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022883 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022884 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022885 }
22886 }
22887 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022888 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889}
22890
Bram Moolenaara7043832005-01-21 11:56:39 +000022891/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022892 * Delete a variable from hashtab "ht" at item "hi".
22893 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022896delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897{
Bram Moolenaar33570922005-01-25 22:26:29 +000022898 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022899
22900 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022901 clear_tv(&di->di_tv);
22902 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903}
22904
22905/*
22906 * List the value of one internal variable.
22907 */
22908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022909list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022911 char_u *tofree;
22912 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022913 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022914
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022915 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022916 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022917 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022918 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919}
22920
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022922list_one_var_a(
22923 char_u *prefix,
22924 char_u *name,
22925 int type,
22926 char_u *string,
22927 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928{
Bram Moolenaar31859182007-08-14 20:41:13 +000022929 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22930 msg_start();
22931 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 if (name != NULL) /* "a:" vars don't have a name stored */
22933 msg_puts(name);
22934 msg_putchar(' ');
22935 msg_advance(22);
22936 if (type == VAR_NUMBER)
22937 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022938 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022939 msg_putchar('*');
22940 else if (type == VAR_LIST)
22941 {
22942 msg_putchar('[');
22943 if (*string == '[')
22944 ++string;
22945 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022946 else if (type == VAR_DICT)
22947 {
22948 msg_putchar('{');
22949 if (*string == '{')
22950 ++string;
22951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 else
22953 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022954
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022956
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022957 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022958 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022959 if (*first)
22960 {
22961 msg_clr_eos();
22962 *first = FALSE;
22963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964}
22965
22966/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022967 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 * If the variable already exists, the value is updated.
22969 * Otherwise the variable is created.
22970 */
22971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022972set_var(
22973 char_u *name,
22974 typval_T *tv,
22975 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976{
Bram Moolenaar33570922005-01-25 22:26:29 +000022977 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022979 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022981 ht = find_var_ht(name, &varname);
22982 if (ht == NULL || *varname == NUL)
22983 {
22984 EMSG2(_(e_illvar), name);
22985 return;
22986 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022987 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022988
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022989 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22990 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022991 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022992
Bram Moolenaar33570922005-01-25 22:26:29 +000022993 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022995 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022996 if (var_check_ro(v->di_flags, name, FALSE)
22997 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022998 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022999
23000 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023001 * Handle setting internal v: variables separately where needed to
23002 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023003 */
23004 if (ht == &vimvarht)
23005 {
23006 if (v->di_tv.v_type == VAR_STRING)
23007 {
23008 vim_free(v->di_tv.vval.v_string);
23009 if (copy || tv->v_type != VAR_STRING)
23010 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23011 else
23012 {
23013 /* Take over the string to avoid an extra alloc/free. */
23014 v->di_tv.vval.v_string = tv->vval.v_string;
23015 tv->vval.v_string = NULL;
23016 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023017 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023018 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023019 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023020 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023022 if (STRCMP(varname, "searchforward") == 0)
23023 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023024#ifdef FEAT_SEARCH_EXTRA
23025 else if (STRCMP(varname, "hlsearch") == 0)
23026 {
23027 no_hlsearch = !v->di_tv.vval.v_number;
23028 redraw_all_later(SOME_VALID);
23029 }
23030#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023031 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023032 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023033 else if (v->di_tv.v_type != tv->v_type)
23034 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023035 }
23036
23037 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 }
23039 else /* add a new variable */
23040 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023041 /* Can't add "v:" variable. */
23042 if (ht == &vimvarht)
23043 {
23044 EMSG2(_(e_illvar), name);
23045 return;
23046 }
23047
Bram Moolenaar92124a32005-06-17 22:03:40 +000023048 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023049 if (!valid_varname(varname))
23050 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023051
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023052 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23053 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023054 if (v == NULL)
23055 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023056 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023057 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023059 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 return;
23061 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023062 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023064
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023065 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023066 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023067 else
23068 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023069 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023070 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023071 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073}
23074
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023075/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023076 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023077 * Also give an error message.
23078 */
23079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023080var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023081{
23082 if (flags & DI_FLAGS_RO)
23083 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023084 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023085 return TRUE;
23086 }
23087 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23088 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023089 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023090 return TRUE;
23091 }
23092 return FALSE;
23093}
23094
23095/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023096 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23097 * Also give an error message.
23098 */
23099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023100var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023101{
23102 if (flags & DI_FLAGS_FIX)
23103 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023104 EMSG2(_("E795: Cannot delete variable %s"),
23105 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023106 return TRUE;
23107 }
23108 return FALSE;
23109}
23110
23111/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023112 * Check if a funcref is assigned to a valid variable name.
23113 * Return TRUE and give an error if not.
23114 */
23115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023116var_check_func_name(
23117 char_u *name, /* points to start of variable name */
23118 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023119{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023120 /* Allow for w: b: s: and t:. */
23121 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023122 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23123 ? name[2] : name[0]))
23124 {
23125 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23126 name);
23127 return TRUE;
23128 }
23129 /* Don't allow hiding a function. When "v" is not NULL we might be
23130 * assigning another function to the same var, the type is checked
23131 * below. */
23132 if (new_var && function_exists(name))
23133 {
23134 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23135 name);
23136 return TRUE;
23137 }
23138 return FALSE;
23139}
23140
23141/*
23142 * Check if a variable name is valid.
23143 * Return FALSE and give an error if not.
23144 */
23145 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023146valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023147{
23148 char_u *p;
23149
23150 for (p = varname; *p != NUL; ++p)
23151 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23152 && *p != AUTOLOAD_CHAR)
23153 {
23154 EMSG2(_(e_illvar), varname);
23155 return FALSE;
23156 }
23157 return TRUE;
23158}
23159
23160/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023161 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023162 * Also give an error message, using "name" or _("name") when use_gettext is
23163 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023164 */
23165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023166tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023167{
23168 if (lock & VAR_LOCKED)
23169 {
23170 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023171 name == NULL ? (char_u *)_("Unknown")
23172 : use_gettext ? (char_u *)_(name)
23173 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023174 return TRUE;
23175 }
23176 if (lock & VAR_FIXED)
23177 {
23178 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023179 name == NULL ? (char_u *)_("Unknown")
23180 : use_gettext ? (char_u *)_(name)
23181 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023182 return TRUE;
23183 }
23184 return FALSE;
23185}
23186
23187/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023188 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023189 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023190 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023191 * It is OK for "from" and "to" to point to the same item. This is used to
23192 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023193 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023195copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023197 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023198 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023199 switch (from->v_type)
23200 {
23201 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023202 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023203 to->vval.v_number = from->vval.v_number;
23204 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023205 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023206#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023207 to->vval.v_float = from->vval.v_float;
23208 break;
23209#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023210 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023211#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023212 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023213 if (to->vval.v_job != NULL)
23214 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023215 break;
23216#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023217 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023218#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023219 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023220 if (to->vval.v_channel != NULL)
23221 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023222 break;
23223#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023224 case VAR_STRING:
23225 case VAR_FUNC:
23226 if (from->vval.v_string == NULL)
23227 to->vval.v_string = NULL;
23228 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023229 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023230 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023231 if (from->v_type == VAR_FUNC)
23232 func_ref(to->vval.v_string);
23233 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023234 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023235 case VAR_PARTIAL:
23236 if (from->vval.v_partial == NULL)
23237 to->vval.v_partial = NULL;
23238 else
23239 {
23240 to->vval.v_partial = from->vval.v_partial;
23241 ++to->vval.v_partial->pt_refcount;
23242 }
23243 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023244 case VAR_LIST:
23245 if (from->vval.v_list == NULL)
23246 to->vval.v_list = NULL;
23247 else
23248 {
23249 to->vval.v_list = from->vval.v_list;
23250 ++to->vval.v_list->lv_refcount;
23251 }
23252 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023253 case VAR_DICT:
23254 if (from->vval.v_dict == NULL)
23255 to->vval.v_dict = NULL;
23256 else
23257 {
23258 to->vval.v_dict = from->vval.v_dict;
23259 ++to->vval.v_dict->dv_refcount;
23260 }
23261 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023262 case VAR_UNKNOWN:
23263 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023264 break;
23265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266}
23267
23268/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023269 * Make a copy of an item.
23270 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023271 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23272 * reference to an already copied list/dict can be used.
23273 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023274 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023275 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023276item_copy(
23277 typval_T *from,
23278 typval_T *to,
23279 int deep,
23280 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023281{
23282 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023283 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023284
Bram Moolenaar33570922005-01-25 22:26:29 +000023285 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023286 {
23287 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023288 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023289 }
23290 ++recurse;
23291
23292 switch (from->v_type)
23293 {
23294 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023295 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023296 case VAR_STRING:
23297 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023298 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023299 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023300 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023301 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023302 copy_tv(from, to);
23303 break;
23304 case VAR_LIST:
23305 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023306 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023307 if (from->vval.v_list == NULL)
23308 to->vval.v_list = NULL;
23309 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23310 {
23311 /* use the copy made earlier */
23312 to->vval.v_list = from->vval.v_list->lv_copylist;
23313 ++to->vval.v_list->lv_refcount;
23314 }
23315 else
23316 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23317 if (to->vval.v_list == NULL)
23318 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023319 break;
23320 case VAR_DICT:
23321 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023322 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023323 if (from->vval.v_dict == NULL)
23324 to->vval.v_dict = NULL;
23325 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23326 {
23327 /* use the copy made earlier */
23328 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23329 ++to->vval.v_dict->dv_refcount;
23330 }
23331 else
23332 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23333 if (to->vval.v_dict == NULL)
23334 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023335 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023336 case VAR_UNKNOWN:
23337 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023338 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023339 }
23340 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023341 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023342}
23343
23344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345 * ":echo expr1 ..." print each argument separated with a space, add a
23346 * newline at the end.
23347 * ":echon expr1 ..." print each argument plain.
23348 */
23349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023350ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351{
23352 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023353 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023354 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 char_u *p;
23356 int needclr = TRUE;
23357 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359
23360 if (eap->skip)
23361 ++emsg_skip;
23362 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23363 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023364 /* If eval1() causes an error message the text from the command may
23365 * still need to be cleared. E.g., "echo 22,44". */
23366 need_clr_eos = needclr;
23367
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023369 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 {
23371 /*
23372 * Report the invalid expression unless the expression evaluation
23373 * has been cancelled due to an aborting error, an interrupt, or an
23374 * exception.
23375 */
23376 if (!aborting())
23377 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023378 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 break;
23380 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023381 need_clr_eos = FALSE;
23382
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 if (!eap->skip)
23384 {
23385 if (atstart)
23386 {
23387 atstart = FALSE;
23388 /* Call msg_start() after eval1(), evaluating the expression
23389 * may cause a message to appear. */
23390 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023391 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023392 /* Mark the saved text as finishing the line, so that what
23393 * follows is displayed on a new line when scrolling back
23394 * at the more prompt. */
23395 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 }
23399 else if (eap->cmdidx == CMD_echo)
23400 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023401 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023402 if (p != NULL)
23403 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023405 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023407 if (*p != TAB && needclr)
23408 {
23409 /* remove any text still there from the command */
23410 msg_clr_eos();
23411 needclr = FALSE;
23412 }
23413 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 }
23415 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023416 {
23417#ifdef FEAT_MBYTE
23418 if (has_mbyte)
23419 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023420 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023421
23422 (void)msg_outtrans_len_attr(p, i, echo_attr);
23423 p += i - 1;
23424 }
23425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023427 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023430 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433 arg = skipwhite(arg);
23434 }
23435 eap->nextcmd = check_nextcmd(arg);
23436
23437 if (eap->skip)
23438 --emsg_skip;
23439 else
23440 {
23441 /* remove text that may still be there from the command */
23442 if (needclr)
23443 msg_clr_eos();
23444 if (eap->cmdidx == CMD_echo)
23445 msg_end();
23446 }
23447}
23448
23449/*
23450 * ":echohl {name}".
23451 */
23452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023453ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454{
23455 int id;
23456
23457 id = syn_name2id(eap->arg);
23458 if (id == 0)
23459 echo_attr = 0;
23460 else
23461 echo_attr = syn_id2attr(id);
23462}
23463
23464/*
23465 * ":execute expr1 ..." execute the result of an expression.
23466 * ":echomsg expr1 ..." Print a message
23467 * ":echoerr expr1 ..." Print an error
23468 * Each gets spaces around each argument and a newline at the end for
23469 * echo commands
23470 */
23471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023472ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473{
23474 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023475 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 int ret = OK;
23477 char_u *p;
23478 garray_T ga;
23479 int len;
23480 int save_did_emsg;
23481
23482 ga_init2(&ga, 1, 80);
23483
23484 if (eap->skip)
23485 ++emsg_skip;
23486 while (*arg != NUL && *arg != '|' && *arg != '\n')
23487 {
23488 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023489 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 {
23491 /*
23492 * Report the invalid expression unless the expression evaluation
23493 * has been cancelled due to an aborting error, an interrupt, or an
23494 * exception.
23495 */
23496 if (!aborting())
23497 EMSG2(_(e_invexpr2), p);
23498 ret = FAIL;
23499 break;
23500 }
23501
23502 if (!eap->skip)
23503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023504 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023505 len = (int)STRLEN(p);
23506 if (ga_grow(&ga, len + 2) == FAIL)
23507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023508 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 ret = FAIL;
23510 break;
23511 }
23512 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023515 ga.ga_len += len;
23516 }
23517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023518 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 arg = skipwhite(arg);
23520 }
23521
23522 if (ret != FAIL && ga.ga_data != NULL)
23523 {
23524 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023527 out_flush();
23528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 else if (eap->cmdidx == CMD_echoerr)
23530 {
23531 /* We don't want to abort following commands, restore did_emsg. */
23532 save_did_emsg = did_emsg;
23533 EMSG((char_u *)ga.ga_data);
23534 if (!force_abort)
23535 did_emsg = save_did_emsg;
23536 }
23537 else if (eap->cmdidx == CMD_execute)
23538 do_cmdline((char_u *)ga.ga_data,
23539 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23540 }
23541
23542 ga_clear(&ga);
23543
23544 if (eap->skip)
23545 --emsg_skip;
23546
23547 eap->nextcmd = check_nextcmd(arg);
23548}
23549
23550/*
23551 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23552 * "arg" points to the "&" or '+' when called, to "option" when returning.
23553 * Returns NULL when no option name found. Otherwise pointer to the char
23554 * after the option name.
23555 */
23556 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023557find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558{
23559 char_u *p = *arg;
23560
23561 ++p;
23562 if (*p == 'g' && p[1] == ':')
23563 {
23564 *opt_flags = OPT_GLOBAL;
23565 p += 2;
23566 }
23567 else if (*p == 'l' && p[1] == ':')
23568 {
23569 *opt_flags = OPT_LOCAL;
23570 p += 2;
23571 }
23572 else
23573 *opt_flags = 0;
23574
23575 if (!ASCII_ISALPHA(*p))
23576 return NULL;
23577 *arg = p;
23578
23579 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23580 p += 4; /* termcap option */
23581 else
23582 while (ASCII_ISALPHA(*p))
23583 ++p;
23584 return p;
23585}
23586
23587/*
23588 * ":function"
23589 */
23590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023591ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592{
23593 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023594 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 int j;
23596 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023598 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 char_u *name = NULL;
23600 char_u *p;
23601 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023602 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 garray_T newargs;
23604 garray_T newlines;
23605 int varargs = FALSE;
23606 int mustend = FALSE;
23607 int flags = 0;
23608 ufunc_T *fp;
23609 int indent;
23610 int nesting;
23611 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023612 dictitem_T *v;
23613 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023614 static int func_nr = 0; /* number for nameless function */
23615 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023616 hashtab_T *ht;
23617 int todo;
23618 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023619 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620
23621 /*
23622 * ":function" without argument: list functions.
23623 */
23624 if (ends_excmd(*eap->arg))
23625 {
23626 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023627 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023628 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023629 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023630 {
23631 if (!HASHITEM_EMPTY(hi))
23632 {
23633 --todo;
23634 fp = HI2UF(hi);
23635 if (!isdigit(*fp->uf_name))
23636 list_func_head(fp, FALSE);
23637 }
23638 }
23639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 eap->nextcmd = check_nextcmd(eap->arg);
23641 return;
23642 }
23643
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023644 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023645 * ":function /pat": list functions matching pattern.
23646 */
23647 if (*eap->arg == '/')
23648 {
23649 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23650 if (!eap->skip)
23651 {
23652 regmatch_T regmatch;
23653
23654 c = *p;
23655 *p = NUL;
23656 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23657 *p = c;
23658 if (regmatch.regprog != NULL)
23659 {
23660 regmatch.rm_ic = p_ic;
23661
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023662 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023663 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23664 {
23665 if (!HASHITEM_EMPTY(hi))
23666 {
23667 --todo;
23668 fp = HI2UF(hi);
23669 if (!isdigit(*fp->uf_name)
23670 && vim_regexec(&regmatch, fp->uf_name, 0))
23671 list_func_head(fp, FALSE);
23672 }
23673 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023674 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023675 }
23676 }
23677 if (*p == '/')
23678 ++p;
23679 eap->nextcmd = check_nextcmd(p);
23680 return;
23681 }
23682
23683 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023684 * Get the function name. There are these situations:
23685 * func normal function name
23686 * "name" == func, "fudi.fd_dict" == NULL
23687 * dict.func new dictionary entry
23688 * "name" == NULL, "fudi.fd_dict" set,
23689 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23690 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023691 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023692 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23693 * dict.func existing dict entry that's not a Funcref
23694 * "name" == NULL, "fudi.fd_dict" set,
23695 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023696 * s:func script-local function name
23697 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023700 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023701 paren = (vim_strchr(p, '(') != NULL);
23702 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 {
23704 /*
23705 * Return on an invalid expression in braces, unless the expression
23706 * evaluation has been cancelled due to an aborting error, an
23707 * interrupt, or an exception.
23708 */
23709 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023710 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023711 if (!eap->skip && fudi.fd_newkey != NULL)
23712 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023713 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 else
23717 eap->skip = TRUE;
23718 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023719
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 /* An error in a function call during evaluation of an expression in magic
23721 * braces should not cause the function not to be defined. */
23722 saved_did_emsg = did_emsg;
23723 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023724
23725 /*
23726 * ":function func" with only function name: list function.
23727 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023728 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 {
23730 if (!ends_excmd(*skipwhite(p)))
23731 {
23732 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023733 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 }
23735 eap->nextcmd = check_nextcmd(p);
23736 if (eap->nextcmd != NULL)
23737 *p = NUL;
23738 if (!eap->skip && !got_int)
23739 {
23740 fp = find_func(name);
23741 if (fp != NULL)
23742 {
23743 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023744 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023746 if (FUNCLINE(fp, j) == NULL)
23747 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 msg_putchar('\n');
23749 msg_outnum((long)(j + 1));
23750 if (j < 9)
23751 msg_putchar(' ');
23752 if (j < 99)
23753 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023754 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 out_flush(); /* show a line at a time */
23756 ui_breakcheck();
23757 }
23758 if (!got_int)
23759 {
23760 msg_putchar('\n');
23761 msg_puts((char_u *)" endfunction");
23762 }
23763 }
23764 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023765 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023767 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 }
23769
23770 /*
23771 * ":function name(arg1, arg2)" Define function.
23772 */
23773 p = skipwhite(p);
23774 if (*p != '(')
23775 {
23776 if (!eap->skip)
23777 {
23778 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023779 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780 }
23781 /* attempt to continue by skipping some text */
23782 if (vim_strchr(p, '(') != NULL)
23783 p = vim_strchr(p, '(');
23784 }
23785 p = skipwhite(p + 1);
23786
23787 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23788 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23789
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023790 if (!eap->skip)
23791 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023792 /* Check the name of the function. Unless it's a dictionary function
23793 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023794 if (name != NULL)
23795 arg = name;
23796 else
23797 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023798 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023799 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23800 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023801 {
23802 if (*arg == K_SPECIAL)
23803 j = 3;
23804 else
23805 j = 0;
23806 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23807 : eval_isnamec(arg[j])))
23808 ++j;
23809 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023810 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023811 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023812 /* Disallow using the g: dict. */
23813 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23814 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023815 }
23816
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 /*
23818 * Isolate the arguments: "arg1, arg2, ...)"
23819 */
23820 while (*p != ')')
23821 {
23822 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23823 {
23824 varargs = TRUE;
23825 p += 3;
23826 mustend = TRUE;
23827 }
23828 else
23829 {
23830 arg = p;
23831 while (ASCII_ISALNUM(*p) || *p == '_')
23832 ++p;
23833 if (arg == p || isdigit(*arg)
23834 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23835 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23836 {
23837 if (!eap->skip)
23838 EMSG2(_("E125: Illegal argument: %s"), arg);
23839 break;
23840 }
23841 if (ga_grow(&newargs, 1) == FAIL)
23842 goto erret;
23843 c = *p;
23844 *p = NUL;
23845 arg = vim_strsave(arg);
23846 if (arg == NULL)
23847 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023848
23849 /* Check for duplicate argument name. */
23850 for (i = 0; i < newargs.ga_len; ++i)
23851 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23852 {
23853 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023854 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023855 goto erret;
23856 }
23857
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23859 *p = c;
23860 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861 if (*p == ',')
23862 ++p;
23863 else
23864 mustend = TRUE;
23865 }
23866 p = skipwhite(p);
23867 if (mustend && *p != ')')
23868 {
23869 if (!eap->skip)
23870 EMSG2(_(e_invarg2), eap->arg);
23871 break;
23872 }
23873 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023874 if (*p != ')')
23875 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 ++p; /* skip the ')' */
23877
Bram Moolenaare9a41262005-01-15 22:18:47 +000023878 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 for (;;)
23880 {
23881 p = skipwhite(p);
23882 if (STRNCMP(p, "range", 5) == 0)
23883 {
23884 flags |= FC_RANGE;
23885 p += 5;
23886 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023887 else if (STRNCMP(p, "dict", 4) == 0)
23888 {
23889 flags |= FC_DICT;
23890 p += 4;
23891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 else if (STRNCMP(p, "abort", 5) == 0)
23893 {
23894 flags |= FC_ABORT;
23895 p += 5;
23896 }
23897 else
23898 break;
23899 }
23900
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023901 /* When there is a line break use what follows for the function body.
23902 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23903 if (*p == '\n')
23904 line_arg = p + 1;
23905 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 EMSG(_(e_trailing));
23907
23908 /*
23909 * Read the body of the function, until ":endfunction" is found.
23910 */
23911 if (KeyTyped)
23912 {
23913 /* Check if the function already exists, don't let the user type the
23914 * whole function before telling him it doesn't work! For a script we
23915 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023916 if (!eap->skip && !eap->forceit)
23917 {
23918 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23919 EMSG(_(e_funcdict));
23920 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023921 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023924 if (!eap->skip && did_emsg)
23925 goto erret;
23926
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927 msg_putchar('\n'); /* don't overwrite the function name */
23928 cmdline_row = msg_row;
23929 }
23930
23931 indent = 2;
23932 nesting = 0;
23933 for (;;)
23934 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023935 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023936 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023937 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023938 saved_wait_return = FALSE;
23939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023941 sourcing_lnum_off = sourcing_lnum;
23942
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023943 if (line_arg != NULL)
23944 {
23945 /* Use eap->arg, split up in parts by line breaks. */
23946 theline = line_arg;
23947 p = vim_strchr(theline, '\n');
23948 if (p == NULL)
23949 line_arg += STRLEN(line_arg);
23950 else
23951 {
23952 *p = NUL;
23953 line_arg = p + 1;
23954 }
23955 }
23956 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023957 theline = getcmdline(':', 0L, indent);
23958 else
23959 theline = eap->getline(':', eap->cookie, indent);
23960 if (KeyTyped)
23961 lines_left = Rows - 1;
23962 if (theline == NULL)
23963 {
23964 EMSG(_("E126: Missing :endfunction"));
23965 goto erret;
23966 }
23967
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023968 /* Detect line continuation: sourcing_lnum increased more than one. */
23969 if (sourcing_lnum > sourcing_lnum_off + 1)
23970 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23971 else
23972 sourcing_lnum_off = 0;
23973
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 if (skip_until != NULL)
23975 {
23976 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23977 * don't check for ":endfunc". */
23978 if (STRCMP(theline, skip_until) == 0)
23979 {
23980 vim_free(skip_until);
23981 skip_until = NULL;
23982 }
23983 }
23984 else
23985 {
23986 /* skip ':' and blanks*/
23987 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23988 ;
23989
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023990 /* Check for "endfunction". */
23991 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023993 if (line_arg == NULL)
23994 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 break;
23996 }
23997
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023998 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 * at "end". */
24000 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24001 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024002 else if (STRNCMP(p, "if", 2) == 0
24003 || STRNCMP(p, "wh", 2) == 0
24004 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 || STRNCMP(p, "try", 3) == 0)
24006 indent += 2;
24007
24008 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024009 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024011 if (*p == '!')
24012 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024014 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024015 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024017 ++nesting;
24018 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 }
24020 }
24021
24022 /* Check for ":append" or ":insert". */
24023 p = skip_range(p, NULL);
24024 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24025 || (p[0] == 'i'
24026 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24027 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24028 skip_until = vim_strsave((char_u *)".");
24029
24030 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24031 arg = skipwhite(skiptowhite(p));
24032 if (arg[0] == '<' && arg[1] =='<'
24033 && ((p[0] == 'p' && p[1] == 'y'
24034 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24035 || (p[0] == 'p' && p[1] == 'e'
24036 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24037 || (p[0] == 't' && p[1] == 'c'
24038 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024039 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24040 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24042 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024043 || (p[0] == 'm' && p[1] == 'z'
24044 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024045 ))
24046 {
24047 /* ":python <<" continues until a dot, like ":append" */
24048 p = skipwhite(arg + 2);
24049 if (*p == NUL)
24050 skip_until = vim_strsave((char_u *)".");
24051 else
24052 skip_until = vim_strsave(p);
24053 }
24054 }
24055
24056 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024057 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024058 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024059 if (line_arg == NULL)
24060 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024062 }
24063
24064 /* Copy the line to newly allocated memory. get_one_sourceline()
24065 * allocates 250 bytes per line, this saves 80% on average. The cost
24066 * is an extra alloc/free. */
24067 p = vim_strsave(theline);
24068 if (p != NULL)
24069 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024070 if (line_arg == NULL)
24071 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024072 theline = p;
24073 }
24074
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024075 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24076
24077 /* Add NULL lines for continuation lines, so that the line count is
24078 * equal to the index in the growarray. */
24079 while (sourcing_lnum_off-- > 0)
24080 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024081
24082 /* Check for end of eap->arg. */
24083 if (line_arg != NULL && *line_arg == NUL)
24084 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024085 }
24086
24087 /* Don't define the function when skipping commands or when an error was
24088 * detected. */
24089 if (eap->skip || did_emsg)
24090 goto erret;
24091
24092 /*
24093 * If there are no errors, add the function
24094 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024095 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024096 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024097 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024098 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024099 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024100 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024101 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024102 goto erret;
24103 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024104
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024105 fp = find_func(name);
24106 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024108 if (!eap->forceit)
24109 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024110 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024111 goto erret;
24112 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024113 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024114 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024115 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024116 name);
24117 goto erret;
24118 }
24119 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024120 ga_clear_strings(&(fp->uf_args));
24121 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024122 vim_free(name);
24123 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 }
24126 else
24127 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024128 char numbuf[20];
24129
24130 fp = NULL;
24131 if (fudi.fd_newkey == NULL && !eap->forceit)
24132 {
24133 EMSG(_(e_funcdict));
24134 goto erret;
24135 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024136 if (fudi.fd_di == NULL)
24137 {
24138 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024139 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024140 goto erret;
24141 }
24142 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024143 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024144 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024145
24146 /* Give the function a sequential number. Can only be used with a
24147 * Funcref! */
24148 vim_free(name);
24149 sprintf(numbuf, "%d", ++func_nr);
24150 name = vim_strsave((char_u *)numbuf);
24151 if (name == NULL)
24152 goto erret;
24153 }
24154
24155 if (fp == NULL)
24156 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024157 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024158 {
24159 int slen, plen;
24160 char_u *scriptname;
24161
24162 /* Check that the autoload name matches the script name. */
24163 j = FAIL;
24164 if (sourcing_name != NULL)
24165 {
24166 scriptname = autoload_name(name);
24167 if (scriptname != NULL)
24168 {
24169 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024170 plen = (int)STRLEN(p);
24171 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024172 if (slen > plen && fnamecmp(p,
24173 sourcing_name + slen - plen) == 0)
24174 j = OK;
24175 vim_free(scriptname);
24176 }
24177 }
24178 if (j == FAIL)
24179 {
24180 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24181 goto erret;
24182 }
24183 }
24184
24185 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 if (fp == NULL)
24187 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024188
24189 if (fudi.fd_dict != NULL)
24190 {
24191 if (fudi.fd_di == NULL)
24192 {
24193 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024194 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024195 if (fudi.fd_di == NULL)
24196 {
24197 vim_free(fp);
24198 goto erret;
24199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024200 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24201 {
24202 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024203 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024204 goto erret;
24205 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024206 }
24207 else
24208 /* overwrite existing dict entry */
24209 clear_tv(&fudi.fd_di->di_tv);
24210 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024211 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024212 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024213 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024214
24215 /* behave like "dict" was used */
24216 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024217 }
24218
Bram Moolenaar071d4272004-06-13 20:20:40 +000024219 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024220 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024221 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24222 {
24223 vim_free(fp);
24224 goto erret;
24225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024227 fp->uf_args = newargs;
24228 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024229#ifdef FEAT_PROFILE
24230 fp->uf_tml_count = NULL;
24231 fp->uf_tml_total = NULL;
24232 fp->uf_tml_self = NULL;
24233 fp->uf_profiling = FALSE;
24234 if (prof_def_func())
24235 func_do_profile(fp);
24236#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024237 fp->uf_varargs = varargs;
24238 fp->uf_flags = flags;
24239 fp->uf_calls = 0;
24240 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024241 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242
24243erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 ga_clear_strings(&newargs);
24245 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024246ret_free:
24247 vim_free(skip_until);
24248 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024251 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252}
24253
24254/*
24255 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024256 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024258 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024259 * TFN_INT: internal function name OK
24260 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024261 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 * Advances "pp" to just after the function name (if no error).
24263 */
24264 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024265trans_function_name(
24266 char_u **pp,
24267 int skip, /* only find the end, don't evaluate */
24268 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024269 funcdict_T *fdp, /* return: info about dictionary used */
24270 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024272 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 char_u *start;
24274 char_u *end;
24275 int lead;
24276 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024278 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024279
24280 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024281 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024283
24284 /* Check for hard coded <SNR>: already translated function ID (from a user
24285 * command). */
24286 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24287 && (*pp)[2] == (int)KE_SNR)
24288 {
24289 *pp += 3;
24290 len = get_id_len(pp) + 3;
24291 return vim_strnsave(start, len);
24292 }
24293
24294 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24295 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024297 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024299
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024300 /* Note that TFN_ flags use the same values as GLV_ flags. */
24301 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024302 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024303 if (end == start)
24304 {
24305 if (!skip)
24306 EMSG(_("E129: Function name required"));
24307 goto theend;
24308 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024309 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024310 {
24311 /*
24312 * Report an invalid expression in braces, unless the expression
24313 * evaluation has been cancelled due to an aborting error, an
24314 * interrupt, or an exception.
24315 */
24316 if (!aborting())
24317 {
24318 if (end != NULL)
24319 EMSG2(_(e_invarg2), start);
24320 }
24321 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024322 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024323 goto theend;
24324 }
24325
24326 if (lv.ll_tv != NULL)
24327 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024328 if (fdp != NULL)
24329 {
24330 fdp->fd_dict = lv.ll_dict;
24331 fdp->fd_newkey = lv.ll_newkey;
24332 lv.ll_newkey = NULL;
24333 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024334 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024335 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24336 {
24337 name = vim_strsave(lv.ll_tv->vval.v_string);
24338 *pp = end;
24339 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024340 else if (lv.ll_tv->v_type == VAR_PARTIAL
24341 && lv.ll_tv->vval.v_partial != NULL)
24342 {
24343 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24344 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024345 if (partial != NULL)
24346 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024347 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024348 else
24349 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024350 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24351 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024352 EMSG(_(e_funcref));
24353 else
24354 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024355 name = NULL;
24356 }
24357 goto theend;
24358 }
24359
24360 if (lv.ll_name == NULL)
24361 {
24362 /* Error found, but continue after the function name. */
24363 *pp = end;
24364 goto theend;
24365 }
24366
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024367 /* Check if the name is a Funcref. If so, use the value. */
24368 if (lv.ll_exp_name != NULL)
24369 {
24370 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024371 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024372 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024373 if (name == lv.ll_exp_name)
24374 name = NULL;
24375 }
24376 else
24377 {
24378 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024379 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024380 if (name == *pp)
24381 name = NULL;
24382 }
24383 if (name != NULL)
24384 {
24385 name = vim_strsave(name);
24386 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024387 if (STRNCMP(name, "<SNR>", 5) == 0)
24388 {
24389 /* Change "<SNR>" to the byte sequence. */
24390 name[0] = K_SPECIAL;
24391 name[1] = KS_EXTRA;
24392 name[2] = (int)KE_SNR;
24393 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24394 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024395 goto theend;
24396 }
24397
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024398 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024399 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024400 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024401 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24402 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24403 {
24404 /* When there was "s:" already or the name expanded to get a
24405 * leading "s:" then remove it. */
24406 lv.ll_name += 2;
24407 len -= 2;
24408 lead = 2;
24409 }
24410 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024411 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024412 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024413 /* skip over "s:" and "g:" */
24414 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024415 lv.ll_name += 2;
24416 len = (int)(end - lv.ll_name);
24417 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024418
24419 /*
24420 * Copy the function name to allocated memory.
24421 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24422 * Accept <SNR>123_name() outside a script.
24423 */
24424 if (skip)
24425 lead = 0; /* do nothing */
24426 else if (lead > 0)
24427 {
24428 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024429 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24430 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024431 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024432 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024433 if (current_SID <= 0)
24434 {
24435 EMSG(_(e_usingsid));
24436 goto theend;
24437 }
24438 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24439 lead += (int)STRLEN(sid_buf);
24440 }
24441 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024442 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024443 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024444 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024445 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024446 goto theend;
24447 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024448 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024449 {
24450 char_u *cp = vim_strchr(lv.ll_name, ':');
24451
24452 if (cp != NULL && cp < end)
24453 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024454 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024455 goto theend;
24456 }
24457 }
24458
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024459 name = alloc((unsigned)(len + lead + 1));
24460 if (name != NULL)
24461 {
24462 if (lead > 0)
24463 {
24464 name[0] = K_SPECIAL;
24465 name[1] = KS_EXTRA;
24466 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024467 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024468 STRCPY(name + 3, sid_buf);
24469 }
24470 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024471 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024472 }
24473 *pp = end;
24474
24475theend:
24476 clear_lval(&lv);
24477 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024478}
24479
24480/*
24481 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24482 * Return 2 if "p" starts with "s:".
24483 * Return 0 otherwise.
24484 */
24485 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024486eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024488 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24489 * the standard library function. */
24490 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24491 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492 return 5;
24493 if (p[0] == 's' && p[1] == ':')
24494 return 2;
24495 return 0;
24496}
24497
24498/*
24499 * Return TRUE if "p" starts with "<SID>" or "s:".
24500 * Only works if eval_fname_script() returned non-zero for "p"!
24501 */
24502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024503eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504{
24505 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24506}
24507
24508/*
24509 * List the head of the function: "name(arg1, arg2)".
24510 */
24511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024512list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513{
24514 int j;
24515
24516 msg_start();
24517 if (indent)
24518 MSG_PUTS(" ");
24519 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024520 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521 {
24522 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024523 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024524 }
24525 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024526 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024528 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529 {
24530 if (j)
24531 MSG_PUTS(", ");
24532 msg_puts(FUNCARG(fp, j));
24533 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024534 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 {
24536 if (j)
24537 MSG_PUTS(", ");
24538 MSG_PUTS("...");
24539 }
24540 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024541 if (fp->uf_flags & FC_ABORT)
24542 MSG_PUTS(" abort");
24543 if (fp->uf_flags & FC_RANGE)
24544 MSG_PUTS(" range");
24545 if (fp->uf_flags & FC_DICT)
24546 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024547 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024548 if (p_verbose > 0)
24549 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550}
24551
24552/*
24553 * Find a function by name, return pointer to it in ufuncs.
24554 * Return NULL for unknown function.
24555 */
24556 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024557find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024559 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024561 hi = hash_find(&func_hashtab, name);
24562 if (!HASHITEM_EMPTY(hi))
24563 return HI2UF(hi);
24564 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024565}
24566
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024567#if defined(EXITFREE) || defined(PROTO)
24568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024569free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024570{
24571 hashitem_T *hi;
24572
24573 /* Need to start all over every time, because func_free() may change the
24574 * hash table. */
24575 while (func_hashtab.ht_used > 0)
24576 for (hi = func_hashtab.ht_array; ; ++hi)
24577 if (!HASHITEM_EMPTY(hi))
24578 {
24579 func_free(HI2UF(hi));
24580 break;
24581 }
24582}
24583#endif
24584
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024585 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024586translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024587{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024588 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024589 return find_internal_func(name) >= 0;
24590 return find_func(name) != NULL;
24591}
24592
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024593/*
24594 * Return TRUE if a function "name" exists.
24595 */
24596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024597function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024598{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024599 char_u *nm = name;
24600 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024601 int n = FALSE;
24602
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024603 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024604 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024605 nm = skipwhite(nm);
24606
24607 /* Only accept "funcname", "funcname ", "funcname (..." and
24608 * "funcname(...", not "funcname!...". */
24609 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024610 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024611 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024612 return n;
24613}
24614
Bram Moolenaara1544c02013-05-30 12:35:52 +020024615 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024616get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024617{
24618 char_u *nm = name;
24619 char_u *p;
24620
Bram Moolenaar65639032016-03-16 21:40:30 +010024621 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024622
24623 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024624 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024625 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024626
Bram Moolenaara1544c02013-05-30 12:35:52 +020024627 vim_free(p);
24628 return NULL;
24629}
24630
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024631/*
24632 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024633 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24634 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024635 */
24636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024637builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024638{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024639 char_u *p;
24640
24641 if (!ASCII_ISLOWER(name[0]))
24642 return FALSE;
24643 p = vim_strchr(name, AUTOLOAD_CHAR);
24644 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024645}
24646
Bram Moolenaar05159a02005-02-26 23:04:13 +000024647#if defined(FEAT_PROFILE) || defined(PROTO)
24648/*
24649 * Start profiling function "fp".
24650 */
24651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024652func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024653{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024654 int len = fp->uf_lines.ga_len;
24655
24656 if (len == 0)
24657 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024658 fp->uf_tm_count = 0;
24659 profile_zero(&fp->uf_tm_self);
24660 profile_zero(&fp->uf_tm_total);
24661 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024662 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024663 if (fp->uf_tml_total == NULL)
24664 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024665 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024666 if (fp->uf_tml_self == NULL)
24667 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024668 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024669 fp->uf_tml_idx = -1;
24670 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24671 || fp->uf_tml_self == NULL)
24672 return; /* out of memory */
24673
24674 fp->uf_profiling = TRUE;
24675}
24676
24677/*
24678 * Dump the profiling results for all functions in file "fd".
24679 */
24680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024681func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024682{
24683 hashitem_T *hi;
24684 int todo;
24685 ufunc_T *fp;
24686 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024687 ufunc_T **sorttab;
24688 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024689
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024690 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024691 if (todo == 0)
24692 return; /* nothing to dump */
24693
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024694 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024695
Bram Moolenaar05159a02005-02-26 23:04:13 +000024696 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24697 {
24698 if (!HASHITEM_EMPTY(hi))
24699 {
24700 --todo;
24701 fp = HI2UF(hi);
24702 if (fp->uf_profiling)
24703 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024704 if (sorttab != NULL)
24705 sorttab[st_len++] = fp;
24706
Bram Moolenaar05159a02005-02-26 23:04:13 +000024707 if (fp->uf_name[0] == K_SPECIAL)
24708 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24709 else
24710 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24711 if (fp->uf_tm_count == 1)
24712 fprintf(fd, "Called 1 time\n");
24713 else
24714 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24715 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24716 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24717 fprintf(fd, "\n");
24718 fprintf(fd, "count total (s) self (s)\n");
24719
24720 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24721 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024722 if (FUNCLINE(fp, i) == NULL)
24723 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024724 prof_func_line(fd, fp->uf_tml_count[i],
24725 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024726 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24727 }
24728 fprintf(fd, "\n");
24729 }
24730 }
24731 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024732
24733 if (sorttab != NULL && st_len > 0)
24734 {
24735 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24736 prof_total_cmp);
24737 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24738 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24739 prof_self_cmp);
24740 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24741 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024742
24743 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024744}
Bram Moolenaar73830342005-02-28 22:48:19 +000024745
24746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024747prof_sort_list(
24748 FILE *fd,
24749 ufunc_T **sorttab,
24750 int st_len,
24751 char *title,
24752 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024753{
24754 int i;
24755 ufunc_T *fp;
24756
24757 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24758 fprintf(fd, "count total (s) self (s) function\n");
24759 for (i = 0; i < 20 && i < st_len; ++i)
24760 {
24761 fp = sorttab[i];
24762 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24763 prefer_self);
24764 if (fp->uf_name[0] == K_SPECIAL)
24765 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24766 else
24767 fprintf(fd, " %s()\n", fp->uf_name);
24768 }
24769 fprintf(fd, "\n");
24770}
24771
24772/*
24773 * Print the count and times for one function or function line.
24774 */
24775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024776prof_func_line(
24777 FILE *fd,
24778 int count,
24779 proftime_T *total,
24780 proftime_T *self,
24781 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024782{
24783 if (count > 0)
24784 {
24785 fprintf(fd, "%5d ", count);
24786 if (prefer_self && profile_equal(total, self))
24787 fprintf(fd, " ");
24788 else
24789 fprintf(fd, "%s ", profile_msg(total));
24790 if (!prefer_self && profile_equal(total, self))
24791 fprintf(fd, " ");
24792 else
24793 fprintf(fd, "%s ", profile_msg(self));
24794 }
24795 else
24796 fprintf(fd, " ");
24797}
24798
24799/*
24800 * Compare function for total time sorting.
24801 */
24802 static int
24803#ifdef __BORLANDC__
24804_RTLENTRYF
24805#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024806prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024807{
24808 ufunc_T *p1, *p2;
24809
24810 p1 = *(ufunc_T **)s1;
24811 p2 = *(ufunc_T **)s2;
24812 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24813}
24814
24815/*
24816 * Compare function for self time sorting.
24817 */
24818 static int
24819#ifdef __BORLANDC__
24820_RTLENTRYF
24821#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024822prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024823{
24824 ufunc_T *p1, *p2;
24825
24826 p1 = *(ufunc_T **)s1;
24827 p2 = *(ufunc_T **)s2;
24828 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24829}
24830
Bram Moolenaar05159a02005-02-26 23:04:13 +000024831#endif
24832
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024833/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024834 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024835 * Return TRUE if a package was loaded.
24836 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024837 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024838script_autoload(
24839 char_u *name,
24840 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024841{
24842 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024843 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024844 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024845 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024846
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024847 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024848 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024849 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024850 return FALSE;
24851
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024852 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024853
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024854 /* Find the name in the list of previously loaded package names. Skip
24855 * "autoload/", it's always the same. */
24856 for (i = 0; i < ga_loaded.ga_len; ++i)
24857 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24858 break;
24859 if (!reload && i < ga_loaded.ga_len)
24860 ret = FALSE; /* was loaded already */
24861 else
24862 {
24863 /* Remember the name if it wasn't loaded already. */
24864 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24865 {
24866 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24867 tofree = NULL;
24868 }
24869
24870 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024871 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024872 ret = TRUE;
24873 }
24874
24875 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024876 return ret;
24877}
24878
24879/*
24880 * Return the autoload script name for a function or variable name.
24881 * Returns NULL when out of memory.
24882 */
24883 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024884autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024885{
24886 char_u *p;
24887 char_u *scriptname;
24888
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024889 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024890 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24891 if (scriptname == NULL)
24892 return FALSE;
24893 STRCPY(scriptname, "autoload/");
24894 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024895 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024896 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024897 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024898 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024899 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024900}
24901
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24903
24904/*
24905 * Function given to ExpandGeneric() to obtain the list of user defined
24906 * function names.
24907 */
24908 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024909get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024911 static long_u done;
24912 static hashitem_T *hi;
24913 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914
24915 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024917 done = 0;
24918 hi = func_hashtab.ht_array;
24919 }
24920 if (done < func_hashtab.ht_used)
24921 {
24922 if (done++ > 0)
24923 ++hi;
24924 while (HASHITEM_EMPTY(hi))
24925 ++hi;
24926 fp = HI2UF(hi);
24927
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024928 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024929 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024930
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024931 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24932 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933
24934 cat_func_name(IObuff, fp);
24935 if (xp->xp_context != EXPAND_USER_FUNC)
24936 {
24937 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024938 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 STRCAT(IObuff, ")");
24940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941 return IObuff;
24942 }
24943 return NULL;
24944}
24945
24946#endif /* FEAT_CMDL_COMPL */
24947
24948/*
24949 * Copy the function name of "fp" to buffer "buf".
24950 * "buf" must be able to hold the function name plus three bytes.
24951 * Takes care of script-local function names.
24952 */
24953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024954cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024956 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 {
24958 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024959 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960 }
24961 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024962 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963}
24964
24965/*
24966 * ":delfunction {name}"
24967 */
24968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024969ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024971 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024972 char_u *p;
24973 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024974 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975
24976 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024977 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024978 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024980 {
24981 if (fudi.fd_dict != NULL && !eap->skip)
24982 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024983 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 if (!ends_excmd(*skipwhite(p)))
24986 {
24987 vim_free(name);
24988 EMSG(_(e_trailing));
24989 return;
24990 }
24991 eap->nextcmd = check_nextcmd(p);
24992 if (eap->nextcmd != NULL)
24993 *p = NUL;
24994
24995 if (!eap->skip)
24996 fp = find_func(name);
24997 vim_free(name);
24998
24999 if (!eap->skip)
25000 {
25001 if (fp == NULL)
25002 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025003 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 return;
25005 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025006 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025007 {
25008 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25009 return;
25010 }
25011
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025012 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025014 /* Delete the dict item that refers to the function, it will
25015 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025016 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025018 else
25019 func_free(fp);
25020 }
25021}
25022
25023/*
25024 * Free a function and remove it from the list of functions.
25025 */
25026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025027func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025028{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025029 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025030
25031 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025032 ga_clear_strings(&(fp->uf_args));
25033 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025034#ifdef FEAT_PROFILE
25035 vim_free(fp->uf_tml_count);
25036 vim_free(fp->uf_tml_total);
25037 vim_free(fp->uf_tml_self);
25038#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025039
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025040 /* remove the function from the function hashtable */
25041 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25042 if (HASHITEM_EMPTY(hi))
25043 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025044 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025045 hash_remove(&func_hashtab, hi);
25046
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025047 vim_free(fp);
25048}
25049
25050/*
25051 * Unreference a Function: decrement the reference count and free it when it
25052 * becomes zero. Only for numbered functions.
25053 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025054 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025055func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025056{
25057 ufunc_T *fp;
25058
25059 if (name != NULL && isdigit(*name))
25060 {
25061 fp = find_func(name);
25062 if (fp == NULL)
25063 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025064 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025065 {
25066 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025067 * when "uf_calls" becomes zero. */
25068 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025069 func_free(fp);
25070 }
25071 }
25072}
25073
25074/*
25075 * Count a reference to a Function.
25076 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025078func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025079{
25080 ufunc_T *fp;
25081
25082 if (name != NULL && isdigit(*name))
25083 {
25084 fp = find_func(name);
25085 if (fp == NULL)
25086 EMSG2(_(e_intern2), "func_ref()");
25087 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025088 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 }
25090}
25091
25092/*
25093 * Call a user function.
25094 */
25095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025096call_user_func(
25097 ufunc_T *fp, /* pointer to function */
25098 int argcount, /* nr of args */
25099 typval_T *argvars, /* arguments */
25100 typval_T *rettv, /* return value */
25101 linenr_T firstline, /* first line of range */
25102 linenr_T lastline, /* last line of range */
25103 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104{
Bram Moolenaar33570922005-01-25 22:26:29 +000025105 char_u *save_sourcing_name;
25106 linenr_T save_sourcing_lnum;
25107 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025108 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025109 int save_did_emsg;
25110 static int depth = 0;
25111 dictitem_T *v;
25112 int fixvar_idx = 0; /* index in fixvar[] */
25113 int i;
25114 int ai;
25115 char_u numbuf[NUMBUFLEN];
25116 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025117 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025118#ifdef FEAT_PROFILE
25119 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025120 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122
25123 /* If depth of calling is getting too high, don't execute the function */
25124 if (depth >= p_mfd)
25125 {
25126 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025127 rettv->v_type = VAR_NUMBER;
25128 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025129 return;
25130 }
25131 ++depth;
25132
25133 line_breakcheck(); /* check for CTRL-C hit */
25134
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025135 fc = (funccall_T *)alloc(sizeof(funccall_T));
25136 fc->caller = current_funccal;
25137 current_funccal = fc;
25138 fc->func = fp;
25139 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025140 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025141 fc->linenr = 0;
25142 fc->returned = FALSE;
25143 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025145 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25146 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025147
Bram Moolenaar33570922005-01-25 22:26:29 +000025148 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025149 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025150 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25151 * each argument variable and saves a lot of time.
25152 */
25153 /*
25154 * Init l: variables.
25155 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025156 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025157 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025158 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025159 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25160 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025161 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025162 name = v->di_key;
25163 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025164 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025165 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025166 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025167 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025168 v->di_tv.vval.v_dict = selfdict;
25169 ++selfdict->dv_refcount;
25170 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025171
Bram Moolenaar33570922005-01-25 22:26:29 +000025172 /*
25173 * Init a: variables.
25174 * Set a:0 to "argcount".
25175 * Set a:000 to a list with room for the "..." arguments.
25176 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025177 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025178 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025179 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025180 /* Use "name" to avoid a warning from some compiler that checks the
25181 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025182 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025183 name = v->di_key;
25184 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025185 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025186 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025187 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025188 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025189 v->di_tv.vval.v_list = &fc->l_varlist;
25190 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25191 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25192 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025193
25194 /*
25195 * Set a:firstline to "firstline" and a:lastline to "lastline".
25196 * Set a:name to named arguments.
25197 * Set a:N to the "..." arguments.
25198 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025199 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025200 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025201 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025202 (varnumber_T)lastline);
25203 for (i = 0; i < argcount; ++i)
25204 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025205 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025206 if (ai < 0)
25207 /* named argument a:name */
25208 name = FUNCARG(fp, i);
25209 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025210 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025211 /* "..." argument a:1, a:2, etc. */
25212 sprintf((char *)numbuf, "%d", ai + 1);
25213 name = numbuf;
25214 }
25215 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25216 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025217 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025218 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25219 }
25220 else
25221 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025222 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25223 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025224 if (v == NULL)
25225 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025226 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025227 }
25228 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025229 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025230
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025231 /* Note: the values are copied directly to avoid alloc/free.
25232 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025233 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025234 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025235
25236 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25237 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025238 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25239 fc->l_listitems[ai].li_tv = argvars[i];
25240 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025241 }
25242 }
25243
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244 /* Don't redraw while executing the function. */
25245 ++RedrawingDisabled;
25246 save_sourcing_name = sourcing_name;
25247 save_sourcing_lnum = sourcing_lnum;
25248 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025249 /* need space for function name + ("function " + 3) or "[number]" */
25250 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25251 + STRLEN(fp->uf_name) + 20;
25252 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 if (sourcing_name != NULL)
25254 {
25255 if (save_sourcing_name != NULL
25256 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025257 sprintf((char *)sourcing_name, "%s[%d]..",
25258 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025259 else
25260 STRCPY(sourcing_name, "function ");
25261 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25262
25263 if (p_verbose >= 12)
25264 {
25265 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025266 verbose_enter_scroll();
25267
Bram Moolenaar555b2802005-05-19 21:08:39 +000025268 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269 if (p_verbose >= 14)
25270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025272 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025273 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025274 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275
25276 msg_puts((char_u *)"(");
25277 for (i = 0; i < argcount; ++i)
25278 {
25279 if (i > 0)
25280 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025281 if (argvars[i].v_type == VAR_NUMBER)
25282 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283 else
25284 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025285 /* Do not want errors such as E724 here. */
25286 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025287 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025288 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025289 if (s != NULL)
25290 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025291 if (vim_strsize(s) > MSG_BUF_CLEN)
25292 {
25293 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25294 s = buf;
25295 }
25296 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025297 vim_free(tofree);
25298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299 }
25300 }
25301 msg_puts((char_u *)")");
25302 }
25303 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025304
25305 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 --no_wait_return;
25307 }
25308 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025309#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025310 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025311 {
25312 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25313 func_do_profile(fp);
25314 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025315 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025316 {
25317 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025318 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025319 profile_zero(&fp->uf_tm_children);
25320 }
25321 script_prof_save(&wait_start);
25322 }
25323#endif
25324
Bram Moolenaar071d4272004-06-13 20:20:40 +000025325 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025326 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327 save_did_emsg = did_emsg;
25328 did_emsg = FALSE;
25329
25330 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025331 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25333
25334 --RedrawingDisabled;
25335
25336 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025337 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025339 clear_tv(rettv);
25340 rettv->v_type = VAR_NUMBER;
25341 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342 }
25343
Bram Moolenaar05159a02005-02-26 23:04:13 +000025344#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025345 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025346 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025347 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025348 profile_end(&call_start);
25349 profile_sub_wait(&wait_start, &call_start);
25350 profile_add(&fp->uf_tm_total, &call_start);
25351 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025352 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025353 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025354 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25355 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025356 }
25357 }
25358#endif
25359
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 /* when being verbose, mention the return value */
25361 if (p_verbose >= 12)
25362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025364 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025365
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025367 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025368 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025369 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025370 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025373 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025374 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025375 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025376 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025377
Bram Moolenaar555b2802005-05-19 21:08:39 +000025378 /* The value may be very long. Skip the middle part, so that we
25379 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025380 * truncate it at the end. Don't want errors such as E724 here. */
25381 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025382 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025383 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025384 if (s != NULL)
25385 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025386 if (vim_strsize(s) > MSG_BUF_CLEN)
25387 {
25388 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25389 s = buf;
25390 }
25391 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025392 vim_free(tofree);
25393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 }
25395 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025396
25397 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398 --no_wait_return;
25399 }
25400
25401 vim_free(sourcing_name);
25402 sourcing_name = save_sourcing_name;
25403 sourcing_lnum = save_sourcing_lnum;
25404 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025405#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025406 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025407 script_prof_restore(&wait_start);
25408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409
25410 if (p_verbose >= 12 && sourcing_name != NULL)
25411 {
25412 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025413 verbose_enter_scroll();
25414
Bram Moolenaar555b2802005-05-19 21:08:39 +000025415 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025417
25418 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 --no_wait_return;
25420 }
25421
25422 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025423 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025425
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025426 /* If the a:000 list and the l: and a: dicts are not referenced we can
25427 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025428 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25429 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25430 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25431 {
25432 free_funccal(fc, FALSE);
25433 }
25434 else
25435 {
25436 hashitem_T *hi;
25437 listitem_T *li;
25438 int todo;
25439
25440 /* "fc" is still in use. This can happen when returning "a:000" or
25441 * assigning "l:" to a global variable.
25442 * Link "fc" in the list for garbage collection later. */
25443 fc->caller = previous_funccal;
25444 previous_funccal = fc;
25445
25446 /* Make a copy of the a: variables, since we didn't do that above. */
25447 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25448 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25449 {
25450 if (!HASHITEM_EMPTY(hi))
25451 {
25452 --todo;
25453 v = HI2DI(hi);
25454 copy_tv(&v->di_tv, &v->di_tv);
25455 }
25456 }
25457
25458 /* Make a copy of the a:000 items, since we didn't do that above. */
25459 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25460 copy_tv(&li->li_tv, &li->li_tv);
25461 }
25462}
25463
25464/*
25465 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025466 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025467 */
25468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025469can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025470{
25471 return (fc->l_varlist.lv_copyID != copyID
25472 && fc->l_vars.dv_copyID != copyID
25473 && fc->l_avars.dv_copyID != copyID);
25474}
25475
25476/*
25477 * Free "fc" and what it contains.
25478 */
25479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025480free_funccal(
25481 funccall_T *fc,
25482 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025483{
25484 listitem_T *li;
25485
25486 /* The a: variables typevals may not have been allocated, only free the
25487 * allocated variables. */
25488 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25489
25490 /* free all l: variables */
25491 vars_clear(&fc->l_vars.dv_hashtab);
25492
25493 /* Free the a:000 variables if they were allocated. */
25494 if (free_val)
25495 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25496 clear_tv(&li->li_tv);
25497
25498 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499}
25500
25501/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025502 * Add a number variable "name" to dict "dp" with value "nr".
25503 */
25504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025505add_nr_var(
25506 dict_T *dp,
25507 dictitem_T *v,
25508 char *name,
25509 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025510{
25511 STRCPY(v->di_key, name);
25512 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25513 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25514 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025515 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025516 v->di_tv.vval.v_number = nr;
25517}
25518
25519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520 * ":return [expr]"
25521 */
25522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025523ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524{
25525 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025526 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527 int returning = FALSE;
25528
25529 if (current_funccal == NULL)
25530 {
25531 EMSG(_("E133: :return not inside a function"));
25532 return;
25533 }
25534
25535 if (eap->skip)
25536 ++emsg_skip;
25537
25538 eap->nextcmd = NULL;
25539 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025540 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025541 {
25542 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025543 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025545 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546 }
25547 /* It's safer to return also on error. */
25548 else if (!eap->skip)
25549 {
25550 /*
25551 * Return unless the expression evaluation has been cancelled due to an
25552 * aborting error, an interrupt, or an exception.
25553 */
25554 if (!aborting())
25555 returning = do_return(eap, FALSE, TRUE, NULL);
25556 }
25557
25558 /* When skipping or the return gets pending, advance to the next command
25559 * in this line (!returning). Otherwise, ignore the rest of the line.
25560 * Following lines will be ignored by get_func_line(). */
25561 if (returning)
25562 eap->nextcmd = NULL;
25563 else if (eap->nextcmd == NULL) /* no argument */
25564 eap->nextcmd = check_nextcmd(arg);
25565
25566 if (eap->skip)
25567 --emsg_skip;
25568}
25569
25570/*
25571 * Return from a function. Possibly makes the return pending. Also called
25572 * for a pending return at the ":endtry" or after returning from an extra
25573 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025574 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025575 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 * FALSE when the return gets pending.
25577 */
25578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025579do_return(
25580 exarg_T *eap,
25581 int reanimate,
25582 int is_cmd,
25583 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584{
25585 int idx;
25586 struct condstack *cstack = eap->cstack;
25587
25588 if (reanimate)
25589 /* Undo the return. */
25590 current_funccal->returned = FALSE;
25591
25592 /*
25593 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25594 * not in its finally clause (which then is to be executed next) is found.
25595 * In this case, make the ":return" pending for execution at the ":endtry".
25596 * Otherwise, return normally.
25597 */
25598 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25599 if (idx >= 0)
25600 {
25601 cstack->cs_pending[idx] = CSTP_RETURN;
25602
25603 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025604 /* A pending return again gets pending. "rettv" points to an
25605 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025607 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 else
25609 {
25610 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025611 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025613 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025615 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616 {
25617 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025618 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025619 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620 else
25621 EMSG(_(e_outofmem));
25622 }
25623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025624 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025625
25626 if (reanimate)
25627 {
25628 /* The pending return value could be overwritten by a ":return"
25629 * without argument in a finally clause; reset the default
25630 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025631 current_funccal->rettv->v_type = VAR_NUMBER;
25632 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025633 }
25634 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025635 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 }
25637 else
25638 {
25639 current_funccal->returned = TRUE;
25640
25641 /* If the return is carried out now, store the return value. For
25642 * a return immediately after reanimation, the value is already
25643 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025644 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025646 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025647 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025649 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650 }
25651 }
25652
25653 return idx < 0;
25654}
25655
25656/*
25657 * Free the variable with a pending return value.
25658 */
25659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025660discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661{
Bram Moolenaar33570922005-01-25 22:26:29 +000025662 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663}
25664
25665/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025666 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 * is an allocated string. Used by report_pending() for verbose messages.
25668 */
25669 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025670get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025672 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025673 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025674 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025675
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025676 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025677 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025678 if (s == NULL)
25679 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025680
25681 STRCPY(IObuff, ":return ");
25682 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25683 if (STRLEN(s) + 8 >= IOSIZE)
25684 STRCPY(IObuff + IOSIZE - 4, "...");
25685 vim_free(tofree);
25686 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025687}
25688
25689/*
25690 * Get next function line.
25691 * Called by do_cmdline() to get the next line.
25692 * Returns allocated string, or NULL for end of function.
25693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025695get_func_line(
25696 int c UNUSED,
25697 void *cookie,
25698 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025699{
Bram Moolenaar33570922005-01-25 22:26:29 +000025700 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025701 ufunc_T *fp = fcp->func;
25702 char_u *retval;
25703 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704
25705 /* If breakpoints have been added/deleted need to check for it. */
25706 if (fcp->dbg_tick != debug_tick)
25707 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025708 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025709 sourcing_lnum);
25710 fcp->dbg_tick = debug_tick;
25711 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025712#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025713 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025714 func_line_end(cookie);
25715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716
Bram Moolenaar05159a02005-02-26 23:04:13 +000025717 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025718 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25719 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720 retval = NULL;
25721 else
25722 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025723 /* Skip NULL lines (continuation lines). */
25724 while (fcp->linenr < gap->ga_len
25725 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25726 ++fcp->linenr;
25727 if (fcp->linenr >= gap->ga_len)
25728 retval = NULL;
25729 else
25730 {
25731 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25732 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025733#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025734 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025735 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025736#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 }
25739
25740 /* Did we encounter a breakpoint? */
25741 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25742 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025743 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025745 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746 sourcing_lnum);
25747 fcp->dbg_tick = debug_tick;
25748 }
25749
25750 return retval;
25751}
25752
Bram Moolenaar05159a02005-02-26 23:04:13 +000025753#if defined(FEAT_PROFILE) || defined(PROTO)
25754/*
25755 * Called when starting to read a function line.
25756 * "sourcing_lnum" must be correct!
25757 * When skipping lines it may not actually be executed, but we won't find out
25758 * until later and we need to store the time now.
25759 */
25760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025761func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025762{
25763 funccall_T *fcp = (funccall_T *)cookie;
25764 ufunc_T *fp = fcp->func;
25765
25766 if (fp->uf_profiling && sourcing_lnum >= 1
25767 && sourcing_lnum <= fp->uf_lines.ga_len)
25768 {
25769 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025770 /* Skip continuation lines. */
25771 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25772 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025773 fp->uf_tml_execed = FALSE;
25774 profile_start(&fp->uf_tml_start);
25775 profile_zero(&fp->uf_tml_children);
25776 profile_get_wait(&fp->uf_tml_wait);
25777 }
25778}
25779
25780/*
25781 * Called when actually executing a function line.
25782 */
25783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025784func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025785{
25786 funccall_T *fcp = (funccall_T *)cookie;
25787 ufunc_T *fp = fcp->func;
25788
25789 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25790 fp->uf_tml_execed = TRUE;
25791}
25792
25793/*
25794 * Called when done with a function line.
25795 */
25796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025797func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025798{
25799 funccall_T *fcp = (funccall_T *)cookie;
25800 ufunc_T *fp = fcp->func;
25801
25802 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25803 {
25804 if (fp->uf_tml_execed)
25805 {
25806 ++fp->uf_tml_count[fp->uf_tml_idx];
25807 profile_end(&fp->uf_tml_start);
25808 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025809 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025810 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25811 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025812 }
25813 fp->uf_tml_idx = -1;
25814 }
25815}
25816#endif
25817
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818/*
25819 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025820 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025821 */
25822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025823func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025824{
Bram Moolenaar33570922005-01-25 22:26:29 +000025825 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826
25827 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25828 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025829 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 || fcp->returned);
25831}
25832
25833/*
25834 * return TRUE if cookie indicates a function which "abort"s on errors.
25835 */
25836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025837func_has_abort(
25838 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025840 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025841}
25842
25843#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25844typedef enum
25845{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025846 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25847 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25848 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025849} var_flavour_T;
25850
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025851static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852
25853 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025854var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025855{
25856 char_u *p = varname;
25857
25858 if (ASCII_ISUPPER(*p))
25859 {
25860 while (*(++p))
25861 if (ASCII_ISLOWER(*p))
25862 return VAR_FLAVOUR_SESSION;
25863 return VAR_FLAVOUR_VIMINFO;
25864 }
25865 else
25866 return VAR_FLAVOUR_DEFAULT;
25867}
25868#endif
25869
25870#if defined(FEAT_VIMINFO) || defined(PROTO)
25871/*
25872 * Restore global vars that start with a capital from the viminfo file
25873 */
25874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025875read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025876{
25877 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025878 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025879 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025880 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025881
25882 if (!writing && (find_viminfo_parameter('!') != NULL))
25883 {
25884 tab = vim_strchr(virp->vir_line + 1, '\t');
25885 if (tab != NULL)
25886 {
25887 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025888 switch (*tab)
25889 {
25890 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025891#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025892 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025893#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025894 case 'D': type = VAR_DICT; break;
25895 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025896 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025898
25899 tab = vim_strchr(tab, '\t');
25900 if (tab != NULL)
25901 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025902 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025903 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025904 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025905 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025906#ifdef FEAT_FLOAT
25907 else if (type == VAR_FLOAT)
25908 (void)string2float(tab + 1, &tv.vval.v_float);
25909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025911 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025912 if (type == VAR_DICT || type == VAR_LIST)
25913 {
25914 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25915
25916 if (etv == NULL)
25917 /* Failed to parse back the dict or list, use it as a
25918 * string. */
25919 tv.v_type = VAR_STRING;
25920 else
25921 {
25922 vim_free(tv.vval.v_string);
25923 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025924 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025925 }
25926 }
25927
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025928 /* when in a function use global variables */
25929 save_funccal = current_funccal;
25930 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025931 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025932 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025933
25934 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025935 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025936 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25937 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938 }
25939 }
25940 }
25941
25942 return viminfo_readline(virp);
25943}
25944
25945/*
25946 * Write global vars that start with a capital to the viminfo file
25947 */
25948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025949write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950{
Bram Moolenaar33570922005-01-25 22:26:29 +000025951 hashitem_T *hi;
25952 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025953 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025954 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025955 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025956 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025957 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025958
25959 if (find_viminfo_parameter('!') == NULL)
25960 return;
25961
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025962 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025963
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025964 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025965 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025967 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025969 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025970 this_var = HI2DI(hi);
25971 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025972 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025973 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025974 {
25975 case VAR_STRING: s = "STR"; break;
25976 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025977 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025978 case VAR_DICT: s = "DIC"; break;
25979 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025980 case VAR_SPECIAL: s = "XPL"; break;
25981
25982 case VAR_UNKNOWN:
25983 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025984 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025985 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025986 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025987 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025988 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025989 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025990 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025991 if (p != NULL)
25992 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025993 vim_free(tofree);
25994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025995 }
25996 }
25997}
25998#endif
25999
26000#if defined(FEAT_SESSION) || defined(PROTO)
26001 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026002store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026003{
Bram Moolenaar33570922005-01-25 22:26:29 +000026004 hashitem_T *hi;
26005 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026006 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007 char_u *p, *t;
26008
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026009 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026010 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026011 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026012 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026014 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026015 this_var = HI2DI(hi);
26016 if ((this_var->di_tv.v_type == VAR_NUMBER
26017 || this_var->di_tv.v_type == VAR_STRING)
26018 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026019 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026020 /* Escape special characters with a backslash. Turn a LF and
26021 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026022 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026023 (char_u *)"\\\"\n\r");
26024 if (p == NULL) /* out of memory */
26025 break;
26026 for (t = p; *t != NUL; ++t)
26027 if (*t == '\n')
26028 *t = 'n';
26029 else if (*t == '\r')
26030 *t = 'r';
26031 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026032 this_var->di_key,
26033 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26034 : ' ',
26035 p,
26036 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26037 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026038 || put_eol(fd) == FAIL)
26039 {
26040 vim_free(p);
26041 return FAIL;
26042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043 vim_free(p);
26044 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026045#ifdef FEAT_FLOAT
26046 else if (this_var->di_tv.v_type == VAR_FLOAT
26047 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26048 {
26049 float_T f = this_var->di_tv.vval.v_float;
26050 int sign = ' ';
26051
26052 if (f < 0)
26053 {
26054 f = -f;
26055 sign = '-';
26056 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026057 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026058 this_var->di_key, sign, f) < 0)
26059 || put_eol(fd) == FAIL)
26060 return FAIL;
26061 }
26062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063 }
26064 }
26065 return OK;
26066}
26067#endif
26068
Bram Moolenaar661b1822005-07-28 22:36:45 +000026069/*
26070 * Display script name where an item was last set.
26071 * Should only be invoked when 'verbose' is non-zero.
26072 */
26073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026074last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026075{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026076 char_u *p;
26077
Bram Moolenaar661b1822005-07-28 22:36:45 +000026078 if (scriptID != 0)
26079 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026080 p = home_replace_save(NULL, get_scriptname(scriptID));
26081 if (p != NULL)
26082 {
26083 verbose_enter();
26084 MSG_PUTS(_("\n\tLast set from "));
26085 MSG_PUTS(p);
26086 vim_free(p);
26087 verbose_leave();
26088 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026089 }
26090}
26091
Bram Moolenaard812df62008-11-09 12:46:09 +000026092/*
26093 * List v:oldfiles in a nice way.
26094 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026096ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026097{
26098 list_T *l = vimvars[VV_OLDFILES].vv_list;
26099 listitem_T *li;
26100 int nr = 0;
26101
26102 if (l == NULL)
26103 msg((char_u *)_("No old files"));
26104 else
26105 {
26106 msg_start();
26107 msg_scroll = TRUE;
26108 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26109 {
26110 msg_outnum((long)++nr);
26111 MSG_PUTS(": ");
26112 msg_outtrans(get_tv_string(&li->li_tv));
26113 msg_putchar('\n');
26114 out_flush(); /* output one line at a time */
26115 ui_breakcheck();
26116 }
26117 /* Assume "got_int" was set to truncate the listing. */
26118 got_int = FALSE;
26119
26120#ifdef FEAT_BROWSE_CMD
26121 if (cmdmod.browse)
26122 {
26123 quit_more = FALSE;
26124 nr = prompt_for_number(FALSE);
26125 msg_starthere();
26126 if (nr > 0)
26127 {
26128 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26129 (long)nr);
26130
26131 if (p != NULL)
26132 {
26133 p = expand_env_save(p);
26134 eap->arg = p;
26135 eap->cmdidx = CMD_edit;
26136 cmdmod.browse = FALSE;
26137 do_exedit(eap, NULL);
26138 vim_free(p);
26139 }
26140 }
26141 }
26142#endif
26143 }
26144}
26145
Bram Moolenaar53744302015-07-17 17:38:22 +020026146/* reset v:option_new, v:option_old and v:option_type */
26147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026148reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026149{
26150 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26151 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26152 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26153}
26154
26155
Bram Moolenaar071d4272004-06-13 20:20:40 +000026156#endif /* FEAT_EVAL */
26157
Bram Moolenaar071d4272004-06-13 20:20:40 +000026158
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026159#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160
26161#ifdef WIN3264
26162/*
26163 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26164 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026165static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26166static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26167static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026168
26169/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026170 * Get the short path (8.3) for the filename in "fnamep".
26171 * Only works for a valid file name.
26172 * When the path gets longer "fnamep" is changed and the allocated buffer
26173 * is put in "bufp".
26174 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26175 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026176 */
26177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026178get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026179{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026180 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026181 char_u *newbuf;
26182
26183 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026184 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026185 if (l > len - 1)
26186 {
26187 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026188 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026189 newbuf = vim_strnsave(*fnamep, l);
26190 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192
26193 vim_free(*bufp);
26194 *fnamep = *bufp = newbuf;
26195
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026196 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026197 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198 }
26199
26200 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026201 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026202}
26203
26204/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026205 * Get the short path (8.3) for the filename in "fname". The converted
26206 * path is returned in "bufp".
26207 *
26208 * Some of the directories specified in "fname" may not exist. This function
26209 * will shorten the existing directories at the beginning of the path and then
26210 * append the remaining non-existing path.
26211 *
26212 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026213 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026214 * bufp - Pointer to an allocated buffer for the filename.
26215 * fnamelen - Length of the filename pointed to by fname
26216 *
26217 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026218 */
26219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026220shortpath_for_invalid_fname(
26221 char_u **fname,
26222 char_u **bufp,
26223 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026224{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026225 char_u *short_fname, *save_fname, *pbuf_unused;
26226 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026228 int old_len, len;
26229 int new_len, sfx_len;
26230 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026231
26232 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026233 old_len = *fnamelen;
26234 save_fname = vim_strnsave(*fname, old_len);
26235 pbuf_unused = NULL;
26236 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026237
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026238 endp = save_fname + old_len - 1; /* Find the end of the copy */
26239 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026240
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026241 /*
26242 * Try shortening the supplied path till it succeeds by removing one
26243 * directory at a time from the tail of the path.
26244 */
26245 len = 0;
26246 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026247 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026248 /* go back one path-separator */
26249 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26250 --endp;
26251 if (endp <= save_fname)
26252 break; /* processed the complete path */
26253
26254 /*
26255 * Replace the path separator with a NUL and try to shorten the
26256 * resulting path.
26257 */
26258 ch = *endp;
26259 *endp = 0;
26260 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026261 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026262 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26263 {
26264 retval = FAIL;
26265 goto theend;
26266 }
26267 *endp = ch; /* preserve the string */
26268
26269 if (len > 0)
26270 break; /* successfully shortened the path */
26271
26272 /* failed to shorten the path. Skip the path separator */
26273 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026274 }
26275
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026276 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026277 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026278 /*
26279 * Succeeded in shortening the path. Now concatenate the shortened
26280 * path with the remaining path at the tail.
26281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026282
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026283 /* Compute the length of the new path. */
26284 sfx_len = (int)(save_endp - endp) + 1;
26285 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026286
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026287 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026288 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026289 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026290 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026291 /* There is not enough space in the currently allocated string,
26292 * copy it to a buffer big enough. */
26293 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026294 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026295 {
26296 retval = FAIL;
26297 goto theend;
26298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026299 }
26300 else
26301 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026302 /* Transfer short_fname to the main buffer (it's big enough),
26303 * unless get_short_pathname() did its work in-place. */
26304 *fname = *bufp = save_fname;
26305 if (short_fname != save_fname)
26306 vim_strncpy(save_fname, short_fname, len);
26307 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026309
26310 /* concat the not-shortened part of the path */
26311 vim_strncpy(*fname + len, endp, sfx_len);
26312 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026313 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026314
26315theend:
26316 vim_free(pbuf_unused);
26317 vim_free(save_fname);
26318
26319 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026320}
26321
26322/*
26323 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026324 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026325 */
26326 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026327shortpath_for_partial(
26328 char_u **fnamep,
26329 char_u **bufp,
26330 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026331{
26332 int sepcount, len, tflen;
26333 char_u *p;
26334 char_u *pbuf, *tfname;
26335 int hasTilde;
26336
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026337 /* Count up the path separators from the RHS.. so we know which part
26338 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026339 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026340 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026341 if (vim_ispathsep(*p))
26342 ++sepcount;
26343
26344 /* Need full path first (use expand_env() to remove a "~/") */
26345 hasTilde = (**fnamep == '~');
26346 if (hasTilde)
26347 pbuf = tfname = expand_env_save(*fnamep);
26348 else
26349 pbuf = tfname = FullName_save(*fnamep, FALSE);
26350
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026351 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026353 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026355
26356 if (len == 0)
26357 {
26358 /* Don't have a valid filename, so shorten the rest of the
26359 * path if we can. This CAN give us invalid 8.3 filenames, but
26360 * there's not a lot of point in guessing what it might be.
26361 */
26362 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026363 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026365 }
26366
26367 /* Count the paths backward to find the beginning of the desired string. */
26368 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026369 {
26370#ifdef FEAT_MBYTE
26371 if (has_mbyte)
26372 p -= mb_head_off(tfname, p);
26373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026374 if (vim_ispathsep(*p))
26375 {
26376 if (sepcount == 0 || (hasTilde && sepcount == 1))
26377 break;
26378 else
26379 sepcount --;
26380 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382 if (hasTilde)
26383 {
26384 --p;
26385 if (p >= tfname)
26386 *p = '~';
26387 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026388 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026389 }
26390 else
26391 ++p;
26392
26393 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26394 vim_free(*bufp);
26395 *fnamelen = (int)STRLEN(p);
26396 *bufp = pbuf;
26397 *fnamep = p;
26398
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026399 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400}
26401#endif /* WIN3264 */
26402
26403/*
26404 * Adjust a filename, according to a string of modifiers.
26405 * *fnamep must be NUL terminated when called. When returning, the length is
26406 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026407 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026408 * When there is an error, *fnamep is set to NULL.
26409 */
26410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026411modify_fname(
26412 char_u *src, /* string with modifiers */
26413 int *usedlen, /* characters after src that are used */
26414 char_u **fnamep, /* file name so far */
26415 char_u **bufp, /* buffer for allocated file name or NULL */
26416 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026417{
26418 int valid = 0;
26419 char_u *tail;
26420 char_u *s, *p, *pbuf;
26421 char_u dirname[MAXPATHL];
26422 int c;
26423 int has_fullname = 0;
26424#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026425 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 int has_shortname = 0;
26427#endif
26428
26429repeat:
26430 /* ":p" - full path/file_name */
26431 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26432 {
26433 has_fullname = 1;
26434
26435 valid |= VALID_PATH;
26436 *usedlen += 2;
26437
26438 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26439 if ((*fnamep)[0] == '~'
26440#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26441 && ((*fnamep)[1] == '/'
26442# ifdef BACKSLASH_IN_FILENAME
26443 || (*fnamep)[1] == '\\'
26444# endif
26445 || (*fnamep)[1] == NUL)
26446
26447#endif
26448 )
26449 {
26450 *fnamep = expand_env_save(*fnamep);
26451 vim_free(*bufp); /* free any allocated file name */
26452 *bufp = *fnamep;
26453 if (*fnamep == NULL)
26454 return -1;
26455 }
26456
26457 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026458 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026459 {
26460 if (vim_ispathsep(*p)
26461 && p[1] == '.'
26462 && (p[2] == NUL
26463 || vim_ispathsep(p[2])
26464 || (p[2] == '.'
26465 && (p[3] == NUL || vim_ispathsep(p[3])))))
26466 break;
26467 }
26468
26469 /* FullName_save() is slow, don't use it when not needed. */
26470 if (*p != NUL || !vim_isAbsName(*fnamep))
26471 {
26472 *fnamep = FullName_save(*fnamep, *p != NUL);
26473 vim_free(*bufp); /* free any allocated file name */
26474 *bufp = *fnamep;
26475 if (*fnamep == NULL)
26476 return -1;
26477 }
26478
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026479#ifdef WIN3264
26480# if _WIN32_WINNT >= 0x0500
26481 if (vim_strchr(*fnamep, '~') != NULL)
26482 {
26483 /* Expand 8.3 filename to full path. Needed to make sure the same
26484 * file does not have two different names.
26485 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26486 p = alloc(_MAX_PATH + 1);
26487 if (p != NULL)
26488 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026489 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026490 {
26491 vim_free(*bufp);
26492 *bufp = *fnamep = p;
26493 }
26494 else
26495 vim_free(p);
26496 }
26497 }
26498# endif
26499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026500 /* Append a path separator to a directory. */
26501 if (mch_isdir(*fnamep))
26502 {
26503 /* Make room for one or two extra characters. */
26504 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26505 vim_free(*bufp); /* free any allocated file name */
26506 *bufp = *fnamep;
26507 if (*fnamep == NULL)
26508 return -1;
26509 add_pathsep(*fnamep);
26510 }
26511 }
26512
26513 /* ":." - path relative to the current directory */
26514 /* ":~" - path relative to the home directory */
26515 /* ":8" - shortname path - postponed till after */
26516 while (src[*usedlen] == ':'
26517 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26518 {
26519 *usedlen += 2;
26520 if (c == '8')
26521 {
26522#ifdef WIN3264
26523 has_shortname = 1; /* Postpone this. */
26524#endif
26525 continue;
26526 }
26527 pbuf = NULL;
26528 /* Need full path first (use expand_env() to remove a "~/") */
26529 if (!has_fullname)
26530 {
26531 if (c == '.' && **fnamep == '~')
26532 p = pbuf = expand_env_save(*fnamep);
26533 else
26534 p = pbuf = FullName_save(*fnamep, FALSE);
26535 }
26536 else
26537 p = *fnamep;
26538
26539 has_fullname = 0;
26540
26541 if (p != NULL)
26542 {
26543 if (c == '.')
26544 {
26545 mch_dirname(dirname, MAXPATHL);
26546 s = shorten_fname(p, dirname);
26547 if (s != NULL)
26548 {
26549 *fnamep = s;
26550 if (pbuf != NULL)
26551 {
26552 vim_free(*bufp); /* free any allocated file name */
26553 *bufp = pbuf;
26554 pbuf = NULL;
26555 }
26556 }
26557 }
26558 else
26559 {
26560 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26561 /* Only replace it when it starts with '~' */
26562 if (*dirname == '~')
26563 {
26564 s = vim_strsave(dirname);
26565 if (s != NULL)
26566 {
26567 *fnamep = s;
26568 vim_free(*bufp);
26569 *bufp = s;
26570 }
26571 }
26572 }
26573 vim_free(pbuf);
26574 }
26575 }
26576
26577 tail = gettail(*fnamep);
26578 *fnamelen = (int)STRLEN(*fnamep);
26579
26580 /* ":h" - head, remove "/file_name", can be repeated */
26581 /* Don't remove the first "/" or "c:\" */
26582 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26583 {
26584 valid |= VALID_HEAD;
26585 *usedlen += 2;
26586 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026587 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026588 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026589 *fnamelen = (int)(tail - *fnamep);
26590#ifdef VMS
26591 if (*fnamelen > 0)
26592 *fnamelen += 1; /* the path separator is part of the path */
26593#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026594 if (*fnamelen == 0)
26595 {
26596 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26597 p = vim_strsave((char_u *)".");
26598 if (p == NULL)
26599 return -1;
26600 vim_free(*bufp);
26601 *bufp = *fnamep = tail = p;
26602 *fnamelen = 1;
26603 }
26604 else
26605 {
26606 while (tail > s && !after_pathsep(s, tail))
26607 mb_ptr_back(*fnamep, tail);
26608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026609 }
26610
26611 /* ":8" - shortname */
26612 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26613 {
26614 *usedlen += 2;
26615#ifdef WIN3264
26616 has_shortname = 1;
26617#endif
26618 }
26619
26620#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026621 /*
26622 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026623 */
26624 if (has_shortname)
26625 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026626 /* Copy the string if it is shortened by :h and when it wasn't copied
26627 * yet, because we are going to change it in place. Avoids changing
26628 * the buffer name for "%:8". */
26629 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026630 {
26631 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026632 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026633 return -1;
26634 vim_free(*bufp);
26635 *bufp = *fnamep = p;
26636 }
26637
26638 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026639 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026640 if (!has_fullname && !vim_isAbsName(*fnamep))
26641 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026642 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026643 return -1;
26644 }
26645 else
26646 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026647 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026648
Bram Moolenaardc935552011-08-17 15:23:23 +020026649 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026650 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026651 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026652 return -1;
26653
26654 if (l == 0)
26655 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026656 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026657 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026658 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026659 return -1;
26660 }
26661 *fnamelen = l;
26662 }
26663 }
26664#endif /* WIN3264 */
26665
26666 /* ":t" - tail, just the basename */
26667 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26668 {
26669 *usedlen += 2;
26670 *fnamelen -= (int)(tail - *fnamep);
26671 *fnamep = tail;
26672 }
26673
26674 /* ":e" - extension, can be repeated */
26675 /* ":r" - root, without extension, can be repeated */
26676 while (src[*usedlen] == ':'
26677 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26678 {
26679 /* find a '.' in the tail:
26680 * - for second :e: before the current fname
26681 * - otherwise: The last '.'
26682 */
26683 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26684 s = *fnamep - 2;
26685 else
26686 s = *fnamep + *fnamelen - 1;
26687 for ( ; s > tail; --s)
26688 if (s[0] == '.')
26689 break;
26690 if (src[*usedlen + 1] == 'e') /* :e */
26691 {
26692 if (s > tail)
26693 {
26694 *fnamelen += (int)(*fnamep - (s + 1));
26695 *fnamep = s + 1;
26696#ifdef VMS
26697 /* cut version from the extension */
26698 s = *fnamep + *fnamelen - 1;
26699 for ( ; s > *fnamep; --s)
26700 if (s[0] == ';')
26701 break;
26702 if (s > *fnamep)
26703 *fnamelen = s - *fnamep;
26704#endif
26705 }
26706 else if (*fnamep <= tail)
26707 *fnamelen = 0;
26708 }
26709 else /* :r */
26710 {
26711 if (s > tail) /* remove one extension */
26712 *fnamelen = (int)(s - *fnamep);
26713 }
26714 *usedlen += 2;
26715 }
26716
26717 /* ":s?pat?foo?" - substitute */
26718 /* ":gs?pat?foo?" - global substitute */
26719 if (src[*usedlen] == ':'
26720 && (src[*usedlen + 1] == 's'
26721 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26722 {
26723 char_u *str;
26724 char_u *pat;
26725 char_u *sub;
26726 int sep;
26727 char_u *flags;
26728 int didit = FALSE;
26729
26730 flags = (char_u *)"";
26731 s = src + *usedlen + 2;
26732 if (src[*usedlen + 1] == 'g')
26733 {
26734 flags = (char_u *)"g";
26735 ++s;
26736 }
26737
26738 sep = *s++;
26739 if (sep)
26740 {
26741 /* find end of pattern */
26742 p = vim_strchr(s, sep);
26743 if (p != NULL)
26744 {
26745 pat = vim_strnsave(s, (int)(p - s));
26746 if (pat != NULL)
26747 {
26748 s = p + 1;
26749 /* find end of substitution */
26750 p = vim_strchr(s, sep);
26751 if (p != NULL)
26752 {
26753 sub = vim_strnsave(s, (int)(p - s));
26754 str = vim_strnsave(*fnamep, *fnamelen);
26755 if (sub != NULL && str != NULL)
26756 {
26757 *usedlen = (int)(p + 1 - src);
26758 s = do_string_sub(str, pat, sub, flags);
26759 if (s != NULL)
26760 {
26761 *fnamep = s;
26762 *fnamelen = (int)STRLEN(s);
26763 vim_free(*bufp);
26764 *bufp = s;
26765 didit = TRUE;
26766 }
26767 }
26768 vim_free(sub);
26769 vim_free(str);
26770 }
26771 vim_free(pat);
26772 }
26773 }
26774 /* after using ":s", repeat all the modifiers */
26775 if (didit)
26776 goto repeat;
26777 }
26778 }
26779
Bram Moolenaar26df0922014-02-23 23:39:13 +010026780 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26781 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026782 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026783 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026784 if (c != NUL)
26785 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026786 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026787 if (c != NUL)
26788 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026789 if (p == NULL)
26790 return -1;
26791 vim_free(*bufp);
26792 *bufp = *fnamep = p;
26793 *fnamelen = (int)STRLEN(p);
26794 *usedlen += 2;
26795 }
26796
Bram Moolenaar071d4272004-06-13 20:20:40 +000026797 return valid;
26798}
26799
26800/*
26801 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26802 * "flags" can be "g" to do a global substitute.
26803 * Returns an allocated string, NULL for error.
26804 */
26805 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026806do_string_sub(
26807 char_u *str,
26808 char_u *pat,
26809 char_u *sub,
26810 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026811{
26812 int sublen;
26813 regmatch_T regmatch;
26814 int i;
26815 int do_all;
26816 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026817 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026818 garray_T ga;
26819 char_u *ret;
26820 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026821 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026822
26823 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26824 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026825 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026826
26827 ga_init2(&ga, 1, 200);
26828
26829 do_all = (flags[0] == 'g');
26830
26831 regmatch.rm_ic = p_ic;
26832 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26833 if (regmatch.regprog != NULL)
26834 {
26835 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026836 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026837 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26838 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026839 /* Skip empty match except for first match. */
26840 if (regmatch.startp[0] == regmatch.endp[0])
26841 {
26842 if (zero_width == regmatch.startp[0])
26843 {
26844 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026845 i = MB_PTR2LEN(tail);
26846 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26847 (size_t)i);
26848 ga.ga_len += i;
26849 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026850 continue;
26851 }
26852 zero_width = regmatch.startp[0];
26853 }
26854
Bram Moolenaar071d4272004-06-13 20:20:40 +000026855 /*
26856 * Get some space for a temporary buffer to do the substitution
26857 * into. It will contain:
26858 * - The text up to where the match is.
26859 * - The substituted text.
26860 * - The text after the match.
26861 */
26862 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026863 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026864 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26865 {
26866 ga_clear(&ga);
26867 break;
26868 }
26869
26870 /* copy the text up to where the match is */
26871 i = (int)(regmatch.startp[0] - tail);
26872 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26873 /* add the substituted text */
26874 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26875 + ga.ga_len + i, TRUE, TRUE, FALSE);
26876 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026877 tail = regmatch.endp[0];
26878 if (*tail == NUL)
26879 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026880 if (!do_all)
26881 break;
26882 }
26883
26884 if (ga.ga_data != NULL)
26885 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26886
Bram Moolenaar473de612013-06-08 18:19:48 +020026887 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026888 }
26889
26890 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26891 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026892 if (p_cpo == empty_option)
26893 p_cpo = save_cpo;
26894 else
26895 /* Darn, evaluating {sub} expression changed the value. */
26896 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026897
26898 return ret;
26899}
26900
26901#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */