blob: e7fbeaa55887a0d35d83249eb6757df1e4e5e1ff [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000113
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100114#define NAMESPACE_CHAR (char_u *)"abglstvw"
115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000128 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 */
130static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131#define COPYID_INC 2
132#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133
Bram Moolenaar8502c702014-06-17 12:51:16 +0200134/* Abort conversion to string after a recursion error. */
135static int did_echo_string_emsg = FALSE;
136
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100212/* List heads for garbage collection. Although there can be a reference loop
213 * from partial to dict to partial, we don't need to keep track of the partial,
214 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100300 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
302} vimvars[VV_LEN] =
303{
304 /*
305 * The order here must match the VV_ defines in vim.h!
306 * Initializing a union does not work, leave tv.vval empty to get zero's.
307 */
308 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("count1", VAR_NUMBER), VV_RO},
310 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
311 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
312 {VV_NAME("warningmsg", VAR_STRING), 0},
313 {VV_NAME("statusmsg", VAR_STRING), 0},
314 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
316 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
318 {VV_NAME("termresponse", VAR_STRING), VV_RO},
319 {VV_NAME("fname", VAR_STRING), VV_RO},
320 {VV_NAME("lang", VAR_STRING), VV_RO},
321 {VV_NAME("lc_time", VAR_STRING), VV_RO},
322 {VV_NAME("ctype", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
325 {VV_NAME("fname_in", VAR_STRING), VV_RO},
326 {VV_NAME("fname_out", VAR_STRING), VV_RO},
327 {VV_NAME("fname_new", VAR_STRING), VV_RO},
328 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
329 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
330 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
333 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("progname", VAR_STRING), VV_RO},
335 {VV_NAME("servername", VAR_STRING), VV_RO},
336 {VV_NAME("dying", VAR_NUMBER), VV_RO},
337 {VV_NAME("exception", VAR_STRING), VV_RO},
338 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
339 {VV_NAME("register", VAR_STRING), VV_RO},
340 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
341 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000342 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
343 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000344 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000345 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
346 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000347 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000352 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000353 {VV_NAME("swapname", VAR_STRING), VV_RO},
354 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000355 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200356 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000357 {VV_NAME("mouse_win", VAR_NUMBER), 0},
358 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
359 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000360 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100362 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000363 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200364 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200365 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200366 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200367 {VV_NAME("option_new", VAR_STRING), VV_RO},
368 {VV_NAME("option_old", VAR_STRING), VV_RO},
369 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100370 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371 {VV_NAME("false", VAR_SPECIAL), VV_RO},
372 {VV_NAME("true", VAR_SPECIAL), VV_RO},
373 {VV_NAME("null", VAR_SPECIAL), VV_RO},
374 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100375 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000376};
377
378/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000379#define vv_type vv_di.di_tv.v_type
380#define vv_nr vv_di.di_tv.vval.v_number
381#define vv_float vv_di.di_tv.vval.v_float
382#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000383#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200384#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000386
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200387static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000388#define vimvarht vimvardict.dv_hashtab
389
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100390static void prepare_vimvar(int idx, typval_T *save_tv);
391static void restore_vimvar(int idx, typval_T *save_tv);
392static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
393static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
394static char_u *skip_var_one(char_u *arg);
395static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
396static void list_glob_vars(int *first);
397static void list_buf_vars(int *first);
398static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000401#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100402static void list_vim_vars(int *first);
403static void list_script_vars(int *first);
404static void list_func_vars(int *first);
405static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
406static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
407static int check_changedtick(char_u *arg);
408static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
409static void clear_lval(lval_T *lp);
410static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
411static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
412static void list_fix_watch(list_T *l, listitem_T *item);
413static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
414static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
415static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
416static void item_lock(typval_T *tv, int deep, int lock);
417static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100419static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
420static int eval1(char_u **arg, typval_T *rettv, int evaluate);
421static int eval2(char_u **arg, typval_T *rettv, int evaluate);
422static int eval3(char_u **arg, typval_T *rettv, int evaluate);
423static int eval4(char_u **arg, typval_T *rettv, int evaluate);
424static int eval5(char_u **arg, typval_T *rettv, int evaluate);
425static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
426static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000427
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100428static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
429static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
433static long list_len(list_T *l);
434static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
435static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
436static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
437static long list_find_nr(list_T *l, long idx, int *errorp);
438static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
440static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
441static list_T *list_copy(list_T *orig, int deep, int copyID);
442static char_u *list2string(typval_T *tv, int copyID);
443static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
444static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
445static int free_unref_items(int copyID);
446static dictitem_T *dictitem_copy(dictitem_T *org);
447static void dictitem_remove(dict_T *dict, dictitem_T *item);
448static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
449static long dict_len(dict_T *d);
450static char_u *dict2string(typval_T *tv, int copyID);
451static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
452static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
454static char_u *string_quote(char_u *str, int function);
455static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
456static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100457static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
458static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static void emsg_funcname(char *ermsg, char_u *name);
460static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000461
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100463static void f_abs(typval_T *argvars, typval_T *rettv);
464static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100466static void f_add(typval_T *argvars, typval_T *rettv);
467static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
468static void f_and(typval_T *argvars, typval_T *rettv);
469static void f_append(typval_T *argvars, typval_T *rettv);
470static void f_argc(typval_T *argvars, typval_T *rettv);
471static void f_argidx(typval_T *argvars, typval_T *rettv);
472static void f_arglistid(typval_T *argvars, typval_T *rettv);
473static void f_argv(typval_T *argvars, typval_T *rettv);
474static void f_assert_equal(typval_T *argvars, typval_T *rettv);
475static void f_assert_exception(typval_T *argvars, typval_T *rettv);
476static void f_assert_fails(typval_T *argvars, typval_T *rettv);
477static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200478static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200479static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
480static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100481static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_asin(typval_T *argvars, typval_T *rettv);
484static void f_atan(typval_T *argvars, typval_T *rettv);
485static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100487static void f_browse(typval_T *argvars, typval_T *rettv);
488static void f_browsedir(typval_T *argvars, typval_T *rettv);
489static void f_bufexists(typval_T *argvars, typval_T *rettv);
490static void f_buflisted(typval_T *argvars, typval_T *rettv);
491static void f_bufloaded(typval_T *argvars, typval_T *rettv);
492static void f_bufname(typval_T *argvars, typval_T *rettv);
493static void f_bufnr(typval_T *argvars, typval_T *rettv);
494static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
495static void f_byte2line(typval_T *argvars, typval_T *rettv);
496static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
497static void f_byteidx(typval_T *argvars, typval_T *rettv);
498static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
499static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100501static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100504static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100505static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
506static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100507static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100508static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100509static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100510static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
512static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100513static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100514static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
516static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100517static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100518static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100519#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_changenr(typval_T *argvars, typval_T *rettv);
521static void f_char2nr(typval_T *argvars, typval_T *rettv);
522static void f_cindent(typval_T *argvars, typval_T *rettv);
523static void f_clearmatches(typval_T *argvars, typval_T *rettv);
524static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000525#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_complete(typval_T *argvars, typval_T *rettv);
527static void f_complete_add(typval_T *argvars, typval_T *rettv);
528static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_confirm(typval_T *argvars, typval_T *rettv);
531static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_cos(typval_T *argvars, typval_T *rettv);
534static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000535#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100536static void f_count(typval_T *argvars, typval_T *rettv);
537static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
538static void f_cursor(typval_T *argsvars, typval_T *rettv);
539static void f_deepcopy(typval_T *argvars, typval_T *rettv);
540static void f_delete(typval_T *argvars, typval_T *rettv);
541static void f_did_filetype(typval_T *argvars, typval_T *rettv);
542static void f_diff_filler(typval_T *argvars, typval_T *rettv);
543static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100544static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100545static void f_empty(typval_T *argvars, typval_T *rettv);
546static void f_escape(typval_T *argvars, typval_T *rettv);
547static void f_eval(typval_T *argvars, typval_T *rettv);
548static void f_eventhandler(typval_T *argvars, typval_T *rettv);
549static void f_executable(typval_T *argvars, typval_T *rettv);
550static void f_exepath(typval_T *argvars, typval_T *rettv);
551static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200552#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100553static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200554#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_expand(typval_T *argvars, typval_T *rettv);
556static void f_extend(typval_T *argvars, typval_T *rettv);
557static void f_feedkeys(typval_T *argvars, typval_T *rettv);
558static void f_filereadable(typval_T *argvars, typval_T *rettv);
559static void f_filewritable(typval_T *argvars, typval_T *rettv);
560static void f_filter(typval_T *argvars, typval_T *rettv);
561static void f_finddir(typval_T *argvars, typval_T *rettv);
562static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000563#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_float2nr(typval_T *argvars, typval_T *rettv);
565static void f_floor(typval_T *argvars, typval_T *rettv);
566static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000567#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100568static void f_fnameescape(typval_T *argvars, typval_T *rettv);
569static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
570static void f_foldclosed(typval_T *argvars, typval_T *rettv);
571static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
572static void f_foldlevel(typval_T *argvars, typval_T *rettv);
573static void f_foldtext(typval_T *argvars, typval_T *rettv);
574static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
575static void f_foreground(typval_T *argvars, typval_T *rettv);
576static void f_function(typval_T *argvars, typval_T *rettv);
577static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
578static void f_get(typval_T *argvars, typval_T *rettv);
579static void f_getbufline(typval_T *argvars, typval_T *rettv);
580static void f_getbufvar(typval_T *argvars, typval_T *rettv);
581static void f_getchar(typval_T *argvars, typval_T *rettv);
582static void f_getcharmod(typval_T *argvars, typval_T *rettv);
583static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
584static void f_getcmdline(typval_T *argvars, typval_T *rettv);
585static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
586static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
587static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
588static void f_getcwd(typval_T *argvars, typval_T *rettv);
589static void f_getfontname(typval_T *argvars, typval_T *rettv);
590static void f_getfperm(typval_T *argvars, typval_T *rettv);
591static void f_getfsize(typval_T *argvars, typval_T *rettv);
592static void f_getftime(typval_T *argvars, typval_T *rettv);
593static void f_getftype(typval_T *argvars, typval_T *rettv);
594static void f_getline(typval_T *argvars, typval_T *rettv);
595static void f_getmatches(typval_T *argvars, typval_T *rettv);
596static void f_getpid(typval_T *argvars, typval_T *rettv);
597static void f_getcurpos(typval_T *argvars, typval_T *rettv);
598static void f_getpos(typval_T *argvars, typval_T *rettv);
599static void f_getqflist(typval_T *argvars, typval_T *rettv);
600static void f_getreg(typval_T *argvars, typval_T *rettv);
601static void f_getregtype(typval_T *argvars, typval_T *rettv);
602static void f_gettabvar(typval_T *argvars, typval_T *rettv);
603static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
604static void f_getwinposx(typval_T *argvars, typval_T *rettv);
605static void f_getwinposy(typval_T *argvars, typval_T *rettv);
606static void f_getwinvar(typval_T *argvars, typval_T *rettv);
607static void f_glob(typval_T *argvars, typval_T *rettv);
608static void f_globpath(typval_T *argvars, typval_T *rettv);
609static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
610static void f_has(typval_T *argvars, typval_T *rettv);
611static void f_has_key(typval_T *argvars, typval_T *rettv);
612static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
613static void f_hasmapto(typval_T *argvars, typval_T *rettv);
614static void f_histadd(typval_T *argvars, typval_T *rettv);
615static void f_histdel(typval_T *argvars, typval_T *rettv);
616static void f_histget(typval_T *argvars, typval_T *rettv);
617static void f_histnr(typval_T *argvars, typval_T *rettv);
618static void f_hlID(typval_T *argvars, typval_T *rettv);
619static void f_hlexists(typval_T *argvars, typval_T *rettv);
620static void f_hostname(typval_T *argvars, typval_T *rettv);
621static void f_iconv(typval_T *argvars, typval_T *rettv);
622static void f_indent(typval_T *argvars, typval_T *rettv);
623static void f_index(typval_T *argvars, typval_T *rettv);
624static void f_input(typval_T *argvars, typval_T *rettv);
625static void f_inputdialog(typval_T *argvars, typval_T *rettv);
626static void f_inputlist(typval_T *argvars, typval_T *rettv);
627static void f_inputrestore(typval_T *argvars, typval_T *rettv);
628static void f_inputsave(typval_T *argvars, typval_T *rettv);
629static void f_inputsecret(typval_T *argvars, typval_T *rettv);
630static void f_insert(typval_T *argvars, typval_T *rettv);
631static void f_invert(typval_T *argvars, typval_T *rettv);
632static void f_isdirectory(typval_T *argvars, typval_T *rettv);
633static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100634#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
635static void f_isnan(typval_T *argvars, typval_T *rettv);
636#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100637static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100638#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100639static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100640static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100641static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100642static void f_job_start(typval_T *argvars, typval_T *rettv);
643static void f_job_stop(typval_T *argvars, typval_T *rettv);
644static void f_job_status(typval_T *argvars, typval_T *rettv);
645#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100647static void f_js_decode(typval_T *argvars, typval_T *rettv);
648static void f_js_encode(typval_T *argvars, typval_T *rettv);
649static void f_json_decode(typval_T *argvars, typval_T *rettv);
650static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_keys(typval_T *argvars, typval_T *rettv);
652static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
653static void f_len(typval_T *argvars, typval_T *rettv);
654static void f_libcall(typval_T *argvars, typval_T *rettv);
655static void f_libcallnr(typval_T *argvars, typval_T *rettv);
656static void f_line(typval_T *argvars, typval_T *rettv);
657static void f_line2byte(typval_T *argvars, typval_T *rettv);
658static void f_lispindent(typval_T *argvars, typval_T *rettv);
659static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000660#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_log(typval_T *argvars, typval_T *rettv);
662static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200664#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200666#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_map(typval_T *argvars, typval_T *rettv);
668static void f_maparg(typval_T *argvars, typval_T *rettv);
669static void f_mapcheck(typval_T *argvars, typval_T *rettv);
670static void f_match(typval_T *argvars, typval_T *rettv);
671static void f_matchadd(typval_T *argvars, typval_T *rettv);
672static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
673static void f_matcharg(typval_T *argvars, typval_T *rettv);
674static void f_matchdelete(typval_T *argvars, typval_T *rettv);
675static void f_matchend(typval_T *argvars, typval_T *rettv);
676static void f_matchlist(typval_T *argvars, typval_T *rettv);
677static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200678static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_max(typval_T *argvars, typval_T *rettv);
680static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000681#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000683#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100684static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100685#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100687#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
689static void f_nr2char(typval_T *argvars, typval_T *rettv);
690static void f_or(typval_T *argvars, typval_T *rettv);
691static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100692#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100694#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
699static void f_printf(typval_T *argvars, typval_T *rettv);
700static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200701#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200703#endif
704#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_range(typval_T *argvars, typval_T *rettv);
708static void f_readfile(typval_T *argvars, typval_T *rettv);
709static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100710#ifdef FEAT_FLOAT
711static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
712#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_reltimestr(typval_T *argvars, typval_T *rettv);
714static void f_remote_expr(typval_T *argvars, typval_T *rettv);
715static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
716static void f_remote_peek(typval_T *argvars, typval_T *rettv);
717static void f_remote_read(typval_T *argvars, typval_T *rettv);
718static void f_remote_send(typval_T *argvars, typval_T *rettv);
719static void f_remove(typval_T *argvars, typval_T *rettv);
720static void f_rename(typval_T *argvars, typval_T *rettv);
721static void f_repeat(typval_T *argvars, typval_T *rettv);
722static void f_resolve(typval_T *argvars, typval_T *rettv);
723static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000724#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100725static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000726#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_screenattr(typval_T *argvars, typval_T *rettv);
728static void f_screenchar(typval_T *argvars, typval_T *rettv);
729static void f_screencol(typval_T *argvars, typval_T *rettv);
730static void f_screenrow(typval_T *argvars, typval_T *rettv);
731static void f_search(typval_T *argvars, typval_T *rettv);
732static void f_searchdecl(typval_T *argvars, typval_T *rettv);
733static void f_searchpair(typval_T *argvars, typval_T *rettv);
734static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
735static void f_searchpos(typval_T *argvars, typval_T *rettv);
736static void f_server2client(typval_T *argvars, typval_T *rettv);
737static void f_serverlist(typval_T *argvars, typval_T *rettv);
738static void f_setbufvar(typval_T *argvars, typval_T *rettv);
739static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
740static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100741static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100742static void f_setline(typval_T *argvars, typval_T *rettv);
743static void f_setloclist(typval_T *argvars, typval_T *rettv);
744static void f_setmatches(typval_T *argvars, typval_T *rettv);
745static void f_setpos(typval_T *argvars, typval_T *rettv);
746static void f_setqflist(typval_T *argvars, typval_T *rettv);
747static void f_setreg(typval_T *argvars, typval_T *rettv);
748static void f_settabvar(typval_T *argvars, typval_T *rettv);
749static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
750static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100751#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100753#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_shellescape(typval_T *argvars, typval_T *rettv);
755static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
756static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sin(typval_T *argvars, typval_T *rettv);
759static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000760#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sort(typval_T *argvars, typval_T *rettv);
762static void f_soundfold(typval_T *argvars, typval_T *rettv);
763static void f_spellbadword(typval_T *argvars, typval_T *rettv);
764static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
765static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_sqrt(typval_T *argvars, typval_T *rettv);
768static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000769#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_str2nr(typval_T *argvars, typval_T *rettv);
771static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000772#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000774#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_stridx(typval_T *argvars, typval_T *rettv);
776static void f_string(typval_T *argvars, typval_T *rettv);
777static void f_strlen(typval_T *argvars, typval_T *rettv);
778static void f_strpart(typval_T *argvars, typval_T *rettv);
779static void f_strridx(typval_T *argvars, typval_T *rettv);
780static void f_strtrans(typval_T *argvars, typval_T *rettv);
781static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
782static void f_strwidth(typval_T *argvars, typval_T *rettv);
783static void f_submatch(typval_T *argvars, typval_T *rettv);
784static void f_substitute(typval_T *argvars, typval_T *rettv);
785static void f_synID(typval_T *argvars, typval_T *rettv);
786static void f_synIDattr(typval_T *argvars, typval_T *rettv);
787static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
788static void f_synstack(typval_T *argvars, typval_T *rettv);
789static void f_synconcealed(typval_T *argvars, typval_T *rettv);
790static void f_system(typval_T *argvars, typval_T *rettv);
791static void f_systemlist(typval_T *argvars, typval_T *rettv);
792static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
793static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
794static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
795static void f_taglist(typval_T *argvars, typval_T *rettv);
796static void f_tagfiles(typval_T *argvars, typval_T *rettv);
797static void f_tempname(typval_T *argvars, typval_T *rettv);
798static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200799#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_tan(typval_T *argvars, typval_T *rettv);
801static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200802#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100803#ifdef FEAT_TIMERS
804static void f_timer_start(typval_T *argvars, typval_T *rettv);
805static void f_timer_stop(typval_T *argvars, typval_T *rettv);
806#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100807static void f_tolower(typval_T *argvars, typval_T *rettv);
808static void f_toupper(typval_T *argvars, typval_T *rettv);
809static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000810#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100811static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000812#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100813static void f_type(typval_T *argvars, typval_T *rettv);
814static void f_undofile(typval_T *argvars, typval_T *rettv);
815static void f_undotree(typval_T *argvars, typval_T *rettv);
816static void f_uniq(typval_T *argvars, typval_T *rettv);
817static void f_values(typval_T *argvars, typval_T *rettv);
818static void f_virtcol(typval_T *argvars, typval_T *rettv);
819static void f_visualmode(typval_T *argvars, typval_T *rettv);
820static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100821static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100822static void f_win_getid(typval_T *argvars, typval_T *rettv);
823static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
824static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
825static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static void f_winbufnr(typval_T *argvars, typval_T *rettv);
827static void f_wincol(typval_T *argvars, typval_T *rettv);
828static void f_winheight(typval_T *argvars, typval_T *rettv);
829static void f_winline(typval_T *argvars, typval_T *rettv);
830static void f_winnr(typval_T *argvars, typval_T *rettv);
831static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
832static void f_winrestview(typval_T *argvars, typval_T *rettv);
833static void f_winsaveview(typval_T *argvars, typval_T *rettv);
834static void f_winwidth(typval_T *argvars, typval_T *rettv);
835static void f_writefile(typval_T *argvars, typval_T *rettv);
836static void f_wordcount(typval_T *argvars, typval_T *rettv);
837static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000838
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
840static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
841static int get_env_len(char_u **arg);
842static int get_id_len(char_u **arg);
843static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
844static 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 +0000845#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
846#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
847 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100848static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
849static int eval_isnamec(int c);
850static int eval_isnamec1(int c);
851static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
852static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100853static typval_T *alloc_string_tv(char_u *string);
854static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100855#ifdef FEAT_FLOAT
856static float_T get_tv_float(typval_T *varp);
857#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100858static linenr_T get_tv_lnum(typval_T *argvars);
859static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100860static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
861static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
862static hashtab_T *find_var_ht(char_u *name, char_u **varname);
863static funccall_T *get_funccal(void);
864static void vars_clear_ext(hashtab_T *ht, int free_val);
865static void delete_var(hashtab_T *ht, hashitem_T *hi);
866static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
867static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
868static void set_var(char_u *name, typval_T *varp, int copy);
869static int var_check_ro(int flags, char_u *name, int use_gettext);
870static int var_check_fixed(int flags, char_u *name, int use_gettext);
871static int var_check_func_name(char_u *name, int new_var);
872static int valid_varname(char_u *varname);
873static int tv_check_lock(int lock, char_u *name, int use_gettext);
874static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
875static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100876static 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 +0100877static int eval_fname_script(char_u *p);
878static int eval_fname_sid(char_u *p);
879static void list_func_head(ufunc_T *fp, int indent);
880static ufunc_T *find_func(char_u *name);
881static int function_exists(char_u *name);
882static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000883#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884static void func_do_profile(ufunc_T *fp);
885static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
886static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000887static int
888# ifdef __BORLANDC__
889 _RTLENTRYF
890# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100891 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000892static int
893# ifdef __BORLANDC__
894 _RTLENTRYF
895# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100896 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100898static int script_autoload(char_u *name, int reload);
899static char_u *autoload_name(char_u *name);
900static void cat_func_name(char_u *buf, ufunc_T *fp);
901static void func_free(ufunc_T *fp);
902static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
903static int can_free_funccal(funccall_T *fc, int copyID) ;
904static void free_funccal(funccall_T *fc, int free_val);
905static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
906static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
907static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
908static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
909static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
910static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
911static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
912static int write_list(FILE *fd, list_T *list, int binary);
913static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000914
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200915
916#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100917static int compare_func_name(const void *s1, const void *s2);
918static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200919#endif
920
Bram Moolenaar33570922005-01-25 22:26:29 +0000921/*
922 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000923 */
924 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100925eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000926{
Bram Moolenaar33570922005-01-25 22:26:29 +0000927 int i;
928 struct vimvar *p;
929
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200930 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
931 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200932 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000933 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000934 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000935
936 for (i = 0; i < VV_LEN; ++i)
937 {
938 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200939 if (STRLEN(p->vv_name) > 16)
940 {
941 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
942 getout(1);
943 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000944 STRCPY(p->vv_di.di_key, p->vv_name);
945 if (p->vv_flags & VV_RO)
946 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
947 else if (p->vv_flags & VV_RO_SBX)
948 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
949 else
950 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000951
952 /* add to v: scope dict, unless the value is not always available */
953 if (p->vv_type != VAR_UNKNOWN)
954 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000956 /* add to compat scope dict */
957 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000958 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100959 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
960
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000961 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100962 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200963 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100964 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100965
966 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
967 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
968 set_vim_var_nr(VV_NONE, VVAL_NONE);
969 set_vim_var_nr(VV_NULL, VVAL_NULL);
970
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200971 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200972
973#ifdef EBCDIC
974 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100975 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200976 */
977 sortFunctions();
978#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000979}
980
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000981#if defined(EXITFREE) || defined(PROTO)
982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100983eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984{
985 int i;
986 struct vimvar *p;
987
988 for (i = 0; i < VV_LEN; ++i)
989 {
990 p = &vimvars[i];
991 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000992 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000993 vim_free(p->vv_str);
994 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000995 }
996 else if (p->vv_di.di_tv.v_type == VAR_LIST)
997 {
998 list_unref(p->vv_list);
999 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001000 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001 }
1002 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001003 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004 hash_clear(&compat_hashtab);
1005
Bram Moolenaard9fba312005-06-26 22:34:35 +00001006 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001007# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001008 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001009# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010
1011 /* global variables */
1012 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001013
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001014 /* autoloaded script names */
1015 ga_clear_strings(&ga_loaded);
1016
Bram Moolenaarcca74132013-09-25 21:00:28 +02001017 /* Script-local variables. First clear all the variables and in a second
1018 * loop free the scriptvar_T, because a variable in one script might hold
1019 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001020 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001021 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001022 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001023 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001024 ga_clear(&ga_scripts);
1025
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001026 /* unreferenced lists and dicts */
1027 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001028
1029 /* functions */
1030 free_all_functions();
1031 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001032}
1033#endif
1034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 * Return the name of the executed function.
1037 */
1038 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001039func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042}
1043
1044/*
1045 * Return the address holding the next breakpoint line for a funccall cookie.
1046 */
1047 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001048func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar33570922005-01-25 22:26:29 +00001050 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053/*
1054 * Return the address holding the debug tick for a funccall cookie.
1055 */
1056 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001057func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060}
1061
1062/*
1063 * Return the nesting level for a funccall cookie.
1064 */
1065 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001066func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
Bram Moolenaar33570922005-01-25 22:26:29 +00001068 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069}
1070
1071/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001072funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001074/* pointer to list of previously used funccal, still around because some
1075 * item in it is still being used. */
1076funccall_T *previous_funccal = NULL;
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078/*
1079 * Return TRUE when a function was ended by a ":return" command.
1080 */
1081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001082current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 return current_funccal->returned;
1085}
1086
1087
1088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 * Set an internal variable to a string value. Creates the variable if it does
1090 * not already exist.
1091 */
1092 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001093set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001095 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001096 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
1098 val = vim_strsave(value);
1099 if (val != NULL)
1100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001101 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001102 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001104 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001105 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 }
1108}
1109
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001111static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112static char_u *redir_endp = NULL;
1113static char_u *redir_varname = NULL;
1114
1115/*
1116 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001117 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 * Returns OK if successfully completed the setup. FAIL otherwise.
1119 */
1120 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001121var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122{
1123 int save_emsg;
1124 int err;
1125 typval_T tv;
1126
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001127 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001128 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 {
1130 EMSG(_(e_invarg));
1131 return FAIL;
1132 }
1133
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 redir_varname = vim_strsave(name);
1136 if (redir_varname == NULL)
1137 return FAIL;
1138
1139 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1140 if (redir_lval == NULL)
1141 {
1142 var_redir_stop();
1143 return FAIL;
1144 }
1145
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001146 /* The output is stored in growarray "redir_ga" until redirection ends. */
1147 ga_init2(&redir_ga, (int)sizeof(char), 500);
1148
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001150 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001151 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1153 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001154 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 if (redir_endp != NULL && *redir_endp != NUL)
1156 /* Trailing characters are present after the variable name */
1157 EMSG(_(e_trailing));
1158 else
1159 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001160 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 var_redir_stop();
1162 return FAIL;
1163 }
1164
1165 /* check if we can write to the variable: set it to or append an empty
1166 * string */
1167 save_emsg = did_emsg;
1168 did_emsg = FALSE;
1169 tv.v_type = VAR_STRING;
1170 tv.vval.v_string = (char_u *)"";
1171 if (append)
1172 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1173 else
1174 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001175 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001177 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 if (err)
1179 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 var_redir_stop();
1182 return FAIL;
1183 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 * Append "value[value_len]" to the variable set by var_redir_start().
1190 * The actual appending is postponed until redirection ends, because the value
1191 * appended may in fact be the string we write to, changing it may cause freed
1192 * memory to be used:
1193 * :redir => foo
1194 * :let foo
1195 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 */
1197 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001198var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001200 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201
1202 if (redir_lval == NULL)
1203 return;
1204
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001205 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001206 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001208 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001210 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001211 {
1212 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001213 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001214 }
1215 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217}
1218
1219/*
1220 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001221 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001222 */
1223 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001224var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001225{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226 typval_T tv;
1227
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228 if (redir_lval != NULL)
1229 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001230 /* If there was no error: assign the text to the variable. */
1231 if (redir_endp != NULL)
1232 {
1233 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1234 tv.v_type = VAR_STRING;
1235 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001236 /* Call get_lval() again, if it's inside a Dict or List it may
1237 * have changed. */
1238 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001239 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001240 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1241 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1242 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001243 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001244
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001245 /* free the collected output */
1246 vim_free(redir_ga.ga_data);
1247 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001248
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001249 vim_free(redir_lval);
1250 redir_lval = NULL;
1251 }
1252 vim_free(redir_varname);
1253 redir_varname = NULL;
1254}
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256# if defined(FEAT_MBYTE) || defined(PROTO)
1257 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001258eval_charconvert(
1259 char_u *enc_from,
1260 char_u *enc_to,
1261 char_u *fname_from,
1262 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263{
1264 int err = FALSE;
1265
1266 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1267 set_vim_var_string(VV_CC_TO, enc_to, -1);
1268 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1269 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1270 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1271 err = TRUE;
1272 set_vim_var_string(VV_CC_FROM, NULL, -1);
1273 set_vim_var_string(VV_CC_TO, NULL, -1);
1274 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1275 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1276
1277 if (err)
1278 return FAIL;
1279 return OK;
1280}
1281# endif
1282
1283# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1284 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001285eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 int err = FALSE;
1288
1289 set_vim_var_string(VV_FNAME_IN, fname, -1);
1290 set_vim_var_string(VV_CMDARG, args, -1);
1291 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1292 err = TRUE;
1293 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1294 set_vim_var_string(VV_CMDARG, NULL, -1);
1295
1296 if (err)
1297 {
1298 mch_remove(fname);
1299 return FAIL;
1300 }
1301 return OK;
1302}
1303# endif
1304
1305# if defined(FEAT_DIFF) || defined(PROTO)
1306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001307eval_diff(
1308 char_u *origfile,
1309 char_u *newfile,
1310 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 int err = FALSE;
1313
1314 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1315 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1316 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1317 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1318 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1319 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1320 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1321}
1322
1323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001324eval_patch(
1325 char_u *origfile,
1326 char_u *difffile,
1327 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 int err;
1330
1331 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1332 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1333 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1334 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1335 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1336 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1337 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1338}
1339# endif
1340
1341/*
1342 * Top level evaluation function, returning a boolean.
1343 * Sets "error" to TRUE if there was an error.
1344 * Return TRUE or FALSE.
1345 */
1346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001347eval_to_bool(
1348 char_u *arg,
1349 int *error,
1350 char_u **nextcmd,
1351 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
Bram Moolenaar33570922005-01-25 22:26:29 +00001353 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 int retval = FALSE;
1355
1356 if (skip)
1357 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 else
1361 {
1362 *error = FALSE;
1363 if (!skip)
1364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001365 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368 }
1369 if (skip)
1370 --emsg_skip;
1371
1372 return retval;
1373}
1374
1375/*
1376 * Top level evaluation function, returning a string. If "skip" is TRUE,
1377 * only parsing to "nextcmd" is done, without reporting errors. Return
1378 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1379 */
1380 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001381eval_to_string_skip(
1382 char_u *arg,
1383 char_u **nextcmd,
1384 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
Bram Moolenaar33570922005-01-25 22:26:29 +00001386 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 char_u *retval;
1388
1389 if (skip)
1390 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 retval = NULL;
1393 else
1394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 retval = vim_strsave(get_tv_string(&tv));
1396 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 if (skip)
1399 --emsg_skip;
1400
1401 return retval;
1402}
1403
1404/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001405 * Skip over an expression at "*pp".
1406 * Return FAIL for an error, OK otherwise.
1407 */
1408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001409skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001412
1413 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001415}
1416
1417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001419 * When "convert" is TRUE convert a List into a sequence of lines and convert
1420 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 * Return pointer to allocated memory, or NULL for failure.
1422 */
1423 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001424eval_to_string(
1425 char_u *arg,
1426 char_u **nextcmd,
1427 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
Bram Moolenaar33570922005-01-25 22:26:29 +00001429 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001431 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001432#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001433 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001436 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 retval = NULL;
1438 else
1439 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001440 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001441 {
1442 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001443 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001444 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001445 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001446 if (tv.vval.v_list->lv_len > 0)
1447 ga_append(&ga, NL);
1448 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001449 ga_append(&ga, NUL);
1450 retval = (char_u *)ga.ga_data;
1451 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001452#ifdef FEAT_FLOAT
1453 else if (convert && tv.v_type == VAR_FLOAT)
1454 {
1455 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1456 retval = vim_strsave(numbuf);
1457 }
1458#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001459 else
1460 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001461 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463
1464 return retval;
1465}
1466
1467/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001468 * Call eval_to_string() without using current local variables and using
1469 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 */
1471 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001472eval_to_string_safe(
1473 char_u *arg,
1474 char_u **nextcmd,
1475 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 char_u *retval;
1478 void *save_funccalp;
1479
1480 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001481 if (use_sandbox)
1482 ++sandbox;
1483 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001484 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001485 if (use_sandbox)
1486 --sandbox;
1487 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 restore_funccal(save_funccalp);
1489 return retval;
1490}
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492/*
1493 * Top level evaluation function, returning a number.
1494 * Evaluates "expr" silently.
1495 * Returns -1 for an error.
1496 */
1497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001498eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaar33570922005-01-25 22:26:29 +00001500 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001502 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
1504 ++emsg_off;
1505
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001506 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 retval = -1;
1508 else
1509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001510 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 --emsg_off;
1514
1515 return retval;
1516}
1517
Bram Moolenaara40058a2005-07-11 22:42:07 +00001518/*
1519 * Prepare v: variable "idx" to be used.
1520 * Save the current typeval in "save_tv".
1521 * When not used yet add the variable to the v: hashtable.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525{
1526 *save_tv = vimvars[idx].vv_tv;
1527 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1528 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1529}
1530
1531/*
1532 * Restore v: variable "idx" to typeval "save_tv".
1533 * When no longer defined, remove the variable from the v: hashtable.
1534 */
1535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001537{
1538 hashitem_T *hi;
1539
Bram Moolenaara40058a2005-07-11 22:42:07 +00001540 vimvars[idx].vv_tv = *save_tv;
1541 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1542 {
1543 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1544 if (HASHITEM_EMPTY(hi))
1545 EMSG2(_(e_intern2), "restore_vimvar()");
1546 else
1547 hash_remove(&vimvarht, hi);
1548 }
1549}
1550
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001551#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001552/*
1553 * Evaluate an expression to a list with suggestions.
1554 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001555 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001556 */
1557 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001558eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001559{
1560 typval_T save_val;
1561 typval_T rettv;
1562 list_T *list = NULL;
1563 char_u *p = skipwhite(expr);
1564
1565 /* Set "v:val" to the bad word. */
1566 prepare_vimvar(VV_VAL, &save_val);
1567 vimvars[VV_VAL].vv_type = VAR_STRING;
1568 vimvars[VV_VAL].vv_str = badword;
1569 if (p_verbose == 0)
1570 ++emsg_off;
1571
1572 if (eval1(&p, &rettv, TRUE) == OK)
1573 {
1574 if (rettv.v_type != VAR_LIST)
1575 clear_tv(&rettv);
1576 else
1577 list = rettv.vval.v_list;
1578 }
1579
1580 if (p_verbose == 0)
1581 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001582 restore_vimvar(VV_VAL, &save_val);
1583
1584 return list;
1585}
1586
1587/*
1588 * "list" is supposed to contain two items: a word and a number. Return the
1589 * word in "pp" and the number as the return value.
1590 * Return -1 if anything isn't right.
1591 * Used to get the good word and score from the eval_spell_expr() result.
1592 */
1593 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001595{
1596 listitem_T *li;
1597
1598 li = list->lv_first;
1599 if (li == NULL)
1600 return -1;
1601 *pp = get_tv_string(&li->li_tv);
1602
1603 li = li->li_next;
1604 if (li == NULL)
1605 return -1;
1606 return get_tv_number(&li->li_tv);
1607}
1608#endif
1609
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001611 * Top level evaluation function.
1612 * Returns an allocated typval_T with the result.
1613 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001614 */
1615 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001617{
1618 typval_T *tv;
1619
1620 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001621 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001622 {
1623 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001624 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001625 }
1626
1627 return tv;
1628}
1629
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001633 * Uses argv[argc] for the function arguments. Only Number and String
1634 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001638call_vim_function(
1639 char_u *func,
1640 int argc,
1641 char_u **argv,
1642 int safe, /* use the sandbox */
1643 int str_arg_only, /* all arguments are strings */
1644 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
Bram Moolenaar33570922005-01-25 22:26:29 +00001646 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 long n;
1648 int len;
1649 int i;
1650 int doesrange;
1651 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001654 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658 for (i = 0; i < argc; i++)
1659 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001660 /* Pass a NULL or empty argument as an empty string */
1661 if (argv[i] == NULL || *argv[i] == NUL)
1662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001663 argvars[i].v_type = VAR_STRING;
1664 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001665 continue;
1666 }
1667
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001668 if (str_arg_only)
1669 len = 0;
1670 else
1671 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001672 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (len != 0 && len == (int)STRLEN(argv[i]))
1674 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001675 argvars[i].v_type = VAR_NUMBER;
1676 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 }
1678 else
1679 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001680 argvars[i].v_type = VAR_STRING;
1681 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 }
1683 }
1684
1685 if (safe)
1686 {
1687 save_funccalp = save_funccal();
1688 ++sandbox;
1689 }
1690
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1692 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001694 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (safe)
1696 {
1697 --sandbox;
1698 restore_funccal(save_funccalp);
1699 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 vim_free(argvars);
1701
1702 if (ret == FAIL)
1703 clear_tv(rettv);
1704
1705 return ret;
1706}
1707
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001708/*
1709 * Call vimL function "func" and return the result as a number.
1710 * Returns -1 when calling the function fails.
1711 * Uses argv[argc] for the function arguments.
1712 */
1713 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001714call_func_retnr(
1715 char_u *func,
1716 int argc,
1717 char_u **argv,
1718 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001719{
1720 typval_T rettv;
1721 long retval;
1722
1723 /* All arguments are passed as strings, no conversion to number. */
1724 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1725 return -1;
1726
1727 retval = get_tv_number_chk(&rettv, NULL);
1728 clear_tv(&rettv);
1729 return retval;
1730}
1731
1732#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1733 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1734
Bram Moolenaar4f688582007-07-24 12:34:30 +00001735# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001737 * Call vimL function "func" and return the result as a string.
1738 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 * Uses argv[argc] for the function arguments.
1740 */
1741 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001742call_func_retstr(
1743 char_u *func,
1744 int argc,
1745 char_u **argv,
1746 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747{
1748 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001749 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001751 /* All arguments are passed as strings, no conversion to number. */
1752 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753 return NULL;
1754
1755 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 return retval;
1758}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001759# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001761/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001762 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001764 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001765 */
1766 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001767call_func_retlist(
1768 char_u *func,
1769 int argc,
1770 char_u **argv,
1771 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772{
1773 typval_T rettv;
1774
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001775 /* All arguments are passed as strings, no conversion to number. */
1776 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001777 return NULL;
1778
1779 if (rettv.v_type != VAR_LIST)
1780 {
1781 clear_tv(&rettv);
1782 return NULL;
1783 }
1784
1785 return rettv.vval.v_list;
1786}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#endif
1788
1789/*
1790 * Save the current function call pointer, and set it to NULL.
1791 * Used when executing autocommands and for ":source".
1792 */
1793 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001794save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001796 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 current_funccal = NULL;
1799 return (void *)fc;
1800}
1801
1802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001805 funccall_T *fc = (funccall_T *)vfc;
1806
1807 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808}
1809
Bram Moolenaar05159a02005-02-26 23:04:13 +00001810#if defined(FEAT_PROFILE) || defined(PROTO)
1811/*
1812 * Prepare profiling for entering a child or something else that is not
1813 * counted for the script/function itself.
1814 * Should always be called in pair with prof_child_exit().
1815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817prof_child_enter(
1818 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001819{
1820 funccall_T *fc = current_funccal;
1821
1822 if (fc != NULL && fc->func->uf_profiling)
1823 profile_start(&fc->prof_child);
1824 script_prof_save(tm);
1825}
1826
1827/*
1828 * Take care of time spent in a child.
1829 * Should always be called after prof_child_enter().
1830 */
1831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001832prof_child_exit(
1833 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001834{
1835 funccall_T *fc = current_funccal;
1836
1837 if (fc != NULL && fc->func->uf_profiling)
1838 {
1839 profile_end(&fc->prof_child);
1840 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1841 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1842 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1843 }
1844 script_prof_restore(tm);
1845}
1846#endif
1847
1848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849#ifdef FEAT_FOLDING
1850/*
1851 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1852 * it in "*cp". Doesn't give error messages.
1853 */
1854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001855eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856{
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 int retval;
1859 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001860 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1861 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001864 if (use_sandbox)
1865 ++sandbox;
1866 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 retval = 0;
1870 else
1871 {
1872 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 if (tv.v_type == VAR_NUMBER)
1874 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001875 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 retval = 0;
1877 else
1878 {
1879 /* If the result is a string, check if there is a non-digit before
1880 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (!VIM_ISDIGIT(*s) && *s != '-')
1883 *cp = *s++;
1884 retval = atol((char *)s);
1885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001889 if (use_sandbox)
1890 --sandbox;
1891 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
1893 return retval;
1894}
1895#endif
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 * ":let" list all variable values
1899 * ":let var1 var2" list variable values
1900 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 * ":let var += expr" assignment command.
1902 * ":let var -= expr" assignment command.
1903 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 */
1906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001907ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908{
1909 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 int var_count = 0;
1914 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001916 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001917 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
Bram Moolenaardb552d602006-03-23 22:59:57 +00001919 argend = skip_var_list(arg, &var_count, &semicolon);
1920 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001922 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1923 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001924 expr = skipwhite(argend);
1925 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1926 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001928 /*
1929 * ":let" without "=": list variables
1930 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 if (*arg == '[')
1932 EMSG(_(e_invarg));
1933 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001935 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001939 list_glob_vars(&first);
1940 list_buf_vars(&first);
1941 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001942#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001943 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001944#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001945 list_script_vars(&first);
1946 list_func_vars(&first);
1947 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 eap->nextcmd = check_nextcmd(arg);
1950 }
1951 else
1952 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 op[0] = '=';
1954 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001955 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001957 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1958 op[0] = *expr; /* +=, -= or .= */
1959 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001961 else
1962 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (eap->skip)
1965 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (eap->skip)
1968 {
1969 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 --emsg_skip;
1972 }
1973 else if (i != FAIL)
1974 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001977 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979 }
1980}
1981
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982/*
1983 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1984 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 * When "nextchars" is not NULL it points to a string with characters that
1986 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1987 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 * Returns OK or FAIL;
1989 */
1990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001991ex_let_vars(
1992 char_u *arg_start,
1993 typval_T *tv,
1994 int copy, /* copy values from "tv", don't move */
1995 int semicolon, /* from skip_var_list() */
1996 int var_count, /* from skip_var_list() */
1997 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998{
1999 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002000 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002002 listitem_T *item;
2003 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004
2005 if (*arg != '[')
2006 {
2007 /*
2008 * ":let var = expr" or ":for var in list"
2009 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 return FAIL;
2012 return OK;
2013 }
2014
2015 /*
2016 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2017 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002018 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 {
2020 EMSG(_(e_listreq));
2021 return FAIL;
2022 }
2023
2024 i = list_len(l);
2025 if (semicolon == 0 && var_count < i)
2026 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002027 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 return FAIL;
2029 }
2030 if (var_count - semicolon > i)
2031 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002032 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002033 return FAIL;
2034 }
2035
2036 item = l->lv_first;
2037 while (*arg != ']')
2038 {
2039 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002040 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 item = item->li_next;
2042 if (arg == NULL)
2043 return FAIL;
2044
2045 arg = skipwhite(arg);
2046 if (*arg == ';')
2047 {
2048 /* Put the rest of the list (may be empty) in the var after ';'.
2049 * Create a new list for this. */
2050 l = list_alloc();
2051 if (l == NULL)
2052 return FAIL;
2053 while (item != NULL)
2054 {
2055 list_append_tv(l, &item->li_tv);
2056 item = item->li_next;
2057 }
2058
2059 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002060 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 ltv.vval.v_list = l;
2062 l->lv_refcount = 1;
2063
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002064 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2065 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 clear_tv(&ltv);
2067 if (arg == NULL)
2068 return FAIL;
2069 break;
2070 }
2071 else if (*arg != ',' && *arg != ']')
2072 {
2073 EMSG2(_(e_intern2), "ex_let_vars()");
2074 return FAIL;
2075 }
2076 }
2077
2078 return OK;
2079}
2080
2081/*
2082 * Skip over assignable variable "var" or list of variables "[var, var]".
2083 * Used for ":let varvar = expr" and ":for varvar in expr".
2084 * For "[var, var]" increment "*var_count" for each variable.
2085 * for "[var, var; var]" set "semicolon".
2086 * Return NULL for an error.
2087 */
2088 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002089skip_var_list(
2090 char_u *arg,
2091 int *var_count,
2092 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093{
2094 char_u *p, *s;
2095
2096 if (*arg == '[')
2097 {
2098 /* "[var, var]": find the matching ']'. */
2099 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002100 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002101 {
2102 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2103 s = skip_var_one(p);
2104 if (s == p)
2105 {
2106 EMSG2(_(e_invarg2), p);
2107 return NULL;
2108 }
2109 ++*var_count;
2110
2111 p = skipwhite(s);
2112 if (*p == ']')
2113 break;
2114 else if (*p == ';')
2115 {
2116 if (*semicolon == 1)
2117 {
2118 EMSG(_("Double ; in list of variables"));
2119 return NULL;
2120 }
2121 *semicolon = 1;
2122 }
2123 else if (*p != ',')
2124 {
2125 EMSG2(_(e_invarg2), p);
2126 return NULL;
2127 }
2128 }
2129 return p + 1;
2130 }
2131 else
2132 return skip_var_one(arg);
2133}
2134
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002136 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002137 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002138 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002140skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002141{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002142 if (*arg == '@' && arg[1] != NUL)
2143 return arg + 2;
2144 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2145 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146}
2147
Bram Moolenaara7043832005-01-21 11:56:39 +00002148/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 * List variables for hashtab "ht" with prefix "prefix".
2150 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002153list_hashtable_vars(
2154 hashtab_T *ht,
2155 char_u *prefix,
2156 int empty,
2157 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar33570922005-01-25 22:26:29 +00002159 hashitem_T *hi;
2160 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002161 int todo;
2162
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002163 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002164 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2165 {
2166 if (!HASHITEM_EMPTY(hi))
2167 {
2168 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002169 di = HI2DI(hi);
2170 if (empty || di->di_tv.v_type != VAR_STRING
2171 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002173 }
2174 }
2175}
2176
2177/*
2178 * List global variables.
2179 */
2180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002181list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002184}
2185
2186/*
2187 * List buffer variables.
2188 */
2189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002191{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 char_u numbuf[NUMBUFLEN];
2193
Bram Moolenaar429fa852013-04-15 12:27:36 +02002194 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196
2197 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2199 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002200}
2201
2202/*
2203 * List window variables.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002207{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002208 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002210}
2211
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002212#ifdef FEAT_WINDOWS
2213/*
2214 * List tab page variables.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002218{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002219 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002221}
2222#endif
2223
Bram Moolenaara7043832005-01-21 11:56:39 +00002224/*
2225 * List Vim variables.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231}
2232
2233/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002234 * List script-local variables, if there is a script.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002238{
2239 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2241 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002242}
2243
2244/*
2245 * List function variables, if there is a function.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002249{
2250 if (current_funccal != NULL)
2251 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002253}
2254
2255/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 * List variables in "arg".
2257 */
2258 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002259list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260{
2261 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 char_u *name_start;
2265 char_u *arg_subsc;
2266 char_u *tofree;
2267 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268
2269 while (!ends_excmd(*arg) && !got_int)
2270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002273 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2275 {
2276 emsg_severe = TRUE;
2277 EMSG(_(e_trailing));
2278 break;
2279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 /* get_name_len() takes care of expanding curly braces */
2284 name_start = name = arg;
2285 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2286 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 /* This is mainly to keep test 49 working: when expanding
2289 * curly braces fails overrule the exception error message. */
2290 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 emsg_severe = TRUE;
2293 EMSG2(_(e_invarg2), arg);
2294 break;
2295 }
2296 error = TRUE;
2297 }
2298 else
2299 {
2300 if (tofree != NULL)
2301 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002302 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 else
2305 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306 /* handle d.key, l[idx], f(expr) */
2307 arg_subsc = arg;
2308 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002315 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002316 case 'g': list_glob_vars(first); break;
2317 case 'b': list_buf_vars(first); break;
2318 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002319#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002320 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002321#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002322 case 'v': list_vim_vars(first); break;
2323 case 's': list_script_vars(first); break;
2324 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 default:
2326 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002327 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002328 }
2329 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330 {
2331 char_u numbuf[NUMBUFLEN];
2332 char_u *tf;
2333 int c;
2334 char_u *s;
2335
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002336 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337 c = *arg;
2338 *arg = NUL;
2339 list_one_var_a((char_u *)"",
2340 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002341 tv.v_type,
2342 s == NULL ? (char_u *)"" : s,
2343 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002344 *arg = c;
2345 vim_free(tf);
2346 }
2347 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 }
2350 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002351
2352 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002354
2355 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 }
2357
2358 return arg;
2359}
2360
2361/*
2362 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2363 * Returns a pointer to the char just after the var name.
2364 * Returns NULL if there is an error.
2365 */
2366 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002367ex_let_one(
2368 char_u *arg, /* points to variable name */
2369 typval_T *tv, /* value to assign to variable */
2370 int copy, /* copy value from "tv" */
2371 char_u *endchars, /* valid chars after variable name or NULL */
2372 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373{
2374 int c1;
2375 char_u *name;
2376 char_u *p;
2377 char_u *arg_end = NULL;
2378 int len;
2379 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381
2382 /*
2383 * ":let $VAR = expr": Set environment variable.
2384 */
2385 if (*arg == '$')
2386 {
2387 /* Find the end of the name. */
2388 ++arg;
2389 name = arg;
2390 len = get_env_len(&arg);
2391 if (len == 0)
2392 EMSG2(_(e_invarg2), name - 1);
2393 else
2394 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 if (op != NULL && (*op == '+' || *op == '-'))
2396 EMSG2(_(e_letwrong), op);
2397 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002398 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002400 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401 {
2402 c1 = name[len];
2403 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002404 p = get_tv_string_chk(tv);
2405 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 {
2407 int mustfree = FALSE;
2408 char_u *s = vim_getenv(name, &mustfree);
2409
2410 if (s != NULL)
2411 {
2412 p = tofree = concat_str(s, p);
2413 if (mustfree)
2414 vim_free(s);
2415 }
2416 }
2417 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 if (STRICMP(name, "HOME") == 0)
2421 init_homedir();
2422 else if (didset_vim && STRICMP(name, "VIM") == 0)
2423 didset_vim = FALSE;
2424 else if (didset_vimruntime
2425 && STRICMP(name, "VIMRUNTIME") == 0)
2426 didset_vimruntime = FALSE;
2427 arg_end = arg;
2428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 }
2432 }
2433 }
2434
2435 /*
2436 * ":let &option = expr": Set option value.
2437 * ":let &l:option = expr": Set local option value.
2438 * ":let &g:option = expr": Set global option value.
2439 */
2440 else if (*arg == '&')
2441 {
2442 /* Find the end of the name. */
2443 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444 if (p == NULL || (endchars != NULL
2445 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 EMSG(_(e_letunexp));
2447 else
2448 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 long n;
2450 int opt_type;
2451 long numval;
2452 char_u *stringval = NULL;
2453 char_u *s;
2454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 c1 = *p;
2456 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457
2458 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 s = get_tv_string_chk(tv); /* != NULL if number or string */
2460 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 {
2462 opt_type = get_option_value(arg, &numval,
2463 &stringval, opt_flags);
2464 if ((opt_type == 1 && *op == '.')
2465 || (opt_type == 0 && *op != '.'))
2466 EMSG2(_(e_letwrong), op);
2467 else
2468 {
2469 if (opt_type == 1) /* number */
2470 {
2471 if (*op == '+')
2472 n = numval + n;
2473 else
2474 n = numval - n;
2475 }
2476 else if (opt_type == 0 && stringval != NULL) /* string */
2477 {
2478 s = concat_str(stringval, s);
2479 vim_free(stringval);
2480 stringval = s;
2481 }
2482 }
2483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 if (s != NULL)
2485 {
2486 set_option_value(arg, n, s, opt_flags);
2487 arg_end = p;
2488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
2492 }
2493
2494 /*
2495 * ":let @r = expr": Set register contents.
2496 */
2497 else if (*arg == '@')
2498 {
2499 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 if (op != NULL && (*op == '+' || *op == '-'))
2501 EMSG2(_(e_letwrong), op);
2502 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002503 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 EMSG(_(e_letunexp));
2505 else
2506 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 char_u *s;
2509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002510 p = get_tv_string_chk(tv);
2511 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002513 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 if (s != NULL)
2515 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 vim_free(s);
2518 }
2519 }
2520 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002522 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002523 arg_end = arg + 1;
2524 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002525 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 }
2527 }
2528
2529 /*
2530 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002533 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002537 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2541 EMSG(_(e_letunexp));
2542 else
2543 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 arg_end = p;
2546 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 }
2550
2551 else
2552 EMSG2(_(e_invarg2), arg);
2553
2554 return arg_end;
2555}
2556
2557/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2559 */
2560 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002561check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562{
2563 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2564 {
2565 EMSG2(_(e_readonlyvar), arg);
2566 return TRUE;
2567 }
2568 return FALSE;
2569}
2570
2571/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 * Get an lval: variable, Dict item or List item that can be assigned a value
2573 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2574 * "name.key", "name.key[expr]" etc.
2575 * Indexing only works if "name" is an existing List or Dictionary.
2576 * "name" points to the start of the name.
2577 * If "rettv" is not NULL it points to the value to be assigned.
2578 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2579 * wrong; must end in space or cmd separator.
2580 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002581 * flags:
2582 * GLV_QUIET: do not give error messages
2583 * GLV_NO_AUTOLOAD: do not use script autoloading
2584 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002586 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 */
2589 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002590get_lval(
2591 char_u *name,
2592 typval_T *rettv,
2593 lval_T *lp,
2594 int unlet,
2595 int skip,
2596 int flags, /* GLV_ values */
2597 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 char_u *expr_start, *expr_end;
2601 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002602 dictitem_T *v;
2603 typval_T var1;
2604 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002606 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002607 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002609 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002610 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002613 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614
2615 if (skip)
2616 {
2617 /* When skipping just find the end of the name. */
2618 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002619 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 }
2621
2622 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002623 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (expr_start != NULL)
2625 {
2626 /* Don't expand the name when we already know there is an error. */
2627 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2628 && *p != '[' && *p != '.')
2629 {
2630 EMSG(_(e_trailing));
2631 return NULL;
2632 }
2633
2634 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2635 if (lp->ll_exp_name == NULL)
2636 {
2637 /* Report an invalid expression in braces, unless the
2638 * expression evaluation has been cancelled due to an
2639 * aborting error, an interrupt, or an exception. */
2640 if (!aborting() && !quiet)
2641 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002642 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 EMSG2(_(e_invarg2), name);
2644 return NULL;
2645 }
2646 }
2647 lp->ll_name = lp->ll_exp_name;
2648 }
2649 else
2650 lp->ll_name = name;
2651
2652 /* Without [idx] or .key we are done. */
2653 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2654 return p;
2655
2656 cc = *p;
2657 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002658 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (v == NULL && !quiet)
2660 EMSG2(_(e_undefvar), lp->ll_name);
2661 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 if (v == NULL)
2663 return NULL;
2664
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 /*
2666 * Loop until no more [idx] or .key is following.
2667 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002668 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2672 && !(lp->ll_tv->v_type == VAR_DICT
2673 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 if (!quiet)
2676 EMSG(_("E689: Can only index a List or Dictionary"));
2677 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (!quiet)
2682 EMSG(_("E708: [:] must come last"));
2683 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 len = -1;
2687 if (*p == '.')
2688 {
2689 key = p + 1;
2690 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2691 ;
2692 if (len == 0)
2693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (!quiet)
2695 EMSG(_(e_emptykey));
2696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = key + len;
2699 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 else
2701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002703 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (*p == ':')
2705 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706 else
2707 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 empty1 = FALSE;
2709 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711 if (get_tv_string_chk(&var1) == NULL)
2712 {
2713 /* not a number or string */
2714 clear_tv(&var1);
2715 return NULL;
2716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718
2719 /* Optionally get the second index [ :expr]. */
2720 if (*p == ':')
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 if (!empty1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (rettv != NULL && (rettv->v_type != VAR_LIST
2731 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (!quiet)
2734 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (!empty1)
2736 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
2739 p = skipwhite(p + 1);
2740 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 else
2743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (!empty1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002751 if (get_tv_string_chk(&var2) == NULL)
2752 {
2753 /* not a number or string */
2754 if (!empty1)
2755 clear_tv(&var1);
2756 clear_tv(&var2);
2757 return NULL;
2758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002764
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (!quiet)
2768 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (!empty1)
2770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775
2776 /* Skip to past ']'. */
2777 ++p;
2778 }
2779
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 {
2782 if (len == -1)
2783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002785 key = get_tv_string_chk(&var1); /* is number or string */
2786 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002792 lp->ll_list = NULL;
2793 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002794 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 /* When assigning to a scope dictionary check that a function and
2797 * variable name is valid (only variable name unless it is l: or
2798 * g: dictionary). Disallow overwriting a builtin function. */
2799 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 int prevval;
2802 int wrong;
2803
2804 if (len != -1)
2805 {
2806 prevval = key[len];
2807 key[len] = NUL;
2808 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002809 else
2810 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002811 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2812 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002813 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002814 || !valid_varname(key);
2815 if (len != -1)
2816 key[len] = prevval;
2817 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 return NULL;
2819 }
2820
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002823 /* Can't add "v:" variable. */
2824 if (lp->ll_dict == &vimvardict)
2825 {
2826 EMSG2(_(e_illvar), name);
2827 return NULL;
2828 }
2829
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002830 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002834 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (len == -1)
2836 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 if (len == -1)
2844 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 p = NULL;
2847 break;
2848 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002849 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002850 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002851 return NULL;
2852
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 if (len == -1)
2854 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 }
2857 else
2858 {
2859 /*
2860 * Get the number and item for the only or first index of the List.
2861 */
2862 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 else
2865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002866 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var1);
2868 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 lp->ll_list = lp->ll_tv->vval.v_list;
2871 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2872 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002874 if (lp->ll_n1 < 0)
2875 {
2876 lp->ll_n1 = 0;
2877 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2878 }
2879 }
2880 if (lp->ll_li == NULL)
2881 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 if (!quiet)
2885 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 }
2888
2889 /*
2890 * May need to find the item or absolute index for the second
2891 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 * When no index given: "lp->ll_empty2" is TRUE.
2893 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002897 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002898 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002900 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002902 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002903 {
2904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2912 if (lp->ll_n1 < 0)
2913 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2914 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002915 {
2916 if (!quiet)
2917 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002919 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002920 }
2921
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 }
2925
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 return p;
2927}
2928
2929/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002930 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 */
2932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002933clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934{
2935 vim_free(lp->ll_exp_name);
2936 vim_free(lp->ll_newkey);
2937}
2938
2939/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002940 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 */
2944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002945set_var_lval(
2946 lval_T *lp,
2947 char_u *endp,
2948 typval_T *rettv,
2949 int copy,
2950 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951{
2952 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 listitem_T *ri;
2954 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955
2956 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 cc = *endp;
2961 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 if (op != NULL && *op != '=')
2963 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002965
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002967 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002968 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002969 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002971 if ((di == NULL
2972 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2973 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2974 FALSE)))
2975 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 set_var(lp->ll_name, &tv, FALSE);
2977 clear_tv(&tv);
2978 }
2979 }
2980 else
2981 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002985 else if (tv_check_lock(lp->ll_newkey == NULL
2986 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002987 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002988 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 else if (lp->ll_range)
2990 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002991 listitem_T *ll_li = lp->ll_li;
2992 int ll_n1 = lp->ll_n1;
2993
2994 /*
2995 * Check whether any of the list items is locked
2996 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002997 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002998 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002999 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003000 return;
3001 ri = ri->li_next;
3002 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3003 break;
3004 ll_li = ll_li->li_next;
3005 ++ll_n1;
3006 }
3007
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 /*
3009 * Assign the List values to the list items.
3010 */
3011 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 if (op != NULL && *op != '=')
3014 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3015 else
3016 {
3017 clear_tv(&lp->ll_li->li_tv);
3018 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3019 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 ri = ri->li_next;
3021 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3022 break;
3023 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003026 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 ri = NULL;
3029 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003030 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003031 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 lp->ll_li = lp->ll_li->li_next;
3033 ++lp->ll_n1;
3034 }
3035 if (ri != NULL)
3036 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 else if (lp->ll_empty2
3038 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 : lp->ll_n1 != lp->ll_n2)
3040 EMSG(_("E711: List value has not enough items"));
3041 }
3042 else
3043 {
3044 /*
3045 * Assign to a List or Dictionary item.
3046 */
3047 if (lp->ll_newkey != NULL)
3048 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 if (op != NULL && *op != '=')
3050 {
3051 EMSG2(_(e_letwrong), op);
3052 return;
3053 }
3054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003056 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 if (di == NULL)
3058 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003059 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3060 {
3061 vim_free(di);
3062 return;
3063 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003065 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 else if (op != NULL && *op != '=')
3067 {
3068 tv_op(lp->ll_tv, rettv, op);
3069 return;
3070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003072 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003073
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 /*
3075 * Assign the value to the variable or list item.
3076 */
3077 if (copy)
3078 copy_tv(rettv, lp->ll_tv);
3079 else
3080 {
3081 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003082 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003083 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003084 }
3085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003086}
3087
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3090 * Returns OK or FAIL.
3091 */
3092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003093tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094{
3095 long n;
3096 char_u numbuf[NUMBUFLEN];
3097 char_u *s;
3098
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003099 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3100 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3101 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 {
3103 switch (tv1->v_type)
3104 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003105 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003106 case VAR_DICT:
3107 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003108 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003109 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003110 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003111 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003112 break;
3113
3114 case VAR_LIST:
3115 if (*op != '+' || tv2->v_type != VAR_LIST)
3116 break;
3117 /* List += List */
3118 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3119 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3120 return OK;
3121
3122 case VAR_NUMBER:
3123 case VAR_STRING:
3124 if (tv2->v_type == VAR_LIST)
3125 break;
3126 if (*op == '+' || *op == '-')
3127 {
3128 /* nr += nr or nr -= nr*/
3129 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#ifdef FEAT_FLOAT
3131 if (tv2->v_type == VAR_FLOAT)
3132 {
3133 float_T f = n;
3134
3135 if (*op == '+')
3136 f += tv2->vval.v_float;
3137 else
3138 f -= tv2->vval.v_float;
3139 clear_tv(tv1);
3140 tv1->v_type = VAR_FLOAT;
3141 tv1->vval.v_float = f;
3142 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144#endif
3145 {
3146 if (*op == '+')
3147 n += get_tv_number(tv2);
3148 else
3149 n -= get_tv_number(tv2);
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_NUMBER;
3152 tv1->vval.v_number = n;
3153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 }
3155 else
3156 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 if (tv2->v_type == VAR_FLOAT)
3158 break;
3159
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003160 /* str .= str */
3161 s = get_tv_string(tv1);
3162 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3163 clear_tv(tv1);
3164 tv1->v_type = VAR_STRING;
3165 tv1->vval.v_string = s;
3166 }
3167 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003170#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171 {
3172 float_T f;
3173
3174 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3175 && tv2->v_type != VAR_NUMBER
3176 && tv2->v_type != VAR_STRING))
3177 break;
3178 if (tv2->v_type == VAR_FLOAT)
3179 f = tv2->vval.v_float;
3180 else
3181 f = get_tv_number(tv2);
3182 if (*op == '+')
3183 tv1->vval.v_float += f;
3184 else
3185 tv1->vval.v_float -= f;
3186 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003187#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003188 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003189 }
3190 }
3191
3192 EMSG2(_(e_letwrong), op);
3193 return FAIL;
3194}
3195
3196/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197 * Add a watcher to a list.
3198 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003200list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201{
3202 lw->lw_next = l->lv_watch;
3203 l->lv_watch = lw;
3204}
3205
3206/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003207 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 * No warning when it isn't found...
3209 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 lwp = &l->lv_watch;
3216 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3217 {
3218 if (lw == lwrem)
3219 {
3220 *lwp = lw->lw_next;
3221 break;
3222 }
3223 lwp = &lw->lw_next;
3224 }
3225}
3226
3227/*
3228 * Just before removing an item from a list: advance watchers to the next
3229 * item.
3230 */
3231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3237 if (lw->lw_item == item)
3238 lw->lw_item = item->li_next;
3239}
3240
3241/*
3242 * Evaluate the expression used in a ":for var in expr" command.
3243 * "arg" points to "var".
3244 * Set "*errp" to TRUE for an error, FALSE otherwise;
3245 * Return a pointer that holds the info. Null when there is an error.
3246 */
3247 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003248eval_for_line(
3249 char_u *arg,
3250 int *errp,
3251 char_u **nextcmdp,
3252 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 typval_T tv;
3257 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258
3259 *errp = TRUE; /* default: there is an error */
3260
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 if (fi == NULL)
3263 return NULL;
3264
3265 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3266 if (expr == NULL)
3267 return fi;
3268
3269 expr = skipwhite(expr);
3270 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3271 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003272 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 return fi;
3274 }
3275
3276 if (skip)
3277 ++emsg_skip;
3278 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3279 {
3280 *errp = FALSE;
3281 if (!skip)
3282 {
3283 l = tv.vval.v_list;
3284 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 clear_tv(&tv);
3288 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289 else
3290 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003291 /* No need to increment the refcount, it's already set for the
3292 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 fi->fi_list = l;
3294 list_add_watch(l, &fi->fi_lw);
3295 fi->fi_lw.lw_item = l->lv_first;
3296 }
3297 }
3298 }
3299 if (skip)
3300 --emsg_skip;
3301
3302 return fi;
3303}
3304
3305/*
3306 * Use the first item in a ":for" list. Advance to the next.
3307 * Assign the values to the variable (list). "arg" points to the first one.
3308 * Return TRUE when a valid item was found, FALSE when at end of list or
3309 * something wrong.
3310 */
3311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003312next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003314 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003316 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317
3318 item = fi->fi_lw.lw_item;
3319 if (item == NULL)
3320 result = FALSE;
3321 else
3322 {
3323 fi->fi_lw.lw_item = item->li_next;
3324 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3325 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3326 }
3327 return result;
3328}
3329
3330/*
3331 * Free the structure used to store info used by ":for".
3332 */
3333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003334free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335{
Bram Moolenaar33570922005-01-25 22:26:29 +00003336 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003338 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003339 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003341 list_unref(fi->fi_list);
3342 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 vim_free(fi);
3344}
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3347
3348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003349set_context_for_expression(
3350 expand_T *xp,
3351 char_u *arg,
3352 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 int got_eq = FALSE;
3355 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 if (cmdidx == CMD_let)
3359 {
3360 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003361 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362 {
3363 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003364 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 {
3366 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003367 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 if (vim_iswhite(*p))
3369 break;
3370 }
3371 return;
3372 }
3373 }
3374 else
3375 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3376 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 while ((xp->xp_pattern = vim_strpbrk(arg,
3378 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3379 {
3380 c = *xp->xp_pattern;
3381 if (c == '&')
3382 {
3383 c = xp->xp_pattern[1];
3384 if (c == '&')
3385 {
3386 ++xp->xp_pattern;
3387 xp->xp_context = cmdidx != CMD_let || got_eq
3388 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3389 }
3390 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003393 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3394 xp->xp_pattern += 2;
3395
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 else if (c == '$')
3399 {
3400 /* environment variable */
3401 xp->xp_context = EXPAND_ENV_VARS;
3402 }
3403 else if (c == '=')
3404 {
3405 got_eq = TRUE;
3406 xp->xp_context = EXPAND_EXPRESSION;
3407 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003408 else if (c == '#'
3409 && xp->xp_context == EXPAND_EXPRESSION)
3410 {
3411 /* Autoload function/variable contains '#'. */
3412 break;
3413 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003414 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 && xp->xp_context == EXPAND_FUNCTIONS
3416 && vim_strchr(xp->xp_pattern, '(') == NULL)
3417 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003418 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 break;
3420 }
3421 else if (cmdidx != CMD_let || got_eq)
3422 {
3423 if (c == '"') /* string */
3424 {
3425 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3426 if (c == '\\' && xp->xp_pattern[1] != NUL)
3427 ++xp->xp_pattern;
3428 xp->xp_context = EXPAND_NOTHING;
3429 }
3430 else if (c == '\'') /* literal string */
3431 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003432 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3434 /* skip */ ;
3435 xp->xp_context = EXPAND_NOTHING;
3436 }
3437 else if (c == '|')
3438 {
3439 if (xp->xp_pattern[1] == '|')
3440 {
3441 ++xp->xp_pattern;
3442 xp->xp_context = EXPAND_EXPRESSION;
3443 }
3444 else
3445 xp->xp_context = EXPAND_COMMANDS;
3446 }
3447 else
3448 xp->xp_context = EXPAND_EXPRESSION;
3449 }
3450 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003451 /* Doesn't look like something valid, expand as an expression
3452 * anyway. */
3453 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 arg = xp->xp_pattern;
3455 if (*arg != NUL)
3456 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3457 /* skip */ ;
3458 }
3459 xp->xp_pattern = arg;
3460}
3461
3462#endif /* FEAT_CMDL_COMPL */
3463
3464/*
3465 * ":1,25call func(arg1, arg2)" function call.
3466 */
3467 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003468ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
3470 char_u *arg = eap->arg;
3471 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003473 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003475 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 linenr_T lnum;
3477 int doesrange;
3478 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003479 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003480 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003482 if (eap->skip)
3483 {
3484 /* trans_function_name() doesn't work well when skipping, use eval0()
3485 * instead to skip to any following command, e.g. for:
3486 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003487 ++emsg_skip;
3488 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3489 clear_tv(&rettv);
3490 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003491 return;
3492 }
3493
Bram Moolenaar65639032016-03-16 21:40:30 +01003494 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003495 if (fudi.fd_newkey != NULL)
3496 {
3497 /* Still need to give an error message for missing key. */
3498 EMSG2(_(e_dictkey), fudi.fd_newkey);
3499 vim_free(fudi.fd_newkey);
3500 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003501 if (tofree == NULL)
3502 return;
3503
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003504 /* Increase refcount on dictionary, it could get deleted when evaluating
3505 * the arguments. */
3506 if (fudi.fd_dict != NULL)
3507 ++fudi.fd_dict->dv_refcount;
3508
Bram Moolenaar65639032016-03-16 21:40:30 +01003509 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3510 * contents. For VAR_PARTIAL get its partial, unless we already have one
3511 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003512 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003513 name = deref_func_name(tofree, &len,
3514 partial != NULL ? NULL : &partial, FALSE);
3515
Bram Moolenaar532c7802005-01-27 14:44:31 +00003516 /* Skip white space to allow ":call func ()". Not good, but required for
3517 * backward compatibility. */
3518 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003519 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 if (*startarg != '(')
3522 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003523 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 goto end;
3525 }
3526
3527 /*
3528 * When skipping, evaluate the function once, to find the end of the
3529 * arguments.
3530 * When the function takes a range, this is discovered after the first
3531 * call, and the loop is broken.
3532 */
3533 if (eap->skip)
3534 {
3535 ++emsg_skip;
3536 lnum = eap->line2; /* do it once, also with an invalid range */
3537 }
3538 else
3539 lnum = eap->line1;
3540 for ( ; lnum <= eap->line2; ++lnum)
3541 {
3542 if (!eap->skip && eap->addr_count > 0)
3543 {
3544 curwin->w_cursor.lnum = lnum;
3545 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003546#ifdef FEAT_VIRTUALEDIT
3547 curwin->w_cursor.coladd = 0;
3548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003551 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003552 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003553 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555 failed = TRUE;
3556 break;
3557 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003558
3559 /* Handle a function returning a Funcref, Dictionary or List. */
3560 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3561 {
3562 failed = TRUE;
3563 break;
3564 }
3565
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003566 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 if (doesrange || eap->skip)
3568 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003571 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003572 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003573 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (aborting())
3575 break;
3576 }
3577 if (eap->skip)
3578 --emsg_skip;
3579
3580 if (!failed)
3581 {
3582 /* Check for trailing illegal characters and a following command. */
3583 if (!ends_excmd(*arg))
3584 {
3585 emsg_severe = TRUE;
3586 EMSG(_(e_trailing));
3587 }
3588 else
3589 eap->nextcmd = check_nextcmd(arg);
3590 }
3591
3592end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003593 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003594 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595}
3596
3597/*
3598 * ":unlet[!] var1 ... " command.
3599 */
3600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603 ex_unletlock(eap, eap->arg, 0);
3604}
3605
3606/*
3607 * ":lockvar" and ":unlockvar" commands
3608 */
3609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003610ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 int deep = 2;
3614
3615 if (eap->forceit)
3616 deep = -1;
3617 else if (vim_isdigit(*arg))
3618 {
3619 deep = getdigits(&arg);
3620 arg = skipwhite(arg);
3621 }
3622
3623 ex_unletlock(eap, arg, deep);
3624}
3625
3626/*
3627 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3628 */
3629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003630ex_unletlock(
3631 exarg_T *eap,
3632 char_u *argstart,
3633 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634{
3635 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 do
3641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003643 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003644 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (lv.ll_name == NULL)
3646 error = TRUE; /* error but continue parsing */
3647 if (name_end == NULL || (!vim_iswhite(*name_end)
3648 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 if (name_end != NULL)
3651 {
3652 emsg_severe = TRUE;
3653 EMSG(_(e_trailing));
3654 }
3655 if (!(eap->skip || error))
3656 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 break;
3658 }
3659
3660 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661 {
3662 if (eap->cmdidx == CMD_unlet)
3663 {
3664 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3665 error = TRUE;
3666 }
3667 else
3668 {
3669 if (do_lock_var(&lv, name_end, deep,
3670 eap->cmdidx == CMD_lockvar) == FAIL)
3671 error = TRUE;
3672 }
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 if (!eap->skip)
3676 clear_lval(&lv);
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 arg = skipwhite(name_end);
3679 } while (!ends_excmd(*arg));
3680
3681 eap->nextcmd = check_nextcmd(arg);
3682}
3683
Bram Moolenaar8c711452005-01-14 21:53:12 +00003684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003685do_unlet_var(
3686 lval_T *lp,
3687 char_u *name_end,
3688 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 int ret = OK;
3691 int cc;
3692
3693 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 cc = *name_end;
3696 *name_end = NUL;
3697
3698 /* Normal name or expanded name. */
3699 if (check_changedtick(lp->ll_name))
3700 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003701 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003702 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003704 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003705 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003706 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003707 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003708 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003709 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 else if (lp->ll_range)
3711 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003713 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003714 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003715
3716 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3717 {
3718 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003719 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003720 return FAIL;
3721 ll_li = li;
3722 ++ll_n1;
3723 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724
3725 /* Delete a range of List items. */
3726 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3727 {
3728 li = lp->ll_li->li_next;
3729 listitem_remove(lp->ll_list, lp->ll_li);
3730 lp->ll_li = li;
3731 ++lp->ll_n1;
3732 }
3733 }
3734 else
3735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003736 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003737 /* unlet a List item. */
3738 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003739 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003740 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003741 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003742 }
3743
3744 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003745}
3746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747/*
3748 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 */
3751 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003752do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
Bram Moolenaar33570922005-01-25 22:26:29 +00003754 hashtab_T *ht;
3755 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003756 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003757 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003758 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 ht = find_var_ht(name, &varname);
3761 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003763 if (ht == &globvarht)
3764 d = &globvardict;
3765 else if (current_funccal != NULL
3766 && ht == &current_funccal->l_vars.dv_hashtab)
3767 d = &current_funccal->l_vars;
3768 else if (ht == &compat_hashtab)
3769 d = &vimvardict;
3770 else
3771 {
3772 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3773 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3774 }
3775 if (d == NULL)
3776 {
3777 EMSG2(_(e_intern2), "do_unlet()");
3778 return FAIL;
3779 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 hi = hash_find(ht, varname);
3781 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003783 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003784 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003785 || var_check_ro(di->di_flags, name, FALSE)
3786 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003787 return FAIL;
3788
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003789 delete_var(ht, hi);
3790 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003793 if (forceit)
3794 return OK;
3795 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return FAIL;
3797}
3798
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003799/*
3800 * Lock or unlock variable indicated by "lp".
3801 * "deep" is the levels to go (-1 for unlimited);
3802 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3803 */
3804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003805do_lock_var(
3806 lval_T *lp,
3807 char_u *name_end,
3808 int deep,
3809 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003810{
3811 int ret = OK;
3812 int cc;
3813 dictitem_T *di;
3814
3815 if (deep == 0) /* nothing to do */
3816 return OK;
3817
3818 if (lp->ll_tv == NULL)
3819 {
3820 cc = *name_end;
3821 *name_end = NUL;
3822
3823 /* Normal name or expanded name. */
3824 if (check_changedtick(lp->ll_name))
3825 ret = FAIL;
3826 else
3827 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003828 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003829 if (di == NULL)
3830 ret = FAIL;
3831 else
3832 {
3833 if (lock)
3834 di->di_flags |= DI_FLAGS_LOCK;
3835 else
3836 di->di_flags &= ~DI_FLAGS_LOCK;
3837 item_lock(&di->di_tv, deep, lock);
3838 }
3839 }
3840 *name_end = cc;
3841 }
3842 else if (lp->ll_range)
3843 {
3844 listitem_T *li = lp->ll_li;
3845
3846 /* (un)lock a range of List items. */
3847 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3848 {
3849 item_lock(&li->li_tv, deep, lock);
3850 li = li->li_next;
3851 ++lp->ll_n1;
3852 }
3853 }
3854 else if (lp->ll_list != NULL)
3855 /* (un)lock a List item. */
3856 item_lock(&lp->ll_li->li_tv, deep, lock);
3857 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003858 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003859 item_lock(&lp->ll_di->di_tv, deep, lock);
3860
3861 return ret;
3862}
3863
3864/*
3865 * Lock or unlock an item. "deep" is nr of levels to go.
3866 */
3867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003868item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003869{
3870 static int recurse = 0;
3871 list_T *l;
3872 listitem_T *li;
3873 dict_T *d;
3874 hashitem_T *hi;
3875 int todo;
3876
3877 if (recurse >= DICT_MAXNEST)
3878 {
3879 EMSG(_("E743: variable nested too deep for (un)lock"));
3880 return;
3881 }
3882 if (deep == 0)
3883 return;
3884 ++recurse;
3885
3886 /* lock/unlock the item itself */
3887 if (lock)
3888 tv->v_lock |= VAR_LOCKED;
3889 else
3890 tv->v_lock &= ~VAR_LOCKED;
3891
3892 switch (tv->v_type)
3893 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003894 case VAR_UNKNOWN:
3895 case VAR_NUMBER:
3896 case VAR_STRING:
3897 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003898 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003899 case VAR_FLOAT:
3900 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003901 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003902 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003903 break;
3904
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003905 case VAR_LIST:
3906 if ((l = tv->vval.v_list) != NULL)
3907 {
3908 if (lock)
3909 l->lv_lock |= VAR_LOCKED;
3910 else
3911 l->lv_lock &= ~VAR_LOCKED;
3912 if (deep < 0 || deep > 1)
3913 /* recursive: lock/unlock the items the List contains */
3914 for (li = l->lv_first; li != NULL; li = li->li_next)
3915 item_lock(&li->li_tv, deep - 1, lock);
3916 }
3917 break;
3918 case VAR_DICT:
3919 if ((d = tv->vval.v_dict) != NULL)
3920 {
3921 if (lock)
3922 d->dv_lock |= VAR_LOCKED;
3923 else
3924 d->dv_lock &= ~VAR_LOCKED;
3925 if (deep < 0 || deep > 1)
3926 {
3927 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003928 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003929 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3930 {
3931 if (!HASHITEM_EMPTY(hi))
3932 {
3933 --todo;
3934 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3935 }
3936 }
3937 }
3938 }
3939 }
3940 --recurse;
3941}
3942
Bram Moolenaara40058a2005-07-11 22:42:07 +00003943/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003944 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3945 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003946 */
3947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003949{
3950 return (tv->v_lock & VAR_LOCKED)
3951 || (tv->v_type == VAR_LIST
3952 && tv->vval.v_list != NULL
3953 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3954 || (tv->v_type == VAR_DICT
3955 && tv->vval.v_dict != NULL
3956 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3957}
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3960/*
3961 * Delete all "menutrans_" variables.
3962 */
3963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003967 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003970 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003972 {
3973 if (!HASHITEM_EMPTY(hi))
3974 {
3975 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003976 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3977 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003978 }
3979 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981}
3982#endif
3983
3984#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3985
3986/*
3987 * Local string buffer for the next two functions to store a variable name
3988 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3989 * get_user_var_name().
3990 */
3991
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003992static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994static char_u *varnamebuf = NULL;
3995static int varnamebuflen = 0;
3996
3997/*
3998 * Function to concatenate a prefix and a variable name.
3999 */
4000 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 int len;
4004
4005 len = (int)STRLEN(name) + 3;
4006 if (len > varnamebuflen)
4007 {
4008 vim_free(varnamebuf);
4009 len += 10; /* some additional space */
4010 varnamebuf = alloc(len);
4011 if (varnamebuf == NULL)
4012 {
4013 varnamebuflen = 0;
4014 return NULL;
4015 }
4016 varnamebuflen = len;
4017 }
4018 *varnamebuf = prefix;
4019 varnamebuf[1] = ':';
4020 STRCPY(varnamebuf + 2, name);
4021 return varnamebuf;
4022}
4023
4024/*
4025 * Function given to ExpandGeneric() to obtain the list of user defined
4026 * (global/buffer/window/built-in) variable names.
4027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004029get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004031 static long_u gdone;
4032 static long_u bdone;
4033 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004034#ifdef FEAT_WINDOWS
4035 static long_u tdone;
4036#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004037 static int vidx;
4038 static hashitem_T *hi;
4039 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004044#ifdef FEAT_WINDOWS
4045 tdone = 0;
4046#endif
4047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
4049 /* Global variables */
4050 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004054 else
4055 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004056 while (HASHITEM_EMPTY(hi))
4057 ++hi;
4058 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4059 return cat_prefix_varname('g', hi->hi_key);
4060 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004062
4063 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004064 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004067 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004069 else
4070 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004071 while (HASHITEM_EMPTY(hi))
4072 ++hi;
4073 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004077 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return (char_u *)"b:changedtick";
4079 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004080
4081 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004082 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004085 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004087 else
4088 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004089 while (HASHITEM_EMPTY(hi))
4090 ++hi;
4091 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004093
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004094#ifdef FEAT_WINDOWS
4095 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004096 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004097 if (tdone < ht->ht_used)
4098 {
4099 if (tdone++ == 0)
4100 hi = ht->ht_array;
4101 else
4102 ++hi;
4103 while (HASHITEM_EMPTY(hi))
4104 ++hi;
4105 return cat_prefix_varname('t', hi->hi_key);
4106 }
4107#endif
4108
Bram Moolenaar33570922005-01-25 22:26:29 +00004109 /* v: variables */
4110 if (vidx < VV_LEN)
4111 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 vim_free(varnamebuf);
4114 varnamebuf = NULL;
4115 varnamebuflen = 0;
4116 return NULL;
4117}
4118
4119#endif /* FEAT_CMDL_COMPL */
4120
4121/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004122 * Return TRUE if "pat" matches "text".
4123 * Does not use 'cpo' and always uses 'magic'.
4124 */
4125 static int
4126pattern_match(char_u *pat, char_u *text, int ic)
4127{
4128 int matches = FALSE;
4129 char_u *save_cpo;
4130 regmatch_T regmatch;
4131
4132 /* avoid 'l' flag in 'cpoptions' */
4133 save_cpo = p_cpo;
4134 p_cpo = (char_u *)"";
4135 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4136 if (regmatch.regprog != NULL)
4137 {
4138 regmatch.rm_ic = ic;
4139 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4140 vim_regfree(regmatch.regprog);
4141 }
4142 p_cpo = save_cpo;
4143 return matches;
4144}
4145
4146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 * types for expressions.
4148 */
4149typedef enum
4150{
4151 TYPE_UNKNOWN = 0
4152 , TYPE_EQUAL /* == */
4153 , TYPE_NEQUAL /* != */
4154 , TYPE_GREATER /* > */
4155 , TYPE_GEQUAL /* >= */
4156 , TYPE_SMALLER /* < */
4157 , TYPE_SEQUAL /* <= */
4158 , TYPE_MATCH /* =~ */
4159 , TYPE_NOMATCH /* !~ */
4160} exptype_T;
4161
4162/*
4163 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4166 */
4167
4168/*
4169 * Handle zero level expression.
4170 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004172 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * Return OK or FAIL.
4174 */
4175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004176eval0(
4177 char_u *arg,
4178 typval_T *rettv,
4179 char_u **nextcmd,
4180 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
4182 int ret;
4183 char_u *p;
4184
4185 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (ret == FAIL || !ends_excmd(*p))
4188 {
4189 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 /*
4192 * Report the invalid expression unless the expression evaluation has
4193 * been cancelled due to an aborting error, an interrupt, or an
4194 * exception.
4195 */
4196 if (!aborting())
4197 EMSG2(_(e_invexpr2), arg);
4198 ret = FAIL;
4199 }
4200 if (nextcmd != NULL)
4201 *nextcmd = check_nextcmd(p);
4202
4203 return ret;
4204}
4205
4206/*
4207 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004208 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 *
4210 * "arg" must point to the first non-white of the expression.
4211 * "arg" is advanced to the next non-white after the recognized expression.
4212 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004213 * Note: "rettv.v_lock" is not set.
4214 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 * Return OK or FAIL.
4216 */
4217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004218eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004221 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * Get the first variable.
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228
4229 if ((*arg)[0] == '?')
4230 {
4231 result = FALSE;
4232 if (evaluate)
4233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
4235
4236 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (error)
4240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242
4243 /*
4244 * Get the second variable.
4245 */
4246 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return FAIL;
4249
4250 /*
4251 * Check for the ":".
4252 */
4253 if ((*arg)[0] != ':')
4254 {
4255 EMSG(_("E109: Missing ':' after '?'"));
4256 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return FAIL;
4259 }
4260
4261 /*
4262 * Get the third variable.
4263 */
4264 *arg = skipwhite(*arg + 1);
4265 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4266 {
4267 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 return FAIL;
4270 }
4271 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274
4275 return OK;
4276}
4277
4278/*
4279 * Handle first level expression:
4280 * expr2 || expr2 || expr2 logical OR
4281 *
4282 * "arg" must point to the first non-white of the expression.
4283 * "arg" is advanced to the next non-white after the recognized expression.
4284 *
4285 * Return OK or FAIL.
4286 */
4287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004288eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289{
Bram Moolenaar33570922005-01-25 22:26:29 +00004290 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 long result;
4292 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295 /*
4296 * Get the first variable.
4297 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return FAIL;
4300
4301 /*
4302 * Repeat until there is no following "||".
4303 */
4304 first = TRUE;
4305 result = FALSE;
4306 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4307 {
4308 if (evaluate && first)
4309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 if (error)
4314 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 first = FALSE;
4316 }
4317
4318 /*
4319 * Get the second variable.
4320 */
4321 *arg = skipwhite(*arg + 2);
4322 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4323 return FAIL;
4324
4325 /*
4326 * Compute the result.
4327 */
4328 if (evaluate && !result)
4329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 if (error)
4334 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336 if (evaluate)
4337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 rettv->v_type = VAR_NUMBER;
4339 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 }
4342
4343 return OK;
4344}
4345
4346/*
4347 * Handle second level expression:
4348 * expr3 && expr3 && expr3 logical AND
4349 *
4350 * "arg" must point to the first non-white of the expression.
4351 * "arg" is advanced to the next non-white after the recognized expression.
4352 *
4353 * Return OK or FAIL.
4354 */
4355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004356eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
Bram Moolenaar33570922005-01-25 22:26:29 +00004358 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 long result;
4360 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 /*
4364 * Get the first variable.
4365 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 return FAIL;
4368
4369 /*
4370 * Repeat until there is no following "&&".
4371 */
4372 first = TRUE;
4373 result = TRUE;
4374 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4375 {
4376 if (evaluate && first)
4377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004378 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 if (error)
4382 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 first = FALSE;
4384 }
4385
4386 /*
4387 * Get the second variable.
4388 */
4389 *arg = skipwhite(*arg + 2);
4390 if (eval4(arg, &var2, evaluate && result) == FAIL)
4391 return FAIL;
4392
4393 /*
4394 * Compute the result.
4395 */
4396 if (evaluate && result)
4397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004400 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 if (error)
4402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 if (evaluate)
4405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 rettv->v_type = VAR_NUMBER;
4407 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409 }
4410
4411 return OK;
4412}
4413
4414/*
4415 * Handle third level expression:
4416 * var1 == var2
4417 * var1 =~ var2
4418 * var1 != var2
4419 * var1 !~ var2
4420 * var1 > var2
4421 * var1 >= var2
4422 * var1 < var2
4423 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 * var1 is var2
4425 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 *
4427 * "arg" must point to the first non-white of the expression.
4428 * "arg" is advanced to the next non-white after the recognized expression.
4429 *
4430 * Return OK or FAIL.
4431 */
4432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004433eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
Bram Moolenaar33570922005-01-25 22:26:29 +00004435 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 char_u *p;
4437 int i;
4438 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 int len = 2;
4441 long n1, n2;
4442 char_u *s1, *s2;
4443 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 /*
4447 * Get the first variable.
4448 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 return FAIL;
4451
4452 p = *arg;
4453 switch (p[0])
4454 {
4455 case '=': if (p[1] == '=')
4456 type = TYPE_EQUAL;
4457 else if (p[1] == '~')
4458 type = TYPE_MATCH;
4459 break;
4460 case '!': if (p[1] == '=')
4461 type = TYPE_NEQUAL;
4462 else if (p[1] == '~')
4463 type = TYPE_NOMATCH;
4464 break;
4465 case '>': if (p[1] != '=')
4466 {
4467 type = TYPE_GREATER;
4468 len = 1;
4469 }
4470 else
4471 type = TYPE_GEQUAL;
4472 break;
4473 case '<': if (p[1] != '=')
4474 {
4475 type = TYPE_SMALLER;
4476 len = 1;
4477 }
4478 else
4479 type = TYPE_SEQUAL;
4480 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 case 'i': if (p[1] == 's')
4482 {
4483 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4484 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004485 i = p[len];
4486 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004487 {
4488 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4489 type_is = TRUE;
4490 }
4491 }
4492 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
4495 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004496 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 */
4498 if (type != TYPE_UNKNOWN)
4499 {
4500 /* extra question mark appended: ignore case */
4501 if (p[len] == '?')
4502 {
4503 ic = TRUE;
4504 ++len;
4505 }
4506 /* extra '#' appended: match case */
4507 else if (p[len] == '#')
4508 {
4509 ic = FALSE;
4510 ++len;
4511 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 else
4514 ic = p_ic;
4515
4516 /*
4517 * Get the second variable.
4518 */
4519 *arg = skipwhite(p + len);
4520 if (eval5(arg, &var2, evaluate) == FAIL)
4521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 return FAIL;
4524 }
4525
4526 if (evaluate)
4527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004528 if (type_is && rettv->v_type != var2.v_type)
4529 {
4530 /* For "is" a different type always means FALSE, for "notis"
4531 * it means TRUE. */
4532 n1 = (type == TYPE_NEQUAL);
4533 }
4534 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4535 {
4536 if (type_is)
4537 {
4538 n1 = (rettv->v_type == var2.v_type
4539 && rettv->vval.v_list == var2.vval.v_list);
4540 if (type == TYPE_NEQUAL)
4541 n1 = !n1;
4542 }
4543 else if (rettv->v_type != var2.v_type
4544 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4545 {
4546 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004547 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004549 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 clear_tv(rettv);
4551 clear_tv(&var2);
4552 return FAIL;
4553 }
4554 else
4555 {
4556 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004557 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4558 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 if (type == TYPE_NEQUAL)
4560 n1 = !n1;
4561 }
4562 }
4563
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004564 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4565 {
4566 if (type_is)
4567 {
4568 n1 = (rettv->v_type == var2.v_type
4569 && rettv->vval.v_dict == var2.vval.v_dict);
4570 if (type == TYPE_NEQUAL)
4571 n1 = !n1;
4572 }
4573 else if (rettv->v_type != var2.v_type
4574 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4575 {
4576 if (rettv->v_type != var2.v_type)
4577 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4578 else
4579 EMSG(_("E736: Invalid operation for Dictionary"));
4580 clear_tv(rettv);
4581 clear_tv(&var2);
4582 return FAIL;
4583 }
4584 else
4585 {
4586 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004587 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4588 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004589 if (type == TYPE_NEQUAL)
4590 n1 = !n1;
4591 }
4592 }
4593
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004594 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4595 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004596 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004597 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004598 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004599 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004600 clear_tv(rettv);
4601 clear_tv(&var2);
4602 return FAIL;
4603 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004604 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004605 if (type == TYPE_NEQUAL)
4606 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004607 }
4608
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004609#ifdef FEAT_FLOAT
4610 /*
4611 * If one of the two variables is a float, compare as a float.
4612 * When using "=~" or "!~", always compare as string.
4613 */
4614 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4615 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4616 {
4617 float_T f1, f2;
4618
4619 if (rettv->v_type == VAR_FLOAT)
4620 f1 = rettv->vval.v_float;
4621 else
4622 f1 = get_tv_number(rettv);
4623 if (var2.v_type == VAR_FLOAT)
4624 f2 = var2.vval.v_float;
4625 else
4626 f2 = get_tv_number(&var2);
4627 n1 = FALSE;
4628 switch (type)
4629 {
4630 case TYPE_EQUAL: n1 = (f1 == f2); break;
4631 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4632 case TYPE_GREATER: n1 = (f1 > f2); break;
4633 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4634 case TYPE_SMALLER: n1 = (f1 < f2); break;
4635 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4636 case TYPE_UNKNOWN:
4637 case TYPE_MATCH:
4638 case TYPE_NOMATCH: break; /* avoid gcc warning */
4639 }
4640 }
4641#endif
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 /*
4644 * If one of the two variables is a number, compare as a number.
4645 * When using "=~" or "!~", always compare as string.
4646 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004647 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004650 n1 = get_tv_number(rettv);
4651 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 switch (type)
4653 {
4654 case TYPE_EQUAL: n1 = (n1 == n2); break;
4655 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4656 case TYPE_GREATER: n1 = (n1 > n2); break;
4657 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4658 case TYPE_SMALLER: n1 = (n1 < n2); break;
4659 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4660 case TYPE_UNKNOWN:
4661 case TYPE_MATCH:
4662 case TYPE_NOMATCH: break; /* avoid gcc warning */
4663 }
4664 }
4665 else
4666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 s1 = get_tv_string_buf(rettv, buf1);
4668 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4670 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4671 else
4672 i = 0;
4673 n1 = FALSE;
4674 switch (type)
4675 {
4676 case TYPE_EQUAL: n1 = (i == 0); break;
4677 case TYPE_NEQUAL: n1 = (i != 0); break;
4678 case TYPE_GREATER: n1 = (i > 0); break;
4679 case TYPE_GEQUAL: n1 = (i >= 0); break;
4680 case TYPE_SMALLER: n1 = (i < 0); break;
4681 case TYPE_SEQUAL: n1 = (i <= 0); break;
4682
4683 case TYPE_MATCH:
4684 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004685 n1 = pattern_match(s2, s1, ic);
4686 if (type == TYPE_NOMATCH)
4687 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 break;
4689
4690 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4691 }
4692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 clear_tv(rettv);
4694 clear_tv(&var2);
4695 rettv->v_type = VAR_NUMBER;
4696 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698 }
4699
4700 return OK;
4701}
4702
4703/*
4704 * Handle fourth level expression:
4705 * + number addition
4706 * - number subtraction
4707 * . string concatenation
4708 *
4709 * "arg" must point to the first non-white of the expression.
4710 * "arg" is advanced to the next non-white after the recognized expression.
4711 *
4712 * Return OK or FAIL.
4713 */
4714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004715eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
Bram Moolenaar33570922005-01-25 22:26:29 +00004717 typval_T var2;
4718 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 int op;
4720 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721#ifdef FEAT_FLOAT
4722 float_T f1 = 0, f2 = 0;
4723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 char_u *s1, *s2;
4725 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4726 char_u *p;
4727
4728 /*
4729 * Get the first variable.
4730 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004731 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return FAIL;
4733
4734 /*
4735 * Repeat computing, until no '+', '-' or '.' is following.
4736 */
4737 for (;;)
4738 {
4739 op = **arg;
4740 if (op != '+' && op != '-' && op != '.')
4741 break;
4742
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743 if ((op != '+' || rettv->v_type != VAR_LIST)
4744#ifdef FEAT_FLOAT
4745 && (op == '.' || rettv->v_type != VAR_FLOAT)
4746#endif
4747 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 {
4749 /* For "list + ...", an illegal use of the first operand as
4750 * a number cannot be determined before evaluating the 2nd
4751 * operand: if this is also a list, all is ok.
4752 * For "something . ...", "something - ..." or "non-list + ...",
4753 * we know that the first operand needs to be a string or number
4754 * without evaluating the 2nd operand. So check before to avoid
4755 * side effects after an error. */
4756 if (evaluate && get_tv_string_chk(rettv) == NULL)
4757 {
4758 clear_tv(rettv);
4759 return FAIL;
4760 }
4761 }
4762
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 /*
4764 * Get the second variable.
4765 */
4766 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004767 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 return FAIL;
4771 }
4772
4773 if (evaluate)
4774 {
4775 /*
4776 * Compute the result.
4777 */
4778 if (op == '.')
4779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004780 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4781 s2 = get_tv_string_buf_chk(&var2, buf2);
4782 if (s2 == NULL) /* type error ? */
4783 {
4784 clear_tv(rettv);
4785 clear_tv(&var2);
4786 return FAIL;
4787 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004788 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 clear_tv(rettv);
4790 rettv->v_type = VAR_STRING;
4791 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004793 else if (op == '+' && rettv->v_type == VAR_LIST
4794 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004795 {
4796 /* concatenate Lists */
4797 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4798 &var3) == FAIL)
4799 {
4800 clear_tv(rettv);
4801 clear_tv(&var2);
4802 return FAIL;
4803 }
4804 clear_tv(rettv);
4805 *rettv = var3;
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 else
4808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004809 int error = FALSE;
4810
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811#ifdef FEAT_FLOAT
4812 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 f1 = rettv->vval.v_float;
4815 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817 else
4818#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004819 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004820 n1 = get_tv_number_chk(rettv, &error);
4821 if (error)
4822 {
4823 /* This can only happen for "list + non-list". For
4824 * "non-list + ..." or "something - ...", we returned
4825 * before evaluating the 2nd operand. */
4826 clear_tv(rettv);
4827 return FAIL;
4828 }
4829#ifdef FEAT_FLOAT
4830 if (var2.v_type == VAR_FLOAT)
4831 f1 = n1;
4832#endif
4833 }
4834#ifdef FEAT_FLOAT
4835 if (var2.v_type == VAR_FLOAT)
4836 {
4837 f2 = var2.vval.v_float;
4838 n2 = 0;
4839 }
4840 else
4841#endif
4842 {
4843 n2 = get_tv_number_chk(&var2, &error);
4844 if (error)
4845 {
4846 clear_tv(rettv);
4847 clear_tv(&var2);
4848 return FAIL;
4849 }
4850#ifdef FEAT_FLOAT
4851 if (rettv->v_type == VAR_FLOAT)
4852 f2 = n2;
4853#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856
4857#ifdef FEAT_FLOAT
4858 /* If there is a float on either side the result is a float. */
4859 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4860 {
4861 if (op == '+')
4862 f1 = f1 + f2;
4863 else
4864 f1 = f1 - f2;
4865 rettv->v_type = VAR_FLOAT;
4866 rettv->vval.v_float = f1;
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869#endif
4870 {
4871 if (op == '+')
4872 n1 = n1 + n2;
4873 else
4874 n1 = n1 - n2;
4875 rettv->v_type = VAR_NUMBER;
4876 rettv->vval.v_number = n1;
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 }
4882 return OK;
4883}
4884
4885/*
4886 * Handle fifth level expression:
4887 * * number multiplication
4888 * / number division
4889 * % number modulo
4890 *
4891 * "arg" must point to the first non-white of the expression.
4892 * "arg" is advanced to the next non-white after the recognized expression.
4893 *
4894 * Return OK or FAIL.
4895 */
4896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004897eval6(
4898 char_u **arg,
4899 typval_T *rettv,
4900 int evaluate,
4901 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
Bram Moolenaar33570922005-01-25 22:26:29 +00004903 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 int op;
4905 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906#ifdef FEAT_FLOAT
4907 int use_float = FALSE;
4908 float_T f1 = 0, f2;
4909#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004910 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911
4912 /*
4913 * Get the first variable.
4914 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004915 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 return FAIL;
4917
4918 /*
4919 * Repeat computing, until no '*', '/' or '%' is following.
4920 */
4921 for (;;)
4922 {
4923 op = **arg;
4924 if (op != '*' && op != '/' && op != '%')
4925 break;
4926
4927 if (evaluate)
4928 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#ifdef FEAT_FLOAT
4930 if (rettv->v_type == VAR_FLOAT)
4931 {
4932 f1 = rettv->vval.v_float;
4933 use_float = TRUE;
4934 n1 = 0;
4935 }
4936 else
4937#endif
4938 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004940 if (error)
4941 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943 else
4944 n1 = 0;
4945
4946 /*
4947 * Get the second variable.
4948 */
4949 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004950 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 return FAIL;
4952
4953 if (evaluate)
4954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (var2.v_type == VAR_FLOAT)
4957 {
4958 if (!use_float)
4959 {
4960 f1 = n1;
4961 use_float = TRUE;
4962 }
4963 f2 = var2.vval.v_float;
4964 n2 = 0;
4965 }
4966 else
4967#endif
4968 {
4969 n2 = get_tv_number_chk(&var2, &error);
4970 clear_tv(&var2);
4971 if (error)
4972 return FAIL;
4973#ifdef FEAT_FLOAT
4974 if (use_float)
4975 f2 = n2;
4976#endif
4977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
4979 /*
4980 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983#ifdef FEAT_FLOAT
4984 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 if (op == '*')
4987 f1 = f1 * f2;
4988 else if (op == '/')
4989 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004990# ifdef VMS
4991 /* VMS crashes on divide by zero, work around it */
4992 if (f2 == 0.0)
4993 {
4994 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004995 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004996 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004997 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004999 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005000 }
5001 else
5002 f1 = f1 / f2;
5003# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 /* We rely on the floating point library to handle divide
5005 * by zero to result in "inf" and not a crash. */
5006 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005007# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005011 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 return FAIL;
5013 }
5014 rettv->v_type = VAR_FLOAT;
5015 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 if (op == '*')
5021 n1 = n1 * n2;
5022 else if (op == '/')
5023 {
5024 if (n2 == 0) /* give an error message? */
5025 {
5026 if (n1 == 0)
5027 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5028 else if (n1 < 0)
5029 n1 = -0x7fffffffL;
5030 else
5031 n1 = 0x7fffffffL;
5032 }
5033 else
5034 n1 = n1 / n2;
5035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 {
5038 if (n2 == 0) /* give an error message? */
5039 n1 = 0;
5040 else
5041 n1 = n1 % n2;
5042 }
5043 rettv->v_type = VAR_NUMBER;
5044 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
5047 }
5048
5049 return OK;
5050}
5051
5052/*
5053 * Handle sixth level expression:
5054 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005055 * "string" string constant
5056 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 * &option-name option value
5058 * @r register contents
5059 * identifier variable value
5060 * function() function call
5061 * $VAR environment variable
5062 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005063 * [expr, expr] List
5064 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 *
5066 * Also handle:
5067 * ! in front logical NOT
5068 * - in front unary minus
5069 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 * trailing [] subscript in String or List
5071 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 *
5073 * "arg" must point to the first non-white of the expression.
5074 * "arg" is advanced to the next non-white after the recognized expression.
5075 *
5076 * Return OK or FAIL.
5077 */
5078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005079eval7(
5080 char_u **arg,
5081 typval_T *rettv,
5082 int evaluate,
5083 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 long n;
5086 int len;
5087 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 char_u *start_leader, *end_leader;
5089 int ret = OK;
5090 char_u *alias;
5091
5092 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097
5098 /*
5099 * Skip '!' and '-' characters. They are handled later.
5100 */
5101 start_leader = *arg;
5102 while (**arg == '!' || **arg == '-' || **arg == '+')
5103 *arg = skipwhite(*arg + 1);
5104 end_leader = *arg;
5105
5106 switch (**arg)
5107 {
5108 /*
5109 * Number constant.
5110 */
5111 case '0':
5112 case '1':
5113 case '2':
5114 case '3':
5115 case '4':
5116 case '5':
5117 case '6':
5118 case '7':
5119 case '8':
5120 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005121 {
5122#ifdef FEAT_FLOAT
5123 char_u *p = skipdigits(*arg + 1);
5124 int get_float = FALSE;
5125
5126 /* We accept a float when the format matches
5127 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005128 * strict to avoid backwards compatibility problems.
5129 * Don't look for a float after the "." operator, so that
5130 * ":let vers = 1.2.3" doesn't fail. */
5131 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005133 get_float = TRUE;
5134 p = skipdigits(p + 2);
5135 if (*p == 'e' || *p == 'E')
5136 {
5137 ++p;
5138 if (*p == '-' || *p == '+')
5139 ++p;
5140 if (!vim_isdigit(*p))
5141 get_float = FALSE;
5142 else
5143 p = skipdigits(p + 1);
5144 }
5145 if (ASCII_ISALPHA(*p) || *p == '.')
5146 get_float = FALSE;
5147 }
5148 if (get_float)
5149 {
5150 float_T f;
5151
5152 *arg += string2float(*arg, &f);
5153 if (evaluate)
5154 {
5155 rettv->v_type = VAR_FLOAT;
5156 rettv->vval.v_float = f;
5157 }
5158 }
5159 else
5160#endif
5161 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005162 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005163 *arg += len;
5164 if (evaluate)
5165 {
5166 rettv->v_type = VAR_NUMBER;
5167 rettv->vval.v_number = n;
5168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173 /*
5174 * String constant: "string".
5175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178
5179 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 break;
5184
5185 /*
5186 * List: [expr, expr]
5187 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 break;
5190
5191 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 * Dictionary: {key: val, key: val}
5193 */
5194 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5195 break;
5196
5197 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005198 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005200 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 break;
5202
5203 /*
5204 * Environment variable: $VAR.
5205 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
5208
5209 /*
5210 * Register contents: @r.
5211 */
5212 case '@': ++*arg;
5213 if (evaluate)
5214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005215 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005216 rettv->vval.v_string = get_reg_contents(**arg,
5217 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219 if (**arg != NUL)
5220 ++*arg;
5221 break;
5222
5223 /*
5224 * nested expression: (expression).
5225 */
5226 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (**arg == ')')
5229 ++*arg;
5230 else if (ret == OK)
5231 {
5232 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005233 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 ret = FAIL;
5235 }
5236 break;
5237
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 break;
5240 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241
5242 if (ret == NOTDONE)
5243 {
5244 /*
5245 * Must be a variable or function name.
5246 * Can also be a curly-braces kind of name: {expr}.
5247 */
5248 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005249 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (alias != NULL)
5251 s = alias;
5252
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005253 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 ret = FAIL;
5255 else
5256 {
5257 if (**arg == '(') /* recursive! */
5258 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005259 partial_T *partial;
5260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 /* If "s" is the name of a variable of type VAR_FUNC
5262 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005263 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264
5265 /* Invoke the function. */
5266 ret = get_func_tv(s, len, rettv, arg,
5267 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005268 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005269
5270 /* If evaluate is FALSE rettv->v_type was not set in
5271 * get_func_tv, but it's needed in handle_subscript() to parse
5272 * what follows. So set it here. */
5273 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5274 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005275 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005276 rettv->v_type = VAR_FUNC;
5277 }
5278
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 /* Stop the expression evaluation when immediately
5280 * aborting on error, or when an interrupt occurred or
5281 * an exception was thrown but not caught. */
5282 if (aborting())
5283 {
5284 if (ret == OK)
5285 clear_tv(rettv);
5286 ret = FAIL;
5287 }
5288 }
5289 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005290 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005291 else
5292 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005294 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 }
5296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 *arg = skipwhite(*arg);
5298
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005299 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5300 * expr(expr). */
5301 if (ret == OK)
5302 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 /*
5305 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5306 */
5307 if (ret == OK && evaluate && end_leader > start_leader)
5308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005310 int val = 0;
5311#ifdef FEAT_FLOAT
5312 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005314 if (rettv->v_type == VAR_FLOAT)
5315 f = rettv->vval.v_float;
5316 else
5317#endif
5318 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 clear_tv(rettv);
5322 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 else
5325 {
5326 while (end_leader > start_leader)
5327 {
5328 --end_leader;
5329 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005330 {
5331#ifdef FEAT_FLOAT
5332 if (rettv->v_type == VAR_FLOAT)
5333 f = !f;
5334 else
5335#endif
5336 val = !val;
5337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339 {
5340#ifdef FEAT_FLOAT
5341 if (rettv->v_type == VAR_FLOAT)
5342 f = -f;
5343 else
5344#endif
5345 val = -val;
5346 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005347 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005348#ifdef FEAT_FLOAT
5349 if (rettv->v_type == VAR_FLOAT)
5350 {
5351 clear_tv(rettv);
5352 rettv->vval.v_float = f;
5353 }
5354 else
5355#endif
5356 {
5357 clear_tv(rettv);
5358 rettv->v_type = VAR_NUMBER;
5359 rettv->vval.v_number = val;
5360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363
5364 return ret;
5365}
5366
5367/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005368 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5369 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5371 */
5372 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005373eval_index(
5374 char_u **arg,
5375 typval_T *rettv,
5376 int evaluate,
5377 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378{
5379 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005380 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 long len = -1;
5383 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386
Bram Moolenaara03f2332016-02-06 18:09:59 +01005387 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005389 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005390 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005391 if (verbose)
5392 EMSG(_("E695: Cannot index a Funcref"));
5393 return FAIL;
5394 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005395#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005396 if (verbose)
5397 EMSG(_(e_float_as_string));
5398 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005399#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005400 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005401 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005402 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005403 if (verbose)
5404 EMSG(_("E909: Cannot index a special variable"));
5405 return FAIL;
5406 case VAR_UNKNOWN:
5407 if (evaluate)
5408 return FAIL;
5409 /* FALLTHROUGH */
5410
5411 case VAR_STRING:
5412 case VAR_NUMBER:
5413 case VAR_LIST:
5414 case VAR_DICT:
5415 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005416 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005418 init_tv(&var1);
5419 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 /*
5423 * dict.name
5424 */
5425 key = *arg + 1;
5426 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5427 ;
5428 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 }
5432 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 /*
5435 * something[idx]
5436 *
5437 * Get the (first) variable from inside the [].
5438 */
5439 *arg = skipwhite(*arg + 1);
5440 if (**arg == ':')
5441 empty1 = TRUE;
5442 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5443 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005444 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5445 {
5446 /* not a number or string */
5447 clear_tv(&var1);
5448 return FAIL;
5449 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450
5451 /*
5452 * Get the second variable from inside the [:].
5453 */
5454 if (**arg == ':')
5455 {
5456 range = TRUE;
5457 *arg = skipwhite(*arg + 1);
5458 if (**arg == ']')
5459 empty2 = TRUE;
5460 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005462 if (!empty1)
5463 clear_tv(&var1);
5464 return FAIL;
5465 }
5466 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5467 {
5468 /* not a number or string */
5469 if (!empty1)
5470 clear_tv(&var1);
5471 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472 return FAIL;
5473 }
5474 }
5475
5476 /* Check for the ']'. */
5477 if (**arg != ']')
5478 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005479 if (verbose)
5480 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481 clear_tv(&var1);
5482 if (range)
5483 clear_tv(&var2);
5484 return FAIL;
5485 }
5486 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 }
5488
5489 if (evaluate)
5490 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 n1 = 0;
5492 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 n1 = get_tv_number(&var1);
5495 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 }
5497 if (range)
5498 {
5499 if (empty2)
5500 n2 = -1;
5501 else
5502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 n2 = get_tv_number(&var2);
5504 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 }
5506 }
5507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005510 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005511 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005512 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005513 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005514 case VAR_SPECIAL:
5515 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005516 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005517 break; /* not evaluating, skipping over subscript */
5518
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 case VAR_NUMBER:
5520 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 len = (long)STRLEN(s);
5523 if (range)
5524 {
5525 /* The resulting variable is a substring. If the indexes
5526 * are out of range the result is empty. */
5527 if (n1 < 0)
5528 {
5529 n1 = len + n1;
5530 if (n1 < 0)
5531 n1 = 0;
5532 }
5533 if (n2 < 0)
5534 n2 = len + n2;
5535 else if (n2 >= len)
5536 n2 = len;
5537 if (n1 >= len || n2 < 0 || n1 > n2)
5538 s = NULL;
5539 else
5540 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5541 }
5542 else
5543 {
5544 /* The resulting variable is a string of a single
5545 * character. If the index is too big or negative the
5546 * result is empty. */
5547 if (n1 >= len || n1 < 0)
5548 s = NULL;
5549 else
5550 s = vim_strnsave(s + n1, 1);
5551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 clear_tv(rettv);
5553 rettv->v_type = VAR_STRING;
5554 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 break;
5556
5557 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 if (n1 < 0)
5560 n1 = len + n1;
5561 if (!empty1 && (n1 < 0 || n1 >= len))
5562 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005563 /* For a range we allow invalid values and return an empty
5564 * list. A list index out of range is an error. */
5565 if (!range)
5566 {
5567 if (verbose)
5568 EMSGN(_(e_listidx), n1);
5569 return FAIL;
5570 }
5571 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 }
5573 if (range)
5574 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005575 list_T *l;
5576 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577
5578 if (n2 < 0)
5579 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005580 else if (n2 >= len)
5581 n2 = len - 1;
5582 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005583 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 l = list_alloc();
5585 if (l == NULL)
5586 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005587 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 n1 <= n2; ++n1)
5589 {
5590 if (list_append_tv(l, &item->li_tv) == FAIL)
5591 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005592 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 return FAIL;
5594 }
5595 item = item->li_next;
5596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597 clear_tv(rettv);
5598 rettv->v_type = VAR_LIST;
5599 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005600 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 }
5602 else
5603 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005604 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 clear_tv(rettv);
5606 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005607 }
5608 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609
5610 case VAR_DICT:
5611 if (range)
5612 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005613 if (verbose)
5614 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 if (len == -1)
5616 clear_tv(&var1);
5617 return FAIL;
5618 }
5619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621
5622 if (len == -1)
5623 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005624 key = get_tv_string_chk(&var1);
5625 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 clear_tv(&var1);
5628 return FAIL;
5629 }
5630 }
5631
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005632 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005634 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005635 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 if (len == -1)
5637 clear_tv(&var1);
5638 if (item == NULL)
5639 return FAIL;
5640
5641 copy_tv(&item->di_tv, &var1);
5642 clear_tv(rettv);
5643 *rettv = var1;
5644 }
5645 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 }
5647 }
5648
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 return OK;
5650}
5651
5652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 * Get an option value.
5654 * "arg" points to the '&' or '+' before the option name.
5655 * "arg" is advanced to character after the option name.
5656 * Return OK or FAIL.
5657 */
5658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005659get_option_tv(
5660 char_u **arg,
5661 typval_T *rettv, /* when NULL, only check if option exists */
5662 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664 char_u *option_end;
5665 long numval;
5666 char_u *stringval;
5667 int opt_type;
5668 int c;
5669 int working = (**arg == '+'); /* has("+option") */
5670 int ret = OK;
5671 int opt_flags;
5672
5673 /*
5674 * Isolate the option name and find its value.
5675 */
5676 option_end = find_option_end(arg, &opt_flags);
5677 if (option_end == NULL)
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 EMSG2(_("E112: Option name missing: %s"), *arg);
5681 return FAIL;
5682 }
5683
5684 if (!evaluate)
5685 {
5686 *arg = option_end;
5687 return OK;
5688 }
5689
5690 c = *option_end;
5691 *option_end = NUL;
5692 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
5695 if (opt_type == -3) /* invalid name */
5696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 EMSG2(_("E113: Unknown option: %s"), *arg);
5699 ret = FAIL;
5700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
5703 if (opt_type == -2) /* hidden string option */
5704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 rettv->v_type = VAR_STRING;
5706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708 else if (opt_type == -1) /* hidden number option */
5709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005710 rettv->v_type = VAR_NUMBER;
5711 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713 else if (opt_type == 1) /* number option */
5714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005715 rettv->v_type = VAR_NUMBER;
5716 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 else /* string option */
5719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005720 rettv->v_type = VAR_STRING;
5721 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723 }
5724 else if (working && (opt_type == -2 || opt_type == -1))
5725 ret = FAIL;
5726
5727 *option_end = c; /* put back for error messages */
5728 *arg = option_end;
5729
5730 return ret;
5731}
5732
5733/*
5734 * Allocate a variable for a string constant.
5735 * Return OK or FAIL.
5736 */
5737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005738get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 char_u *p;
5741 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 int extra = 0;
5743
5744 /*
5745 * Find the end of the string, skipping backslashed characters.
5746 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
5749 if (*p == '\\' && p[1] != NUL)
5750 {
5751 ++p;
5752 /* A "\<x>" form occupies at least 4 characters, and produces up
5753 * to 6 characters: reserve space for 2 extra */
5754 if (*p == '<')
5755 extra += 2;
5756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758
5759 if (*p != '"')
5760 {
5761 EMSG2(_("E114: Missing quote: %s"), *arg);
5762 return FAIL;
5763 }
5764
5765 /* If only parsing, set *arg and return here */
5766 if (!evaluate)
5767 {
5768 *arg = p + 1;
5769 return OK;
5770 }
5771
5772 /*
5773 * Copy the string into allocated memory, handling backslashed
5774 * characters.
5775 */
5776 name = alloc((unsigned)(p - *arg + extra));
5777 if (name == NULL)
5778 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 rettv->v_type = VAR_STRING;
5780 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 {
5784 if (*p == '\\')
5785 {
5786 switch (*++p)
5787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 case 'b': *name++ = BS; ++p; break;
5789 case 'e': *name++ = ESC; ++p; break;
5790 case 'f': *name++ = FF; ++p; break;
5791 case 'n': *name++ = NL; ++p; break;
5792 case 'r': *name++ = CAR; ++p; break;
5793 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 case 'X': /* hex: "\x1", "\x12" */
5796 case 'x':
5797 case 'u': /* Unicode: "\u0023" */
5798 case 'U':
5799 if (vim_isxdigit(p[1]))
5800 {
5801 int n, nr;
5802 int c = toupper(*p);
5803
5804 if (c == 'X')
5805 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005806 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005808 else
5809 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 nr = 0;
5811 while (--n >= 0 && vim_isxdigit(p[1]))
5812 {
5813 ++p;
5814 nr = (nr << 4) + hex2nr(*p);
5815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817#ifdef FEAT_MBYTE
5818 /* For "\u" store the number according to
5819 * 'encoding'. */
5820 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 else
5823#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827
5828 /* octal: "\1", "\12", "\123" */
5829 case '0':
5830 case '1':
5831 case '2':
5832 case '3':
5833 case '4':
5834 case '5':
5835 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 case '7': *name = *p++ - '0';
5837 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 *name = (*name << 3) + *p++ - '0';
5840 if (*p >= '0' && *p <= '7')
5841 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 break;
5845
5846 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if (extra != 0)
5849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852 }
5853 /* FALLTHROUGH */
5854
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 break;
5857 }
5858 }
5859 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 *arg = p + 1;
5865
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 return OK;
5867}
5868
5869/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 * Return OK or FAIL.
5872 */
5873 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005874get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 int reduce = 0;
5879
5880 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 */
5883 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5884 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 break;
5889 ++reduce;
5890 ++p;
5891 }
5892 }
5893
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 return FAIL;
5898 }
5899
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 if (!evaluate)
5902 {
5903 *arg = p + 1;
5904 return OK;
5905 }
5906
5907 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005909 */
5910 str = alloc((unsigned)((p - *arg) - reduce));
5911 if (str == NULL)
5912 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 rettv->v_type = VAR_STRING;
5914 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005915
Bram Moolenaar8c711452005-01-14 21:53:12 +00005916 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921 break;
5922 ++p;
5923 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 *arg = p + 1;
5928
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 return OK;
5930}
5931
Bram Moolenaarddecc252016-04-06 22:59:37 +02005932 static void
5933partial_free(partial_T *pt, int recursive)
5934{
5935 int i;
5936
5937 for (i = 0; i < pt->pt_argc; ++i)
5938 {
5939 typval_T *tv = &pt->pt_argv[i];
5940
5941 if (recursive || (tv->v_type != VAR_DICT && tv->v_type != VAR_LIST))
5942 clear_tv(tv);
5943 }
5944 vim_free(pt->pt_argv);
5945 if (recursive)
5946 dict_unref(pt->pt_dict);
5947 func_unref(pt->pt_name);
5948 vim_free(pt->pt_name);
5949 vim_free(pt);
5950}
5951
5952/*
5953 * Unreference a closure: decrement the reference count and free it when it
5954 * becomes zero.
5955 */
5956 void
5957partial_unref(partial_T *pt)
5958{
5959 if (pt != NULL && --pt->pt_refcount <= 0)
5960 partial_free(pt, TRUE);
5961}
5962
5963/*
5964 * Like clear_tv(), but do not free lists or dictionaries.
5965 * This is when called via free_unref_items().
5966 */
5967 static void
5968clear_tv_no_recurse(typval_T *tv)
5969{
5970 if (tv->v_type == VAR_PARTIAL)
5971 {
5972 partial_T *pt = tv->vval.v_partial;
5973
5974 /* We unref the partial but not the dict or any list it
5975 * refers to. */
5976 if (pt != NULL && --pt->pt_refcount == 0)
5977 partial_free(pt, FALSE);
5978 }
5979 else if (tv->v_type != VAR_LIST && tv->v_type != VAR_DICT)
5980 clear_tv(tv);
5981}
5982
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 * Allocate a variable for a List and fill it from "*arg".
5985 * Return OK or FAIL.
5986 */
5987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005988get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989{
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 list_T *l = NULL;
5991 typval_T tv;
5992 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993
5994 if (evaluate)
5995 {
5996 l = list_alloc();
5997 if (l == NULL)
5998 return FAIL;
5999 }
6000
6001 *arg = skipwhite(*arg + 1);
6002 while (**arg != ']' && **arg != NUL)
6003 {
6004 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6005 goto failret;
6006 if (evaluate)
6007 {
6008 item = listitem_alloc();
6009 if (item != NULL)
6010 {
6011 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006012 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 list_append(l, item);
6014 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006015 else
6016 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 }
6018
6019 if (**arg == ']')
6020 break;
6021 if (**arg != ',')
6022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006023 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 goto failret;
6025 }
6026 *arg = skipwhite(*arg + 1);
6027 }
6028
6029 if (**arg != ']')
6030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006031 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032failret:
6033 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006034 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 return FAIL;
6036 }
6037
6038 *arg = skipwhite(*arg + 1);
6039 if (evaluate)
6040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006041 rettv->v_type = VAR_LIST;
6042 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 ++l->lv_refcount;
6044 }
6045
6046 return OK;
6047}
6048
6049/*
6050 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006051 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006053 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006054list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006056 list_T *l;
6057
6058 l = (list_T *)alloc_clear(sizeof(list_T));
6059 if (l != NULL)
6060 {
6061 /* Prepend the list to the list of lists for garbage collection. */
6062 if (first_list != NULL)
6063 first_list->lv_used_prev = l;
6064 l->lv_used_prev = NULL;
6065 l->lv_used_next = first_list;
6066 first_list = l;
6067 }
6068 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069}
6070
6071/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006072 * Allocate an empty list for a return value.
6073 * Returns OK or FAIL.
6074 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006077{
6078 list_T *l = list_alloc();
6079
6080 if (l == NULL)
6081 return FAIL;
6082
6083 rettv->vval.v_list = l;
6084 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006085 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006086 ++l->lv_refcount;
6087 return OK;
6088}
6089
6090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 * Unreference a list: decrement the reference count and free it when it
6092 * becomes zero.
6093 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006097 if (l != NULL && --l->lv_refcount <= 0)
6098 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099}
6100
6101/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006102 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103 * Ignores the reference count.
6104 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006105 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006106list_free(
6107 list_T *l,
6108 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006109{
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006112 /* Remove the list from the list of lists for garbage collection. */
6113 if (l->lv_used_prev == NULL)
6114 first_list = l->lv_used_next;
6115 else
6116 l->lv_used_prev->lv_used_next = l->lv_used_next;
6117 if (l->lv_used_next != NULL)
6118 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6119
Bram Moolenaard9fba312005-06-26 22:34:35 +00006120 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006121 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006122 /* Remove the item before deleting it. */
6123 l->lv_first = item->li_next;
Bram Moolenaarddecc252016-04-06 22:59:37 +02006124 if (recurse)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006125 clear_tv(&item->li_tv);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006126 else
6127 clear_tv_no_recurse(&item->li_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006128 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 }
6130 vim_free(l);
6131}
6132
6133/*
6134 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006135 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006137 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006138listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139{
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141}
6142
6143/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006144 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006147listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006149 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 vim_free(item);
6151}
6152
6153/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006154 * Remove a list item from a List and free it. Also clears the value.
6155 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006158{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006159 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006160 listitem_free(item);
6161}
6162
6163/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 * Get the number of items in a list.
6165 */
6166 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 if (l == NULL)
6170 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006171 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172}
6173
6174/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 * Return TRUE when two lists have exactly the same values.
6176 */
6177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178list_equal(
6179 list_T *l1,
6180 list_T *l2,
6181 int ic, /* ignore case for strings */
6182 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183{
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006186 if (l1 == NULL || l2 == NULL)
6187 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006188 if (l1 == l2)
6189 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006190 if (list_len(l1) != list_len(l2))
6191 return FALSE;
6192
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193 for (item1 = l1->lv_first, item2 = l2->lv_first;
6194 item1 != NULL && item2 != NULL;
6195 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 return FALSE;
6198 return item1 == NULL && item2 == NULL;
6199}
6200
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006201/*
6202 * Return the dictitem that an entry in a hashtable points to.
6203 */
6204 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006205dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006206{
6207 return HI2DI(hi);
6208}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006209
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006210/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006211 * Return TRUE when two dictionaries have exactly the same key/values.
6212 */
6213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214dict_equal(
6215 dict_T *d1,
6216 dict_T *d2,
6217 int ic, /* ignore case for strings */
6218 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006219{
Bram Moolenaar33570922005-01-25 22:26:29 +00006220 hashitem_T *hi;
6221 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006223
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006224 if (d1 == NULL || d2 == NULL)
6225 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006226 if (d1 == d2)
6227 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228 if (dict_len(d1) != dict_len(d2))
6229 return FALSE;
6230
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006231 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006233 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006234 if (!HASHITEM_EMPTY(hi))
6235 {
6236 item2 = dict_find(d2, hi->hi_key, -1);
6237 if (item2 == NULL)
6238 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006239 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006240 return FALSE;
6241 --todo;
6242 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006243 }
6244 return TRUE;
6245}
6246
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006247static int tv_equal_recurse_limit;
6248
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006249/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006250 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006251 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006252 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 */
6254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006255tv_equal(
6256 typval_T *tv1,
6257 typval_T *tv2,
6258 int ic, /* ignore case */
6259 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006260{
6261 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006262 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006263 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006264 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006266 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6267 if ((tv1->v_type == VAR_FUNC
6268 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6269 && (tv2->v_type == VAR_FUNC
6270 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6271 {
6272 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6273 : tv1->vval.v_partial->pt_name;
6274 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6275 : tv2->vval.v_partial->pt_name;
6276 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6277 }
6278
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006279 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006280 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006281
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006282 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006283 * recursiveness to a limit. We guess they are equal then.
6284 * A fixed limit has the problem of still taking an awful long time.
6285 * Reduce the limit every time running into it. That should work fine for
6286 * deeply linked structures that are not recursively linked and catch
6287 * recursiveness quickly. */
6288 if (!recursive)
6289 tv_equal_recurse_limit = 1000;
6290 if (recursive_cnt >= tv_equal_recurse_limit)
6291 {
6292 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006293 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006294 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006295
6296 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006297 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006298 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006299 ++recursive_cnt;
6300 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6301 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006302 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006303
6304 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006305 ++recursive_cnt;
6306 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6307 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006308 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006309
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006310 case VAR_NUMBER:
6311 return tv1->vval.v_number == tv2->vval.v_number;
6312
6313 case VAR_STRING:
6314 s1 = get_tv_string_buf(tv1, buf1);
6315 s2 = get_tv_string_buf(tv2, buf2);
6316 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006317
6318 case VAR_SPECIAL:
6319 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006320
6321 case VAR_FLOAT:
6322#ifdef FEAT_FLOAT
6323 return tv1->vval.v_float == tv2->vval.v_float;
6324#endif
6325 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006327 return tv1->vval.v_job == tv2->vval.v_job;
6328#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006329 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006330#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006331 return tv1->vval.v_channel == tv2->vval.v_channel;
6332#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006333 case VAR_FUNC:
6334 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006335 case VAR_UNKNOWN:
6336 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006338
Bram Moolenaara03f2332016-02-06 18:09:59 +01006339 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6340 * does not equal anything, not even itself. */
6341 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006342}
6343
6344/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 * Locate item with index "n" in list "l" and return it.
6346 * A negative index is counted from the end; -1 is the last item.
6347 * Returns NULL when "n" is out of range.
6348 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006349 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351{
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353 long idx;
6354
6355 if (l == NULL)
6356 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006357
6358 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006360 n = l->lv_len + n;
6361
6362 /* Check for index out of range. */
6363 if (n < 0 || n >= l->lv_len)
6364 return NULL;
6365
6366 /* When there is a cached index may start search from there. */
6367 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006369 if (n < l->lv_idx / 2)
6370 {
6371 /* closest to the start of the list */
6372 item = l->lv_first;
6373 idx = 0;
6374 }
6375 else if (n > (l->lv_idx + l->lv_len) / 2)
6376 {
6377 /* closest to the end of the list */
6378 item = l->lv_last;
6379 idx = l->lv_len - 1;
6380 }
6381 else
6382 {
6383 /* closest to the cached index */
6384 item = l->lv_idx_item;
6385 idx = l->lv_idx;
6386 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 }
6388 else
6389 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006390 if (n < l->lv_len / 2)
6391 {
6392 /* closest to the start of the list */
6393 item = l->lv_first;
6394 idx = 0;
6395 }
6396 else
6397 {
6398 /* closest to the end of the list */
6399 item = l->lv_last;
6400 idx = l->lv_len - 1;
6401 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006403
6404 while (n > idx)
6405 {
6406 /* search forward */
6407 item = item->li_next;
6408 ++idx;
6409 }
6410 while (n < idx)
6411 {
6412 /* search backward */
6413 item = item->li_prev;
6414 --idx;
6415 }
6416
6417 /* cache the used index */
6418 l->lv_idx = idx;
6419 l->lv_idx_item = item;
6420
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 return item;
6422}
6423
6424/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006425 * Get list item "l[idx]" as a number.
6426 */
6427 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006428list_find_nr(
6429 list_T *l,
6430 long idx,
6431 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006432{
6433 listitem_T *li;
6434
6435 li = list_find(l, idx);
6436 if (li == NULL)
6437 {
6438 if (errorp != NULL)
6439 *errorp = TRUE;
6440 return -1L;
6441 }
6442 return get_tv_number_chk(&li->li_tv, errorp);
6443}
6444
6445/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006446 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6447 */
6448 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006450{
6451 listitem_T *li;
6452
6453 li = list_find(l, idx - 1);
6454 if (li == NULL)
6455 {
6456 EMSGN(_(e_listidx), idx);
6457 return NULL;
6458 }
6459 return get_tv_string(&li->li_tv);
6460}
6461
6462/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006463 * Locate "item" list "l" and return its index.
6464 * Returns -1 when "item" is not in the list.
6465 */
6466 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006467list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006468{
6469 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006470 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006471
6472 if (l == NULL)
6473 return -1;
6474 idx = 0;
6475 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6476 ++idx;
6477 if (li == NULL)
6478 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006479 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006480}
6481
6482/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * Append item "item" to the end of list "l".
6484 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006485 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006486list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487{
6488 if (l->lv_last == NULL)
6489 {
6490 /* empty list */
6491 l->lv_first = item;
6492 l->lv_last = item;
6493 item->li_prev = NULL;
6494 }
6495 else
6496 {
6497 l->lv_last->li_next = item;
6498 item->li_prev = l->lv_last;
6499 l->lv_last = item;
6500 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 item->li_next = NULL;
6503}
6504
6505/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006512 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513
Bram Moolenaar05159a02005-02-26 23:04:13 +00006514 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006516 copy_tv(tv, &li->li_tv);
6517 list_append(l, li);
6518 return OK;
6519}
6520
6521/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006522 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006523 * Return FAIL when out of memory.
6524 */
6525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006526list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006527{
6528 listitem_T *li = listitem_alloc();
6529
6530 if (li == NULL)
6531 return FAIL;
6532 li->li_tv.v_type = VAR_DICT;
6533 li->li_tv.v_lock = 0;
6534 li->li_tv.vval.v_dict = dict;
6535 list_append(list, li);
6536 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 return OK;
6538}
6539
6540/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006541 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006542 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006543 * Returns FAIL when out of memory.
6544 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006546list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006547{
6548 listitem_T *li = listitem_alloc();
6549
6550 if (li == NULL)
6551 return FAIL;
6552 list_append(l, li);
6553 li->li_tv.v_type = VAR_STRING;
6554 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006555 if (str == NULL)
6556 li->li_tv.vval.v_string = NULL;
6557 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006558 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006559 return FAIL;
6560 return OK;
6561}
6562
6563/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006564 * Append "n" to list "l".
6565 * Returns FAIL when out of memory.
6566 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006568list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006569{
6570 listitem_T *li;
6571
6572 li = listitem_alloc();
6573 if (li == NULL)
6574 return FAIL;
6575 li->li_tv.v_type = VAR_NUMBER;
6576 li->li_tv.v_lock = 0;
6577 li->li_tv.vval.v_number = n;
6578 list_append(l, li);
6579 return OK;
6580}
6581
6582/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584 * If "item" is NULL append at the end.
6585 * Return FAIL when out of memory.
6586 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006587 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006588list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589{
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591
6592 if (ni == NULL)
6593 return FAIL;
6594 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006595 list_insert(l, ni, item);
6596 return OK;
6597}
6598
6599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006600list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006601{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602 if (item == NULL)
6603 /* Append new item at end of list. */
6604 list_append(l, ni);
6605 else
6606 {
6607 /* Insert new item before existing item. */
6608 ni->li_prev = item->li_prev;
6609 ni->li_next = item;
6610 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006611 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006613 ++l->lv_idx;
6614 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006615 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006616 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006617 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006618 l->lv_idx_item = NULL;
6619 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006620 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006621 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006622 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006623}
6624
6625/*
6626 * Extend "l1" with "l2".
6627 * If "bef" is NULL append at the end, otherwise insert before this item.
6628 * Returns FAIL when out of memory.
6629 */
6630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006631list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632{
Bram Moolenaar33570922005-01-25 22:26:29 +00006633 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006634 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006636 /* We also quit the loop when we have inserted the original item count of
6637 * the list, avoid a hang when we extend a list with itself. */
6638 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6640 return FAIL;
6641 return OK;
6642}
6643
6644/*
6645 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6646 * Return FAIL when out of memory.
6647 */
6648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006649list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650{
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006653 if (l1 == NULL || l2 == NULL)
6654 return FAIL;
6655
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006656 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006657 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006658 if (l == NULL)
6659 return FAIL;
6660 tv->v_type = VAR_LIST;
6661 tv->vval.v_list = l;
6662
6663 /* append all items from the second list */
6664 return list_extend(l, l2, NULL);
6665}
6666
6667/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006668 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006670 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 * Returns NULL when out of memory.
6672 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675{
Bram Moolenaar33570922005-01-25 22:26:29 +00006676 list_T *copy;
6677 listitem_T *item;
6678 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679
6680 if (orig == NULL)
6681 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682
6683 copy = list_alloc();
6684 if (copy != NULL)
6685 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006686 if (copyID != 0)
6687 {
6688 /* Do this before adding the items, because one of the items may
6689 * refer back to this list. */
6690 orig->lv_copyID = copyID;
6691 orig->lv_copylist = copy;
6692 }
6693 for (item = orig->lv_first; item != NULL && !got_int;
6694 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695 {
6696 ni = listitem_alloc();
6697 if (ni == NULL)
6698 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006699 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006700 {
6701 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6702 {
6703 vim_free(ni);
6704 break;
6705 }
6706 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006708 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709 list_append(copy, ni);
6710 }
6711 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 if (item != NULL)
6713 {
6714 list_unref(copy);
6715 copy = NULL;
6716 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006717 }
6718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 return copy;
6720}
6721
6722/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006723 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006724 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006725 * This used to be called list_remove, but that conflicts with a Sun header
6726 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006729vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006730{
Bram Moolenaar33570922005-01-25 22:26:29 +00006731 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006732
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733 /* notify watchers */
6734 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006736 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006737 list_fix_watch(l, ip);
6738 if (ip == item2)
6739 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006740 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741
6742 if (item2->li_next == NULL)
6743 l->lv_last = item->li_prev;
6744 else
6745 item2->li_next->li_prev = item->li_prev;
6746 if (item->li_prev == NULL)
6747 l->lv_first = item2->li_next;
6748 else
6749 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006750 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751}
6752
6753/*
6754 * Return an allocated string with the string representation of a list.
6755 * May return NULL.
6756 */
6757 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006758list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006759{
6760 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761
6762 if (tv->vval.v_list == NULL)
6763 return NULL;
6764 ga_init2(&ga, (int)sizeof(char), 80);
6765 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006766 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 {
6768 vim_free(ga.ga_data);
6769 return NULL;
6770 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006771 ga_append(&ga, ']');
6772 ga_append(&ga, NUL);
6773 return (char_u *)ga.ga_data;
6774}
6775
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006776typedef struct join_S {
6777 char_u *s;
6778 char_u *tofree;
6779} join_T;
6780
6781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006782list_join_inner(
6783 garray_T *gap, /* to store the result in */
6784 list_T *l,
6785 char_u *sep,
6786 int echo_style,
6787 int copyID,
6788 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006789{
6790 int i;
6791 join_T *p;
6792 int len;
6793 int sumlen = 0;
6794 int first = TRUE;
6795 char_u *tofree;
6796 char_u numbuf[NUMBUFLEN];
6797 listitem_T *item;
6798 char_u *s;
6799
6800 /* Stringify each item in the list. */
6801 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6802 {
6803 if (echo_style)
6804 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6805 else
6806 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6807 if (s == NULL)
6808 return FAIL;
6809
6810 len = (int)STRLEN(s);
6811 sumlen += len;
6812
Bram Moolenaarcde88542015-08-11 19:14:00 +02006813 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6815 if (tofree != NULL || s != numbuf)
6816 {
6817 p->s = s;
6818 p->tofree = tofree;
6819 }
6820 else
6821 {
6822 p->s = vim_strnsave(s, len);
6823 p->tofree = p->s;
6824 }
6825
6826 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006827 if (did_echo_string_emsg) /* recursion error, bail out */
6828 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006829 }
6830
6831 /* Allocate result buffer with its total size, avoid re-allocation and
6832 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6833 if (join_gap->ga_len >= 2)
6834 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6835 if (ga_grow(gap, sumlen + 2) == FAIL)
6836 return FAIL;
6837
6838 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6839 {
6840 if (first)
6841 first = FALSE;
6842 else
6843 ga_concat(gap, sep);
6844 p = ((join_T *)join_gap->ga_data) + i;
6845
6846 if (p->s != NULL)
6847 ga_concat(gap, p->s);
6848 line_breakcheck();
6849 }
6850
6851 return OK;
6852}
6853
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006854/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006855 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006856 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006857 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006858 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860list_join(
6861 garray_T *gap,
6862 list_T *l,
6863 char_u *sep,
6864 int echo_style,
6865 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006866{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006867 garray_T join_ga;
6868 int retval;
6869 join_T *p;
6870 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006871
Bram Moolenaard39a7512015-04-16 22:51:22 +02006872 if (l->lv_len < 1)
6873 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006874 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6875 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6876
6877 /* Dispose each item in join_ga. */
6878 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006879 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006880 p = (join_T *)join_ga.ga_data;
6881 for (i = 0; i < join_ga.ga_len; ++i)
6882 {
6883 vim_free(p->tofree);
6884 ++p;
6885 }
6886 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006887 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006888
6889 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006890}
6891
6892/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006893 * Return the next (unique) copy ID.
6894 * Used for serializing nested structures.
6895 */
6896 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006897get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006898{
6899 current_copyID += COPYID_INC;
6900 return current_copyID;
6901}
6902
6903/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 * Garbage collection for lists and dictionaries.
6905 *
6906 * We use reference counts to be able to free most items right away when they
6907 * are no longer used. But for composite items it's possible that it becomes
6908 * unused while the reference count is > 0: When there is a recursive
6909 * reference. Example:
6910 * :let l = [1, 2, 3]
6911 * :let d = {9: l}
6912 * :let l[1] = d
6913 *
6914 * Since this is quite unusual we handle this with garbage collection: every
6915 * once in a while find out which lists and dicts are not referenced from any
6916 * variable.
6917 *
6918 * Here is a good reference text about garbage collection (refers to Python
6919 * but it applies to all reference-counting mechanisms):
6920 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006921 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006922
6923/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 * Do garbage collection for lists and dicts.
6925 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006926 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006928garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006929{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006930 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006932 buf_T *buf;
6933 win_T *wp;
6934 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006935 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006936 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006937 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006938#ifdef FEAT_WINDOWS
6939 tabpage_T *tp;
6940#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006942 /* Only do this once. */
6943 want_garbage_collect = FALSE;
6944 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006945 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006946
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 /* We advance by two because we add one for items referenced through
6948 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006949 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006950
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 /*
6952 * 1. Go through all accessible variables and mark all lists and dicts
6953 * with copyID.
6954 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955
6956 /* Don't free variables in the previous_funccal list unless they are only
6957 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006958 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6960 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6962 NULL);
6963 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6964 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 }
6966
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 /* script-local variables */
6968 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006969 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970
6971 /* buffer-local variables */
6972 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006973 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6974 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975
6976 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006977 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006978 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6979 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006980#ifdef FEAT_AUTOCMD
6981 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006982 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6983 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006984#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006986#ifdef FEAT_WINDOWS
6987 /* tabpage-local variables */
6988 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6990 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006991#endif
6992
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006994 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995
6996 /* function-local variables */
6997 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6998 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006999 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7000 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 }
7002
Bram Moolenaard812df62008-11-09 12:46:09 +00007003 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007004 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007005
Bram Moolenaar1dced572012-04-05 16:54:08 +02007006#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007008#endif
7009
Bram Moolenaardb913952012-06-29 12:54:53 +02007010#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007012#endif
7013
7014#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007016#endif
7017
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007018#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007019 abort = abort || set_ref_in_channel(copyID);
7020#endif
7021
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007023 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 /*
7025 * 2. Free lists and dictionaries that are not referenced.
7026 */
7027 did_free = free_unref_items(copyID);
7028
7029 /*
7030 * 3. Check if any funccal can be freed now.
7031 */
7032 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007033 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 if (can_free_funccal(*pfc, copyID))
7035 {
7036 fc = *pfc;
7037 *pfc = fc->caller;
7038 free_funccal(fc, TRUE);
7039 did_free = TRUE;
7040 did_free_funccal = TRUE;
7041 }
7042 else
7043 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007044 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 if (did_free_funccal)
7046 /* When a funccal was freed some more items might be garbage
7047 * collected, so run again. */
7048 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 else if (p_verbose > 0)
7051 {
7052 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7053 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007054
7055 return did_free;
7056}
7057
7058/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01007059 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007060 */
7061 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007062free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007063{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007064 dict_T *dd, *dd_next;
7065 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007066 int did_free = FALSE;
7067
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007069 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 */
7071 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007072 {
7073 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007076 /* Free the Dictionary and ordinary items it contains, but don't
7077 * recurse into Lists and Dictionaries, they will be in the list
7078 * of dicts or list of lists. */
7079 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007082 dd = dd_next;
7083 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084
7085 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007086 * Go through the list of lists and free items without the copyID.
7087 * But don't free a list that has a watcher (used in a for loop), these
7088 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089 */
7090 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007091 {
7092 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007093 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7094 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007096 /* Free the List and ordinary items it contains, but don't recurse
7097 * into Lists and Dictionaries, they will be in the list of dicts
7098 * or list of lists. */
7099 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007102 ll = ll_next;
7103 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007104
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105 return did_free;
7106}
7107
7108/*
7109 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 * "list_stack" is used to add lists to be marked. Can be NULL.
7111 *
7112 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007115set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116{
7117 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007120 hashtab_T *cur_ht;
7121 ht_stack_T *ht_stack = NULL;
7122 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007123
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007124 cur_ht = ht;
7125 for (;;)
7126 {
7127 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007128 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007129 /* Mark each item in the hashtab. If the item contains a hashtab
7130 * it is added to ht_stack, if it contains a list it is added to
7131 * list_stack. */
7132 todo = (int)cur_ht->ht_used;
7133 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7134 if (!HASHITEM_EMPTY(hi))
7135 {
7136 --todo;
7137 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7138 &ht_stack, list_stack);
7139 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007140 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007141
7142 if (ht_stack == NULL)
7143 break;
7144
7145 /* take an item from the stack */
7146 cur_ht = ht_stack->ht;
7147 tempitem = ht_stack;
7148 ht_stack = ht_stack->prev;
7149 free(tempitem);
7150 }
7151
7152 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007153}
7154
7155/*
7156 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007157 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7158 *
7159 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007160 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007162set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007163{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007164 listitem_T *li;
7165 int abort = FALSE;
7166 list_T *cur_l;
7167 list_stack_T *list_stack = NULL;
7168 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007169
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007170 cur_l = l;
7171 for (;;)
7172 {
7173 if (!abort)
7174 /* Mark each item in the list. If the item contains a hashtab
7175 * it is added to ht_stack, if it contains a list it is added to
7176 * list_stack. */
7177 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7178 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7179 ht_stack, &list_stack);
7180 if (list_stack == NULL)
7181 break;
7182
7183 /* take an item from the stack */
7184 cur_l = list_stack->list;
7185 tempitem = list_stack;
7186 list_stack = list_stack->prev;
7187 free(tempitem);
7188 }
7189
7190 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191}
7192
7193/*
7194 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007195 * "list_stack" is used to add lists to be marked. Can be NULL.
7196 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7197 *
7198 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007199 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007200 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007201set_ref_in_item(
7202 typval_T *tv,
7203 int copyID,
7204 ht_stack_T **ht_stack,
7205 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007206{
7207 dict_T *dd;
7208 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007209 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007210
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007211 if (tv->v_type == VAR_DICT || tv->v_type == VAR_PARTIAL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007212 {
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007213 if (tv->v_type == VAR_DICT)
7214 dd = tv->vval.v_dict;
7215 else if (tv->vval.v_partial != NULL)
7216 dd = tv->vval.v_partial->pt_dict;
7217 else
7218 dd = NULL;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007219 if (dd != NULL && dd->dv_copyID != copyID)
7220 {
7221 /* Didn't see this dict yet. */
7222 dd->dv_copyID = copyID;
7223 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007224 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007225 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7226 }
7227 else
7228 {
7229 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7230 if (newitem == NULL)
7231 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007232 else
7233 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007234 newitem->ht = &dd->dv_hashtab;
7235 newitem->prev = *ht_stack;
7236 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007237 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007238 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007239 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02007240 if (tv->v_type == VAR_PARTIAL)
7241 {
7242 partial_T *pt = tv->vval.v_partial;
7243 int i;
7244
7245 if (pt != NULL)
7246 for (i = 0; i < pt->pt_argc; ++i)
7247 set_ref_in_item(&pt->pt_argv[i], copyID,
7248 ht_stack, list_stack);
7249 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007250 }
7251 else if (tv->v_type == VAR_LIST)
7252 {
7253 ll = tv->vval.v_list;
7254 if (ll != NULL && ll->lv_copyID != copyID)
7255 {
7256 /* Didn't see this list yet. */
7257 ll->lv_copyID = copyID;
7258 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007259 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007260 abort = set_ref_in_list(ll, copyID, ht_stack);
7261 }
7262 else
7263 {
7264 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007265 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007266 if (newitem == NULL)
7267 abort = TRUE;
7268 else
7269 {
7270 newitem->list = ll;
7271 newitem->prev = *list_stack;
7272 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007273 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007274 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007275 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007276 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007277 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007278}
7279
7280/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 * Allocate an empty header for a dictionary.
7282 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007283 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007284dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285{
Bram Moolenaar33570922005-01-25 22:26:29 +00007286 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007290 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007291 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007292 if (first_dict != NULL)
7293 first_dict->dv_used_prev = d;
7294 d->dv_used_next = first_dict;
7295 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007296 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007297
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007299 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007300 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007301 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007302 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007303 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305}
7306
7307/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007308 * Allocate an empty dict for a return value.
7309 * Returns OK or FAIL.
7310 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007313{
7314 dict_T *d = dict_alloc();
7315
7316 if (d == NULL)
7317 return FAIL;
7318
7319 rettv->vval.v_dict = d;
7320 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007321 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007322 ++d->dv_refcount;
7323 return OK;
7324}
7325
7326
7327/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 * Unreference a Dictionary: decrement the reference count and free it when it
7329 * becomes zero.
7330 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007332dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007334 if (d != NULL && --d->dv_refcount <= 0)
7335 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336}
7337
7338/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007339 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340 * Ignores the reference count.
7341 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343dict_free(
7344 dict_T *d,
7345 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007349 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007351 /* Remove the dict from the list of dicts for garbage collection. */
7352 if (d->dv_used_prev == NULL)
7353 first_dict = d->dv_used_next;
7354 else
7355 d->dv_used_prev->dv_used_next = d->dv_used_next;
7356 if (d->dv_used_next != NULL)
7357 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7358
7359 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007360 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007361 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 if (!HASHITEM_EMPTY(hi))
7365 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007366 /* Remove the item before deleting it, just in case there is
7367 * something recursive causing trouble. */
7368 di = HI2DI(hi);
7369 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaarddecc252016-04-06 22:59:37 +02007370 if (recurse)
7371 clear_tv(&di->di_tv);
7372 else
7373 clear_tv_no_recurse(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007374 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375 --todo;
7376 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007377 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379 vim_free(d);
7380}
7381
7382/*
7383 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007384 * The "key" is copied to the new item.
7385 * Note that the value of the item "di_tv" still needs to be initialized!
7386 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007388 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390{
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007393 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007394 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007397 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007398 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400}
7401
7402/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 * Make a copy of a Dictionary item.
7404 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007406dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407{
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007410 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7411 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 if (di != NULL)
7413 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007415 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416 copy_tv(&org->di_tv, &di->di_tv);
7417 }
7418 return di;
7419}
7420
7421/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007422 * Remove item "item" from Dictionary "dict" and free it.
7423 */
7424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007425dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007426{
Bram Moolenaar33570922005-01-25 22:26:29 +00007427 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007428
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007430 if (HASHITEM_EMPTY(hi))
7431 EMSG2(_(e_intern2), "dictitem_remove()");
7432 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007433 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007434 dictitem_free(item);
7435}
7436
7437/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 * Free a dict item. Also clears the value.
7439 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007441dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007444 if (item->di_flags & DI_FLAGS_ALLOC)
7445 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446}
7447
7448/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007449 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7450 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007451 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007452 * Returns NULL when out of memory.
7453 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007455dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007456{
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 dict_T *copy;
7458 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007459 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007461
7462 if (orig == NULL)
7463 return NULL;
7464
7465 copy = dict_alloc();
7466 if (copy != NULL)
7467 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007468 if (copyID != 0)
7469 {
7470 orig->dv_copyID = copyID;
7471 orig->dv_copydict = copy;
7472 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007473 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007474 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007476 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007478 --todo;
7479
7480 di = dictitem_alloc(hi->hi_key);
7481 if (di == NULL)
7482 break;
7483 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007484 {
7485 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7486 copyID) == FAIL)
7487 {
7488 vim_free(di);
7489 break;
7490 }
7491 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492 else
7493 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7494 if (dict_add(copy, di) == FAIL)
7495 {
7496 dictitem_free(di);
7497 break;
7498 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007500 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501
Bram Moolenaare9a41262005-01-15 22:18:47 +00007502 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007503 if (todo > 0)
7504 {
7505 dict_unref(copy);
7506 copy = NULL;
7507 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508 }
7509
7510 return copy;
7511}
7512
7513/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007515 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007518dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519{
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521}
7522
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007524 * Add a number or string entry to dictionary "d".
7525 * When "str" is NULL use number "nr", otherwise use "str".
7526 * Returns FAIL when out of memory and when key already exists.
7527 */
7528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007529dict_add_nr_str(
7530 dict_T *d,
7531 char *key,
7532 long nr,
7533 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007534{
7535 dictitem_T *item;
7536
7537 item = dictitem_alloc((char_u *)key);
7538 if (item == NULL)
7539 return FAIL;
7540 item->di_tv.v_lock = 0;
7541 if (str == NULL)
7542 {
7543 item->di_tv.v_type = VAR_NUMBER;
7544 item->di_tv.vval.v_number = nr;
7545 }
7546 else
7547 {
7548 item->di_tv.v_type = VAR_STRING;
7549 item->di_tv.vval.v_string = vim_strsave(str);
7550 }
7551 if (dict_add(d, item) == FAIL)
7552 {
7553 dictitem_free(item);
7554 return FAIL;
7555 }
7556 return OK;
7557}
7558
7559/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007560 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007561 * Returns FAIL when out of memory and when key already exists.
7562 */
7563 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007565{
7566 dictitem_T *item;
7567
7568 item = dictitem_alloc((char_u *)key);
7569 if (item == NULL)
7570 return FAIL;
7571 item->di_tv.v_lock = 0;
7572 item->di_tv.v_type = VAR_LIST;
7573 item->di_tv.vval.v_list = list;
7574 if (dict_add(d, item) == FAIL)
7575 {
7576 dictitem_free(item);
7577 return FAIL;
7578 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007579 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007580 return OK;
7581}
7582
7583/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007584 * Get the number of items in a Dictionary.
7585 */
7586 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007587dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007588{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 if (d == NULL)
7590 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007591 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007592}
7593
7594/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 * Find item "key[len]" in Dictionary "d".
7596 * If "len" is negative use strlen(key).
7597 * Returns NULL when not found.
7598 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007599 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007600dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007602#define AKEYLEN 200
7603 char_u buf[AKEYLEN];
7604 char_u *akey;
7605 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007608 if (len < 0)
7609 akey = key;
7610 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007612 tofree = akey = vim_strnsave(key, len);
7613 if (akey == NULL)
7614 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007615 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007616 else
7617 {
7618 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007619 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007620 akey = buf;
7621 }
7622
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007624 vim_free(tofree);
7625 if (HASHITEM_EMPTY(hi))
7626 return NULL;
7627 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628}
7629
7630/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007631 * Get a string item from a dictionary.
7632 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007633 * Returns NULL if the entry doesn't exist or out of memory.
7634 */
7635 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007636get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007637{
7638 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007639 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007640
7641 di = dict_find(d, key, -1);
7642 if (di == NULL)
7643 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007644 s = get_tv_string(&di->di_tv);
7645 if (save && s != NULL)
7646 s = vim_strsave(s);
7647 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007648}
7649
7650/*
7651 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007652 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007653 */
7654 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007655get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007656{
7657 dictitem_T *di;
7658
7659 di = dict_find(d, key, -1);
7660 if (di == NULL)
7661 return 0;
7662 return get_tv_number(&di->di_tv);
7663}
7664
7665/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 * Return an allocated string with the string representation of a Dictionary.
7667 * May return NULL.
7668 */
7669 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007670dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671{
7672 garray_T ga;
7673 int first = TRUE;
7674 char_u *tofree;
7675 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007676 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007679 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007681 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 return NULL;
7683 ga_init2(&ga, (int)sizeof(char), 80);
7684 ga_append(&ga, '{');
7685
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007686 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007687 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007689 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 --todo;
7692
7693 if (first)
7694 first = FALSE;
7695 else
7696 ga_concat(&ga, (char_u *)", ");
7697
7698 tofree = string_quote(hi->hi_key, FALSE);
7699 if (tofree != NULL)
7700 {
7701 ga_concat(&ga, tofree);
7702 vim_free(tofree);
7703 }
7704 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007705 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007706 if (s != NULL)
7707 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007709 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007710 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007711 line_breakcheck();
7712
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007715 if (todo > 0)
7716 {
7717 vim_free(ga.ga_data);
7718 return NULL;
7719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720
7721 ga_append(&ga, '}');
7722 ga_append(&ga, NUL);
7723 return (char_u *)ga.ga_data;
7724}
7725
7726/*
7727 * Allocate a variable for a Dictionary and fill it from "*arg".
7728 * Return OK or FAIL. Returns NOTDONE for {expr}.
7729 */
7730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007731get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732{
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 dict_T *d = NULL;
7734 typval_T tvkey;
7735 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007736 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740
7741 /*
7742 * First check if it's not a curly-braces thing: {expr}.
7743 * Must do this without evaluating, otherwise a function may be called
7744 * twice. Unfortunately this means we need to call eval1() twice for the
7745 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007746 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007748 if (*start != '}')
7749 {
7750 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7751 return FAIL;
7752 if (*start == '}')
7753 return NOTDONE;
7754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755
7756 if (evaluate)
7757 {
7758 d = dict_alloc();
7759 if (d == NULL)
7760 return FAIL;
7761 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007762 tvkey.v_type = VAR_UNKNOWN;
7763 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764
7765 *arg = skipwhite(*arg + 1);
7766 while (**arg != '}' && **arg != NUL)
7767 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007768 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007769 goto failret;
7770 if (**arg != ':')
7771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007772 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007773 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774 goto failret;
7775 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007776 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007778 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007779 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007780 {
7781 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007782 clear_tv(&tvkey);
7783 goto failret;
7784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786
7787 *arg = skipwhite(*arg + 1);
7788 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7789 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007790 if (evaluate)
7791 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007792 goto failret;
7793 }
7794 if (evaluate)
7795 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007796 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007797 if (item != NULL)
7798 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007799 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007800 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007801 clear_tv(&tv);
7802 goto failret;
7803 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007804 item = dictitem_alloc(key);
7805 clear_tv(&tvkey);
7806 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007808 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007809 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007810 if (dict_add(d, item) == FAIL)
7811 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007812 }
7813 }
7814
7815 if (**arg == '}')
7816 break;
7817 if (**arg != ',')
7818 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007819 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007820 goto failret;
7821 }
7822 *arg = skipwhite(*arg + 1);
7823 }
7824
7825 if (**arg != '}')
7826 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007827 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828failret:
7829 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007830 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007831 return FAIL;
7832 }
7833
7834 *arg = skipwhite(*arg + 1);
7835 if (evaluate)
7836 {
7837 rettv->v_type = VAR_DICT;
7838 rettv->vval.v_dict = d;
7839 ++d->dv_refcount;
7840 }
7841
7842 return OK;
7843}
7844
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007845#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007846#endif
7847
Bram Moolenaar17a13432016-01-24 14:22:10 +01007848 static char *
7849get_var_special_name(int nr)
7850{
7851 switch (nr)
7852 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007853 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007854 case VVAL_TRUE: return "v:true";
7855 case VVAL_NONE: return "v:none";
7856 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007857 }
7858 EMSG2(_(e_intern2), "get_var_special_name()");
7859 return "42";
7860}
7861
Bram Moolenaar8c711452005-01-14 21:53:12 +00007862/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007863 * Return a string with the string representation of a variable.
7864 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007865 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007866 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007867 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007868 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869 */
7870 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007871echo_string(
7872 typval_T *tv,
7873 char_u **tofree,
7874 char_u *numbuf,
7875 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007876{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 static int recurse = 0;
7878 char_u *r = NULL;
7879
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007881 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007882 if (!did_echo_string_emsg)
7883 {
7884 /* Only give this message once for a recursive call to avoid
7885 * flooding the user with errors. And stop iterating over lists
7886 * and dicts. */
7887 did_echo_string_emsg = TRUE;
7888 EMSG(_("E724: variable nested too deep for displaying"));
7889 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007890 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007891 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007892 }
7893 ++recurse;
7894
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007895 switch (tv->v_type)
7896 {
7897 case VAR_FUNC:
7898 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007899 r = tv->vval.v_string;
7900 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007901
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007902 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007903 {
7904 partial_T *pt = tv->vval.v_partial;
7905 char_u *fname = string_quote(pt == NULL ? NULL
7906 : pt->pt_name, FALSE);
7907 garray_T ga;
7908 int i;
7909 char_u *tf;
7910
7911 ga_init2(&ga, 1, 100);
7912 ga_concat(&ga, (char_u *)"function(");
7913 if (fname != NULL)
7914 {
7915 ga_concat(&ga, fname);
7916 vim_free(fname);
7917 }
7918 if (pt != NULL && pt->pt_argc > 0)
7919 {
7920 ga_concat(&ga, (char_u *)", [");
7921 for (i = 0; i < pt->pt_argc; ++i)
7922 {
7923 if (i > 0)
7924 ga_concat(&ga, (char_u *)", ");
7925 ga_concat(&ga,
7926 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7927 vim_free(tf);
7928 }
7929 ga_concat(&ga, (char_u *)"]");
7930 }
7931 if (pt != NULL && pt->pt_dict != NULL)
7932 {
7933 typval_T dtv;
7934
7935 ga_concat(&ga, (char_u *)", ");
7936 dtv.v_type = VAR_DICT;
7937 dtv.vval.v_dict = pt->pt_dict;
7938 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7939 vim_free(tf);
7940 }
7941 ga_concat(&ga, (char_u *)")");
7942
7943 *tofree = ga.ga_data;
7944 r = *tofree;
7945 break;
7946 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007948 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007949 if (tv->vval.v_list == NULL)
7950 {
7951 *tofree = NULL;
7952 r = NULL;
7953 }
7954 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7955 {
7956 *tofree = NULL;
7957 r = (char_u *)"[...]";
7958 }
7959 else
7960 {
7961 tv->vval.v_list->lv_copyID = copyID;
7962 *tofree = list2string(tv, copyID);
7963 r = *tofree;
7964 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007965 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007966
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007968 if (tv->vval.v_dict == NULL)
7969 {
7970 *tofree = NULL;
7971 r = NULL;
7972 }
7973 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7974 {
7975 *tofree = NULL;
7976 r = (char_u *)"{...}";
7977 }
7978 else
7979 {
7980 tv->vval.v_dict->dv_copyID = copyID;
7981 *tofree = dict2string(tv, copyID);
7982 r = *tofree;
7983 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007984 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007985
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986 case VAR_STRING:
7987 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007988 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007989 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007990 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007991 *tofree = NULL;
7992 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007993 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007994
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007995 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007996#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007997 *tofree = NULL;
7998 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7999 r = numbuf;
8000 break;
8001#endif
8002
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008003 case VAR_SPECIAL:
8004 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008005 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008006 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008007 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008008
Bram Moolenaar8502c702014-06-17 12:51:16 +02008009 if (--recurse == 0)
8010 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008011 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008012}
8013
8014/*
8015 * Return a string with the string representation of a variable.
8016 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8017 * "numbuf" is used for a number.
8018 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008019 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008020 */
8021 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008022tv2string(
8023 typval_T *tv,
8024 char_u **tofree,
8025 char_u *numbuf,
8026 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008027{
8028 switch (tv->v_type)
8029 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008030 case VAR_FUNC:
8031 *tofree = string_quote(tv->vval.v_string, TRUE);
8032 return *tofree;
8033 case VAR_STRING:
8034 *tofree = string_quote(tv->vval.v_string, FALSE);
8035 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008036 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008037#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008038 *tofree = NULL;
8039 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8040 return numbuf;
8041#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008042 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008043 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008044 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008045 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008046 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008047 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008048 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008049 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008050 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008051 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008052 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008053}
8054
8055/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 * Return string "str" in ' quotes, doubling ' characters.
8057 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008058 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008059 */
8060 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008061string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008062{
Bram Moolenaar33570922005-01-25 22:26:29 +00008063 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008064 char_u *p, *r, *s;
8065
Bram Moolenaar33570922005-01-25 22:26:29 +00008066 len = (function ? 13 : 3);
8067 if (str != NULL)
8068 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008069 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008070 for (p = str; *p != NUL; mb_ptr_adv(p))
8071 if (*p == '\'')
8072 ++len;
8073 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008074 s = r = alloc(len);
8075 if (r != NULL)
8076 {
8077 if (function)
8078 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008079 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008080 r += 10;
8081 }
8082 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008083 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008084 if (str != NULL)
8085 for (p = str; *p != NUL; )
8086 {
8087 if (*p == '\'')
8088 *r++ = '\'';
8089 MB_COPY_CHAR(p, r);
8090 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008091 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008092 if (function)
8093 *r++ = ')';
8094 *r++ = NUL;
8095 }
8096 return s;
8097}
8098
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008099#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008100/*
8101 * Convert the string "text" to a floating point number.
8102 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8103 * this always uses a decimal point.
8104 * Returns the length of the text that was consumed.
8105 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008106 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008107string2float(
8108 char_u *text,
8109 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008110{
8111 char *s = (char *)text;
8112 float_T f;
8113
8114 f = strtod(s, &s);
8115 *value = f;
8116 return (int)((char_u *)s - text);
8117}
8118#endif
8119
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 * Get the value of an environment variable.
8122 * "arg" is pointing to the '$'. It is advanced to after the name.
8123 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008124 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 */
8126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008127get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 char_u *string = NULL;
8130 int len;
8131 int cc;
8132 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008133 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134
8135 ++*arg;
8136 name = *arg;
8137 len = get_env_len(arg);
8138 if (evaluate)
8139 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008140 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008141 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008142
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008143 cc = name[len];
8144 name[len] = NUL;
8145 /* first try vim_getenv(), fast for normal environment vars */
8146 string = vim_getenv(name, &mustfree);
8147 if (string != NULL && *string != NUL)
8148 {
8149 if (!mustfree)
8150 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008152 else
8153 {
8154 if (mustfree)
8155 vim_free(string);
8156
8157 /* next try expanding things like $VIM and ${HOME} */
8158 string = expand_env_save(name - 1);
8159 if (string != NULL && *string == '$')
8160 {
8161 vim_free(string);
8162 string = NULL;
8163 }
8164 }
8165 name[len] = cc;
8166
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167 rettv->v_type = VAR_STRING;
8168 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 }
8170
8171 return OK;
8172}
8173
8174/*
8175 * Array with names and number of arguments of all internal functions
8176 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8177 */
8178static struct fst
8179{
8180 char *f_name; /* function name */
8181 char f_min_argc; /* minimal number of arguments */
8182 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008183 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008184 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185} functions[] =
8186{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008187#ifdef FEAT_FLOAT
8188 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008189 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008190#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008191 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008192 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008193 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"append", 2, 2, f_append},
8195 {"argc", 0, 0, f_argc},
8196 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008197 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008198 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008199#ifdef FEAT_FLOAT
8200 {"asin", 1, 1, f_asin}, /* WJMc */
8201#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008202 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008203 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008204 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008205 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008206 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008207 {"assert_notequal", 2, 3, f_assert_notequal},
8208 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008209 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008210#ifdef FEAT_FLOAT
8211 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008212 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008215 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"bufexists", 1, 1, f_bufexists},
8217 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8218 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8219 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8220 {"buflisted", 1, 1, f_buflisted},
8221 {"bufloaded", 1, 1, f_bufloaded},
8222 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008223 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"bufwinnr", 1, 1, f_bufwinnr},
8225 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008226 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008227 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008228 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229#ifdef FEAT_FLOAT
8230 {"ceil", 1, 1, f_ceil},
8231#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008232#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008233 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008234 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8235 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008236 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008237 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008238 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008239 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008240 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008241 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008242 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008243 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008244 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8245 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008246 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008247 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008248#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008249 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008250 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008252 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008254#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008255 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008256 {"complete_add", 1, 1, f_complete_add},
8257 {"complete_check", 0, 0, f_complete_check},
8258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008260 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008261#ifdef FEAT_FLOAT
8262 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008263 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008264#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008265 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008267 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008268 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008269 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008271 {"diff_filler", 1, 1, f_diff_filler},
8272 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008273 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008274 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008276 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {"eventhandler", 0, 0, f_eventhandler},
8278 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008279 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008281#ifdef FEAT_FLOAT
8282 {"exp", 1, 1, f_exp},
8283#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008284 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008285 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008286 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8288 {"filereadable", 1, 1, f_filereadable},
8289 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008290 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008291 {"finddir", 1, 3, f_finddir},
8292 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008293#ifdef FEAT_FLOAT
8294 {"float2nr", 1, 1, f_float2nr},
8295 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008296 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008297#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008298 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"fnamemodify", 2, 2, f_fnamemodify},
8300 {"foldclosed", 1, 1, f_foldclosed},
8301 {"foldclosedend", 1, 1, f_foldclosedend},
8302 {"foldlevel", 1, 1, f_foldlevel},
8303 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008304 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008306 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008307 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008308 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008309 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008310 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"getchar", 0, 1, f_getchar},
8312 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008313 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"getcmdline", 0, 0, f_getcmdline},
8315 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008316 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008317 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008318 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008319 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008320 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008321 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"getfsize", 1, 1, f_getfsize},
8323 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008324 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008325 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008326 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008327 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008328 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008329 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008330 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008331 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008333 {"gettabvar", 2, 3, f_gettabvar},
8334 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"getwinposx", 0, 0, f_getwinposx},
8336 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008337 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008338 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008339 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008340 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008342 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008343 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008344 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8346 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8347 {"histadd", 2, 2, f_histadd},
8348 {"histdel", 1, 2, f_histdel},
8349 {"histget", 1, 2, f_histget},
8350 {"histnr", 1, 1, f_histnr},
8351 {"hlID", 1, 1, f_hlID},
8352 {"hlexists", 1, 1, f_hlexists},
8353 {"hostname", 0, 0, f_hostname},
8354 {"iconv", 3, 3, f_iconv},
8355 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008356 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008357 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008359 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"inputrestore", 0, 0, f_inputrestore},
8361 {"inputsave", 0, 0, f_inputsave},
8362 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008364 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008366 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008367#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8368 {"isnan", 1, 1, f_isnan},
8369#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008370 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008371#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008372 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008373 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008374 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008375 {"job_start", 1, 2, f_job_start},
8376 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008377 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008378#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008379 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008380 {"js_decode", 1, 1, f_js_decode},
8381 {"js_encode", 1, 1, f_js_encode},
8382 {"json_decode", 1, 1, f_json_decode},
8383 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008384 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008386 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"libcall", 3, 3, f_libcall},
8388 {"libcallnr", 3, 3, f_libcallnr},
8389 {"line", 1, 1, f_line},
8390 {"line2byte", 1, 1, f_line2byte},
8391 {"lispindent", 1, 1, f_lispindent},
8392 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008394 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008395 {"log10", 1, 1, f_log10},
8396#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008397#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008398 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008399#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008400 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008401 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008402 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008403 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008404 {"matchadd", 2, 5, f_matchadd},
8405 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008406 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008407 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008408 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008409 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008410 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008411 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008412 {"max", 1, 1, f_max},
8413 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008414#ifdef vim_mkdir
8415 {"mkdir", 1, 3, f_mkdir},
8416#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008417 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008418#ifdef FEAT_MZSCHEME
8419 {"mzeval", 1, 1, f_mzeval},
8420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008422 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008423 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008424 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008425#ifdef FEAT_PERL
8426 {"perleval", 1, 1, f_perleval},
8427#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008428#ifdef FEAT_FLOAT
8429 {"pow", 2, 2, f_pow},
8430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008432 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008433 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008434#ifdef FEAT_PYTHON3
8435 {"py3eval", 1, 1, f_py3eval},
8436#endif
8437#ifdef FEAT_PYTHON
8438 {"pyeval", 1, 1, f_pyeval},
8439#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008440 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008441 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008442 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008443#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008444 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008445#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008446 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"remote_expr", 2, 3, f_remote_expr},
8448 {"remote_foreground", 1, 1, f_remote_foreground},
8449 {"remote_peek", 1, 2, f_remote_peek},
8450 {"remote_read", 1, 1, f_remote_read},
8451 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008452 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008454 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008456 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008457#ifdef FEAT_FLOAT
8458 {"round", 1, 1, f_round},
8459#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008460 {"screenattr", 2, 2, f_screenattr},
8461 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008462 {"screencol", 0, 0, f_screencol},
8463 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008464 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008465 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008466 {"searchpair", 3, 7, f_searchpair},
8467 {"searchpairpos", 3, 7, f_searchpairpos},
8468 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {"server2client", 2, 2, f_server2client},
8470 {"serverlist", 0, 0, f_serverlist},
8471 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008472 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008474 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008476 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008477 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008478 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008479 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008481 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008482 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008484#ifdef FEAT_CRYPT
8485 {"sha256", 1, 1, f_sha256},
8486#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008487 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008488 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008490#ifdef FEAT_FLOAT
8491 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008492 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008493#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008494 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008495 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008496 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008497 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008498 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008499#ifdef FEAT_FLOAT
8500 {"sqrt", 1, 1, f_sqrt},
8501 {"str2float", 1, 1, f_str2float},
8502#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008503 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008504 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008505 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506#ifdef HAVE_STRFTIME
8507 {"strftime", 1, 2, f_strftime},
8508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008509 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008510 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {"strlen", 1, 1, f_strlen},
8512 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008513 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008515 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008516 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"substitute", 4, 4, f_substitute},
8518 {"synID", 3, 3, f_synID},
8519 {"synIDattr", 2, 3, f_synIDattr},
8520 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008521 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008522 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008523 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008524 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008525 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008526 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008527 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008528 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008529 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008530#ifdef FEAT_FLOAT
8531 {"tan", 1, 1, f_tan},
8532 {"tanh", 1, 1, f_tanh},
8533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008535 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008536#ifdef FEAT_TIMERS
8537 {"timer_start", 2, 3, f_timer_start},
8538 {"timer_stop", 1, 1, f_timer_stop},
8539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"tolower", 1, 1, f_tolower},
8541 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008542 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008543#ifdef FEAT_FLOAT
8544 {"trunc", 1, 1, f_trunc},
8545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008547 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008548 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008549 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008550 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 {"virtcol", 1, 1, f_virtcol},
8552 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008553 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008554 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008555 {"win_getid", 0, 2, f_win_getid},
8556 {"win_gotoid", 1, 1, f_win_gotoid},
8557 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8558 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {"winbufnr", 1, 1, f_winbufnr},
8560 {"wincol", 0, 0, f_wincol},
8561 {"winheight", 1, 1, f_winheight},
8562 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008563 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008565 {"winrestview", 1, 1, f_winrestview},
8566 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008568 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008569 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008570 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571};
8572
8573#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8574
8575/*
8576 * Function given to ExpandGeneric() to obtain the list of internal
8577 * or user defined function names.
8578 */
8579 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008580get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
8582 static int intidx = -1;
8583 char_u *name;
8584
8585 if (idx == 0)
8586 intidx = -1;
8587 if (intidx < 0)
8588 {
8589 name = get_user_func_name(xp, idx);
8590 if (name != NULL)
8591 return name;
8592 }
8593 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8594 {
8595 STRCPY(IObuff, functions[intidx].f_name);
8596 STRCAT(IObuff, "(");
8597 if (functions[intidx].f_max_argc == 0)
8598 STRCAT(IObuff, ")");
8599 return IObuff;
8600 }
8601
8602 return NULL;
8603}
8604
8605/*
8606 * Function given to ExpandGeneric() to obtain the list of internal or
8607 * user defined variable or function names.
8608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008610get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
8612 static int intidx = -1;
8613 char_u *name;
8614
8615 if (idx == 0)
8616 intidx = -1;
8617 if (intidx < 0)
8618 {
8619 name = get_function_name(xp, idx);
8620 if (name != NULL)
8621 return name;
8622 }
8623 return get_user_var_name(xp, ++intidx);
8624}
8625
8626#endif /* FEAT_CMDL_COMPL */
8627
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008628#if defined(EBCDIC) || defined(PROTO)
8629/*
8630 * Compare struct fst by function name.
8631 */
8632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008633compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008634{
8635 struct fst *p1 = (struct fst *)s1;
8636 struct fst *p2 = (struct fst *)s2;
8637
8638 return STRCMP(p1->f_name, p2->f_name);
8639}
8640
8641/*
8642 * Sort the function table by function name.
8643 * The sorting of the table above is ASCII dependant.
8644 * On machines using EBCDIC we have to sort it.
8645 */
8646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008647sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008648{
8649 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8650
8651 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8652}
8653#endif
8654
8655
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656/*
8657 * Find internal function in table above.
8658 * Return index, or -1 if not found
8659 */
8660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008661find_internal_func(
8662 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663{
8664 int first = 0;
8665 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8666 int cmp;
8667 int x;
8668
8669 /*
8670 * Find the function name in the table. Binary search.
8671 */
8672 while (first <= last)
8673 {
8674 x = first + ((unsigned)(last - first) >> 1);
8675 cmp = STRCMP(name, functions[x].f_name);
8676 if (cmp < 0)
8677 last = x - 1;
8678 else if (cmp > 0)
8679 first = x + 1;
8680 else
8681 return x;
8682 }
8683 return -1;
8684}
8685
8686/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008687 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8688 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008689 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8690 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008691 */
8692 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008693deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008694{
Bram Moolenaar33570922005-01-25 22:26:29 +00008695 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008696 int cc;
8697
Bram Moolenaar65639032016-03-16 21:40:30 +01008698 if (partialp != NULL)
8699 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008700
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008701 cc = name[*lenp];
8702 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008703 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008704 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008706 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008707 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 {
8709 *lenp = 0;
8710 return (char_u *)""; /* just in case */
8711 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008712 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008713 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008714 }
8715
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008716 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8717 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008718 partial_T *pt = v->di_tv.vval.v_partial;
8719
8720 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008721 {
8722 *lenp = 0;
8723 return (char_u *)""; /* just in case */
8724 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008725 if (partialp != NULL)
8726 *partialp = pt;
8727 *lenp = (int)STRLEN(pt->pt_name);
8728 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008729 }
8730
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008731 return name;
8732}
8733
8734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 * Allocate a variable for the result of a function.
8736 * Return OK or FAIL.
8737 */
8738 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008739get_func_tv(
8740 char_u *name, /* name of the function */
8741 int len, /* length of "name" */
8742 typval_T *rettv,
8743 char_u **arg, /* argument, pointing to the '(' */
8744 linenr_T firstline, /* first line of range */
8745 linenr_T lastline, /* last line of range */
8746 int *doesrange, /* return: function handled range */
8747 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008748 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008749 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
8751 char_u *argp;
8752 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008753 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 int argcount = 0; /* number of arguments found */
8755
8756 /*
8757 * Get the arguments.
8758 */
8759 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008760 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 {
8762 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8763 if (*argp == ')' || *argp == ',' || *argp == NUL)
8764 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8766 {
8767 ret = FAIL;
8768 break;
8769 }
8770 ++argcount;
8771 if (*argp != ',')
8772 break;
8773 }
8774 if (*argp == ')')
8775 ++argp;
8776 else
8777 ret = FAIL;
8778
8779 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008781 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 {
8784 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008785 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008787 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789
8790 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792
8793 *arg = skipwhite(argp);
8794 return ret;
8795}
8796
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008797#define ERROR_UNKNOWN 0
8798#define ERROR_TOOMANY 1
8799#define ERROR_TOOFEW 2
8800#define ERROR_SCRIPT 3
8801#define ERROR_DICT 4
8802#define ERROR_NONE 5
8803#define ERROR_OTHER 6
8804#define FLEN_FIXED 40
8805
8806/*
8807 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8808 * Change <SNR>123_name() to K_SNR 123_name().
8809 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8810 * (slow).
8811 */
8812 static char_u *
8813fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8814{
8815 int llen;
8816 char_u *fname;
8817 int i;
8818
8819 llen = eval_fname_script(name);
8820 if (llen > 0)
8821 {
8822 fname_buf[0] = K_SPECIAL;
8823 fname_buf[1] = KS_EXTRA;
8824 fname_buf[2] = (int)KE_SNR;
8825 i = 3;
8826 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8827 {
8828 if (current_SID <= 0)
8829 *error = ERROR_SCRIPT;
8830 else
8831 {
8832 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8833 i = (int)STRLEN(fname_buf);
8834 }
8835 }
8836 if (i + STRLEN(name + llen) < FLEN_FIXED)
8837 {
8838 STRCPY(fname_buf + i, name + llen);
8839 fname = fname_buf;
8840 }
8841 else
8842 {
8843 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8844 if (fname == NULL)
8845 *error = ERROR_OTHER;
8846 else
8847 {
8848 *tofree = fname;
8849 mch_memmove(fname, fname_buf, (size_t)i);
8850 STRCPY(fname + i, name + llen);
8851 }
8852 }
8853 }
8854 else
8855 fname = name;
8856 return fname;
8857}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858
8859/*
8860 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008861 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008862 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008864 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008865call_func(
8866 char_u *funcname, /* name of the function */
8867 int len, /* length of "name" */
8868 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008869 int argcount_in, /* number of "argvars" */
8870 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008871 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008872 linenr_T firstline, /* first line of range */
8873 linenr_T lastline, /* last line of range */
8874 int *doesrange, /* return: function handled range */
8875 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008876 partial_T *partial, /* optional, can be NULL */
8877 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 int error = ERROR_NONE;
8881 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008884 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008886 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008887 int argcount = argcount_in;
8888 typval_T *argvars = argvars_in;
8889 dict_T *selfdict = selfdict_in;
8890 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8891 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008892
8893 /* Make a copy of the name, if it comes from a funcref variable it could
8894 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008895 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008896 if (name == NULL)
8897 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008899 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
8901 *doesrange = FALSE;
8902
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008903 if (partial != NULL)
8904 {
8905 if (partial->pt_dict != NULL)
8906 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008907 /* When the function has a partial with a dict and there is a dict
8908 * argument, use the dict argument. That is backwards compatible.
8909 */
8910 if (selfdict_in == NULL)
8911 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008912 }
8913 if (error == ERROR_NONE && partial->pt_argc > 0)
8914 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008915 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8916 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8917 for (i = 0; i < argcount_in; ++i)
8918 argv[i + argv_clear] = argvars_in[i];
8919 argvars = argv;
8920 argcount = partial->pt_argc + argcount_in;
8921 }
8922 }
8923
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924
8925 /* execute the function if no errors detected and executing */
8926 if (evaluate && error == ERROR_NONE)
8927 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008928 char_u *rfname = fname;
8929
8930 /* Ignore "g:" before a function name. */
8931 if (fname[0] == 'g' && fname[1] == ':')
8932 rfname = fname + 2;
8933
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008934 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8935 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 error = ERROR_UNKNOWN;
8937
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008938 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 {
8940 /*
8941 * User defined function.
8942 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008943 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008944
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008946 /* Trigger FuncUndefined event, may load the function. */
8947 if (fp == NULL
8948 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008949 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008950 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008952 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008953 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 }
8955#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008956 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008957 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008958 {
8959 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008960 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008961 }
8962
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 if (fp != NULL)
8964 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008965 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008967 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008969 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008971 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008972 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 else
8974 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008975 int did_save_redo = FALSE;
8976
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 /*
8978 * Call the user function.
8979 * Save and restore search patterns, script variables and
8980 * redo buffer.
8981 */
8982 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008983#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008984 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008985#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008986 {
8987 saveRedobuff();
8988 did_save_redo = TRUE;
8989 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008990 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008992 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008993 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8994 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8995 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008996 /* Function was unreferenced while being used, free it
8997 * now. */
8998 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008999 if (did_save_redo)
9000 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 restore_search_patterns();
9002 error = ERROR_NONE;
9003 }
9004 }
9005 }
9006 else
9007 {
9008 /*
9009 * Find the function name in the table, call its implementation.
9010 */
9011 i = find_internal_func(fname);
9012 if (i >= 0)
9013 {
9014 if (argcount < functions[i].f_min_argc)
9015 error = ERROR_TOOFEW;
9016 else if (argcount > functions[i].f_max_argc)
9017 error = ERROR_TOOMANY;
9018 else
9019 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 error = ERROR_NONE;
9023 }
9024 }
9025 }
9026 /*
9027 * The function call (or "FuncUndefined" autocommand sequence) might
9028 * have been aborted by an error, an interrupt, or an explicitly thrown
9029 * exception that has not been caught so far. This situation can be
9030 * tested for by calling aborting(). For an error in an internal
9031 * function or for the "E132" error in call_user_func(), however, the
9032 * throw point at which the "force_abort" flag (temporarily reset by
9033 * emsg()) is normally updated has not been reached yet. We need to
9034 * update that flag first to make aborting() reliable.
9035 */
9036 update_force_abort();
9037 }
9038 if (error == ERROR_NONE)
9039 ret = OK;
9040
9041 /*
9042 * Report an error unless the argument evaluation or function call has been
9043 * cancelled due to an aborting error, an interrupt, or an exception.
9044 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009045 if (!aborting())
9046 {
9047 switch (error)
9048 {
9049 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009050 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009051 break;
9052 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009053 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009054 break;
9055 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009056 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009057 name);
9058 break;
9059 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009060 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009061 name);
9062 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009063 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009064 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009065 name);
9066 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009067 }
9068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009070 while (argv_clear > 0)
9071 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009072 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009073 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074
9075 return ret;
9076}
9077
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009078/*
9079 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009080 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009081 */
9082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009083emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009084{
9085 char_u *p;
9086
9087 if (*name == K_SPECIAL)
9088 p = concat_str((char_u *)"<SNR>", name + 3);
9089 else
9090 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009091 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009092 if (p != name)
9093 vim_free(p);
9094}
9095
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009096/*
9097 * Return TRUE for a non-zero Number and a non-empty String.
9098 */
9099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009100non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009101{
9102 return ((argvars[0].v_type == VAR_NUMBER
9103 && argvars[0].vval.v_number != 0)
9104 || (argvars[0].v_type == VAR_STRING
9105 && argvars[0].vval.v_string != NULL
9106 && *argvars[0].vval.v_string != NUL));
9107}
9108
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109/*********************************************
9110 * Implementation of the built-in functions
9111 */
9112
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009113#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009114static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009115
9116/*
9117 * Get the float value of "argvars[0]" into "f".
9118 * Returns FAIL when the argument is not a Number or Float.
9119 */
9120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009121get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009122{
9123 if (argvars[0].v_type == VAR_FLOAT)
9124 {
9125 *f = argvars[0].vval.v_float;
9126 return OK;
9127 }
9128 if (argvars[0].v_type == VAR_NUMBER)
9129 {
9130 *f = (float_T)argvars[0].vval.v_number;
9131 return OK;
9132 }
9133 EMSG(_("E808: Number or Float required"));
9134 return FAIL;
9135}
9136
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009137/*
9138 * "abs(expr)" function
9139 */
9140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009141f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009142{
9143 if (argvars[0].v_type == VAR_FLOAT)
9144 {
9145 rettv->v_type = VAR_FLOAT;
9146 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9147 }
9148 else
9149 {
9150 varnumber_T n;
9151 int error = FALSE;
9152
9153 n = get_tv_number_chk(&argvars[0], &error);
9154 if (error)
9155 rettv->vval.v_number = -1;
9156 else if (n > 0)
9157 rettv->vval.v_number = n;
9158 else
9159 rettv->vval.v_number = -n;
9160 }
9161}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009162
9163/*
9164 * "acos()" function
9165 */
9166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009167f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009168{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009169 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009170
9171 rettv->v_type = VAR_FLOAT;
9172 if (get_float_arg(argvars, &f) == OK)
9173 rettv->vval.v_float = acos(f);
9174 else
9175 rettv->vval.v_float = 0.0;
9176}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009177#endif
9178
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009180 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 */
9182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009183f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184{
Bram Moolenaar33570922005-01-25 22:26:29 +00009185 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009188 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009190 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009191 && !tv_check_lock(l->lv_lock,
9192 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009193 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009195 }
9196 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009197 EMSG(_(e_listreq));
9198}
9199
9200/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009201 * "alloc_fail(id, countdown, repeat)" function
9202 */
9203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009204f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009205{
9206 if (argvars[0].v_type != VAR_NUMBER
9207 || argvars[0].vval.v_number <= 0
9208 || argvars[1].v_type != VAR_NUMBER
9209 || argvars[1].vval.v_number < 0
9210 || argvars[2].v_type != VAR_NUMBER)
9211 EMSG(_(e_invarg));
9212 else
9213 {
9214 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009215 if (alloc_fail_id >= aid_last)
9216 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009217 alloc_fail_countdown = argvars[1].vval.v_number;
9218 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009219 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009220 }
9221}
9222
9223/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009224 * "and(expr, expr)" function
9225 */
9226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009227f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009228{
9229 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9230 & get_tv_number_chk(&argvars[1], NULL);
9231}
9232
9233/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009234 * "append(lnum, string/list)" function
9235 */
9236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009237f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009238{
9239 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009241 list_T *l = NULL;
9242 listitem_T *li = NULL;
9243 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009244 long added = 0;
9245
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009246 /* When coming here from Insert mode, sync undo, so that this can be
9247 * undone separately from what was previously inserted. */
9248 if (u_sync_once == 2)
9249 {
9250 u_sync_once = 1; /* notify that u_sync() was called */
9251 u_sync(TRUE);
9252 }
9253
Bram Moolenaar0d660222005-01-07 21:51:51 +00009254 lnum = get_tv_lnum(argvars);
9255 if (lnum >= 0
9256 && lnum <= curbuf->b_ml.ml_line_count
9257 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009258 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009259 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009260 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009261 l = argvars[1].vval.v_list;
9262 if (l == NULL)
9263 return;
9264 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009265 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009266 for (;;)
9267 {
9268 if (l == NULL)
9269 tv = &argvars[1]; /* append a string */
9270 else if (li == NULL)
9271 break; /* end of list */
9272 else
9273 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009274 line = get_tv_string_chk(tv);
9275 if (line == NULL) /* type error */
9276 {
9277 rettv->vval.v_number = 1; /* Failed */
9278 break;
9279 }
9280 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009281 ++added;
9282 if (l == NULL)
9283 break;
9284 li = li->li_next;
9285 }
9286
9287 appended_lines_mark(lnum, added);
9288 if (curwin->w_cursor.lnum > lnum)
9289 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009291 else
9292 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293}
9294
9295/*
9296 * "argc()" function
9297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009299f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009301 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302}
9303
9304/*
9305 * "argidx()" function
9306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009308f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311}
9312
9313/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009314 * "arglistid()" function
9315 */
9316 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009317f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009318{
9319 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009320
9321 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009322 wp = find_tabwin(&argvars[0], &argvars[1]);
9323 if (wp != NULL)
9324 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009325}
9326
9327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 * "argv(nr)" function
9329 */
9330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009331f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332{
9333 int idx;
9334
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009335 if (argvars[0].v_type != VAR_UNKNOWN)
9336 {
9337 idx = get_tv_number_chk(&argvars[0], NULL);
9338 if (idx >= 0 && idx < ARGCOUNT)
9339 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9340 else
9341 rettv->vval.v_string = NULL;
9342 rettv->v_type = VAR_STRING;
9343 }
9344 else if (rettv_list_alloc(rettv) == OK)
9345 for (idx = 0; idx < ARGCOUNT; ++idx)
9346 list_append_string(rettv->vval.v_list,
9347 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348}
9349
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009350typedef enum
9351{
9352 ASSERT_EQUAL,
9353 ASSERT_NOTEQUAL,
9354 ASSERT_MATCH,
9355 ASSERT_NOTMATCH,
9356 ASSERT_OTHER,
9357} assert_type_T;
9358
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009359static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009360static 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 +01009361static void assert_error(garray_T *gap);
9362static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009363
9364/*
9365 * Prepare "gap" for an assert error and add the sourcing position.
9366 */
9367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009368prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009369{
9370 char buf[NUMBUFLEN];
9371
9372 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009373 if (sourcing_name != NULL)
9374 {
9375 ga_concat(gap, sourcing_name);
9376 if (sourcing_lnum > 0)
9377 ga_concat(gap, (char_u *)" ");
9378 }
9379 if (sourcing_lnum > 0)
9380 {
9381 sprintf(buf, "line %ld", (long)sourcing_lnum);
9382 ga_concat(gap, (char_u *)buf);
9383 }
9384 if (sourcing_name != NULL || sourcing_lnum > 0)
9385 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009386}
9387
9388/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009389 * Append "str" to "gap", escaping unprintable characters.
9390 * Changes NL to \n, CR to \r, etc.
9391 */
9392 static void
9393ga_concat_esc(garray_T *gap, char_u *str)
9394{
9395 char_u *p;
9396 char_u buf[NUMBUFLEN];
9397
Bram Moolenaarf1551962016-03-15 12:55:58 +01009398 if (str == NULL)
9399 {
9400 ga_concat(gap, (char_u *)"NULL");
9401 return;
9402 }
9403
Bram Moolenaar23689172016-02-15 22:37:37 +01009404 for (p = str; *p != NUL; ++p)
9405 switch (*p)
9406 {
9407 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9408 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9409 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9410 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9411 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9412 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9413 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9414 default:
9415 if (*p < ' ')
9416 {
9417 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9418 ga_concat(gap, buf);
9419 }
9420 else
9421 ga_append(gap, *p);
9422 break;
9423 }
9424}
9425
9426/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009427 * Fill "gap" with information about an assert error.
9428 */
9429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009430fill_assert_error(
9431 garray_T *gap,
9432 typval_T *opt_msg_tv,
9433 char_u *exp_str,
9434 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009435 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009436 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009437{
9438 char_u numbuf[NUMBUFLEN];
9439 char_u *tofree;
9440
9441 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9442 {
9443 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9444 vim_free(tofree);
9445 }
9446 else
9447 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009448 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009449 ga_concat(gap, (char_u *)"Pattern ");
9450 else
9451 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009452 if (exp_str == NULL)
9453 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009454 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009455 vim_free(tofree);
9456 }
9457 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009458 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009459 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009460 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009461 else if (atype == ASSERT_NOTMATCH)
9462 ga_concat(gap, (char_u *)" does match ");
9463 else if (atype == ASSERT_NOTEQUAL)
9464 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009465 else
9466 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009467 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009468 vim_free(tofree);
9469 }
9470}
Bram Moolenaar43345542015-11-29 17:35:35 +01009471
9472/*
9473 * Add an assert error to v:errors.
9474 */
9475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009476assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009477{
9478 struct vimvar *vp = &vimvars[VV_ERRORS];
9479
9480 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9481 /* Make sure v:errors is a list. */
9482 set_vim_var_list(VV_ERRORS, list_alloc());
9483 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9484}
9485
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009486 static void
9487assert_equal_common(typval_T *argvars, assert_type_T atype)
9488{
9489 garray_T ga;
9490
9491 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9492 != (atype == ASSERT_EQUAL))
9493 {
9494 prepare_assert_error(&ga);
9495 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9496 atype);
9497 assert_error(&ga);
9498 ga_clear(&ga);
9499 }
9500}
9501
Bram Moolenaar43345542015-11-29 17:35:35 +01009502/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009503 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009504 */
9505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009506f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009507{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009508 assert_equal_common(argvars, ASSERT_EQUAL);
9509}
Bram Moolenaar43345542015-11-29 17:35:35 +01009510
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009511/*
9512 * "assert_notequal(expected, actual[, msg])" function
9513 */
9514 static void
9515f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9516{
9517 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009518}
9519
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009520/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009521 * "assert_exception(string[, msg])" function
9522 */
9523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009524f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009525{
9526 garray_T ga;
9527 char *error;
9528
9529 error = (char *)get_tv_string_chk(&argvars[0]);
9530 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9531 {
9532 prepare_assert_error(&ga);
9533 ga_concat(&ga, (char_u *)"v:exception is not set");
9534 assert_error(&ga);
9535 ga_clear(&ga);
9536 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009537 else if (error != NULL
9538 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009539 {
9540 prepare_assert_error(&ga);
9541 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009542 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009543 assert_error(&ga);
9544 ga_clear(&ga);
9545 }
9546}
9547
9548/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009549 * "assert_fails(cmd [, error])" function
9550 */
9551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009552f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009553{
9554 char_u *cmd = get_tv_string_chk(&argvars[0]);
9555 garray_T ga;
9556
9557 called_emsg = FALSE;
9558 suppress_errthrow = TRUE;
9559 emsg_silent = TRUE;
9560 do_cmdline_cmd(cmd);
9561 if (!called_emsg)
9562 {
9563 prepare_assert_error(&ga);
9564 ga_concat(&ga, (char_u *)"command did not fail: ");
9565 ga_concat(&ga, cmd);
9566 assert_error(&ga);
9567 ga_clear(&ga);
9568 }
9569 else if (argvars[1].v_type != VAR_UNKNOWN)
9570 {
9571 char_u buf[NUMBUFLEN];
9572 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9573
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009574 if (error == NULL
9575 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009576 {
9577 prepare_assert_error(&ga);
9578 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009579 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009580 assert_error(&ga);
9581 ga_clear(&ga);
9582 }
9583 }
9584
9585 called_emsg = FALSE;
9586 suppress_errthrow = FALSE;
9587 emsg_silent = FALSE;
9588 emsg_on_display = FALSE;
9589 set_vim_var_string(VV_ERRMSG, NULL, 0);
9590}
9591
9592/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009593 * Common for assert_true() and assert_false().
9594 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009596assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009597{
9598 int error = FALSE;
9599 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009600
Bram Moolenaar37127922016-02-06 20:29:28 +01009601 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009602 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009603 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009604 if (argvars[0].v_type != VAR_NUMBER
9605 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9606 || error)
9607 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009608 prepare_assert_error(&ga);
9609 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009610 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009611 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009612 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009613 ga_clear(&ga);
9614 }
9615}
9616
9617/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009618 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009619 */
9620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009621f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009622{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009623 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009624}
9625
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009626 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009627assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009628{
9629 garray_T ga;
9630 char_u buf1[NUMBUFLEN];
9631 char_u buf2[NUMBUFLEN];
9632 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9633 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9634
Bram Moolenaar72188e92016-03-28 22:48:29 +02009635 if (pat == NULL || text == NULL)
9636 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009637 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009638 {
9639 prepare_assert_error(&ga);
9640 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009641 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009642 assert_error(&ga);
9643 ga_clear(&ga);
9644 }
9645}
9646
9647/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009648 * "assert_match(pattern, actual[, msg])" function
9649 */
9650 static void
9651f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9652{
9653 assert_match_common(argvars, ASSERT_MATCH);
9654}
9655
9656/*
9657 * "assert_notmatch(pattern, actual[, msg])" function
9658 */
9659 static void
9660f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9661{
9662 assert_match_common(argvars, ASSERT_NOTMATCH);
9663}
9664
9665/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009666 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009667 */
9668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009669f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009670{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009671 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009672}
9673
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009674#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009675/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009676 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009677 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009679f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009680{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009681 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009682
9683 rettv->v_type = VAR_FLOAT;
9684 if (get_float_arg(argvars, &f) == OK)
9685 rettv->vval.v_float = asin(f);
9686 else
9687 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009688}
9689
9690/*
9691 * "atan()" function
9692 */
9693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009694f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009695{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009696 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009697
9698 rettv->v_type = VAR_FLOAT;
9699 if (get_float_arg(argvars, &f) == OK)
9700 rettv->vval.v_float = atan(f);
9701 else
9702 rettv->vval.v_float = 0.0;
9703}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009704
9705/*
9706 * "atan2()" function
9707 */
9708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009709f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009710{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009711 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009712
9713 rettv->v_type = VAR_FLOAT;
9714 if (get_float_arg(argvars, &fx) == OK
9715 && get_float_arg(&argvars[1], &fy) == OK)
9716 rettv->vval.v_float = atan2(fx, fy);
9717 else
9718 rettv->vval.v_float = 0.0;
9719}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009720#endif
9721
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722/*
9723 * "browse(save, title, initdir, default)" function
9724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009726f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727{
9728#ifdef FEAT_BROWSE
9729 int save;
9730 char_u *title;
9731 char_u *initdir;
9732 char_u *defname;
9733 char_u buf[NUMBUFLEN];
9734 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009735 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009737 save = get_tv_number_chk(&argvars[0], &error);
9738 title = get_tv_string_chk(&argvars[1]);
9739 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9740 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 if (error || title == NULL || initdir == NULL || defname == NULL)
9743 rettv->vval.v_string = NULL;
9744 else
9745 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009746 do_browse(save ? BROWSE_SAVE : 0,
9747 title, defname, NULL, initdir, NULL, curbuf);
9748#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009750#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009752}
9753
9754/*
9755 * "browsedir(title, initdir)" function
9756 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009758f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009759{
9760#ifdef FEAT_BROWSE
9761 char_u *title;
9762 char_u *initdir;
9763 char_u buf[NUMBUFLEN];
9764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009765 title = get_tv_string_chk(&argvars[0]);
9766 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009768 if (title == NULL || initdir == NULL)
9769 rettv->vval.v_string = NULL;
9770 else
9771 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009772 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777}
9778
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009779static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009780
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781/*
9782 * Find a buffer by number or exact name.
9783 */
9784 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009785find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
9787 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009789 if (avar->v_type == VAR_NUMBER)
9790 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009791 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009793 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009794 if (buf == NULL)
9795 {
9796 /* No full path name match, try a match with a URL or a "nofile"
9797 * buffer, these don't use the full path. */
9798 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9799 if (buf->b_fname != NULL
9800 && (path_with_url(buf->b_fname)
9801#ifdef FEAT_QUICKFIX
9802 || bt_nofile(buf)
9803#endif
9804 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009805 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009806 break;
9807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 }
9809 return buf;
9810}
9811
9812/*
9813 * "bufexists(expr)" function
9814 */
9815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009816f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819}
9820
9821/*
9822 * "buflisted(expr)" function
9823 */
9824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009825f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826{
9827 buf_T *buf;
9828
9829 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831}
9832
9833/*
9834 * "bufloaded(expr)" function
9835 */
9836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
9839 buf_T *buf;
9840
9841 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843}
9844
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009845 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009846buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 int save_magic;
9849 char_u *save_cpo;
9850 buf_T *buf;
9851
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9853 save_magic = p_magic;
9854 p_magic = TRUE;
9855 save_cpo = p_cpo;
9856 p_cpo = (char_u *)"";
9857
9858 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009859 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860
9861 p_magic = save_magic;
9862 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009863 return buf;
9864}
9865
9866/*
9867 * Get buffer by number or pattern.
9868 */
9869 static buf_T *
9870get_buf_tv(typval_T *tv, int curtab_only)
9871{
9872 char_u *name = tv->vval.v_string;
9873 buf_T *buf;
9874
9875 if (tv->v_type == VAR_NUMBER)
9876 return buflist_findnr((int)tv->vval.v_number);
9877 if (tv->v_type != VAR_STRING)
9878 return NULL;
9879 if (name == NULL || *name == NUL)
9880 return curbuf;
9881 if (name[0] == '$' && name[1] == NUL)
9882 return lastbuf;
9883
9884 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885
9886 /* If not found, try expanding the name, like done for bufexists(). */
9887 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889
9890 return buf;
9891}
9892
9893/*
9894 * "bufname(expr)" function
9895 */
9896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009897f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898{
9899 buf_T *buf;
9900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009903 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009904 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009906 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 --emsg_off;
9910}
9911
9912/*
9913 * "bufnr(expr)" function
9914 */
9915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009916f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917{
9918 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009919 int error = FALSE;
9920 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009924 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009925 --emsg_off;
9926
9927 /* If the buffer isn't found and the second argument is not zero create a
9928 * new buffer. */
9929 if (buf == NULL
9930 && argvars[1].v_type != VAR_UNKNOWN
9931 && get_tv_number_chk(&argvars[1], &error) != 0
9932 && !error
9933 && (name = get_tv_string_chk(&argvars[0])) != NULL
9934 && !error)
9935 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9936
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941}
9942
9943/*
9944 * "bufwinnr(nr)" function
9945 */
9946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948{
9949#ifdef FEAT_WINDOWS
9950 win_T *wp;
9951 int winnr = 0;
9952#endif
9953 buf_T *buf;
9954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009957 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958#ifdef FEAT_WINDOWS
9959 for (wp = firstwin; wp; wp = wp->w_next)
9960 {
9961 ++winnr;
9962 if (wp->w_buffer == buf)
9963 break;
9964 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968#endif
9969 --emsg_off;
9970}
9971
9972/*
9973 * "byte2line(byte)" function
9974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009976f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
9978#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980#else
9981 long boff = 0;
9982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009983 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 (linenr_T)0, &boff);
9989#endif
9990}
9991
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009993byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009994{
9995#ifdef FEAT_MBYTE
9996 char_u *t;
9997#endif
9998 char_u *str;
9999 long idx;
10000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010001 str = get_tv_string_chk(&argvars[0]);
10002 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010004 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010005 return;
10006
10007#ifdef FEAT_MBYTE
10008 t = str;
10009 for ( ; idx > 0; idx--)
10010 {
10011 if (*t == NUL) /* EOL reached */
10012 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010013 if (enc_utf8 && comp)
10014 t += utf_ptr2len(t);
10015 else
10016 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010017 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010018 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010019#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010020 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010022#endif
10023}
10024
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010025/*
10026 * "byteidx()" function
10027 */
10028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010029f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010030{
10031 byteidx(argvars, rettv, FALSE);
10032}
10033
10034/*
10035 * "byteidxcomp()" function
10036 */
10037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010038f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010039{
10040 byteidx(argvars, rettv, TRUE);
10041}
10042
Bram Moolenaardb913952012-06-29 12:54:53 +020010043 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010044func_call(
10045 char_u *name,
10046 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010047 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010048 dict_T *selfdict,
10049 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010050{
10051 listitem_T *item;
10052 typval_T argv[MAX_FUNC_ARGS + 1];
10053 int argc = 0;
10054 int dummy;
10055 int r = 0;
10056
10057 for (item = args->vval.v_list->lv_first; item != NULL;
10058 item = item->li_next)
10059 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010060 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010061 {
10062 EMSG(_("E699: Too many arguments"));
10063 break;
10064 }
10065 /* Make a copy of each argument. This is needed to be able to set
10066 * v_lock to VAR_FIXED in the copy without changing the original list.
10067 */
10068 copy_tv(&item->li_tv, &argv[argc++]);
10069 }
10070
10071 if (item == NULL)
10072 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10073 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010074 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010075
10076 /* Free the arguments. */
10077 while (argc > 0)
10078 clear_tv(&argv[--argc]);
10079
10080 return r;
10081}
10082
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010083/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010084 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010085 */
10086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010087f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010088{
10089 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010090 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010091 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010092
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010093 if (argvars[1].v_type != VAR_LIST)
10094 {
10095 EMSG(_(e_listreq));
10096 return;
10097 }
10098 if (argvars[1].vval.v_list == NULL)
10099 return;
10100
10101 if (argvars[0].v_type == VAR_FUNC)
10102 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010103 else if (argvars[0].v_type == VAR_PARTIAL)
10104 {
10105 partial = argvars[0].vval.v_partial;
10106 func = partial->pt_name;
10107 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010108 else
10109 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110 if (*func == NUL)
10111 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010112
Bram Moolenaare9a41262005-01-15 22:18:47 +000010113 if (argvars[2].v_type != VAR_UNKNOWN)
10114 {
10115 if (argvars[2].v_type != VAR_DICT)
10116 {
10117 EMSG(_(e_dictreq));
10118 return;
10119 }
10120 selfdict = argvars[2].vval.v_dict;
10121 }
10122
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010123 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010124}
10125
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010126#ifdef FEAT_FLOAT
10127/*
10128 * "ceil({float})" function
10129 */
10130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010131f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010132{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010133 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010134
10135 rettv->v_type = VAR_FLOAT;
10136 if (get_float_arg(argvars, &f) == OK)
10137 rettv->vval.v_float = ceil(f);
10138 else
10139 rettv->vval.v_float = 0.0;
10140}
10141#endif
10142
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010144/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010145 * "ch_close()" function
10146 */
10147 static void
10148f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10149{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010150 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010151
10152 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010153 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010154 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010155 channel_clear(channel);
10156 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010157}
10158
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010159/*
10160 * "ch_getbufnr()" function
10161 */
10162 static void
10163f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10164{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010165 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010166
10167 rettv->vval.v_number = -1;
10168 if (channel != NULL)
10169 {
10170 char_u *what = get_tv_string(&argvars[1]);
10171 int part;
10172
10173 if (STRCMP(what, "err") == 0)
10174 part = PART_ERR;
10175 else if (STRCMP(what, "out") == 0)
10176 part = PART_OUT;
10177 else if (STRCMP(what, "in") == 0)
10178 part = PART_IN;
10179 else
10180 part = PART_SOCK;
10181 if (channel->ch_part[part].ch_buffer != NULL)
10182 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10183 }
10184}
10185
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010186/*
10187 * "ch_getjob()" function
10188 */
10189 static void
10190f_ch_getjob(typval_T *argvars, typval_T *rettv)
10191{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010192 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010193
10194 if (channel != NULL)
10195 {
10196 rettv->v_type = VAR_JOB;
10197 rettv->vval.v_job = channel->ch_job;
10198 if (channel->ch_job != NULL)
10199 ++channel->ch_job->jv_refcount;
10200 }
10201}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010202
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010203/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010204 * "ch_info()" function
10205 */
10206 static void
10207f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10208{
10209 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10210
10211 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10212 channel_info(channel, rettv->vval.v_dict);
10213}
10214
10215/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010216 * "ch_log()" function
10217 */
10218 static void
10219f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10220{
10221 char_u *msg = get_tv_string(&argvars[0]);
10222 channel_T *channel = NULL;
10223
10224 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010225 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010226
10227 ch_log(channel, (char *)msg);
10228}
10229
10230/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010231 * "ch_logfile()" function
10232 */
10233 static void
10234f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10235{
10236 char_u *fname;
10237 char_u *opt = (char_u *)"";
10238 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010239
10240 fname = get_tv_string(&argvars[0]);
10241 if (argvars[1].v_type == VAR_STRING)
10242 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010243 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010244}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010245
10246/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010247 * "ch_open()" function
10248 */
10249 static void
10250f_ch_open(typval_T *argvars, typval_T *rettv)
10251{
Bram Moolenaar77073442016-02-13 23:23:53 +010010252 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010253 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010254}
10255
10256/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010257 * "ch_read()" function
10258 */
10259 static void
10260f_ch_read(typval_T *argvars, typval_T *rettv)
10261{
10262 common_channel_read(argvars, rettv, FALSE);
10263}
10264
10265/*
10266 * "ch_readraw()" function
10267 */
10268 static void
10269f_ch_readraw(typval_T *argvars, typval_T *rettv)
10270{
10271 common_channel_read(argvars, rettv, TRUE);
10272}
10273
10274/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010275 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010276 */
10277 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010278f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10279{
10280 ch_expr_common(argvars, rettv, TRUE);
10281}
10282
10283/*
10284 * "ch_sendexpr()" function
10285 */
10286 static void
10287f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10288{
10289 ch_expr_common(argvars, rettv, FALSE);
10290}
10291
10292/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010293 * "ch_evalraw()" function
10294 */
10295 static void
10296f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10297{
10298 ch_raw_common(argvars, rettv, TRUE);
10299}
10300
10301/*
10302 * "ch_sendraw()" function
10303 */
10304 static void
10305f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10306{
10307 ch_raw_common(argvars, rettv, FALSE);
10308}
10309
10310/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010311 * "ch_setoptions()" function
10312 */
10313 static void
10314f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10315{
10316 channel_T *channel;
10317 jobopt_T opt;
10318
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010319 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010320 if (channel == NULL)
10321 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010322 clear_job_options(&opt);
10323 if (get_job_options(&argvars[1], &opt,
10324 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010325 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010326 channel_set_options(channel, &opt);
10327}
10328
10329/*
10330 * "ch_status()" function
10331 */
10332 static void
10333f_ch_status(typval_T *argvars, typval_T *rettv)
10334{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010335 channel_T *channel;
10336
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010337 /* return an empty string by default */
10338 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010339 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010340
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010341 channel = get_channel_arg(&argvars[0], FALSE);
10342 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010343}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010344#endif
10345
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010346/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010347 * "changenr()" function
10348 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010350f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010351{
10352 rettv->vval.v_number = curbuf->b_u_seq_cur;
10353}
10354
10355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 * "char2nr(string)" function
10357 */
10358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010359f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
10361#ifdef FEAT_MBYTE
10362 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010363 {
10364 int utf8 = 0;
10365
10366 if (argvars[1].v_type != VAR_UNKNOWN)
10367 utf8 = get_tv_number_chk(&argvars[1], NULL);
10368
10369 if (utf8)
10370 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10371 else
10372 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 else
10375#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377}
10378
10379/*
10380 * "cindent(lnum)" function
10381 */
10382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010383f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384{
10385#ifdef FEAT_CINDENT
10386 pos_T pos;
10387 linenr_T lnum;
10388
10389 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10392 {
10393 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010394 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 curwin->w_cursor = pos;
10396 }
10397 else
10398#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400}
10401
10402/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010403 * "clearmatches()" function
10404 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010406f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010407{
10408#ifdef FEAT_SEARCH_EXTRA
10409 clear_matches(curwin);
10410#endif
10411}
10412
10413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 * "col(string)" function
10415 */
10416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010417f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418{
10419 colnr_T col = 0;
10420 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010421 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010423 fp = var2fpos(&argvars[0], FALSE, &fnum);
10424 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 {
10426 if (fp->col == MAXCOL)
10427 {
10428 /* '> can be MAXCOL, get the length of the line then */
10429 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010430 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 else
10432 col = MAXCOL;
10433 }
10434 else
10435 {
10436 col = fp->col + 1;
10437#ifdef FEAT_VIRTUALEDIT
10438 /* col(".") when the cursor is on the NUL at the end of the line
10439 * because of "coladd" can be seen as an extra column. */
10440 if (virtual_active() && fp == &curwin->w_cursor)
10441 {
10442 char_u *p = ml_get_cursor();
10443
10444 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10445 curwin->w_virtcol - curwin->w_cursor.coladd))
10446 {
10447# ifdef FEAT_MBYTE
10448 int l;
10449
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010450 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 col += l;
10452# else
10453 if (*p != NUL && p[1] == NUL)
10454 ++col;
10455# endif
10456 }
10457 }
10458#endif
10459 }
10460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462}
10463
Bram Moolenaar572cb562005-08-05 21:35:02 +000010464#if defined(FEAT_INS_EXPAND)
10465/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010466 * "complete()" function
10467 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010469f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010470{
10471 int startcol;
10472
10473 if ((State & INSERT) == 0)
10474 {
10475 EMSG(_("E785: complete() can only be used in Insert mode"));
10476 return;
10477 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010478
10479 /* Check for undo allowed here, because if something was already inserted
10480 * the line was already saved for undo and this check isn't done. */
10481 if (!undo_allowed())
10482 return;
10483
Bram Moolenaarade00832006-03-10 21:46:58 +000010484 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10485 {
10486 EMSG(_(e_invarg));
10487 return;
10488 }
10489
10490 startcol = get_tv_number_chk(&argvars[0], NULL);
10491 if (startcol <= 0)
10492 return;
10493
10494 set_completion(startcol - 1, argvars[1].vval.v_list);
10495}
10496
10497/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010498 * "complete_add()" function
10499 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010501f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010502{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010503 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010504}
10505
10506/*
10507 * "complete_check()" function
10508 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010510f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010511{
10512 int saved = RedrawingDisabled;
10513
10514 RedrawingDisabled = 0;
10515 ins_compl_check_keys(0);
10516 rettv->vval.v_number = compl_interrupted;
10517 RedrawingDisabled = saved;
10518}
10519#endif
10520
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521/*
10522 * "confirm(message, buttons[, default [, type]])" function
10523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010525f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526{
10527#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10528 char_u *message;
10529 char_u *buttons = NULL;
10530 char_u buf[NUMBUFLEN];
10531 char_u buf2[NUMBUFLEN];
10532 int def = 1;
10533 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 char_u *typestr;
10535 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 message = get_tv_string_chk(&argvars[0]);
10538 if (message == NULL)
10539 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010540 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10543 if (buttons == NULL)
10544 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010545 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010547 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010548 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010550 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10551 if (typestr == NULL)
10552 error = TRUE;
10553 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010555 switch (TOUPPER_ASC(*typestr))
10556 {
10557 case 'E': type = VIM_ERROR; break;
10558 case 'Q': type = VIM_QUESTION; break;
10559 case 'I': type = VIM_INFO; break;
10560 case 'W': type = VIM_WARNING; break;
10561 case 'G': type = VIM_GENERIC; break;
10562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 }
10564 }
10565 }
10566 }
10567
10568 if (buttons == NULL || *buttons == NUL)
10569 buttons = (char_u *)_("&Ok");
10570
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010571 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010573 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574#endif
10575}
10576
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010577/*
10578 * "copy()" function
10579 */
10580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010581f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010582{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010583 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010584}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010586#ifdef FEAT_FLOAT
10587/*
10588 * "cos()" function
10589 */
10590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010591f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010592{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010593 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010594
10595 rettv->v_type = VAR_FLOAT;
10596 if (get_float_arg(argvars, &f) == OK)
10597 rettv->vval.v_float = cos(f);
10598 else
10599 rettv->vval.v_float = 0.0;
10600}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010601
10602/*
10603 * "cosh()" function
10604 */
10605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010606f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010607{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010608 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010609
10610 rettv->v_type = VAR_FLOAT;
10611 if (get_float_arg(argvars, &f) == OK)
10612 rettv->vval.v_float = cosh(f);
10613 else
10614 rettv->vval.v_float = 0.0;
10615}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010616#endif
10617
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010619 * "count()" function
10620 */
10621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010622f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010623{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010624 long n = 0;
10625 int ic = FALSE;
10626
Bram Moolenaare9a41262005-01-15 22:18:47 +000010627 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010628 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010629 listitem_T *li;
10630 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010631 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010632
Bram Moolenaare9a41262005-01-15 22:18:47 +000010633 if ((l = argvars[0].vval.v_list) != NULL)
10634 {
10635 li = l->lv_first;
10636 if (argvars[2].v_type != VAR_UNKNOWN)
10637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010638 int error = FALSE;
10639
10640 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010641 if (argvars[3].v_type != VAR_UNKNOWN)
10642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643 idx = get_tv_number_chk(&argvars[3], &error);
10644 if (!error)
10645 {
10646 li = list_find(l, idx);
10647 if (li == NULL)
10648 EMSGN(_(e_listidx), idx);
10649 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010650 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010651 if (error)
10652 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010653 }
10654
10655 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010656 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010657 ++n;
10658 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010660 else if (argvars[0].v_type == VAR_DICT)
10661 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010662 int todo;
10663 dict_T *d;
10664 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010665
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010666 if ((d = argvars[0].vval.v_dict) != NULL)
10667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010668 int error = FALSE;
10669
Bram Moolenaare9a41262005-01-15 22:18:47 +000010670 if (argvars[2].v_type != VAR_UNKNOWN)
10671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010672 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010673 if (argvars[3].v_type != VAR_UNKNOWN)
10674 EMSG(_(e_invarg));
10675 }
10676
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010677 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010678 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010679 {
10680 if (!HASHITEM_EMPTY(hi))
10681 {
10682 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010683 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010684 ++n;
10685 }
10686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010687 }
10688 }
10689 else
10690 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010691 rettv->vval.v_number = n;
10692}
10693
10694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10696 *
10697 * Checks the existence of a cscope connection.
10698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010700f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
10702#ifdef FEAT_CSCOPE
10703 int num = 0;
10704 char_u *dbpath = NULL;
10705 char_u *prepend = NULL;
10706 char_u buf[NUMBUFLEN];
10707
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010708 if (argvars[0].v_type != VAR_UNKNOWN
10709 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711 num = (int)get_tv_number(&argvars[0]);
10712 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010713 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 }
10716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718#endif
10719}
10720
10721/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010722 * "cursor(lnum, col)" function, or
10723 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010725 * Moves the cursor to the specified line and column.
10726 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010729f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730{
10731 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010732#ifdef FEAT_VIRTUALEDIT
10733 long coladd = 0;
10734#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010735 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010737 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010738 if (argvars[1].v_type == VAR_UNKNOWN)
10739 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010740 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010741 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010742
Bram Moolenaar493c1782014-05-28 14:34:46 +020010743 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010744 {
10745 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010746 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010747 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010748 line = pos.lnum;
10749 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010750#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010751 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010752#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010753 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010754 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010755 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010756 set_curswant = FALSE;
10757 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010758 }
10759 else
10760 {
10761 line = get_tv_lnum(argvars);
10762 col = get_tv_number_chk(&argvars[1], NULL);
10763#ifdef FEAT_VIRTUALEDIT
10764 if (argvars[2].v_type != VAR_UNKNOWN)
10765 coladd = get_tv_number_chk(&argvars[2], NULL);
10766#endif
10767 }
10768 if (line < 0 || col < 0
10769#ifdef FEAT_VIRTUALEDIT
10770 || coladd < 0
10771#endif
10772 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 if (line > 0)
10775 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 if (col > 0)
10777 curwin->w_cursor.col = col - 1;
10778#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010779 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780#endif
10781
10782 /* Make sure the cursor is in a valid position. */
10783 check_cursor();
10784#ifdef FEAT_MBYTE
10785 /* Correct cursor for multi-byte character. */
10786 if (has_mbyte)
10787 mb_adjust_cursor();
10788#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010789
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010790 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010791 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792}
10793
10794/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010795 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 */
10797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010798f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010800 int noref = 0;
10801
10802 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010804 if (noref < 0 || noref > 1)
10805 EMSG(_(e_invarg));
10806 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010807 {
10808 current_copyID += COPYID_INC;
10809 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811}
10812
10813/*
10814 * "delete()" function
10815 */
10816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010817f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010819 char_u nbuf[NUMBUFLEN];
10820 char_u *name;
10821 char_u *flags;
10822
10823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010825 return;
10826
10827 name = get_tv_string(&argvars[0]);
10828 if (name == NULL || *name == NUL)
10829 {
10830 EMSG(_(e_invarg));
10831 return;
10832 }
10833
10834 if (argvars[1].v_type != VAR_UNKNOWN)
10835 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010837 flags = (char_u *)"";
10838
10839 if (*flags == NUL)
10840 /* delete a file */
10841 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10842 else if (STRCMP(flags, "d") == 0)
10843 /* delete an empty directory */
10844 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10845 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010846 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010847 rettv->vval.v_number = delete_recursive(name);
10848 else
10849 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850}
10851
10852/*
10853 * "did_filetype()" function
10854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010856f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857{
10858#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860#endif
10861}
10862
10863/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010864 * "diff_filler()" function
10865 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010867f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010868{
10869#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010871#endif
10872}
10873
10874/*
10875 * "diff_hlID()" function
10876 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010878f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010879{
10880#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010882 static linenr_T prev_lnum = 0;
10883 static int changedtick = 0;
10884 static int fnum = 0;
10885 static int change_start = 0;
10886 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010887 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010888 int filler_lines;
10889 int col;
10890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 if (lnum < 0) /* ignore type error in {lnum} arg */
10892 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010893 if (lnum != prev_lnum
10894 || changedtick != curbuf->b_changedtick
10895 || fnum != curbuf->b_fnum)
10896 {
10897 /* New line, buffer, change: need to get the values. */
10898 filler_lines = diff_check(curwin, lnum);
10899 if (filler_lines < 0)
10900 {
10901 if (filler_lines == -1)
10902 {
10903 change_start = MAXCOL;
10904 change_end = -1;
10905 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10906 hlID = HLF_ADD; /* added line */
10907 else
10908 hlID = HLF_CHD; /* changed line */
10909 }
10910 else
10911 hlID = HLF_ADD; /* added line */
10912 }
10913 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010914 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010915 prev_lnum = lnum;
10916 changedtick = curbuf->b_changedtick;
10917 fnum = curbuf->b_fnum;
10918 }
10919
10920 if (hlID == HLF_CHD || hlID == HLF_TXD)
10921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010922 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010923 if (col >= change_start && col <= change_end)
10924 hlID = HLF_TXD; /* changed text */
10925 else
10926 hlID = HLF_CHD; /* changed line */
10927 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010928 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010929#endif
10930}
10931
10932/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010933 * "disable_char_avail_for_testing({expr})" function
10934 */
10935 static void
10936f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10937{
10938 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10939}
10940
10941/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010942 * "empty({expr})" function
10943 */
10944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010945f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010946{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010947 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010948
10949 switch (argvars[0].v_type)
10950 {
10951 case VAR_STRING:
10952 case VAR_FUNC:
10953 n = argvars[0].vval.v_string == NULL
10954 || *argvars[0].vval.v_string == NUL;
10955 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010956 case VAR_PARTIAL:
10957 n = FALSE;
10958 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010959 case VAR_NUMBER:
10960 n = argvars[0].vval.v_number == 0;
10961 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010962 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010963#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010964 n = argvars[0].vval.v_float == 0.0;
10965 break;
10966#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010967 case VAR_LIST:
10968 n = argvars[0].vval.v_list == NULL
10969 || argvars[0].vval.v_list->lv_first == NULL;
10970 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010971 case VAR_DICT:
10972 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010973 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010974 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010975 case VAR_SPECIAL:
10976 n = argvars[0].vval.v_number != VVAL_TRUE;
10977 break;
10978
Bram Moolenaar835dc632016-02-07 14:27:38 +010010979 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010980#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010981 n = argvars[0].vval.v_job == NULL
10982 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10983 break;
10984#endif
10985 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010986#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010987 n = argvars[0].vval.v_channel == NULL
10988 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010989 break;
10990#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010991 case VAR_UNKNOWN:
10992 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10993 n = TRUE;
10994 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010995 }
10996
10997 rettv->vval.v_number = n;
10998}
10999
11000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 * "escape({string}, {chars})" function
11002 */
11003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011004f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005{
11006 char_u buf[NUMBUFLEN];
11007
Bram Moolenaar758711c2005-02-02 23:11:38 +000011008 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11009 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011}
11012
11013/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011014 * "eval()" function
11015 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011017f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011018{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011019 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011021 s = get_tv_string_chk(&argvars[0]);
11022 if (s != NULL)
11023 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011024
Bram Moolenaar615b9972015-01-14 17:15:05 +010011025 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011026 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11027 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011028 if (p != NULL && !aborting())
11029 EMSG2(_(e_invexpr2), p);
11030 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011031 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011032 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011033 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011034 else if (*s != NUL)
11035 EMSG(_(e_trailing));
11036}
11037
11038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 * "eventhandler()" function
11040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011042f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045}
11046
11047/*
11048 * "executable()" function
11049 */
11050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011051f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011053 char_u *name = get_tv_string(&argvars[0]);
11054
11055 /* Check in $PATH and also check directly if there is a directory name. */
11056 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11057 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011058}
11059
11060/*
11061 * "exepath()" function
11062 */
11063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011064f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011065{
11066 char_u *p = NULL;
11067
Bram Moolenaarb5971142015-03-21 17:32:19 +010011068 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011069 rettv->v_type = VAR_STRING;
11070 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071}
11072
11073/*
11074 * "exists()" function
11075 */
11076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011077f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 char_u *p;
11080 char_u *name;
11081 int n = FALSE;
11082 int len = 0;
11083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011084 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 if (*p == '$') /* environment variable */
11086 {
11087 /* first try "normal" environment variables (fast) */
11088 if (mch_getenv(p + 1) != NULL)
11089 n = TRUE;
11090 else
11091 {
11092 /* try expanding things like $VIM and ${HOME} */
11093 p = expand_env_save(p);
11094 if (p != NULL && *p != '$')
11095 n = TRUE;
11096 vim_free(p);
11097 }
11098 }
11099 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011102 if (*skipwhite(p) != NUL)
11103 n = FALSE; /* trailing garbage */
11104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 else if (*p == '*') /* internal or user defined function */
11106 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011107 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 }
11109 else if (*p == ':')
11110 {
11111 n = cmd_exists(p + 1);
11112 }
11113 else if (*p == '#')
11114 {
11115#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011116 if (p[1] == '#')
11117 n = autocmd_supported(p + 2);
11118 else
11119 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120#endif
11121 }
11122 else /* internal variable */
11123 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011124 char_u *tofree;
11125 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011127 /* get_name_len() takes care of expanding curly braces */
11128 name = p;
11129 len = get_name_len(&p, &tofree, TRUE, FALSE);
11130 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011132 if (tofree != NULL)
11133 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011134 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011135 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011137 /* handle d.key, l[idx], f(expr) */
11138 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11139 if (n)
11140 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 }
11142 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011143 if (*p != NUL)
11144 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011146 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 }
11148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150}
11151
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011152#ifdef FEAT_FLOAT
11153/*
11154 * "exp()" function
11155 */
11156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011157f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011158{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011159 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011160
11161 rettv->v_type = VAR_FLOAT;
11162 if (get_float_arg(argvars, &f) == OK)
11163 rettv->vval.v_float = exp(f);
11164 else
11165 rettv->vval.v_float = 0.0;
11166}
11167#endif
11168
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169/*
11170 * "expand()" function
11171 */
11172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011173f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174{
11175 char_u *s;
11176 int len;
11177 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011178 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011180 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011181 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011184 if (argvars[1].v_type != VAR_UNKNOWN
11185 && argvars[2].v_type != VAR_UNKNOWN
11186 && get_tv_number_chk(&argvars[2], &error)
11187 && !error)
11188 {
11189 rettv->v_type = VAR_LIST;
11190 rettv->vval.v_list = NULL;
11191 }
11192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 if (*s == '%' || *s == '#' || *s == '<')
11195 {
11196 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011197 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011199 if (rettv->v_type == VAR_LIST)
11200 {
11201 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11202 list_append_string(rettv->vval.v_list, result, -1);
11203 else
11204 vim_free(result);
11205 }
11206 else
11207 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 }
11209 else
11210 {
11211 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011212 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213 if (argvars[1].v_type != VAR_UNKNOWN
11214 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011215 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 if (!error)
11217 {
11218 ExpandInit(&xpc);
11219 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011220 if (p_wic)
11221 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011222 if (rettv->v_type == VAR_STRING)
11223 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11224 options, WILD_ALL);
11225 else if (rettv_list_alloc(rettv) != FAIL)
11226 {
11227 int i;
11228
11229 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11230 for (i = 0; i < xpc.xp_numfiles; i++)
11231 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11232 ExpandCleanup(&xpc);
11233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234 }
11235 else
11236 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238}
11239
11240/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011241 * Go over all entries in "d2" and add them to "d1".
11242 * When "action" is "error" then a duplicate key is an error.
11243 * When "action" is "force" then a duplicate key is overwritten.
11244 * Otherwise duplicate keys are ignored ("action" is "keep").
11245 */
11246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011247dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011248{
11249 dictitem_T *di1;
11250 hashitem_T *hi2;
11251 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011252 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011253
11254 todo = (int)d2->dv_hashtab.ht_used;
11255 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11256 {
11257 if (!HASHITEM_EMPTY(hi2))
11258 {
11259 --todo;
11260 di1 = dict_find(d1, hi2->hi_key, -1);
11261 if (d1->dv_scope != 0)
11262 {
11263 /* Disallow replacing a builtin function in l: and g:.
11264 * Check the key to be valid when adding to any
11265 * scope. */
11266 if (d1->dv_scope == VAR_DEF_SCOPE
11267 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11268 && var_check_func_name(hi2->hi_key,
11269 di1 == NULL))
11270 break;
11271 if (!valid_varname(hi2->hi_key))
11272 break;
11273 }
11274 if (di1 == NULL)
11275 {
11276 di1 = dictitem_copy(HI2DI(hi2));
11277 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11278 dictitem_free(di1);
11279 }
11280 else if (*action == 'e')
11281 {
11282 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11283 break;
11284 }
11285 else if (*action == 'f' && HI2DI(hi2) != di1)
11286 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011287 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11288 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011289 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011290 clear_tv(&di1->di_tv);
11291 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11292 }
11293 }
11294 }
11295}
11296
11297/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011298 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011299 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011300 */
11301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011302f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011303{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011304 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011305
Bram Moolenaare9a41262005-01-15 22:18:47 +000011306 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011307 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 list_T *l1, *l2;
11309 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011310 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011312
Bram Moolenaare9a41262005-01-15 22:18:47 +000011313 l1 = argvars[0].vval.v_list;
11314 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011315 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011316 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011317 {
11318 if (argvars[2].v_type != VAR_UNKNOWN)
11319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 before = get_tv_number_chk(&argvars[2], &error);
11321 if (error)
11322 return; /* type error; errmsg already given */
11323
Bram Moolenaar758711c2005-02-02 23:11:38 +000011324 if (before == l1->lv_len)
11325 item = NULL;
11326 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011327 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011328 item = list_find(l1, before);
11329 if (item == NULL)
11330 {
11331 EMSGN(_(e_listidx), before);
11332 return;
11333 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011334 }
11335 }
11336 else
11337 item = NULL;
11338 list_extend(l1, l2, item);
11339
Bram Moolenaare9a41262005-01-15 22:18:47 +000011340 copy_tv(&argvars[0], rettv);
11341 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011342 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011343 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11344 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011345 dict_T *d1, *d2;
11346 char_u *action;
11347 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011348
11349 d1 = argvars[0].vval.v_dict;
11350 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011351 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011352 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011353 {
11354 /* Check the third argument. */
11355 if (argvars[2].v_type != VAR_UNKNOWN)
11356 {
11357 static char *(av[]) = {"keep", "force", "error"};
11358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 action = get_tv_string_chk(&argvars[2]);
11360 if (action == NULL)
11361 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011362 for (i = 0; i < 3; ++i)
11363 if (STRCMP(action, av[i]) == 0)
11364 break;
11365 if (i == 3)
11366 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011367 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011368 return;
11369 }
11370 }
11371 else
11372 action = (char_u *)"force";
11373
Bram Moolenaara9922d62013-05-30 13:01:18 +020011374 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011375
Bram Moolenaare9a41262005-01-15 22:18:47 +000011376 copy_tv(&argvars[0], rettv);
11377 }
11378 }
11379 else
11380 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011381}
11382
11383/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011384 * "feedkeys()" function
11385 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011387f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011388{
11389 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011390 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011391 char_u *keys, *flags;
11392 char_u nbuf[NUMBUFLEN];
11393 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011394 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011395 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011396
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011397 /* This is not allowed in the sandbox. If the commands would still be
11398 * executed in the sandbox it would be OK, but it probably happens later,
11399 * when "sandbox" is no longer set. */
11400 if (check_secure())
11401 return;
11402
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011403 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011404
11405 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011406 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011407 flags = get_tv_string_buf(&argvars[1], nbuf);
11408 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011409 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011410 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011411 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011412 case 'n': remap = FALSE; break;
11413 case 'm': remap = TRUE; break;
11414 case 't': typed = TRUE; break;
11415 case 'i': insert = TRUE; break;
11416 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011417 }
11418 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011419 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011420
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011421 if (*keys != NUL || execute)
11422 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011423 /* Need to escape K_SPECIAL and CSI before putting the string in the
11424 * typeahead buffer. */
11425 keys_esc = vim_strsave_escape_csi(keys);
11426 if (keys_esc != NULL)
11427 {
11428 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011429 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011430 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011431 if (vgetc_busy)
11432 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011433 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011434 {
11435 int save_msg_scroll = msg_scroll;
11436
11437 /* Avoid a 1 second delay when the keys start Insert mode. */
11438 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011439
11440 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011441 exec_normal(TRUE);
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011442 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011443 msg_scroll |= save_msg_scroll;
11444 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011445 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011446 }
11447}
11448
11449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 * "filereadable()" function
11451 */
11452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011453f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011455 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 char_u *p;
11457 int n;
11458
Bram Moolenaarc236c162008-07-13 17:41:49 +000011459#ifndef O_NONBLOCK
11460# define O_NONBLOCK 0
11461#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011463 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11464 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 {
11466 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011467 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 }
11469 else
11470 n = FALSE;
11471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473}
11474
11475/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011476 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 * rights to write into.
11478 */
11479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011480f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011482 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483}
11484
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011485 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011486findfilendir(
11487 typval_T *argvars UNUSED,
11488 typval_T *rettv,
11489 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011490{
11491#ifdef FEAT_SEARCHPATH
11492 char_u *fname;
11493 char_u *fresult = NULL;
11494 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11495 char_u *p;
11496 char_u pathbuf[NUMBUFLEN];
11497 int count = 1;
11498 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011499 int error = FALSE;
11500#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011501
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011502 rettv->vval.v_string = NULL;
11503 rettv->v_type = VAR_STRING;
11504
11505#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011507
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011508 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011510 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11511 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011512 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011513 else
11514 {
11515 if (*p != NUL)
11516 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011519 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011520 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011521 }
11522
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011523 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11524 error = TRUE;
11525
11526 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528 do
11529 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011530 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011531 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 fresult = find_file_in_path_option(first ? fname : NULL,
11533 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011534 0, first, path,
11535 find_what,
11536 curbuf->b_ffname,
11537 find_what == FINDFILE_DIR
11538 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011540
11541 if (fresult != NULL && rettv->v_type == VAR_LIST)
11542 list_append_string(rettv->vval.v_list, fresult, -1);
11543
11544 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011546
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011547 if (rettv->v_type == VAR_STRING)
11548 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011549#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011550}
11551
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011552static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11553static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011554
11555/*
11556 * Implementation of map() and filter().
11557 */
11558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011559filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011560{
11561 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011562 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011563 listitem_T *li, *nli;
11564 list_T *l = NULL;
11565 dictitem_T *di;
11566 hashtab_T *ht;
11567 hashitem_T *hi;
11568 dict_T *d = NULL;
11569 typval_T save_val;
11570 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011571 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011572 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011573 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011574 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011575 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011576 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011577 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011578
Bram Moolenaare9a41262005-01-15 22:18:47 +000011579 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011580 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011581 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011582 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011583 return;
11584 }
11585 else if (argvars[0].v_type == VAR_DICT)
11586 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011587 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011588 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011589 return;
11590 }
11591 else
11592 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011593 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011594 return;
11595 }
11596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011597 expr = get_tv_string_buf_chk(&argvars[1], buf);
11598 /* On type errors, the preceding call has already displayed an error
11599 * message. Avoid a misleading error message for an empty string that
11600 * was not passed as argument. */
11601 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011603 prepare_vimvar(VV_VAL, &save_val);
11604 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011605
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011606 /* We reset "did_emsg" to be able to detect whether an error
11607 * occurred during evaluation of the expression. */
11608 save_did_emsg = did_emsg;
11609 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011610
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011611 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011614 vimvars[VV_KEY].vv_type = VAR_STRING;
11615
11616 ht = &d->dv_hashtab;
11617 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011618 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011619 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011621 if (!HASHITEM_EMPTY(hi))
11622 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011623 int r;
11624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 --todo;
11626 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011627 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011628 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11629 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011630 break;
11631 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011632 r = filter_map_one(&di->di_tv, expr, map, &rem);
11633 clear_tv(&vimvars[VV_KEY].vv_tv);
11634 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011635 break;
11636 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011637 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011638 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11639 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011640 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011641 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011642 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011643 }
11644 }
11645 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011646 }
11647 else
11648 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011649 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 for (li = l->lv_first; li != NULL; li = nli)
11652 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011653 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011654 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011655 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011656 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011657 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011658 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011659 break;
11660 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011661 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011662 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011663 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011664 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011665
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011666 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011668
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011669 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011670 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011671
11672 copy_tv(&argvars[0], rettv);
11673}
11674
11675 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011676filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011677{
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011679 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011680 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011681
Bram Moolenaar33570922005-01-25 22:26:29 +000011682 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011683 s = expr;
11684 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011685 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011686 if (*s != NUL) /* check for trailing chars after expr */
11687 {
11688 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011689 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011690 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011691 }
11692 if (map)
11693 {
11694 /* map(): replace the list item value */
11695 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011696 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011697 *tv = rettv;
11698 }
11699 else
11700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701 int error = FALSE;
11702
Bram Moolenaare9a41262005-01-15 22:18:47 +000011703 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011704 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 /* On type error, nothing has been removed; return FAIL to stop the
11707 * loop. The error message was given by get_tv_number_chk(). */
11708 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011709 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011710 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011711 retval = OK;
11712theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011713 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011714 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011715}
11716
11717/*
11718 * "filter()" function
11719 */
11720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011721f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011722{
11723 filter_map(argvars, rettv, FALSE);
11724}
11725
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011726/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011727 * "finddir({fname}[, {path}[, {count}]])" function
11728 */
11729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011730f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011731{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011732 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011733}
11734
11735/*
11736 * "findfile({fname}[, {path}[, {count}]])" function
11737 */
11738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011739f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011740{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011741 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011742}
11743
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011744#ifdef FEAT_FLOAT
11745/*
11746 * "float2nr({float})" function
11747 */
11748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011749f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011750{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011751 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011752
11753 if (get_float_arg(argvars, &f) == OK)
11754 {
11755 if (f < -0x7fffffff)
11756 rettv->vval.v_number = -0x7fffffff;
11757 else if (f > 0x7fffffff)
11758 rettv->vval.v_number = 0x7fffffff;
11759 else
11760 rettv->vval.v_number = (varnumber_T)f;
11761 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011762}
11763
11764/*
11765 * "floor({float})" function
11766 */
11767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011768f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011769{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011770 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011771
11772 rettv->v_type = VAR_FLOAT;
11773 if (get_float_arg(argvars, &f) == OK)
11774 rettv->vval.v_float = floor(f);
11775 else
11776 rettv->vval.v_float = 0.0;
11777}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011778
11779/*
11780 * "fmod()" function
11781 */
11782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011783f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011784{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011785 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011786
11787 rettv->v_type = VAR_FLOAT;
11788 if (get_float_arg(argvars, &fx) == OK
11789 && get_float_arg(&argvars[1], &fy) == OK)
11790 rettv->vval.v_float = fmod(fx, fy);
11791 else
11792 rettv->vval.v_float = 0.0;
11793}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011794#endif
11795
Bram Moolenaar0d660222005-01-07 21:51:51 +000011796/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011797 * "fnameescape({string})" function
11798 */
11799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011800f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011801{
11802 rettv->vval.v_string = vim_strsave_fnameescape(
11803 get_tv_string(&argvars[0]), FALSE);
11804 rettv->v_type = VAR_STRING;
11805}
11806
11807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 * "fnamemodify({fname}, {mods})" function
11809 */
11810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011811f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812{
11813 char_u *fname;
11814 char_u *mods;
11815 int usedlen = 0;
11816 int len;
11817 char_u *fbuf = NULL;
11818 char_u buf[NUMBUFLEN];
11819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 fname = get_tv_string_chk(&argvars[0]);
11821 mods = get_tv_string_buf_chk(&argvars[1], buf);
11822 if (fname == NULL || mods == NULL)
11823 fname = NULL;
11824 else
11825 {
11826 len = (int)STRLEN(fname);
11827 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011834 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 vim_free(fbuf);
11836}
11837
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011838static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839
11840/*
11841 * "foldclosed()" function
11842 */
11843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011844foldclosed_both(
11845 typval_T *argvars UNUSED,
11846 typval_T *rettv,
11847 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848{
11849#ifdef FEAT_FOLDING
11850 linenr_T lnum;
11851 linenr_T first, last;
11852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11855 {
11856 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11857 {
11858 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 return;
11863 }
11864 }
11865#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867}
11868
11869/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011870 * "foldclosed()" function
11871 */
11872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011873f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011874{
11875 foldclosed_both(argvars, rettv, FALSE);
11876}
11877
11878/*
11879 * "foldclosedend()" function
11880 */
11881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011882f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011883{
11884 foldclosed_both(argvars, rettv, TRUE);
11885}
11886
11887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 * "foldlevel()" function
11889 */
11890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011891f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892{
11893#ifdef FEAT_FOLDING
11894 linenr_T lnum;
11895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900}
11901
11902/*
11903 * "foldtext()" function
11904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011906f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907{
11908#ifdef FEAT_FOLDING
11909 linenr_T lnum;
11910 char_u *s;
11911 char_u *r;
11912 int len;
11913 char *txt;
11914#endif
11915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011916 rettv->v_type = VAR_STRING;
11917 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011919 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11920 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11921 <= curbuf->b_ml.ml_line_count
11922 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 {
11924 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011925 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11926 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 {
11928 if (!linewhite(lnum))
11929 break;
11930 ++lnum;
11931 }
11932
11933 /* Find interesting text in this line. */
11934 s = skipwhite(ml_get(lnum));
11935 /* skip C comment-start */
11936 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011939 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011940 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011941 {
11942 s = skipwhite(ml_get(lnum + 1));
11943 if (*s == '*')
11944 s = skipwhite(s + 1);
11945 }
11946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 txt = _("+-%s%3ld lines: ");
11948 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011949 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 + 20 /* for %3ld */
11951 + STRLEN(s))); /* concatenated */
11952 if (r != NULL)
11953 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011954 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11955 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11956 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 len = (int)STRLEN(r);
11958 STRCAT(r, s);
11959 /* remove 'foldmarker' and 'commentstring' */
11960 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962 }
11963 }
11964#endif
11965}
11966
11967/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011968 * "foldtextresult(lnum)" function
11969 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011971f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011972{
11973#ifdef FEAT_FOLDING
11974 linenr_T lnum;
11975 char_u *text;
11976 char_u buf[51];
11977 foldinfo_T foldinfo;
11978 int fold_count;
11979#endif
11980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981 rettv->v_type = VAR_STRING;
11982 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011983#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985 /* treat illegal types and illegal string values for {lnum} the same */
11986 if (lnum < 0)
11987 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011988 fold_count = foldedCount(curwin, lnum, &foldinfo);
11989 if (fold_count > 0)
11990 {
11991 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11992 &foldinfo, buf);
11993 if (text == buf)
11994 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011996 }
11997#endif
11998}
11999
12000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 * "foreground()" function
12002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012004f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006#ifdef FEAT_GUI
12007 if (gui.in_use)
12008 gui_mch_set_foreground();
12009#else
12010# ifdef WIN32
12011 win32_set_foreground();
12012# endif
12013#endif
12014}
12015
12016/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012017 * "function()" function
12018 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012020f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012021{
12022 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012023 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012024 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012025 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012026
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012027 if (argvars[0].v_type == VAR_FUNC)
12028 {
12029 /* function(MyFunc, [arg], dict) */
12030 s = argvars[0].vval.v_string;
12031 }
12032 else if (argvars[0].v_type == VAR_PARTIAL
12033 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012034 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012035 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012036 arg_pt = argvars[0].vval.v_partial;
12037 s = arg_pt->pt_name;
12038 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012039 else
12040 {
12041 /* function('MyFunc', [arg], dict) */
12042 s = get_tv_string(&argvars[0]);
12043 use_string = TRUE;
12044 }
12045
12046 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012047 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012048 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012049 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12050 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012051 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012052 else
12053 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012054 int dict_idx = 0;
12055 int arg_idx = 0;
12056 list_T *list = NULL;
12057
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012058 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012059 {
12060 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012061 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012062
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012063 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12064 * also be called from another script. Using trans_function_name()
12065 * would also work, but some plugins depend on the name being
12066 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012067 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012068 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12069 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012070 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012071 STRCPY(name, sid_buf);
12072 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012073 }
12074 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012075 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012076 name = vim_strsave(s);
12077
12078 if (argvars[1].v_type != VAR_UNKNOWN)
12079 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012080 if (argvars[2].v_type != VAR_UNKNOWN)
12081 {
12082 /* function(name, [args], dict) */
12083 arg_idx = 1;
12084 dict_idx = 2;
12085 }
12086 else if (argvars[1].v_type == VAR_DICT)
12087 /* function(name, dict) */
12088 dict_idx = 1;
12089 else
12090 /* function(name, [args]) */
12091 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012092 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012093 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012094 if (argvars[dict_idx].v_type != VAR_DICT)
12095 {
12096 EMSG(_("E922: expected a dict"));
12097 vim_free(name);
12098 return;
12099 }
12100 if (argvars[dict_idx].vval.v_dict == NULL)
12101 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012102 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012103 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012104 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012105 if (argvars[arg_idx].v_type != VAR_LIST)
12106 {
12107 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12108 vim_free(name);
12109 return;
12110 }
12111 list = argvars[arg_idx].vval.v_list;
12112 if (list == NULL || list->lv_len == 0)
12113 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012114 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012115 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012116 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012117 {
12118 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012119
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012120 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012121 if (pt == NULL)
12122 vim_free(name);
12123 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012124 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012125 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012126 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012127 listitem_T *li;
12128 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012129 int arg_len = 0;
12130 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012131
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012132 if (arg_pt != NULL)
12133 arg_len = arg_pt->pt_argc;
12134 if (list != NULL)
12135 lv_len = list->lv_len;
12136 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012137 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012138 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012139 if (pt->pt_argv == NULL)
12140 {
12141 vim_free(pt);
12142 vim_free(name);
12143 return;
12144 }
12145 else
12146 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012147 for (i = 0; i < arg_len; i++)
12148 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12149 if (lv_len > 0)
12150 for (li = list->lv_first; li != NULL;
12151 li = li->li_next)
12152 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012153 }
12154 }
12155
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012156 /* For "function(dict.func, [], dict)" and "func" is a partial
12157 * use "dict". That is backwards compatible. */
12158 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012159 {
12160 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12161 ++pt->pt_dict->dv_refcount;
12162 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012163 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012164 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012165 pt->pt_dict = arg_pt->pt_dict;
12166 if (pt->pt_dict != NULL)
12167 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012168 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012169
12170 pt->pt_refcount = 1;
12171 pt->pt_name = name;
12172 func_ref(pt->pt_name);
12173 }
12174 rettv->v_type = VAR_PARTIAL;
12175 rettv->vval.v_partial = pt;
12176 }
12177 else
12178 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012179 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012180 rettv->v_type = VAR_FUNC;
12181 rettv->vval.v_string = name;
12182 func_ref(name);
12183 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012184 }
12185}
12186
12187/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012188 * "garbagecollect()" function
12189 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012191f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012192{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012193 /* This is postponed until we are back at the toplevel, because we may be
12194 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12195 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012196
12197 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12198 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012199}
12200
12201/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012202 * "get()" function
12203 */
12204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012205f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206{
Bram Moolenaar33570922005-01-25 22:26:29 +000012207 listitem_T *li;
12208 list_T *l;
12209 dictitem_T *di;
12210 dict_T *d;
12211 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012212
Bram Moolenaare9a41262005-01-15 22:18:47 +000012213 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012215 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012217 int error = FALSE;
12218
12219 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12220 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012221 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012222 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012224 else if (argvars[0].v_type == VAR_DICT)
12225 {
12226 if ((d = argvars[0].vval.v_dict) != NULL)
12227 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012228 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012229 if (di != NULL)
12230 tv = &di->di_tv;
12231 }
12232 }
12233 else
12234 EMSG2(_(e_listdictarg), "get()");
12235
12236 if (tv == NULL)
12237 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012238 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012239 copy_tv(&argvars[2], rettv);
12240 }
12241 else
12242 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012243}
12244
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012245static 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 +000012246
12247/*
12248 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012249 * Return a range (from start to end) of lines in rettv from the specified
12250 * buffer.
12251 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012252 */
12253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012254get_buffer_lines(
12255 buf_T *buf,
12256 linenr_T start,
12257 linenr_T end,
12258 int retlist,
12259 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012260{
12261 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012262
Bram Moolenaar959a1432013-12-14 12:17:38 +010012263 rettv->v_type = VAR_STRING;
12264 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012265 if (retlist && rettv_list_alloc(rettv) == FAIL)
12266 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012267
12268 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12269 return;
12270
12271 if (!retlist)
12272 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012273 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12274 p = ml_get_buf(buf, start, FALSE);
12275 else
12276 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012277 rettv->vval.v_string = vim_strsave(p);
12278 }
12279 else
12280 {
12281 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012282 return;
12283
12284 if (start < 1)
12285 start = 1;
12286 if (end > buf->b_ml.ml_line_count)
12287 end = buf->b_ml.ml_line_count;
12288 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012289 if (list_append_string(rettv->vval.v_list,
12290 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012291 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012292 }
12293}
12294
12295/*
12296 * "getbufline()" function
12297 */
12298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012299f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012300{
12301 linenr_T lnum;
12302 linenr_T end;
12303 buf_T *buf;
12304
12305 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12306 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012307 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012308 --emsg_off;
12309
Bram Moolenaar661b1822005-07-28 22:36:45 +000012310 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012311 if (argvars[2].v_type == VAR_UNKNOWN)
12312 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012313 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012314 end = get_tv_lnum_buf(&argvars[2], buf);
12315
Bram Moolenaar342337a2005-07-21 21:11:17 +000012316 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012317}
12318
Bram Moolenaar0d660222005-01-07 21:51:51 +000012319/*
12320 * "getbufvar()" function
12321 */
12322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012323f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012324{
12325 buf_T *buf;
12326 buf_T *save_curbuf;
12327 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012328 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012329 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012331 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12332 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012333 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012334 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012336 rettv->v_type = VAR_STRING;
12337 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012338
12339 if (buf != NULL && varname != NULL)
12340 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012341 /* set curbuf to be our buf, temporarily */
12342 save_curbuf = curbuf;
12343 curbuf = buf;
12344
Bram Moolenaar0d660222005-01-07 21:51:51 +000012345 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012346 {
12347 if (get_option_tv(&varname, rettv, TRUE) == OK)
12348 done = TRUE;
12349 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012350 else if (STRCMP(varname, "changedtick") == 0)
12351 {
12352 rettv->v_type = VAR_NUMBER;
12353 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012354 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012355 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012356 else
12357 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012358 /* Look up the variable. */
12359 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12360 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12361 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012362 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012363 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012364 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012365 done = TRUE;
12366 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012367 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012368
12369 /* restore previous notion of curbuf */
12370 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371 }
12372
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012373 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12374 /* use the default value */
12375 copy_tv(&argvars[2], rettv);
12376
Bram Moolenaar0d660222005-01-07 21:51:51 +000012377 --emsg_off;
12378}
12379
12380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 * "getchar()" function
12382 */
12383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012384f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385{
12386 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012387 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012389 /* Position the cursor. Needed after a message that ends in a space. */
12390 windgoto(msg_row, msg_col);
12391
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 ++no_mapping;
12393 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012394 for (;;)
12395 {
12396 if (argvars[0].v_type == VAR_UNKNOWN)
12397 /* getchar(): blocking wait. */
12398 n = safe_vgetc();
12399 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12400 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012401 n = vpeekc_any();
12402 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012403 /* illegal argument or getchar(0) and no char avail: return zero */
12404 n = 0;
12405 else
12406 /* getchar(0) and char avail: return char */
12407 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012408
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012409 if (n == K_IGNORE)
12410 continue;
12411 break;
12412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 --no_mapping;
12414 --allow_keys;
12415
Bram Moolenaar219b8702006-11-01 14:32:36 +000012416 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12417 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12418 vimvars[VV_MOUSE_COL].vv_nr = 0;
12419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 if (IS_SPECIAL(n) || mod_mask != 0)
12422 {
12423 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12424 int i = 0;
12425
12426 /* Turn a special key into three bytes, plus modifier. */
12427 if (mod_mask != 0)
12428 {
12429 temp[i++] = K_SPECIAL;
12430 temp[i++] = KS_MODIFIER;
12431 temp[i++] = mod_mask;
12432 }
12433 if (IS_SPECIAL(n))
12434 {
12435 temp[i++] = K_SPECIAL;
12436 temp[i++] = K_SECOND(n);
12437 temp[i++] = K_THIRD(n);
12438 }
12439#ifdef FEAT_MBYTE
12440 else if (has_mbyte)
12441 i += (*mb_char2bytes)(n, temp + i);
12442#endif
12443 else
12444 temp[i++] = n;
12445 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012446 rettv->v_type = VAR_STRING;
12447 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012448
12449#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012450 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012451 {
12452 int row = mouse_row;
12453 int col = mouse_col;
12454 win_T *win;
12455 linenr_T lnum;
12456# ifdef FEAT_WINDOWS
12457 win_T *wp;
12458# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012459 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012460
12461 if (row >= 0 && col >= 0)
12462 {
12463 /* Find the window at the mouse coordinates and compute the
12464 * text position. */
12465 win = mouse_find_win(&row, &col);
12466 (void)mouse_comp_pos(win, &row, &col, &lnum);
12467# ifdef FEAT_WINDOWS
12468 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012469 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012470# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012471 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012472 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12473 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12474 }
12475 }
12476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 }
12478}
12479
12480/*
12481 * "getcharmod()" function
12482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012484f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487}
12488
12489/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012490 * "getcharsearch()" function
12491 */
12492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012493f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012494{
12495 if (rettv_dict_alloc(rettv) != FAIL)
12496 {
12497 dict_T *dict = rettv->vval.v_dict;
12498
12499 dict_add_nr_str(dict, "char", 0L, last_csearch());
12500 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12501 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12502 }
12503}
12504
12505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 * "getcmdline()" function
12507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012509f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 rettv->v_type = VAR_STRING;
12512 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513}
12514
12515/*
12516 * "getcmdpos()" function
12517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012519f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522}
12523
12524/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012525 * "getcmdtype()" function
12526 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012528f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012529{
12530 rettv->v_type = VAR_STRING;
12531 rettv->vval.v_string = alloc(2);
12532 if (rettv->vval.v_string != NULL)
12533 {
12534 rettv->vval.v_string[0] = get_cmdline_type();
12535 rettv->vval.v_string[1] = NUL;
12536 }
12537}
12538
12539/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012540 * "getcmdwintype()" function
12541 */
12542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012543f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012544{
12545 rettv->v_type = VAR_STRING;
12546 rettv->vval.v_string = NULL;
12547#ifdef FEAT_CMDWIN
12548 rettv->vval.v_string = alloc(2);
12549 if (rettv->vval.v_string != NULL)
12550 {
12551 rettv->vval.v_string[0] = cmdwin_type;
12552 rettv->vval.v_string[1] = NUL;
12553 }
12554#endif
12555}
12556
12557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558 * "getcwd()" function
12559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012561f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012563 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012564 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012566 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012567 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012568
12569 wp = find_tabwin(&argvars[0], &argvars[1]);
12570 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012572 if (wp->w_localdir != NULL)
12573 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12574 else if(globaldir != NULL)
12575 rettv->vval.v_string = vim_strsave(globaldir);
12576 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012577 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012578 cwd = alloc(MAXPATHL);
12579 if (cwd != NULL)
12580 {
12581 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12582 rettv->vval.v_string = vim_strsave(cwd);
12583 vim_free(cwd);
12584 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012585 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012586#ifdef BACKSLASH_IN_FILENAME
12587 if (rettv->vval.v_string != NULL)
12588 slash_adjust(rettv->vval.v_string);
12589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 }
12591}
12592
12593/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012594 * "getfontname()" function
12595 */
12596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012597f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599 rettv->v_type = VAR_STRING;
12600 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012601#ifdef FEAT_GUI
12602 if (gui.in_use)
12603 {
12604 GuiFont font;
12605 char_u *name = NULL;
12606
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012607 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012608 {
12609 /* Get the "Normal" font. Either the name saved by
12610 * hl_set_font_name() or from the font ID. */
12611 font = gui.norm_font;
12612 name = hl_get_font_name();
12613 }
12614 else
12615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012617 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12618 return;
12619 font = gui_mch_get_font(name, FALSE);
12620 if (font == NOFONT)
12621 return; /* Invalid font name, return empty string. */
12622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012623 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012624 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012625 gui_mch_free_font(font);
12626 }
12627#endif
12628}
12629
12630/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012631 * "getfperm({fname})" function
12632 */
12633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012634f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012635{
12636 char_u *fname;
12637 struct stat st;
12638 char_u *perm = NULL;
12639 char_u flags[] = "rwx";
12640 int i;
12641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012642 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012644 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012645 if (mch_stat((char *)fname, &st) >= 0)
12646 {
12647 perm = vim_strsave((char_u *)"---------");
12648 if (perm != NULL)
12649 {
12650 for (i = 0; i < 9; i++)
12651 {
12652 if (st.st_mode & (1 << (8 - i)))
12653 perm[i] = flags[i % 3];
12654 }
12655 }
12656 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012658}
12659
12660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661 * "getfsize({fname})" function
12662 */
12663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012664f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665{
12666 char_u *fname;
12667 struct stat st;
12668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012669 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672
12673 if (mch_stat((char *)fname, &st) >= 0)
12674 {
12675 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012680
12681 /* non-perfect check for overflow */
12682 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12683 rettv->vval.v_number = -2;
12684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 }
12686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688}
12689
12690/*
12691 * "getftime({fname})" function
12692 */
12693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012694f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695{
12696 char_u *fname;
12697 struct stat st;
12698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700
12701 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012702 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705}
12706
12707/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012708 * "getftype({fname})" function
12709 */
12710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012711f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012712{
12713 char_u *fname;
12714 struct stat st;
12715 char_u *type = NULL;
12716 char *t;
12717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012721 if (mch_lstat((char *)fname, &st) >= 0)
12722 {
12723#ifdef S_ISREG
12724 if (S_ISREG(st.st_mode))
12725 t = "file";
12726 else if (S_ISDIR(st.st_mode))
12727 t = "dir";
12728# ifdef S_ISLNK
12729 else if (S_ISLNK(st.st_mode))
12730 t = "link";
12731# endif
12732# ifdef S_ISBLK
12733 else if (S_ISBLK(st.st_mode))
12734 t = "bdev";
12735# endif
12736# ifdef S_ISCHR
12737 else if (S_ISCHR(st.st_mode))
12738 t = "cdev";
12739# endif
12740# ifdef S_ISFIFO
12741 else if (S_ISFIFO(st.st_mode))
12742 t = "fifo";
12743# endif
12744# ifdef S_ISSOCK
12745 else if (S_ISSOCK(st.st_mode))
12746 t = "fifo";
12747# endif
12748 else
12749 t = "other";
12750#else
12751# ifdef S_IFMT
12752 switch (st.st_mode & S_IFMT)
12753 {
12754 case S_IFREG: t = "file"; break;
12755 case S_IFDIR: t = "dir"; break;
12756# ifdef S_IFLNK
12757 case S_IFLNK: t = "link"; break;
12758# endif
12759# ifdef S_IFBLK
12760 case S_IFBLK: t = "bdev"; break;
12761# endif
12762# ifdef S_IFCHR
12763 case S_IFCHR: t = "cdev"; break;
12764# endif
12765# ifdef S_IFIFO
12766 case S_IFIFO: t = "fifo"; break;
12767# endif
12768# ifdef S_IFSOCK
12769 case S_IFSOCK: t = "socket"; break;
12770# endif
12771 default: t = "other";
12772 }
12773# else
12774 if (mch_isdir(fname))
12775 t = "dir";
12776 else
12777 t = "file";
12778# endif
12779#endif
12780 type = vim_strsave((char_u *)t);
12781 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012783}
12784
12785/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012786 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012787 */
12788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012789f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012790{
12791 linenr_T lnum;
12792 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012793 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012794
12795 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012796 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012797 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012798 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012799 retlist = FALSE;
12800 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012801 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012802 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012803 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012804 retlist = TRUE;
12805 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012806
Bram Moolenaar342337a2005-07-21 21:11:17 +000012807 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012808}
12809
12810/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012811 * "getmatches()" function
12812 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012814f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012815{
12816#ifdef FEAT_SEARCH_EXTRA
12817 dict_T *dict;
12818 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012819 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012820
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012821 if (rettv_list_alloc(rettv) == OK)
12822 {
12823 while (cur != NULL)
12824 {
12825 dict = dict_alloc();
12826 if (dict == NULL)
12827 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012828 if (cur->match.regprog == NULL)
12829 {
12830 /* match added with matchaddpos() */
12831 for (i = 0; i < MAXPOSMATCH; ++i)
12832 {
12833 llpos_T *llpos;
12834 char buf[6];
12835 list_T *l;
12836
12837 llpos = &cur->pos.pos[i];
12838 if (llpos->lnum == 0)
12839 break;
12840 l = list_alloc();
12841 if (l == NULL)
12842 break;
12843 list_append_number(l, (varnumber_T)llpos->lnum);
12844 if (llpos->col > 0)
12845 {
12846 list_append_number(l, (varnumber_T)llpos->col);
12847 list_append_number(l, (varnumber_T)llpos->len);
12848 }
12849 sprintf(buf, "pos%d", i + 1);
12850 dict_add_list(dict, buf, l);
12851 }
12852 }
12853 else
12854 {
12855 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12856 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012857 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012858 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12859 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020012860# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020012861 if (cur->conceal_char)
12862 {
12863 char_u buf[MB_MAXBYTES + 1];
12864
12865 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12866 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12867 }
12868# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012869 list_append_dict(rettv->vval.v_list, dict);
12870 cur = cur->next;
12871 }
12872 }
12873#endif
12874}
12875
12876/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012877 * "getpid()" function
12878 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012880f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012881{
12882 rettv->vval.v_number = mch_get_pid();
12883}
12884
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012885static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012886
12887/*
12888 * "getcurpos()" function
12889 */
12890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012891f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012892{
12893 getpos_both(argvars, rettv, TRUE);
12894}
12895
Bram Moolenaar18081e32008-02-20 19:11:07 +000012896/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012897 * "getpos(string)" function
12898 */
12899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012900f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012901{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012902 getpos_both(argvars, rettv, FALSE);
12903}
12904
12905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012906getpos_both(
12907 typval_T *argvars,
12908 typval_T *rettv,
12909 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012910{
Bram Moolenaara5525202006-03-02 22:52:09 +000012911 pos_T *fp;
12912 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012913 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012914
12915 if (rettv_list_alloc(rettv) == OK)
12916 {
12917 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012918 if (getcurpos)
12919 fp = &curwin->w_cursor;
12920 else
12921 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012922 if (fnum != -1)
12923 list_append_number(l, (varnumber_T)fnum);
12924 else
12925 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012926 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12927 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012928 list_append_number(l, (fp != NULL)
12929 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012930 : (varnumber_T)0);
12931 list_append_number(l,
12932#ifdef FEAT_VIRTUALEDIT
12933 (fp != NULL) ? (varnumber_T)fp->coladd :
12934#endif
12935 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012936 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012937 {
12938 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012939 list_append_number(l, curwin->w_curswant == MAXCOL ?
12940 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012941 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012942 }
12943 else
12944 rettv->vval.v_number = FALSE;
12945}
12946
12947/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012948 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012949 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012951f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012952{
12953#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012954 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012955#endif
12956
Bram Moolenaar2641f772005-03-25 21:58:17 +000012957#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012958 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012959 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012960 wp = NULL;
12961 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12962 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012963 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012964 if (wp == NULL)
12965 return;
12966 }
12967
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012968 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012969 }
12970#endif
12971}
12972
12973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 * "getreg()" function
12975 */
12976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012977f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978{
12979 char_u *strregname;
12980 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012981 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012982 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012983 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012985 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012987 strregname = get_tv_string_chk(&argvars[0]);
12988 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012989 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012991 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012992 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12993 return_list = get_tv_number_chk(&argvars[2], &error);
12994 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012997 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012998
12999 if (error)
13000 return;
13001
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002 regname = (strregname == NULL ? '"' : *strregname);
13003 if (regname == 0)
13004 regname = '"';
13005
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013006 if (return_list)
13007 {
13008 rettv->v_type = VAR_LIST;
13009 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13010 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013011 if (rettv->vval.v_list != NULL)
13012 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013013 }
13014 else
13015 {
13016 rettv->v_type = VAR_STRING;
13017 rettv->vval.v_string = get_reg_contents(regname,
13018 arg2 ? GREG_EXPR_SRC : 0);
13019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020}
13021
13022/*
13023 * "getregtype()" function
13024 */
13025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013026f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027{
13028 char_u *strregname;
13029 int regname;
13030 char_u buf[NUMBUFLEN + 2];
13031 long reglen = 0;
13032
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013033 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013034 {
13035 strregname = get_tv_string_chk(&argvars[0]);
13036 if (strregname == NULL) /* type error; errmsg already given */
13037 {
13038 rettv->v_type = VAR_STRING;
13039 rettv->vval.v_string = NULL;
13040 return;
13041 }
13042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 else
13044 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013045 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046
13047 regname = (strregname == NULL ? '"' : *strregname);
13048 if (regname == 0)
13049 regname = '"';
13050
13051 buf[0] = NUL;
13052 buf[1] = NUL;
13053 switch (get_reg_type(regname, &reglen))
13054 {
13055 case MLINE: buf[0] = 'V'; break;
13056 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057 case MBLOCK:
13058 buf[0] = Ctrl_V;
13059 sprintf((char *)buf + 1, "%ld", reglen + 1);
13060 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062 rettv->v_type = VAR_STRING;
13063 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064}
13065
13066/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013067 * "gettabvar()" function
13068 */
13069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013070f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013071{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013072 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013073 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013074 dictitem_T *v;
13075 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013076 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013077
13078 rettv->v_type = VAR_STRING;
13079 rettv->vval.v_string = NULL;
13080
13081 varname = get_tv_string_chk(&argvars[1]);
13082 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13083 if (tp != NULL && varname != NULL)
13084 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013085 /* Set tp to be our tabpage, temporarily. Also set the window to the
13086 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013087 if (switch_win(&oldcurwin, &oldtabpage,
13088 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013089 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013090 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013091 /* look up the variable */
13092 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13093 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13094 if (v != NULL)
13095 {
13096 copy_tv(&v->di_tv, rettv);
13097 done = TRUE;
13098 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013099 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013100
13101 /* restore previous notion of curwin */
13102 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013103 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013104
13105 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013106 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013107}
13108
13109/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013110 * "gettabwinvar()" function
13111 */
13112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013113f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013114{
13115 getwinvar(argvars, rettv, 1);
13116}
13117
13118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 * "getwinposx()" function
13120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013122f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013124 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125#ifdef FEAT_GUI
13126 if (gui.in_use)
13127 {
13128 int x, y;
13129
13130 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 }
13133#endif
13134}
13135
13136/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013137 * "win_findbuf()" function
13138 */
13139 static void
13140f_win_findbuf(typval_T *argvars, typval_T *rettv)
13141{
13142 if (rettv_list_alloc(rettv) != FAIL)
13143 win_findbuf(argvars, rettv->vval.v_list);
13144}
13145
13146/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013147 * "win_getid()" function
13148 */
13149 static void
13150f_win_getid(typval_T *argvars, typval_T *rettv)
13151{
13152 rettv->vval.v_number = win_getid(argvars);
13153}
13154
13155/*
13156 * "win_gotoid()" function
13157 */
13158 static void
13159f_win_gotoid(typval_T *argvars, typval_T *rettv)
13160{
13161 rettv->vval.v_number = win_gotoid(argvars);
13162}
13163
13164/*
13165 * "win_id2tabwin()" function
13166 */
13167 static void
13168f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13169{
13170 if (rettv_list_alloc(rettv) != FAIL)
13171 win_id2tabwin(argvars, rettv->vval.v_list);
13172}
13173
13174/*
13175 * "win_id2win()" function
13176 */
13177 static void
13178f_win_id2win(typval_T *argvars, typval_T *rettv)
13179{
13180 rettv->vval.v_number = win_id2win(argvars);
13181}
13182
13183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 * "getwinposy()" function
13185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013187f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190#ifdef FEAT_GUI
13191 if (gui.in_use)
13192 {
13193 int x, y;
13194
13195 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013196 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 }
13198#endif
13199}
13200
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013201/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013202 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013203 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013204 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013205find_win_by_nr(
13206 typval_T *vp,
13207 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013208{
13209#ifdef FEAT_WINDOWS
13210 win_T *wp;
13211#endif
13212 int nr;
13213
13214 nr = get_tv_number_chk(vp, NULL);
13215
13216#ifdef FEAT_WINDOWS
13217 if (nr < 0)
13218 return NULL;
13219 if (nr == 0)
13220 return curwin;
13221
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013222 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13223 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013224 if (--nr <= 0)
13225 break;
13226 return wp;
13227#else
13228 if (nr == 0 || nr == 1)
13229 return curwin;
13230 return NULL;
13231#endif
13232}
13233
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013235 * Find window specified by "wvp" in tabpage "tvp".
13236 */
13237 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013238find_tabwin(
13239 typval_T *wvp, /* VAR_UNKNOWN for current window */
13240 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013241{
13242 win_T *wp = NULL;
13243 tabpage_T *tp = NULL;
13244 long n;
13245
13246 if (wvp->v_type != VAR_UNKNOWN)
13247 {
13248 if (tvp->v_type != VAR_UNKNOWN)
13249 {
13250 n = get_tv_number(tvp);
13251 if (n >= 0)
13252 tp = find_tabpage(n);
13253 }
13254 else
13255 tp = curtab;
13256
13257 if (tp != NULL)
13258 wp = find_win_by_nr(wvp, tp);
13259 }
13260 else
13261 wp = curwin;
13262
13263 return wp;
13264}
13265
13266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 * "getwinvar()" function
13268 */
13269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013270f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013272 getwinvar(argvars, rettv, 0);
13273}
13274
13275/*
13276 * getwinvar() and gettabwinvar()
13277 */
13278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013279getwinvar(
13280 typval_T *argvars,
13281 typval_T *rettv,
13282 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013283{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013284 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013286 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013287 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013288 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013289#ifdef FEAT_WINDOWS
13290 win_T *oldcurwin;
13291 tabpage_T *oldtabpage;
13292 int need_switch_win;
13293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013295#ifdef FEAT_WINDOWS
13296 if (off == 1)
13297 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13298 else
13299 tp = curtab;
13300#endif
13301 win = find_win_by_nr(&argvars[off], tp);
13302 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013303 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013305 rettv->v_type = VAR_STRING;
13306 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307
13308 if (win != NULL && varname != NULL)
13309 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013310#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013311 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013312 * otherwise the window is not valid. Only do this when needed,
13313 * autocommands get blocked. */
13314 need_switch_win = !(tp == curtab && win == curwin);
13315 if (!need_switch_win
13316 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13317#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013318 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013319 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013320 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013321 if (get_option_tv(&varname, rettv, 1) == OK)
13322 done = TRUE;
13323 }
13324 else
13325 {
13326 /* Look up the variable. */
13327 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13328 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13329 varname, FALSE);
13330 if (v != NULL)
13331 {
13332 copy_tv(&v->di_tv, rettv);
13333 done = TRUE;
13334 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013337
Bram Moolenaarba117c22015-09-29 16:53:22 +020013338#ifdef FEAT_WINDOWS
13339 if (need_switch_win)
13340 /* restore previous notion of curwin */
13341 restore_win(oldcurwin, oldtabpage, TRUE);
13342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 }
13344
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013345 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13346 /* use the default return value */
13347 copy_tv(&argvars[off + 2], rettv);
13348
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 --emsg_off;
13350}
13351
13352/*
13353 * "glob()" function
13354 */
13355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013356f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013358 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013360 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013362 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013363 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013364 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013365 if (argvars[1].v_type != VAR_UNKNOWN)
13366 {
13367 if (get_tv_number_chk(&argvars[1], &error))
13368 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013369 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013370 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013371 if (get_tv_number_chk(&argvars[2], &error))
13372 {
13373 rettv->v_type = VAR_LIST;
13374 rettv->vval.v_list = NULL;
13375 }
13376 if (argvars[3].v_type != VAR_UNKNOWN
13377 && get_tv_number_chk(&argvars[3], &error))
13378 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013379 }
13380 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013381 if (!error)
13382 {
13383 ExpandInit(&xpc);
13384 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013385 if (p_wic)
13386 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013387 if (rettv->v_type == VAR_STRING)
13388 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013389 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013390 else if (rettv_list_alloc(rettv) != FAIL)
13391 {
13392 int i;
13393
13394 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13395 NULL, options, WILD_ALL_KEEP);
13396 for (i = 0; i < xpc.xp_numfiles; i++)
13397 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13398
13399 ExpandCleanup(&xpc);
13400 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013401 }
13402 else
13403 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404}
13405
13406/*
13407 * "globpath()" function
13408 */
13409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013410f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013412 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013415 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013416 garray_T ga;
13417 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013419 /* When the optional second argument is non-zero, don't remove matches
13420 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013422 if (argvars[2].v_type != VAR_UNKNOWN)
13423 {
13424 if (get_tv_number_chk(&argvars[2], &error))
13425 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013426 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013427 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013428 if (get_tv_number_chk(&argvars[3], &error))
13429 {
13430 rettv->v_type = VAR_LIST;
13431 rettv->vval.v_list = NULL;
13432 }
13433 if (argvars[4].v_type != VAR_UNKNOWN
13434 && get_tv_number_chk(&argvars[4], &error))
13435 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013436 }
13437 }
13438 if (file != NULL && !error)
13439 {
13440 ga_init2(&ga, (int)sizeof(char_u *), 10);
13441 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13442 if (rettv->v_type == VAR_STRING)
13443 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13444 else if (rettv_list_alloc(rettv) != FAIL)
13445 for (i = 0; i < ga.ga_len; ++i)
13446 list_append_string(rettv->vval.v_list,
13447 ((char_u **)(ga.ga_data))[i], -1);
13448 ga_clear_strings(&ga);
13449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013450 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013451 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452}
13453
13454/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013455 * "glob2regpat()" function
13456 */
13457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013458f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013459{
13460 char_u *pat = get_tv_string_chk(&argvars[0]);
13461
13462 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013463 rettv->vval.v_string = (pat == NULL)
13464 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013465}
13466
13467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 * "has()" function
13469 */
13470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013471f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472{
13473 int i;
13474 char_u *name;
13475 int n = FALSE;
13476 static char *(has_list[]) =
13477 {
13478#ifdef AMIGA
13479 "amiga",
13480# ifdef FEAT_ARP
13481 "arp",
13482# endif
13483#endif
13484#ifdef __BEOS__
13485 "beos",
13486#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013487#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 "mac",
13489#endif
13490#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013491 "macunix", /* built with 'darwin' enabled */
13492#endif
13493#if defined(__APPLE__) && __APPLE__ == 1
13494 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496#ifdef __QNX__
13497 "qnx",
13498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499#ifdef UNIX
13500 "unix",
13501#endif
13502#ifdef VMS
13503 "vms",
13504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505#ifdef WIN32
13506 "win32",
13507#endif
13508#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13509 "win32unix",
13510#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013511#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 "win64",
13513#endif
13514#ifdef EBCDIC
13515 "ebcdic",
13516#endif
13517#ifndef CASE_INSENSITIVE_FILENAME
13518 "fname_case",
13519#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013520#ifdef HAVE_ACL
13521 "acl",
13522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523#ifdef FEAT_ARABIC
13524 "arabic",
13525#endif
13526#ifdef FEAT_AUTOCMD
13527 "autocmd",
13528#endif
13529#ifdef FEAT_BEVAL
13530 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013531# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13532 "balloon_multiline",
13533# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534#endif
13535#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13536 "builtin_terms",
13537# ifdef ALL_BUILTIN_TCAPS
13538 "all_builtin_terms",
13539# endif
13540#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013541#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13542 || defined(FEAT_GUI_W32) \
13543 || defined(FEAT_GUI_MOTIF))
13544 "browsefilter",
13545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546#ifdef FEAT_BYTEOFF
13547 "byte_offset",
13548#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013549#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013550 "channel",
13551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552#ifdef FEAT_CINDENT
13553 "cindent",
13554#endif
13555#ifdef FEAT_CLIENTSERVER
13556 "clientserver",
13557#endif
13558#ifdef FEAT_CLIPBOARD
13559 "clipboard",
13560#endif
13561#ifdef FEAT_CMDL_COMPL
13562 "cmdline_compl",
13563#endif
13564#ifdef FEAT_CMDHIST
13565 "cmdline_hist",
13566#endif
13567#ifdef FEAT_COMMENTS
13568 "comments",
13569#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013570#ifdef FEAT_CONCEAL
13571 "conceal",
13572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573#ifdef FEAT_CRYPT
13574 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013575 "crypt-blowfish",
13576 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577#endif
13578#ifdef FEAT_CSCOPE
13579 "cscope",
13580#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013581#ifdef FEAT_CURSORBIND
13582 "cursorbind",
13583#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013584#ifdef CURSOR_SHAPE
13585 "cursorshape",
13586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587#ifdef DEBUG
13588 "debug",
13589#endif
13590#ifdef FEAT_CON_DIALOG
13591 "dialog_con",
13592#endif
13593#ifdef FEAT_GUI_DIALOG
13594 "dialog_gui",
13595#endif
13596#ifdef FEAT_DIFF
13597 "diff",
13598#endif
13599#ifdef FEAT_DIGRAPHS
13600 "digraphs",
13601#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013602#ifdef FEAT_DIRECTX
13603 "directx",
13604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605#ifdef FEAT_DND
13606 "dnd",
13607#endif
13608#ifdef FEAT_EMACS_TAGS
13609 "emacs_tags",
13610#endif
13611 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013612 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613#ifdef FEAT_SEARCH_EXTRA
13614 "extra_search",
13615#endif
13616#ifdef FEAT_FKMAP
13617 "farsi",
13618#endif
13619#ifdef FEAT_SEARCHPATH
13620 "file_in_path",
13621#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013622#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013623 "filterpipe",
13624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625#ifdef FEAT_FIND_ID
13626 "find_in_path",
13627#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013628#ifdef FEAT_FLOAT
13629 "float",
13630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631#ifdef FEAT_FOLDING
13632 "folding",
13633#endif
13634#ifdef FEAT_FOOTER
13635 "footer",
13636#endif
13637#if !defined(USE_SYSTEM) && defined(UNIX)
13638 "fork",
13639#endif
13640#ifdef FEAT_GETTEXT
13641 "gettext",
13642#endif
13643#ifdef FEAT_GUI
13644 "gui",
13645#endif
13646#ifdef FEAT_GUI_ATHENA
13647# ifdef FEAT_GUI_NEXTAW
13648 "gui_neXtaw",
13649# else
13650 "gui_athena",
13651# endif
13652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653#ifdef FEAT_GUI_GTK
13654 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013655# ifdef USE_GTK3
13656 "gui_gtk3",
13657# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013661#ifdef FEAT_GUI_GNOME
13662 "gui_gnome",
13663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664#ifdef FEAT_GUI_MAC
13665 "gui_mac",
13666#endif
13667#ifdef FEAT_GUI_MOTIF
13668 "gui_motif",
13669#endif
13670#ifdef FEAT_GUI_PHOTON
13671 "gui_photon",
13672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673#ifdef FEAT_GUI_W32
13674 "gui_win32",
13675#endif
13676#ifdef FEAT_HANGULIN
13677 "hangul_input",
13678#endif
13679#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13680 "iconv",
13681#endif
13682#ifdef FEAT_INS_EXPAND
13683 "insert_expand",
13684#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013685#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013686 "job",
13687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688#ifdef FEAT_JUMPLIST
13689 "jumplist",
13690#endif
13691#ifdef FEAT_KEYMAP
13692 "keymap",
13693#endif
13694#ifdef FEAT_LANGMAP
13695 "langmap",
13696#endif
13697#ifdef FEAT_LIBCALL
13698 "libcall",
13699#endif
13700#ifdef FEAT_LINEBREAK
13701 "linebreak",
13702#endif
13703#ifdef FEAT_LISP
13704 "lispindent",
13705#endif
13706#ifdef FEAT_LISTCMDS
13707 "listcmds",
13708#endif
13709#ifdef FEAT_LOCALMAP
13710 "localmap",
13711#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013712#ifdef FEAT_LUA
13713# ifndef DYNAMIC_LUA
13714 "lua",
13715# endif
13716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717#ifdef FEAT_MENU
13718 "menu",
13719#endif
13720#ifdef FEAT_SESSION
13721 "mksession",
13722#endif
13723#ifdef FEAT_MODIFY_FNAME
13724 "modify_fname",
13725#endif
13726#ifdef FEAT_MOUSE
13727 "mouse",
13728#endif
13729#ifdef FEAT_MOUSESHAPE
13730 "mouseshape",
13731#endif
13732#if defined(UNIX) || defined(VMS)
13733# ifdef FEAT_MOUSE_DEC
13734 "mouse_dec",
13735# endif
13736# ifdef FEAT_MOUSE_GPM
13737 "mouse_gpm",
13738# endif
13739# ifdef FEAT_MOUSE_JSB
13740 "mouse_jsbterm",
13741# endif
13742# ifdef FEAT_MOUSE_NET
13743 "mouse_netterm",
13744# endif
13745# ifdef FEAT_MOUSE_PTERM
13746 "mouse_pterm",
13747# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013748# ifdef FEAT_MOUSE_SGR
13749 "mouse_sgr",
13750# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013751# ifdef FEAT_SYSMOUSE
13752 "mouse_sysmouse",
13753# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013754# ifdef FEAT_MOUSE_URXVT
13755 "mouse_urxvt",
13756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757# ifdef FEAT_MOUSE_XTERM
13758 "mouse_xterm",
13759# endif
13760#endif
13761#ifdef FEAT_MBYTE
13762 "multi_byte",
13763#endif
13764#ifdef FEAT_MBYTE_IME
13765 "multi_byte_ime",
13766#endif
13767#ifdef FEAT_MULTI_LANG
13768 "multi_lang",
13769#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013770#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013771#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013772 "mzscheme",
13773#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775#ifdef FEAT_OLE
13776 "ole",
13777#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013778 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779#ifdef FEAT_PATH_EXTRA
13780 "path_extra",
13781#endif
13782#ifdef FEAT_PERL
13783#ifndef DYNAMIC_PERL
13784 "perl",
13785#endif
13786#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013787#ifdef FEAT_PERSISTENT_UNDO
13788 "persistent_undo",
13789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790#ifdef FEAT_PYTHON
13791#ifndef DYNAMIC_PYTHON
13792 "python",
13793#endif
13794#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013795#ifdef FEAT_PYTHON3
13796#ifndef DYNAMIC_PYTHON3
13797 "python3",
13798#endif
13799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800#ifdef FEAT_POSTSCRIPT
13801 "postscript",
13802#endif
13803#ifdef FEAT_PRINTER
13804 "printer",
13805#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013806#ifdef FEAT_PROFILE
13807 "profile",
13808#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013809#ifdef FEAT_RELTIME
13810 "reltime",
13811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812#ifdef FEAT_QUICKFIX
13813 "quickfix",
13814#endif
13815#ifdef FEAT_RIGHTLEFT
13816 "rightleft",
13817#endif
13818#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13819 "ruby",
13820#endif
13821#ifdef FEAT_SCROLLBIND
13822 "scrollbind",
13823#endif
13824#ifdef FEAT_CMDL_INFO
13825 "showcmd",
13826 "cmdline_info",
13827#endif
13828#ifdef FEAT_SIGNS
13829 "signs",
13830#endif
13831#ifdef FEAT_SMARTINDENT
13832 "smartindent",
13833#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013834#ifdef STARTUPTIME
13835 "startuptime",
13836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837#ifdef FEAT_STL_OPT
13838 "statusline",
13839#endif
13840#ifdef FEAT_SUN_WORKSHOP
13841 "sun_workshop",
13842#endif
13843#ifdef FEAT_NETBEANS_INTG
13844 "netbeans_intg",
13845#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013846#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013847 "spell",
13848#endif
13849#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 "syntax",
13851#endif
13852#if defined(USE_SYSTEM) || !defined(UNIX)
13853 "system",
13854#endif
13855#ifdef FEAT_TAG_BINS
13856 "tag_binary",
13857#endif
13858#ifdef FEAT_TAG_OLDSTATIC
13859 "tag_old_static",
13860#endif
13861#ifdef FEAT_TAG_ANYWHITE
13862 "tag_any_white",
13863#endif
13864#ifdef FEAT_TCL
13865# ifndef DYNAMIC_TCL
13866 "tcl",
13867# endif
13868#endif
13869#ifdef TERMINFO
13870 "terminfo",
13871#endif
13872#ifdef FEAT_TERMRESPONSE
13873 "termresponse",
13874#endif
13875#ifdef FEAT_TEXTOBJ
13876 "textobjects",
13877#endif
13878#ifdef HAVE_TGETENT
13879 "tgetent",
13880#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013881#ifdef FEAT_TIMERS
13882 "timers",
13883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884#ifdef FEAT_TITLE
13885 "title",
13886#endif
13887#ifdef FEAT_TOOLBAR
13888 "toolbar",
13889#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013890#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13891 "unnamedplus",
13892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893#ifdef FEAT_USR_CMDS
13894 "user-commands", /* was accidentally included in 5.4 */
13895 "user_commands",
13896#endif
13897#ifdef FEAT_VIMINFO
13898 "viminfo",
13899#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013900#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 "vertsplit",
13902#endif
13903#ifdef FEAT_VIRTUALEDIT
13904 "virtualedit",
13905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907#ifdef FEAT_VISUALEXTRA
13908 "visualextra",
13909#endif
13910#ifdef FEAT_VREPLACE
13911 "vreplace",
13912#endif
13913#ifdef FEAT_WILDIGN
13914 "wildignore",
13915#endif
13916#ifdef FEAT_WILDMENU
13917 "wildmenu",
13918#endif
13919#ifdef FEAT_WINDOWS
13920 "windows",
13921#endif
13922#ifdef FEAT_WAK
13923 "winaltkeys",
13924#endif
13925#ifdef FEAT_WRITEBACKUP
13926 "writebackup",
13927#endif
13928#ifdef FEAT_XIM
13929 "xim",
13930#endif
13931#ifdef FEAT_XFONTSET
13932 "xfontset",
13933#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013934#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013935 "xpm",
13936 "xpm_w32", /* for backward compatibility */
13937#else
13938# if defined(HAVE_XPM)
13939 "xpm",
13940# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942#ifdef USE_XSMP
13943 "xsmp",
13944#endif
13945#ifdef USE_XSMP_INTERACT
13946 "xsmp_interact",
13947#endif
13948#ifdef FEAT_XCLIPBOARD
13949 "xterm_clipboard",
13950#endif
13951#ifdef FEAT_XTERM_SAVE
13952 "xterm_save",
13953#endif
13954#if defined(UNIX) && defined(FEAT_X11)
13955 "X11",
13956#endif
13957 NULL
13958 };
13959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 for (i = 0; has_list[i] != NULL; ++i)
13962 if (STRICMP(name, has_list[i]) == 0)
13963 {
13964 n = TRUE;
13965 break;
13966 }
13967
13968 if (n == FALSE)
13969 {
13970 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013971 {
13972 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010013973 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013974 && vim_isdigit(name[6])
13975 && vim_isdigit(name[8])
13976 && vim_isdigit(name[10]))
13977 {
13978 int major = atoi((char *)name + 6);
13979 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013980
13981 /* Expect "patch-9.9.01234". */
13982 n = (major < VIM_VERSION_MAJOR
13983 || (major == VIM_VERSION_MAJOR
13984 && (minor < VIM_VERSION_MINOR
13985 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013986 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013987 }
13988 else
13989 n = has_patch(atoi((char *)name + 5));
13990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991 else if (STRICMP(name, "vim_starting") == 0)
13992 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013993#ifdef FEAT_MBYTE
13994 else if (STRICMP(name, "multi_byte_encoding") == 0)
13995 n = has_mbyte;
13996#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013997#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13998 else if (STRICMP(name, "balloon_multiline") == 0)
13999 n = multiline_balloon_available();
14000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001#ifdef DYNAMIC_TCL
14002 else if (STRICMP(name, "tcl") == 0)
14003 n = tcl_enabled(FALSE);
14004#endif
14005#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14006 else if (STRICMP(name, "iconv") == 0)
14007 n = iconv_enabled(FALSE);
14008#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014009#ifdef DYNAMIC_LUA
14010 else if (STRICMP(name, "lua") == 0)
14011 n = lua_enabled(FALSE);
14012#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014013#ifdef DYNAMIC_MZSCHEME
14014 else if (STRICMP(name, "mzscheme") == 0)
14015 n = mzscheme_enabled(FALSE);
14016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017#ifdef DYNAMIC_RUBY
14018 else if (STRICMP(name, "ruby") == 0)
14019 n = ruby_enabled(FALSE);
14020#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014021#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022#ifdef DYNAMIC_PYTHON
14023 else if (STRICMP(name, "python") == 0)
14024 n = python_enabled(FALSE);
14025#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014026#endif
14027#ifdef FEAT_PYTHON3
14028#ifdef DYNAMIC_PYTHON3
14029 else if (STRICMP(name, "python3") == 0)
14030 n = python3_enabled(FALSE);
14031#endif
14032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033#ifdef DYNAMIC_PERL
14034 else if (STRICMP(name, "perl") == 0)
14035 n = perl_enabled(FALSE);
14036#endif
14037#ifdef FEAT_GUI
14038 else if (STRICMP(name, "gui_running") == 0)
14039 n = (gui.in_use || gui.starting);
14040# ifdef FEAT_GUI_W32
14041 else if (STRICMP(name, "gui_win32s") == 0)
14042 n = gui_is_win32s();
14043# endif
14044# ifdef FEAT_BROWSE
14045 else if (STRICMP(name, "browse") == 0)
14046 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14047# endif
14048#endif
14049#ifdef FEAT_SYN_HL
14050 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014051 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052#endif
14053#if defined(WIN3264)
14054 else if (STRICMP(name, "win95") == 0)
14055 n = mch_windows95();
14056#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014057#ifdef FEAT_NETBEANS_INTG
14058 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014059 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 }
14062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014063 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064}
14065
14066/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014067 * "has_key()" function
14068 */
14069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014070f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014071{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014072 if (argvars[0].v_type != VAR_DICT)
14073 {
14074 EMSG(_(e_dictreq));
14075 return;
14076 }
14077 if (argvars[0].vval.v_dict == NULL)
14078 return;
14079
14080 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014081 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014082}
14083
14084/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014085 * "haslocaldir()" function
14086 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014088f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014089{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014090 win_T *wp = NULL;
14091
14092 wp = find_tabwin(&argvars[0], &argvars[1]);
14093 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014094}
14095
14096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 * "hasmapto()" function
14098 */
14099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014100f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101{
14102 char_u *name;
14103 char_u *mode;
14104 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014105 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014108 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 mode = (char_u *)"nvo";
14110 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014112 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014113 if (argvars[2].v_type != VAR_UNKNOWN)
14114 abbr = get_tv_number(&argvars[2]);
14115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116
Bram Moolenaar2c932302006-03-18 21:42:09 +000014117 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014118 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014120 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121}
14122
14123/*
14124 * "histadd()" function
14125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014127f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128{
14129#ifdef FEAT_CMDHIST
14130 int histype;
14131 char_u *str;
14132 char_u buf[NUMBUFLEN];
14133#endif
14134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014135 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 if (check_restricted() || check_secure())
14137 return;
14138#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014139 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14140 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 if (histype >= 0)
14142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014143 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 if (*str != NUL)
14145 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014146 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014148 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 return;
14150 }
14151 }
14152#endif
14153}
14154
14155/*
14156 * "histdel()" function
14157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014159f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160{
14161#ifdef FEAT_CMDHIST
14162 int n;
14163 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14167 if (str == NULL)
14168 n = 0;
14169 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014172 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 else
14177 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 get_tv_string_buf(&argvars[1], buf));
14180 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181#endif
14182}
14183
14184/*
14185 * "histget()" function
14186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014188f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189{
14190#ifdef FEAT_CMDHIST
14191 int type;
14192 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14196 if (str == NULL)
14197 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 {
14200 type = get_histtype(str);
14201 if (argvars[1].v_type == VAR_UNKNOWN)
14202 idx = get_history_idx(type);
14203 else
14204 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14205 /* -1 on type error */
14206 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014209 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014211 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212}
14213
14214/*
14215 * "histnr()" function
14216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014218f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219{
14220 int i;
14221
14222#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 char_u *history = get_tv_string_chk(&argvars[0]);
14224
14225 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 if (i >= HIST_CMD && i < HIST_COUNT)
14227 i = get_history_idx(i);
14228 else
14229#endif
14230 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014231 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232}
14233
14234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 * "highlightID(name)" function
14236 */
14237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014238f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241}
14242
14243/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244 * "highlight_exists()" function
14245 */
14246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014247f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014248{
14249 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14250}
14251
14252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 * "hostname()" function
14254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014256f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257{
14258 char_u hostname[256];
14259
14260 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014261 rettv->v_type = VAR_STRING;
14262 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263}
14264
14265/*
14266 * iconv() function
14267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014269f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270{
14271#ifdef FEAT_MBYTE
14272 char_u buf1[NUMBUFLEN];
14273 char_u buf2[NUMBUFLEN];
14274 char_u *from, *to, *str;
14275 vimconv_T vimconv;
14276#endif
14277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014278 rettv->v_type = VAR_STRING;
14279 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280
14281#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014282 str = get_tv_string(&argvars[0]);
14283 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14284 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 vimconv.vc_type = CONV_NONE;
14286 convert_setup(&vimconv, from, to);
14287
14288 /* If the encodings are equal, no conversion needed. */
14289 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014292 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293
14294 convert_setup(&vimconv, NULL, NULL);
14295 vim_free(from);
14296 vim_free(to);
14297#endif
14298}
14299
14300/*
14301 * "indent()" function
14302 */
14303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014304f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305{
14306 linenr_T lnum;
14307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014310 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014312 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313}
14314
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014315/*
14316 * "index()" function
14317 */
14318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014319f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014320{
Bram Moolenaar33570922005-01-25 22:26:29 +000014321 list_T *l;
14322 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014323 long idx = 0;
14324 int ic = FALSE;
14325
14326 rettv->vval.v_number = -1;
14327 if (argvars[0].v_type != VAR_LIST)
14328 {
14329 EMSG(_(e_listreq));
14330 return;
14331 }
14332 l = argvars[0].vval.v_list;
14333 if (l != NULL)
14334 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014335 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014336 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 int error = FALSE;
14339
Bram Moolenaar758711c2005-02-02 23:11:38 +000014340 /* Start at specified item. Use the cached index that list_find()
14341 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014343 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014344 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014345 ic = get_tv_number_chk(&argvars[3], &error);
14346 if (error)
14347 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014348 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014349
Bram Moolenaar758711c2005-02-02 23:11:38 +000014350 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014351 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014352 {
14353 rettv->vval.v_number = idx;
14354 break;
14355 }
14356 }
14357}
14358
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359static int inputsecret_flag = 0;
14360
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014361static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014362
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014364 * This function is used by f_input() and f_inputdialog() functions. The third
14365 * argument to f_input() specifies the type of completion to use at the
14366 * prompt. The third argument to f_inputdialog() specifies the value to return
14367 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 */
14369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014370get_user_input(
14371 typval_T *argvars,
14372 typval_T *rettv,
14373 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 char_u *p = NULL;
14377 int c;
14378 char_u buf[NUMBUFLEN];
14379 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014381 int xp_type = EXPAND_NOTHING;
14382 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014384 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014385 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386
14387#ifdef NO_CONSOLE_INPUT
14388 /* While starting up, there is no place to enter text. */
14389 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391#endif
14392
14393 cmd_silent = FALSE; /* Want to see the prompt. */
14394 if (prompt != NULL)
14395 {
14396 /* Only the part of the message after the last NL is considered as
14397 * prompt for the command line */
14398 p = vim_strrchr(prompt, '\n');
14399 if (p == NULL)
14400 p = prompt;
14401 else
14402 {
14403 ++p;
14404 c = *p;
14405 *p = NUL;
14406 msg_start();
14407 msg_clr_eos();
14408 msg_puts_attr(prompt, echo_attr);
14409 msg_didout = FALSE;
14410 msg_starthere();
14411 *p = c;
14412 }
14413 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 if (argvars[1].v_type != VAR_UNKNOWN)
14416 {
14417 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14418 if (defstr != NULL)
14419 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014421 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014422 {
14423 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014424 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014425 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014426
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014427 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014428 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014429
Bram Moolenaar4463f292005-09-25 22:20:24 +000014430 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14431 if (xp_name == NULL)
14432 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014433
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014434 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014435
Bram Moolenaar4463f292005-09-25 22:20:24 +000014436 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14437 &xp_arg) == FAIL)
14438 return;
14439 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014440 }
14441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014442 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014443 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014444 int save_ex_normal_busy = ex_normal_busy;
14445 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014447 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14448 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014449 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014450 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014451 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014452 && argvars[1].v_type != VAR_UNKNOWN
14453 && argvars[2].v_type != VAR_UNKNOWN)
14454 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14455 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014456
14457 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014459 /* since the user typed this, no need to wait for return */
14460 need_wait_return = FALSE;
14461 msg_didout = FALSE;
14462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 cmd_silent = cmd_silent_save;
14464}
14465
14466/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014467 * "input()" function
14468 * Also handles inputsecret() when inputsecret is set.
14469 */
14470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014471f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014472{
14473 get_user_input(argvars, rettv, FALSE);
14474}
14475
14476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 * "inputdialog()" function
14478 */
14479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014480f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481{
14482#if defined(FEAT_GUI_TEXTDIALOG)
14483 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14484 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14485 {
14486 char_u *message;
14487 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014488 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014490 message = get_tv_string_chk(&argvars[0]);
14491 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014492 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014493 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 else
14495 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 if (message != NULL && defstr != NULL
14497 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014498 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 else
14501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014502 if (message != NULL && defstr != NULL
14503 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014504 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014505 rettv->vval.v_string = vim_strsave(
14506 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014510 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 }
14512 else
14513#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014514 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515}
14516
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014517/*
14518 * "inputlist()" function
14519 */
14520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014521f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014522{
14523 listitem_T *li;
14524 int selected;
14525 int mouse_used;
14526
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014527#ifdef NO_CONSOLE_INPUT
14528 /* While starting up, there is no place to enter text. */
14529 if (no_console_input())
14530 return;
14531#endif
14532 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14533 {
14534 EMSG2(_(e_listarg), "inputlist()");
14535 return;
14536 }
14537
14538 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014539 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014540 lines_left = Rows; /* avoid more prompt */
14541 msg_scroll = TRUE;
14542 msg_clr_eos();
14543
14544 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14545 {
14546 msg_puts(get_tv_string(&li->li_tv));
14547 msg_putchar('\n');
14548 }
14549
14550 /* Ask for choice. */
14551 selected = prompt_for_number(&mouse_used);
14552 if (mouse_used)
14553 selected -= lines_left;
14554
14555 rettv->vval.v_number = selected;
14556}
14557
14558
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14560
14561/*
14562 * "inputrestore()" function
14563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014565f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566{
14567 if (ga_userinput.ga_len > 0)
14568 {
14569 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14571 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014572 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 }
14574 else if (p_verbose > 1)
14575 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014576 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014577 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 }
14579}
14580
14581/*
14582 * "inputsave()" function
14583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014585f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014587 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 if (ga_grow(&ga_userinput, 1) == OK)
14589 {
14590 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14591 + ga_userinput.ga_len);
14592 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014593 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 }
14595 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014596 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597}
14598
14599/*
14600 * "inputsecret()" function
14601 */
14602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014603f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604{
14605 ++cmdline_star;
14606 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 --cmdline_star;
14609 --inputsecret_flag;
14610}
14611
14612/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014613 * "insert()" function
14614 */
14615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014616f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014617{
14618 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014619 listitem_T *item;
14620 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014622
14623 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014624 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014625 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014626 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014627 {
14628 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 before = get_tv_number_chk(&argvars[2], &error);
14630 if (error)
14631 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014632
Bram Moolenaar758711c2005-02-02 23:11:38 +000014633 if (before == l->lv_len)
14634 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014635 else
14636 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014637 item = list_find(l, before);
14638 if (item == NULL)
14639 {
14640 EMSGN(_(e_listidx), before);
14641 l = NULL;
14642 }
14643 }
14644 if (l != NULL)
14645 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014646 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014647 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014648 }
14649 }
14650}
14651
14652/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014653 * "invert(expr)" function
14654 */
14655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014656f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014657{
14658 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14659}
14660
14661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662 * "isdirectory()" function
14663 */
14664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014665f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014667 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668}
14669
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014670/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014671 * "islocked()" function
14672 */
14673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014674f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014675{
14676 lval_T lv;
14677 char_u *end;
14678 dictitem_T *di;
14679
14680 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014681 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14682 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014683 if (end != NULL && lv.ll_name != NULL)
14684 {
14685 if (*end != NUL)
14686 EMSG(_(e_trailing));
14687 else
14688 {
14689 if (lv.ll_tv == NULL)
14690 {
14691 if (check_changedtick(lv.ll_name))
14692 rettv->vval.v_number = 1; /* always locked */
14693 else
14694 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014695 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014696 if (di != NULL)
14697 {
14698 /* Consider a variable locked when:
14699 * 1. the variable itself is locked
14700 * 2. the value of the variable is locked.
14701 * 3. the List or Dict value is locked.
14702 */
14703 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14704 || tv_islocked(&di->di_tv));
14705 }
14706 }
14707 }
14708 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014709 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014710 else if (lv.ll_newkey != NULL)
14711 EMSG2(_(e_dictkey), lv.ll_newkey);
14712 else if (lv.ll_list != NULL)
14713 /* List item. */
14714 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14715 else
14716 /* Dictionary item. */
14717 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14718 }
14719 }
14720
14721 clear_lval(&lv);
14722}
14723
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014724#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14725/*
14726 * "isnan()" function
14727 */
14728 static void
14729f_isnan(typval_T *argvars, typval_T *rettv)
14730{
14731 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14732 && isnan(argvars[0].vval.v_float);
14733}
14734#endif
14735
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014736static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014737
14738/*
14739 * Turn a dict into a list:
14740 * "what" == 0: list of keys
14741 * "what" == 1: list of values
14742 * "what" == 2: list of items
14743 */
14744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014745dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014746{
Bram Moolenaar33570922005-01-25 22:26:29 +000014747 list_T *l2;
14748 dictitem_T *di;
14749 hashitem_T *hi;
14750 listitem_T *li;
14751 listitem_T *li2;
14752 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014753 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014754
Bram Moolenaar8c711452005-01-14 21:53:12 +000014755 if (argvars[0].v_type != VAR_DICT)
14756 {
14757 EMSG(_(e_dictreq));
14758 return;
14759 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014760 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014761 return;
14762
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014763 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014764 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014765
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014766 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014767 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014768 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014769 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014770 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014771 --todo;
14772 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014773
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014774 li = listitem_alloc();
14775 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014776 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014777 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014778
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014779 if (what == 0)
14780 {
14781 /* keys() */
14782 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014783 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014784 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14785 }
14786 else if (what == 1)
14787 {
14788 /* values() */
14789 copy_tv(&di->di_tv, &li->li_tv);
14790 }
14791 else
14792 {
14793 /* items() */
14794 l2 = list_alloc();
14795 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014796 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014797 li->li_tv.vval.v_list = l2;
14798 if (l2 == NULL)
14799 break;
14800 ++l2->lv_refcount;
14801
14802 li2 = listitem_alloc();
14803 if (li2 == NULL)
14804 break;
14805 list_append(l2, li2);
14806 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014807 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014808 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14809
14810 li2 = listitem_alloc();
14811 if (li2 == NULL)
14812 break;
14813 list_append(l2, li2);
14814 copy_tv(&di->di_tv, &li2->li_tv);
14815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014816 }
14817 }
14818}
14819
14820/*
14821 * "items(dict)" function
14822 */
14823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014824f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014825{
14826 dict_list(argvars, rettv, 2);
14827}
14828
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014829#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014830/*
14831 * Get the job from the argument.
14832 * Returns NULL if the job is invalid.
14833 */
14834 static job_T *
14835get_job_arg(typval_T *tv)
14836{
14837 job_T *job;
14838
14839 if (tv->v_type != VAR_JOB)
14840 {
14841 EMSG2(_(e_invarg2), get_tv_string(tv));
14842 return NULL;
14843 }
14844 job = tv->vval.v_job;
14845
14846 if (job == NULL)
14847 EMSG(_("E916: not a valid job"));
14848 return job;
14849}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014850
Bram Moolenaar835dc632016-02-07 14:27:38 +010014851/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014852 * "job_getchannel()" function
14853 */
14854 static void
14855f_job_getchannel(typval_T *argvars, typval_T *rettv)
14856{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014857 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014858
Bram Moolenaar65edff82016-02-21 16:40:11 +010014859 if (job != NULL)
14860 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014861 rettv->v_type = VAR_CHANNEL;
14862 rettv->vval.v_channel = job->jv_channel;
14863 if (job->jv_channel != NULL)
14864 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014865 }
14866}
14867
14868/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014869 * "job_info()" function
14870 */
14871 static void
14872f_job_info(typval_T *argvars, typval_T *rettv)
14873{
14874 job_T *job = get_job_arg(&argvars[0]);
14875
14876 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14877 job_info(job, rettv->vval.v_dict);
14878}
14879
14880/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014881 * "job_setoptions()" function
14882 */
14883 static void
14884f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14885{
14886 job_T *job = get_job_arg(&argvars[0]);
14887 jobopt_T opt;
14888
14889 if (job == NULL)
14890 return;
14891 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014892 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014893 return;
14894 job_set_options(job, &opt);
14895}
14896
14897/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014898 * "job_start()" function
14899 */
14900 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014901f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014902{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014903 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014904 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014905}
14906
14907/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014908 * "job_status()" function
14909 */
14910 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014911f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014912{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014913 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014914
Bram Moolenaar65edff82016-02-21 16:40:11 +010014915 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014916 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014917 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014918 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014919 }
14920}
14921
14922/*
14923 * "job_stop()" function
14924 */
14925 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014926f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014927{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014928 job_T *job = get_job_arg(&argvars[0]);
14929
14930 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014931 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014932}
14933#endif
14934
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014936 * "join()" function
14937 */
14938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014939f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014940{
14941 garray_T ga;
14942 char_u *sep;
14943
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014944 if (argvars[0].v_type != VAR_LIST)
14945 {
14946 EMSG(_(e_listreq));
14947 return;
14948 }
14949 if (argvars[0].vval.v_list == NULL)
14950 return;
14951 if (argvars[1].v_type == VAR_UNKNOWN)
14952 sep = (char_u *)" ";
14953 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014954 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014955
14956 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014957
14958 if (sep != NULL)
14959 {
14960 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014961 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014962 ga_append(&ga, NUL);
14963 rettv->vval.v_string = (char_u *)ga.ga_data;
14964 }
14965 else
14966 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014967}
14968
14969/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014970 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014971 */
14972 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014973f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014974{
14975 js_read_T reader;
14976
14977 reader.js_buf = get_tv_string(&argvars[0]);
14978 reader.js_fill = NULL;
14979 reader.js_used = 0;
14980 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14981 EMSG(_(e_invarg));
14982}
14983
14984/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014985 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014986 */
14987 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014988f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014989{
14990 rettv->v_type = VAR_STRING;
14991 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14992}
14993
14994/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014995 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014996 */
14997 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014998f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014999{
15000 js_read_T reader;
15001
15002 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015003 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015004 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015005 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015006 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015007}
15008
15009/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015010 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015011 */
15012 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015013f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015014{
15015 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015016 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015017}
15018
15019/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015020 * "keys()" function
15021 */
15022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015023f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015024{
15025 dict_list(argvars, rettv, 0);
15026}
15027
15028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 * "last_buffer_nr()" function.
15030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015032f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033{
15034 int n = 0;
15035 buf_T *buf;
15036
15037 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15038 if (n < buf->b_fnum)
15039 n = buf->b_fnum;
15040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015041 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015042}
15043
15044/*
15045 * "len()" function
15046 */
15047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015048f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015049{
15050 switch (argvars[0].v_type)
15051 {
15052 case VAR_STRING:
15053 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015054 rettv->vval.v_number = (varnumber_T)STRLEN(
15055 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015056 break;
15057 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015058 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015059 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015060 case VAR_DICT:
15061 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15062 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015063 case VAR_UNKNOWN:
15064 case VAR_SPECIAL:
15065 case VAR_FLOAT:
15066 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015067 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015068 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015069 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015070 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015071 break;
15072 }
15073}
15074
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015075static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015076
15077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015078libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015079{
15080#ifdef FEAT_LIBCALL
15081 char_u *string_in;
15082 char_u **string_result;
15083 int nr_result;
15084#endif
15085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015086 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015087 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015088 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015089
15090 if (check_restricted() || check_secure())
15091 return;
15092
15093#ifdef FEAT_LIBCALL
15094 /* The first two args must be strings, otherwise its meaningless */
15095 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15096 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015097 string_in = NULL;
15098 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015099 string_in = argvars[2].vval.v_string;
15100 if (type == VAR_NUMBER)
15101 string_result = NULL;
15102 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015103 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015104 if (mch_libcall(argvars[0].vval.v_string,
15105 argvars[1].vval.v_string,
15106 string_in,
15107 argvars[2].vval.v_number,
15108 string_result,
15109 &nr_result) == OK
15110 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015111 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015112 }
15113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114}
15115
15116/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117 * "libcall()" function
15118 */
15119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015120f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121{
15122 libcall_common(argvars, rettv, VAR_STRING);
15123}
15124
15125/*
15126 * "libcallnr()" function
15127 */
15128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015129f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015130{
15131 libcall_common(argvars, rettv, VAR_NUMBER);
15132}
15133
15134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135 * "line(string)" function
15136 */
15137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015138f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139{
15140 linenr_T lnum = 0;
15141 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015142 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015144 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 if (fp != NULL)
15146 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015147 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148}
15149
15150/*
15151 * "line2byte(lnum)" function
15152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015154f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155{
15156#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015157 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158#else
15159 linenr_T lnum;
15160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015161 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15166 if (rettv->vval.v_number >= 0)
15167 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168#endif
15169}
15170
15171/*
15172 * "lispindent(lnum)" function
15173 */
15174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015175f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176{
15177#ifdef FEAT_LISP
15178 pos_T pos;
15179 linenr_T lnum;
15180
15181 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015182 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15184 {
15185 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015186 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 curwin->w_cursor = pos;
15188 }
15189 else
15190#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015191 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192}
15193
15194/*
15195 * "localtime()" function
15196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015198f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015200 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201}
15202
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015203static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204
15205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015206get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207{
15208 char_u *keys;
15209 char_u *which;
15210 char_u buf[NUMBUFLEN];
15211 char_u *keys_buf = NULL;
15212 char_u *rhs;
15213 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015214 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015215 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015216 mapblock_T *mp;
15217 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218
15219 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015220 rettv->v_type = VAR_STRING;
15221 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015223 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015224 if (*keys == NUL)
15225 return;
15226
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015227 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015230 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015231 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015232 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015233 if (argvars[3].v_type != VAR_UNKNOWN)
15234 get_dict = get_tv_number(&argvars[3]);
15235 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237 else
15238 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015239 if (which == NULL)
15240 return;
15241
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242 mode = get_map_mode(&which, 0);
15243
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015244 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015245 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015247
15248 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015250 /* Return a string. */
15251 if (rhs != NULL)
15252 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253
Bram Moolenaarbd743252010-10-20 21:23:33 +020015254 }
15255 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15256 {
15257 /* Return a dictionary. */
15258 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15259 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15260 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261
Bram Moolenaarbd743252010-10-20 21:23:33 +020015262 dict_add_nr_str(dict, "lhs", 0L, lhs);
15263 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15264 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15265 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15266 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15267 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15268 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015269 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015270 dict_add_nr_str(dict, "mode", 0L, mapmode);
15271
15272 vim_free(lhs);
15273 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 }
15275}
15276
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015277#ifdef FEAT_FLOAT
15278/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015279 * "log()" function
15280 */
15281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015282f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015283{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015284 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015285
15286 rettv->v_type = VAR_FLOAT;
15287 if (get_float_arg(argvars, &f) == OK)
15288 rettv->vval.v_float = log(f);
15289 else
15290 rettv->vval.v_float = 0.0;
15291}
15292
15293/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015294 * "log10()" function
15295 */
15296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015297f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015298{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015299 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015300
15301 rettv->v_type = VAR_FLOAT;
15302 if (get_float_arg(argvars, &f) == OK)
15303 rettv->vval.v_float = log10(f);
15304 else
15305 rettv->vval.v_float = 0.0;
15306}
15307#endif
15308
Bram Moolenaar1dced572012-04-05 16:54:08 +020015309#ifdef FEAT_LUA
15310/*
15311 * "luaeval()" function
15312 */
15313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015314f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015315{
15316 char_u *str;
15317 char_u buf[NUMBUFLEN];
15318
15319 str = get_tv_string_buf(&argvars[0], buf);
15320 do_luaeval(str, argvars + 1, rettv);
15321}
15322#endif
15323
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015325 * "map()" function
15326 */
15327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015328f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015329{
15330 filter_map(argvars, rettv, TRUE);
15331}
15332
15333/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015334 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 */
15336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015337f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340}
15341
15342/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 */
15345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015346f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349}
15350
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015351static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352
15353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015354find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015356 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015357 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015358 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 char_u *pat;
15360 regmatch_T regmatch;
15361 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015362 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363 char_u *save_cpo;
15364 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015365 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015366 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015367 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015368 list_T *l = NULL;
15369 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015370 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015371 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372
15373 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15374 save_cpo = p_cpo;
15375 p_cpo = (char_u *)"";
15376
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015377 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015378 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015379 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015380 /* type 3: return empty list when there are no matches.
15381 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015382 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015383 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015384 if (type == 4
15385 && (list_append_string(rettv->vval.v_list,
15386 (char_u *)"", 0) == FAIL
15387 || list_append_number(rettv->vval.v_list,
15388 (varnumber_T)-1) == FAIL
15389 || list_append_number(rettv->vval.v_list,
15390 (varnumber_T)-1) == FAIL
15391 || list_append_number(rettv->vval.v_list,
15392 (varnumber_T)-1) == FAIL))
15393 {
15394 list_free(rettv->vval.v_list, TRUE);
15395 rettv->vval.v_list = NULL;
15396 goto theend;
15397 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015398 }
15399 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 rettv->v_type = VAR_STRING;
15402 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015405 if (argvars[0].v_type == VAR_LIST)
15406 {
15407 if ((l = argvars[0].vval.v_list) == NULL)
15408 goto theend;
15409 li = l->lv_first;
15410 }
15411 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015412 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015413 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015414 len = (long)STRLEN(str);
15415 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015417 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15418 if (pat == NULL)
15419 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015420
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015421 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015423 int error = FALSE;
15424
15425 start = get_tv_number_chk(&argvars[2], &error);
15426 if (error)
15427 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015428 if (l != NULL)
15429 {
15430 li = list_find(l, start);
15431 if (li == NULL)
15432 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015433 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015434 }
15435 else
15436 {
15437 if (start < 0)
15438 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015439 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015440 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015441 /* When "count" argument is there ignore matches before "start",
15442 * otherwise skip part of the string. Differs when pattern is "^"
15443 * or "\<". */
15444 if (argvars[3].v_type != VAR_UNKNOWN)
15445 startcol = start;
15446 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015447 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015448 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015449 len -= start;
15450 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015451 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015452
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015453 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015454 nth = get_tv_number_chk(&argvars[3], &error);
15455 if (error)
15456 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 }
15458
15459 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15460 if (regmatch.regprog != NULL)
15461 {
15462 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015463
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015464 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015465 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015466 if (l != NULL)
15467 {
15468 if (li == NULL)
15469 {
15470 match = FALSE;
15471 break;
15472 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015473 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015474 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015475 if (str == NULL)
15476 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015477 }
15478
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015479 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015480
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015481 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015482 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015483 if (l == NULL && !match)
15484 break;
15485
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015486 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015487 if (l != NULL)
15488 {
15489 li = li->li_next;
15490 ++idx;
15491 }
15492 else
15493 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015494#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015495 startcol = (colnr_T)(regmatch.startp[0]
15496 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015497#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015498 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015499#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015500 if (startcol > (colnr_T)len
15501 || str + startcol <= regmatch.startp[0])
15502 {
15503 match = FALSE;
15504 break;
15505 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015506 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015507 }
15508
15509 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015511 if (type == 4)
15512 {
15513 listitem_T *li1 = rettv->vval.v_list->lv_first;
15514 listitem_T *li2 = li1->li_next;
15515 listitem_T *li3 = li2->li_next;
15516 listitem_T *li4 = li3->li_next;
15517
15518 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15519 (int)(regmatch.endp[0] - regmatch.startp[0]));
15520 li3->li_tv.vval.v_number =
15521 (varnumber_T)(regmatch.startp[0] - expr);
15522 li4->li_tv.vval.v_number =
15523 (varnumber_T)(regmatch.endp[0] - expr);
15524 if (l != NULL)
15525 li2->li_tv.vval.v_number = (varnumber_T)idx;
15526 }
15527 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015528 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015529 int i;
15530
15531 /* return list with matched string and submatches */
15532 for (i = 0; i < NSUBEXP; ++i)
15533 {
15534 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015535 {
15536 if (list_append_string(rettv->vval.v_list,
15537 (char_u *)"", 0) == FAIL)
15538 break;
15539 }
15540 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015541 regmatch.startp[i],
15542 (int)(regmatch.endp[i] - regmatch.startp[i]))
15543 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015544 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015545 }
15546 }
15547 else if (type == 2)
15548 {
15549 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015550 if (l != NULL)
15551 copy_tv(&li->li_tv, rettv);
15552 else
15553 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015555 }
15556 else if (l != NULL)
15557 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 else
15559 {
15560 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015561 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 (varnumber_T)(regmatch.startp[0] - str);
15563 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015564 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015566 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 }
15568 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015569 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 }
15571
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015572 if (type == 4 && l == NULL)
15573 /* matchstrpos() without a list: drop the second item. */
15574 listitem_remove(rettv->vval.v_list,
15575 rettv->vval.v_list->lv_first->li_next);
15576
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015578 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 p_cpo = save_cpo;
15580}
15581
15582/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015583 * "match()" function
15584 */
15585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015586f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015587{
15588 find_some_match(argvars, rettv, 1);
15589}
15590
15591/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015592 * "matchadd()" function
15593 */
15594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015595f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015596{
15597#ifdef FEAT_SEARCH_EXTRA
15598 char_u buf[NUMBUFLEN];
15599 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15600 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15601 int prio = 10; /* default priority */
15602 int id = -1;
15603 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015604 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015605
15606 rettv->vval.v_number = -1;
15607
15608 if (grp == NULL || pat == NULL)
15609 return;
15610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015611 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015612 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015613 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015614 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015615 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015616 if (argvars[4].v_type != VAR_UNKNOWN)
15617 {
15618 if (argvars[4].v_type != VAR_DICT)
15619 {
15620 EMSG(_(e_dictreq));
15621 return;
15622 }
15623 if (dict_find(argvars[4].vval.v_dict,
15624 (char_u *)"conceal", -1) != NULL)
15625 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15626 (char_u *)"conceal", FALSE);
15627 }
15628 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015629 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015630 if (error == TRUE)
15631 return;
15632 if (id >= 1 && id <= 3)
15633 {
15634 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15635 return;
15636 }
15637
Bram Moolenaar6561d522015-07-21 15:48:27 +020015638 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15639 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015640#endif
15641}
15642
15643/*
15644 * "matchaddpos()" function
15645 */
15646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015647f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015648{
15649#ifdef FEAT_SEARCH_EXTRA
15650 char_u buf[NUMBUFLEN];
15651 char_u *group;
15652 int prio = 10;
15653 int id = -1;
15654 int error = FALSE;
15655 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015656 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015657
15658 rettv->vval.v_number = -1;
15659
15660 group = get_tv_string_buf_chk(&argvars[0], buf);
15661 if (group == NULL)
15662 return;
15663
15664 if (argvars[1].v_type != VAR_LIST)
15665 {
15666 EMSG2(_(e_listarg), "matchaddpos()");
15667 return;
15668 }
15669 l = argvars[1].vval.v_list;
15670 if (l == NULL)
15671 return;
15672
15673 if (argvars[2].v_type != VAR_UNKNOWN)
15674 {
15675 prio = get_tv_number_chk(&argvars[2], &error);
15676 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015677 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015678 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015679 if (argvars[4].v_type != VAR_UNKNOWN)
15680 {
15681 if (argvars[4].v_type != VAR_DICT)
15682 {
15683 EMSG(_(e_dictreq));
15684 return;
15685 }
15686 if (dict_find(argvars[4].vval.v_dict,
15687 (char_u *)"conceal", -1) != NULL)
15688 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15689 (char_u *)"conceal", FALSE);
15690 }
15691 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015692 }
15693 if (error == TRUE)
15694 return;
15695
15696 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15697 if (id == 1 || id == 2)
15698 {
15699 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15700 return;
15701 }
15702
Bram Moolenaar6561d522015-07-21 15:48:27 +020015703 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15704 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015705#endif
15706}
15707
15708/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015709 * "matcharg()" function
15710 */
15711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015712f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015713{
15714 if (rettv_list_alloc(rettv) == OK)
15715 {
15716#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015717 int id = get_tv_number(&argvars[0]);
15718 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015719
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015720 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015721 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015722 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15723 {
15724 list_append_string(rettv->vval.v_list,
15725 syn_id2name(m->hlg_id), -1);
15726 list_append_string(rettv->vval.v_list, m->pattern, -1);
15727 }
15728 else
15729 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015730 list_append_string(rettv->vval.v_list, NULL, -1);
15731 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015732 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015733 }
15734#endif
15735 }
15736}
15737
15738/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015739 * "matchdelete()" function
15740 */
15741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015742f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015743{
15744#ifdef FEAT_SEARCH_EXTRA
15745 rettv->vval.v_number = match_delete(curwin,
15746 (int)get_tv_number(&argvars[0]), TRUE);
15747#endif
15748}
15749
15750/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751 * "matchend()" function
15752 */
15753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015754f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755{
15756 find_some_match(argvars, rettv, 0);
15757}
15758
15759/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015760 * "matchlist()" function
15761 */
15762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015763f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015764{
15765 find_some_match(argvars, rettv, 3);
15766}
15767
15768/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015769 * "matchstr()" function
15770 */
15771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015772f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773{
15774 find_some_match(argvars, rettv, 2);
15775}
15776
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015777/*
15778 * "matchstrpos()" function
15779 */
15780 static void
15781f_matchstrpos(typval_T *argvars, typval_T *rettv)
15782{
15783 find_some_match(argvars, rettv, 4);
15784}
15785
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015786static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015787
15788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015789max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015790{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015791 long n = 0;
15792 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015793 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015794
15795 if (argvars[0].v_type == VAR_LIST)
15796 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015797 list_T *l;
15798 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015799
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015800 l = argvars[0].vval.v_list;
15801 if (l != NULL)
15802 {
15803 li = l->lv_first;
15804 if (li != NULL)
15805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015806 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015807 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015808 {
15809 li = li->li_next;
15810 if (li == NULL)
15811 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015812 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015813 if (domax ? i > n : i < n)
15814 n = i;
15815 }
15816 }
15817 }
15818 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015819 else if (argvars[0].v_type == VAR_DICT)
15820 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015821 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015822 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015823 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015824 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015825
15826 d = argvars[0].vval.v_dict;
15827 if (d != NULL)
15828 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015829 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015831 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015832 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015833 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015834 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015835 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015836 if (first)
15837 {
15838 n = i;
15839 first = FALSE;
15840 }
15841 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015842 n = i;
15843 }
15844 }
15845 }
15846 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015847 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015848 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015849 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015850}
15851
15852/*
15853 * "max()" function
15854 */
15855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015856f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015857{
15858 max_min(argvars, rettv, TRUE);
15859}
15860
15861/*
15862 * "min()" function
15863 */
15864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015865f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015866{
15867 max_min(argvars, rettv, FALSE);
15868}
15869
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015870static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015871
15872/*
15873 * Create the directory in which "dir" is located, and higher levels when
15874 * needed.
15875 */
15876 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015877mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015878{
15879 char_u *p;
15880 char_u *updir;
15881 int r = FAIL;
15882
15883 /* Get end of directory name in "dir".
15884 * We're done when it's "/" or "c:/". */
15885 p = gettail_sep(dir);
15886 if (p <= get_past_head(dir))
15887 return OK;
15888
15889 /* If the directory exists we're done. Otherwise: create it.*/
15890 updir = vim_strnsave(dir, (int)(p - dir));
15891 if (updir == NULL)
15892 return FAIL;
15893 if (mch_isdir(updir))
15894 r = OK;
15895 else if (mkdir_recurse(updir, prot) == OK)
15896 r = vim_mkdir_emsg(updir, prot);
15897 vim_free(updir);
15898 return r;
15899}
15900
15901#ifdef vim_mkdir
15902/*
15903 * "mkdir()" function
15904 */
15905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015906f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015907{
15908 char_u *dir;
15909 char_u buf[NUMBUFLEN];
15910 int prot = 0755;
15911
15912 rettv->vval.v_number = FAIL;
15913 if (check_restricted() || check_secure())
15914 return;
15915
15916 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015917 if (*dir == NUL)
15918 rettv->vval.v_number = FAIL;
15919 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015920 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015921 if (*gettail(dir) == NUL)
15922 /* remove trailing slashes */
15923 *gettail_sep(dir) = NUL;
15924
15925 if (argvars[1].v_type != VAR_UNKNOWN)
15926 {
15927 if (argvars[2].v_type != VAR_UNKNOWN)
15928 prot = get_tv_number_chk(&argvars[2], NULL);
15929 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15930 mkdir_recurse(dir, prot);
15931 }
15932 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015933 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015934}
15935#endif
15936
Bram Moolenaar0d660222005-01-07 21:51:51 +000015937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 * "mode()" function
15939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015941f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015943 char_u buf[3];
15944
15945 buf[1] = NUL;
15946 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 if (VIsual_active)
15949 {
15950 if (VIsual_select)
15951 buf[0] = VIsual_mode + 's' - 'v';
15952 else
15953 buf[0] = VIsual_mode;
15954 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015955 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015956 || State == CONFIRM)
15957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015959 if (State == ASKMORE)
15960 buf[1] = 'm';
15961 else if (State == CONFIRM)
15962 buf[1] = '?';
15963 }
15964 else if (State == EXTERNCMD)
15965 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 else if (State & INSERT)
15967 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015968#ifdef FEAT_VREPLACE
15969 if (State & VREPLACE_FLAG)
15970 {
15971 buf[0] = 'R';
15972 buf[1] = 'v';
15973 }
15974 else
15975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976 if (State & REPLACE_FLAG)
15977 buf[0] = 'R';
15978 else
15979 buf[0] = 'i';
15980 }
15981 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015984 if (exmode_active)
15985 buf[1] = 'v';
15986 }
15987 else if (exmode_active)
15988 {
15989 buf[0] = 'c';
15990 buf[1] = 'e';
15991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015995 if (finish_op)
15996 buf[1] = 'o';
15997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015999 /* Clear out the minor mode when the argument is not a non-zero number or
16000 * non-empty string. */
16001 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016002 buf[1] = NUL;
16003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016004 rettv->vval.v_string = vim_strsave(buf);
16005 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006}
16007
Bram Moolenaar429fa852013-04-15 12:27:36 +020016008#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016009/*
16010 * "mzeval()" function
16011 */
16012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016013f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016014{
16015 char_u *str;
16016 char_u buf[NUMBUFLEN];
16017
16018 str = get_tv_string_buf(&argvars[0], buf);
16019 do_mzeval(str, rettv);
16020}
Bram Moolenaar75676462013-01-30 14:55:42 +010016021
16022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016023mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016024{
16025 typval_T argvars[3];
16026
16027 argvars[0].v_type = VAR_STRING;
16028 argvars[0].vval.v_string = name;
16029 copy_tv(args, &argvars[1]);
16030 argvars[2].v_type = VAR_UNKNOWN;
16031 f_call(argvars, rettv);
16032 clear_tv(&argvars[1]);
16033}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016034#endif
16035
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016037 * "nextnonblank()" function
16038 */
16039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016040f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016041{
16042 linenr_T lnum;
16043
16044 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016046 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016047 {
16048 lnum = 0;
16049 break;
16050 }
16051 if (*skipwhite(ml_get(lnum)) != NUL)
16052 break;
16053 }
16054 rettv->vval.v_number = lnum;
16055}
16056
16057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 * "nr2char()" function
16059 */
16060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016061f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062{
16063 char_u buf[NUMBUFLEN];
16064
16065#ifdef FEAT_MBYTE
16066 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016067 {
16068 int utf8 = 0;
16069
16070 if (argvars[1].v_type != VAR_UNKNOWN)
16071 utf8 = get_tv_number_chk(&argvars[1], NULL);
16072 if (utf8)
16073 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16074 else
16075 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 else
16078#endif
16079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016080 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 buf[1] = NUL;
16082 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016083 rettv->v_type = VAR_STRING;
16084 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016085}
16086
16087/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016088 * "or(expr, expr)" function
16089 */
16090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016091f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016092{
16093 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16094 | get_tv_number_chk(&argvars[1], NULL);
16095}
16096
16097/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016098 * "pathshorten()" function
16099 */
16100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016101f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016102{
16103 char_u *p;
16104
16105 rettv->v_type = VAR_STRING;
16106 p = get_tv_string_chk(&argvars[0]);
16107 if (p == NULL)
16108 rettv->vval.v_string = NULL;
16109 else
16110 {
16111 p = vim_strsave(p);
16112 rettv->vval.v_string = p;
16113 if (p != NULL)
16114 shorten_dir(p);
16115 }
16116}
16117
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016118#ifdef FEAT_PERL
16119/*
16120 * "perleval()" function
16121 */
16122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016123f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016124{
16125 char_u *str;
16126 char_u buf[NUMBUFLEN];
16127
16128 str = get_tv_string_buf(&argvars[0], buf);
16129 do_perleval(str, rettv);
16130}
16131#endif
16132
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016133#ifdef FEAT_FLOAT
16134/*
16135 * "pow()" function
16136 */
16137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016138f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016139{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016140 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016141
16142 rettv->v_type = VAR_FLOAT;
16143 if (get_float_arg(argvars, &fx) == OK
16144 && get_float_arg(&argvars[1], &fy) == OK)
16145 rettv->vval.v_float = pow(fx, fy);
16146 else
16147 rettv->vval.v_float = 0.0;
16148}
16149#endif
16150
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016151/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016152 * "prevnonblank()" function
16153 */
16154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016155f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016156{
16157 linenr_T lnum;
16158
16159 lnum = get_tv_lnum(argvars);
16160 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16161 lnum = 0;
16162 else
16163 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16164 --lnum;
16165 rettv->vval.v_number = lnum;
16166}
16167
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016168/* This dummy va_list is here because:
16169 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16170 * - locally in the function results in a "used before set" warning
16171 * - using va_start() to initialize it gives "function with fixed args" error */
16172static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016173
Bram Moolenaar8c711452005-01-14 21:53:12 +000016174/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016175 * "printf()" function
16176 */
16177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016178f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016179{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016180 char_u buf[NUMBUFLEN];
16181 int len;
16182 char_u *s;
16183 int saved_did_emsg = did_emsg;
16184 char *fmt;
16185
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016186 rettv->v_type = VAR_STRING;
16187 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016188
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016189 /* Get the required length, allocate the buffer and do it for real. */
16190 did_emsg = FALSE;
16191 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16192 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16193 if (!did_emsg)
16194 {
16195 s = alloc(len + 1);
16196 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016197 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016198 rettv->vval.v_string = s;
16199 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016200 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016201 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016202 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016203}
16204
16205/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016206 * "pumvisible()" function
16207 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016209f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016210{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016211#ifdef FEAT_INS_EXPAND
16212 if (pum_visible())
16213 rettv->vval.v_number = 1;
16214#endif
16215}
16216
Bram Moolenaardb913952012-06-29 12:54:53 +020016217#ifdef FEAT_PYTHON3
16218/*
16219 * "py3eval()" function
16220 */
16221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016222f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016223{
16224 char_u *str;
16225 char_u buf[NUMBUFLEN];
16226
16227 str = get_tv_string_buf(&argvars[0], buf);
16228 do_py3eval(str, rettv);
16229}
16230#endif
16231
16232#ifdef FEAT_PYTHON
16233/*
16234 * "pyeval()" function
16235 */
16236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016237f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016238{
16239 char_u *str;
16240 char_u buf[NUMBUFLEN];
16241
16242 str = get_tv_string_buf(&argvars[0], buf);
16243 do_pyeval(str, rettv);
16244}
16245#endif
16246
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016247/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016248 * "range()" function
16249 */
16250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016252{
16253 long start;
16254 long end;
16255 long stride = 1;
16256 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016257 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016260 if (argvars[1].v_type == VAR_UNKNOWN)
16261 {
16262 end = start - 1;
16263 start = 0;
16264 }
16265 else
16266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016267 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016268 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016269 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016270 }
16271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016272 if (error)
16273 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016274 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016275 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016276 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016277 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016278 else
16279 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016280 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016281 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016282 if (list_append_number(rettv->vval.v_list,
16283 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016284 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016285 }
16286}
16287
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016288/*
16289 * "readfile()" function
16290 */
16291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016292f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016293{
16294 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016295 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016296 char_u *fname;
16297 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016298 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16299 int io_size = sizeof(buf);
16300 int readlen; /* size of last fread() */
16301 char_u *prev = NULL; /* previously read bytes, if any */
16302 long prevlen = 0; /* length of data in prev */
16303 long prevsize = 0; /* size of prev buffer */
16304 long maxline = MAXLNUM;
16305 long cnt = 0;
16306 char_u *p; /* position in buf */
16307 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016308
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016309 if (argvars[1].v_type != VAR_UNKNOWN)
16310 {
16311 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16312 binary = TRUE;
16313 if (argvars[2].v_type != VAR_UNKNOWN)
16314 maxline = get_tv_number(&argvars[2]);
16315 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016316
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016317 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016318 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016319
16320 /* Always open the file in binary mode, library functions have a mind of
16321 * their own about CR-LF conversion. */
16322 fname = get_tv_string(&argvars[0]);
16323 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16324 {
16325 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16326 return;
16327 }
16328
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016329 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016330 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016331 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016332
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016333 /* This for loop processes what was read, but is also entered at end
16334 * of file so that either:
16335 * - an incomplete line gets written
16336 * - a "binary" file gets an empty line at the end if it ends in a
16337 * newline. */
16338 for (p = buf, start = buf;
16339 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16340 ++p)
16341 {
16342 if (*p == '\n' || readlen <= 0)
16343 {
16344 listitem_T *li;
16345 char_u *s = NULL;
16346 long_u len = p - start;
16347
16348 /* Finished a line. Remove CRs before NL. */
16349 if (readlen > 0 && !binary)
16350 {
16351 while (len > 0 && start[len - 1] == '\r')
16352 --len;
16353 /* removal may cross back to the "prev" string */
16354 if (len == 0)
16355 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16356 --prevlen;
16357 }
16358 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016359 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016360 else
16361 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016362 /* Change "prev" buffer to be the right size. This way
16363 * the bytes are only copied once, and very long lines are
16364 * allocated only once. */
16365 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016366 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016367 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016368 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016369 prev = NULL; /* the list will own the string */
16370 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016371 }
16372 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016373 if (s == NULL)
16374 {
16375 do_outofmem_msg((long_u) prevlen + len + 1);
16376 failed = TRUE;
16377 break;
16378 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016379
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016380 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016381 {
16382 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016383 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016384 break;
16385 }
16386 li->li_tv.v_type = VAR_STRING;
16387 li->li_tv.v_lock = 0;
16388 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016389 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016390
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016391 start = p + 1; /* step over newline */
16392 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016393 break;
16394 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016395 else if (*p == NUL)
16396 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016397#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016398 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16399 * when finding the BF and check the previous two bytes. */
16400 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016401 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016402 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16403 * + 1, these may be in the "prev" string. */
16404 char_u back1 = p >= buf + 1 ? p[-1]
16405 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16406 char_u back2 = p >= buf + 2 ? p[-2]
16407 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16408 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016409
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016410 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016411 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016412 char_u *dest = p - 2;
16413
16414 /* Usually a BOM is at the beginning of a file, and so at
16415 * the beginning of a line; then we can just step over it.
16416 */
16417 if (start == dest)
16418 start = p + 1;
16419 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016420 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016421 /* have to shuffle buf to close gap */
16422 int adjust_prevlen = 0;
16423
16424 if (dest < buf)
16425 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016426 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016427 dest = buf;
16428 }
16429 if (readlen > p - buf + 1)
16430 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16431 readlen -= 3 - adjust_prevlen;
16432 prevlen -= adjust_prevlen;
16433 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016434 }
16435 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016436 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016437#endif
16438 } /* for */
16439
16440 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16441 break;
16442 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016443 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016444 /* There's part of a line in buf, store it in "prev". */
16445 if (p - start + prevlen >= prevsize)
16446 {
16447 /* need bigger "prev" buffer */
16448 char_u *newprev;
16449
16450 /* A common use case is ordinary text files and "prev" gets a
16451 * fragment of a line, so the first allocation is made
16452 * small, to avoid repeatedly 'allocing' large and
16453 * 'reallocing' small. */
16454 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016455 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016456 else
16457 {
16458 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016459 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016460 prevsize = grow50pc > growmin ? grow50pc : growmin;
16461 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016462 newprev = prev == NULL ? alloc(prevsize)
16463 : vim_realloc(prev, prevsize);
16464 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016465 {
16466 do_outofmem_msg((long_u)prevsize);
16467 failed = TRUE;
16468 break;
16469 }
16470 prev = newprev;
16471 }
16472 /* Add the line part to end of "prev". */
16473 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016474 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016475 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016476 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016477
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016478 /*
16479 * For a negative line count use only the lines at the end of the file,
16480 * free the rest.
16481 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016482 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016483 while (cnt > -maxline)
16484 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016485 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016486 --cnt;
16487 }
16488
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016489 if (failed)
16490 {
16491 list_free(rettv->vval.v_list, TRUE);
16492 /* readfile doc says an empty list is returned on error */
16493 rettv->vval.v_list = list_alloc();
16494 }
16495
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016496 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016497 fclose(fd);
16498}
16499
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016500#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016501static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016502
16503/*
16504 * Convert a List to proftime_T.
16505 * Return FAIL when there is something wrong.
16506 */
16507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016508list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016509{
16510 long n1, n2;
16511 int error = FALSE;
16512
16513 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16514 || arg->vval.v_list->lv_len != 2)
16515 return FAIL;
16516 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16517 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16518# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016519 tm->HighPart = n1;
16520 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016521# else
16522 tm->tv_sec = n1;
16523 tm->tv_usec = n2;
16524# endif
16525 return error ? FAIL : OK;
16526}
16527#endif /* FEAT_RELTIME */
16528
16529/*
16530 * "reltime()" function
16531 */
16532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016533f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016534{
16535#ifdef FEAT_RELTIME
16536 proftime_T res;
16537 proftime_T start;
16538
16539 if (argvars[0].v_type == VAR_UNKNOWN)
16540 {
16541 /* No arguments: get current time. */
16542 profile_start(&res);
16543 }
16544 else if (argvars[1].v_type == VAR_UNKNOWN)
16545 {
16546 if (list2proftime(&argvars[0], &res) == FAIL)
16547 return;
16548 profile_end(&res);
16549 }
16550 else
16551 {
16552 /* Two arguments: compute the difference. */
16553 if (list2proftime(&argvars[0], &start) == FAIL
16554 || list2proftime(&argvars[1], &res) == FAIL)
16555 return;
16556 profile_sub(&res, &start);
16557 }
16558
16559 if (rettv_list_alloc(rettv) == OK)
16560 {
16561 long n1, n2;
16562
16563# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016564 n1 = res.HighPart;
16565 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016566# else
16567 n1 = res.tv_sec;
16568 n2 = res.tv_usec;
16569# endif
16570 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16571 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16572 }
16573#endif
16574}
16575
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016576#ifdef FEAT_FLOAT
16577/*
16578 * "reltimefloat()" function
16579 */
16580 static void
16581f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16582{
16583# ifdef FEAT_RELTIME
16584 proftime_T tm;
16585# endif
16586
16587 rettv->v_type = VAR_FLOAT;
16588 rettv->vval.v_float = 0;
16589# ifdef FEAT_RELTIME
16590 if (list2proftime(&argvars[0], &tm) == OK)
16591 rettv->vval.v_float = profile_float(&tm);
16592# endif
16593}
16594#endif
16595
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016596/*
16597 * "reltimestr()" function
16598 */
16599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016600f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016601{
16602#ifdef FEAT_RELTIME
16603 proftime_T tm;
16604#endif
16605
16606 rettv->v_type = VAR_STRING;
16607 rettv->vval.v_string = NULL;
16608#ifdef FEAT_RELTIME
16609 if (list2proftime(&argvars[0], &tm) == OK)
16610 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16611#endif
16612}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016613
Bram Moolenaar0d660222005-01-07 21:51:51 +000016614#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016615static void make_connection(void);
16616static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016617
16618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016619make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016620{
16621 if (X_DISPLAY == NULL
16622# ifdef FEAT_GUI
16623 && !gui.in_use
16624# endif
16625 )
16626 {
16627 x_force_connect = TRUE;
16628 setup_term_clip();
16629 x_force_connect = FALSE;
16630 }
16631}
16632
16633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016634check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016635{
16636 make_connection();
16637 if (X_DISPLAY == NULL)
16638 {
16639 EMSG(_("E240: No connection to Vim server"));
16640 return FAIL;
16641 }
16642 return OK;
16643}
16644#endif
16645
16646#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016647static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016648
16649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016650remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016651{
16652 char_u *server_name;
16653 char_u *keys;
16654 char_u *r = NULL;
16655 char_u buf[NUMBUFLEN];
16656# ifdef WIN32
16657 HWND w;
16658# else
16659 Window w;
16660# endif
16661
16662 if (check_restricted() || check_secure())
16663 return;
16664
16665# ifdef FEAT_X11
16666 if (check_connection() == FAIL)
16667 return;
16668# endif
16669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016670 server_name = get_tv_string_chk(&argvars[0]);
16671 if (server_name == NULL)
16672 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016673 keys = get_tv_string_buf(&argvars[1], buf);
16674# ifdef WIN32
16675 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16676# else
16677 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16678 < 0)
16679# endif
16680 {
16681 if (r != NULL)
16682 EMSG(r); /* sending worked but evaluation failed */
16683 else
16684 EMSG2(_("E241: Unable to send to %s"), server_name);
16685 return;
16686 }
16687
16688 rettv->vval.v_string = r;
16689
16690 if (argvars[2].v_type != VAR_UNKNOWN)
16691 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016692 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016693 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016694 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016695
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016696 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016697 v.di_tv.v_type = VAR_STRING;
16698 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016699 idvar = get_tv_string_chk(&argvars[2]);
16700 if (idvar != NULL)
16701 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016703 }
16704}
16705#endif
16706
16707/*
16708 * "remote_expr()" function
16709 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016711f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016712{
16713 rettv->v_type = VAR_STRING;
16714 rettv->vval.v_string = NULL;
16715#ifdef FEAT_CLIENTSERVER
16716 remote_common(argvars, rettv, TRUE);
16717#endif
16718}
16719
16720/*
16721 * "remote_foreground()" function
16722 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016724f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016725{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726#ifdef FEAT_CLIENTSERVER
16727# ifdef WIN32
16728 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016729 {
16730 char_u *server_name = get_tv_string_chk(&argvars[0]);
16731
16732 if (server_name != NULL)
16733 serverForeground(server_name);
16734 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016735# else
16736 /* Send a foreground() expression to the server. */
16737 argvars[1].v_type = VAR_STRING;
16738 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16739 argvars[2].v_type = VAR_UNKNOWN;
16740 remote_common(argvars, rettv, TRUE);
16741 vim_free(argvars[1].vval.v_string);
16742# endif
16743#endif
16744}
16745
Bram Moolenaar0d660222005-01-07 21:51:51 +000016746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016747f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016748{
16749#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016750 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016751 char_u *s = NULL;
16752# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016753 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016755 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016756
16757 if (check_restricted() || check_secure())
16758 {
16759 rettv->vval.v_number = -1;
16760 return;
16761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016762 serverid = get_tv_string_chk(&argvars[0]);
16763 if (serverid == NULL)
16764 {
16765 rettv->vval.v_number = -1;
16766 return; /* type error; errmsg already given */
16767 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016768# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016769 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016770 if (n == 0)
16771 rettv->vval.v_number = -1;
16772 else
16773 {
16774 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16775 rettv->vval.v_number = (s != NULL);
16776 }
16777# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016778 if (check_connection() == FAIL)
16779 return;
16780
16781 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016782 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783# endif
16784
16785 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016787 char_u *retvar;
16788
Bram Moolenaar33570922005-01-25 22:26:29 +000016789 v.di_tv.v_type = VAR_STRING;
16790 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016791 retvar = get_tv_string_chk(&argvars[1]);
16792 if (retvar != NULL)
16793 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016794 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016795 }
16796#else
16797 rettv->vval.v_number = -1;
16798#endif
16799}
16800
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016802f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016803{
16804 char_u *r = NULL;
16805
16806#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016807 char_u *serverid = get_tv_string_chk(&argvars[0]);
16808
16809 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016810 {
16811# ifdef WIN32
16812 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016813 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016814
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016815 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016816 if (n != 0)
16817 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16818 if (r == NULL)
16819# else
16820 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016821 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016822# endif
16823 EMSG(_("E277: Unable to read a server reply"));
16824 }
16825#endif
16826 rettv->v_type = VAR_STRING;
16827 rettv->vval.v_string = r;
16828}
16829
16830/*
16831 * "remote_send()" function
16832 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016834f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016835{
16836 rettv->v_type = VAR_STRING;
16837 rettv->vval.v_string = NULL;
16838#ifdef FEAT_CLIENTSERVER
16839 remote_common(argvars, rettv, FALSE);
16840#endif
16841}
16842
16843/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016844 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016845 */
16846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016847f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016848{
Bram Moolenaar33570922005-01-25 22:26:29 +000016849 list_T *l;
16850 listitem_T *item, *item2;
16851 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016852 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016853 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016854 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016855 dict_T *d;
16856 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016857 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016858
Bram Moolenaar8c711452005-01-14 21:53:12 +000016859 if (argvars[0].v_type == VAR_DICT)
16860 {
16861 if (argvars[2].v_type != VAR_UNKNOWN)
16862 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016863 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016864 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 key = get_tv_string_chk(&argvars[1]);
16867 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016869 di = dict_find(d, key, -1);
16870 if (di == NULL)
16871 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016872 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16873 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016874 {
16875 *rettv = di->di_tv;
16876 init_tv(&di->di_tv);
16877 dictitem_remove(d, di);
16878 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016879 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016880 }
16881 }
16882 else if (argvars[0].v_type != VAR_LIST)
16883 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016884 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016885 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016887 int error = FALSE;
16888
16889 idx = get_tv_number_chk(&argvars[1], &error);
16890 if (error)
16891 ; /* type error: do nothing, errmsg already given */
16892 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016893 EMSGN(_(e_listidx), idx);
16894 else
16895 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016896 if (argvars[2].v_type == VAR_UNKNOWN)
16897 {
16898 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016899 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016900 *rettv = item->li_tv;
16901 vim_free(item);
16902 }
16903 else
16904 {
16905 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016906 end = get_tv_number_chk(&argvars[2], &error);
16907 if (error)
16908 ; /* type error: do nothing */
16909 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016910 EMSGN(_(e_listidx), end);
16911 else
16912 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016913 int cnt = 0;
16914
16915 for (li = item; li != NULL; li = li->li_next)
16916 {
16917 ++cnt;
16918 if (li == item2)
16919 break;
16920 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016921 if (li == NULL) /* didn't find "item2" after "item" */
16922 EMSG(_(e_invrange));
16923 else
16924 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016925 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016926 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016927 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016928 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016929 l->lv_first = item;
16930 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016931 item->li_prev = NULL;
16932 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016933 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016934 }
16935 }
16936 }
16937 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016938 }
16939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940}
16941
16942/*
16943 * "rename({from}, {to})" function
16944 */
16945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016946f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947{
16948 char_u buf[NUMBUFLEN];
16949
16950 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016951 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016953 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16954 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955}
16956
16957/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016958 * "repeat()" function
16959 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016961f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016962{
16963 char_u *p;
16964 int n;
16965 int slen;
16966 int len;
16967 char_u *r;
16968 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016969
16970 n = get_tv_number(&argvars[1]);
16971 if (argvars[0].v_type == VAR_LIST)
16972 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016973 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016974 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016975 if (list_extend(rettv->vval.v_list,
16976 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016977 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016978 }
16979 else
16980 {
16981 p = get_tv_string(&argvars[0]);
16982 rettv->v_type = VAR_STRING;
16983 rettv->vval.v_string = NULL;
16984
16985 slen = (int)STRLEN(p);
16986 len = slen * n;
16987 if (len <= 0)
16988 return;
16989
16990 r = alloc(len + 1);
16991 if (r != NULL)
16992 {
16993 for (i = 0; i < n; i++)
16994 mch_memmove(r + i * slen, p, (size_t)slen);
16995 r[len] = NUL;
16996 }
16997
16998 rettv->vval.v_string = r;
16999 }
17000}
17001
17002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 * "resolve()" function
17004 */
17005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017006f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007{
17008 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017009#ifdef HAVE_READLINK
17010 char_u *buf = NULL;
17011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017013 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014#ifdef FEAT_SHORTCUT
17015 {
17016 char_u *v = NULL;
17017
17018 v = mch_resolve_shortcut(p);
17019 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017020 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 }
17024#else
17025# ifdef HAVE_READLINK
17026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 char_u *cpy;
17028 int len;
17029 char_u *remain = NULL;
17030 char_u *q;
17031 int is_relative_to_current = FALSE;
17032 int has_trailing_pathsep = FALSE;
17033 int limit = 100;
17034
17035 p = vim_strsave(p);
17036
17037 if (p[0] == '.' && (vim_ispathsep(p[1])
17038 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17039 is_relative_to_current = TRUE;
17040
17041 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017042 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017045 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047
17048 q = getnextcomp(p);
17049 if (*q != NUL)
17050 {
17051 /* Separate the first path component in "p", and keep the
17052 * remainder (beginning with the path separator). */
17053 remain = vim_strsave(q - 1);
17054 q[-1] = NUL;
17055 }
17056
Bram Moolenaard9462e32011-04-11 21:35:11 +020017057 buf = alloc(MAXPATHL + 1);
17058 if (buf == NULL)
17059 goto fail;
17060
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061 for (;;)
17062 {
17063 for (;;)
17064 {
17065 len = readlink((char *)p, (char *)buf, MAXPATHL);
17066 if (len <= 0)
17067 break;
17068 buf[len] = NUL;
17069
17070 if (limit-- == 0)
17071 {
17072 vim_free(p);
17073 vim_free(remain);
17074 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 goto fail;
17077 }
17078
17079 /* Ensure that the result will have a trailing path separator
17080 * if the argument has one. */
17081 if (remain == NULL && has_trailing_pathsep)
17082 add_pathsep(buf);
17083
17084 /* Separate the first path component in the link value and
17085 * concatenate the remainders. */
17086 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17087 if (*q != NUL)
17088 {
17089 if (remain == NULL)
17090 remain = vim_strsave(q - 1);
17091 else
17092 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017093 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094 if (cpy != NULL)
17095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 vim_free(remain);
17097 remain = cpy;
17098 }
17099 }
17100 q[-1] = NUL;
17101 }
17102
17103 q = gettail(p);
17104 if (q > p && *q == NUL)
17105 {
17106 /* Ignore trailing path separator. */
17107 q[-1] = NUL;
17108 q = gettail(p);
17109 }
17110 if (q > p && !mch_isFullName(buf))
17111 {
17112 /* symlink is relative to directory of argument */
17113 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17114 if (cpy != NULL)
17115 {
17116 STRCPY(cpy, p);
17117 STRCPY(gettail(cpy), buf);
17118 vim_free(p);
17119 p = cpy;
17120 }
17121 }
17122 else
17123 {
17124 vim_free(p);
17125 p = vim_strsave(buf);
17126 }
17127 }
17128
17129 if (remain == NULL)
17130 break;
17131
17132 /* Append the first path component of "remain" to "p". */
17133 q = getnextcomp(remain + 1);
17134 len = q - remain - (*q != NUL);
17135 cpy = vim_strnsave(p, STRLEN(p) + len);
17136 if (cpy != NULL)
17137 {
17138 STRNCAT(cpy, remain, len);
17139 vim_free(p);
17140 p = cpy;
17141 }
17142 /* Shorten "remain". */
17143 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017144 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 else
17146 {
17147 vim_free(remain);
17148 remain = NULL;
17149 }
17150 }
17151
17152 /* If the result is a relative path name, make it explicitly relative to
17153 * the current directory if and only if the argument had this form. */
17154 if (!vim_ispathsep(*p))
17155 {
17156 if (is_relative_to_current
17157 && *p != NUL
17158 && !(p[0] == '.'
17159 && (p[1] == NUL
17160 || vim_ispathsep(p[1])
17161 || (p[1] == '.'
17162 && (p[2] == NUL
17163 || vim_ispathsep(p[2]))))))
17164 {
17165 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017166 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 if (cpy != NULL)
17168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 vim_free(p);
17170 p = cpy;
17171 }
17172 }
17173 else if (!is_relative_to_current)
17174 {
17175 /* Strip leading "./". */
17176 q = p;
17177 while (q[0] == '.' && vim_ispathsep(q[1]))
17178 q += 2;
17179 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017180 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 }
17182 }
17183
17184 /* Ensure that the result will have no trailing path separator
17185 * if the argument had none. But keep "/" or "//". */
17186 if (!has_trailing_pathsep)
17187 {
17188 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017189 if (after_pathsep(p, q))
17190 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 }
17192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017193 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 }
17195# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017196 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197# endif
17198#endif
17199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017200 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201
17202#ifdef HAVE_READLINK
17203fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017204 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017206 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207}
17208
17209/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017210 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 */
17212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017213f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214{
Bram Moolenaar33570922005-01-25 22:26:29 +000017215 list_T *l;
17216 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217
Bram Moolenaar0d660222005-01-07 21:51:51 +000017218 if (argvars[0].v_type != VAR_LIST)
17219 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017220 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017221 && !tv_check_lock(l->lv_lock,
17222 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017223 {
17224 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017225 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017226 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227 while (li != NULL)
17228 {
17229 ni = li->li_prev;
17230 list_append(l, li);
17231 li = ni;
17232 }
17233 rettv->vval.v_list = l;
17234 rettv->v_type = VAR_LIST;
17235 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017236 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238}
17239
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017240#define SP_NOMOVE 0x01 /* don't move cursor */
17241#define SP_REPEAT 0x02 /* repeat to find outer pair */
17242#define SP_RETCOUNT 0x04 /* return matchcount */
17243#define SP_SETPCMARK 0x08 /* set previous context mark */
17244#define SP_START 0x10 /* accept match at start position */
17245#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17246#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017247#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017248
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017249static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017250
17251/*
17252 * Get flags for a search function.
17253 * Possibly sets "p_ws".
17254 * Returns BACKWARD, FORWARD or zero (for an error).
17255 */
17256 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017257get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258{
17259 int dir = FORWARD;
17260 char_u *flags;
17261 char_u nbuf[NUMBUFLEN];
17262 int mask;
17263
17264 if (varp->v_type != VAR_UNKNOWN)
17265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017266 flags = get_tv_string_buf_chk(varp, nbuf);
17267 if (flags == NULL)
17268 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017269 while (*flags != NUL)
17270 {
17271 switch (*flags)
17272 {
17273 case 'b': dir = BACKWARD; break;
17274 case 'w': p_ws = TRUE; break;
17275 case 'W': p_ws = FALSE; break;
17276 default: mask = 0;
17277 if (flagsp != NULL)
17278 switch (*flags)
17279 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017280 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017281 case 'e': mask = SP_END; break;
17282 case 'm': mask = SP_RETCOUNT; break;
17283 case 'n': mask = SP_NOMOVE; break;
17284 case 'p': mask = SP_SUBPAT; break;
17285 case 'r': mask = SP_REPEAT; break;
17286 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017287 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017288 }
17289 if (mask == 0)
17290 {
17291 EMSG2(_(e_invarg2), flags);
17292 dir = 0;
17293 }
17294 else
17295 *flagsp |= mask;
17296 }
17297 if (dir == 0)
17298 break;
17299 ++flags;
17300 }
17301 }
17302 return dir;
17303}
17304
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017306 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017308 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017309search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017311 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 char_u *pat;
17313 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017314 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 int save_p_ws = p_ws;
17316 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017317 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017318 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017319 proftime_T tm;
17320#ifdef FEAT_RELTIME
17321 long time_limit = 0;
17322#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017323 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017324 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017326 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017327 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017328 if (dir == 0)
17329 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017330 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017331 if (flags & SP_START)
17332 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017333 if (flags & SP_END)
17334 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017335 if (flags & SP_COLUMN)
17336 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017337
Bram Moolenaar76929292008-01-06 19:07:36 +000017338 /* Optional arguments: line number to stop searching and timeout. */
17339 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017340 {
17341 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17342 if (lnum_stop < 0)
17343 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017344#ifdef FEAT_RELTIME
17345 if (argvars[3].v_type != VAR_UNKNOWN)
17346 {
17347 time_limit = get_tv_number_chk(&argvars[3], NULL);
17348 if (time_limit < 0)
17349 goto theend;
17350 }
17351#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017352 }
17353
Bram Moolenaar76929292008-01-06 19:07:36 +000017354#ifdef FEAT_RELTIME
17355 /* Set the time limit, if there is one. */
17356 profile_setlimit(time_limit, &tm);
17357#endif
17358
Bram Moolenaar231334e2005-07-25 20:46:57 +000017359 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017360 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017361 * Check to make sure only those flags are set.
17362 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17363 * flags cannot be set. Check for that condition also.
17364 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017365 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017366 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017368 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017369 goto theend;
17370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017372 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017373 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017374 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017375 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017377 if (flags & SP_SUBPAT)
17378 retval = subpatnum;
17379 else
17380 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017381 if (flags & SP_SETPCMARK)
17382 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017384 if (match_pos != NULL)
17385 {
17386 /* Store the match cursor position */
17387 match_pos->lnum = pos.lnum;
17388 match_pos->col = pos.col + 1;
17389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 /* "/$" will put the cursor after the end of the line, may need to
17391 * correct that here */
17392 check_cursor();
17393 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017394
17395 /* If 'n' flag is used: restore cursor position. */
17396 if (flags & SP_NOMOVE)
17397 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017398 else
17399 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017400theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017402
17403 return retval;
17404}
17405
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017406#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017407
17408/*
17409 * round() is not in C90, use ceil() or floor() instead.
17410 */
17411 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017412vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017413{
17414 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17415}
17416
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017417/*
17418 * "round({float})" function
17419 */
17420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017421f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017422{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017423 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017424
17425 rettv->v_type = VAR_FLOAT;
17426 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017427 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017428 else
17429 rettv->vval.v_float = 0.0;
17430}
17431#endif
17432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017433/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017434 * "screenattr()" function
17435 */
17436 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017437f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017438{
17439 int row;
17440 int col;
17441 int c;
17442
17443 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17444 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17445 if (row < 0 || row >= screen_Rows
17446 || col < 0 || col >= screen_Columns)
17447 c = -1;
17448 else
17449 c = ScreenAttrs[LineOffset[row] + col];
17450 rettv->vval.v_number = c;
17451}
17452
17453/*
17454 * "screenchar()" function
17455 */
17456 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017457f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017458{
17459 int row;
17460 int col;
17461 int off;
17462 int c;
17463
17464 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17465 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17466 if (row < 0 || row >= screen_Rows
17467 || col < 0 || col >= screen_Columns)
17468 c = -1;
17469 else
17470 {
17471 off = LineOffset[row] + col;
17472#ifdef FEAT_MBYTE
17473 if (enc_utf8 && ScreenLinesUC[off] != 0)
17474 c = ScreenLinesUC[off];
17475 else
17476#endif
17477 c = ScreenLines[off];
17478 }
17479 rettv->vval.v_number = c;
17480}
17481
17482/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017483 * "screencol()" function
17484 *
17485 * First column is 1 to be consistent with virtcol().
17486 */
17487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017488f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017489{
17490 rettv->vval.v_number = screen_screencol() + 1;
17491}
17492
17493/*
17494 * "screenrow()" function
17495 */
17496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017497f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017498{
17499 rettv->vval.v_number = screen_screenrow() + 1;
17500}
17501
17502/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017503 * "search()" function
17504 */
17505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017506f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017507{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017508 int flags = 0;
17509
17510 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511}
17512
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017514 * "searchdecl()" function
17515 */
17516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017517f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017518{
17519 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017520 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017521 int error = FALSE;
17522 char_u *name;
17523
17524 rettv->vval.v_number = 1; /* default: FAIL */
17525
17526 name = get_tv_string_chk(&argvars[0]);
17527 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017528 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017529 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017530 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17531 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17532 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017533 if (!error && name != NULL)
17534 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017535 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017536}
17537
17538/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017539 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017542searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543{
17544 char_u *spat, *mpat, *epat;
17545 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 int dir;
17548 int flags = 0;
17549 char_u nbuf1[NUMBUFLEN];
17550 char_u nbuf2[NUMBUFLEN];
17551 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017552 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017553 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017554 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017557 spat = get_tv_string_chk(&argvars[0]);
17558 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17559 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17560 if (spat == NULL || mpat == NULL || epat == NULL)
17561 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 /* Handle the optional fourth argument: flags */
17564 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017565 if (dir == 0)
17566 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017567
17568 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017569 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17570 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017571 if ((flags & (SP_END | SP_SUBPAT)) != 0
17572 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017573 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017574 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017575 goto theend;
17576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017578 /* Using 'r' implies 'W', otherwise it doesn't work. */
17579 if (flags & SP_REPEAT)
17580 p_ws = FALSE;
17581
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017582 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017583 if (argvars[3].v_type == VAR_UNKNOWN
17584 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 skip = (char_u *)"";
17586 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017588 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017589 if (argvars[5].v_type != VAR_UNKNOWN)
17590 {
17591 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17592 if (lnum_stop < 0)
17593 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017594#ifdef FEAT_RELTIME
17595 if (argvars[6].v_type != VAR_UNKNOWN)
17596 {
17597 time_limit = get_tv_number_chk(&argvars[6], NULL);
17598 if (time_limit < 0)
17599 goto theend;
17600 }
17601#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017602 }
17603 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017604 if (skip == NULL)
17605 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017607 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017608 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017609
17610theend:
17611 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017612
17613 return retval;
17614}
17615
17616/*
17617 * "searchpair()" function
17618 */
17619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017620f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017621{
17622 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17623}
17624
17625/*
17626 * "searchpairpos()" function
17627 */
17628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017629f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017630{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017631 pos_T match_pos;
17632 int lnum = 0;
17633 int col = 0;
17634
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017635 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017636 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017637
17638 if (searchpair_cmn(argvars, &match_pos) > 0)
17639 {
17640 lnum = match_pos.lnum;
17641 col = match_pos.col;
17642 }
17643
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017644 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17645 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017646}
17647
17648/*
17649 * Search for a start/middle/end thing.
17650 * Used by searchpair(), see its documentation for the details.
17651 * Returns 0 or -1 for no match,
17652 */
17653 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017654do_searchpair(
17655 char_u *spat, /* start pattern */
17656 char_u *mpat, /* middle pattern */
17657 char_u *epat, /* end pattern */
17658 int dir, /* BACKWARD or FORWARD */
17659 char_u *skip, /* skip expression */
17660 int flags, /* SP_SETPCMARK and other SP_ values */
17661 pos_T *match_pos,
17662 linenr_T lnum_stop, /* stop at this line if not zero */
17663 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017664{
17665 char_u *save_cpo;
17666 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17667 long retval = 0;
17668 pos_T pos;
17669 pos_T firstpos;
17670 pos_T foundpos;
17671 pos_T save_cursor;
17672 pos_T save_pos;
17673 int n;
17674 int r;
17675 int nest = 1;
17676 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017677 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017678 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017679
17680 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17681 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017682 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017683
Bram Moolenaar76929292008-01-06 19:07:36 +000017684#ifdef FEAT_RELTIME
17685 /* Set the time limit, if there is one. */
17686 profile_setlimit(time_limit, &tm);
17687#endif
17688
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017689 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17690 * start/middle/end (pat3, for the top pair). */
17691 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17692 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17693 if (pat2 == NULL || pat3 == NULL)
17694 goto theend;
17695 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17696 if (*mpat == NUL)
17697 STRCPY(pat3, pat2);
17698 else
17699 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17700 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017701 if (flags & SP_START)
17702 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017703
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 save_cursor = curwin->w_cursor;
17705 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017706 clearpos(&firstpos);
17707 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 pat = pat3;
17709 for (;;)
17710 {
17711 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017712 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17714 /* didn't find it or found the first match again: FAIL */
17715 break;
17716
17717 if (firstpos.lnum == 0)
17718 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017719 if (equalpos(pos, foundpos))
17720 {
17721 /* Found the same position again. Can happen with a pattern that
17722 * has "\zs" at the end and searching backwards. Advance one
17723 * character and try again. */
17724 if (dir == BACKWARD)
17725 decl(&pos);
17726 else
17727 incl(&pos);
17728 }
17729 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017731 /* clear the start flag to avoid getting stuck here */
17732 options &= ~SEARCH_START;
17733
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 /* If the skip pattern matches, ignore this match. */
17735 if (*skip != NUL)
17736 {
17737 save_pos = curwin->w_cursor;
17738 curwin->w_cursor = pos;
17739 r = eval_to_bool(skip, &err, NULL, FALSE);
17740 curwin->w_cursor = save_pos;
17741 if (err)
17742 {
17743 /* Evaluating {skip} caused an error, break here. */
17744 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017745 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 break;
17747 }
17748 if (r)
17749 continue;
17750 }
17751
17752 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17753 {
17754 /* Found end when searching backwards or start when searching
17755 * forward: nested pair. */
17756 ++nest;
17757 pat = pat2; /* nested, don't search for middle */
17758 }
17759 else
17760 {
17761 /* Found end when searching forward or start when searching
17762 * backward: end of (nested) pair; or found middle in outer pair. */
17763 if (--nest == 1)
17764 pat = pat3; /* outer level, search for middle */
17765 }
17766
17767 if (nest == 0)
17768 {
17769 /* Found the match: return matchcount or line number. */
17770 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017771 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017773 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017774 if (flags & SP_SETPCMARK)
17775 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 curwin->w_cursor = pos;
17777 if (!(flags & SP_REPEAT))
17778 break;
17779 nest = 1; /* search for next unmatched */
17780 }
17781 }
17782
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017783 if (match_pos != NULL)
17784 {
17785 /* Store the match cursor position */
17786 match_pos->lnum = curwin->w_cursor.lnum;
17787 match_pos->col = curwin->w_cursor.col + 1;
17788 }
17789
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017791 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 curwin->w_cursor = save_cursor;
17793
17794theend:
17795 vim_free(pat2);
17796 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017797 if (p_cpo == empty_option)
17798 p_cpo = save_cpo;
17799 else
17800 /* Darn, evaluating the {skip} expression changed the value. */
17801 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017802
17803 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804}
17805
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017806/*
17807 * "searchpos()" function
17808 */
17809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017810f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017811{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017812 pos_T match_pos;
17813 int lnum = 0;
17814 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017815 int n;
17816 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017817
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017818 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017819 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017820
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017821 n = search_cmn(argvars, &match_pos, &flags);
17822 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017823 {
17824 lnum = match_pos.lnum;
17825 col = match_pos.col;
17826 }
17827
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017828 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17829 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017830 if (flags & SP_SUBPAT)
17831 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017832}
17833
Bram Moolenaar0d660222005-01-07 21:51:51 +000017834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017835f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017837#ifdef FEAT_CLIENTSERVER
17838 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017839 char_u *server = get_tv_string_chk(&argvars[0]);
17840 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841
Bram Moolenaar0d660222005-01-07 21:51:51 +000017842 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017843 if (server == NULL || reply == NULL)
17844 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017845 if (check_restricted() || check_secure())
17846 return;
17847# ifdef FEAT_X11
17848 if (check_connection() == FAIL)
17849 return;
17850# endif
17851
17852 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017854 EMSG(_("E258: Unable to send to client"));
17855 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017857 rettv->vval.v_number = 0;
17858#else
17859 rettv->vval.v_number = -1;
17860#endif
17861}
17862
Bram Moolenaar0d660222005-01-07 21:51:51 +000017863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017864f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017865{
17866 char_u *r = NULL;
17867
17868#ifdef FEAT_CLIENTSERVER
17869# ifdef WIN32
17870 r = serverGetVimNames();
17871# else
17872 make_connection();
17873 if (X_DISPLAY != NULL)
17874 r = serverGetVimNames(X_DISPLAY);
17875# endif
17876#endif
17877 rettv->v_type = VAR_STRING;
17878 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879}
17880
17881/*
17882 * "setbufvar()" function
17883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017885f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886{
17887 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017890 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 char_u nbuf[NUMBUFLEN];
17892
17893 if (check_restricted() || check_secure())
17894 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017895 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17896 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017897 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 varp = &argvars[2];
17899
17900 if (buf != NULL && varname != NULL && varp != NULL)
17901 {
17902 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904
17905 if (*varname == '&')
17906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017907 long numval;
17908 char_u *strval;
17909 int error = FALSE;
17910
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017912 numval = get_tv_number_chk(varp, &error);
17913 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017914 if (!error && strval != NULL)
17915 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 }
17917 else
17918 {
17919 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17920 if (bufvarname != NULL)
17921 {
17922 STRCPY(bufvarname, "b:");
17923 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017924 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 vim_free(bufvarname);
17926 }
17927 }
17928
17929 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932}
17933
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017935f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017936{
17937 dict_T *d;
17938 dictitem_T *di;
17939 char_u *csearch;
17940
17941 if (argvars[0].v_type != VAR_DICT)
17942 {
17943 EMSG(_(e_dictreq));
17944 return;
17945 }
17946
17947 if ((d = argvars[0].vval.v_dict) != NULL)
17948 {
17949 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17950 if (csearch != NULL)
17951 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017952#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017953 if (enc_utf8)
17954 {
17955 int pcc[MAX_MCO];
17956 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017957
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017958 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17959 }
17960 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017961#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017962 set_last_csearch(PTR2CHAR(csearch),
17963 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017964 }
17965
17966 di = dict_find(d, (char_u *)"forward", -1);
17967 if (di != NULL)
17968 set_csearch_direction(get_tv_number(&di->di_tv)
17969 ? FORWARD : BACKWARD);
17970
17971 di = dict_find(d, (char_u *)"until", -1);
17972 if (di != NULL)
17973 set_csearch_until(!!get_tv_number(&di->di_tv));
17974 }
17975}
17976
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977/*
17978 * "setcmdpos()" function
17979 */
17980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017981f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017983 int pos = (int)get_tv_number(&argvars[0]) - 1;
17984
17985 if (pos >= 0)
17986 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987}
17988
17989/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017990 * "setfperm({fname}, {mode})" function
17991 */
17992 static void
17993f_setfperm(typval_T *argvars, typval_T *rettv)
17994{
17995 char_u *fname;
17996 char_u modebuf[NUMBUFLEN];
17997 char_u *mode_str;
17998 int i;
17999 int mask;
18000 int mode = 0;
18001
18002 rettv->vval.v_number = 0;
18003 fname = get_tv_string_chk(&argvars[0]);
18004 if (fname == NULL)
18005 return;
18006 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18007 if (mode_str == NULL)
18008 return;
18009 if (STRLEN(mode_str) != 9)
18010 {
18011 EMSG2(_(e_invarg2), mode_str);
18012 return;
18013 }
18014
18015 mask = 1;
18016 for (i = 8; i >= 0; --i)
18017 {
18018 if (mode_str[i] != '-')
18019 mode |= mask;
18020 mask = mask << 1;
18021 }
18022 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18023}
18024
18025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 * "setline()" function
18027 */
18028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018029f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030{
18031 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018032 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018033 list_T *l = NULL;
18034 listitem_T *li = NULL;
18035 long added = 0;
18036 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018038 lnum = get_tv_lnum(&argvars[0]);
18039 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018041 l = argvars[1].vval.v_list;
18042 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018044 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018045 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018046
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018047 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018048 for (;;)
18049 {
18050 if (l != NULL)
18051 {
18052 /* list argument, get next string */
18053 if (li == NULL)
18054 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018055 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018056 li = li->li_next;
18057 }
18058
18059 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018060 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018061 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018062
18063 /* When coming here from Insert mode, sync undo, so that this can be
18064 * undone separately from what was previously inserted. */
18065 if (u_sync_once == 2)
18066 {
18067 u_sync_once = 1; /* notify that u_sync() was called */
18068 u_sync(TRUE);
18069 }
18070
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018071 if (lnum <= curbuf->b_ml.ml_line_count)
18072 {
18073 /* existing line, replace it */
18074 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18075 {
18076 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018077 if (lnum == curwin->w_cursor.lnum)
18078 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018079 rettv->vval.v_number = 0; /* OK */
18080 }
18081 }
18082 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18083 {
18084 /* lnum is one past the last line, append the line */
18085 ++added;
18086 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18087 rettv->vval.v_number = 0; /* OK */
18088 }
18089
18090 if (l == NULL) /* only one string argument */
18091 break;
18092 ++lnum;
18093 }
18094
18095 if (added > 0)
18096 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097}
18098
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018099static 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 +000018100
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018102 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018103 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018105set_qf_ll_list(
18106 win_T *wp UNUSED,
18107 typval_T *list_arg UNUSED,
18108 typval_T *action_arg UNUSED,
18109 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018110{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018111#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018112 char_u *act;
18113 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018114#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018115
Bram Moolenaar2641f772005-03-25 21:58:17 +000018116 rettv->vval.v_number = -1;
18117
18118#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018119 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018120 EMSG(_(e_listreq));
18121 else
18122 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018123 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018124
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018125 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018126 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018127 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018128 if (act == NULL)
18129 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018130 if (*act == 'a' || *act == 'r')
18131 action = *act;
18132 }
18133
Bram Moolenaar81484f42012-12-05 15:16:47 +010018134 if (l != NULL && set_errorlist(wp, l, action,
18135 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018136 rettv->vval.v_number = 0;
18137 }
18138#endif
18139}
18140
18141/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018142 * "setloclist()" function
18143 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018145f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018146{
18147 win_T *win;
18148
18149 rettv->vval.v_number = -1;
18150
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018151 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018152 if (win != NULL)
18153 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18154}
18155
18156/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018157 * "setmatches()" function
18158 */
18159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018160f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018161{
18162#ifdef FEAT_SEARCH_EXTRA
18163 list_T *l;
18164 listitem_T *li;
18165 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018166 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018167
18168 rettv->vval.v_number = -1;
18169 if (argvars[0].v_type != VAR_LIST)
18170 {
18171 EMSG(_(e_listreq));
18172 return;
18173 }
18174 if ((l = argvars[0].vval.v_list) != NULL)
18175 {
18176
18177 /* To some extent make sure that we are dealing with a list from
18178 * "getmatches()". */
18179 li = l->lv_first;
18180 while (li != NULL)
18181 {
18182 if (li->li_tv.v_type != VAR_DICT
18183 || (d = li->li_tv.vval.v_dict) == NULL)
18184 {
18185 EMSG(_(e_invarg));
18186 return;
18187 }
18188 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018189 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18190 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018191 && dict_find(d, (char_u *)"priority", -1) != NULL
18192 && dict_find(d, (char_u *)"id", -1) != NULL))
18193 {
18194 EMSG(_(e_invarg));
18195 return;
18196 }
18197 li = li->li_next;
18198 }
18199
18200 clear_matches(curwin);
18201 li = l->lv_first;
18202 while (li != NULL)
18203 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018204 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018205 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018206 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018207 char_u *group;
18208 int priority;
18209 int id;
18210 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018211
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018212 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018213 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18214 {
18215 if (s == NULL)
18216 {
18217 s = list_alloc();
18218 if (s == NULL)
18219 return;
18220 }
18221
18222 /* match from matchaddpos() */
18223 for (i = 1; i < 9; i++)
18224 {
18225 sprintf((char *)buf, (char *)"pos%d", i);
18226 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18227 {
18228 if (di->di_tv.v_type != VAR_LIST)
18229 return;
18230
18231 list_append_tv(s, &di->di_tv);
18232 s->lv_refcount++;
18233 }
18234 else
18235 break;
18236 }
18237 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018238
18239 group = get_dict_string(d, (char_u *)"group", FALSE);
18240 priority = (int)get_dict_number(d, (char_u *)"priority");
18241 id = (int)get_dict_number(d, (char_u *)"id");
18242 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18243 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18244 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018245 if (i == 0)
18246 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018247 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018248 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018249 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018250 }
18251 else
18252 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018253 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018254 list_unref(s);
18255 s = NULL;
18256 }
18257
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018258 li = li->li_next;
18259 }
18260 rettv->vval.v_number = 0;
18261 }
18262#endif
18263}
18264
18265/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018266 * "setpos()" function
18267 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018269f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018270{
18271 pos_T pos;
18272 int fnum;
18273 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018274 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018275
Bram Moolenaar08250432008-02-13 11:42:46 +000018276 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018277 name = get_tv_string_chk(argvars);
18278 if (name != NULL)
18279 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018280 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018281 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018282 if (--pos.col < 0)
18283 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018284 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018285 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018286 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018287 if (fnum == curbuf->b_fnum)
18288 {
18289 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018290 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018291 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018292 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018293 curwin->w_set_curswant = FALSE;
18294 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018295 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018296 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018297 }
18298 else
18299 EMSG(_(e_invarg));
18300 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018301 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18302 {
18303 /* set mark */
18304 if (setmark_pos(name[1], &pos, fnum) == OK)
18305 rettv->vval.v_number = 0;
18306 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018307 else
18308 EMSG(_(e_invarg));
18309 }
18310 }
18311}
18312
18313/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018314 * "setqflist()" function
18315 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018317f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018318{
18319 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18320}
18321
18322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 * "setreg()" function
18324 */
18325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018326f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327{
18328 int regname;
18329 char_u *strregname;
18330 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018331 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 int append;
18333 char_u yank_type;
18334 long block_len;
18335
18336 block_len = -1;
18337 yank_type = MAUTO;
18338 append = FALSE;
18339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018340 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018341 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018343 if (strregname == NULL)
18344 return; /* type error; errmsg already given */
18345 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 if (regname == 0 || regname == '@')
18347 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018349 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018351 stropt = get_tv_string_chk(&argvars[2]);
18352 if (stropt == NULL)
18353 return; /* type error */
18354 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 switch (*stropt)
18356 {
18357 case 'a': case 'A': /* append */
18358 append = TRUE;
18359 break;
18360 case 'v': case 'c': /* character-wise selection */
18361 yank_type = MCHAR;
18362 break;
18363 case 'V': case 'l': /* line-wise selection */
18364 yank_type = MLINE;
18365 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 case 'b': case Ctrl_V: /* block-wise selection */
18367 yank_type = MBLOCK;
18368 if (VIM_ISDIGIT(stropt[1]))
18369 {
18370 ++stropt;
18371 block_len = getdigits(&stropt) - 1;
18372 --stropt;
18373 }
18374 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 }
18376 }
18377
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018378 if (argvars[1].v_type == VAR_LIST)
18379 {
18380 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018381 char_u **allocval;
18382 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018383 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018384 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018385 int len = argvars[1].vval.v_list->lv_len;
18386 listitem_T *li;
18387
Bram Moolenaar7d647822014-04-05 21:28:56 +020018388 /* First half: use for pointers to result lines; second half: use for
18389 * pointers to allocated copies. */
18390 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018391 if (lstval == NULL)
18392 return;
18393 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018394 allocval = lstval + len + 2;
18395 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018396
18397 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18398 li = li->li_next)
18399 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018400 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018401 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018402 goto free_lstval;
18403 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018404 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018405 /* Need to make a copy, next get_tv_string_buf_chk() will
18406 * overwrite the string. */
18407 strval = vim_strsave(buf);
18408 if (strval == NULL)
18409 goto free_lstval;
18410 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018411 }
18412 *curval++ = strval;
18413 }
18414 *curval++ = NULL;
18415
18416 write_reg_contents_lst(regname, lstval, -1,
18417 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018418free_lstval:
18419 while (curallocval > allocval)
18420 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018421 vim_free(lstval);
18422 }
18423 else
18424 {
18425 strval = get_tv_string_chk(&argvars[1]);
18426 if (strval == NULL)
18427 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018428 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018431 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432}
18433
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018434/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018435 * "settabvar()" function
18436 */
18437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018438f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018439{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018440#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018441 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018442 tabpage_T *tp;
18443#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018444 char_u *varname, *tabvarname;
18445 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018446
18447 rettv->vval.v_number = 0;
18448
18449 if (check_restricted() || check_secure())
18450 return;
18451
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018452#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018453 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018454#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018455 varname = get_tv_string_chk(&argvars[1]);
18456 varp = &argvars[2];
18457
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018458 if (varname != NULL && varp != NULL
18459#ifdef FEAT_WINDOWS
18460 && tp != NULL
18461#endif
18462 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018463 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018464#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018465 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018466 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018467#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018468
18469 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18470 if (tabvarname != NULL)
18471 {
18472 STRCPY(tabvarname, "t:");
18473 STRCPY(tabvarname + 2, varname);
18474 set_var(tabvarname, varp, TRUE);
18475 vim_free(tabvarname);
18476 }
18477
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018478#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018479 /* Restore current tabpage */
18480 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018481 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018482#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018483 }
18484}
18485
18486/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018487 * "settabwinvar()" function
18488 */
18489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018490f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018491{
18492 setwinvar(argvars, rettv, 1);
18493}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494
18495/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018496 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018499f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018501 setwinvar(argvars, rettv, 0);
18502}
18503
18504/*
18505 * "setwinvar()" and "settabwinvar()" functions
18506 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018507
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018509setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018510{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 win_T *win;
18512#ifdef FEAT_WINDOWS
18513 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018514 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018515 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516#endif
18517 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018518 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018520 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521
18522 if (check_restricted() || check_secure())
18523 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018524
18525#ifdef FEAT_WINDOWS
18526 if (off == 1)
18527 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18528 else
18529 tp = curtab;
18530#endif
18531 win = find_win_by_nr(&argvars[off], tp);
18532 varname = get_tv_string_chk(&argvars[off + 1]);
18533 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534
18535 if (win != NULL && varname != NULL && varp != NULL)
18536 {
18537#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018538 need_switch_win = !(tp == curtab && win == curwin);
18539 if (!need_switch_win
18540 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018543 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018545 long numval;
18546 char_u *strval;
18547 int error = FALSE;
18548
18549 ++varname;
18550 numval = get_tv_number_chk(varp, &error);
18551 strval = get_tv_string_buf_chk(varp, nbuf);
18552 if (!error && strval != NULL)
18553 set_option_value(varname, numval, strval, OPT_LOCAL);
18554 }
18555 else
18556 {
18557 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18558 if (winvarname != NULL)
18559 {
18560 STRCPY(winvarname, "w:");
18561 STRCPY(winvarname + 2, varname);
18562 set_var(winvarname, varp, TRUE);
18563 vim_free(winvarname);
18564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 }
18566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018568 if (need_switch_win)
18569 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570#endif
18571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572}
18573
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018574#ifdef FEAT_CRYPT
18575/*
18576 * "sha256({string})" function
18577 */
18578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018579f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018580{
18581 char_u *p;
18582
18583 p = get_tv_string(&argvars[0]);
18584 rettv->vval.v_string = vim_strsave(
18585 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18586 rettv->v_type = VAR_STRING;
18587}
18588#endif /* FEAT_CRYPT */
18589
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018591 * "shellescape({string})" function
18592 */
18593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018594f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018595{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018596 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018597 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018598 rettv->v_type = VAR_STRING;
18599}
18600
18601/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018602 * shiftwidth() function
18603 */
18604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018605f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018606{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018607 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018608}
18609
18610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018611 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 */
18613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018614f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018616 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617
Bram Moolenaar0d660222005-01-07 21:51:51 +000018618 p = get_tv_string(&argvars[0]);
18619 rettv->vval.v_string = vim_strsave(p);
18620 simplify_filename(rettv->vval.v_string); /* simplify in place */
18621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622}
18623
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018624#ifdef FEAT_FLOAT
18625/*
18626 * "sin()" function
18627 */
18628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018629f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018630{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018631 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018632
18633 rettv->v_type = VAR_FLOAT;
18634 if (get_float_arg(argvars, &f) == OK)
18635 rettv->vval.v_float = sin(f);
18636 else
18637 rettv->vval.v_float = 0.0;
18638}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018639
18640/*
18641 * "sinh()" function
18642 */
18643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018644f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018645{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018646 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018647
18648 rettv->v_type = VAR_FLOAT;
18649 if (get_float_arg(argvars, &f) == OK)
18650 rettv->vval.v_float = sinh(f);
18651 else
18652 rettv->vval.v_float = 0.0;
18653}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018654#endif
18655
Bram Moolenaar0d660222005-01-07 21:51:51 +000018656static int
18657#ifdef __BORLANDC__
18658 _RTLENTRYF
18659#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018660 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018661static int
18662#ifdef __BORLANDC__
18663 _RTLENTRYF
18664#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018665 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018666
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018667/* struct used in the array that's given to qsort() */
18668typedef struct
18669{
18670 listitem_T *item;
18671 int idx;
18672} sortItem_T;
18673
Bram Moolenaar0b962472016-02-22 22:51:33 +010018674/* struct storing information about current sort */
18675typedef struct
18676{
18677 int item_compare_ic;
18678 int item_compare_numeric;
18679 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018680#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018681 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018682#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018683 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018684 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018685 dict_T *item_compare_selfdict;
18686 int item_compare_func_err;
18687 int item_compare_keep_zero;
18688} sortinfo_T;
18689static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018690static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018691#define ITEM_COMPARE_FAIL 999
18692
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018694 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018696 static int
18697#ifdef __BORLANDC__
18698_RTLENTRYF
18699#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018700item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018702 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018703 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018704 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018705 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018706 int res;
18707 char_u numbuf1[NUMBUFLEN];
18708 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018710 si1 = (sortItem_T *)s1;
18711 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018712 tv1 = &si1->item->li_tv;
18713 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018714
Bram Moolenaar0b962472016-02-22 22:51:33 +010018715 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018716 {
18717 long v1 = get_tv_number(tv1);
18718 long v2 = get_tv_number(tv2);
18719
18720 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18721 }
18722
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018723#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018724 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018725 {
18726 float_T v1 = get_tv_float(tv1);
18727 float_T v2 = get_tv_float(tv2);
18728
18729 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18730 }
18731#endif
18732
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018733 /* tv2string() puts quotes around a string and allocates memory. Don't do
18734 * that for string variables. Use a single quote when comparing with a
18735 * non-string to do what the docs promise. */
18736 if (tv1->v_type == VAR_STRING)
18737 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018738 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018739 p1 = (char_u *)"'";
18740 else
18741 p1 = tv1->vval.v_string;
18742 }
18743 else
18744 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18745 if (tv2->v_type == VAR_STRING)
18746 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018747 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018748 p2 = (char_u *)"'";
18749 else
18750 p2 = tv2->vval.v_string;
18751 }
18752 else
18753 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018754 if (p1 == NULL)
18755 p1 = (char_u *)"";
18756 if (p2 == NULL)
18757 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018758 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018759 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018760 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018761 res = STRICMP(p1, p2);
18762 else
18763 res = STRCMP(p1, p2);
18764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018766 {
18767 double n1, n2;
18768 n1 = strtod((char *)p1, (char **)&p1);
18769 n2 = strtod((char *)p2, (char **)&p2);
18770 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18771 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018772
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018773 /* When the result would be zero, compare the item indexes. Makes the
18774 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018775 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018776 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018777
Bram Moolenaar0d660222005-01-07 21:51:51 +000018778 vim_free(tofree1);
18779 vim_free(tofree2);
18780 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781}
18782
18783 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018784#ifdef __BORLANDC__
18785_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018787item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018788{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018789 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018790 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018792 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018793 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018794 char_u *func_name;
18795 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018797 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018798 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018799 return 0;
18800
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018801 si1 = (sortItem_T *)s1;
18802 si2 = (sortItem_T *)s2;
18803
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018804 if (partial == NULL)
18805 func_name = sortinfo->item_compare_func;
18806 else
18807 func_name = partial->pt_name;
18808
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018809 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018810 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018811 copy_tv(&si1->item->li_tv, &argv[0]);
18812 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018813
18814 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018815 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018816 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018817 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018818 clear_tv(&argv[0]);
18819 clear_tv(&argv[1]);
18820
18821 if (res == FAIL)
18822 res = ITEM_COMPARE_FAIL;
18823 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018824 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18825 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018826 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018827 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018828
18829 /* When the result would be zero, compare the pointers themselves. Makes
18830 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018831 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018832 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018833
Bram Moolenaar0d660222005-01-07 21:51:51 +000018834 return res;
18835}
18836
18837/*
18838 * "sort({list})" function
18839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018841do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842{
Bram Moolenaar33570922005-01-25 22:26:29 +000018843 list_T *l;
18844 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018845 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018846 sortinfo_T *old_sortinfo;
18847 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018848 long len;
18849 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850
Bram Moolenaar0b962472016-02-22 22:51:33 +010018851 /* Pointer to current info struct used in compare function. Save and
18852 * restore the current one for nested calls. */
18853 old_sortinfo = sortinfo;
18854 sortinfo = &info;
18855
Bram Moolenaar0d660222005-01-07 21:51:51 +000018856 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018857 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 else
18859 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018860 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018861 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018862 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18863 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018864 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018865 rettv->vval.v_list = l;
18866 rettv->v_type = VAR_LIST;
18867 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868
Bram Moolenaar0d660222005-01-07 21:51:51 +000018869 len = list_len(l);
18870 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018871 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872
Bram Moolenaar0b962472016-02-22 22:51:33 +010018873 info.item_compare_ic = FALSE;
18874 info.item_compare_numeric = FALSE;
18875 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018876#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018877 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018878#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018879 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018880 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018881 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018882 if (argvars[1].v_type != VAR_UNKNOWN)
18883 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018884 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018885 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018886 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018887 else if (argvars[1].v_type == VAR_PARTIAL)
18888 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018889 else
18890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018891 int error = FALSE;
18892
18893 i = get_tv_number_chk(&argvars[1], &error);
18894 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018895 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018896 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018897 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018898 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018899 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018900 else if (i != 0)
18901 {
18902 EMSG(_(e_invarg));
18903 goto theend;
18904 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018905 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018906 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018907 if (*info.item_compare_func == NUL)
18908 {
18909 /* empty string means default sort */
18910 info.item_compare_func = NULL;
18911 }
18912 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018913 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018914 info.item_compare_func = NULL;
18915 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018916 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018917 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018918 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018919 info.item_compare_func = NULL;
18920 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018921 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018922#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018923 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018924 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018925 info.item_compare_func = NULL;
18926 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018927 }
18928#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018929 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018930 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018931 info.item_compare_func = NULL;
18932 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018933 }
18934 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018935 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018936
18937 if (argvars[2].v_type != VAR_UNKNOWN)
18938 {
18939 /* optional third argument: {dict} */
18940 if (argvars[2].v_type != VAR_DICT)
18941 {
18942 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018943 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018944 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018945 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018946 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948
Bram Moolenaar0d660222005-01-07 21:51:51 +000018949 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018950 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018951 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018952 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953
Bram Moolenaar327aa022014-03-25 18:24:23 +010018954 i = 0;
18955 if (sort)
18956 {
18957 /* sort(): ptrs will be the list to sort */
18958 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018959 {
18960 ptrs[i].item = li;
18961 ptrs[i].idx = i;
18962 ++i;
18963 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018964
Bram Moolenaar0b962472016-02-22 22:51:33 +010018965 info.item_compare_func_err = FALSE;
18966 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018967 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018968 if ((info.item_compare_func != NULL
18969 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018970 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018971 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018972 EMSG(_("E702: Sort compare function failed"));
18973 else
18974 {
18975 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018976 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018977 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018978 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018979 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018980
Bram Moolenaar0b962472016-02-22 22:51:33 +010018981 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018982 {
18983 /* Clear the List and append the items in sorted order. */
18984 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18985 l->lv_len = 0;
18986 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018987 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018988 }
18989 }
18990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018992 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018993 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018994
18995 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018996 info.item_compare_func_err = FALSE;
18997 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018998 item_compare_func_ptr = info.item_compare_func != NULL
18999 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019000 ? item_compare2 : item_compare;
19001
19002 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19003 li = li->li_next)
19004 {
19005 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19006 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019007 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019008 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019009 {
19010 EMSG(_("E882: Uniq compare function failed"));
19011 break;
19012 }
19013 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014
Bram Moolenaar0b962472016-02-22 22:51:33 +010019015 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019016 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019017 while (--i >= 0)
19018 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019019 li = ptrs[i].item->li_next;
19020 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019021 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019022 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019023 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019024 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019025 list_fix_watch(l, li);
19026 listitem_free(li);
19027 l->lv_len--;
19028 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019029 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019030 }
19031
19032 vim_free(ptrs);
19033 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019034theend:
19035 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019036}
19037
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019038/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019039 * "sort({list})" function
19040 */
19041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019042f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019043{
19044 do_sort_uniq(argvars, rettv, TRUE);
19045}
19046
19047/*
19048 * "uniq({list})" function
19049 */
19050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019051f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019052{
19053 do_sort_uniq(argvars, rettv, FALSE);
19054}
19055
19056/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019057 * "soundfold({word})" function
19058 */
19059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019060f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019061{
19062 char_u *s;
19063
19064 rettv->v_type = VAR_STRING;
19065 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019066#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019067 rettv->vval.v_string = eval_soundfold(s);
19068#else
19069 rettv->vval.v_string = vim_strsave(s);
19070#endif
19071}
19072
19073/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019074 * "spellbadword()" function
19075 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019077f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019078{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019079 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019080 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019081 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019082
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019083 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019084 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019085
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019086#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019087 if (argvars[0].v_type == VAR_UNKNOWN)
19088 {
19089 /* Find the start and length of the badly spelled word. */
19090 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19091 if (len != 0)
19092 word = ml_get_cursor();
19093 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019094 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019095 {
19096 char_u *str = get_tv_string_chk(&argvars[0]);
19097 int capcol = -1;
19098
19099 if (str != NULL)
19100 {
19101 /* Check the argument for spelling. */
19102 while (*str != NUL)
19103 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019104 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019105 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019106 {
19107 word = str;
19108 break;
19109 }
19110 str += len;
19111 }
19112 }
19113 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019114#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019115
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019116 list_append_string(rettv->vval.v_list, word, len);
19117 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019118 attr == HLF_SPB ? "bad" :
19119 attr == HLF_SPR ? "rare" :
19120 attr == HLF_SPL ? "local" :
19121 attr == HLF_SPC ? "caps" :
19122 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019123}
19124
19125/*
19126 * "spellsuggest()" function
19127 */
19128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019129f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019130{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019131#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019132 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019133 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019134 int maxcount;
19135 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019136 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019137 listitem_T *li;
19138 int need_capital = FALSE;
19139#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019141 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019142 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019143
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019144#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019145 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019146 {
19147 str = get_tv_string(&argvars[0]);
19148 if (argvars[1].v_type != VAR_UNKNOWN)
19149 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019150 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019151 if (maxcount <= 0)
19152 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019153 if (argvars[2].v_type != VAR_UNKNOWN)
19154 {
19155 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19156 if (typeerr)
19157 return;
19158 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019159 }
19160 else
19161 maxcount = 25;
19162
Bram Moolenaar4770d092006-01-12 23:22:24 +000019163 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019164
19165 for (i = 0; i < ga.ga_len; ++i)
19166 {
19167 str = ((char_u **)ga.ga_data)[i];
19168
19169 li = listitem_alloc();
19170 if (li == NULL)
19171 vim_free(str);
19172 else
19173 {
19174 li->li_tv.v_type = VAR_STRING;
19175 li->li_tv.v_lock = 0;
19176 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019177 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019178 }
19179 }
19180 ga_clear(&ga);
19181 }
19182#endif
19183}
19184
Bram Moolenaar0d660222005-01-07 21:51:51 +000019185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019186f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019187{
19188 char_u *str;
19189 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019190 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019191 regmatch_T regmatch;
19192 char_u patbuf[NUMBUFLEN];
19193 char_u *save_cpo;
19194 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019195 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019196 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019197 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019198
19199 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19200 save_cpo = p_cpo;
19201 p_cpo = (char_u *)"";
19202
19203 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019204 if (argvars[1].v_type != VAR_UNKNOWN)
19205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019206 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19207 if (pat == NULL)
19208 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019209 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019210 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019211 }
19212 if (pat == NULL || *pat == NUL)
19213 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019214
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019215 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019217 if (typeerr)
19218 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219
Bram Moolenaar0d660222005-01-07 21:51:51 +000019220 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19221 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019223 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019224 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019225 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019226 if (*str == NUL)
19227 match = FALSE; /* empty item at the end */
19228 else
19229 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019230 if (match)
19231 end = regmatch.startp[0];
19232 else
19233 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019234 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19235 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019236 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019237 if (list_append_string(rettv->vval.v_list, str,
19238 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019239 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019240 }
19241 if (!match)
19242 break;
19243 /* Advance to just after the match. */
19244 if (regmatch.endp[0] > str)
19245 col = 0;
19246 else
19247 {
19248 /* Don't get stuck at the same match. */
19249#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019250 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019251#else
19252 col = 1;
19253#endif
19254 }
19255 str = regmatch.endp[0];
19256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257
Bram Moolenaar473de612013-06-08 18:19:48 +020019258 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260
Bram Moolenaar0d660222005-01-07 21:51:51 +000019261 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262}
19263
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019264#ifdef FEAT_FLOAT
19265/*
19266 * "sqrt()" function
19267 */
19268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019269f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019270{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019271 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019272
19273 rettv->v_type = VAR_FLOAT;
19274 if (get_float_arg(argvars, &f) == OK)
19275 rettv->vval.v_float = sqrt(f);
19276 else
19277 rettv->vval.v_float = 0.0;
19278}
19279
19280/*
19281 * "str2float()" function
19282 */
19283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019284f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019285{
19286 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19287
19288 if (*p == '+')
19289 p = skipwhite(p + 1);
19290 (void)string2float(p, &rettv->vval.v_float);
19291 rettv->v_type = VAR_FLOAT;
19292}
19293#endif
19294
Bram Moolenaar2c932302006-03-18 21:42:09 +000019295/*
19296 * "str2nr()" function
19297 */
19298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019299f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019300{
19301 int base = 10;
19302 char_u *p;
19303 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019304 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019305
19306 if (argvars[1].v_type != VAR_UNKNOWN)
19307 {
19308 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019309 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019310 {
19311 EMSG(_(e_invarg));
19312 return;
19313 }
19314 }
19315
19316 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019317 if (*p == '+')
19318 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019319 switch (base)
19320 {
19321 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19322 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19323 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19324 default: what = 0;
19325 }
19326 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019327 rettv->vval.v_number = n;
19328}
19329
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330#ifdef HAVE_STRFTIME
19331/*
19332 * "strftime({format}[, {time}])" function
19333 */
19334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019335f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336{
19337 char_u result_buf[256];
19338 struct tm *curtime;
19339 time_t seconds;
19340 char_u *p;
19341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019342 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019344 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019345 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 seconds = time(NULL);
19347 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019348 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 curtime = localtime(&seconds);
19350 /* MSVC returns NULL for an invalid value of seconds. */
19351 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019352 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 else
19354 {
19355# ifdef FEAT_MBYTE
19356 vimconv_T conv;
19357 char_u *enc;
19358
19359 conv.vc_type = CONV_NONE;
19360 enc = enc_locale();
19361 convert_setup(&conv, p_enc, enc);
19362 if (conv.vc_type != CONV_NONE)
19363 p = string_convert(&conv, p, NULL);
19364# endif
19365 if (p != NULL)
19366 (void)strftime((char *)result_buf, sizeof(result_buf),
19367 (char *)p, curtime);
19368 else
19369 result_buf[0] = NUL;
19370
19371# ifdef FEAT_MBYTE
19372 if (conv.vc_type != CONV_NONE)
19373 vim_free(p);
19374 convert_setup(&conv, enc, p_enc);
19375 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019376 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 else
19378# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019379 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380
19381# ifdef FEAT_MBYTE
19382 /* Release conversion descriptors */
19383 convert_setup(&conv, NULL, NULL);
19384 vim_free(enc);
19385# endif
19386 }
19387}
19388#endif
19389
19390/*
19391 * "stridx()" function
19392 */
19393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019394f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395{
19396 char_u buf[NUMBUFLEN];
19397 char_u *needle;
19398 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019399 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019401 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019403 needle = get_tv_string_chk(&argvars[1]);
19404 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019405 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019406 if (needle == NULL || haystack == NULL)
19407 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408
Bram Moolenaar33570922005-01-25 22:26:29 +000019409 if (argvars[2].v_type != VAR_UNKNOWN)
19410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019411 int error = FALSE;
19412
19413 start_idx = get_tv_number_chk(&argvars[2], &error);
19414 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019415 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019416 if (start_idx >= 0)
19417 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019418 }
19419
19420 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19421 if (pos != NULL)
19422 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423}
19424
19425/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019426 * "string()" function
19427 */
19428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019429f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019430{
19431 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019432 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019434 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019435 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19436 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019437 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019438 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019439 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440}
19441
19442/*
19443 * "strlen()" function
19444 */
19445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019446f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019448 rettv->vval.v_number = (varnumber_T)(STRLEN(
19449 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450}
19451
19452/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019453 * "strchars()" function
19454 */
19455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019456f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019457{
19458 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019459 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019460#ifdef FEAT_MBYTE
19461 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019462 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019463#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019464
19465 if (argvars[1].v_type != VAR_UNKNOWN)
19466 skipcc = get_tv_number_chk(&argvars[1], NULL);
19467 if (skipcc < 0 || skipcc > 1)
19468 EMSG(_(e_invarg));
19469 else
19470 {
19471#ifdef FEAT_MBYTE
19472 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19473 while (*s != NUL)
19474 {
19475 func_mb_ptr2char_adv(&s);
19476 ++len;
19477 }
19478 rettv->vval.v_number = len;
19479#else
19480 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19481#endif
19482 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019483}
19484
19485/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019486 * "strdisplaywidth()" function
19487 */
19488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019489f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019490{
19491 char_u *s = get_tv_string(&argvars[0]);
19492 int col = 0;
19493
19494 if (argvars[1].v_type != VAR_UNKNOWN)
19495 col = get_tv_number(&argvars[1]);
19496
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019497 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019498}
19499
19500/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019501 * "strwidth()" function
19502 */
19503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019504f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019505{
19506 char_u *s = get_tv_string(&argvars[0]);
19507
19508 rettv->vval.v_number = (varnumber_T)(
19509#ifdef FEAT_MBYTE
19510 mb_string2cells(s, -1)
19511#else
19512 STRLEN(s)
19513#endif
19514 );
19515}
19516
19517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518 * "strpart()" function
19519 */
19520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019521f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522{
19523 char_u *p;
19524 int n;
19525 int len;
19526 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019527 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019529 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 slen = (int)STRLEN(p);
19531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019532 n = get_tv_number_chk(&argvars[1], &error);
19533 if (error)
19534 len = 0;
19535 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019536 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537 else
19538 len = slen - n; /* default len: all bytes that are available. */
19539
19540 /*
19541 * Only return the overlap between the specified part and the actual
19542 * string.
19543 */
19544 if (n < 0)
19545 {
19546 len += n;
19547 n = 0;
19548 }
19549 else if (n > slen)
19550 n = slen;
19551 if (len < 0)
19552 len = 0;
19553 else if (n + len > slen)
19554 len = slen - n;
19555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556 rettv->v_type = VAR_STRING;
19557 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558}
19559
19560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019561 * "strridx()" function
19562 */
19563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019564f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019565{
19566 char_u buf[NUMBUFLEN];
19567 char_u *needle;
19568 char_u *haystack;
19569 char_u *rest;
19570 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019571 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019573 needle = get_tv_string_chk(&argvars[1]);
19574 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019575
19576 rettv->vval.v_number = -1;
19577 if (needle == NULL || haystack == NULL)
19578 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019579
19580 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019581 if (argvars[2].v_type != VAR_UNKNOWN)
19582 {
19583 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019584 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019585 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019586 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019587 }
19588 else
19589 end_idx = haystack_len;
19590
Bram Moolenaar0d660222005-01-07 21:51:51 +000019591 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019592 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019593 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019594 lastmatch = haystack + end_idx;
19595 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019596 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019597 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019598 for (rest = haystack; *rest != '\0'; ++rest)
19599 {
19600 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019601 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019602 break;
19603 lastmatch = rest;
19604 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019605 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019606
19607 if (lastmatch == NULL)
19608 rettv->vval.v_number = -1;
19609 else
19610 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19611}
19612
19613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 * "strtrans()" function
19615 */
19616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019617f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019619 rettv->v_type = VAR_STRING;
19620 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621}
19622
19623/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019624 * "submatch()" function
19625 */
19626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019627f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019628{
Bram Moolenaar41571762014-04-02 19:00:58 +020019629 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019630 int no;
19631 int retList = 0;
19632
19633 no = (int)get_tv_number_chk(&argvars[0], &error);
19634 if (error)
19635 return;
19636 error = FALSE;
19637 if (argvars[1].v_type != VAR_UNKNOWN)
19638 retList = get_tv_number_chk(&argvars[1], &error);
19639 if (error)
19640 return;
19641
19642 if (retList == 0)
19643 {
19644 rettv->v_type = VAR_STRING;
19645 rettv->vval.v_string = reg_submatch(no);
19646 }
19647 else
19648 {
19649 rettv->v_type = VAR_LIST;
19650 rettv->vval.v_list = reg_submatch_list(no);
19651 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019652}
19653
19654/*
19655 * "substitute()" function
19656 */
19657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019658f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019659{
19660 char_u patbuf[NUMBUFLEN];
19661 char_u subbuf[NUMBUFLEN];
19662 char_u flagsbuf[NUMBUFLEN];
19663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019664 char_u *str = get_tv_string_chk(&argvars[0]);
19665 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19666 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19667 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19668
Bram Moolenaar0d660222005-01-07 21:51:51 +000019669 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019670 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19671 rettv->vval.v_string = NULL;
19672 else
19673 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019674}
19675
19676/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019677 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019680f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681{
19682 int id = 0;
19683#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019684 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 long col;
19686 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019687 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019689 lnum = get_tv_lnum(argvars); /* -1 on type error */
19690 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19691 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019693 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019694 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019695 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696#endif
19697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019698 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699}
19700
19701/*
19702 * "synIDattr(id, what [, mode])" function
19703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019705f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706{
19707 char_u *p = NULL;
19708#ifdef FEAT_SYN_HL
19709 int id;
19710 char_u *what;
19711 char_u *mode;
19712 char_u modebuf[NUMBUFLEN];
19713 int modec;
19714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019715 id = get_tv_number(&argvars[0]);
19716 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019717 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019721 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 modec = 0; /* replace invalid with current */
19723 }
19724 else
19725 {
19726#ifdef FEAT_GUI
19727 if (gui.in_use)
19728 modec = 'g';
19729 else
19730#endif
19731 if (t_colors > 1)
19732 modec = 'c';
19733 else
19734 modec = 't';
19735 }
19736
19737
19738 switch (TOLOWER_ASC(what[0]))
19739 {
19740 case 'b':
19741 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19742 p = highlight_color(id, what, modec);
19743 else /* bold */
19744 p = highlight_has_attr(id, HL_BOLD, modec);
19745 break;
19746
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019747 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 p = highlight_color(id, what, modec);
19749 break;
19750
19751 case 'i':
19752 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19753 p = highlight_has_attr(id, HL_INVERSE, modec);
19754 else /* italic */
19755 p = highlight_has_attr(id, HL_ITALIC, modec);
19756 break;
19757
19758 case 'n': /* name */
19759 p = get_highlight_name(NULL, id - 1);
19760 break;
19761
19762 case 'r': /* reverse */
19763 p = highlight_has_attr(id, HL_INVERSE, modec);
19764 break;
19765
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019766 case 's':
19767 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19768 p = highlight_color(id, what, modec);
19769 else /* standout */
19770 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 break;
19772
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019773 case 'u':
19774 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19775 /* underline */
19776 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19777 else
19778 /* undercurl */
19779 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 break;
19781 }
19782
19783 if (p != NULL)
19784 p = vim_strsave(p);
19785#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019786 rettv->v_type = VAR_STRING;
19787 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788}
19789
19790/*
19791 * "synIDtrans(id)" function
19792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019794f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795{
19796 int id;
19797
19798#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019799 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800
19801 if (id > 0)
19802 id = syn_get_final_id(id);
19803 else
19804#endif
19805 id = 0;
19806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019807 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808}
19809
19810/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019811 * "synconcealed(lnum, col)" function
19812 */
19813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019814f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019815{
19816#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19817 long lnum;
19818 long col;
19819 int syntax_flags = 0;
19820 int cchar;
19821 int matchid = 0;
19822 char_u str[NUMBUFLEN];
19823#endif
19824
19825 rettv->v_type = VAR_LIST;
19826 rettv->vval.v_list = NULL;
19827
19828#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19829 lnum = get_tv_lnum(argvars); /* -1 on type error */
19830 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19831
19832 vim_memset(str, NUL, sizeof(str));
19833
19834 if (rettv_list_alloc(rettv) != FAIL)
19835 {
19836 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19837 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19838 && curwin->w_p_cole > 0)
19839 {
19840 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19841 syntax_flags = get_syntax_info(&matchid);
19842
19843 /* get the conceal character */
19844 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19845 {
19846 cchar = syn_get_sub_char();
19847 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19848 cchar = lcs_conceal;
19849 if (cchar != NUL)
19850 {
19851# ifdef FEAT_MBYTE
19852 if (has_mbyte)
19853 (*mb_char2bytes)(cchar, str);
19854 else
19855# endif
19856 str[0] = cchar;
19857 }
19858 }
19859 }
19860
19861 list_append_number(rettv->vval.v_list,
19862 (syntax_flags & HL_CONCEAL) != 0);
19863 /* -1 to auto-determine strlen */
19864 list_append_string(rettv->vval.v_list, str, -1);
19865 list_append_number(rettv->vval.v_list, matchid);
19866 }
19867#endif
19868}
19869
19870/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019871 * "synstack(lnum, col)" function
19872 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019874f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019875{
19876#ifdef FEAT_SYN_HL
19877 long lnum;
19878 long col;
19879 int i;
19880 int id;
19881#endif
19882
19883 rettv->v_type = VAR_LIST;
19884 rettv->vval.v_list = NULL;
19885
19886#ifdef FEAT_SYN_HL
19887 lnum = get_tv_lnum(argvars); /* -1 on type error */
19888 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19889
19890 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019891 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019892 && rettv_list_alloc(rettv) != FAIL)
19893 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019894 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019895 for (i = 0; ; ++i)
19896 {
19897 id = syn_get_stack_item(i);
19898 if (id < 0)
19899 break;
19900 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19901 break;
19902 }
19903 }
19904#endif
19905}
19906
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019908get_cmd_output_as_rettv(
19909 typval_T *argvars,
19910 typval_T *rettv,
19911 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019913 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019915 char_u *infile = NULL;
19916 char_u buf[NUMBUFLEN];
19917 int err = FALSE;
19918 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019919 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019920 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019922 rettv->v_type = VAR_STRING;
19923 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019924 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019925 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019926
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019927 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019928 {
19929 /*
19930 * Write the string to a temp file, to be used for input of the shell
19931 * command.
19932 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019933 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019934 {
19935 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019936 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019937 }
19938
19939 fd = mch_fopen((char *)infile, WRITEBIN);
19940 if (fd == NULL)
19941 {
19942 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019943 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019944 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019945 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019946 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019947 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19948 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019949 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019950 else
19951 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019952 size_t len;
19953
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019954 p = get_tv_string_buf_chk(&argvars[1], buf);
19955 if (p == NULL)
19956 {
19957 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019958 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019959 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019960 len = STRLEN(p);
19961 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019962 err = TRUE;
19963 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019964 if (fclose(fd) != 0)
19965 err = TRUE;
19966 if (err)
19967 {
19968 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019969 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019970 }
19971 }
19972
Bram Moolenaar52a72462014-08-29 15:53:52 +020019973 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19974 * echoes typeahead, that messes up the display. */
19975 if (!msg_silent)
19976 flags += SHELL_COOKED;
19977
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019978 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019980 int len;
19981 listitem_T *li;
19982 char_u *s = NULL;
19983 char_u *start;
19984 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019985 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986
Bram Moolenaar52a72462014-08-29 15:53:52 +020019987 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019988 if (res == NULL)
19989 goto errret;
19990
19991 list = list_alloc();
19992 if (list == NULL)
19993 goto errret;
19994
19995 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019997 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019998 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019999 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020000 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020001
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020002 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020003 if (s == NULL)
20004 goto errret;
20005
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020006 for (p = s; start < end; ++p, ++start)
20007 *p = *start == NUL ? NL : *start;
20008 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020009
20010 li = listitem_alloc();
20011 if (li == NULL)
20012 {
20013 vim_free(s);
20014 goto errret;
20015 }
20016 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020017 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020018 li->li_tv.vval.v_string = s;
20019 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020021
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020022 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020023 rettv->v_type = VAR_LIST;
20024 rettv->vval.v_list = list;
20025 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020027 else
20028 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020029 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020030#ifdef USE_CR
20031 /* translate <CR> into <NL> */
20032 if (res != NULL)
20033 {
20034 char_u *s;
20035
20036 for (s = res; *s; ++s)
20037 {
20038 if (*s == CAR)
20039 *s = NL;
20040 }
20041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042#else
20043# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020044 /* translate <CR><NL> into <NL> */
20045 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020047 char_u *s, *d;
20048
20049 d = res;
20050 for (s = res; *s; ++s)
20051 {
20052 if (s[0] == CAR && s[1] == NL)
20053 ++s;
20054 *d++ = *s;
20055 }
20056 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058# endif
20059#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020060 rettv->vval.v_string = res;
20061 res = NULL;
20062 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020063
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020064errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020065 if (infile != NULL)
20066 {
20067 mch_remove(infile);
20068 vim_free(infile);
20069 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020070 if (res != NULL)
20071 vim_free(res);
20072 if (list != NULL)
20073 list_free(list, TRUE);
20074}
20075
20076/*
20077 * "system()" function
20078 */
20079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020080f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020081{
20082 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20083}
20084
20085/*
20086 * "systemlist()" function
20087 */
20088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020089f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020090{
20091 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092}
20093
20094/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020095 * "tabpagebuflist()" function
20096 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020098f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020099{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020100#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020101 tabpage_T *tp;
20102 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020103
20104 if (argvars[0].v_type == VAR_UNKNOWN)
20105 wp = firstwin;
20106 else
20107 {
20108 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20109 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020110 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020111 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020112 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020113 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020114 for (; wp != NULL; wp = wp->w_next)
20115 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020116 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020117 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020118 }
20119#endif
20120}
20121
20122
20123/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020124 * "tabpagenr()" function
20125 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020127f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020128{
20129 int nr = 1;
20130#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020131 char_u *arg;
20132
20133 if (argvars[0].v_type != VAR_UNKNOWN)
20134 {
20135 arg = get_tv_string_chk(&argvars[0]);
20136 nr = 0;
20137 if (arg != NULL)
20138 {
20139 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020140 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020141 else
20142 EMSG2(_(e_invexpr2), arg);
20143 }
20144 }
20145 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020146 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020147#endif
20148 rettv->vval.v_number = nr;
20149}
20150
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020151
20152#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020153static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020154
20155/*
20156 * Common code for tabpagewinnr() and winnr().
20157 */
20158 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020159get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020160{
20161 win_T *twin;
20162 int nr = 1;
20163 win_T *wp;
20164 char_u *arg;
20165
20166 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20167 if (argvar->v_type != VAR_UNKNOWN)
20168 {
20169 arg = get_tv_string_chk(argvar);
20170 if (arg == NULL)
20171 nr = 0; /* type error; errmsg already given */
20172 else if (STRCMP(arg, "$") == 0)
20173 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20174 else if (STRCMP(arg, "#") == 0)
20175 {
20176 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20177 if (twin == NULL)
20178 nr = 0;
20179 }
20180 else
20181 {
20182 EMSG2(_(e_invexpr2), arg);
20183 nr = 0;
20184 }
20185 }
20186
20187 if (nr > 0)
20188 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20189 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020190 {
20191 if (wp == NULL)
20192 {
20193 /* didn't find it in this tabpage */
20194 nr = 0;
20195 break;
20196 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020197 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020198 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020199 return nr;
20200}
20201#endif
20202
20203/*
20204 * "tabpagewinnr()" function
20205 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020207f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020208{
20209 int nr = 1;
20210#ifdef FEAT_WINDOWS
20211 tabpage_T *tp;
20212
20213 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20214 if (tp == NULL)
20215 nr = 0;
20216 else
20217 nr = get_winnr(tp, &argvars[1]);
20218#endif
20219 rettv->vval.v_number = nr;
20220}
20221
20222
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020223/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020224 * "tagfiles()" function
20225 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020227f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020228{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020229 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020230 tagname_T tn;
20231 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020232
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020233 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020234 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020235 fname = alloc(MAXPATHL);
20236 if (fname == NULL)
20237 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020238
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020239 for (first = TRUE; ; first = FALSE)
20240 if (get_tagfname(&tn, first, fname) == FAIL
20241 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020242 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020243 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020244 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020245}
20246
20247/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020248 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020249 */
20250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020251f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020252{
20253 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020254
20255 tag_pattern = get_tv_string(&argvars[0]);
20256
20257 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020258 if (*tag_pattern == NUL)
20259 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020260
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020261 if (rettv_list_alloc(rettv) == OK)
20262 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020263}
20264
20265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 * "tempname()" function
20267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020269f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270{
20271 static int x = 'A';
20272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020273 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020274 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275
20276 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20277 * names. Skip 'I' and 'O', they are used for shell redirection. */
20278 do
20279 {
20280 if (x == 'Z')
20281 x = '0';
20282 else if (x == '9')
20283 x = 'A';
20284 else
20285 {
20286#ifdef EBCDIC
20287 if (x == 'I')
20288 x = 'J';
20289 else if (x == 'R')
20290 x = 'S';
20291 else
20292#endif
20293 ++x;
20294 }
20295 } while (x == 'I' || x == 'O');
20296}
20297
20298/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020299 * "test(list)" function: Just checking the walls...
20300 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020302f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020303{
20304 /* Used for unit testing. Change the code below to your liking. */
20305#if 0
20306 listitem_T *li;
20307 list_T *l;
20308 char_u *bad, *good;
20309
20310 if (argvars[0].v_type != VAR_LIST)
20311 return;
20312 l = argvars[0].vval.v_list;
20313 if (l == NULL)
20314 return;
20315 li = l->lv_first;
20316 if (li == NULL)
20317 return;
20318 bad = get_tv_string(&li->li_tv);
20319 li = li->li_next;
20320 if (li == NULL)
20321 return;
20322 good = get_tv_string(&li->li_tv);
20323 rettv->vval.v_number = test_edit_score(bad, good);
20324#endif
20325}
20326
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020327#ifdef FEAT_FLOAT
20328/*
20329 * "tan()" function
20330 */
20331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020332f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020333{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020334 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020335
20336 rettv->v_type = VAR_FLOAT;
20337 if (get_float_arg(argvars, &f) == OK)
20338 rettv->vval.v_float = tan(f);
20339 else
20340 rettv->vval.v_float = 0.0;
20341}
20342
20343/*
20344 * "tanh()" function
20345 */
20346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020347f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020348{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020349 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020350
20351 rettv->v_type = VAR_FLOAT;
20352 if (get_float_arg(argvars, &f) == OK)
20353 rettv->vval.v_float = tanh(f);
20354 else
20355 rettv->vval.v_float = 0.0;
20356}
20357#endif
20358
Bram Moolenaar975b5272016-03-15 23:10:59 +010020359#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20360/*
20361 * Get a callback from "arg". It can be a Funcref or a function name.
20362 * When "arg" is zero return an empty string.
20363 * Return NULL for an invalid argument.
20364 */
20365 char_u *
20366get_callback(typval_T *arg, partial_T **pp)
20367{
20368 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20369 {
20370 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020371 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020372 return (*pp)->pt_name;
20373 }
20374 *pp = NULL;
20375 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20376 return arg->vval.v_string;
20377 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20378 return (char_u *)"";
20379 EMSG(_("E921: Invalid callback argument"));
20380 return NULL;
20381}
20382#endif
20383
20384#ifdef FEAT_TIMERS
20385/*
20386 * "timer_start(time, callback [, options])" function
20387 */
20388 static void
20389f_timer_start(typval_T *argvars, typval_T *rettv)
20390{
20391 long msec = get_tv_number(&argvars[0]);
20392 timer_T *timer;
20393 int repeat = 0;
20394 char_u *callback;
20395 dict_T *dict;
20396
20397 if (argvars[2].v_type != VAR_UNKNOWN)
20398 {
20399 if (argvars[2].v_type != VAR_DICT
20400 || (dict = argvars[2].vval.v_dict) == NULL)
20401 {
20402 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20403 return;
20404 }
20405 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20406 repeat = get_dict_number(dict, (char_u *)"repeat");
20407 }
20408
20409 timer = create_timer(msec, repeat);
20410 callback = get_callback(&argvars[1], &timer->tr_partial);
20411 if (callback == NULL)
20412 {
20413 stop_timer(timer);
20414 rettv->vval.v_number = -1;
20415 }
20416 else
20417 {
20418 timer->tr_callback = vim_strsave(callback);
20419 rettv->vval.v_number = timer->tr_id;
20420 }
20421}
20422
20423/*
20424 * "timer_stop(timer)" function
20425 */
20426 static void
20427f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20428{
20429 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20430
20431 if (timer != NULL)
20432 stop_timer(timer);
20433}
20434#endif
20435
Bram Moolenaard52d9742005-08-21 22:20:28 +000020436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 * "tolower(string)" function
20438 */
20439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020440f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441{
20442 char_u *p;
20443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020444 p = vim_strsave(get_tv_string(&argvars[0]));
20445 rettv->v_type = VAR_STRING;
20446 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447
20448 if (p != NULL)
20449 while (*p != NUL)
20450 {
20451#ifdef FEAT_MBYTE
20452 int l;
20453
20454 if (enc_utf8)
20455 {
20456 int c, lc;
20457
20458 c = utf_ptr2char(p);
20459 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020460 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 /* TODO: reallocate string when byte count changes. */
20462 if (utf_char2len(lc) == l)
20463 utf_char2bytes(lc, p);
20464 p += l;
20465 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020466 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 p += l; /* skip multi-byte character */
20468 else
20469#endif
20470 {
20471 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20472 ++p;
20473 }
20474 }
20475}
20476
20477/*
20478 * "toupper(string)" function
20479 */
20480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020481f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020483 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020484 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485}
20486
20487/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020488 * "tr(string, fromstr, tostr)" function
20489 */
20490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020491f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020492{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020493 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020494 char_u *fromstr;
20495 char_u *tostr;
20496 char_u *p;
20497#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020498 int inlen;
20499 int fromlen;
20500 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020501 int idx;
20502 char_u *cpstr;
20503 int cplen;
20504 int first = TRUE;
20505#endif
20506 char_u buf[NUMBUFLEN];
20507 char_u buf2[NUMBUFLEN];
20508 garray_T ga;
20509
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020510 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020511 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20512 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020513
20514 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020515 rettv->v_type = VAR_STRING;
20516 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020517 if (fromstr == NULL || tostr == NULL)
20518 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020519 ga_init2(&ga, (int)sizeof(char), 80);
20520
20521#ifdef FEAT_MBYTE
20522 if (!has_mbyte)
20523#endif
20524 /* not multi-byte: fromstr and tostr must be the same length */
20525 if (STRLEN(fromstr) != STRLEN(tostr))
20526 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020527#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020528error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020529#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020530 EMSG2(_(e_invarg2), fromstr);
20531 ga_clear(&ga);
20532 return;
20533 }
20534
20535 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020536 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020537 {
20538#ifdef FEAT_MBYTE
20539 if (has_mbyte)
20540 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020541 inlen = (*mb_ptr2len)(in_str);
20542 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020543 cplen = inlen;
20544 idx = 0;
20545 for (p = fromstr; *p != NUL; p += fromlen)
20546 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020547 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020548 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020549 {
20550 for (p = tostr; *p != NUL; p += tolen)
20551 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020552 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020553 if (idx-- == 0)
20554 {
20555 cplen = tolen;
20556 cpstr = p;
20557 break;
20558 }
20559 }
20560 if (*p == NUL) /* tostr is shorter than fromstr */
20561 goto error;
20562 break;
20563 }
20564 ++idx;
20565 }
20566
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020567 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020568 {
20569 /* Check that fromstr and tostr have the same number of
20570 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020571 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020572 first = FALSE;
20573 for (p = tostr; *p != NUL; p += tolen)
20574 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020575 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020576 --idx;
20577 }
20578 if (idx != 0)
20579 goto error;
20580 }
20581
Bram Moolenaarcde88542015-08-11 19:14:00 +020020582 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020583 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020584 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020585
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020586 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020587 }
20588 else
20589#endif
20590 {
20591 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020592 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020593 if (p != NULL)
20594 ga_append(&ga, tostr[p - fromstr]);
20595 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020596 ga_append(&ga, *in_str);
20597 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020598 }
20599 }
20600
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020601 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020602 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020603 ga_append(&ga, NUL);
20604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020605 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020606}
20607
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020608#ifdef FEAT_FLOAT
20609/*
20610 * "trunc({float})" function
20611 */
20612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020613f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020614{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020615 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020616
20617 rettv->v_type = VAR_FLOAT;
20618 if (get_float_arg(argvars, &f) == OK)
20619 /* trunc() is not in C90, use floor() or ceil() instead. */
20620 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20621 else
20622 rettv->vval.v_float = 0.0;
20623}
20624#endif
20625
Bram Moolenaar8299df92004-07-10 09:47:34 +000020626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 * "type(expr)" function
20628 */
20629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020630f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020632 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020633
20634 switch (argvars[0].v_type)
20635 {
20636 case VAR_NUMBER: n = 0; break;
20637 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020638 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020639 case VAR_FUNC: n = 2; break;
20640 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020641 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020642 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020643 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020644 if (argvars[0].vval.v_number == VVAL_FALSE
20645 || argvars[0].vval.v_number == VVAL_TRUE)
20646 n = 6;
20647 else
20648 n = 7;
20649 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020650 case VAR_JOB: n = 8; break;
20651 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020652 case VAR_UNKNOWN:
20653 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20654 n = -1;
20655 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020656 }
20657 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658}
20659
20660/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020661 * "undofile(name)" function
20662 */
20663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020664f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020665{
20666 rettv->v_type = VAR_STRING;
20667#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020668 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020669 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020670
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020671 if (*fname == NUL)
20672 {
20673 /* If there is no file name there will be no undo file. */
20674 rettv->vval.v_string = NULL;
20675 }
20676 else
20677 {
20678 char_u *ffname = FullName_save(fname, FALSE);
20679
20680 if (ffname != NULL)
20681 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20682 vim_free(ffname);
20683 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020684 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020685#else
20686 rettv->vval.v_string = NULL;
20687#endif
20688}
20689
20690/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020691 * "undotree()" function
20692 */
20693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020694f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020695{
20696 if (rettv_dict_alloc(rettv) == OK)
20697 {
20698 dict_T *dict = rettv->vval.v_dict;
20699 list_T *list;
20700
Bram Moolenaar730cde92010-06-27 05:18:54 +020020701 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020702 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020703 dict_add_nr_str(dict, "save_last",
20704 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020705 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20706 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020707 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020708
20709 list = list_alloc();
20710 if (list != NULL)
20711 {
20712 u_eval_tree(curbuf->b_u_oldhead, list);
20713 dict_add_list(dict, "entries", list);
20714 }
20715 }
20716}
20717
20718/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020719 * "values(dict)" function
20720 */
20721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020722f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020723{
20724 dict_list(argvars, rettv, 1);
20725}
20726
20727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 * "virtcol(string)" function
20729 */
20730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020731f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732{
20733 colnr_T vcol = 0;
20734 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020735 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020737 fp = var2fpos(&argvars[0], FALSE, &fnum);
20738 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20739 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 {
20741 getvvcol(curwin, fp, NULL, NULL, &vcol);
20742 ++vcol;
20743 }
20744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020745 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746}
20747
20748/*
20749 * "visualmode()" function
20750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020752f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 char_u str[2];
20755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757 str[0] = curbuf->b_visual_mode_eval;
20758 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020759 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760
20761 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020762 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764}
20765
20766/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020767 * "wildmenumode()" function
20768 */
20769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020770f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020771{
20772#ifdef FEAT_WILDMENU
20773 if (wild_menu_showing)
20774 rettv->vval.v_number = 1;
20775#endif
20776}
20777
20778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 * "winbufnr(nr)" function
20780 */
20781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020782f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783{
20784 win_T *wp;
20785
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020786 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020790 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791}
20792
20793/*
20794 * "wincol()" function
20795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020797f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798{
20799 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020800 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801}
20802
20803/*
20804 * "winheight(nr)" function
20805 */
20806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020807f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808{
20809 win_T *wp;
20810
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020811 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020815 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816}
20817
20818/*
20819 * "winline()" function
20820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020822f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823{
20824 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020825 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826}
20827
20828/*
20829 * "winnr()" function
20830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020832f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833{
20834 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020835
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020837 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020839 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840}
20841
20842/*
20843 * "winrestcmd()" function
20844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020846f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847{
20848#ifdef FEAT_WINDOWS
20849 win_T *wp;
20850 int winnr = 1;
20851 garray_T ga;
20852 char_u buf[50];
20853
20854 ga_init2(&ga, (int)sizeof(char), 70);
20855 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20856 {
20857 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20858 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20860 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 ++winnr;
20862 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020863 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020865 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020867 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020869 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870}
20871
20872/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020873 * "winrestview()" function
20874 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020876f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020877{
20878 dict_T *dict;
20879
20880 if (argvars[0].v_type != VAR_DICT
20881 || (dict = argvars[0].vval.v_dict) == NULL)
20882 EMSG(_(e_invarg));
20883 else
20884 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020885 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20886 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20887 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20888 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020889#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020890 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20891 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020892#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020893 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20894 {
20895 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20896 curwin->w_set_curswant = FALSE;
20897 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020898
Bram Moolenaar82c25852014-05-28 16:47:16 +020020899 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20900 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020901#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020902 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20903 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020904#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020905 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20906 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20907 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20908 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020909
20910 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020911 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020912# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020913 win_new_width(curwin, W_WIDTH(curwin));
20914# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020915 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020916
Bram Moolenaarb851a962014-10-31 15:45:52 +010020917 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020918 curwin->w_topline = 1;
20919 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20920 curwin->w_topline = curbuf->b_ml.ml_line_count;
20921#ifdef FEAT_DIFF
20922 check_topfill(curwin, TRUE);
20923#endif
20924 }
20925}
20926
20927/*
20928 * "winsaveview()" function
20929 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020931f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020932{
20933 dict_T *dict;
20934
Bram Moolenaara800b422010-06-27 01:15:55 +020020935 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020936 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020937 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020938
20939 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20940 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20941#ifdef FEAT_VIRTUALEDIT
20942 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20943#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020944 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020945 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20946
20947 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20948#ifdef FEAT_DIFF
20949 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20950#endif
20951 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20952 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20953}
20954
20955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 * "winwidth(nr)" function
20957 */
20958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020959f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960{
20961 win_T *wp;
20962
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020963 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020967#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020968 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020970 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971#endif
20972}
20973
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020975 * "wordcount()" function
20976 */
20977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020978f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020979{
20980 if (rettv_dict_alloc(rettv) == FAIL)
20981 return;
20982 cursor_pos_info(rettv->vval.v_dict);
20983}
20984
20985/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020986 * Write list of strings to file
20987 */
20988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020989write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020990{
20991 listitem_T *li;
20992 int c;
20993 int ret = OK;
20994 char_u *s;
20995
20996 for (li = list->lv_first; li != NULL; li = li->li_next)
20997 {
20998 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20999 {
21000 if (*s == '\n')
21001 c = putc(NUL, fd);
21002 else
21003 c = putc(*s, fd);
21004 if (c == EOF)
21005 {
21006 ret = FAIL;
21007 break;
21008 }
21009 }
21010 if (!binary || li->li_next != NULL)
21011 if (putc('\n', fd) == EOF)
21012 {
21013 ret = FAIL;
21014 break;
21015 }
21016 if (ret == FAIL)
21017 {
21018 EMSG(_(e_write));
21019 break;
21020 }
21021 }
21022 return ret;
21023}
21024
21025/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021026 * "writefile()" function
21027 */
21028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021030{
21031 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021032 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021033 char_u *fname;
21034 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021035 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021036
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021037 if (check_restricted() || check_secure())
21038 return;
21039
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021040 if (argvars[0].v_type != VAR_LIST)
21041 {
21042 EMSG2(_(e_listarg), "writefile()");
21043 return;
21044 }
21045 if (argvars[0].vval.v_list == NULL)
21046 return;
21047
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021048 if (argvars[2].v_type != VAR_UNKNOWN)
21049 {
21050 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21051 binary = TRUE;
21052 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21053 append = TRUE;
21054 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021055
21056 /* Always open the file in binary mode, library functions have a mind of
21057 * their own about CR-LF conversion. */
21058 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021059 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21060 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021061 {
21062 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21063 ret = -1;
21064 }
21065 else
21066 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021067 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21068 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021069 fclose(fd);
21070 }
21071
21072 rettv->vval.v_number = ret;
21073}
21074
21075/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021076 * "xor(expr, expr)" function
21077 */
21078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021079f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021080{
21081 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21082 ^ get_tv_number_chk(&argvars[1], NULL);
21083}
21084
21085
21086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021088 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 */
21090 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021091var2fpos(
21092 typval_T *varp,
21093 int dollar_lnum, /* TRUE when $ is last line */
21094 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021096 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021098 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099
Bram Moolenaara5525202006-03-02 22:52:09 +000021100 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021101 if (varp->v_type == VAR_LIST)
21102 {
21103 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021104 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021105 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021106 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021107
21108 l = varp->vval.v_list;
21109 if (l == NULL)
21110 return NULL;
21111
21112 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021113 pos.lnum = list_find_nr(l, 0L, &error);
21114 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021115 return NULL; /* invalid line number */
21116
21117 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021118 pos.col = list_find_nr(l, 1L, &error);
21119 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021120 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021121 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021122
21123 /* We accept "$" for the column number: last column. */
21124 li = list_find(l, 1L);
21125 if (li != NULL && li->li_tv.v_type == VAR_STRING
21126 && li->li_tv.vval.v_string != NULL
21127 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21128 pos.col = len + 1;
21129
Bram Moolenaara5525202006-03-02 22:52:09 +000021130 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021131 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021132 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021133 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021134
Bram Moolenaara5525202006-03-02 22:52:09 +000021135#ifdef FEAT_VIRTUALEDIT
21136 /* Get the virtual offset. Defaults to zero. */
21137 pos.coladd = list_find_nr(l, 2L, &error);
21138 if (error)
21139 pos.coladd = 0;
21140#endif
21141
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021142 return &pos;
21143 }
21144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021145 name = get_tv_string_chk(varp);
21146 if (name == NULL)
21147 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021148 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021150 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21151 {
21152 if (VIsual_active)
21153 return &VIsual;
21154 return &curwin->w_cursor;
21155 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021156 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021158 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21160 return NULL;
21161 return pp;
21162 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021163
21164#ifdef FEAT_VIRTUALEDIT
21165 pos.coladd = 0;
21166#endif
21167
Bram Moolenaar477933c2007-07-17 14:32:23 +000021168 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021169 {
21170 pos.col = 0;
21171 if (name[1] == '0') /* "w0": first visible line */
21172 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021173 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021174 pos.lnum = curwin->w_topline;
21175 return &pos;
21176 }
21177 else if (name[1] == '$') /* "w$": last visible line */
21178 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021179 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021180 pos.lnum = curwin->w_botline - 1;
21181 return &pos;
21182 }
21183 }
21184 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021186 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 {
21188 pos.lnum = curbuf->b_ml.ml_line_count;
21189 pos.col = 0;
21190 }
21191 else
21192 {
21193 pos.lnum = curwin->w_cursor.lnum;
21194 pos.col = (colnr_T)STRLEN(ml_get_curline());
21195 }
21196 return &pos;
21197 }
21198 return NULL;
21199}
21200
21201/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021202 * Convert list in "arg" into a position and optional file number.
21203 * When "fnump" is NULL there is no file number, only 3 items.
21204 * Note that the column is passed on as-is, the caller may want to decrement
21205 * it to use 1 for the first column.
21206 * Return FAIL when conversion is not possible, doesn't check the position for
21207 * validity.
21208 */
21209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210list2fpos(
21211 typval_T *arg,
21212 pos_T *posp,
21213 int *fnump,
21214 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021215{
21216 list_T *l = arg->vval.v_list;
21217 long i = 0;
21218 long n;
21219
Bram Moolenaar493c1782014-05-28 14:34:46 +020021220 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21221 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021222 if (arg->v_type != VAR_LIST
21223 || l == NULL
21224 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021225 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021226 return FAIL;
21227
21228 if (fnump != NULL)
21229 {
21230 n = list_find_nr(l, i++, NULL); /* fnum */
21231 if (n < 0)
21232 return FAIL;
21233 if (n == 0)
21234 n = curbuf->b_fnum; /* current buffer */
21235 *fnump = n;
21236 }
21237
21238 n = list_find_nr(l, i++, NULL); /* lnum */
21239 if (n < 0)
21240 return FAIL;
21241 posp->lnum = n;
21242
21243 n = list_find_nr(l, i++, NULL); /* col */
21244 if (n < 0)
21245 return FAIL;
21246 posp->col = n;
21247
21248#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021249 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021250 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021251 posp->coladd = 0;
21252 else
21253 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021254#endif
21255
Bram Moolenaar493c1782014-05-28 14:34:46 +020021256 if (curswantp != NULL)
21257 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21258
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021259 return OK;
21260}
21261
21262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 * Get the length of an environment variable name.
21264 * Advance "arg" to the first character after the name.
21265 * Return 0 for error.
21266 */
21267 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021268get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269{
21270 char_u *p;
21271 int len;
21272
21273 for (p = *arg; vim_isIDc(*p); ++p)
21274 ;
21275 if (p == *arg) /* no name found */
21276 return 0;
21277
21278 len = (int)(p - *arg);
21279 *arg = p;
21280 return len;
21281}
21282
21283/*
21284 * Get the length of the name of a function or internal variable.
21285 * "arg" is advanced to the first non-white character after the name.
21286 * Return 0 if something is wrong.
21287 */
21288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021289get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290{
21291 char_u *p;
21292 int len;
21293
21294 /* Find the end of the name. */
21295 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021296 {
21297 if (*p == ':')
21298 {
21299 /* "s:" is start of "s:var", but "n:" is not and can be used in
21300 * slice "[n:]". Also "xx:" is not a namespace. */
21301 len = (int)(p - *arg);
21302 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21303 || len > 1)
21304 break;
21305 }
21306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 if (p == *arg) /* no name found */
21308 return 0;
21309
21310 len = (int)(p - *arg);
21311 *arg = skipwhite(p);
21312
21313 return len;
21314}
21315
21316/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021317 * Get the length of the name of a variable or function.
21318 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021320 * Return -1 if curly braces expansion failed.
21321 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 * If the name contains 'magic' {}'s, expand them and return the
21323 * expanded name in an allocated string via 'alias' - caller must free.
21324 */
21325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021326get_name_len(
21327 char_u **arg,
21328 char_u **alias,
21329 int evaluate,
21330 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331{
21332 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 char_u *p;
21334 char_u *expr_start;
21335 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336
21337 *alias = NULL; /* default to no alias */
21338
21339 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21340 && (*arg)[2] == (int)KE_SNR)
21341 {
21342 /* hard coded <SNR>, already translated */
21343 *arg += 3;
21344 return get_id_len(arg) + 3;
21345 }
21346 len = eval_fname_script(*arg);
21347 if (len > 0)
21348 {
21349 /* literal "<SID>", "s:" or "<SNR>" */
21350 *arg += len;
21351 }
21352
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021354 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021356 p = find_name_end(*arg, &expr_start, &expr_end,
21357 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 if (expr_start != NULL)
21359 {
21360 char_u *temp_string;
21361
21362 if (!evaluate)
21363 {
21364 len += (int)(p - *arg);
21365 *arg = skipwhite(p);
21366 return len;
21367 }
21368
21369 /*
21370 * Include any <SID> etc in the expanded string:
21371 * Thus the -len here.
21372 */
21373 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21374 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021375 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 *alias = temp_string;
21377 *arg = skipwhite(p);
21378 return (int)STRLEN(temp_string);
21379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380
21381 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021382 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 EMSG2(_(e_invexpr2), *arg);
21384
21385 return len;
21386}
21387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021388/*
21389 * Find the end of a variable or function name, taking care of magic braces.
21390 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21391 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021392 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021393 * Return a pointer to just after the name. Equal to "arg" if there is no
21394 * valid name.
21395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021397find_name_end(
21398 char_u *arg,
21399 char_u **expr_start,
21400 char_u **expr_end,
21401 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021403 int mb_nest = 0;
21404 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021406 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021408 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021410 *expr_start = NULL;
21411 *expr_end = NULL;
21412 }
21413
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021414 /* Quick check for valid starting character. */
21415 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21416 return arg;
21417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021418 for (p = arg; *p != NUL
21419 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021420 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021421 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021422 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021423 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021424 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021425 if (*p == '\'')
21426 {
21427 /* skip over 'string' to avoid counting [ and ] inside it. */
21428 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21429 ;
21430 if (*p == NUL)
21431 break;
21432 }
21433 else if (*p == '"')
21434 {
21435 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21436 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21437 if (*p == '\\' && p[1] != NUL)
21438 ++p;
21439 if (*p == NUL)
21440 break;
21441 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021442 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21443 {
21444 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021445 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021446 len = (int)(p - arg);
21447 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021448 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021449 break;
21450 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021452 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021454 if (*p == '[')
21455 ++br_nest;
21456 else if (*p == ']')
21457 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021460 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021462 if (*p == '{')
21463 {
21464 mb_nest++;
21465 if (expr_start != NULL && *expr_start == NULL)
21466 *expr_start = p;
21467 }
21468 else if (*p == '}')
21469 {
21470 mb_nest--;
21471 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21472 *expr_end = p;
21473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 }
21476
21477 return p;
21478}
21479
21480/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021481 * Expands out the 'magic' {}'s in a variable/function name.
21482 * Note that this can call itself recursively, to deal with
21483 * constructs like foo{bar}{baz}{bam}
21484 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21485 * "in_start" ^
21486 * "expr_start" ^
21487 * "expr_end" ^
21488 * "in_end" ^
21489 *
21490 * Returns a new allocated string, which the caller must free.
21491 * Returns NULL for failure.
21492 */
21493 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021494make_expanded_name(
21495 char_u *in_start,
21496 char_u *expr_start,
21497 char_u *expr_end,
21498 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021499{
21500 char_u c1;
21501 char_u *retval = NULL;
21502 char_u *temp_result;
21503 char_u *nextcmd = NULL;
21504
21505 if (expr_end == NULL || in_end == NULL)
21506 return NULL;
21507 *expr_start = NUL;
21508 *expr_end = NUL;
21509 c1 = *in_end;
21510 *in_end = NUL;
21511
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021512 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021513 if (temp_result != NULL && nextcmd == NULL)
21514 {
21515 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21516 + (in_end - expr_end) + 1));
21517 if (retval != NULL)
21518 {
21519 STRCPY(retval, in_start);
21520 STRCAT(retval, temp_result);
21521 STRCAT(retval, expr_end + 1);
21522 }
21523 }
21524 vim_free(temp_result);
21525
21526 *in_end = c1; /* put char back for error messages */
21527 *expr_start = '{';
21528 *expr_end = '}';
21529
21530 if (retval != NULL)
21531 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021532 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021533 if (expr_start != NULL)
21534 {
21535 /* Further expansion! */
21536 temp_result = make_expanded_name(retval, expr_start,
21537 expr_end, temp_result);
21538 vim_free(retval);
21539 retval = temp_result;
21540 }
21541 }
21542
21543 return retval;
21544}
21545
21546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021548 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549 */
21550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021551eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021553 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21554}
21555
21556/*
21557 * Return TRUE if character "c" can be used as the first character in a
21558 * variable or function name (excluding '{' and '}').
21559 */
21560 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021561eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021562{
21563 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564}
21565
21566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 * Set number v: variable to "val".
21568 */
21569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021570set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021572 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573}
21574
21575/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021576 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 */
21578 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021579get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021581 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582}
21583
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021584/*
21585 * Get string v: variable value. Uses a static buffer, can only be used once.
21586 */
21587 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021588get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021589{
21590 return get_tv_string(&vimvars[idx].vv_tv);
21591}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021592
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021594 * Get List v: variable value. Caller must take care of reference count when
21595 * needed.
21596 */
21597 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021598get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021599{
21600 return vimvars[idx].vv_list;
21601}
21602
21603/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021604 * Set v:char to character "c".
21605 */
21606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021607set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021608{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021609 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021610
21611#ifdef FEAT_MBYTE
21612 if (has_mbyte)
21613 buf[(*mb_char2bytes)(c, buf)] = NUL;
21614 else
21615#endif
21616 {
21617 buf[0] = c;
21618 buf[1] = NUL;
21619 }
21620 set_vim_var_string(VV_CHAR, buf, -1);
21621}
21622
21623/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021624 * Set v:count to "count" and v:count1 to "count1".
21625 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 */
21627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021628set_vcount(
21629 long count,
21630 long count1,
21631 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021633 if (set_prevcount)
21634 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021635 vimvars[VV_COUNT].vv_nr = count;
21636 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637}
21638
21639/*
21640 * Set string v: variable to a copy of "val".
21641 */
21642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021643set_vim_var_string(
21644 int idx,
21645 char_u *val,
21646 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647{
Bram Moolenaara542c682016-01-31 16:28:04 +010021648 clear_tv(&vimvars[idx].vv_di.di_tv);
21649 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021651 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021653 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021655 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656}
21657
21658/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021659 * Set List v: variable to "val".
21660 */
21661 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021662set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021663{
Bram Moolenaara542c682016-01-31 16:28:04 +010021664 clear_tv(&vimvars[idx].vv_di.di_tv);
21665 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021666 vimvars[idx].vv_list = val;
21667 if (val != NULL)
21668 ++val->lv_refcount;
21669}
21670
21671/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021672 * Set Dictionary v: variable to "val".
21673 */
21674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021675set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021676{
21677 int todo;
21678 hashitem_T *hi;
21679
Bram Moolenaara542c682016-01-31 16:28:04 +010021680 clear_tv(&vimvars[idx].vv_di.di_tv);
21681 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021682 vimvars[idx].vv_dict = val;
21683 if (val != NULL)
21684 {
21685 ++val->dv_refcount;
21686
21687 /* Set readonly */
21688 todo = (int)val->dv_hashtab.ht_used;
21689 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21690 {
21691 if (HASHITEM_EMPTY(hi))
21692 continue;
21693 --todo;
21694 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21695 }
21696 }
21697}
21698
21699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 * Set v:register if needed.
21701 */
21702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021703set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704{
21705 char_u regname;
21706
21707 if (c == 0 || c == ' ')
21708 regname = '"';
21709 else
21710 regname = c;
21711 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021712 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 set_vim_var_string(VV_REG, &regname, 1);
21714}
21715
21716/*
21717 * Get or set v:exception. If "oldval" == NULL, return the current value.
21718 * Otherwise, restore the value to "oldval" and return NULL.
21719 * Must always be called in pairs to save and restore v:exception! Does not
21720 * take care of memory allocations.
21721 */
21722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021723v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724{
21725 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021726 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727
Bram Moolenaare9a41262005-01-15 22:18:47 +000021728 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 return NULL;
21730}
21731
21732/*
21733 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21734 * Otherwise, restore the value to "oldval" and return NULL.
21735 * Must always be called in pairs to save and restore v:throwpoint! Does not
21736 * take care of memory allocations.
21737 */
21738 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021739v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740{
21741 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021742 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743
Bram Moolenaare9a41262005-01-15 22:18:47 +000021744 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 return NULL;
21746}
21747
21748#if defined(FEAT_AUTOCMD) || defined(PROTO)
21749/*
21750 * Set v:cmdarg.
21751 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21752 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21753 * Must always be called in pairs!
21754 */
21755 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021756set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757{
21758 char_u *oldval;
21759 char_u *newval;
21760 unsigned len;
21761
Bram Moolenaare9a41262005-01-15 22:18:47 +000021762 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021763 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021765 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021766 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021767 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 }
21769
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021770 if (eap->force_bin == FORCE_BIN)
21771 len = 6;
21772 else if (eap->force_bin == FORCE_NOBIN)
21773 len = 8;
21774 else
21775 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021776
21777 if (eap->read_edit)
21778 len += 7;
21779
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021780 if (eap->force_ff != 0)
21781 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21782# ifdef FEAT_MBYTE
21783 if (eap->force_enc != 0)
21784 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021785 if (eap->bad_char != 0)
21786 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021787# endif
21788
21789 newval = alloc(len + 1);
21790 if (newval == NULL)
21791 return NULL;
21792
21793 if (eap->force_bin == FORCE_BIN)
21794 sprintf((char *)newval, " ++bin");
21795 else if (eap->force_bin == FORCE_NOBIN)
21796 sprintf((char *)newval, " ++nobin");
21797 else
21798 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021799
21800 if (eap->read_edit)
21801 STRCAT(newval, " ++edit");
21802
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021803 if (eap->force_ff != 0)
21804 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21805 eap->cmd + eap->force_ff);
21806# ifdef FEAT_MBYTE
21807 if (eap->force_enc != 0)
21808 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21809 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021810 if (eap->bad_char == BAD_KEEP)
21811 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21812 else if (eap->bad_char == BAD_DROP)
21813 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21814 else if (eap->bad_char != 0)
21815 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021816# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021817 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021818 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819}
21820#endif
21821
21822/*
21823 * Get the value of internal variable "name".
21824 * Return OK or FAIL.
21825 */
21826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021827get_var_tv(
21828 char_u *name,
21829 int len, /* length of "name" */
21830 typval_T *rettv, /* NULL when only checking existence */
21831 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21832 int verbose, /* may give error message */
21833 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834{
21835 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021836 typval_T *tv = NULL;
21837 typval_T atv;
21838 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840
21841 /* truncate the name, so that we can use strcmp() */
21842 cc = name[len];
21843 name[len] = NUL;
21844
21845 /*
21846 * Check for "b:changedtick".
21847 */
21848 if (STRCMP(name, "b:changedtick") == 0)
21849 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021850 atv.v_type = VAR_NUMBER;
21851 atv.vval.v_number = curbuf->b_changedtick;
21852 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 }
21854
21855 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 * Check for user-defined variables.
21857 */
21858 else
21859 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021860 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021862 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021864 if (dip != NULL)
21865 *dip = v;
21866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 }
21868
Bram Moolenaare9a41262005-01-15 22:18:47 +000021869 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021871 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021872 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 ret = FAIL;
21874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021875 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021876 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877
21878 name[len] = cc;
21879
21880 return ret;
21881}
21882
21883/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021884 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21885 * Also handle function call with Funcref variable: func(expr)
21886 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21887 */
21888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021889handle_subscript(
21890 char_u **arg,
21891 typval_T *rettv,
21892 int evaluate, /* do more than finding the end */
21893 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021894{
21895 int ret = OK;
21896 dict_T *selfdict = NULL;
21897 char_u *s;
21898 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021899 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021900
21901 while (ret == OK
21902 && (**arg == '['
21903 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021904 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21905 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021906 && !vim_iswhite(*(*arg - 1)))
21907 {
21908 if (**arg == '(')
21909 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021910 partial_T *pt = NULL;
21911
Bram Moolenaard9fba312005-06-26 22:34:35 +000021912 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021913 if (evaluate)
21914 {
21915 functv = *rettv;
21916 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021917
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021918 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021919 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021920 {
21921 pt = functv.vval.v_partial;
21922 s = pt->pt_name;
21923 }
21924 else
21925 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021926 }
21927 else
21928 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021929 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021930 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021931 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021932
21933 /* Clear the funcref afterwards, so that deleting it while
21934 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021935 if (evaluate)
21936 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021937
21938 /* Stop the expression evaluation when immediately aborting on
21939 * error, or when an interrupt occurred or an exception was thrown
21940 * but not caught. */
21941 if (aborting())
21942 {
21943 if (ret == OK)
21944 clear_tv(rettv);
21945 ret = FAIL;
21946 }
21947 dict_unref(selfdict);
21948 selfdict = NULL;
21949 }
21950 else /* **arg == '[' || **arg == '.' */
21951 {
21952 dict_unref(selfdict);
21953 if (rettv->v_type == VAR_DICT)
21954 {
21955 selfdict = rettv->vval.v_dict;
21956 if (selfdict != NULL)
21957 ++selfdict->dv_refcount;
21958 }
21959 else
21960 selfdict = NULL;
21961 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21962 {
21963 clear_tv(rettv);
21964 ret = FAIL;
21965 }
21966 }
21967 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021968
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021969 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21970 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021971 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021972 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21973 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021974 char_u *tofree = NULL;
21975 ufunc_T *fp;
21976 char_u fname_buf[FLEN_FIXED + 1];
21977 int error;
21978
21979 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021980 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021981 fp = find_func(fname);
21982 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021983
21984 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021985 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021986 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021987 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21988
21989 if (pt != NULL)
21990 {
21991 pt->pt_refcount = 1;
21992 pt->pt_dict = selfdict;
21993 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021994 if (rettv->v_type == VAR_FUNC)
21995 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021996 /* Just a function: Take over the function name and use
21997 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021998 pt->pt_name = rettv->vval.v_string;
21999 }
22000 else
22001 {
22002 partial_T *ret_pt = rettv->vval.v_partial;
22003 int i;
22004
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022005 /* Partial: copy the function name, use selfdict and copy
22006 * args. Can't take over name or args, the partial might
22007 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022008 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022009 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022010 if (ret_pt->pt_argc > 0)
22011 {
22012 pt->pt_argv = (typval_T *)alloc(
22013 sizeof(typval_T) * ret_pt->pt_argc);
22014 if (pt->pt_argv == NULL)
22015 /* out of memory: drop the arguments */
22016 pt->pt_argc = 0;
22017 else
22018 {
22019 pt->pt_argc = ret_pt->pt_argc;
22020 for (i = 0; i < pt->pt_argc; i++)
22021 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22022 }
22023 }
22024 partial_unref(ret_pt);
22025 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022026 rettv->v_type = VAR_PARTIAL;
22027 rettv->vval.v_partial = pt;
22028 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022029 }
22030 }
22031
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022032 dict_unref(selfdict);
22033 return ret;
22034}
22035
22036/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022037 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022038 * value).
22039 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022040 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022041alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022042{
Bram Moolenaar33570922005-01-25 22:26:29 +000022043 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022044}
22045
22046/*
22047 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 * The string "s" must have been allocated, it is consumed.
22049 * Return NULL for out of memory, the variable otherwise.
22050 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022051 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022052alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053{
Bram Moolenaar33570922005-01-25 22:26:29 +000022054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022056 rettv = alloc_tv();
22057 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022059 rettv->v_type = VAR_STRING;
22060 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 }
22062 else
22063 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022064 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065}
22066
22067/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022068 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022071free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072{
22073 if (varp != NULL)
22074 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022075 switch (varp->v_type)
22076 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022077 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022078 func_unref(varp->vval.v_string);
22079 /*FALLTHROUGH*/
22080 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022081 vim_free(varp->vval.v_string);
22082 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022083 case VAR_PARTIAL:
22084 partial_unref(varp->vval.v_partial);
22085 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022086 case VAR_LIST:
22087 list_unref(varp->vval.v_list);
22088 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022089 case VAR_DICT:
22090 dict_unref(varp->vval.v_dict);
22091 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022092 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022093#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022094 job_unref(varp->vval.v_job);
22095 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022096#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022097 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022098#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022099 channel_unref(varp->vval.v_channel);
22100 break;
22101#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022102 case VAR_NUMBER:
22103 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022104 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022105 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022106 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 vim_free(varp);
22109 }
22110}
22111
22112/*
22113 * Free the memory for a variable value and set the value to NULL or 0.
22114 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022116clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117{
22118 if (varp != NULL)
22119 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022120 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022122 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022123 func_unref(varp->vval.v_string);
22124 /*FALLTHROUGH*/
22125 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022126 vim_free(varp->vval.v_string);
22127 varp->vval.v_string = NULL;
22128 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022129 case VAR_PARTIAL:
22130 partial_unref(varp->vval.v_partial);
22131 varp->vval.v_partial = NULL;
22132 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022133 case VAR_LIST:
22134 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022135 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022136 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022137 case VAR_DICT:
22138 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022139 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022140 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022141 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022142 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022143 varp->vval.v_number = 0;
22144 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022145 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022146#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022147 varp->vval.v_float = 0.0;
22148 break;
22149#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022150 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022151#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022152 job_unref(varp->vval.v_job);
22153 varp->vval.v_job = NULL;
22154#endif
22155 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022156 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022157#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022158 channel_unref(varp->vval.v_channel);
22159 varp->vval.v_channel = NULL;
22160#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022161 case VAR_UNKNOWN:
22162 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022164 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 }
22166}
22167
22168/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022169 * Set the value of a variable to NULL without freeing items.
22170 */
22171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022172init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022173{
22174 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022175 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022176}
22177
22178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 * Get the number value of a variable.
22180 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022181 * For incompatible types, return 0.
22182 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22183 * caller of incompatible types: it sets *denote to TRUE if "denote"
22184 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022186 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022187get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022189 int error = FALSE;
22190
22191 return get_tv_number_chk(varp, &error); /* return 0L on error */
22192}
22193
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022194 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022195get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022196{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022197 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022199 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022201 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022202 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022203 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022204#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022205 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022206 break;
22207#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022208 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022209 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022210 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022211 break;
22212 case VAR_STRING:
22213 if (varp->vval.v_string != NULL)
22214 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022215 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022216 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022217 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022218 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022219 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022220 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022221 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022222 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022223 case VAR_SPECIAL:
22224 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22225 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022226 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022227#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022228 EMSG(_("E910: Using a Job as a Number"));
22229 break;
22230#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022231 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022232#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022233 EMSG(_("E913: Using a Channel as a Number"));
22234 break;
22235#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022236 case VAR_UNKNOWN:
22237 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022240 if (denote == NULL) /* useful for values that must be unsigned */
22241 n = -1;
22242 else
22243 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022244 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245}
22246
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022247#ifdef FEAT_FLOAT
22248 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022249get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022250{
22251 switch (varp->v_type)
22252 {
22253 case VAR_NUMBER:
22254 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022255 case VAR_FLOAT:
22256 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022257 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022258 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022259 EMSG(_("E891: Using a Funcref as a Float"));
22260 break;
22261 case VAR_STRING:
22262 EMSG(_("E892: Using a String as a Float"));
22263 break;
22264 case VAR_LIST:
22265 EMSG(_("E893: Using a List as a Float"));
22266 break;
22267 case VAR_DICT:
22268 EMSG(_("E894: Using a Dictionary as a Float"));
22269 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022270 case VAR_SPECIAL:
22271 EMSG(_("E907: Using a special value as a Float"));
22272 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022273 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022274# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022275 EMSG(_("E911: Using a Job as a Float"));
22276 break;
22277# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022278 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022279# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022280 EMSG(_("E914: Using a Channel as a Float"));
22281 break;
22282# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022283 case VAR_UNKNOWN:
22284 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022285 break;
22286 }
22287 return 0;
22288}
22289#endif
22290
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022292 * Get the lnum from the first argument.
22293 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022294 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 */
22296 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022297get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298{
Bram Moolenaar33570922005-01-25 22:26:29 +000022299 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 linenr_T lnum;
22301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022302 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 if (lnum == 0) /* no valid number, try using line() */
22304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022305 rettv.v_type = VAR_NUMBER;
22306 f_line(argvars, &rettv);
22307 lnum = rettv.vval.v_number;
22308 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 }
22310 return lnum;
22311}
22312
22313/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022314 * Get the lnum from the first argument.
22315 * Also accepts "$", then "buf" is used.
22316 * Returns 0 on error.
22317 */
22318 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022319get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022320{
22321 if (argvars[0].v_type == VAR_STRING
22322 && argvars[0].vval.v_string != NULL
22323 && argvars[0].vval.v_string[0] == '$'
22324 && buf != NULL)
22325 return buf->b_ml.ml_line_count;
22326 return get_tv_number_chk(&argvars[0], NULL);
22327}
22328
22329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 * Get the string value of a variable.
22331 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022332 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22333 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 * If the String variable has never been set, return an empty string.
22335 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022336 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22337 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022340get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341{
22342 static char_u mybuf[NUMBUFLEN];
22343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022344 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345}
22346
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022347 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022348get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022350 char_u *res = get_tv_string_buf_chk(varp, buf);
22351
22352 return res != NULL ? res : (char_u *)"";
22353}
22354
Bram Moolenaar7d647822014-04-05 21:28:56 +020022355/*
22356 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22357 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022359get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022360{
22361 static char_u mybuf[NUMBUFLEN];
22362
22363 return get_tv_string_buf_chk(varp, mybuf);
22364}
22365
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022366 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022367get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022368{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022369 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022371 case VAR_NUMBER:
22372 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22373 return buf;
22374 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022375 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022376 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022377 break;
22378 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022379 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022380 break;
22381 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022382 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022383 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022384 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022385#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022386 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022387 break;
22388#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022389 case VAR_STRING:
22390 if (varp->vval.v_string != NULL)
22391 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022392 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022393 case VAR_SPECIAL:
22394 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22395 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022396 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022397#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022398 {
22399 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022400 char *status;
22401
22402 if (job == NULL)
22403 return (char_u *)"no process";
22404 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022405 : job->jv_status == JOB_ENDED ? "dead"
22406 : "run";
22407# ifdef UNIX
22408 vim_snprintf((char *)buf, NUMBUFLEN,
22409 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022410# elif defined(WIN32)
22411 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022412 "process %ld %s",
22413 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022414 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022415# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022416 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022417 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22418# endif
22419 return buf;
22420 }
22421#endif
22422 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022423 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022424#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022425 {
22426 channel_T *channel = varp->vval.v_channel;
22427 char *status = channel_status(channel);
22428
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022429 if (channel == NULL)
22430 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22431 else
22432 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022433 "channel %d %s", channel->ch_id, status);
22434 return buf;
22435 }
22436#endif
22437 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022438 case VAR_UNKNOWN:
22439 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022442 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443}
22444
22445/*
22446 * Find variable "name" in the list of variables.
22447 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022448 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022449 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022452 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022453find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457
Bram Moolenaara7043832005-01-21 11:56:39 +000022458 ht = find_var_ht(name, &varname);
22459 if (htp != NULL)
22460 *htp = ht;
22461 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022463 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464}
22465
22466/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022467 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022468 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022470 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022471find_var_in_ht(
22472 hashtab_T *ht,
22473 int htname,
22474 char_u *varname,
22475 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022476{
Bram Moolenaar33570922005-01-25 22:26:29 +000022477 hashitem_T *hi;
22478
22479 if (*varname == NUL)
22480 {
22481 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022482 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022483 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022484 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022485 case 'g': return &globvars_var;
22486 case 'v': return &vimvars_var;
22487 case 'b': return &curbuf->b_bufvar;
22488 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022489#ifdef FEAT_WINDOWS
22490 case 't': return &curtab->tp_winvar;
22491#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022492 case 'l': return current_funccal == NULL
22493 ? NULL : &current_funccal->l_vars_var;
22494 case 'a': return current_funccal == NULL
22495 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022496 }
22497 return NULL;
22498 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022499
22500 hi = hash_find(ht, varname);
22501 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022502 {
22503 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022504 * worked find the variable again. Don't auto-load a script if it was
22505 * loaded already, otherwise it would be loaded every time when
22506 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022507 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022508 {
22509 /* Note: script_autoload() may make "hi" invalid. It must either
22510 * be obtained again or not used. */
22511 if (!script_autoload(varname, FALSE) || aborting())
22512 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022513 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022514 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022515 if (HASHITEM_EMPTY(hi))
22516 return NULL;
22517 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022518 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022519}
22520
22521/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022522 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022523 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022524 * Set "varname" to the start of name without ':'.
22525 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022527find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022529 hashitem_T *hi;
22530
Bram Moolenaar73627d02015-08-11 15:46:09 +020022531 if (name[0] == NUL)
22532 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 if (name[1] != ':')
22534 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022535 /* The name must not start with a colon or #. */
22536 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 return NULL;
22538 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022539
22540 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022541 hi = hash_find(&compat_hashtab, name);
22542 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022543 return &compat_hashtab;
22544
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022546 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022547 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 }
22549 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022550 if (*name == 'g') /* global variable */
22551 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022552 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22553 */
22554 if (vim_strchr(name + 2, ':') != NULL
22555 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022556 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022558 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022560 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022561#ifdef FEAT_WINDOWS
22562 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022563 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022564#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022565 if (*name == 'v') /* v: variable */
22566 return &vimvarht;
22567 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022568 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022569 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022570 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 if (*name == 's' /* script variable */
22572 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22573 return &SCRIPT_VARS(current_SID);
22574 return NULL;
22575}
22576
22577/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022578 * Get function call environment based on bactrace debug level
22579 */
22580 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022581get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022582{
22583 int i;
22584 funccall_T *funccal;
22585 funccall_T *temp_funccal;
22586
22587 funccal = current_funccal;
22588 if (debug_backtrace_level > 0)
22589 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022590 for (i = 0; i < debug_backtrace_level; i++)
22591 {
22592 temp_funccal = funccal->caller;
22593 if (temp_funccal)
22594 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022595 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022596 /* backtrace level overflow. reset to max */
22597 debug_backtrace_level = i;
22598 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022599 }
22600 return funccal;
22601}
22602
22603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022605 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606 * Returns NULL when it doesn't exist.
22607 */
22608 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022609get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610{
Bram Moolenaar33570922005-01-25 22:26:29 +000022611 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022613 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 if (v == NULL)
22615 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022616 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617}
22618
22619/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621 * sourcing this script and when executing functions defined in the script.
22622 */
22623 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022624new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625{
Bram Moolenaara7043832005-01-21 11:56:39 +000022626 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022627 hashtab_T *ht;
22628 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022629
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22631 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022632 /* Re-allocating ga_data means that an ht_array pointing to
22633 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022635 for (i = 1; i <= ga_scripts.ga_len; ++i)
22636 {
22637 ht = &SCRIPT_VARS(i);
22638 if (ht->ht_mask == HT_INIT_SIZE - 1)
22639 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022640 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022641 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022642 }
22643
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 while (ga_scripts.ga_len < id)
22645 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022646 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022647 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022648 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 }
22651 }
22652}
22653
22654/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22656 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657 */
22658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022659init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660{
Bram Moolenaar33570922005-01-25 22:26:29 +000022661 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022662 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022663 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022664 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022665 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022666 dict_var->di_tv.vval.v_dict = dict;
22667 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022668 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022669 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22670 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671}
22672
22673/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022674 * Unreference a dictionary initialized by init_var_dict().
22675 */
22676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022677unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022678{
22679 /* Now the dict needs to be freed if no one else is using it, go back to
22680 * normal reference counting. */
22681 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22682 dict_unref(dict);
22683}
22684
22685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022687 * Frees all allocated variables and the value they contain.
22688 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 */
22690 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022691vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022692{
22693 vars_clear_ext(ht, TRUE);
22694}
22695
22696/*
22697 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22698 */
22699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022700vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701{
Bram Moolenaara7043832005-01-21 11:56:39 +000022702 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 hashitem_T *hi;
22704 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705
Bram Moolenaar33570922005-01-25 22:26:29 +000022706 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022707 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022708 for (hi = ht->ht_array; todo > 0; ++hi)
22709 {
22710 if (!HASHITEM_EMPTY(hi))
22711 {
22712 --todo;
22713
Bram Moolenaar33570922005-01-25 22:26:29 +000022714 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022715 * ht_array might change then. hash_clear() takes care of it
22716 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 v = HI2DI(hi);
22718 if (free_val)
22719 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022720 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022721 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022722 }
22723 }
22724 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022725 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726}
22727
Bram Moolenaara7043832005-01-21 11:56:39 +000022728/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022729 * Delete a variable from hashtab "ht" at item "hi".
22730 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022733delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734{
Bram Moolenaar33570922005-01-25 22:26:29 +000022735 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022736
22737 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 clear_tv(&di->di_tv);
22739 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740}
22741
22742/*
22743 * List the value of one internal variable.
22744 */
22745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022746list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022748 char_u *tofree;
22749 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022750 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022751
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022752 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022754 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022755 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756}
22757
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022759list_one_var_a(
22760 char_u *prefix,
22761 char_u *name,
22762 int type,
22763 char_u *string,
22764 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765{
Bram Moolenaar31859182007-08-14 20:41:13 +000022766 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22767 msg_start();
22768 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 if (name != NULL) /* "a:" vars don't have a name stored */
22770 msg_puts(name);
22771 msg_putchar(' ');
22772 msg_advance(22);
22773 if (type == VAR_NUMBER)
22774 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022775 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022776 msg_putchar('*');
22777 else if (type == VAR_LIST)
22778 {
22779 msg_putchar('[');
22780 if (*string == '[')
22781 ++string;
22782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022783 else if (type == VAR_DICT)
22784 {
22785 msg_putchar('{');
22786 if (*string == '{')
22787 ++string;
22788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 else
22790 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022791
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022793
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022794 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022795 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022796 if (*first)
22797 {
22798 msg_clr_eos();
22799 *first = FALSE;
22800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801}
22802
22803/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022804 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805 * If the variable already exists, the value is updated.
22806 * Otherwise the variable is created.
22807 */
22808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022809set_var(
22810 char_u *name,
22811 typval_T *tv,
22812 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813{
Bram Moolenaar33570922005-01-25 22:26:29 +000022814 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022816 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022818 ht = find_var_ht(name, &varname);
22819 if (ht == NULL || *varname == NUL)
22820 {
22821 EMSG2(_(e_illvar), name);
22822 return;
22823 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022824 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022825
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022826 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22827 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022828 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022829
Bram Moolenaar33570922005-01-25 22:26:29 +000022830 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022832 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022833 if (var_check_ro(v->di_flags, name, FALSE)
22834 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022835 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022836
22837 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022838 * Handle setting internal v: variables separately where needed to
22839 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022840 */
22841 if (ht == &vimvarht)
22842 {
22843 if (v->di_tv.v_type == VAR_STRING)
22844 {
22845 vim_free(v->di_tv.vval.v_string);
22846 if (copy || tv->v_type != VAR_STRING)
22847 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22848 else
22849 {
22850 /* Take over the string to avoid an extra alloc/free. */
22851 v->di_tv.vval.v_string = tv->vval.v_string;
22852 tv->vval.v_string = NULL;
22853 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022854 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022855 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022856 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022857 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022859 if (STRCMP(varname, "searchforward") == 0)
22860 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022861#ifdef FEAT_SEARCH_EXTRA
22862 else if (STRCMP(varname, "hlsearch") == 0)
22863 {
22864 no_hlsearch = !v->di_tv.vval.v_number;
22865 redraw_all_later(SOME_VALID);
22866 }
22867#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022868 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022869 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022870 else if (v->di_tv.v_type != tv->v_type)
22871 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022872 }
22873
22874 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 }
22876 else /* add a new variable */
22877 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022878 /* Can't add "v:" variable. */
22879 if (ht == &vimvarht)
22880 {
22881 EMSG2(_(e_illvar), name);
22882 return;
22883 }
22884
Bram Moolenaar92124a32005-06-17 22:03:40 +000022885 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022886 if (!valid_varname(varname))
22887 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022888
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022889 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22890 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022891 if (v == NULL)
22892 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022893 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022894 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022896 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 return;
22898 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022899 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022901
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022902 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022903 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022904 else
22905 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022906 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022907 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022908 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910}
22911
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022912/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022913 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022914 * Also give an error message.
22915 */
22916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022917var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022918{
22919 if (flags & DI_FLAGS_RO)
22920 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022921 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022922 return TRUE;
22923 }
22924 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22925 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022926 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022927 return TRUE;
22928 }
22929 return FALSE;
22930}
22931
22932/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022933 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22934 * Also give an error message.
22935 */
22936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022937var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022938{
22939 if (flags & DI_FLAGS_FIX)
22940 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022941 EMSG2(_("E795: Cannot delete variable %s"),
22942 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022943 return TRUE;
22944 }
22945 return FALSE;
22946}
22947
22948/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022949 * Check if a funcref is assigned to a valid variable name.
22950 * Return TRUE and give an error if not.
22951 */
22952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022953var_check_func_name(
22954 char_u *name, /* points to start of variable name */
22955 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022956{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022957 /* Allow for w: b: s: and t:. */
22958 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022959 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22960 ? name[2] : name[0]))
22961 {
22962 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22963 name);
22964 return TRUE;
22965 }
22966 /* Don't allow hiding a function. When "v" is not NULL we might be
22967 * assigning another function to the same var, the type is checked
22968 * below. */
22969 if (new_var && function_exists(name))
22970 {
22971 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22972 name);
22973 return TRUE;
22974 }
22975 return FALSE;
22976}
22977
22978/*
22979 * Check if a variable name is valid.
22980 * Return FALSE and give an error if not.
22981 */
22982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022983valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022984{
22985 char_u *p;
22986
22987 for (p = varname; *p != NUL; ++p)
22988 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22989 && *p != AUTOLOAD_CHAR)
22990 {
22991 EMSG2(_(e_illvar), varname);
22992 return FALSE;
22993 }
22994 return TRUE;
22995}
22996
22997/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022998 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022999 * Also give an error message, using "name" or _("name") when use_gettext is
23000 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023001 */
23002 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023003tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023004{
23005 if (lock & VAR_LOCKED)
23006 {
23007 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023008 name == NULL ? (char_u *)_("Unknown")
23009 : use_gettext ? (char_u *)_(name)
23010 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023011 return TRUE;
23012 }
23013 if (lock & VAR_FIXED)
23014 {
23015 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023016 name == NULL ? (char_u *)_("Unknown")
23017 : use_gettext ? (char_u *)_(name)
23018 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023019 return TRUE;
23020 }
23021 return FALSE;
23022}
23023
23024/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023026 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023027 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023028 * It is OK for "from" and "to" to point to the same item. This is used to
23029 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023030 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023032copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023034 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023035 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023036 switch (from->v_type)
23037 {
23038 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023039 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023040 to->vval.v_number = from->vval.v_number;
23041 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023042 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023043#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023044 to->vval.v_float = from->vval.v_float;
23045 break;
23046#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023047 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023048#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023049 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023050 if (to->vval.v_job != NULL)
23051 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023052 break;
23053#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023054 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023055#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023056 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023057 if (to->vval.v_channel != NULL)
23058 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023059 break;
23060#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023061 case VAR_STRING:
23062 case VAR_FUNC:
23063 if (from->vval.v_string == NULL)
23064 to->vval.v_string = NULL;
23065 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023066 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023067 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023068 if (from->v_type == VAR_FUNC)
23069 func_ref(to->vval.v_string);
23070 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023071 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023072 case VAR_PARTIAL:
23073 if (from->vval.v_partial == NULL)
23074 to->vval.v_partial = NULL;
23075 else
23076 {
23077 to->vval.v_partial = from->vval.v_partial;
23078 ++to->vval.v_partial->pt_refcount;
23079 }
23080 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023081 case VAR_LIST:
23082 if (from->vval.v_list == NULL)
23083 to->vval.v_list = NULL;
23084 else
23085 {
23086 to->vval.v_list = from->vval.v_list;
23087 ++to->vval.v_list->lv_refcount;
23088 }
23089 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023090 case VAR_DICT:
23091 if (from->vval.v_dict == NULL)
23092 to->vval.v_dict = NULL;
23093 else
23094 {
23095 to->vval.v_dict = from->vval.v_dict;
23096 ++to->vval.v_dict->dv_refcount;
23097 }
23098 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023099 case VAR_UNKNOWN:
23100 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023101 break;
23102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103}
23104
23105/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023106 * Make a copy of an item.
23107 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023108 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23109 * reference to an already copied list/dict can be used.
23110 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023111 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023113item_copy(
23114 typval_T *from,
23115 typval_T *to,
23116 int deep,
23117 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023118{
23119 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023120 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023121
Bram Moolenaar33570922005-01-25 22:26:29 +000023122 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023123 {
23124 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023125 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023126 }
23127 ++recurse;
23128
23129 switch (from->v_type)
23130 {
23131 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023132 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023133 case VAR_STRING:
23134 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023135 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023136 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023137 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023138 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023139 copy_tv(from, to);
23140 break;
23141 case VAR_LIST:
23142 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023143 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023144 if (from->vval.v_list == NULL)
23145 to->vval.v_list = NULL;
23146 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23147 {
23148 /* use the copy made earlier */
23149 to->vval.v_list = from->vval.v_list->lv_copylist;
23150 ++to->vval.v_list->lv_refcount;
23151 }
23152 else
23153 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23154 if (to->vval.v_list == NULL)
23155 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023156 break;
23157 case VAR_DICT:
23158 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023159 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023160 if (from->vval.v_dict == NULL)
23161 to->vval.v_dict = NULL;
23162 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23163 {
23164 /* use the copy made earlier */
23165 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23166 ++to->vval.v_dict->dv_refcount;
23167 }
23168 else
23169 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23170 if (to->vval.v_dict == NULL)
23171 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023172 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023173 case VAR_UNKNOWN:
23174 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023175 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023176 }
23177 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023178 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023179}
23180
23181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 * ":echo expr1 ..." print each argument separated with a space, add a
23183 * newline at the end.
23184 * ":echon expr1 ..." print each argument plain.
23185 */
23186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023187ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188{
23189 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023190 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023191 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 char_u *p;
23193 int needclr = TRUE;
23194 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023195 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196
23197 if (eap->skip)
23198 ++emsg_skip;
23199 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23200 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023201 /* If eval1() causes an error message the text from the command may
23202 * still need to be cleared. E.g., "echo 22,44". */
23203 need_clr_eos = needclr;
23204
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023206 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 {
23208 /*
23209 * Report the invalid expression unless the expression evaluation
23210 * has been cancelled due to an aborting error, an interrupt, or an
23211 * exception.
23212 */
23213 if (!aborting())
23214 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023215 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 break;
23217 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023218 need_clr_eos = FALSE;
23219
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 if (!eap->skip)
23221 {
23222 if (atstart)
23223 {
23224 atstart = FALSE;
23225 /* Call msg_start() after eval1(), evaluating the expression
23226 * may cause a message to appear. */
23227 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023228 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023229 /* Mark the saved text as finishing the line, so that what
23230 * follows is displayed on a new line when scrolling back
23231 * at the more prompt. */
23232 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 }
23236 else if (eap->cmdidx == CMD_echo)
23237 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023238 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023239 if (p != NULL)
23240 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023242 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023244 if (*p != TAB && needclr)
23245 {
23246 /* remove any text still there from the command */
23247 msg_clr_eos();
23248 needclr = FALSE;
23249 }
23250 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251 }
23252 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023253 {
23254#ifdef FEAT_MBYTE
23255 if (has_mbyte)
23256 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023257 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023258
23259 (void)msg_outtrans_len_attr(p, i, echo_attr);
23260 p += i - 1;
23261 }
23262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023264 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023267 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023269 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 arg = skipwhite(arg);
23271 }
23272 eap->nextcmd = check_nextcmd(arg);
23273
23274 if (eap->skip)
23275 --emsg_skip;
23276 else
23277 {
23278 /* remove text that may still be there from the command */
23279 if (needclr)
23280 msg_clr_eos();
23281 if (eap->cmdidx == CMD_echo)
23282 msg_end();
23283 }
23284}
23285
23286/*
23287 * ":echohl {name}".
23288 */
23289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023290ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291{
23292 int id;
23293
23294 id = syn_name2id(eap->arg);
23295 if (id == 0)
23296 echo_attr = 0;
23297 else
23298 echo_attr = syn_id2attr(id);
23299}
23300
23301/*
23302 * ":execute expr1 ..." execute the result of an expression.
23303 * ":echomsg expr1 ..." Print a message
23304 * ":echoerr expr1 ..." Print an error
23305 * Each gets spaces around each argument and a newline at the end for
23306 * echo commands
23307 */
23308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023309ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310{
23311 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023312 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 int ret = OK;
23314 char_u *p;
23315 garray_T ga;
23316 int len;
23317 int save_did_emsg;
23318
23319 ga_init2(&ga, 1, 80);
23320
23321 if (eap->skip)
23322 ++emsg_skip;
23323 while (*arg != NUL && *arg != '|' && *arg != '\n')
23324 {
23325 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023326 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 {
23328 /*
23329 * Report the invalid expression unless the expression evaluation
23330 * has been cancelled due to an aborting error, an interrupt, or an
23331 * exception.
23332 */
23333 if (!aborting())
23334 EMSG2(_(e_invexpr2), p);
23335 ret = FAIL;
23336 break;
23337 }
23338
23339 if (!eap->skip)
23340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023341 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342 len = (int)STRLEN(p);
23343 if (ga_grow(&ga, len + 2) == FAIL)
23344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023345 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 ret = FAIL;
23347 break;
23348 }
23349 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 ga.ga_len += len;
23353 }
23354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023355 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 arg = skipwhite(arg);
23357 }
23358
23359 if (ret != FAIL && ga.ga_data != NULL)
23360 {
23361 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023364 out_flush();
23365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 else if (eap->cmdidx == CMD_echoerr)
23367 {
23368 /* We don't want to abort following commands, restore did_emsg. */
23369 save_did_emsg = did_emsg;
23370 EMSG((char_u *)ga.ga_data);
23371 if (!force_abort)
23372 did_emsg = save_did_emsg;
23373 }
23374 else if (eap->cmdidx == CMD_execute)
23375 do_cmdline((char_u *)ga.ga_data,
23376 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23377 }
23378
23379 ga_clear(&ga);
23380
23381 if (eap->skip)
23382 --emsg_skip;
23383
23384 eap->nextcmd = check_nextcmd(arg);
23385}
23386
23387/*
23388 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23389 * "arg" points to the "&" or '+' when called, to "option" when returning.
23390 * Returns NULL when no option name found. Otherwise pointer to the char
23391 * after the option name.
23392 */
23393 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023394find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395{
23396 char_u *p = *arg;
23397
23398 ++p;
23399 if (*p == 'g' && p[1] == ':')
23400 {
23401 *opt_flags = OPT_GLOBAL;
23402 p += 2;
23403 }
23404 else if (*p == 'l' && p[1] == ':')
23405 {
23406 *opt_flags = OPT_LOCAL;
23407 p += 2;
23408 }
23409 else
23410 *opt_flags = 0;
23411
23412 if (!ASCII_ISALPHA(*p))
23413 return NULL;
23414 *arg = p;
23415
23416 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23417 p += 4; /* termcap option */
23418 else
23419 while (ASCII_ISALPHA(*p))
23420 ++p;
23421 return p;
23422}
23423
23424/*
23425 * ":function"
23426 */
23427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023428ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429{
23430 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023431 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 int j;
23433 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023435 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436 char_u *name = NULL;
23437 char_u *p;
23438 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023439 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440 garray_T newargs;
23441 garray_T newlines;
23442 int varargs = FALSE;
23443 int mustend = FALSE;
23444 int flags = 0;
23445 ufunc_T *fp;
23446 int indent;
23447 int nesting;
23448 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023449 dictitem_T *v;
23450 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023451 static int func_nr = 0; /* number for nameless function */
23452 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023453 hashtab_T *ht;
23454 int todo;
23455 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023456 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457
23458 /*
23459 * ":function" without argument: list functions.
23460 */
23461 if (ends_excmd(*eap->arg))
23462 {
23463 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023464 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023465 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023466 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023467 {
23468 if (!HASHITEM_EMPTY(hi))
23469 {
23470 --todo;
23471 fp = HI2UF(hi);
23472 if (!isdigit(*fp->uf_name))
23473 list_func_head(fp, FALSE);
23474 }
23475 }
23476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 eap->nextcmd = check_nextcmd(eap->arg);
23478 return;
23479 }
23480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023481 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023482 * ":function /pat": list functions matching pattern.
23483 */
23484 if (*eap->arg == '/')
23485 {
23486 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23487 if (!eap->skip)
23488 {
23489 regmatch_T regmatch;
23490
23491 c = *p;
23492 *p = NUL;
23493 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23494 *p = c;
23495 if (regmatch.regprog != NULL)
23496 {
23497 regmatch.rm_ic = p_ic;
23498
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023499 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023500 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23501 {
23502 if (!HASHITEM_EMPTY(hi))
23503 {
23504 --todo;
23505 fp = HI2UF(hi);
23506 if (!isdigit(*fp->uf_name)
23507 && vim_regexec(&regmatch, fp->uf_name, 0))
23508 list_func_head(fp, FALSE);
23509 }
23510 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023511 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023512 }
23513 }
23514 if (*p == '/')
23515 ++p;
23516 eap->nextcmd = check_nextcmd(p);
23517 return;
23518 }
23519
23520 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023521 * Get the function name. There are these situations:
23522 * func normal function name
23523 * "name" == func, "fudi.fd_dict" == NULL
23524 * dict.func new dictionary entry
23525 * "name" == NULL, "fudi.fd_dict" set,
23526 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23527 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023528 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023529 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23530 * dict.func existing dict entry that's not a Funcref
23531 * "name" == NULL, "fudi.fd_dict" set,
23532 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023533 * s:func script-local function name
23534 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023537 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023538 paren = (vim_strchr(p, '(') != NULL);
23539 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 {
23541 /*
23542 * Return on an invalid expression in braces, unless the expression
23543 * evaluation has been cancelled due to an aborting error, an
23544 * interrupt, or an exception.
23545 */
23546 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023547 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023548 if (!eap->skip && fudi.fd_newkey != NULL)
23549 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023550 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553 else
23554 eap->skip = TRUE;
23555 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023556
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 /* An error in a function call during evaluation of an expression in magic
23558 * braces should not cause the function not to be defined. */
23559 saved_did_emsg = did_emsg;
23560 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561
23562 /*
23563 * ":function func" with only function name: list function.
23564 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023565 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 {
23567 if (!ends_excmd(*skipwhite(p)))
23568 {
23569 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023570 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 }
23572 eap->nextcmd = check_nextcmd(p);
23573 if (eap->nextcmd != NULL)
23574 *p = NUL;
23575 if (!eap->skip && !got_int)
23576 {
23577 fp = find_func(name);
23578 if (fp != NULL)
23579 {
23580 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023581 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023583 if (FUNCLINE(fp, j) == NULL)
23584 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585 msg_putchar('\n');
23586 msg_outnum((long)(j + 1));
23587 if (j < 9)
23588 msg_putchar(' ');
23589 if (j < 99)
23590 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023591 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 out_flush(); /* show a line at a time */
23593 ui_breakcheck();
23594 }
23595 if (!got_int)
23596 {
23597 msg_putchar('\n');
23598 msg_puts((char_u *)" endfunction");
23599 }
23600 }
23601 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023602 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023604 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605 }
23606
23607 /*
23608 * ":function name(arg1, arg2)" Define function.
23609 */
23610 p = skipwhite(p);
23611 if (*p != '(')
23612 {
23613 if (!eap->skip)
23614 {
23615 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023616 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 }
23618 /* attempt to continue by skipping some text */
23619 if (vim_strchr(p, '(') != NULL)
23620 p = vim_strchr(p, '(');
23621 }
23622 p = skipwhite(p + 1);
23623
23624 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23625 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23626
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023627 if (!eap->skip)
23628 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023629 /* Check the name of the function. Unless it's a dictionary function
23630 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023631 if (name != NULL)
23632 arg = name;
23633 else
23634 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023635 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023636 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23637 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023638 {
23639 if (*arg == K_SPECIAL)
23640 j = 3;
23641 else
23642 j = 0;
23643 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23644 : eval_isnamec(arg[j])))
23645 ++j;
23646 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023647 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023648 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023649 /* Disallow using the g: dict. */
23650 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23651 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023652 }
23653
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 /*
23655 * Isolate the arguments: "arg1, arg2, ...)"
23656 */
23657 while (*p != ')')
23658 {
23659 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23660 {
23661 varargs = TRUE;
23662 p += 3;
23663 mustend = TRUE;
23664 }
23665 else
23666 {
23667 arg = p;
23668 while (ASCII_ISALNUM(*p) || *p == '_')
23669 ++p;
23670 if (arg == p || isdigit(*arg)
23671 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23672 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23673 {
23674 if (!eap->skip)
23675 EMSG2(_("E125: Illegal argument: %s"), arg);
23676 break;
23677 }
23678 if (ga_grow(&newargs, 1) == FAIL)
23679 goto erret;
23680 c = *p;
23681 *p = NUL;
23682 arg = vim_strsave(arg);
23683 if (arg == NULL)
23684 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023685
23686 /* Check for duplicate argument name. */
23687 for (i = 0; i < newargs.ga_len; ++i)
23688 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23689 {
23690 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023691 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023692 goto erret;
23693 }
23694
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23696 *p = c;
23697 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 if (*p == ',')
23699 ++p;
23700 else
23701 mustend = TRUE;
23702 }
23703 p = skipwhite(p);
23704 if (mustend && *p != ')')
23705 {
23706 if (!eap->skip)
23707 EMSG2(_(e_invarg2), eap->arg);
23708 break;
23709 }
23710 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023711 if (*p != ')')
23712 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713 ++p; /* skip the ')' */
23714
Bram Moolenaare9a41262005-01-15 22:18:47 +000023715 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 for (;;)
23717 {
23718 p = skipwhite(p);
23719 if (STRNCMP(p, "range", 5) == 0)
23720 {
23721 flags |= FC_RANGE;
23722 p += 5;
23723 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023724 else if (STRNCMP(p, "dict", 4) == 0)
23725 {
23726 flags |= FC_DICT;
23727 p += 4;
23728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 else if (STRNCMP(p, "abort", 5) == 0)
23730 {
23731 flags |= FC_ABORT;
23732 p += 5;
23733 }
23734 else
23735 break;
23736 }
23737
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023738 /* When there is a line break use what follows for the function body.
23739 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23740 if (*p == '\n')
23741 line_arg = p + 1;
23742 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 EMSG(_(e_trailing));
23744
23745 /*
23746 * Read the body of the function, until ":endfunction" is found.
23747 */
23748 if (KeyTyped)
23749 {
23750 /* Check if the function already exists, don't let the user type the
23751 * whole function before telling him it doesn't work! For a script we
23752 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023753 if (!eap->skip && !eap->forceit)
23754 {
23755 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23756 EMSG(_(e_funcdict));
23757 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023758 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023761 if (!eap->skip && did_emsg)
23762 goto erret;
23763
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 msg_putchar('\n'); /* don't overwrite the function name */
23765 cmdline_row = msg_row;
23766 }
23767
23768 indent = 2;
23769 nesting = 0;
23770 for (;;)
23771 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023772 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023773 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023774 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023775 saved_wait_return = FALSE;
23776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023778 sourcing_lnum_off = sourcing_lnum;
23779
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023780 if (line_arg != NULL)
23781 {
23782 /* Use eap->arg, split up in parts by line breaks. */
23783 theline = line_arg;
23784 p = vim_strchr(theline, '\n');
23785 if (p == NULL)
23786 line_arg += STRLEN(line_arg);
23787 else
23788 {
23789 *p = NUL;
23790 line_arg = p + 1;
23791 }
23792 }
23793 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 theline = getcmdline(':', 0L, indent);
23795 else
23796 theline = eap->getline(':', eap->cookie, indent);
23797 if (KeyTyped)
23798 lines_left = Rows - 1;
23799 if (theline == NULL)
23800 {
23801 EMSG(_("E126: Missing :endfunction"));
23802 goto erret;
23803 }
23804
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023805 /* Detect line continuation: sourcing_lnum increased more than one. */
23806 if (sourcing_lnum > sourcing_lnum_off + 1)
23807 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23808 else
23809 sourcing_lnum_off = 0;
23810
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 if (skip_until != NULL)
23812 {
23813 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23814 * don't check for ":endfunc". */
23815 if (STRCMP(theline, skip_until) == 0)
23816 {
23817 vim_free(skip_until);
23818 skip_until = NULL;
23819 }
23820 }
23821 else
23822 {
23823 /* skip ':' and blanks*/
23824 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23825 ;
23826
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023827 /* Check for "endfunction". */
23828 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023830 if (line_arg == NULL)
23831 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832 break;
23833 }
23834
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023835 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836 * at "end". */
23837 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23838 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023839 else if (STRNCMP(p, "if", 2) == 0
23840 || STRNCMP(p, "wh", 2) == 0
23841 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 || STRNCMP(p, "try", 3) == 0)
23843 indent += 2;
23844
23845 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023846 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023848 if (*p == '!')
23849 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023851 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023852 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023854 ++nesting;
23855 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 }
23857 }
23858
23859 /* Check for ":append" or ":insert". */
23860 p = skip_range(p, NULL);
23861 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23862 || (p[0] == 'i'
23863 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23864 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23865 skip_until = vim_strsave((char_u *)".");
23866
23867 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23868 arg = skipwhite(skiptowhite(p));
23869 if (arg[0] == '<' && arg[1] =='<'
23870 && ((p[0] == 'p' && p[1] == 'y'
23871 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23872 || (p[0] == 'p' && p[1] == 'e'
23873 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23874 || (p[0] == 't' && p[1] == 'c'
23875 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023876 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23877 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23879 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023880 || (p[0] == 'm' && p[1] == 'z'
23881 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 ))
23883 {
23884 /* ":python <<" continues until a dot, like ":append" */
23885 p = skipwhite(arg + 2);
23886 if (*p == NUL)
23887 skip_until = vim_strsave((char_u *)".");
23888 else
23889 skip_until = vim_strsave(p);
23890 }
23891 }
23892
23893 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023894 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023895 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023896 if (line_arg == NULL)
23897 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023899 }
23900
23901 /* Copy the line to newly allocated memory. get_one_sourceline()
23902 * allocates 250 bytes per line, this saves 80% on average. The cost
23903 * is an extra alloc/free. */
23904 p = vim_strsave(theline);
23905 if (p != NULL)
23906 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023907 if (line_arg == NULL)
23908 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023909 theline = p;
23910 }
23911
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023912 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23913
23914 /* Add NULL lines for continuation lines, so that the line count is
23915 * equal to the index in the growarray. */
23916 while (sourcing_lnum_off-- > 0)
23917 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023918
23919 /* Check for end of eap->arg. */
23920 if (line_arg != NULL && *line_arg == NUL)
23921 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 }
23923
23924 /* Don't define the function when skipping commands or when an error was
23925 * detected. */
23926 if (eap->skip || did_emsg)
23927 goto erret;
23928
23929 /*
23930 * If there are no errors, add the function
23931 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023932 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023933 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023934 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023935 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023936 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023937 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023938 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023939 goto erret;
23940 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023942 fp = find_func(name);
23943 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023945 if (!eap->forceit)
23946 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023947 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023948 goto erret;
23949 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023950 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023951 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023952 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023953 name);
23954 goto erret;
23955 }
23956 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023957 ga_clear_strings(&(fp->uf_args));
23958 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023959 vim_free(name);
23960 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 }
23963 else
23964 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023965 char numbuf[20];
23966
23967 fp = NULL;
23968 if (fudi.fd_newkey == NULL && !eap->forceit)
23969 {
23970 EMSG(_(e_funcdict));
23971 goto erret;
23972 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023973 if (fudi.fd_di == NULL)
23974 {
23975 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023976 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023977 goto erret;
23978 }
23979 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023980 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023981 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023982
23983 /* Give the function a sequential number. Can only be used with a
23984 * Funcref! */
23985 vim_free(name);
23986 sprintf(numbuf, "%d", ++func_nr);
23987 name = vim_strsave((char_u *)numbuf);
23988 if (name == NULL)
23989 goto erret;
23990 }
23991
23992 if (fp == NULL)
23993 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023994 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023995 {
23996 int slen, plen;
23997 char_u *scriptname;
23998
23999 /* Check that the autoload name matches the script name. */
24000 j = FAIL;
24001 if (sourcing_name != NULL)
24002 {
24003 scriptname = autoload_name(name);
24004 if (scriptname != NULL)
24005 {
24006 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024007 plen = (int)STRLEN(p);
24008 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024009 if (slen > plen && fnamecmp(p,
24010 sourcing_name + slen - plen) == 0)
24011 j = OK;
24012 vim_free(scriptname);
24013 }
24014 }
24015 if (j == FAIL)
24016 {
24017 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24018 goto erret;
24019 }
24020 }
24021
24022 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 if (fp == NULL)
24024 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024025
24026 if (fudi.fd_dict != NULL)
24027 {
24028 if (fudi.fd_di == NULL)
24029 {
24030 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024031 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024032 if (fudi.fd_di == NULL)
24033 {
24034 vim_free(fp);
24035 goto erret;
24036 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024037 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24038 {
24039 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024040 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024041 goto erret;
24042 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024043 }
24044 else
24045 /* overwrite existing dict entry */
24046 clear_tv(&fudi.fd_di->di_tv);
24047 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024048 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024049 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024050 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024051
24052 /* behave like "dict" was used */
24053 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024054 }
24055
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024057 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024058 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24059 {
24060 vim_free(fp);
24061 goto erret;
24062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024064 fp->uf_args = newargs;
24065 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024066#ifdef FEAT_PROFILE
24067 fp->uf_tml_count = NULL;
24068 fp->uf_tml_total = NULL;
24069 fp->uf_tml_self = NULL;
24070 fp->uf_profiling = FALSE;
24071 if (prof_def_func())
24072 func_do_profile(fp);
24073#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024074 fp->uf_varargs = varargs;
24075 fp->uf_flags = flags;
24076 fp->uf_calls = 0;
24077 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024078 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079
24080erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081 ga_clear_strings(&newargs);
24082 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024083ret_free:
24084 vim_free(skip_until);
24085 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024086 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024088 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024089}
24090
24091/*
24092 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024093 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024095 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024096 * TFN_INT: internal function name OK
24097 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024098 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099 * Advances "pp" to just after the function name (if no error).
24100 */
24101 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024102trans_function_name(
24103 char_u **pp,
24104 int skip, /* only find the end, don't evaluate */
24105 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024106 funcdict_T *fdp, /* return: info about dictionary used */
24107 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024109 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 char_u *start;
24111 char_u *end;
24112 int lead;
24113 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024114 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024115 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024116
24117 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024118 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024120
24121 /* Check for hard coded <SNR>: already translated function ID (from a user
24122 * command). */
24123 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24124 && (*pp)[2] == (int)KE_SNR)
24125 {
24126 *pp += 3;
24127 len = get_id_len(pp) + 3;
24128 return vim_strnsave(start, len);
24129 }
24130
24131 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24132 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024134 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024136
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024137 /* Note that TFN_ flags use the same values as GLV_ flags. */
24138 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024139 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024140 if (end == start)
24141 {
24142 if (!skip)
24143 EMSG(_("E129: Function name required"));
24144 goto theend;
24145 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024146 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024147 {
24148 /*
24149 * Report an invalid expression in braces, unless the expression
24150 * evaluation has been cancelled due to an aborting error, an
24151 * interrupt, or an exception.
24152 */
24153 if (!aborting())
24154 {
24155 if (end != NULL)
24156 EMSG2(_(e_invarg2), start);
24157 }
24158 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024159 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024160 goto theend;
24161 }
24162
24163 if (lv.ll_tv != NULL)
24164 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024165 if (fdp != NULL)
24166 {
24167 fdp->fd_dict = lv.ll_dict;
24168 fdp->fd_newkey = lv.ll_newkey;
24169 lv.ll_newkey = NULL;
24170 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024171 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024172 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24173 {
24174 name = vim_strsave(lv.ll_tv->vval.v_string);
24175 *pp = end;
24176 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024177 else if (lv.ll_tv->v_type == VAR_PARTIAL
24178 && lv.ll_tv->vval.v_partial != NULL)
24179 {
24180 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24181 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024182 if (partial != NULL)
24183 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024184 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024185 else
24186 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024187 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24188 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024189 EMSG(_(e_funcref));
24190 else
24191 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024192 name = NULL;
24193 }
24194 goto theend;
24195 }
24196
24197 if (lv.ll_name == NULL)
24198 {
24199 /* Error found, but continue after the function name. */
24200 *pp = end;
24201 goto theend;
24202 }
24203
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024204 /* Check if the name is a Funcref. If so, use the value. */
24205 if (lv.ll_exp_name != NULL)
24206 {
24207 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024208 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024209 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024210 if (name == lv.ll_exp_name)
24211 name = NULL;
24212 }
24213 else
24214 {
24215 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024216 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024217 if (name == *pp)
24218 name = NULL;
24219 }
24220 if (name != NULL)
24221 {
24222 name = vim_strsave(name);
24223 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024224 if (STRNCMP(name, "<SNR>", 5) == 0)
24225 {
24226 /* Change "<SNR>" to the byte sequence. */
24227 name[0] = K_SPECIAL;
24228 name[1] = KS_EXTRA;
24229 name[2] = (int)KE_SNR;
24230 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24231 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024232 goto theend;
24233 }
24234
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024235 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024236 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024237 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024238 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24239 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24240 {
24241 /* When there was "s:" already or the name expanded to get a
24242 * leading "s:" then remove it. */
24243 lv.ll_name += 2;
24244 len -= 2;
24245 lead = 2;
24246 }
24247 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024248 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024249 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024250 /* skip over "s:" and "g:" */
24251 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024252 lv.ll_name += 2;
24253 len = (int)(end - lv.ll_name);
24254 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024255
24256 /*
24257 * Copy the function name to allocated memory.
24258 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24259 * Accept <SNR>123_name() outside a script.
24260 */
24261 if (skip)
24262 lead = 0; /* do nothing */
24263 else if (lead > 0)
24264 {
24265 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024266 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24267 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024268 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024269 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024270 if (current_SID <= 0)
24271 {
24272 EMSG(_(e_usingsid));
24273 goto theend;
24274 }
24275 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24276 lead += (int)STRLEN(sid_buf);
24277 }
24278 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024279 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024280 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024281 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024282 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024283 goto theend;
24284 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024285 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024286 {
24287 char_u *cp = vim_strchr(lv.ll_name, ':');
24288
24289 if (cp != NULL && cp < end)
24290 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024291 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024292 goto theend;
24293 }
24294 }
24295
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024296 name = alloc((unsigned)(len + lead + 1));
24297 if (name != NULL)
24298 {
24299 if (lead > 0)
24300 {
24301 name[0] = K_SPECIAL;
24302 name[1] = KS_EXTRA;
24303 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024304 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024305 STRCPY(name + 3, sid_buf);
24306 }
24307 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024308 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024309 }
24310 *pp = end;
24311
24312theend:
24313 clear_lval(&lv);
24314 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315}
24316
24317/*
24318 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24319 * Return 2 if "p" starts with "s:".
24320 * Return 0 otherwise.
24321 */
24322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024323eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024325 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24326 * the standard library function. */
24327 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24328 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329 return 5;
24330 if (p[0] == 's' && p[1] == ':')
24331 return 2;
24332 return 0;
24333}
24334
24335/*
24336 * Return TRUE if "p" starts with "<SID>" or "s:".
24337 * Only works if eval_fname_script() returned non-zero for "p"!
24338 */
24339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024340eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024341{
24342 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24343}
24344
24345/*
24346 * List the head of the function: "name(arg1, arg2)".
24347 */
24348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024349list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350{
24351 int j;
24352
24353 msg_start();
24354 if (indent)
24355 MSG_PUTS(" ");
24356 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024357 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 {
24359 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024360 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 }
24362 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024363 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024365 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366 {
24367 if (j)
24368 MSG_PUTS(", ");
24369 msg_puts(FUNCARG(fp, j));
24370 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024371 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 {
24373 if (j)
24374 MSG_PUTS(", ");
24375 MSG_PUTS("...");
24376 }
24377 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024378 if (fp->uf_flags & FC_ABORT)
24379 MSG_PUTS(" abort");
24380 if (fp->uf_flags & FC_RANGE)
24381 MSG_PUTS(" range");
24382 if (fp->uf_flags & FC_DICT)
24383 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024384 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024385 if (p_verbose > 0)
24386 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387}
24388
24389/*
24390 * Find a function by name, return pointer to it in ufuncs.
24391 * Return NULL for unknown function.
24392 */
24393 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024394find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024396 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024398 hi = hash_find(&func_hashtab, name);
24399 if (!HASHITEM_EMPTY(hi))
24400 return HI2UF(hi);
24401 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402}
24403
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024404#if defined(EXITFREE) || defined(PROTO)
24405 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024406free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024407{
24408 hashitem_T *hi;
24409
24410 /* Need to start all over every time, because func_free() may change the
24411 * hash table. */
24412 while (func_hashtab.ht_used > 0)
24413 for (hi = func_hashtab.ht_array; ; ++hi)
24414 if (!HASHITEM_EMPTY(hi))
24415 {
24416 func_free(HI2UF(hi));
24417 break;
24418 }
24419}
24420#endif
24421
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024423translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024424{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024425 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024426 return find_internal_func(name) >= 0;
24427 return find_func(name) != NULL;
24428}
24429
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024430/*
24431 * Return TRUE if a function "name" exists.
24432 */
24433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024434function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024435{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024436 char_u *nm = name;
24437 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024438 int n = FALSE;
24439
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024440 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024441 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024442 nm = skipwhite(nm);
24443
24444 /* Only accept "funcname", "funcname ", "funcname (..." and
24445 * "funcname(...", not "funcname!...". */
24446 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024447 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024448 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024449 return n;
24450}
24451
Bram Moolenaara1544c02013-05-30 12:35:52 +020024452 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024453get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024454{
24455 char_u *nm = name;
24456 char_u *p;
24457
Bram Moolenaar65639032016-03-16 21:40:30 +010024458 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024459
24460 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024461 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024462 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024463
Bram Moolenaara1544c02013-05-30 12:35:52 +020024464 vim_free(p);
24465 return NULL;
24466}
24467
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024468/*
24469 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024470 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24471 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024472 */
24473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024474builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024475{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024476 char_u *p;
24477
24478 if (!ASCII_ISLOWER(name[0]))
24479 return FALSE;
24480 p = vim_strchr(name, AUTOLOAD_CHAR);
24481 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024482}
24483
Bram Moolenaar05159a02005-02-26 23:04:13 +000024484#if defined(FEAT_PROFILE) || defined(PROTO)
24485/*
24486 * Start profiling function "fp".
24487 */
24488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024489func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024490{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024491 int len = fp->uf_lines.ga_len;
24492
24493 if (len == 0)
24494 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024495 fp->uf_tm_count = 0;
24496 profile_zero(&fp->uf_tm_self);
24497 profile_zero(&fp->uf_tm_total);
24498 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024499 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024500 if (fp->uf_tml_total == NULL)
24501 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024502 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024503 if (fp->uf_tml_self == NULL)
24504 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024505 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024506 fp->uf_tml_idx = -1;
24507 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24508 || fp->uf_tml_self == NULL)
24509 return; /* out of memory */
24510
24511 fp->uf_profiling = TRUE;
24512}
24513
24514/*
24515 * Dump the profiling results for all functions in file "fd".
24516 */
24517 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024518func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024519{
24520 hashitem_T *hi;
24521 int todo;
24522 ufunc_T *fp;
24523 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024524 ufunc_T **sorttab;
24525 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024526
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024527 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024528 if (todo == 0)
24529 return; /* nothing to dump */
24530
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024531 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024532
Bram Moolenaar05159a02005-02-26 23:04:13 +000024533 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24534 {
24535 if (!HASHITEM_EMPTY(hi))
24536 {
24537 --todo;
24538 fp = HI2UF(hi);
24539 if (fp->uf_profiling)
24540 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024541 if (sorttab != NULL)
24542 sorttab[st_len++] = fp;
24543
Bram Moolenaar05159a02005-02-26 23:04:13 +000024544 if (fp->uf_name[0] == K_SPECIAL)
24545 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24546 else
24547 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24548 if (fp->uf_tm_count == 1)
24549 fprintf(fd, "Called 1 time\n");
24550 else
24551 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24552 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24553 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24554 fprintf(fd, "\n");
24555 fprintf(fd, "count total (s) self (s)\n");
24556
24557 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24558 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024559 if (FUNCLINE(fp, i) == NULL)
24560 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024561 prof_func_line(fd, fp->uf_tml_count[i],
24562 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024563 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24564 }
24565 fprintf(fd, "\n");
24566 }
24567 }
24568 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024569
24570 if (sorttab != NULL && st_len > 0)
24571 {
24572 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24573 prof_total_cmp);
24574 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24575 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24576 prof_self_cmp);
24577 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24578 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024579
24580 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024581}
Bram Moolenaar73830342005-02-28 22:48:19 +000024582
24583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024584prof_sort_list(
24585 FILE *fd,
24586 ufunc_T **sorttab,
24587 int st_len,
24588 char *title,
24589 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024590{
24591 int i;
24592 ufunc_T *fp;
24593
24594 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24595 fprintf(fd, "count total (s) self (s) function\n");
24596 for (i = 0; i < 20 && i < st_len; ++i)
24597 {
24598 fp = sorttab[i];
24599 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24600 prefer_self);
24601 if (fp->uf_name[0] == K_SPECIAL)
24602 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24603 else
24604 fprintf(fd, " %s()\n", fp->uf_name);
24605 }
24606 fprintf(fd, "\n");
24607}
24608
24609/*
24610 * Print the count and times for one function or function line.
24611 */
24612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024613prof_func_line(
24614 FILE *fd,
24615 int count,
24616 proftime_T *total,
24617 proftime_T *self,
24618 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024619{
24620 if (count > 0)
24621 {
24622 fprintf(fd, "%5d ", count);
24623 if (prefer_self && profile_equal(total, self))
24624 fprintf(fd, " ");
24625 else
24626 fprintf(fd, "%s ", profile_msg(total));
24627 if (!prefer_self && profile_equal(total, self))
24628 fprintf(fd, " ");
24629 else
24630 fprintf(fd, "%s ", profile_msg(self));
24631 }
24632 else
24633 fprintf(fd, " ");
24634}
24635
24636/*
24637 * Compare function for total time sorting.
24638 */
24639 static int
24640#ifdef __BORLANDC__
24641_RTLENTRYF
24642#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024643prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024644{
24645 ufunc_T *p1, *p2;
24646
24647 p1 = *(ufunc_T **)s1;
24648 p2 = *(ufunc_T **)s2;
24649 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24650}
24651
24652/*
24653 * Compare function for self time sorting.
24654 */
24655 static int
24656#ifdef __BORLANDC__
24657_RTLENTRYF
24658#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024659prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024660{
24661 ufunc_T *p1, *p2;
24662
24663 p1 = *(ufunc_T **)s1;
24664 p2 = *(ufunc_T **)s2;
24665 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24666}
24667
Bram Moolenaar05159a02005-02-26 23:04:13 +000024668#endif
24669
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024670/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024671 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024672 * Return TRUE if a package was loaded.
24673 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024675script_autoload(
24676 char_u *name,
24677 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024678{
24679 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024680 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024681 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024682 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024683
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024684 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024685 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024686 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024687 return FALSE;
24688
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024689 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024690
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024691 /* Find the name in the list of previously loaded package names. Skip
24692 * "autoload/", it's always the same. */
24693 for (i = 0; i < ga_loaded.ga_len; ++i)
24694 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24695 break;
24696 if (!reload && i < ga_loaded.ga_len)
24697 ret = FALSE; /* was loaded already */
24698 else
24699 {
24700 /* Remember the name if it wasn't loaded already. */
24701 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24702 {
24703 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24704 tofree = NULL;
24705 }
24706
24707 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024708 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024709 ret = TRUE;
24710 }
24711
24712 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024713 return ret;
24714}
24715
24716/*
24717 * Return the autoload script name for a function or variable name.
24718 * Returns NULL when out of memory.
24719 */
24720 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024721autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024722{
24723 char_u *p;
24724 char_u *scriptname;
24725
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024726 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024727 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24728 if (scriptname == NULL)
24729 return FALSE;
24730 STRCPY(scriptname, "autoload/");
24731 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024732 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024733 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024734 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024735 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024736 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024737}
24738
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24740
24741/*
24742 * Function given to ExpandGeneric() to obtain the list of user defined
24743 * function names.
24744 */
24745 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024746get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024748 static long_u done;
24749 static hashitem_T *hi;
24750 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751
24752 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024754 done = 0;
24755 hi = func_hashtab.ht_array;
24756 }
24757 if (done < func_hashtab.ht_used)
24758 {
24759 if (done++ > 0)
24760 ++hi;
24761 while (HASHITEM_EMPTY(hi))
24762 ++hi;
24763 fp = HI2UF(hi);
24764
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024765 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024766 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024767
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024768 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24769 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770
24771 cat_func_name(IObuff, fp);
24772 if (xp->xp_context != EXPAND_USER_FUNC)
24773 {
24774 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024775 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776 STRCAT(IObuff, ")");
24777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024778 return IObuff;
24779 }
24780 return NULL;
24781}
24782
24783#endif /* FEAT_CMDL_COMPL */
24784
24785/*
24786 * Copy the function name of "fp" to buffer "buf".
24787 * "buf" must be able to hold the function name plus three bytes.
24788 * Takes care of script-local function names.
24789 */
24790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024791cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024793 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794 {
24795 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024796 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024797 }
24798 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024799 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800}
24801
24802/*
24803 * ":delfunction {name}"
24804 */
24805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024806ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024808 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809 char_u *p;
24810 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024811 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812
24813 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024814 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024815 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024817 {
24818 if (fudi.fd_dict != NULL && !eap->skip)
24819 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024822 if (!ends_excmd(*skipwhite(p)))
24823 {
24824 vim_free(name);
24825 EMSG(_(e_trailing));
24826 return;
24827 }
24828 eap->nextcmd = check_nextcmd(p);
24829 if (eap->nextcmd != NULL)
24830 *p = NUL;
24831
24832 if (!eap->skip)
24833 fp = find_func(name);
24834 vim_free(name);
24835
24836 if (!eap->skip)
24837 {
24838 if (fp == NULL)
24839 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024840 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841 return;
24842 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024843 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024844 {
24845 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24846 return;
24847 }
24848
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024849 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024851 /* Delete the dict item that refers to the function, it will
24852 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024853 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024855 else
24856 func_free(fp);
24857 }
24858}
24859
24860/*
24861 * Free a function and remove it from the list of functions.
24862 */
24863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024864func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024865{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024866 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024867
24868 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024869 ga_clear_strings(&(fp->uf_args));
24870 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024871#ifdef FEAT_PROFILE
24872 vim_free(fp->uf_tml_count);
24873 vim_free(fp->uf_tml_total);
24874 vim_free(fp->uf_tml_self);
24875#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024876
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024877 /* remove the function from the function hashtable */
24878 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24879 if (HASHITEM_EMPTY(hi))
24880 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024881 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024882 hash_remove(&func_hashtab, hi);
24883
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024884 vim_free(fp);
24885}
24886
24887/*
24888 * Unreference a Function: decrement the reference count and free it when it
24889 * becomes zero. Only for numbered functions.
24890 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024892func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024893{
24894 ufunc_T *fp;
24895
24896 if (name != NULL && isdigit(*name))
24897 {
24898 fp = find_func(name);
24899 if (fp == NULL)
24900 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024901 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024902 {
24903 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024904 * when "uf_calls" becomes zero. */
24905 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024906 func_free(fp);
24907 }
24908 }
24909}
24910
24911/*
24912 * Count a reference to a Function.
24913 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024915func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024916{
24917 ufunc_T *fp;
24918
24919 if (name != NULL && isdigit(*name))
24920 {
24921 fp = find_func(name);
24922 if (fp == NULL)
24923 EMSG2(_(e_intern2), "func_ref()");
24924 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024925 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024926 }
24927}
24928
24929/*
24930 * Call a user function.
24931 */
24932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024933call_user_func(
24934 ufunc_T *fp, /* pointer to function */
24935 int argcount, /* nr of args */
24936 typval_T *argvars, /* arguments */
24937 typval_T *rettv, /* return value */
24938 linenr_T firstline, /* first line of range */
24939 linenr_T lastline, /* last line of range */
24940 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941{
Bram Moolenaar33570922005-01-25 22:26:29 +000024942 char_u *save_sourcing_name;
24943 linenr_T save_sourcing_lnum;
24944 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024945 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024946 int save_did_emsg;
24947 static int depth = 0;
24948 dictitem_T *v;
24949 int fixvar_idx = 0; /* index in fixvar[] */
24950 int i;
24951 int ai;
24952 char_u numbuf[NUMBUFLEN];
24953 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024954 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024955#ifdef FEAT_PROFILE
24956 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024957 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959
24960 /* If depth of calling is getting too high, don't execute the function */
24961 if (depth >= p_mfd)
24962 {
24963 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024964 rettv->v_type = VAR_NUMBER;
24965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966 return;
24967 }
24968 ++depth;
24969
24970 line_breakcheck(); /* check for CTRL-C hit */
24971
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024972 fc = (funccall_T *)alloc(sizeof(funccall_T));
24973 fc->caller = current_funccal;
24974 current_funccal = fc;
24975 fc->func = fp;
24976 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024977 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024978 fc->linenr = 0;
24979 fc->returned = FALSE;
24980 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024982 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24983 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024984
Bram Moolenaar33570922005-01-25 22:26:29 +000024985 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024986 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024987 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24988 * each argument variable and saves a lot of time.
24989 */
24990 /*
24991 * Init l: variables.
24992 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024993 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024994 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024995 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024996 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24997 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024998 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024999 name = v->di_key;
25000 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025001 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025002 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025003 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025004 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025005 v->di_tv.vval.v_dict = selfdict;
25006 ++selfdict->dv_refcount;
25007 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025008
Bram Moolenaar33570922005-01-25 22:26:29 +000025009 /*
25010 * Init a: variables.
25011 * Set a:0 to "argcount".
25012 * Set a:000 to a list with room for the "..." arguments.
25013 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025014 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025015 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025016 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025017 /* Use "name" to avoid a warning from some compiler that checks the
25018 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025019 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025020 name = v->di_key;
25021 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025022 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025023 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025024 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025025 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025026 v->di_tv.vval.v_list = &fc->l_varlist;
25027 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25028 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25029 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025030
25031 /*
25032 * Set a:firstline to "firstline" and a:lastline to "lastline".
25033 * Set a:name to named arguments.
25034 * Set a:N to the "..." arguments.
25035 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025036 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025037 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025038 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025039 (varnumber_T)lastline);
25040 for (i = 0; i < argcount; ++i)
25041 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025042 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025043 if (ai < 0)
25044 /* named argument a:name */
25045 name = FUNCARG(fp, i);
25046 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025047 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025048 /* "..." argument a:1, a:2, etc. */
25049 sprintf((char *)numbuf, "%d", ai + 1);
25050 name = numbuf;
25051 }
25052 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25053 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025054 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025055 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25056 }
25057 else
25058 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025059 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25060 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025061 if (v == NULL)
25062 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025063 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025064 }
25065 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025066 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025067
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025068 /* Note: the values are copied directly to avoid alloc/free.
25069 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025070 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025071 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025072
25073 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25074 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025075 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25076 fc->l_listitems[ai].li_tv = argvars[i];
25077 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025078 }
25079 }
25080
Bram Moolenaar071d4272004-06-13 20:20:40 +000025081 /* Don't redraw while executing the function. */
25082 ++RedrawingDisabled;
25083 save_sourcing_name = sourcing_name;
25084 save_sourcing_lnum = sourcing_lnum;
25085 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025086 /* need space for function name + ("function " + 3) or "[number]" */
25087 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25088 + STRLEN(fp->uf_name) + 20;
25089 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025090 if (sourcing_name != NULL)
25091 {
25092 if (save_sourcing_name != NULL
25093 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025094 sprintf((char *)sourcing_name, "%s[%d]..",
25095 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096 else
25097 STRCPY(sourcing_name, "function ");
25098 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25099
25100 if (p_verbose >= 12)
25101 {
25102 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025103 verbose_enter_scroll();
25104
Bram Moolenaar555b2802005-05-19 21:08:39 +000025105 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025106 if (p_verbose >= 14)
25107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025108 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025109 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025110 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025111 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112
25113 msg_puts((char_u *)"(");
25114 for (i = 0; i < argcount; ++i)
25115 {
25116 if (i > 0)
25117 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025118 if (argvars[i].v_type == VAR_NUMBER)
25119 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 else
25121 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025122 /* Do not want errors such as E724 here. */
25123 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025124 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025125 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025126 if (s != NULL)
25127 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025128 if (vim_strsize(s) > MSG_BUF_CLEN)
25129 {
25130 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25131 s = buf;
25132 }
25133 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025134 vim_free(tofree);
25135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136 }
25137 }
25138 msg_puts((char_u *)")");
25139 }
25140 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025141
25142 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025143 --no_wait_return;
25144 }
25145 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025146#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025147 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025148 {
25149 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25150 func_do_profile(fp);
25151 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025152 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025153 {
25154 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025155 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025156 profile_zero(&fp->uf_tm_children);
25157 }
25158 script_prof_save(&wait_start);
25159 }
25160#endif
25161
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025163 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164 save_did_emsg = did_emsg;
25165 did_emsg = FALSE;
25166
25167 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025168 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025169 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25170
25171 --RedrawingDisabled;
25172
25173 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025174 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025176 clear_tv(rettv);
25177 rettv->v_type = VAR_NUMBER;
25178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025179 }
25180
Bram Moolenaar05159a02005-02-26 23:04:13 +000025181#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025182 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025183 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025184 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025185 profile_end(&call_start);
25186 profile_sub_wait(&wait_start, &call_start);
25187 profile_add(&fp->uf_tm_total, &call_start);
25188 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025189 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025190 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025191 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25192 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025193 }
25194 }
25195#endif
25196
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 /* when being verbose, mention the return value */
25198 if (p_verbose >= 12)
25199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025201 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025204 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025205 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025206 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025207 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025208 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025210 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025211 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025212 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025213 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025214
Bram Moolenaar555b2802005-05-19 21:08:39 +000025215 /* The value may be very long. Skip the middle part, so that we
25216 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025217 * truncate it at the end. Don't want errors such as E724 here. */
25218 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025219 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025220 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025221 if (s != NULL)
25222 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025223 if (vim_strsize(s) > MSG_BUF_CLEN)
25224 {
25225 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25226 s = buf;
25227 }
25228 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025229 vim_free(tofree);
25230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231 }
25232 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025233
25234 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 --no_wait_return;
25236 }
25237
25238 vim_free(sourcing_name);
25239 sourcing_name = save_sourcing_name;
25240 sourcing_lnum = save_sourcing_lnum;
25241 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025242#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025243 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025244 script_prof_restore(&wait_start);
25245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246
25247 if (p_verbose >= 12 && sourcing_name != NULL)
25248 {
25249 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025250 verbose_enter_scroll();
25251
Bram Moolenaar555b2802005-05-19 21:08:39 +000025252 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025254
25255 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256 --no_wait_return;
25257 }
25258
25259 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025260 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025262
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025263 /* If the a:000 list and the l: and a: dicts are not referenced we can
25264 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025265 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25266 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25267 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25268 {
25269 free_funccal(fc, FALSE);
25270 }
25271 else
25272 {
25273 hashitem_T *hi;
25274 listitem_T *li;
25275 int todo;
25276
25277 /* "fc" is still in use. This can happen when returning "a:000" or
25278 * assigning "l:" to a global variable.
25279 * Link "fc" in the list for garbage collection later. */
25280 fc->caller = previous_funccal;
25281 previous_funccal = fc;
25282
25283 /* Make a copy of the a: variables, since we didn't do that above. */
25284 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25285 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25286 {
25287 if (!HASHITEM_EMPTY(hi))
25288 {
25289 --todo;
25290 v = HI2DI(hi);
25291 copy_tv(&v->di_tv, &v->di_tv);
25292 }
25293 }
25294
25295 /* Make a copy of the a:000 items, since we didn't do that above. */
25296 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25297 copy_tv(&li->li_tv, &li->li_tv);
25298 }
25299}
25300
25301/*
25302 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025303 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025304 */
25305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025306can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025307{
25308 return (fc->l_varlist.lv_copyID != copyID
25309 && fc->l_vars.dv_copyID != copyID
25310 && fc->l_avars.dv_copyID != copyID);
25311}
25312
25313/*
25314 * Free "fc" and what it contains.
25315 */
25316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025317free_funccal(
25318 funccall_T *fc,
25319 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025320{
25321 listitem_T *li;
25322
25323 /* The a: variables typevals may not have been allocated, only free the
25324 * allocated variables. */
25325 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25326
25327 /* free all l: variables */
25328 vars_clear(&fc->l_vars.dv_hashtab);
25329
25330 /* Free the a:000 variables if they were allocated. */
25331 if (free_val)
25332 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25333 clear_tv(&li->li_tv);
25334
25335 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336}
25337
25338/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025339 * Add a number variable "name" to dict "dp" with value "nr".
25340 */
25341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025342add_nr_var(
25343 dict_T *dp,
25344 dictitem_T *v,
25345 char *name,
25346 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025347{
25348 STRCPY(v->di_key, name);
25349 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25350 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25351 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025352 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025353 v->di_tv.vval.v_number = nr;
25354}
25355
25356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 * ":return [expr]"
25358 */
25359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025360ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361{
25362 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025363 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 int returning = FALSE;
25365
25366 if (current_funccal == NULL)
25367 {
25368 EMSG(_("E133: :return not inside a function"));
25369 return;
25370 }
25371
25372 if (eap->skip)
25373 ++emsg_skip;
25374
25375 eap->nextcmd = NULL;
25376 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025377 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025378 {
25379 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025380 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025382 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 }
25384 /* It's safer to return also on error. */
25385 else if (!eap->skip)
25386 {
25387 /*
25388 * Return unless the expression evaluation has been cancelled due to an
25389 * aborting error, an interrupt, or an exception.
25390 */
25391 if (!aborting())
25392 returning = do_return(eap, FALSE, TRUE, NULL);
25393 }
25394
25395 /* When skipping or the return gets pending, advance to the next command
25396 * in this line (!returning). Otherwise, ignore the rest of the line.
25397 * Following lines will be ignored by get_func_line(). */
25398 if (returning)
25399 eap->nextcmd = NULL;
25400 else if (eap->nextcmd == NULL) /* no argument */
25401 eap->nextcmd = check_nextcmd(arg);
25402
25403 if (eap->skip)
25404 --emsg_skip;
25405}
25406
25407/*
25408 * Return from a function. Possibly makes the return pending. Also called
25409 * for a pending return at the ":endtry" or after returning from an extra
25410 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025411 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025412 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413 * FALSE when the return gets pending.
25414 */
25415 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025416do_return(
25417 exarg_T *eap,
25418 int reanimate,
25419 int is_cmd,
25420 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421{
25422 int idx;
25423 struct condstack *cstack = eap->cstack;
25424
25425 if (reanimate)
25426 /* Undo the return. */
25427 current_funccal->returned = FALSE;
25428
25429 /*
25430 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25431 * not in its finally clause (which then is to be executed next) is found.
25432 * In this case, make the ":return" pending for execution at the ":endtry".
25433 * Otherwise, return normally.
25434 */
25435 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25436 if (idx >= 0)
25437 {
25438 cstack->cs_pending[idx] = CSTP_RETURN;
25439
25440 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025441 /* A pending return again gets pending. "rettv" points to an
25442 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025444 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025445 else
25446 {
25447 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025448 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025449 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025450 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025452 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 {
25454 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025455 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025456 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 else
25458 EMSG(_(e_outofmem));
25459 }
25460 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025461 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462
25463 if (reanimate)
25464 {
25465 /* The pending return value could be overwritten by a ":return"
25466 * without argument in a finally clause; reset the default
25467 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025468 current_funccal->rettv->v_type = VAR_NUMBER;
25469 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025470 }
25471 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025472 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025473 }
25474 else
25475 {
25476 current_funccal->returned = TRUE;
25477
25478 /* If the return is carried out now, store the return value. For
25479 * a return immediately after reanimation, the value is already
25480 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025481 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025483 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025484 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025486 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025487 }
25488 }
25489
25490 return idx < 0;
25491}
25492
25493/*
25494 * Free the variable with a pending return value.
25495 */
25496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025497discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498{
Bram Moolenaar33570922005-01-25 22:26:29 +000025499 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500}
25501
25502/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025503 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 * is an allocated string. Used by report_pending() for verbose messages.
25505 */
25506 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025507get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025508{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025509 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025510 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025511 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025513 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025514 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025515 if (s == NULL)
25516 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025517
25518 STRCPY(IObuff, ":return ");
25519 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25520 if (STRLEN(s) + 8 >= IOSIZE)
25521 STRCPY(IObuff + IOSIZE - 4, "...");
25522 vim_free(tofree);
25523 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524}
25525
25526/*
25527 * Get next function line.
25528 * Called by do_cmdline() to get the next line.
25529 * Returns allocated string, or NULL for end of function.
25530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025532get_func_line(
25533 int c UNUSED,
25534 void *cookie,
25535 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536{
Bram Moolenaar33570922005-01-25 22:26:29 +000025537 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025538 ufunc_T *fp = fcp->func;
25539 char_u *retval;
25540 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025541
25542 /* If breakpoints have been added/deleted need to check for it. */
25543 if (fcp->dbg_tick != debug_tick)
25544 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025545 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546 sourcing_lnum);
25547 fcp->dbg_tick = debug_tick;
25548 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025549#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025550 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025551 func_line_end(cookie);
25552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553
Bram Moolenaar05159a02005-02-26 23:04:13 +000025554 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025555 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25556 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 retval = NULL;
25558 else
25559 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025560 /* Skip NULL lines (continuation lines). */
25561 while (fcp->linenr < gap->ga_len
25562 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25563 ++fcp->linenr;
25564 if (fcp->linenr >= gap->ga_len)
25565 retval = NULL;
25566 else
25567 {
25568 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25569 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025570#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025571 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025572 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025573#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575 }
25576
25577 /* Did we encounter a breakpoint? */
25578 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25579 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025580 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025582 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025583 sourcing_lnum);
25584 fcp->dbg_tick = debug_tick;
25585 }
25586
25587 return retval;
25588}
25589
Bram Moolenaar05159a02005-02-26 23:04:13 +000025590#if defined(FEAT_PROFILE) || defined(PROTO)
25591/*
25592 * Called when starting to read a function line.
25593 * "sourcing_lnum" must be correct!
25594 * When skipping lines it may not actually be executed, but we won't find out
25595 * until later and we need to store the time now.
25596 */
25597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025598func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025599{
25600 funccall_T *fcp = (funccall_T *)cookie;
25601 ufunc_T *fp = fcp->func;
25602
25603 if (fp->uf_profiling && sourcing_lnum >= 1
25604 && sourcing_lnum <= fp->uf_lines.ga_len)
25605 {
25606 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025607 /* Skip continuation lines. */
25608 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25609 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025610 fp->uf_tml_execed = FALSE;
25611 profile_start(&fp->uf_tml_start);
25612 profile_zero(&fp->uf_tml_children);
25613 profile_get_wait(&fp->uf_tml_wait);
25614 }
25615}
25616
25617/*
25618 * Called when actually executing a function line.
25619 */
25620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025621func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025622{
25623 funccall_T *fcp = (funccall_T *)cookie;
25624 ufunc_T *fp = fcp->func;
25625
25626 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25627 fp->uf_tml_execed = TRUE;
25628}
25629
25630/*
25631 * Called when done with a function line.
25632 */
25633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025634func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025635{
25636 funccall_T *fcp = (funccall_T *)cookie;
25637 ufunc_T *fp = fcp->func;
25638
25639 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25640 {
25641 if (fp->uf_tml_execed)
25642 {
25643 ++fp->uf_tml_count[fp->uf_tml_idx];
25644 profile_end(&fp->uf_tml_start);
25645 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025646 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025647 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25648 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025649 }
25650 fp->uf_tml_idx = -1;
25651 }
25652}
25653#endif
25654
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655/*
25656 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025657 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 */
25659 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025660func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661{
Bram Moolenaar33570922005-01-25 22:26:29 +000025662 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663
25664 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25665 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025666 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 || fcp->returned);
25668}
25669
25670/*
25671 * return TRUE if cookie indicates a function which "abort"s on errors.
25672 */
25673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025674func_has_abort(
25675 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025677 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025678}
25679
25680#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25681typedef enum
25682{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025683 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25684 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25685 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686} var_flavour_T;
25687
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025688static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689
25690 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025691var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692{
25693 char_u *p = varname;
25694
25695 if (ASCII_ISUPPER(*p))
25696 {
25697 while (*(++p))
25698 if (ASCII_ISLOWER(*p))
25699 return VAR_FLAVOUR_SESSION;
25700 return VAR_FLAVOUR_VIMINFO;
25701 }
25702 else
25703 return VAR_FLAVOUR_DEFAULT;
25704}
25705#endif
25706
25707#if defined(FEAT_VIMINFO) || defined(PROTO)
25708/*
25709 * Restore global vars that start with a capital from the viminfo file
25710 */
25711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025712read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025713{
25714 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025715 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025716 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025717 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025718
25719 if (!writing && (find_viminfo_parameter('!') != NULL))
25720 {
25721 tab = vim_strchr(virp->vir_line + 1, '\t');
25722 if (tab != NULL)
25723 {
25724 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025725 switch (*tab)
25726 {
25727 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025728#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025729 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025730#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025731 case 'D': type = VAR_DICT; break;
25732 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025733 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025735
25736 tab = vim_strchr(tab, '\t');
25737 if (tab != NULL)
25738 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025739 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025740 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025741 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025742 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025743#ifdef FEAT_FLOAT
25744 else if (type == VAR_FLOAT)
25745 (void)string2float(tab + 1, &tv.vval.v_float);
25746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025747 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025748 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025749 if (type == VAR_DICT || type == VAR_LIST)
25750 {
25751 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25752
25753 if (etv == NULL)
25754 /* Failed to parse back the dict or list, use it as a
25755 * string. */
25756 tv.v_type = VAR_STRING;
25757 else
25758 {
25759 vim_free(tv.vval.v_string);
25760 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025761 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025762 }
25763 }
25764
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025765 /* when in a function use global variables */
25766 save_funccal = current_funccal;
25767 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025768 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025769 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025770
25771 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025772 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025773 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25774 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025775 }
25776 }
25777 }
25778
25779 return viminfo_readline(virp);
25780}
25781
25782/*
25783 * Write global vars that start with a capital to the viminfo file
25784 */
25785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025786write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787{
Bram Moolenaar33570922005-01-25 22:26:29 +000025788 hashitem_T *hi;
25789 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025790 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025791 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025792 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025793 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025794 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795
25796 if (find_viminfo_parameter('!') == NULL)
25797 return;
25798
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025799 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025800
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025801 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025802 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025803 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025804 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025806 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025807 this_var = HI2DI(hi);
25808 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025809 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025810 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025811 {
25812 case VAR_STRING: s = "STR"; break;
25813 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025814 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025815 case VAR_DICT: s = "DIC"; break;
25816 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025817 case VAR_SPECIAL: s = "XPL"; break;
25818
25819 case VAR_UNKNOWN:
25820 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025821 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025822 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025823 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025824 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025825 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025826 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025827 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025828 if (p != NULL)
25829 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025830 vim_free(tofree);
25831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832 }
25833 }
25834}
25835#endif
25836
25837#if defined(FEAT_SESSION) || defined(PROTO)
25838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025839store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025840{
Bram Moolenaar33570922005-01-25 22:26:29 +000025841 hashitem_T *hi;
25842 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025843 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025844 char_u *p, *t;
25845
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025846 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025847 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025849 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025850 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025851 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025852 this_var = HI2DI(hi);
25853 if ((this_var->di_tv.v_type == VAR_NUMBER
25854 || this_var->di_tv.v_type == VAR_STRING)
25855 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025856 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025857 /* Escape special characters with a backslash. Turn a LF and
25858 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025859 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025860 (char_u *)"\\\"\n\r");
25861 if (p == NULL) /* out of memory */
25862 break;
25863 for (t = p; *t != NUL; ++t)
25864 if (*t == '\n')
25865 *t = 'n';
25866 else if (*t == '\r')
25867 *t = 'r';
25868 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025869 this_var->di_key,
25870 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25871 : ' ',
25872 p,
25873 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25874 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025875 || put_eol(fd) == FAIL)
25876 {
25877 vim_free(p);
25878 return FAIL;
25879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025880 vim_free(p);
25881 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025882#ifdef FEAT_FLOAT
25883 else if (this_var->di_tv.v_type == VAR_FLOAT
25884 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25885 {
25886 float_T f = this_var->di_tv.vval.v_float;
25887 int sign = ' ';
25888
25889 if (f < 0)
25890 {
25891 f = -f;
25892 sign = '-';
25893 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025894 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025895 this_var->di_key, sign, f) < 0)
25896 || put_eol(fd) == FAIL)
25897 return FAIL;
25898 }
25899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900 }
25901 }
25902 return OK;
25903}
25904#endif
25905
Bram Moolenaar661b1822005-07-28 22:36:45 +000025906/*
25907 * Display script name where an item was last set.
25908 * Should only be invoked when 'verbose' is non-zero.
25909 */
25910 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025911last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025912{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025913 char_u *p;
25914
Bram Moolenaar661b1822005-07-28 22:36:45 +000025915 if (scriptID != 0)
25916 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025917 p = home_replace_save(NULL, get_scriptname(scriptID));
25918 if (p != NULL)
25919 {
25920 verbose_enter();
25921 MSG_PUTS(_("\n\tLast set from "));
25922 MSG_PUTS(p);
25923 vim_free(p);
25924 verbose_leave();
25925 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025926 }
25927}
25928
Bram Moolenaard812df62008-11-09 12:46:09 +000025929/*
25930 * List v:oldfiles in a nice way.
25931 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025932 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025933ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025934{
25935 list_T *l = vimvars[VV_OLDFILES].vv_list;
25936 listitem_T *li;
25937 int nr = 0;
25938
25939 if (l == NULL)
25940 msg((char_u *)_("No old files"));
25941 else
25942 {
25943 msg_start();
25944 msg_scroll = TRUE;
25945 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25946 {
25947 msg_outnum((long)++nr);
25948 MSG_PUTS(": ");
25949 msg_outtrans(get_tv_string(&li->li_tv));
25950 msg_putchar('\n');
25951 out_flush(); /* output one line at a time */
25952 ui_breakcheck();
25953 }
25954 /* Assume "got_int" was set to truncate the listing. */
25955 got_int = FALSE;
25956
25957#ifdef FEAT_BROWSE_CMD
25958 if (cmdmod.browse)
25959 {
25960 quit_more = FALSE;
25961 nr = prompt_for_number(FALSE);
25962 msg_starthere();
25963 if (nr > 0)
25964 {
25965 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25966 (long)nr);
25967
25968 if (p != NULL)
25969 {
25970 p = expand_env_save(p);
25971 eap->arg = p;
25972 eap->cmdidx = CMD_edit;
25973 cmdmod.browse = FALSE;
25974 do_exedit(eap, NULL);
25975 vim_free(p);
25976 }
25977 }
25978 }
25979#endif
25980 }
25981}
25982
Bram Moolenaar53744302015-07-17 17:38:22 +020025983/* reset v:option_new, v:option_old and v:option_type */
25984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025985reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025986{
25987 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25988 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25989 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25990}
25991
25992
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993#endif /* FEAT_EVAL */
25994
Bram Moolenaar071d4272004-06-13 20:20:40 +000025995
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025996#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997
25998#ifdef WIN3264
25999/*
26000 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26001 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026002static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26003static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26004static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026005
26006/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026007 * Get the short path (8.3) for the filename in "fnamep".
26008 * Only works for a valid file name.
26009 * When the path gets longer "fnamep" is changed and the allocated buffer
26010 * is put in "bufp".
26011 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26012 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013 */
26014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026015get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026016{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026017 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018 char_u *newbuf;
26019
26020 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026021 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026022 if (l > len - 1)
26023 {
26024 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026025 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026 newbuf = vim_strnsave(*fnamep, l);
26027 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026028 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026029
26030 vim_free(*bufp);
26031 *fnamep = *bufp = newbuf;
26032
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026033 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026034 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035 }
26036
26037 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026038 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026039}
26040
26041/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026042 * Get the short path (8.3) for the filename in "fname". The converted
26043 * path is returned in "bufp".
26044 *
26045 * Some of the directories specified in "fname" may not exist. This function
26046 * will shorten the existing directories at the beginning of the path and then
26047 * append the remaining non-existing path.
26048 *
26049 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026050 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026051 * bufp - Pointer to an allocated buffer for the filename.
26052 * fnamelen - Length of the filename pointed to by fname
26053 *
26054 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026055 */
26056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026057shortpath_for_invalid_fname(
26058 char_u **fname,
26059 char_u **bufp,
26060 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026061{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026062 char_u *short_fname, *save_fname, *pbuf_unused;
26063 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026064 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026065 int old_len, len;
26066 int new_len, sfx_len;
26067 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068
26069 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026070 old_len = *fnamelen;
26071 save_fname = vim_strnsave(*fname, old_len);
26072 pbuf_unused = NULL;
26073 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026074
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026075 endp = save_fname + old_len - 1; /* Find the end of the copy */
26076 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026078 /*
26079 * Try shortening the supplied path till it succeeds by removing one
26080 * directory at a time from the tail of the path.
26081 */
26082 len = 0;
26083 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026085 /* go back one path-separator */
26086 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26087 --endp;
26088 if (endp <= save_fname)
26089 break; /* processed the complete path */
26090
26091 /*
26092 * Replace the path separator with a NUL and try to shorten the
26093 * resulting path.
26094 */
26095 ch = *endp;
26096 *endp = 0;
26097 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026098 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026099 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26100 {
26101 retval = FAIL;
26102 goto theend;
26103 }
26104 *endp = ch; /* preserve the string */
26105
26106 if (len > 0)
26107 break; /* successfully shortened the path */
26108
26109 /* failed to shorten the path. Skip the path separator */
26110 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111 }
26112
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026113 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026114 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026115 /*
26116 * Succeeded in shortening the path. Now concatenate the shortened
26117 * path with the remaining path at the tail.
26118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026119
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026120 /* Compute the length of the new path. */
26121 sfx_len = (int)(save_endp - endp) + 1;
26122 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026123
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026124 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026125 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026126 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026127 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026128 /* There is not enough space in the currently allocated string,
26129 * copy it to a buffer big enough. */
26130 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026132 {
26133 retval = FAIL;
26134 goto theend;
26135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026136 }
26137 else
26138 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026139 /* Transfer short_fname to the main buffer (it's big enough),
26140 * unless get_short_pathname() did its work in-place. */
26141 *fname = *bufp = save_fname;
26142 if (short_fname != save_fname)
26143 vim_strncpy(save_fname, short_fname, len);
26144 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026145 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026146
26147 /* concat the not-shortened part of the path */
26148 vim_strncpy(*fname + len, endp, sfx_len);
26149 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026150 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026151
26152theend:
26153 vim_free(pbuf_unused);
26154 vim_free(save_fname);
26155
26156 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026157}
26158
26159/*
26160 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026161 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026162 */
26163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026164shortpath_for_partial(
26165 char_u **fnamep,
26166 char_u **bufp,
26167 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026168{
26169 int sepcount, len, tflen;
26170 char_u *p;
26171 char_u *pbuf, *tfname;
26172 int hasTilde;
26173
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026174 /* Count up the path separators from the RHS.. so we know which part
26175 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026176 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026177 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026178 if (vim_ispathsep(*p))
26179 ++sepcount;
26180
26181 /* Need full path first (use expand_env() to remove a "~/") */
26182 hasTilde = (**fnamep == '~');
26183 if (hasTilde)
26184 pbuf = tfname = expand_env_save(*fnamep);
26185 else
26186 pbuf = tfname = FullName_save(*fnamep, FALSE);
26187
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026188 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026189
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026190 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192
26193 if (len == 0)
26194 {
26195 /* Don't have a valid filename, so shorten the rest of the
26196 * path if we can. This CAN give us invalid 8.3 filenames, but
26197 * there's not a lot of point in guessing what it might be.
26198 */
26199 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026200 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26201 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026202 }
26203
26204 /* Count the paths backward to find the beginning of the desired string. */
26205 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026206 {
26207#ifdef FEAT_MBYTE
26208 if (has_mbyte)
26209 p -= mb_head_off(tfname, p);
26210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026211 if (vim_ispathsep(*p))
26212 {
26213 if (sepcount == 0 || (hasTilde && sepcount == 1))
26214 break;
26215 else
26216 sepcount --;
26217 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026219 if (hasTilde)
26220 {
26221 --p;
26222 if (p >= tfname)
26223 *p = '~';
26224 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026225 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026226 }
26227 else
26228 ++p;
26229
26230 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26231 vim_free(*bufp);
26232 *fnamelen = (int)STRLEN(p);
26233 *bufp = pbuf;
26234 *fnamep = p;
26235
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026236 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026237}
26238#endif /* WIN3264 */
26239
26240/*
26241 * Adjust a filename, according to a string of modifiers.
26242 * *fnamep must be NUL terminated when called. When returning, the length is
26243 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026244 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026245 * When there is an error, *fnamep is set to NULL.
26246 */
26247 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026248modify_fname(
26249 char_u *src, /* string with modifiers */
26250 int *usedlen, /* characters after src that are used */
26251 char_u **fnamep, /* file name so far */
26252 char_u **bufp, /* buffer for allocated file name or NULL */
26253 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026254{
26255 int valid = 0;
26256 char_u *tail;
26257 char_u *s, *p, *pbuf;
26258 char_u dirname[MAXPATHL];
26259 int c;
26260 int has_fullname = 0;
26261#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026262 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026263 int has_shortname = 0;
26264#endif
26265
26266repeat:
26267 /* ":p" - full path/file_name */
26268 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26269 {
26270 has_fullname = 1;
26271
26272 valid |= VALID_PATH;
26273 *usedlen += 2;
26274
26275 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26276 if ((*fnamep)[0] == '~'
26277#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26278 && ((*fnamep)[1] == '/'
26279# ifdef BACKSLASH_IN_FILENAME
26280 || (*fnamep)[1] == '\\'
26281# endif
26282 || (*fnamep)[1] == NUL)
26283
26284#endif
26285 )
26286 {
26287 *fnamep = expand_env_save(*fnamep);
26288 vim_free(*bufp); /* free any allocated file name */
26289 *bufp = *fnamep;
26290 if (*fnamep == NULL)
26291 return -1;
26292 }
26293
26294 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026295 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026296 {
26297 if (vim_ispathsep(*p)
26298 && p[1] == '.'
26299 && (p[2] == NUL
26300 || vim_ispathsep(p[2])
26301 || (p[2] == '.'
26302 && (p[3] == NUL || vim_ispathsep(p[3])))))
26303 break;
26304 }
26305
26306 /* FullName_save() is slow, don't use it when not needed. */
26307 if (*p != NUL || !vim_isAbsName(*fnamep))
26308 {
26309 *fnamep = FullName_save(*fnamep, *p != NUL);
26310 vim_free(*bufp); /* free any allocated file name */
26311 *bufp = *fnamep;
26312 if (*fnamep == NULL)
26313 return -1;
26314 }
26315
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026316#ifdef WIN3264
26317# if _WIN32_WINNT >= 0x0500
26318 if (vim_strchr(*fnamep, '~') != NULL)
26319 {
26320 /* Expand 8.3 filename to full path. Needed to make sure the same
26321 * file does not have two different names.
26322 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26323 p = alloc(_MAX_PATH + 1);
26324 if (p != NULL)
26325 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026326 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026327 {
26328 vim_free(*bufp);
26329 *bufp = *fnamep = p;
26330 }
26331 else
26332 vim_free(p);
26333 }
26334 }
26335# endif
26336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026337 /* Append a path separator to a directory. */
26338 if (mch_isdir(*fnamep))
26339 {
26340 /* Make room for one or two extra characters. */
26341 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26342 vim_free(*bufp); /* free any allocated file name */
26343 *bufp = *fnamep;
26344 if (*fnamep == NULL)
26345 return -1;
26346 add_pathsep(*fnamep);
26347 }
26348 }
26349
26350 /* ":." - path relative to the current directory */
26351 /* ":~" - path relative to the home directory */
26352 /* ":8" - shortname path - postponed till after */
26353 while (src[*usedlen] == ':'
26354 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26355 {
26356 *usedlen += 2;
26357 if (c == '8')
26358 {
26359#ifdef WIN3264
26360 has_shortname = 1; /* Postpone this. */
26361#endif
26362 continue;
26363 }
26364 pbuf = NULL;
26365 /* Need full path first (use expand_env() to remove a "~/") */
26366 if (!has_fullname)
26367 {
26368 if (c == '.' && **fnamep == '~')
26369 p = pbuf = expand_env_save(*fnamep);
26370 else
26371 p = pbuf = FullName_save(*fnamep, FALSE);
26372 }
26373 else
26374 p = *fnamep;
26375
26376 has_fullname = 0;
26377
26378 if (p != NULL)
26379 {
26380 if (c == '.')
26381 {
26382 mch_dirname(dirname, MAXPATHL);
26383 s = shorten_fname(p, dirname);
26384 if (s != NULL)
26385 {
26386 *fnamep = s;
26387 if (pbuf != NULL)
26388 {
26389 vim_free(*bufp); /* free any allocated file name */
26390 *bufp = pbuf;
26391 pbuf = NULL;
26392 }
26393 }
26394 }
26395 else
26396 {
26397 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26398 /* Only replace it when it starts with '~' */
26399 if (*dirname == '~')
26400 {
26401 s = vim_strsave(dirname);
26402 if (s != NULL)
26403 {
26404 *fnamep = s;
26405 vim_free(*bufp);
26406 *bufp = s;
26407 }
26408 }
26409 }
26410 vim_free(pbuf);
26411 }
26412 }
26413
26414 tail = gettail(*fnamep);
26415 *fnamelen = (int)STRLEN(*fnamep);
26416
26417 /* ":h" - head, remove "/file_name", can be repeated */
26418 /* Don't remove the first "/" or "c:\" */
26419 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26420 {
26421 valid |= VALID_HEAD;
26422 *usedlen += 2;
26423 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026424 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026425 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 *fnamelen = (int)(tail - *fnamep);
26427#ifdef VMS
26428 if (*fnamelen > 0)
26429 *fnamelen += 1; /* the path separator is part of the path */
26430#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026431 if (*fnamelen == 0)
26432 {
26433 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26434 p = vim_strsave((char_u *)".");
26435 if (p == NULL)
26436 return -1;
26437 vim_free(*bufp);
26438 *bufp = *fnamep = tail = p;
26439 *fnamelen = 1;
26440 }
26441 else
26442 {
26443 while (tail > s && !after_pathsep(s, tail))
26444 mb_ptr_back(*fnamep, tail);
26445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446 }
26447
26448 /* ":8" - shortname */
26449 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26450 {
26451 *usedlen += 2;
26452#ifdef WIN3264
26453 has_shortname = 1;
26454#endif
26455 }
26456
26457#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026458 /*
26459 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460 */
26461 if (has_shortname)
26462 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026463 /* Copy the string if it is shortened by :h and when it wasn't copied
26464 * yet, because we are going to change it in place. Avoids changing
26465 * the buffer name for "%:8". */
26466 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026467 {
26468 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026469 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026470 return -1;
26471 vim_free(*bufp);
26472 *bufp = *fnamep = p;
26473 }
26474
26475 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026476 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026477 if (!has_fullname && !vim_isAbsName(*fnamep))
26478 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026479 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026480 return -1;
26481 }
26482 else
26483 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026484 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026485
Bram Moolenaardc935552011-08-17 15:23:23 +020026486 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026487 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026488 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026489 return -1;
26490
26491 if (l == 0)
26492 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026493 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026494 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026495 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026496 return -1;
26497 }
26498 *fnamelen = l;
26499 }
26500 }
26501#endif /* WIN3264 */
26502
26503 /* ":t" - tail, just the basename */
26504 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26505 {
26506 *usedlen += 2;
26507 *fnamelen -= (int)(tail - *fnamep);
26508 *fnamep = tail;
26509 }
26510
26511 /* ":e" - extension, can be repeated */
26512 /* ":r" - root, without extension, can be repeated */
26513 while (src[*usedlen] == ':'
26514 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26515 {
26516 /* find a '.' in the tail:
26517 * - for second :e: before the current fname
26518 * - otherwise: The last '.'
26519 */
26520 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26521 s = *fnamep - 2;
26522 else
26523 s = *fnamep + *fnamelen - 1;
26524 for ( ; s > tail; --s)
26525 if (s[0] == '.')
26526 break;
26527 if (src[*usedlen + 1] == 'e') /* :e */
26528 {
26529 if (s > tail)
26530 {
26531 *fnamelen += (int)(*fnamep - (s + 1));
26532 *fnamep = s + 1;
26533#ifdef VMS
26534 /* cut version from the extension */
26535 s = *fnamep + *fnamelen - 1;
26536 for ( ; s > *fnamep; --s)
26537 if (s[0] == ';')
26538 break;
26539 if (s > *fnamep)
26540 *fnamelen = s - *fnamep;
26541#endif
26542 }
26543 else if (*fnamep <= tail)
26544 *fnamelen = 0;
26545 }
26546 else /* :r */
26547 {
26548 if (s > tail) /* remove one extension */
26549 *fnamelen = (int)(s - *fnamep);
26550 }
26551 *usedlen += 2;
26552 }
26553
26554 /* ":s?pat?foo?" - substitute */
26555 /* ":gs?pat?foo?" - global substitute */
26556 if (src[*usedlen] == ':'
26557 && (src[*usedlen + 1] == 's'
26558 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26559 {
26560 char_u *str;
26561 char_u *pat;
26562 char_u *sub;
26563 int sep;
26564 char_u *flags;
26565 int didit = FALSE;
26566
26567 flags = (char_u *)"";
26568 s = src + *usedlen + 2;
26569 if (src[*usedlen + 1] == 'g')
26570 {
26571 flags = (char_u *)"g";
26572 ++s;
26573 }
26574
26575 sep = *s++;
26576 if (sep)
26577 {
26578 /* find end of pattern */
26579 p = vim_strchr(s, sep);
26580 if (p != NULL)
26581 {
26582 pat = vim_strnsave(s, (int)(p - s));
26583 if (pat != NULL)
26584 {
26585 s = p + 1;
26586 /* find end of substitution */
26587 p = vim_strchr(s, sep);
26588 if (p != NULL)
26589 {
26590 sub = vim_strnsave(s, (int)(p - s));
26591 str = vim_strnsave(*fnamep, *fnamelen);
26592 if (sub != NULL && str != NULL)
26593 {
26594 *usedlen = (int)(p + 1 - src);
26595 s = do_string_sub(str, pat, sub, flags);
26596 if (s != NULL)
26597 {
26598 *fnamep = s;
26599 *fnamelen = (int)STRLEN(s);
26600 vim_free(*bufp);
26601 *bufp = s;
26602 didit = TRUE;
26603 }
26604 }
26605 vim_free(sub);
26606 vim_free(str);
26607 }
26608 vim_free(pat);
26609 }
26610 }
26611 /* after using ":s", repeat all the modifiers */
26612 if (didit)
26613 goto repeat;
26614 }
26615 }
26616
Bram Moolenaar26df0922014-02-23 23:39:13 +010026617 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26618 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026619 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026620 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026621 if (c != NUL)
26622 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026623 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026624 if (c != NUL)
26625 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026626 if (p == NULL)
26627 return -1;
26628 vim_free(*bufp);
26629 *bufp = *fnamep = p;
26630 *fnamelen = (int)STRLEN(p);
26631 *usedlen += 2;
26632 }
26633
Bram Moolenaar071d4272004-06-13 20:20:40 +000026634 return valid;
26635}
26636
26637/*
26638 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26639 * "flags" can be "g" to do a global substitute.
26640 * Returns an allocated string, NULL for error.
26641 */
26642 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026643do_string_sub(
26644 char_u *str,
26645 char_u *pat,
26646 char_u *sub,
26647 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026648{
26649 int sublen;
26650 regmatch_T regmatch;
26651 int i;
26652 int do_all;
26653 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026654 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026655 garray_T ga;
26656 char_u *ret;
26657 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026658 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026659
26660 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26661 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026662 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026663
26664 ga_init2(&ga, 1, 200);
26665
26666 do_all = (flags[0] == 'g');
26667
26668 regmatch.rm_ic = p_ic;
26669 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26670 if (regmatch.regprog != NULL)
26671 {
26672 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026673 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026674 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26675 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026676 /* Skip empty match except for first match. */
26677 if (regmatch.startp[0] == regmatch.endp[0])
26678 {
26679 if (zero_width == regmatch.startp[0])
26680 {
26681 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026682 i = MB_PTR2LEN(tail);
26683 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26684 (size_t)i);
26685 ga.ga_len += i;
26686 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026687 continue;
26688 }
26689 zero_width = regmatch.startp[0];
26690 }
26691
Bram Moolenaar071d4272004-06-13 20:20:40 +000026692 /*
26693 * Get some space for a temporary buffer to do the substitution
26694 * into. It will contain:
26695 * - The text up to where the match is.
26696 * - The substituted text.
26697 * - The text after the match.
26698 */
26699 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026700 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026701 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26702 {
26703 ga_clear(&ga);
26704 break;
26705 }
26706
26707 /* copy the text up to where the match is */
26708 i = (int)(regmatch.startp[0] - tail);
26709 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26710 /* add the substituted text */
26711 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26712 + ga.ga_len + i, TRUE, TRUE, FALSE);
26713 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026714 tail = regmatch.endp[0];
26715 if (*tail == NUL)
26716 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026717 if (!do_all)
26718 break;
26719 }
26720
26721 if (ga.ga_data != NULL)
26722 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26723
Bram Moolenaar473de612013-06-08 18:19:48 +020026724 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026725 }
26726
26727 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26728 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026729 if (p_cpo == empty_option)
26730 p_cpo = save_cpo;
26731 else
26732 /* Darn, evaluating {sub} expression changed the value. */
26733 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026734
26735 return ret;
26736}
26737
26738#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */